-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathidentify_nonsynonymous_mutations.pl
executable file
·194 lines (176 loc) · 4.24 KB
/
identify_nonsynonymous_mutations.pl
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/perl
#Taken from Gavin's LOH Analysis
use strict;
use Data::Dumper;
use Bio::Seq;
if(@ARGV != 2){
exit(1);
}
my $in = $ARGV[0];
my $out = $ARGV[1];
my %lookup;
my %error_count; #by gene, for debugging purposes
my %grantham_distances;
load_ambig();
load_grantham();
open(IN, "$in") or exit(2);
open(OUT, ">$out") or exit(3);
while(<IN>){
chomp;
print OUT;
my @cols = split /\s+/;
my $gene = $cols[1];
my $strand = $cols[2];
my $codon = $cols[3];
unless ($codon =~ /[ACTG]{3}/){
print OUT "\n";
next;
}
my $codon_pos = $cols[4];
my $ref = uc($cols[5]);
my $amb = uc($cols[6]);
my $mut = uc(amb_lookup($amb,$ref,$cols[0]));
next if $mut == -1;
my $mut_cor = $mut;
my $ref_cor = $ref;
if($strand < 0){
$mut_cor =~ tr/ACTG/TGAC/;
$ref_cor =~ tr/ACTG/TGAC/;
}
my @three = split //, $codon;
#sanity check
my $codon_ind = $codon_pos -1;
my $orig = uc($three[$codon_ind]);
if($orig ne $ref_cor){
print STDERR "$cols[0]\tERROR, base $ref is not the same as codon position $codon_pos ($orig)\n";
$error_count{$gene}++;
# next;
}
$three[$codon_ind] = $mut_cor;
my $new_codon = join "", @three;
print OUT " $codon $new_codon ";
my $old = Bio::Seq->new(-seq=>$codon);
my $old_aa = $old->translate;
my $new = Bio::Seq->new(-seq=>$new_codon);
my $new_aa = $new->translate;
my $old_aa_base = $old_aa->seq;
my $new_aa_base = $new_aa->seq;
my $type = "CODING";
if($old_aa_base eq $new_aa_base){
$type = "SYNONYMOUS";
}
my ($charge_effect,$polarity_effect) = rad_cons($old_aa_base,$new_aa_base);
my $gdist = $grantham_distances{$old_aa_base}{$new_aa_base};
if ($gdist eq '') {
$gdist = "-";
}
print OUT "$old_aa_base $new_aa_base $type $charge_effect $polarity_effect $gdist\n";
}
close IN;
close OUT;
#print Dumper %error_count;
sub amb_lookup{
my $amb = shift;
my $ref = shift;
my $pos = shift;
if($amb =~ /[ACTG]/){
#print "$amb\n";
return($amb);
}
#print "LOOKUP: $amb\n";
my @all = @{$lookup{$amb}};
#print Dumper @all;
my @some;
for(@all){
next if $_ eq $ref;
push @some, $_;
}
if(@some > 1){
#print Dumper @some;
print STDERR "$pos\t$ref\t$amb\ttwo bases at this position, neither is reference\n";
return(-1);
}
return($some[0]);
}
sub load_grantham{
my $matrix_file = "/share/data/rmorin/data/grantham_matrix.txt";
my @col_names = split //, "ARNDCQEGHILKMFPSTWYV";
open M, $matrix_file or die "$!\n";
my $i = 0;
while(<M>){
next if /^\#/;
my @fields = split /\s+/;
shift @fields;
for (my $j = 0;$j < @fields;$j++){
$fields[$j] =~ s/\.//;
#print "d($col_names[$i],$col_names[$j]) = $fields[$j]\n";
$grantham_distances{$col_names[$i]}{$col_names[$j]} = $fields[$j];
$grantham_distances{$col_names[$j]}{$col_names[$i]} = $fields[$j];
}
$i++;
}
}
sub rad_cons{
my $before = shift;
my $after = shift;
my @pos = qw(R H K);
my @neg = qw(D E);
my @neutral = qw(A N C Q G I L M F P S T W Y V);
my %charges;
for(@pos){
$charges{$_} = "POS";
}
for(@neg){
$charges{$_} = "NEG";
}
for(@neutral){
$charges{$_} = "NEUTRAL";
}
my %polarity;
for(qw(C)){
$polarity{$_} = "SPECIAL";
}
for(qw(A G P S T)){
$polarity{$_} = "SMALL_NEUTRAL";
}
for(qw(N D Q E)){
$polarity{$_} = "SMALL_POLAR";
}
for(qw(R H K)){
$polarity{$_} = "LARGE_POLAR";
}
for(qw(I L M V)){
$polarity{$_} = "SMALL_NONPOLAR";
}
for(qw(F W Y)){
$polarity{$_} = "LARGE_NONPOLAR";
}
my $before_charge = $charges{$before};
my $after_charge = $charges{$after};
my $charge_change = "CONSERVATIVE";
if($before_charge ne $after_charge){
$charge_change = "RADICAL";
}
my $polar_change = "CONSERVATIVE";
if($polarity{$before} ne $polarity{$after}){
$polar_change = "RADICAL";
}
return($charge_change,$polar_change);
}
sub load_ambig{
add('M',qw(A C));
add('R',qw(A G));
add('W',qw(A T));
add('S',qw(C G));
add('Y',qw(T C));
add('K',qw(T G));
add('V',qw(A C G));
add('H',qw(A C T));
add('D',qw(A G T));
add('B',qw(C G T));
}
sub add{
my $amb = shift;
my @copy = @_;
$lookup{$amb} = \@copy;
}