-
Notifications
You must be signed in to change notification settings - Fork 7
/
dna_protein_subs.pl
386 lines (333 loc) · 11.7 KB
/
dna_protein_subs.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#package DNA_PROT;
use strict;
#use Exporter;
#our @EXPORT_OK = qw ( chr_is_mito);
use POSIX;
my %codons = ('TTT' => 'F', 'TCT' => 'S', 'TAT' => 'Y', 'TGT' => 'C', 'TTC' => 'F', 'TCC' => 'S', 'TAC' => 'Y', 'TGC' => 'C',
'TTA' => 'L', 'TCA' => 'S', 'TTG' => 'L', 'TCG' => 'S', 'TGG' => 'W', 'CTT' => 'L', 'CCT' => 'P', 'CAT' => 'H',
'CGT' => 'R', 'CTC' => 'L', 'CCC' => 'P', 'CAC' => 'H', 'CGC' => 'R', 'CTA' => 'L', 'CCA' => 'P', 'CAA' => 'Q',
'CGA' => 'R', 'CTG' => 'L', 'CCG' => 'P', 'CAG' => 'Q', 'CGG' => 'R', 'ATT' => 'I', 'ACT' => 'T', 'AAT' => 'N',
'AGT' => 'S', 'ATC' => 'I', 'ACC' => 'T', 'AAC' => 'N', 'AGC' => 'S', 'ATA' => 'I', 'ACA' => 'T', 'AAA' => 'K',
'AGA' => 'R', 'ATG' => 'M', 'ACG' => 'T', 'AAG' => 'K', 'AGG' => 'R', 'GTT' => 'V', 'GCT' => 'A', 'GAT' => 'D',
'GGT' => 'G', 'GTC' => 'V', 'GCC' => 'A', 'GAC' => 'D', 'GGC' => 'G', 'GTA' => 'V', 'GCA' => 'A', 'GAA' => 'E',
'GGA' => 'G', 'GTG' => 'V', 'GCG' => 'A', 'GAG' => 'E', 'GGG' => 'G', 'TAA' => '*', 'TAG' => '*', 'TGA' => '*');
sub chr_is_plastid {
my ($chr) = @_;
#Arabidopsis has Pt
if ($chr =~ /^P[tT]/ || $chr =~ /chrP[tT]/ || $chr =~ /chloroplast/) {
return 1;
}
return 0;
}
sub chr_is_mito {
my ($chr) = @_;
# Drosophila has "dmel_mitochondrion"
# human vertebrate has "MT"
# one organism had Mito
# C. elegans has Mt
if ($chr =~ /^M[tT]/ || $chr eq "Mito" || $chr =~ /mito/i || $chr =~ /chrM[tT]/) {
return 1;
}
return 0;
}
sub adjust_to_nonstandard_code {
my ($ncbi_code, %cod_table) = @_;
# taken from http://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi
if ($ncbi_code == 1) {
return %cod_table;
# } elsif (chr_is_mito ($chr) && $ncbi_code == 2) {
} elsif ($ncbi_code == 2) {
#vertebrate mitochondrial code
$cod_table{'AGA'} = '*';
$cod_table{'AGG'} = '*';
$cod_table{'ATA'} = 'M';
$cod_table{'TGA'} = 'W';
} elsif ($ncbi_code == 3) {
# } elsif (chr_is_mito ($chr) && $ncbi_code == 3) {
#yeast mitochondrial code
$cod_table{'ATA'} = 'M';
$cod_table{'CTT'} = 'T';
$cod_table{'CTC'} = 'T';
$cod_table{'CTA'} = 'T';
$cod_table{'CTG'} = 'T';
$cod_table{'TGA'} = 'W';
$cod_table{'CGA'} = '';
$cod_table{'CGC'} = '';
} elsif ($ncbi_code == 4) {
# } elsif (chr_is_mito ($chr) && $ncbi_code == 4) {
# Mold, Protozoan, and Coelenterate Mitochondrial Code and the Mycoplasma/Spiroplasma Code
$cod_table{'TGA'} = 'W';
# NOTE website says the following which I did not code for# :
# (I am assuming same amino acid rather than a start = M
# Alternative Initiation Codons:
# Trypanosoma: UUA, UUG, CUG
# Leishmania: AUU, AUA
# Tertrahymena: AUU, AUA, AUG
# Paramecium: AUU, AUA, AUG, AUC, GUG, GUA(?)
# Alternate start codons are still translated as Met when
# they are at the start of a protein (even if the codon encodes a different amino acid otherwise).
# To code thi properly, need to know amino acid pos = 1, and ncbi_code
# ERROR need to separate mold, protozoan, coelenterate mito from mycoplase/spiroplasma!!!!
# I checked mold, I don't think Mt is assembled right now
} elsif ($ncbi_code == 5) {
# } elsif (chr_is_mito ($chr) && $ncbi_code == 5) {
# Invertebrate Mitochondrial Code (transl_table=5)
# $cod_table{'AGG'} = ''
# the codon AGG is absent in Drosophila, but it doesn't say about other invertebrates, so leaving it in, just in case;
$cod_table{'AGA'} = 'S';
$cod_table{'AGG'} = 'S';
$cod_table{'ATA'} = 'M';
$cod_table{'TGA'} = 'W';
} elsif ($ncbi_code == 6) {
# Ciliate, Dasycladacean and Hexamita Nuclear Code
$cod_table{'TAA'} = 'Q';
$cod_table{'TAG'} = 'Q';
} elsif ($ncbi_code == 9) {
# } elsif (chr_is_mito ($chr) && $ncbi_code == 9) {
# Echinoderm and Flatworm Mitochondrial Code
$cod_table{'AAA'} = 'N';
$cod_table{'AGA'} = 'S';
$cod_table{'AGG'} = 'S';
$cod_table{'TGA'} = 'W';
} elsif ($ncbi_code == 10) {
# Euplotid Nuclear Code
$cod_table{'TGA'} = 'C';
} elsif ( $ncbi_code == 11) {
# change start position
# The Bacterial, Archaeal and Plant Plastid Code (transl_table=11)
# different initiation codons
# mixed -- I think it's the entire Bacterial & Archael genome, but just plant plastids.
# what does the NCBI file say?
} elsif ($ncbi_code == 12) {
$cod_table{'CTG'} = 'S';
} elsif ($ncbi_code == 13) {
# } elsif (chr_is_mito ($chr) && $ncbi_code == 13) {
# The Ascidian Mitochondrial Code (transl_table=13)
$cod_table{'AGA'} = 'G';
$cod_table{'AGG'} = 'G';
$cod_table{'ATA'} = 'M';
$cod_table{'TGA'} = 'W';
} elsif($ncbi_code == 14) {
# } elsif (chr_is_mito ($chr) && $ncbi_code == 14) {
# The Alternative Flatworm Mitochondrial Code (transl_table=14)
$cod_table{'AAA'} = 'N';
$cod_table{'AGA'} = 'S';
$cod_table{'TGG'} = 'S';
$cod_table{'TAA'} = 'Y';
$cod_table{'TGA'} = 'W';
} elsif ($ncbi_code == 16) {
# } elsif (chr_is_mito ($chr) && $ncbi_code == 16) {
# Chlorophycean Mitochondrial Code (transl_table=16)
$cod_table{'TAG'} = 'L';
} elsif ($ncbi_code == 21) {
# } elsif (chr_is_mito ($chr) && $ncbi_code == 21) {
# Trematode Mitochondrial Code (transl_table=21)
$cod_table{'TGA'} = 'W';
$cod_table{'ATA'} = 'M';
$cod_table{'AGA'} = 'S';
$cod_table{'AGG'} = 'S';
$cod_table{'AAA'} = 'N';
} elsif ($ncbi_code == 22) {
# } elsif (chr_is_mito ($chr) && $ncbi_code == 22) {
# 22. Scenedesmus obliquus Mitochondrial Code (transl_table=22)
$cod_table{'TCA'} = '*';
$cod_table{'TAG'} = 'L';
} elsif ($ncbi_code == 23) {
# } elsif (chr_is_mito ($chr) && $ncbi_code == 23) {
# 23. Thraustochytrium Mitochondrial Code (transl_table=23)
#start code is different
$cod_table{'TTA'} = '*';
} elsif ($ncbi_code == 24) {
# } elsif (chr_is_mito ($chr) && $ncbi_code == 24) {
# 24. Pterobranchia Mitochondrial Code (transl_table=24)
$cod_table{'AGA'} = 'S';
$cod_table{'AGG'} = 'K';
$cod_table{'TGA'} = 'W';
} elsif ($ncbi_code == 25) {
# Candidate Division SR1 and Gracilibacteria Code (transl_table=25)
$cod_table{'TGA'} = 'G';
}
return %cod_table;
}
sub dna_2_codon_2_aa {
my @dna = @_;
my $cds = join('', @dna);
my @codonArray;
my $counter = 0;
my $cds_length = scalar(@dna)/3;
if ((scalar(@dna) % 3) != '0'){
print "INVALID: CDS is not divisible by 3\n";
}
# print "CDS: $cds_length..$cds\n";
while ($cds =~ m/(\w{3})/g){ #splits coding sequence by 3 letter groups
#print "1: $1\n";
$counter ++;
push(@codonArray, $1) if ($counter == $cds_length);
push(@codonArray, $1) if (($1 !~ m/tga|taa|tag/ig) && ($counter < $cds_length)) ;
#push(@codonArray, $1);
}
# print "COD: $counter..$codonArray[0]..$codonArray[1]..$codonArray[3]\n";
my $translatedCDS = '';
my $count;
foreach my $codon (@codonArray){
#print "inside: $codon..$codons{$codon}\n";
$codon = uc($codon);
$count++;
if ($codon =~ m/tga|taa|tag/ig) {
print "STOP CODON: $count\n";
}
else{
$translatedCDS = $translatedCDS.$codons{$codon};
}
}
# print "PROT: $translatedCDS\n";
my @aa = split("",$translatedCDS);
return (\@codonArray,\@aa);
} #end dna_2_codon_2_aa
sub compare_arrays {
my ($first, $second) = @_;
no warnings; # silence spurious -w undef complaints
return 0 unless @$first == @$second;
for (my $i = 0; $i < @$first; $i++) {
return 0 if $first->[$i] ne $second->[$i];
}
return 1;
}
sub retrieve_seq_from_chr_file {
my $gz_chr_file = $_[0];
my $start_pos = $_[1]+1;
my $end_pos = $_[2];
#my ($gz_chr_file, $start_pos, $end_pos) = @_;
my $dna_seq;
my $totalbases = ($end_pos-$start_pos)+1;
#print "CHRO FILE: $gz_chr_file $start_pos..$end_pos\n";
my $seqtmp = `zcat $gz_chr_file | head -2 | tail -1`;
$_ = $seqtmp;
my $linewidth = tr/[a-zA-Z]//;
#print "CHRO FILE: $start_pos..$end_pos\n";
if ($start_pos < 0) {$start_pos = 0;}
if ($linewidth == 0) { return ""; }
my $linestart = floor ($start_pos / $linewidth) + 1; # add 1 to take
# into account the header > line;
if ($start_pos % $linewidth == 0) {
$linestart = $linestart - 1;
}
my $numlines = ceil ($totalbases / $linewidth) + 1;
$linestart += $numlines;
my @seq = `zcat $gz_chr_file | head -$linestart | tail -$numlines`;
my $basestart = $start_pos % $linewidth;
if ($basestart == 0) { $basestart = $linewidth;}
my $basecount = 0; my $line = 0;
while ($basestart <= $linewidth && $basecount < $totalbases) {
$dna_seq .= substr ($seq[$line], $basestart - 1, 1);
$basestart++;
$basecount++;
}
$line++;
while ( $basecount < $totalbases){
if ($basecount + $linewidth <= $totalbases) {
chomp ($seq[$line]);
$dna_seq .= $seq[$line];
$basecount += $linewidth;
$line++;
} else {
my $index = 0;
while ($basecount < $totalbases) {
$dna_seq .= substr($seq[$line], $index, 1);
$index++;
$basecount++;
}
}
}
return $dna_seq;
}
sub trim_last_codon_if_stop {
my ($seq1_ref, $seq2_ref) = @_;
my $seq1 = $$seq1_ref;
my $seq2 = $$seq2_ref;
$_ = $seq1;
my $length = tr/A-Za-z//;
my $seq1lastcodon = substr ($seq1, $length - 3, 3);
my $seq2lastcodon = substr ($seq2, $length - 3, 3);
$seq1lastcodon = uc ($seq1lastcodon);
$seq2lastcodon = uc ($seq2lastcodon);
# print "last codon is $seq1lastcodon $seq2lastcodon\n";
if ($seq1lastcodon eq "TGA" || $seq2lastcodon eq "TGA" ||
$seq1lastcodon eq "TAG" || $seq2lastcodon eq "TAG" ||
$seq1lastcodon eq "TAA" || $seq2lastcodon eq "TAA") {
$$seq1_ref = substr ($seq1, 0, $length - 3);
$$seq2_ref = substr ($seq2, 0, $length - 3);
}
}
sub mutants {
my($codon) = $_[0];
my @mutants;
my @mut_aa;
my @mut_base;
#my(@nucleotides) = ('A', 'C', 'G', 'T');
my(@nucleotides) = ('a', 'c', 'g', 't');
for (my $i =0; $i < 3; $i++){
my $mut_codon = $codon;
foreach my $nuc (@nucleotides){
substr($mut_codon,$i,1,$nuc);
if ($mut_codon !~ /$codon/i){
my $aa = $codons{uc($mut_codon)};
my $base = uc($nuc);
#print "Trying this:$codon..$mut_codon..$base..$aa\n";
push (@mutants, $mut_codon);
push (@mut_aa, $aa);
push (@mut_base, $base);
}
}
}
# Insert the random nucleotide into the random position in the DNA
# The substr arguments mean the following:
# In the string $dna at position $position change 1 character to
# the string in $newbase
return (\@mut_base,\@mutants,\@mut_aa);
}
sub split_fasta_60 {
use vars qw ($ofile $line $f_cnt );
my $fil = $_[0];
$ofile = $_[1];
# print "split_fasta_60: Reading in from $fil\n";
open(INFILE,"<$fil") || die(" could not open $fil");
#$ofile = "";
$f_cnt = 0;
my @lin;
while ($line = <INFILE> ) {
chomp($line);
if (substr($line,0,1) eq '>') {
if ($ofile) {close (OUTFILE);}
# $ofile = "temp.fasta";
# print "Writing out to $ofile\n";
$f_cnt += 1; # $f_cnt ++;
open(OUTFILE,">$ofile");
print OUTFILE "$line";
}
else{
my @seq = split ("",$line);
my $i;
my $j=length($line);
for ($i=0;$i<$j;$i++){
if ($i % 60 == 0){
print OUTFILE "\n$seq[$i]";
}
else{
print OUTFILE $seq[$i];
}
}
}
}
print OUTFILE "\n";
close OUTFILE;
close INFILE;
}
sub reverse_dna {
my $DNA = $_[0];
my $revcom = reverse $DNA;
$revcom =~ tr/ACGTacgt/TGCAtgca/;
#print "Here is the reverse complement DNA:\n\n$revcom\n";
return $revcom;
}
#1;