-
Notifications
You must be signed in to change notification settings - Fork 37
/
snpgenie_between_group.pl
executable file
·18198 lines (17820 loc) · 288 KB
/
snpgenie_between_group.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, between-group analysis, for two or more aligned multi-FASTA files.
#########################################################################################
# EXAMPLE CALL:
#########################################################################################
# snpgenie_between_group.pl --gtf_file=<CDS_annotations>.gtf --num_bootstraps=10000 --procs_per_node=16
#########################################################################################
# Copyright (C) 2018 Chase W. Nelson
# 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/>.
# DATE CREATED: April 10, 2015
# DATE MODIFIED: December 16, 2016
# AUTHOR: Chase W. Nelson
# CONTACT1: [email protected]
# CONTACT2: [email protected]
# AFFILIATION1: Sackler Institute for Comparative Genomics, American Museum of Natural
# History, New York, NY 10024, USA
# AFFILIATION2: BigPlant Consortium, Center for Genomics and Systems Biology, New York
# University, New York, NY 10003, USA
# ADD:
# —SORT codons before entry into the NNN-NNN comps hh, so we don't have, e.g., both AAG-AAA and AAA-AAG
# —ADD MAJORITY CODON to codon file
# —PROBLEM with min-codon-count in variant results
use strict;
#use warnings;
use Data::Dumper;
use List::Util qw(max);
use Parallel::ForkManager;
use Getopt::Long;
my $time1 = time;
my $local_time1 = localtime;
#########################################################################################
# INITIALIZE (OPTIONAL) INPUT VARIABLES
# AUTOMATICALLY DETECT FASTA FILES IN WORKING DIRECTORY
my @fasta_files = &get_fasta_file_names;
foreach(@fasta_files) {
unless($_ =~ /.fa/) { die "\n\n# FASTA files must contain .fa, .fas, or .fasta extension. TERMINATED\n\n"; }
}
# Initialize others
my $gtf_file;
my $procs_per_node;
my $num_bootstraps;
# Get user input, if given. If a Boolean argument is passed, its value is 1; else undef
GetOptions( "gtf_file=s" => \$gtf_file,
"procs_per_node=i" => \$procs_per_node,
"num_bootstraps=i" => \$num_bootstraps)
or die "\n### WARNING: Error in command line arguments. Script terminated.\n\n";
# If an argument is called as a flag, its value is 0; if not called, it's null
unless($gtf_file =~ /.gtf/) {
die "\n### WARNING: The --gtf_file option must be a file with a .gtf or .gtf extension\n".
"### SNPGenie terminated.\n\n";
}
if(! $procs_per_node) {
if($procs_per_node != 0) {
$procs_per_node = 1; # DEFAULT is 1
}
} elsif($procs_per_node < 1) {
die "\n### WARNING: The --procs_per_node option must be an integer ≥1\n".
"### SNPGenie terminated.\n\n";
}
if(! $num_bootstraps) {
if($num_bootstraps != 0) { # Called as a flag, but given no value
$num_bootstraps = 1000; # default behavior: 1000 bootstraps
}
} else {
print "\n### No bootstrapping will occur.\n\n";
}
print "\n# Number of parallel processes to be invoked is $procs_per_node\.\n";
print "\n################################################################################".
"\n## ##".
"\n## Between-Group SNPGenie Initiated! ##".
"\n## ##".
"\n################################################################################\n";
print "\nSNPGenie initiated at local time $local_time1\n";
#########################################################################################
# Store product information
my @product_names_arr = &get_product_names_from_gtf($gtf_file);
@product_names_arr = sort {$a <=> $b} @product_names_arr;
#print "\nProduct names: @product_names_arr\n";
# Determine all product coordinates
my %product_coordinates_ha;
foreach my $product_name (@product_names_arr) {
#print "product name is: $product_name\n";
my @product_coord_arr = &get_product_coordinates($product_name);
#print "\n\n$product_name product_coord arr: @product_coord_arr\n\n";
# Save to a hash of arrays
push(@{$product_coordinates_ha{$product_name}->{product_coord_arr}},@product_coord_arr);
#print "\n\n$product_name product_coord arr in harr: @{$product_coordinates_ha{$product_name}->{product_coord_arr}} \n\n";
}
my @seqs_aa;
#print "\nfasta file 0 (now b) is: $fasta_files[0]\n";
my @fasta_file_names;
# This proves too prohibitive for big sequence data
# Instead, store just for the products and groups individually, below
foreach(@fasta_files) { # GROUP NAMES ARE THE FASTA FILES
# Read in the sequence from the file
my $seq = '';
my @seqs_arr;
#my $header = '';
#my @headers_arr;
my $seq_num = 0;
my $last_seq_length;
open(IN, "$_") or die "Could not open file $_\n";
print "\nRecording coding sequence data for $_...\n";
push(@fasta_file_names,$_);
while(<IN>) {
chomp;
if(/>/) {
if($seq_num == 0) {
#$header = $_;
$seq_num ++;
} else {
$seq = uc($seq);
$seq =~ tr/U/T/;
push(@seqs_arr,$seq);
#push(@headers_arr,$header);
#$header = $_;
$seq_num ++;
my $this_seq_length = length($seq);
#print "\nseq $seq_num is of length $this_seq_length\n";
if($last_seq_length && ($last_seq_length != $this_seq_length)) {
die "\n\nDIE: The sequences must be aligned, i.e., must be the same length. TERMINATED.\n\n";
} else {
$last_seq_length = $this_seq_length;
#print "\nseq: $seq\n";
$seq = '';
}
}
} else {
$seq .= $_;
}
}
close IN;
$seq = uc($seq);
$seq =~ tr/U/T/;
push(@seqs_arr,$seq);
#push(@headers_arr,$header);
push(@seqs_aa,\@seqs_arr); # ??? work this way. Yes, seems to
}
print "\n";
#print "\nfasta file 0 (now c) is: $fasta_files[0]\n";
# Go through each product and DO DIS THANG
# HERE'S HOW to get the first array:
#print "@{$seqs_aa[0]}\n";
#my $counter=1;
#foreach(@{$seqs_aa[0]}) {
# print "$counter: $_\n\n";
# $counter++;
#}
my %products_groups_seqs_hh;
# FORMAT:
#%products_groups_seqs_hh = {
# 'ORF1a' => {
# 'group_1' => {
# 'seq_1' => 'ACGT',
# 'seq_2' => 'ACGT',
# ... ,
# 'seq_n' => 'ACGT',
# },
# 'group_2' => {
# 'seq_1' => 'ACGT',
# 'seq_2' => 'ACGT',
# ... ,
# 'seq_n' => 'ACGT',
# }
# },
#
# 'ORF1b' => {
# 'group_1' => {
# 'seq_1' => 'ACGT',
# 'seq_2' => 'ACGT',
# ... ,
# 'seq_n' => 'ACGT',
# },
# 'group_2' => {
# 'seq_1' => 'ACGT',
# 'seq_2' => 'ACGT',
# ... ,
# 'seq_n' => 'ACGT',
# }
# }
#};
foreach(@product_names_arr) {
my $product_name = $_;
my @product_coord_arr = @{$product_coordinates_ha{$product_name}->{product_coord_arr}};
# New segments approach
my %product_starts;
my %product_stops;
my $num_segments = (@product_coord_arr / 2);
for(my $i=1; $i<=$num_segments; $i++) { # $i<=scalar(@product_coord_arr)
$product_starts{$i} = $product_coord_arr[2*$i-2];
$product_stops{$i} = $product_coord_arr[2*$i-1];
}
# Build an array of all the sequences for each group
my $group_num = 0;
foreach (@seqs_aa) { # for each group
my @group = @{$_};
my $group_id = $fasta_file_names[$group_num];
$group_num ++;
# my $group_id = 'group_' . $group_num;
my $seq_num = 0;
foreach my $sequence (@group) { # for each seq in alignment (max coverage)
$seq_num ++;
my $seq_id = 'seq_' . $seq_num;
#build the product seq and add it to full group list
my $this_product_seq = '';
for(my $seg=1; $seg<=$num_segments; $seg++) { # for each segment
#my $this_index = $seg - 1;
#my $this_start = $product_starts[$this_index];
my $this_start = $product_starts{$seg};
my $this_start_index = $this_start - 1;
#my $this_stop = $product_stop[$this_index];
my $this_stop = $product_stops{$seg};
my $this_stop_index = $this_stop - 1;
my $this_seg_length = ($this_stop - $this_start + 1);
$this_product_seq .= substr($sequence,$this_start_index,$this_seg_length);
} # done building sequence with all segments
# Add the sequence to an array of all sequences for this product in the group
$products_groups_seqs_hh{$product_name}->{$group_id}->{$seq_id} = $this_product_seq;
}
}
}
#my @test_keys = keys %{$products_groups_seqs_hh{'ORF5'}->{'group_1'}};
#@test_keys = keys %products_groups_seqs_hh;
#print "\n\n @test_keys \n\n";
#my $test_seq = $products_groups_seqs_hh{'ORF5'}->{'group_1'}->{'seq_1'};
#print "\n\n $test_seq \n\n";
STDOUT->autoflush(1);
# READY OUTPUT FILES
open(CODON_FILE,">>between\_group\_codon\_results\.txt");
print CODON_FILE "analysis\tproduct\tgroup_1\tgroup_2\tcodon\tvariability\t".
"num_defined_codons_g1\tnum_defined_codons_g2\tcomparisons\tN_sites\tS_sites\tN_diffs\tS_diffs\n";
close CODON_FILE;
open(OUTFILE,">>between\_group\_product\_results\.txt");
print OUTFILE "analysis\tproduct\tgroup_1\tgroup_2\tN_sites\tS_sites\tN_diffs\tS_diffs\t".
"dN\tdS\t".
"dN\-dS\t".
"dN\/dS\n";
#print "group_1\tgroup_2\tproduct\tN_sites\tS_sites\tN_diffs\tS_diffs\n";
#if($num_bootstraps > 1) {
# print OUTFILE "\tSE_piN_minus_piS\tz_value\n";
#} else {
# print OUTFILE "\n";
#}
# YES, it all works
#my %product_data_hh;
my $between_total_N_sites = 0;
my $between_total_S_sites = 0;
my $between_total_N_diffs = 0;
my $between_total_S_diffs = 0;
my @group_names_arr;
foreach my $product_name (keys %products_groups_seqs_hh) { # for each product
#foreach my $product_name (my @thisarray = ('ORF5')) { # for each product
print "\n#########################################################\n".
"################ Analyzing $product_name\n".
"#########################################################\n";
# I want, for this product:
#groups_codons_aa[group_index]->[codon_index]->array of all codons in group
my @groups_codons_aa;
my $last_num_codons;
my $group_index = 0;
# Store every sequence for each group; this goes really fast
foreach my $group_name (keys %{$products_groups_seqs_hh{$product_name}}) { # for each group (fasta file)
print "\ngroup index $group_index is group $group_name\n";
push(@group_names_arr, $group_name);
foreach my $sequence_name (keys %{$products_groups_seqs_hh{$product_name}->{$group_name}}) { # for each seq in group
my $sequence = $products_groups_seqs_hh{$product_name}->{$group_name}->{$sequence_name};
# Make sure it's a complete set of codons
my $seq_length = length($sequence);
if(($seq_length % 3) != 0) {
die "\n\nDIE: A sequence in $product_name is not a multiple of 3 (complete codon set). TERMINATED.\n\n";
}
# Build all codons, put in @aa with @codon_num->@codons
my $num_codons = $seq_length / 3;
if((! $last_num_codons) && ($last_num_codons ne '') && ($num_codons != $last_num_codons)) {
die "\n\nDIE: In $product_name, there are sequences of different length ($last_num_codons and $num_codons). TERMINATED.\n\n";
} else {
$last_num_codons = $num_codons;
#print "changed\n";
}
my $codon_index = 0; # so we're going to populate $groups_codons_aa[0]->[0]->@
for(my $i=1; $i<=$num_codons; $i++) { # for each codon
my $array_index = $i - 1;
#my $codon = substr($sequence,$codon_index,3);
# More efficient to call substr at beginning and gobble
my $codon = substr($sequence,0,3,"");
push(@{$groups_codons_aa[$group_index]->[$array_index]},$codon);
#$codon_index+=3;
}
}
$group_index++;
} # Done storing all groups
# Compare all codons in one group to all codons in another
my $num_groups = scalar @groups_codons_aa;
# my $num_codons_1 = scalar @{$groups_codons_aa[0]};
# my $num_seqs_1 = scalar @{$groups_codons_aa[0]->[0]};
# my $num_codons_2 = scalar @{$groups_codons_aa[1]};
# my $num_seqs_2 = scalar @{$groups_codons_aa[1]->[0]};
#
# print "\n\n num groups is $num_groups \n";
# print " num codons for $product_name in group 1 $num_codons_1 \n";
# print " num seq for $product_name in group 1 $num_seqs_1 \n";
# print " num codons for $product_name in group 2 $num_codons_2 \n";
# print " num seq for $product_name in group 2 $num_seqs_2 \n\n";
# COMEBACK: make a number of sites subroutine for the whole codon, not just the sites
# OR, instead of calling the subroutine every time, just COUNT the comparisons!
# $hh_comps{CGA}->{CGG}->256 YES!!
# COMPARE ALL THIS POSITION'S CODONS IN ONE GROUP TO ALL CODONS IN ANOTHER
# GROUP i
for(my $i=0; $i<$num_groups; $i++) {
# STORE THE SEQUENCES FOR THIS PRODUCT FROM GROUP I ONLY
# the fasta file corresponding to group i is:
# push(@{$groups_codons_aa[$group_index]->[$array_index]},$codon);
# foreach my $group_name (keys %{$products_groups_seqs_hh{$product_name}}) { # for each group (fasta file) # <- order in which stored
# GROUP j
for(my $j=$i+1; $j<$num_groups; $j++) { # comparing groups i and j
# Get species/sequence names for each group.
#?my @group_1_spp;
#?my @group_2_spp;
#?push(@group_1_spp,$_);
my $group_name_i = $group_names_arr[$i];
my $group_name_j = $group_names_arr[$j];
#print "\nmy group name i is: " . $fasta_files[0] . "\n";
#print "\nmy group name i is: $group_name_i\nmy group name j is: $group_name_j\n";
# STORE THE SEQUENCES FOR THIS PRODUCT FROM GROUP J ONLY
# the fasta file corresponding to group j is:
my $num_codons_in_product = scalar @{$groups_codons_aa[$i]};
#my $num_seqs_per_codon = scalar @{$groups_codons_aa[$i]->[0]}; # but this is different for the two groups COMEBACK#####
# THIS NEEDS TO BE DIFFERENT FOR TWO GROUPS
# Updated here:
my $num_seqs_per_codon_groupi = scalar @{$groups_codons_aa[$i]->[0]};
my $num_seqs_per_codon_groupj = scalar @{$groups_codons_aa[$j]->[0]};
print "\nComparing $num_codons_in_product codons in $product_name between groups $i and $j\...\n";
#groups_codons_aa[group_index]->[codon_index]->array of all codons in group
# Quickly see if it's polymorphic between-group to save time. It's faster just to check,
# EVEN IF not polymorphic
my @polymorphic_codons_arr;
my %codonIndex_conserved;
print "\nDetermining which codons are polymorphic...\n";
# PARALLELIZE IT
##
mkdir("$product_name\_polymorphic_codons_arr");
chdir("$product_name\_polymorphic_codons_arr");
# my $procs_poly = 10; # number to do at once, cores to assign simultaneously, machine-limited. CUVIER has 80
my $pm_poly = Parallel::ForkManager->new($procs_per_node);
# DETERMINE WHICH CODONS ARE POLYMORPHIC
for(my $codon_index = 0; $codon_index < $num_codons_in_product; $codon_index++) { # for each codon in product
##
$pm_poly->start and next; # this is IT
#print "codon index $codon_index\n";
#my $codon_to_print = $groups_codons_aa[$i]->[$codon_index]->[1];
#$| = 1;
#print "$codon_to_print";
my $between_g_comparisons = 0;
my $this_codon_poly = 0;
OUTER: for (my $gi_seq_index = 0; $gi_seq_index < $num_seqs_per_codon_groupi; $gi_seq_index++) { # for each gi sequence at this codon
# Codon to compare against
INNER: for (my $gj_seq_index = 0; $gj_seq_index < $num_seqs_per_codon_groupj; $gj_seq_index++) { # for each gj sequence at this codon
#?my $group_1_species = $group_1_spp[$gi_spp_index];
#?my $group_2_species = $group_2_spp[$gj_spp_index];
my $codon_gi = $groups_codons_aa[$i]->[$codon_index]->[$gi_seq_index];
my $codon_gj = $groups_codons_aa[$j]->[$codon_index]->[$gj_seq_index];
#print "Comparing codons $codon_gi and $codon_gj\n";
#?my $codon_gi = $data_codonnum_spp_codon_hh{$codon_num}->{$group_1_species};
#?my $codon_gj = $data_codonnum_spp_codon_hh{$codon_num}->{$group_2_species};
if(($codon_gi ne $codon_gj) && ! ($codon_gi =~ 'N' || $codon_gi =~ '-' || $codon_gi eq '' || $codon_gj =~ 'N' || $codon_gj =~ '-' || $codon_gj eq '')) {
$this_codon_poly = 1;
#print "\ncodon $codon_index is polymorphic\n";
last OUTER; # gotta break of out TWO loops here and go to next codon in product
}
} # INNER
} # OUTER
## push(@polymorphic_codons_arr,$this_codon_poly);
##
open(THIS_POLY_TEMP_FILE,">>$codon_index");
print THIS_POLY_TEMP_FILE $this_codon_poly;
close THIS_POLY_TEMP_FILE;
$pm_poly->finish; # special name
}
##
$pm_poly->wait_all_children; # special name, methods within module
my @polymorphic_codons_FILES_arr = glob "*";
@polymorphic_codons_FILES_arr = sort {$a <=> $b} @polymorphic_codons_FILES_arr;
#print "sorted polymorphic_codons_FILE_arr: @polymorphic_codons_FILES_arr\n";
foreach(@polymorphic_codons_FILES_arr) { # file names, actually
my $poly_file = $_;
open(CURR_POLY_FILE, $poly_file) or die "\n## Cannot open $poly_file. TERMINATED.\n\n";
while(<CURR_POLY_FILE>) {
chomp;
push(@polymorphic_codons_arr,$_);
}
close CURR_POLY_FILE;
unlink $poly_file;
}
chdir("..");
rmdir("$product_name\_polymorphic_codons_arr");
open(POLY_FILE,">>between\_group\_polymorphism.txt");
print POLY_FILE "\nDone recording polymorphism in $product_name between groups $i and $j\nIt is: @polymorphic_codons_arr\n\n";
close POLY_FILE;
print "\nDone recording polymorphism in $product_name between groups $i and $j\n";
#?print "\nDone recording polymorphism in partition $partition_id between the groups:\nGROUP 1: $group_name_i\nGROUP 2: $group_name_j\n";
#print "It is: @polymorphic_codons_arr\n\n";
print "\nAnalyzing polymorphic codons in $product_name\...\n";
# calculate number of total codons so that we can track % progress?
# INTRO: skip if not poly, just do values for the first codon observed
# my $between_product_N_sites_sum = 0;
# my $between_product_S_sites_sum = 0;
# my $between_product_N_diffs_sum = 0;
# my $between_product_S_diffs_sum = 0;
print "\nAnalyzing each codon...\n";
# PARALLEL
mkdir("$product_name\_codon\_analysis");
chdir("$product_name\_codon\_analysis");
my $pm_codons = Parallel::ForkManager->new($procs_per_node);
# To do this, must write all output to files during the loop
# after all codons finished, then we must read through and scoop results
# while storing in the appropriate places
### NEW POLYMORPHIC SITE ANALYSIS BOOTSTRAP ?
#my %poly_site_data_hh; # $poly_site_data_hh{codon_index}->{unique_variants}
my %codonum_group_numDefinedCodons_hh;
# BOOTSTRAP STORAGE APPROACH
## my %gi_gj_codon_comp_stat_hh; # To store all comparisons for later bootstraps
# STORE FOR SLIDING WINDOWS AND BOOTSTRAPPING
my %codon_data_hh; # Will store codon->N_diffs_codon/S_diffs_codon/...
PAIRWISE_DELETION: for(my $codon_index = 0; $codon_index < $num_codons_in_product; $codon_index++) { # for each codon in product
$pm_codons->start and next; # this is IT
my $codon_num = $codon_index+1;
my $this_codon_output = '';
#print "codon $codon_num at time " . time . "\n";
#print CODON_FILE "$product_name\t$i\t$j\tcodon_" . $codon_num . " \t";
# print CODON_FILE "$product_name\t$group_name_i\t$group_name_j\tcodon_" . $codon_num . " \t";
$this_codon_output .= "ANALYSIS\t$product_name\t$group_name_i\t$group_name_j\tcodon_" . $codon_num . " \t";
# my $curr_comp_num = 0;
# Count number of defined codons in group i
for (my $gi_spp_index = 0; $gi_spp_index < $num_seqs_per_codon_groupi; $gi_spp_index++) { # for each gj sequence at this codon
# my $group_1_species = $group_1_spp[$gi_spp_index];
my $this_species_codon = $groups_codons_aa[$i]->[$codon_index]->[$gi_spp_index];
# my $this_species_codon = $data_codonnum_spp_codon_hh{$codon_num}->{$group_1_species};
if(! ($this_species_codon =~ /N/ || $this_species_codon =~ /-/ || $this_species_codon eq '')) {
$codonum_group_numDefinedCodons_hh{$codon_num}->{group1}++;
}
}
# Count number of defined codons in group j
for (my $gj_spp_index = 0; $gj_spp_index < $num_seqs_per_codon_groupj; $gj_spp_index++) { # for each gj sequence at this codon
# my $group_2_species = $group_2_spp[$gj_spp_index];
my $this_species_codon = $groups_codons_aa[$j]->[$codon_index]->[$gj_spp_index];
# my $this_species_codon = $data_codonnum_spp_codon_hh{$codon_num}->{$group_2_species};
if(! ($this_species_codon =~ /N/ || $this_species_codon =~ /-/ || $this_species_codon eq '')) {
$codonum_group_numDefinedCodons_hh{$codon_num}->{group2}++;
}
}
# ONLY IF POLYMORPHIC between-group (WILL THERE BE DIFFERENCES)
if($polymorphic_codons_arr[$codon_index]) { # polymorphic codon
# print CODON_FILE "polymorphic\t";
# $this_codon_output .= "polymorphic\t";
$this_codon_output .= "polymorphic\t" . $codonum_group_numDefinedCodons_hh{$codon_num}->{group1} .
"\t" . $codonum_group_numDefinedCodons_hh{$codon_num}->{group2} . "\t";
my %comps_hh;
# Sequence (codon) from group 1
for (my $gi_seq_index = 0; $gi_seq_index < $num_seqs_per_codon_groupi; $gi_seq_index++) { # for each gi sequence at this codon
# Sequence (codon) to compare against, group 2
for (my $gj_seq_index = 0; $gj_seq_index < $num_seqs_per_codon_groupj; $gj_seq_index++) { # for each gj sequence at this codon
#?my $group_1_species = $group_1_spp[$gi_spp_index];
#?my $group_2_species = $group_2_spp[$gj_spp_index];
my $codon_gi = $groups_codons_aa[$i]->[$codon_index]->[$gi_seq_index];
my $codon_gj = $groups_codons_aa[$j]->[$codon_index]->[$gj_seq_index];
if(! ($codon_gj =~ 'N' || $codon_gj =~ '-' || $codon_gj eq '' || $codon_gi =~ 'N' || $codon_gi =~ '-' || $codon_gi eq '')) { # they don't have to be the same; syn still stored here
# if(! ($codon_gj =~ 'N') && ! ($codon_gj =~ '-')) { # they don't have to be the same; syn still stored here
$comps_hh{$codon_gi}->{$codon_gj}+=1;
}
}
}
# ^ do this_actual_count here above?
# SUM UP STUFF HERE
my $between_g_comparisons = 0;
my $sum_N_sites = 0;
my $sum_S_sites = 0;
my $sum_N_diffs = 0;
my $sum_S_diffs = 0;
#print"\nBetween groups $i and $j we compare";
foreach my $codon_gi (keys %comps_hh) {
foreach my $codon_gj (keys %{$comps_hh{$codon_gi}}) {
if(($codon_gi =~ /-/ || $codon_gi =~ /N/) && ($codon_gj =~ /-/ || $codon_gj =~ /N/)) { # these will skip all additions
# if($codon_gi =~ /-/ || $codon_gi =~ /N/ || $codon_gj =~ /-/ || $codon_gj =~ /N/) { # these will skip all additions
# print CODON_FILE "COMPLETE_DELETION\tNA\tNA\tNA\tNA\n";
# $this_codon_output .= "COMPLETE_DELETION\tNA\tNA\tNA\tNA\n"; # this was pointless because it's a private string
next PAIRWISE_DELETION;
} else {
my $weight = $comps_hh{$codon_gi}->{$codon_gj};
### NEW POLYMORPHIC SITE ANALYSIS ADDED
# $poly_site_data_hh{$codon_index}->{$codon_si} += $weight;
# $poly_site_data_hh{$codon_index}->{$codon_sj} += $weight;
###
# print CODON_FILE "($codon_gi\-$codon_gj)";
$this_codon_output .= "($codon_gi\-$codon_gj)";
# Sites codon gi
my @codon_gi_sites_1_arr = &get_number_of_sites($codon_gi,1);
my @codon_gi_sites_2_arr = &get_number_of_sites($codon_gi,2);
my @codon_gi_sites_3_arr = &get_number_of_sites($codon_gi,3);
my $codon_gi_N_sites = ($codon_gi_sites_1_arr[0] + $codon_gi_sites_2_arr[0] + $codon_gi_sites_3_arr[0]);
my $codon_gi_S_sites = ($codon_gi_sites_1_arr[1] + $codon_gi_sites_2_arr[1] + $codon_gi_sites_3_arr[1]);
# Site codon gj
my @codon_gj_sites_1_arr = &get_number_of_sites($codon_gj,1);
my @codon_gj_sites_2_arr = &get_number_of_sites($codon_gj,2);
my @codon_gj_sites_3_arr = &get_number_of_sites($codon_gj,3);
my $codon_gj_N_sites = ($codon_gj_sites_1_arr[0] + $codon_gj_sites_2_arr[0] + $codon_gj_sites_3_arr[0]);
my $codon_gj_S_sites = ($codon_gj_sites_1_arr[1] + $codon_gj_sites_2_arr[1] + $codon_gj_sites_3_arr[1]);
#print "\nComparing codons $codon_gi and $codon_gj:\n";
my $mean_comp_N_sites = ($codon_gi_N_sites + $codon_gj_N_sites) / 2;
my $mean_comp_S_sites = ($codon_gi_S_sites + $codon_gj_S_sites) / 2;
$sum_N_sites += $weight * $mean_comp_N_sites;
$sum_S_sites += $weight * $mean_comp_S_sites;
# Differences
my $N_diffs = 0;
my $S_diffs = 0;
if($codon_gi ne $codon_gj) {
my @diffs_arr = &return_avg_diffs($codon_gi,$codon_gj);
$N_diffs = $diffs_arr[0];
$S_diffs = $diffs_arr[1];
}
$sum_N_diffs += $weight * $N_diffs;
$sum_S_diffs += $weight * $S_diffs;
$between_g_comparisons += $weight;
# $this_codon_data .= "$weight\t$mean_comp_N_sites\t$mean_comp_S_sites\t" .
# "$N_diffs\t$S_diffs";
# BOOTSTRAP STORAGE APPROACH
## #my @gi_gj_codon_mm_aa; #gi_gj_codon_mm_aa[group1_index]->[group2_index]->[codon_index]->[N_diffs/S_diffs/N_sites/S_sites]->array of all values for that stat
## if($num_bootstraps > 1) {
## print "\ngroup $i / group $j / codon $codon_index $codon_gi\-$codon_gj / weight $weight starting with comp $curr_comp_num and ending with comp ";
##
## # STORE HERE FOR BOOTSTRAPS
## for(my $k = 1; $k <= $weight; $k++) {
##
##
##
## $gi_gj_codon_comp_stat_hh{$i}->{$j}->{$codon_index}->{$curr_comp_num}->{N_diffs} = $N_diffs; # curr_comp_num starts at 0
## $gi_gj_codon_comp_stat_hh{$i}->{$j}->{$codon_index}->{$curr_comp_num}->{S_diffs} = $S_diffs;
## $gi_gj_codon_comp_stat_hh{$i}->{$j}->{$codon_index}->{$curr_comp_num}->{N_sites} = $mean_comp_N_sites;
## $gi_gj_codon_comp_stat_hh{$i}->{$j}->{$codon_index}->{$curr_comp_num}->{S_sites} = $mean_comp_S_sites;
##
## $curr_comp_num ++;
##
## #push(@{$gi_gj_codon_mm_aa[$i]->[$j]->[$codon_index]->[N_diffs]},$N_diffs);
## #push(@{$gi_gj_codon_mm_aa[$i]->[$j]->[$codon_index]->[S_diffs]},$S_diffs);
## #push(@{$gi_gj_codon_mm_aa[$i]->[$j]->[$codon_index]->[N_sites]},$mean_comp_N_sites);
## #push(@{$gi_gj_codon_mm_aa[$i]->[$j]->[$codon_index]->[S_sites]},$mean_comp_S_sites);
## }
##
## print "$curr_comp_num\n";
## }
#my $random_codon_index = $gi_gj_codon_mm_aa[$i]->[$j]->[int(rand(@{$gi_gj_codon_mm_aa[$i]->[$j]}))];
#my $random_N_diffs = $gi_gj_codon_mm_aa[$i]->[$j]->[$random_codon_index];
#my $random_S_diffs;
#my $random_N_sites;
#my $random_S_sites;
#my $RAND_COMP_NUM = int(rand($THIS_CODON_NUM_COMPS));
#my $random_N_diffs = $gi_gj_codon_comp_stat_hh{$i}->{$j}->{$codon_index}->{$RAND_COMP_NUM}->{N_diffs}
##groups_codons_aa[group_index]->[codon_index]->array of all codons in group
#my @groups_codons_aa;
#push(@{$groups_codons_aa[$group_index]->[$array_index]},$codon);
#my $codon_gi = $groups_codons_aa[$i]->[$codon_index]->[$gi_seq_index];
#my $random_codon = $codonum_codon_aa[$codon_index]->[int(rand(@{$codonum_codon_aa[$codon_index]}))];
}
}
}
# print CODON_FILE "\t";
$this_codon_output .= "\t";
#print "\nfor a total of $between_g_comparisons comparisons\n";
my $mean_N_sites = 0;
my $mean_S_sites = 0;
my $mean_N_diffs = 0;
my $mean_S_diffs = 0;
if($between_g_comparisons > 0) {
$mean_N_sites = ($sum_N_sites / $between_g_comparisons);
$mean_S_sites = ($sum_S_sites / $between_g_comparisons);
$mean_N_diffs = ($sum_N_diffs / $between_g_comparisons);
$mean_S_diffs = ($sum_S_diffs / $between_g_comparisons);
}
# print CODON_FILE "$mean_N_sites\t$mean_S_sites\t$mean_N_diffs\t$mean_S_diffs\n";
$this_codon_output .= "$mean_N_sites\t$mean_S_sites\t$mean_N_diffs\t$mean_S_diffs";
# SLIDING WINDOW & BOOTSTRAP
$codon_data_hh{$codon_num}->{N_sites} = $mean_N_sites;
$codon_data_hh{$codon_num}->{S_sites} = $mean_S_sites;
$codon_data_hh{$codon_num}->{N_diffs} = $mean_N_diffs;
$codon_data_hh{$codon_num}->{S_diffs} = $mean_S_diffs;
###
# $between_product_N_sites_sum += $mean_N_sites;
# $between_product_S_sites_sum += $mean_S_sites;
# $between_product_N_diffs_sum += $mean_N_diffs;
# $between_product_S_diffs_sum += $mean_S_diffs;
###
#print ".. and it's all done.\n";
} else { # not polymorphic; just use first DEFINED codon from the first group because it's conserved
#my $conserved_codon = $groups_codons_aa[$i]->[$codon_index]->[0]; # grab first codon10
my $conserved_codon = '';
## FIND_CONSERVED_CODON: for(my $k = 0; $k < $num_seqs_per_codon_groupi; $k++) { # for each codon in product
##
## my $curr_codon_rep = $groups_codons_aa[$i]->[$codon_index]->[$k]; # grab first codon THAT DOESN'T CONTAIN N or GAP
## if($curr_codon_rep =~ 'N' || $curr_codon_rep =~ '-') {
## next FIND_CONSERVED_CODON;
## } else {
## $conserved_codon = $curr_codon_rep;
## last FIND_CONSERVED_CODON;
## }
## }
FIND_CONSERVED_CODON: for(my $gi_spp_index = 0; $gi_spp_index < $num_seqs_per_codon_groupi; $gi_spp_index++) {
FIND_CONSERVED_G2: for (my $gj_spp_index = 0; $gj_spp_index < $num_seqs_per_codon_groupj; $gj_spp_index++) { # for each gj sequence at this codon
## my $group_1_species = $group_1_spp[$gi_spp_index];
## my $group_2_species = $group_2_spp[$gj_spp_index];
##
## my $codon_gi_rep = $data_codonnum_spp_codon_hh{$codon_num}->{$group_1_species};
## my $codon_gj_rep = $data_codonnum_spp_codon_hh{$codon_num}->{$group_2_species};
my $codon_gi_rep = $groups_codons_aa[$i]->[$codon_index]->[$gi_spp_index];
my $codon_gj_rep = $groups_codons_aa[$j]->[$codon_index]->[$gj_spp_index];
# if($codon_num == 2 && $partition_id == 1) {
# print "\ncodon 2\ngroup_1_species=$group_1_species codon=$codon_gi_rep\ngroup_2_species=$group_2_species codon=$codon_gj_rep\n";
# }
# grab first codon THAT DOESN'T CONTAIN N or GAP
if($codon_gi_rep =~ 'N' || $codon_gi_rep =~ '-' || $codon_gi_rep eq '') {
if($codon_gj_rep =~ 'N' || $codon_gj_rep =~ '-' || $codon_gj_rep eq '') {
#print "\ncodon $codon_num codons are codon_gi_rep $codon_gi_rep and codon_gj_rep $codon_gj_rep and neither work\n";
next FIND_CONSERVED_G2;
} else {
$conserved_codon = $codon_gj_rep;
# if($codon_num == 2 && $partition_id == 1) {
# print "\ncodon $codon_num codons are codon_gi_rep $codon_gi_rep and codon_gj_rep $codon_gj_rep and the last works\n";
# }
last FIND_CONSERVED_CODON;
}
} else {
$conserved_codon = $codon_gi_rep;
# if($codon_num == 2 && $partition_id == 1) {
# print "\ncodon $codon_num codons are codon_gi_rep $codon_gi_rep and codon_gj_rep $codon_gj_rep and the first works\n";
# }
last FIND_CONSERVED_CODON;
}
}
}
# Print a warning if no conserved codon was found
if($conserved_codon eq '') {
warn "\n### WARNING: no codons found in between-group analysis at codon position $codon_num\.\n".
"### Most likely, the selected groups consist of all gaps at this codon, which is not necessarily a problem.\n".
"### Inserting gaps and proceeding.\n\n";
$conserved_codon = '---';
}
#STORE THIS CONSERVED CODON FOR LATER USE IN BOOTSTRAPPING
# $codonIndex_conserved{$codon_index} = $conserved_codon;
#HERE
my @codon_sites_1_arr = &get_number_of_sites($conserved_codon,1);
my @codon_sites_2_arr = &get_number_of_sites($conserved_codon,2);
my @codon_sites_3_arr = &get_number_of_sites($conserved_codon,3);
my $codon_N_sites = ($codon_sites_1_arr[0] + $codon_sites_2_arr[0] + $codon_sites_3_arr[0]);
my $codon_S_sites = ($codon_sites_1_arr[1] + $codon_sites_2_arr[1] + $codon_sites_3_arr[1]);
# print CODON_FILE "conserved\t$conserved_codon\t".
# "$codon_N_sites\t$codon_S_sites\t0\t0\n";
## $this_codon_output .= "conserved\t$conserved_codon\t".
## "$codon_N_sites\t$codon_S_sites\t0\t0";
# $between_product_N_sites_sum += $codon_N_sites;
# $between_product_S_sites_sum += $codon_S_sites;
$this_codon_output .= "conserved\t" . $codonum_group_numDefinedCodons_hh{$codon_num}->{group1} .
"\t" . $codonum_group_numDefinedCodons_hh{$codon_num}->{group2} . "\t$conserved_codon\t".
"$codon_N_sites\t$codon_S_sites\t0\t0";
# SLIDING WINDOW & BOOTSTRAP
$codon_data_hh{$codon_num}->{N_sites} = $codon_N_sites;
$codon_data_hh{$codon_num}->{S_sites} = $codon_S_sites;
#if($product_name =~ /2K/) {
# print "\nProduct $product_name, conserved codon $conserved_codon N sites $codon_N_sites\n";
#}
# BOOTSTRAP STORAGE APPROACH
## # STORE HERE FOR BOOTSTRAPS: just one value
## if($num_bootstraps > 1) {
## $gi_gj_codon_comp_stat_hh{$i}->{$j}->{$codon_index}->{$curr_comp_num}->{N_diffs} = 0; # curr_comp_num starts at 0
## $gi_gj_codon_comp_stat_hh{$i}->{$j}->{$codon_index}->{$curr_comp_num}->{S_diffs} = 0;
## $gi_gj_codon_comp_stat_hh{$i}->{$j}->{$codon_index}->{$curr_comp_num}->{N_sites} = $codon_N_sites;
## $gi_gj_codon_comp_stat_hh{$i}->{$j}->{$codon_index}->{$curr_comp_num}->{S_sites} = $codon_S_sites;
## }
} # end not polymorphic
open(THIS_CODON_TEMP_FILE,">>$codon_index");
print THIS_CODON_TEMP_FILE "$this_codon_output";
close THIS_CODON_TEMP_FILE;
$pm_codons->finish; # special name
} # end all codons in product
# PARALLEL
$pm_codons->wait_all_children; # special name, methods within module
# Calculate overall values for the product
my $between_product_N_sites_sum = 0;
my $between_product_S_sites_sum = 0;
my $between_product_N_diffs_sum = 0;
my $between_product_S_diffs_sum = 0;
# Vacuum up output and delete temp files
my @codon_lines_arr;
my @codon_analysis_FILES_arr = glob "*";
@codon_analysis_FILES_arr = sort {$a <=> $b} @codon_analysis_FILES_arr;
#print "sorted polymorphic_codons_FILE_arr: @polymorphic_codons_FILES_arr\n";
foreach(@codon_analysis_FILES_arr) { # file names, actually
my $codon_file = $_; # this is the INDEX
open(CURR_CODON_FILE, $codon_file) or die "\n## Cannot open $codon_file. TERMINATED.\n\n";
while(<CURR_CODON_FILE>) {
chomp;
push(@codon_lines_arr,$_);
my @this_line_arr = split(/\t/,$_,-1);
#analysis / product / group_1 / group_2 / codon / variability / comparisons / N_sites / S_sites / N_diffs / S_diffs
my $poly = $this_line_arr[5]; #?
# IF POLY
if($poly eq 'polymorphic') {
$between_product_N_sites_sum += $this_line_arr[9];
$between_product_S_sites_sum += $this_line_arr[10];
$between_product_N_diffs_sum += $this_line_arr[11];
$between_product_S_diffs_sum += $this_line_arr[12];
# IF CONSERVED
} elsif($poly eq 'conserved') {
$between_product_N_sites_sum += $this_line_arr[9];
$between_product_S_sites_sum += $this_line_arr[10];
#STORE THIS CONSERVED CODON FOR LATER USE IN BOOTSTRAPPING