-
Notifications
You must be signed in to change notification settings - Fork 2
/
OLGenie.pl
executable file
·5067 lines (3912 loc) · 194 KB
/
OLGenie.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env perl
# PROGRAM: SNPGenie + Wei-Zhang method for overlapping genes (overlapgenie; OLGenie)
# using codon-based analysis for dNN/dSN/dNS/dSS
#########################################################################################
# EXAMPLE CALL:
#########################################################################################
# OLGenie.pl --fasta_file=all_seqs.fasta --frame=sas11 --verbose > OLGenie_log.txt
#########################################################################################
#########################################################################################
## LICENSE
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
#########################################################################################
# AUTHOR: Chase W. Nelson
# Copyright (C) 2019 Chase W. Nelson
# DATE CREATED: April 2019
# CONTACT1: [email protected]
# CONTACT2: [email protected]
# AFFILIATION: Sackler Institute for Comparative Genomics, American Museum of Natural
# History, New York, NY 10024, USA
# CITATION1: OLGenie, https://github.com/chasewnelson/OLGenie
# CITATION2: Nelson CW, Ardern Z, Wei X. OLGenie: detecting natural selection to identify functional overlapping genes. In preparation.
# CITATION2: Nelson CW, Moncla LH, Hughes AL (2015) SNPGenie: estimating evolutionary
# parameters to detect natural selection using pooled next-generation sequencing data.
# Bioinformatics 31(22):3709-11, doi: 10.1093/bioinformatics/btv449.
use strict;
use Data::Dumper;
use List::Util qw(max);
use Getopt::Long;
# Get the time
my $time1 = time;
my $local_time1 = localtime;
STDOUT->autoflush(1);
my @nucleotides = qw/A C G T/;
#########################################################################################
# INITIALIZE (OPTIONAL) INPUT VARIABLES
my $fasta_file;
my $frame;
my $output_file;
my $mode;
my $verbose;
my $easter;
my $verbose_messages; # only meant for developing code
my $die_message = "\n\n" .
"################################################################################\n" .
"### OLGenie for analysis of selection in overlapping genes using member pairs.\n" .
"################################################################################\n\n" .
"\n################################################################################\n" .
"### OPTIONS:\n" .
"################################################################################\n" .
"\n" .
"\t--fasta_file (REQUIRED): a FASTA file containing multiple aligned sequences of one coding sequence.\n" .
"\t\tThe entire coding sequence must be an overlapping gene (OLG), with no non-overlapping codons.\n" .
"\t\tThe frame must be the frame of the reference gene (ORF1). (See the --frame option.)\n\n" .
"\t--frame (REQUIRED): the frame of the overlapping gene (OLG) relationship: ss12, ss13, sas11, sas12, or sas13:\n\n" .
"\t\tSENSE-SENSE:\n" .
"\t\t\tss12:\n\t\t\tORF1: 1-2-3-1-2-3-1\n\t\t\tORF2: 2-3-1-2-3-1-2\n" .
"\t\t\tss13:\n\t\t\tORF1: 1-2-3-1-2-3-1\n\t\t\tORF2: 3-1-2-3-1-2-3\n\n ".
"\t\tSENSE-ANTISENSE:\n" .
"\t\t\tsas11:\n\t\t\tORF1: 1-2-3-1-2-3-1\n\t\t\tORF2: 1-3-2-1-3-2-1\n" .
"\t\t\tsas12:\n\t\t\tORF1: 1-2-3-1-2-3-1\n\t\t\tORF2: 2-1-3-2-1-3-2\n" .
"\t\t\tsas13:\n\t\t\tORF1: 1-2-3-1-2-3-1\n\t\t\tORF2: 3-2-1-3-2-1-3\n\n\n" .
"\t--output_file (OPTIONAL): name of the TAB-delimited output file to be placed in the working directory\n" .
"\t\tunless a full path name is given. If not specified, a file will be printed in the working directory\n" .
"\t\tby the name OLGenie_codon_results.txt (DEFAULT).\n\n" .
"\t--verbose (OPTIONAL): tell OLGenie to report all unique nonamers (9-mers) overlapping each reference\n" .
"\t\tcodon, along with their counts, in the output file. May lead to large output files in cases with\n" .
"\t\tmany and/or divergent sequences.\n\n" .
"\n################################################################################\n" .
"### EXAMPLE:\n" .
"################################################################################\n\n" .
"\t\$ OLGenie.pl --fasta_file=my_alignment.fasta --frame=ss13 --output_file=OLGenie_codon_results.txt --verbose\n\n" .
"################################################################################\n\n";
# Get user input, if given. If a Boolean argument is passed, its value is 1; else undef
GetOptions( "fasta_file=s" => \$fasta_file,
"frame=s" => \$frame,
"output_file=s" => \$output_file,
"mode=s" => \$mode,
"verbose" => \$verbose,
"easter" => \$easter,
"verbose_messages" => \$verbose_messages )
or die $die_message;
# If an argument is called as a flag, its value is 0; if not called, it's null
unless(-f "$fasta_file" &&
($frame eq 'ss12' || $frame eq 'ss13' || $frame eq 'sas11' || $frame eq 'sas12' || $frame eq 'sas13')) {
die $die_message;
}
my $fasta_file_short = $fasta_file;
$fasta_file_short =~ s/(.*\/)?(.+\.\w+)/$2/;
unless("$output_file") {
$output_file = "OLGenie\_codon\_results\.txt";
}
if($mode) {
if($mode ne 'within_group' && $mode ne 'between_group') {
die $die_message;
}
}
unless("$mode") {
$mode = 'within_group';
}
print "\n################################################################################" .
"\n## ##" .
"\n## OLGenie Initiated! ##" .
"\n## ##" .
"\n################################################################################\n";
print "\nOLGenie initiated at local time $local_time1\n";
print "\n################################################################################";
print "\nANALYSIS MODE=$mode\...\n";
# Read in the group of sequences from the fasta file
my %header2sequence;
my $seq = '';
my $header = '';
my @headers_arr;
my $seq_num = 0;
my $last_seq_length;
open(IN_FASTA, "$fasta_file") or die "Could not open file $fasta_file\n";
print "\n################################################################################";
print "\nRecording coding sequence data for $fasta_file...\n";
while(<IN_FASTA>) {
chomp;
if(/>/) {
if($seq_num == 0) {
$header = $_;
$header =~ s/^>//; # get rid of FASTA header indicator
$header =~ tr/\|/_/; # convert pipes (|) to underscores (_), following IQTree convention
$header =~ s/\s.*$//; # trim anything including and after the first whitespace
$seq_num ++;
} else {
$seq = uc($seq);
$seq =~ tr/U/T/;
if($header =~ /[^\w^\-^\.]/) {
die "\n### TAXA NAMES CONTAIN INAPPROPRIATE CHARACTERS IN FASTA FILE: $header.\n" .
"### Only alphanumeric characters (a-z, A-Z, 0-9), underscores (_), dashes (-), and periods (.) may be used. SCRIPT TERMINATED.\n\n";
}
if(exists $header2sequence{$header}) {
die "\n\n### DIE: Each sequence in the FASTA multiple sequence alignment must have a unique header (up to first whitespace) for identification. TERMINATED.\n\n";
} else {
$header2sequence{$header} = $seq;
push(@headers_arr, $header);
}
$header = $_;
$header =~ s/^>//; # get rid of FASTA header indicator
$header =~ tr/\|/_/; # convert pipes (|) to underscores (_), following IQTree convention
$header =~ s/\s.*$//; # trim anything including and after the first whitespace
$seq_num ++;
my $this_seq_length = length($seq);
unless($this_seq_length % 3 == 0) {
die "\n\n### DIE: Sequences must be a complete set of codons, i.e., the nucleotide length".
"### must be evenly divisible by 3. Instead, the length is $this_seq_length\. TERMINATED.\n\n";
}
if($last_seq_length && ($last_seq_length != $this_seq_length)) {
die "\n\n### DIE: The sequences must be aligned, i.e., must be the same length. TERMINATED.\n\n";
} else {
$last_seq_length = $this_seq_length;
$seq = '';
}
}
} else {
$seq .= $_;
}
}
close IN_FASTA;
$seq = uc($seq);
$seq =~ tr/U/T/;
if($header =~ /^[^\w^\-^\.]/) {
die "\n### TAXA NAMES CONTAIN INAPPROPRIATE CHARACTERS IN FASTA FILE: $header.\n" .
"### Only alphanumeric characters (a-z, A-Z, 0-9), underscores (_), dashes (-), and periods (.) may be used. SCRIPT TERMINATED.\n\n";
}
$header2sequence{$header} = $seq;
push(@headers_arr, $header);
##########################################################################################
# Store the sequence index of each FASTA header
#my %header_to_index;
my %header_to_def_len;
foreach my $curr_header (keys %header2sequence) {
# Get seq
my $seq = $header2sequence{$curr_header};
# Count undefined length
my $num_N = $seq =~ s/N//g;
my $num_gap = $seq =~ s/-//g;
my $defined_length = $last_seq_length - $num_N - $num_gap;
$header_to_def_len{$curr_header} = $defined_length;
if($verbose_messages) { print "seq $header has a defined length of $defined_length\n" }
}
##########################################################################################
# Print which frame
print "\n################################################################################";
print "\nAnalyzing overlapping gene in frame $frame\...\n";
##########################################################################################
# DETERMINE ALL UNIQUE 9-mers along the alignment
my $print_codon_header = 0;
# IF PAIRWISE MODE ACTIVATED, record results separately for each pair
if($mode eq 'between_group') {
print "\n################################################################################";
print "\nActivating between-group mode (pairwise comparisons between user-defined groups)\...\n";
for(my $i = 0; $i < @headers_arr; $i++) {
my $curr_header_1 = $headers_arr[$i];
my $curr_seq_1 = $header2sequence{$curr_header_1};
for(my $k = $i + 1; $k < @headers_arr; $k++) {
my $curr_header_2 = $headers_arr[$k];
my $curr_seq_2 = $header2sequence{$curr_header_2};
# We've already made sure the sequence is a multiple of 3; loop codons
my %unique_9mers;
for(my $j = 0; $j <= length($curr_seq_1) - 9; $j += 3) {
$unique_9mers{$j}->{substr($curr_seq_1, $j, 9)}++; # increment current 9-mer count
$unique_9mers{$j}->{substr($curr_seq_2, $j, 9)}++; # increment current 9-mer count
}
# The current pair's data have been placed in %unique_9mers; call method
&olgenie_method(\%unique_9mers, $curr_header_1, $curr_header_2);
}
}
} elsif($mode eq 'within_group') { # don't calculate all pairs separately
print "\n################################################################################";
print "\nActivating within-group mode (all pairwise comparisons)\...\n";
my %unique_9mers;
for(my $i = 0; $i < @headers_arr; $i++) {
my $curr_header = $headers_arr[$i];
my $curr_seq = $header2sequence{$curr_header};
# We've already made sure the sequence is a multiple of 3
for(my $j = 0; $j <= length($curr_seq) - 9; $j += 3) {
$unique_9mers{$j}->{substr($curr_seq, $j, 9)}++; # increment current 9-mer count
}
}
# The whole alignments data have been placed in %unique_9mers; call method
&olgenie_method(\%unique_9mers);
}
sub olgenie_method {
my($unique_9mers_ref, $seq_1, $seq_2) = @_;
# if($verbose) { print "We got the arguments: @_\n" }
my %unique_9mers;
if($unique_9mers_ref ne '') {
%unique_9mers = %{$unique_9mers_ref};
}
my $seqs_compared;
if(! $seq_1 && ! $seq_2) {
$seqs_compared = 'all';
} else {
$seqs_compared = "$seq_1\:$seq_2";
}
if($verbose_messages) {
foreach my $site (sort {$a <=> $b} keys %unique_9mers) {
print "SITE: " . ($site + 1) . "-" . ($site + 9) . "\n";
foreach my $nonamer (sort keys %{$unique_9mers{$site}}) {
print "$nonamer\: " . $unique_9mers{$site}->{$nonamer} . "\n";
}
print "\n";
}
}
##########################################################################################
# ANALYZE ALL UNIQUE PAIRS, PRINT CODON FILE, AND STORE TOTALS
my %site_diffs_hh;
my %seq2sites;
my %seq_completed;
print "\n################################################################################".
"\nANALYZING SEQUENCE PAIR(S): $seqs_compared\n";
my %codon_data_hh;
# LOOP ALL NONAMER POSITIONS
foreach my $seq_site_index (sort {$a <=> $b} keys %unique_9mers) {
if($verbose_messages) { print "\n\n###seq_site_index=$seq_site_index\n" }
### NEW PHYLOGENY-NAIVE APPROACH
my @nonamers_sorted = sort keys %{$unique_9mers{$seq_site_index}};
my $codon_in_seq = ($seq_site_index + 6) / 3; # start with codon 2 (middle of first nonamer)
my $comparisons_sum = 0;
my $NN_sites_numerator = 0;
my $SN_sites_numerator = 0;
my $NS_sites_numerator = 0;
my $SS_sites_numerator = 0;
my $NN_diffs_numerator = 0;
my $SN_diffs_numerator = 0;
my $NS_diffs_numerator = 0;
my $SS_diffs_numerator = 0;
my $this_codon_MNV = 'FALSE';
my %ref_codons_to_counts;
my %alt1_codons_to_counts;
my %alt2_codons_to_counts;
for(my $nonamer1_index = 0; $nonamer1_index < @nonamers_sorted; $nonamer1_index++) {
my $nonamer1 = $nonamers_sorted[$nonamer1_index];
my $nonamer1_count = $unique_9mers{$seq_site_index}->{$nonamer1};
$codon_data_hh{$codon_in_seq}->{nonamers} .= "$nonamer1\:";
$codon_data_hh{$codon_in_seq}->{nonamer_counts} .= "$nonamer1_count\:";
$ref_codons_to_counts{substr($nonamer1, 3, 3)} += $nonamer1_count; # middle codon
for(my $nonamer2_index = $nonamer1_index; $nonamer2_index < @nonamers_sorted; $nonamer2_index++) { # start with SELF and proceed
my $nonamer2 = $nonamers_sorted[$nonamer2_index];
if($verbose_messages) { print "\n#nonamer pair: $nonamer1_index vs. $nonamer2_index\n" }
# EXAMINE THE PROVIDED PRODUCT FOR THE CURRENT MEMBER PAIR
my $nonamer1_len = length($nonamer1);
my $nonamer2_len = length($nonamer2);
if(($nonamer1_len % 3) != 0) {
die "\n\nDIE: sequence of $nonamer1 is not a multiple of 3 (complete codon set). TERMINATED.\n\n";
}
if(($nonamer2_len % 3) != 0) {
die "\n\nDIE: sequence of in $nonamer2 is not a multiple of 3 (complete codon set). TERMINATED.\n\n";
}
if($nonamer1_len != $nonamer2_len) {
die "\n\nDIE: The length of sequence is different in $nonamer1 and $nonamer2\. TERMINATED.\n\n";
}
my $num_codons = $nonamer1_len / 3;
if($verbose_messages) { print "num_codons=$num_codons\n" }
# STORE CODONS for nonamer1 and nonamer2 here; we've verified they're the same length
#NONAMER_CODONS: for(my $codon_num = 1; $codon_num <= $num_codons; $codon_num++) { # each ORF1 codon
my $codon_num = 2; # with the new approach, it's always the central/focal/ref codon
my $codon_index = $codon_num - 1;
my $site_index = 0 + (3 * $codon_index);
# Extract codons for ORF1
my $codon_nonamer1_ORF1 = substr($nonamer1, $site_index, 3);
my $codon_nonamer2_ORF1 = substr($nonamer2, $site_index, 3);
my $AA_nonamer1_ORF1 = get_amino_acid($codon_nonamer1_ORF1);
my $AA_nonamer2_ORF1 = get_amino_acid($codon_nonamer2_ORF1);
if($verbose_messages) {
print "codon_nonamer1_ORF1=$codon_nonamer1_ORF1\n";
print "codon_nonamer2_ORF1=$codon_nonamer2_ORF1\n";
print "AA_nonamer1_ORF1=$AA_nonamer1_ORF1\n";
print "AA_nonamer2_ORF1=$AA_nonamer2_ORF1\n";
}
if($codon_num < $num_codons && ($AA_nonamer1_ORF1 eq '*' || $AA_nonamer2_ORF1 eq '*')) {
print "### WARNING! ORF1, $nonamer1\-$nonamer2 comparison, codon $codon_in_seq encodes a within-frame STOP codon. Wrong frame selection ($frame)?\n";
}
# IF UNDEFINED CODONS, NOTHING AT ALL CAN BE DETERMINED
unless($codon_nonamer1_ORF1 =~ /N/ || $codon_nonamer2_ORF1 =~ /N/ || $codon_nonamer1_ORF1 =~ /-/ ||$codon_nonamer2_ORF1 =~ /-/) {
# Extract codons for (OVERLAPPING) ORF2
##################################################################################################
##################### SENSE-SENSE:
##################### ss12:
##################### ORF1: 1-2-3-1-2-3-1
##################### ORF2: 2-3-1-2-3-1-2
##################################################################################################
if($frame eq 'ss12') {
# Last 2 nt of prev codon, first 1 nt of next codon (same strand)
my $codon_nonamer1_ORF2_prev = substr($nonamer1, ($site_index - 1), 3);
my $codon_nonamer1_ORF2_next = substr($nonamer1, ($site_index + 2), 3);
my $codon_nonamer2_ORF2_prev = substr($nonamer2, ($site_index - 1), 3);
my $codon_nonamer2_ORF2_next = substr($nonamer2, ($site_index + 2), 3);
# COMEBACK: speed could be improved by re-using last round's 'codon' and '*_next'
if($verbose_messages) {
print "site_index=$site_index\n";
print "nonamer1=$nonamer1\n";
print "nonamer2=$nonamer2\n";
print "nonamer1_count=$nonamer1_count\n";
print "codon_nonamer1_ORF2_prev=$codon_nonamer1_ORF2_prev\n";
print "codon_nonamer1_ORF2_next=$codon_nonamer1_ORF2_next\n";
print "codon_nonamer2_ORF2_prev=$codon_nonamer2_ORF2_prev\n";
print "codon_nonamer2_ORF2_next=$codon_nonamer2_ORF2_next\n";
}
my $AA_nonamer1_ORF2_prev = get_amino_acid($codon_nonamer1_ORF2_prev);
my $AA_nonamer1_ORF2_next = get_amino_acid($codon_nonamer1_ORF2_next);
my $AA_nonamer2_ORF2_prev = get_amino_acid($codon_nonamer2_ORF2_prev);
my $AA_nonamer2_ORF2_next = get_amino_acid($codon_nonamer2_ORF2_next);
if($codon_num > 2 && ($AA_nonamer1_ORF2_prev eq '*' || $AA_nonamer1_ORF2_next eq '*' || $AA_nonamer2_ORF2_prev eq '*' || $AA_nonamer2_ORF2_next eq '*')) {
print "### WARNING! ORF2, $nonamer1\-$nonamer2 comparison, near ORF1 codon $codon_in_seq encodes a within-frame STOP codon. Wrong frame selection ($frame)?\n";
}
#######################
# GET NUMBER OF SITES
if($codon_num > 1 && $codon_num < $num_codons) {
# both ORF2's PREV and NEXT codons fully defined, 3 sites to examine
# New phylogeny-naive approach
$alt1_codons_to_counts{$codon_nonamer1_ORF2_prev} += $nonamer1_count;
$alt1_codons_to_counts{$codon_nonamer2_ORF2_prev} += $nonamer1_count; # ADDED
$alt2_codons_to_counts{$codon_nonamer1_ORF2_next} += $nonamer1_count;
$alt2_codons_to_counts{$codon_nonamer2_ORF2_next} += $nonamer1_count; # ADDED
my $nonamer1_num_changes_poss_site1 = 0;
my $nonamer1_num_changes_NN_site1 = 0;
my $nonamer1_num_changes_SN_site1 = 0;
my $nonamer1_num_changes_NS_site1 = 0;
my $nonamer1_num_changes_SS_site1 = 0;
my $nonamer1_num_changes_poss_site2 = 0;
my $nonamer1_num_changes_NN_site2 = 0;
my $nonamer1_num_changes_SN_site2 = 0;
my $nonamer1_num_changes_NS_site2 = 0;
my $nonamer1_num_changes_SS_site2 = 0;
my $nonamer1_num_changes_poss_site3 = 0;
my $nonamer1_num_changes_NN_site3 = 0;
my $nonamer1_num_changes_SN_site3 = 0;
my $nonamer1_num_changes_NS_site3 = 0;
my $nonamer1_num_changes_SS_site3 = 0;
my $nonamer2_num_changes_poss_site1 = 0;
my $nonamer2_num_changes_NN_site1 = 0;
my $nonamer2_num_changes_SN_site1 = 0;
my $nonamer2_num_changes_NS_site1 = 0;
my $nonamer2_num_changes_SS_site1 = 0;
my $nonamer2_num_changes_poss_site2 = 0;
my $nonamer2_num_changes_NN_site2 = 0;
my $nonamer2_num_changes_SN_site2 = 0;
my $nonamer2_num_changes_NS_site2 = 0;
my $nonamer2_num_changes_SS_site2 = 0;
my $nonamer2_num_changes_poss_site3 = 0;
my $nonamer2_num_changes_NN_site3 = 0;
my $nonamer2_num_changes_SN_site3 = 0;
my $nonamer2_num_changes_NS_site3 = 0;
my $nonamer2_num_changes_SS_site3 = 0;
my $NN_diffs = 0;
my $SN_diffs = 0;
my $NS_diffs = 0;
my $SS_diffs = 0;
# Just ORF1 (reference)
my $nt1_nonamer1_WT = substr($codon_nonamer1_ORF1, 0, 1);
my $nt2_nonamer1_WT = substr($codon_nonamer1_ORF1, 1, 1);
my $nt3_nonamer1_WT = substr($codon_nonamer1_ORF1, 2, 1);
my $nt1_nonamer2_WT = substr($codon_nonamer2_ORF1, 0, 1);
my $nt2_nonamer2_WT = substr($codon_nonamer2_ORF1, 1, 1);
my $nt3_nonamer2_WT = substr($codon_nonamer2_ORF1, 2, 1);
foreach my $nt (@nucleotides) {
##################################################################
# nonamer1
# SITE 1
# What is each change to ORF1 CODON SITE 1 / ORF2 CODON SITE 2?
if($nt ne $nt1_nonamer1_WT) { # only one possibility for this site
# only one possibility for this site
my $nonamer1_STOP_caused = 0;
my $codon_nonamer1_ORF1_MUT = $codon_nonamer1_ORF1;
$codon_nonamer1_ORF1_MUT =~ s/[ACGT]([ACGT])([ACGT])/$nt$1$2/;
my $codon_nonamer1_ORF2_prev_MUT = $codon_nonamer1_ORF2_prev;
$codon_nonamer1_ORF2_prev_MUT =~ s/([ACGT])[ACGT]([ACGT])/$1$nt$2/;
my $nonamer1_ORF1_effect = 'S';
#nonamer1-ORF1
my $AA_nonamer1_ORF1_MUT = get_amino_acid($codon_nonamer1_ORF1_MUT);
if($AA_nonamer1_ORF1_MUT ne $AA_nonamer1_ORF1) {
$nonamer1_ORF1_effect = 'N';
}
if($AA_nonamer1_ORF1 eq '*' || $AA_nonamer1_ORF1_MUT eq '*') {
$nonamer1_STOP_caused++;
}
my $nonamer1_ORF2_prev_effect = 'S';
#nonamer1-ORF2
my $AA_nonamer1_ORF2_prev_MUT = get_amino_acid($codon_nonamer1_ORF2_prev_MUT);
if($AA_nonamer1_ORF2_prev_MUT ne $AA_nonamer1_ORF2_prev) {
$nonamer1_ORF2_prev_effect = 'N';
}
if($AA_nonamer1_ORF2_prev eq '*' || $AA_nonamer1_ORF2_prev_MUT eq '*') {
$nonamer1_STOP_caused++;
}
# EASTER: enumerate all possible, site 1
if($easter) { print "$codon_in_seq\tnonamer1\t$codon_nonamer1_ORF1\t$codon_nonamer1_ORF2_prev\t$nt1_nonamer1_WT\t$nt\t$AA_nonamer1_ORF1\t$AA_nonamer1_ORF2_prev\t1\t$AA_nonamer1_ORF1_MUT\t$AA_nonamer1_ORF2_prev_MUT\n" }
# TALLY VIABLE CHANGES, GET NUMBER OF DIFFS
unless($nonamer1_STOP_caused > 0) {
$nonamer1_num_changes_poss_site1++;
if($nonamer1_ORF1_effect eq 'N') {
if($nonamer1_ORF2_prev_effect eq 'N') {
$nonamer1_num_changes_NN_site1++;
# ACTUAL DIFF
if($nt eq $nt1_nonamer2_WT) {
$NN_diffs++;
$site_diffs_hh{$codon_num}->{1}->{NN_diffs}++;
}
} elsif($nonamer1_ORF2_prev_effect eq 'S') {
$nonamer1_num_changes_NS_site1++;
# ACTUAL DIFF
if($nt eq $nt1_nonamer2_WT) {
$NS_diffs++;
$site_diffs_hh{$codon_num}->{1}->{NS_diffs}++;
}
}
} elsif($nonamer1_ORF1_effect eq 'S') {
if($nonamer1_ORF2_prev_effect eq 'N') {
$nonamer1_num_changes_SN_site1++;
# ACTUAL DIFF
if($nt eq $nt1_nonamer2_WT) {
$SN_diffs++;
$site_diffs_hh{$codon_num}->{1}->{SN_diffs}++;
}
} elsif($nonamer1_ORF2_prev_effect eq 'S') {
$nonamer1_num_changes_SS_site1++;
# ACTUAL DIFF
if($nt eq $nt1_nonamer2_WT) {
$SS_diffs++;
$site_diffs_hh{$codon_num}->{1}->{SS_diffs}++;
}
}
}
}
} # end member 1 site 1
# SITE 2
# What is each change to ORF1 CODON SITE 2 / ORF2 CODON SITE 3?
if($nt ne $nt2_nonamer1_WT) { # only one possibility for this site
my $nonamer1_STOP_caused = 0;
my $codon_nonamer1_ORF1_MUT = $codon_nonamer1_ORF1;
$codon_nonamer1_ORF1_MUT =~ s/([ACGT])[ACGT]([ACGT])/$1$nt$2/;
my $codon_nonamer1_ORF2_prev_MUT = $codon_nonamer1_ORF2_prev;
$codon_nonamer1_ORF2_prev_MUT =~ s/([ACGT])([ACGT])[ACGT]/$1$2$nt/;
my $nonamer1_ORF1_effect = 'S';
#nonamer1-ORF1
my $AA_nonamer1_ORF1_MUT = get_amino_acid($codon_nonamer1_ORF1_MUT);
if($AA_nonamer1_ORF1_MUT ne $AA_nonamer1_ORF1) {
$nonamer1_ORF1_effect = 'N';
}
if($AA_nonamer1_ORF1 eq '*' || $AA_nonamer1_ORF1_MUT eq '*') {
$nonamer1_STOP_caused++;
}
my $nonamer1_ORF2_prev_effect = 'S';
#nonamer1-ORF2
my $AA_nonamer1_ORF2_prev_MUT = get_amino_acid($codon_nonamer1_ORF2_prev_MUT);
if($AA_nonamer1_ORF2_prev_MUT ne $AA_nonamer1_ORF2_prev) {
$nonamer1_ORF2_prev_effect = 'N';
}
if($AA_nonamer1_ORF2_prev eq '*' || $AA_nonamer1_ORF2_prev_MUT eq '*') {
$nonamer1_STOP_caused++;
}
# EASTER: enumerate all possible, site 2
if($easter) { print "$codon_in_seq\tnonamer1\t$codon_nonamer1_ORF1\t$codon_nonamer1_ORF2_prev\t$nt2_nonamer1_WT\t$nt\t$AA_nonamer1_ORF1\t$AA_nonamer1_ORF2_prev\t2\t$AA_nonamer1_ORF1_MUT\t$AA_nonamer1_ORF2_prev_MUT\n" }
# TALLY VIABLE CHANGES, GET NUMBER OF DIFFS
unless($nonamer1_STOP_caused > 0) {
$nonamer1_num_changes_poss_site2++;
if($nonamer1_ORF1_effect eq 'N') {
if($nonamer1_ORF2_prev_effect eq 'N') {
$nonamer1_num_changes_NN_site2++;
# ACTUAL DIFF
if($nt eq $nt2_nonamer2_WT) {
$NN_diffs++;
$site_diffs_hh{$codon_num}->{2}->{NN_diffs}++;
}
} elsif($nonamer1_ORF2_prev_effect eq 'S') {
$nonamer1_num_changes_NS_site2++;
# ACTUAL DIFF
if($nt eq $nt2_nonamer2_WT) {
$NS_diffs++;
$site_diffs_hh{$codon_num}->{2}->{NS_diffs}++;
}
}
} elsif($nonamer1_ORF1_effect eq 'S') {
if($nonamer1_ORF2_prev_effect eq 'N') {
$nonamer1_num_changes_SN_site2++;
# ACTUAL DIFF
if($nt eq $nt2_nonamer2_WT) {
$SN_diffs++;
$site_diffs_hh{$codon_num}->{2}->{SN_diffs}++;
}
} elsif($nonamer1_ORF2_prev_effect eq 'S') {
$nonamer1_num_changes_SS_site2++;
# ACTUAL DIFF
if($nt eq $nt2_nonamer2_WT) {
$SS_diffs++;
$site_diffs_hh{$codon_num}->{2}->{SS_diffs}++;
}
}
}
}
} # end member 1 site 2
# SITE 3
# What is each change to ORF1 CODON SITE 3 / ORF2 (next) CODON SITE 1?
if($nt ne $nt3_nonamer1_WT) { # only one possibility for this site
my $nonamer1_STOP_caused = 0;
my $codon_nonamer1_ORF1_MUT = $codon_nonamer1_ORF1;
$codon_nonamer1_ORF1_MUT =~ s/([ACGT])([ACGT])[ACGT]/$1$2$nt/;
my $codon_nonamer1_ORF2_next_MUT = $codon_nonamer1_ORF2_next;
$codon_nonamer1_ORF2_next_MUT =~ s/[ACGT]([ACGT])([ACGT])/$nt$1$2/;
my $nonamer1_ORF1_effect = 'S';
#nonamer1-ORF1
my $AA_nonamer1_ORF1_MUT = get_amino_acid($codon_nonamer1_ORF1_MUT);
if($AA_nonamer1_ORF1_MUT ne $AA_nonamer1_ORF1) {
$nonamer1_ORF1_effect = 'N';
}
if($AA_nonamer1_ORF1 eq '*' || $AA_nonamer1_ORF1_MUT eq '*') {
$nonamer1_STOP_caused++;
}
my $nonamer1_ORF2_next_effect = 'S';
#nonamer1-ORF2
my $AA_nonamer1_ORF2_next_MUT = get_amino_acid($codon_nonamer1_ORF2_next_MUT);
if($AA_nonamer1_ORF2_next_MUT ne $AA_nonamer1_ORF2_next) {
$nonamer1_ORF2_next_effect = 'N';
}
if($AA_nonamer1_ORF2_next eq '*' || $AA_nonamer1_ORF2_next_MUT eq '*') {
$nonamer1_STOP_caused++;
}
# EASTER: enumerate all possible, site 3
if($easter) { print "$codon_in_seq\tnonamer1\t$codon_nonamer1_ORF1\t$codon_nonamer1_ORF2_next\t$nt3_nonamer1_WT\t$nt\t$AA_nonamer1_ORF1\t$AA_nonamer1_ORF2_next\t3\t$AA_nonamer1_ORF1_MUT\t$AA_nonamer1_ORF2_next_MUT\n" }
# TALLY VIABLE CHANGES, GET NUMBER OF DIFFS
unless($nonamer1_STOP_caused > 0) {
$nonamer1_num_changes_poss_site3++;
if($nonamer1_ORF1_effect eq 'N') {
if($nonamer1_ORF2_next_effect eq 'N') {
$nonamer1_num_changes_NN_site3++;
# ACTUAL DIFF
if($nt eq $nt3_nonamer2_WT) {
$NN_diffs++;
$site_diffs_hh{$codon_num}->{3}->{NN_diffs}++;
}
} elsif($nonamer1_ORF2_next_effect eq 'S') {
$nonamer1_num_changes_NS_site3++;
# ACTUAL DIFF
if($nt eq $nt3_nonamer2_WT) {
$NS_diffs++;
$site_diffs_hh{$codon_num}->{3}->{NS_diffs}++;
}
}
} elsif($nonamer1_ORF1_effect eq 'S') {
if($nonamer1_ORF2_next_effect eq 'N') {
$nonamer1_num_changes_SN_site3++;
# ACTUAL DIFF
if($nt eq $nt3_nonamer2_WT) {
$SN_diffs++;
$site_diffs_hh{$codon_num}->{3}->{SN_diffs}++;
}
} elsif($nonamer1_ORF2_next_effect eq 'S') {
$nonamer1_num_changes_SS_site3++;
# ACTUAL DIFF
if($nt eq $nt3_nonamer2_WT) {
$SS_diffs++;
$site_diffs_hh{$codon_num}->{3}->{SS_diffs}++;
}
}
}
}
} # end member 1 site 3
##################################################################
# nonamer2
# SITE 1
# What is each change to ORF1 CODON SITE 1 / ORF2 CODON SITE 2?
if($nt ne $nt1_nonamer2_WT) { # only one possibility for this site
# only one possibility for this site
my $nonamer2_STOP_caused = 0;
my $codon_nonamer2_ORF1_MUT = $codon_nonamer2_ORF1;
$codon_nonamer2_ORF1_MUT =~ s/[ACGT]([ACGT])([ACGT])/$nt$1$2/;
my $codon_nonamer2_ORF2_prev_MUT = $codon_nonamer2_ORF2_prev;
$codon_nonamer2_ORF2_prev_MUT =~ s/([ACGT])[ACGT]([ACGT])/$1$nt$2/;
my $nonamer2_ORF1_effect = 'S';
#nonamer1-ORF1
my $AA_nonamer2_ORF1_MUT = get_amino_acid($codon_nonamer2_ORF1_MUT);
if($AA_nonamer2_ORF1_MUT ne $AA_nonamer2_ORF1) {
$nonamer2_ORF1_effect = 'N';
}
if($AA_nonamer2_ORF1 eq '*' || $AA_nonamer2_ORF1_MUT eq '*') {
$nonamer2_STOP_caused++;
}
my $nonamer2_ORF2_prev_effect = 'S';
#nonamer1-ORF2
my $AA_nonamer2_ORF2_prev_MUT = get_amino_acid($codon_nonamer2_ORF2_prev_MUT);
if($AA_nonamer2_ORF2_prev_MUT ne $AA_nonamer2_ORF2_prev) {
$nonamer2_ORF2_prev_effect = 'N';
}
if($AA_nonamer2_ORF2_prev eq '*' || $AA_nonamer2_ORF2_prev_MUT eq '*') {
$nonamer2_STOP_caused++;
}
# EASTER: enumerate all possible, site 1
if($easter) { print "$codon_in_seq\tnonamer2\t$codon_nonamer2_ORF1\t$codon_nonamer2_ORF2_prev\t$nt1_nonamer2_WT\t$nt\t$AA_nonamer2_ORF1\t$AA_nonamer2_ORF2_prev\t1\t$AA_nonamer2_ORF1_MUT\t$AA_nonamer2_ORF2_prev_MUT\n" }
# TALLY VIABLE CHANGES, GET NUMBER OF DIFFS
unless($nonamer2_STOP_caused > 0) {
$nonamer2_num_changes_poss_site1++;
if($nonamer2_ORF1_effect eq 'N') {
if($nonamer2_ORF2_prev_effect eq 'N') {
$nonamer2_num_changes_NN_site1++;
} elsif($nonamer2_ORF2_prev_effect eq 'S') {
$nonamer2_num_changes_NS_site1++;
}
} elsif($nonamer2_ORF1_effect eq 'S') {
if($nonamer2_ORF2_prev_effect eq 'N') {
$nonamer2_num_changes_SN_site1++;
} elsif($nonamer2_ORF2_prev_effect eq 'S') {
$nonamer2_num_changes_SS_site1++;
}
}
}
} # end member 2 site 1
# SITE 2
# What is each change to ORF1 CODON SITE 2 / ORF2 CODON SITE 3?
if($nt ne $nt2_nonamer2_WT) { # only one possibility for this site
my $nonamer2_STOP_caused = 0;
my $codon_nonamer2_ORF1_MUT = $codon_nonamer2_ORF1;
$codon_nonamer2_ORF1_MUT =~ s/([ACGT])[ACGT]([ACGT])/$1$nt$2/;
my $codon_nonamer2_ORF2_prev_MUT = $codon_nonamer2_ORF2_prev;
$codon_nonamer2_ORF2_prev_MUT =~ s/([ACGT])([ACGT])[ACGT]/$1$2$nt/;
my $nonamer2_ORF1_effect = 'S';
#nonamer1-ORF1
my $AA_nonamer2_ORF1_MUT = get_amino_acid($codon_nonamer2_ORF1_MUT);
if($AA_nonamer2_ORF1_MUT ne $AA_nonamer2_ORF1) {
$nonamer2_ORF1_effect = 'N';
}
if($AA_nonamer2_ORF1 eq '*' || $AA_nonamer2_ORF1_MUT eq '*') {
$nonamer2_STOP_caused++;
}
my $nonamer2_ORF2_prev_effect = 'S';
#nonamer1-ORF2
my $AA_nonamer2_ORF2_prev_MUT = get_amino_acid($codon_nonamer2_ORF2_prev_MUT);
if($AA_nonamer2_ORF2_prev_MUT ne $AA_nonamer2_ORF2_prev) {
$nonamer2_ORF2_prev_effect = 'N';
}
if($AA_nonamer2_ORF2_prev eq '*' || $AA_nonamer2_ORF2_prev_MUT eq '*') {
$nonamer2_STOP_caused++;
}
# EASTER: enumerate all possible, site 2
if($easter) { print "$codon_in_seq\tnonamer2\t$codon_nonamer2_ORF1\t$codon_nonamer2_ORF2_prev\t$nt2_nonamer2_WT\t$nt\t$AA_nonamer2_ORF1\t$AA_nonamer2_ORF2_prev\t2\t$AA_nonamer2_ORF1_MUT\t$AA_nonamer2_ORF2_prev_MUT\n" }
# TALLY VIABLE CHANGES, GET NUMBER OF DIFFS
unless($nonamer2_STOP_caused > 0) {
$nonamer2_num_changes_poss_site2++;
if($nonamer2_ORF1_effect eq 'N') {
if($nonamer2_ORF2_prev_effect eq 'N') {
$nonamer2_num_changes_NN_site2++;
} elsif($nonamer2_ORF2_prev_effect eq 'S') {
$nonamer2_num_changes_NS_site2++;
}
} elsif($nonamer2_ORF1_effect eq 'S') {
if($nonamer2_ORF2_prev_effect eq 'N') {
$nonamer2_num_changes_SN_site2++;
} elsif($nonamer2_ORF2_prev_effect eq 'S') {
$nonamer2_num_changes_SS_site2++;
}
}
}
} # end member 2 site 2
# SITE 3
# What is each change to ORF1 CODON SITE 3 / ORF2 (next) CODON SITE 1?
if($nt ne $nt3_nonamer2_WT) { # only one possibility for this site
my $nonamer2_STOP_caused = 0;
my $codon_nonamer2_ORF1_MUT = $codon_nonamer2_ORF1;
$codon_nonamer2_ORF1_MUT =~ s/([ACGT])([ACGT])[ACGT]/$1$2$nt/;
my $codon_nonamer2_ORF2_next_MUT = $codon_nonamer2_ORF2_next;
$codon_nonamer2_ORF2_next_MUT =~ s/[ACGT]([ACGT])([ACGT])/$nt$1$2/;
my $nonamer2_ORF1_effect = 'S';
#nonamer2-ORF1
my $AA_nonamer2_ORF1_MUT = get_amino_acid($codon_nonamer2_ORF1_MUT);
if($AA_nonamer2_ORF1_MUT ne $AA_nonamer2_ORF1) {
$nonamer2_ORF1_effect = 'N';
}
if($AA_nonamer2_ORF1 eq '*' || $AA_nonamer2_ORF1_MUT eq '*') {
$nonamer2_STOP_caused++;
}
my $nonamer2_ORF2_next_effect = 'S';
#nonamer2-ORF2
my $AA_nonamer2_ORF2_next_MUT = get_amino_acid($codon_nonamer2_ORF2_next_MUT);
if($AA_nonamer2_ORF2_next_MUT ne $AA_nonamer2_ORF2_next) {
$nonamer2_ORF2_next_effect = 'N';
}
if($AA_nonamer2_ORF2_next eq '*' || $AA_nonamer2_ORF2_next_MUT eq '*') {
$nonamer2_STOP_caused++;
}
# EASTER: enumerate all possible, site 3
if($easter) { print "$codon_in_seq\tnonamer2\t$codon_nonamer2_ORF1\t$codon_nonamer2_ORF2_next\t$nt3_nonamer2_WT\t$nt\t$AA_nonamer2_ORF1\t$AA_nonamer2_ORF2_next\t3\t$AA_nonamer2_ORF1_MUT\t$AA_nonamer2_ORF2_next_MUT\n" }
# TALLY VIABLE CHANGES, GET NUMBER OF DIFFS
unless($nonamer2_STOP_caused > 0) {
$nonamer2_num_changes_poss_site3++;
if($nonamer2_ORF1_effect eq 'N') {
if($nonamer2_ORF2_next_effect eq 'N') {
$nonamer2_num_changes_NN_site3++;
} elsif($nonamer2_ORF2_next_effect eq 'S') {
$nonamer2_num_changes_NS_site3++;
}
} elsif($nonamer2_ORF1_effect eq 'S') {
if($nonamer2_ORF2_next_effect eq 'N') {