-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathgetMutationType.pl
More file actions
executable file
·168 lines (133 loc) · 3.72 KB
/
getMutationType.pl
File metadata and controls
executable file
·168 lines (133 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env perl
#
use strict;
use warnings;
use Getopt::Long;
use File::Basename;
use Carp;
use Bed;
use Sequence;
my $prog=basename($0);
my $cmdDir=dirname($0);
my $selectType = "";
my $substType = "";
my $summaryFile = "";
my $verbose = 0;
GetOptions ('t|mutationType:s'=>\$selectType,
'subt:s'=>\$substType,
'summary:s'=>\$summaryFile,
'v'=>\$verbose);
if (@ARGV != 2 && @ARGV != 1)
{
print "Get specific types of mutations\n";
print "usage: $prog [options] <tag.mutation.txt> [tag.mutation.type.bed]\n";
print " <tag.mutation.txt> : input mutation file\n";
print " [tag.mutation.type.bed] : output mutations of selected type (if not provided, only summary will be printed\n";
print "options:\n";
print " -t [string]: the type of mutations (del|ins|sub)\n";
print " --subt [string]: substitution type (e.g., t2c, effective only when using -t sub; all substitutions when not specified)\n";
print " --summary [string]: print summary statistics to file\n";
print " -v : verbose\n";
exit (1);
}
my ($mutationFile, $outBedFile) = @ARGV;
my %summaryHash;
#if ($selectType ne 'del' && $selectType ne 'ins' && $selectType ne 'sub' && $selectType ne 't2c' && $selectType ne 'c2t')
if ($selectType ne 'del' && $selectType ne 'ins' && $selectType ne 'sub' && $selectType ne '')
{
Carp::croak "Wrong type of mutation \n";
}
my ($substFrom, $substTo);
if ($selectType eq 'sub')
{
if ($substType ne '')
{
my $tmp;
($substFrom, $tmp, $substTo) =split (//, $substType);
$substFrom = uc($substFrom);
$substTo = uc ($substTo);
$substFrom = 'T' if $substFrom eq 'U';
$substTo = 'T' if $substTo eq 'U';
Carp::croak "wrong substitution pattern: $substType\n" unless $substFrom=~/[ACGT]/ && $substTo=~/[ACGT]/ && $substFrom ne $substTo;
}
}
my ($fin, $fout);
open ($fin, "<$mutationFile") || Carp::croak "cannot open $mutationFile to read\n";
if (defined $outBedFile && $outBedFile ne '')
{
open ($fout, ">$outBedFile") || Carp::croak "cannot open $outBedFile to write\n";
}
my $iter = 0;
while (my $line = <$fin>)
{
chomp $line;
next if $line =~/^\s*$/;
print "$iter ...\n" if $verbose && $iter % 100000 == 0;
$iter++;
my @cols = split (/\s+/, $line);
my $strand = $cols[5];
my ($from, $type, $to) = @cols [7..9];
if ($type eq '-')
{
$summaryHash{'del'}++;
if (defined $outBedFile && $outBedFile ne '')
{
print $fout join ("\t", @cols[0..5]), "\n" if $selectType eq 'del';
}
}
elsif ($type eq '+')
{
$summaryHash{'ins'}++;
if (defined $outBedFile && $outBedFile ne '')
{
print $fout join ("\t", @cols[0..5]), "\n" if $selectType eq 'ins';
}
}
elsif ($type eq '>')
{
if ($strand eq '-')
{
$from = uc (revcom ($from));
$to = uc (revcom ($to));
}
else
{
$from = uc ($from);
$to = uc ($to);
}
$summaryHash{'sub'}{"$from-$to"}++;
$summaryHash{'sub'}{'total'}++;
if ($selectType eq 'sub')
{
if ($substType ne '')
{
next unless $from eq $substFrom && $to eq $substTo;
}
if (defined $outBedFile && $outBedFile ne '')
{
print $fout join ("\t", @cols[0..5]), "\n";
}
}
}
}
close ($fin);
if (defined $outBedFile && $outBedFile ne '')
{
close ($fout);
}
if ($summaryFile ne '')
{
open ($fout, ">$summaryFile") || Carp::croak "cannot open file $summaryFile to write\n";
print $fout "Deletion: ", exists $summaryHash{'del'} ? $summaryHash{'del'} : 0, "\n";
print $fout "Insertion: ", exists $summaryHash{'ins'} ? $summaryHash{'ins'} : 0, "\n";
if (exists $summaryHash{'sub'})
{
print $fout "Substitution: ", $summaryHash{'sub'}{'total'}, "\n";
foreach my $t (sort keys %{$summaryHash{'sub'}})
{
next if $t eq 'total';
print $fout "$t: ", $summaryHash{'sub'}{$t}, "\n";
}
}
close ($fout);
}