forked from josephhughes/Sequence-manipulation
-
Notifications
You must be signed in to change notification settings - Fork 2
/
CountSynNonSyn.pl
234 lines (226 loc) · 6.54 KB
/
CountSynNonSyn.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/perl -w
# A perlscript written by Joseph Hughes, University of Glasgow
# use this perl script to count the number of nonsynonynous and synonymous mutations
# per sequence in an aligned multi-fasta file and the number of substitutions per sequence
# compared to a reference sequence
# Plots the distribution counts to the terminal and the data to a file
# specify whether it is DNA or RNA sequences
use strict;
use Getopt::Long;
use Bio::SeqIO;
my ($infasta,$out,$code,$help,$inref);
&GetOptions(
'in:s' => \$infasta,#aligned multi-fastafile or a single sequence
'out:s' => \$out,#printing output data
'ref:s' => \$inref, #reference sequence
"code:s" => \$code, # genetic code
"help" => \$help, # provides help with usage
);
if (($help)&&!($help)||!($infasta)||!($out)||!($code)||!($inref)){
print "Usage : CountSynNonSyn.pl <list of arguments>\n";
print " -in <txt> - aligned according to reference input file in fasta format, sequences must start with the first codon position\n";
print " -out <txt> - the name of the output file for the data\n";
print " -code <dna/rna> - a parameter to specify the genetic code to use dna or rna.\n";
print " -help - Get this help\n";
exit();
}
my (%genetic_code,@refcodon_list,%nonsyn,%syn,%nuc);
print "You have selected the $code genetic code\n";
%genetic_code=getCode($code);
my $ref = Bio::SeqIO->new(-file => "$inref" ,
-format => 'fasta');
while (my $refseq = $ref->next_seq()){
my $refseq_str=uc($refseq->seq);
#@refcodon_list = split (/(\S{3})/,$refseq_str);
@refcodon_list = $refseq_str =~ /(\S{3})/g;
}
my $in = Bio::SeqIO->new(-file => "$infasta" ,
-format => 'fasta');
open (OUT, ">$out")||die "Can't open output $out\n";
while (my $seq = $in->next_seq()){
my $seq_str=uc($seq->seq);
#print "$seq_str\n";
my $nonsyncount=0;
my $syncount=0;
my $nuccount=0;
#my @codon_list = split (/(\S{3})/,$seq_str);
my @codon_list = $seq_str =~ /(\S{3})/g;
for (my $i=0; $i<scalar(@codon_list); $i++){
#print "$codon_list[$i] $refcodon_list[$i]\n";
# print "test ".$genetic_code{$codon_list[$i]}." test ".$genetic_code{$refcodon_list[$i]}."\n";
my @refsite=split(//,$refcodon_list[$i]);
my @site=split(//,$codon_list[$i]);
my $codoncount=0;
for (my $j=0; $j<scalar(@refsite); $j++){
if ($site[$j] ne $refsite[$j]){
$nuccount++;
$codoncount++;
}
}
if ($genetic_code{$codon_list[$i]} ne $genetic_code{$refcodon_list[$i]}){
$nonsyncount=$nonsyncount+$codoncount;
}elsif ($genetic_code{$codon_list[$i]} eq $genetic_code{$refcodon_list[$i]}){
$syncount=$syncount+$codoncount;
}
}
$nonsyn{$nonsyncount}++;
if ($nonsyncount>4){
my $seq_id=$seq->id();
print "$seq_id has $nonsyncount nonsynonymous mutations\n";
}
$syn{$syncount}++;
$nuc{$nuccount}++;
}
print "Number of sequences with x synonymous mutations\n";
for my $nbsyn (keys %syn){
print "$nbsyn $syn{$nbsyn}\n";
}
print "Number of sequences with x nonsynonymous mutations\n";
for my $nbnonsyn (keys %nonsyn){
print "$nbnonsyn $nonsyn{$nbnonsyn}\n";
}
print "Number of sequences with x nucleotide mutations\n";
for my $nbnuc (keys %nuc){
print "$nbnuc $nuc{$nbnuc}\n";
}
sub getCode {
my $code_type=$_[0];
print "$code_type in subroutine\n";
my %code;
if($code_type=~/rna/){
(%code) = (
'UCA' => 'S', # Serine
'UCC' => 'S', # Serine
'UCG' => 'S', # Serine
'UCU' => 'S', # Serine
'UUC' => 'F', # Phenylalanine
'UUU' => 'F', # Phenylalanine
'UUA' => 'L', # Leucine
'UUG' => 'L', # Leucine
'UAC' => 'Y', # Tyrosine
'UAU' => 'Y', # Tyrosine
'UAA' => '*', # Stop
'UAG' => '*', # Stop
'UGC' => 'C', # Cysteine
'UGU' => 'C', # Cysteine
'UGA' => '*', # Stop
'UGG' => 'W', # Tryptophan
'CUA' => 'L', # Leucine
'CUC' => 'L', # Leucine
'CUG' => 'L', # Leucine
'CUU' => 'L', # Leucine
'CCA' => 'P', # Proline
'CAU' => 'H', # Histidine
'CAA' => 'Q', # Glutamine
'CAG' => 'Q', # Glutamine
'CGA' => 'R', # Arginine
'CGC' => 'R', # Arginine
'CGG' => 'R', # Arginine
'CGU' => 'R', # Arginine
'AUA' => 'I', # Isoleucine
'AUC' => 'I', # Isoleucine
'AUU' => 'I', # Isoleucine
'AUG' => 'M', # Methionine
'ACA' => 'T', # Threonine
'ACC' => 'T', # Threonine
'ACG' => 'T', # Threonine
'ACU' => 'T', # Threonine
'AAC' => 'N', # Asparagine
'AAU' => 'N', # Asparagine
'AAA' => 'K', # Lysine
'AAG' => 'K', # Lysine
'AGC' => 'S', # Serine
'AGU' => 'S', # Serine
'AGA' => 'R', # Arginine
'AGG' => 'R', # Arginine
'CCC' => 'P', # Proline
'CCG' => 'P', # Proline
'CCU' => 'P', # Proline
'CAC' => 'H', # Histidine
'GUA' => 'V', # Valine
'GUC' => 'V', # Valine
'GUG' => 'V', # Valine
'GUU' => 'V', # Valine
'GCA' => 'A', # Alanine
'GCC' => 'A', # Alanine
'GCG' => 'A', # Alanine
'GCU' => 'A', # Alanine
'GAC' => 'D', # Aspartic Acid
'GAU' => 'D', # Aspartic Acid
'GAA' => 'E', # Glutamic Acid
'GAG' => 'E', # Glutamic Acid
'GGA' => 'G', # Glycine
'GGC' => 'G', # Glycine
'GGG' => 'G', # Glycine
'GGU' => 'G' # Glycine
);
}elsif ($code_type=~/dna/){
(%code) = (
'TCA' => 'S', # Serine
'TCC' => 'S', # Serine
'TCG' => 'S', # Serine
'TCT' => 'S', # Serine
'TTC' => 'F', # Phenylalanine
'TTT' => 'F', # Phenylalanine
'TTA' => 'L', # LeTcine
'TTG' => 'L', # LeTcine
'TAC' => 'Y', # Tyrosine
'TAT' => 'Y', # Tyrosine
'TAA' => '*', # Stop
'TAG' => '*', # Stop
'TGC' => 'C', # Cysteine
'TGT' => 'C', # Cysteine
'TGA' => '*', # Stop
'TGG' => 'W', # Tryptophan
'CTA' => 'L', # LeTcine
'CTC' => 'L', # LeTcine
'CTG' => 'L', # LeTcine
'CTT' => 'L', # LeTcine
'CCA' => 'P', # Proline
'CAT' => 'H', # Histidine
'CAA' => 'Q', # GlTtamine
'CAG' => 'Q', # GlTtamine
'CGA' => 'R', # Arginine
'CGC' => 'R', # Arginine
'CGG' => 'R', # Arginine
'CGT' => 'R', # Arginine
'ATA' => 'I', # IsoleTcine
'ATC' => 'I', # IsoleTcine
'ATT' => 'I', # IsoleTcine
'ATG' => 'M', # Methionine
'ACA' => 'T', # Threonine
'ACC' => 'T', # Threonine
'ACG' => 'T', # Threonine
'ACT' => 'T', # Threonine
'AAC' => 'N', # Asparagine
'AAT' => 'N', # Asparagine
'AAA' => 'K', # Lysine
'AAG' => 'K', # Lysine
'AGC' => 'S', # Serine
'AGT' => 'S', # Serine
'AGA' => 'R', # Arginine
'AGG' => 'R', # Arginine
'CCC' => 'P', # Proline
'CCG' => 'P', # Proline
'CCT' => 'P', # Proline
'CAC' => 'H', # Histidine
'GTA' => 'V', # Valine
'GTC' => 'V', # Valine
'GTG' => 'V', # Valine
'GTT' => 'V', # Valine
'GCA' => 'A', # Alanine
'GCC' => 'A', # Alanine
'GCG' => 'A', # Alanine
'GCT' => 'A', # Alanine
'GAC' => 'D', # Aspartic Acid
'GAT' => 'D', # Aspartic Acid
'GAA' => 'E', # GlTtamic Acid
'GAG' => 'E', # GlTtamic Acid
'GGA' => 'G', # Glycine
'GGC' => 'G', # Glycine
'GGG' => 'G', # Glycine
'GGT' => 'G' # Glycine
);
}
return %code;
}