-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathidentify_founders.pl
1712 lines (1582 loc) · 93.8 KB
/
identify_founders.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/perl -w
##---------------------------------------------------------------------------##
## File:
## @(#) identify_founders
## Author:
## Paul Thatcher Edlefsen [email protected]
## Description:
##
## Script for using LANL and DiveIn tools (at the Mullins lab web site) to
## estimate the number of founders and perform sequence
## reconstruction of the founders.
##
## Note that this creates output files in subdirectories named
## after the input fasta file name (unless you specify an output dir).
##
## R packages you'll need: (see installRPackages.R).
## ade4
## ape
## dynamicTreeCut
## entropy
## ggplot2
## binom
## source("https://bioconductor.org/biocLite.R")
## biocLite("Biostrings")
## biocLite("seqinr")
##
## Try: rm -r rv217_1W_gold_standard-hiv-founder-id_-fr_resultDir/; mkdir rv217_1W_gold_standard-hiv-founder-id_-fr_resultDir/; perl -w ./identify_founders.pl -fr -O rv217_1W_gold_standard-hiv-founder-id_-fr_resultDir/ ~/src/from-git/projects/tholzman/MorgansFounderIDMethod/rv217_1W_gold_standard.list > rv217_1W_gold_standard-hiv-founder-id_-fr_resultDir/identify-founders.out
## Or: rm -r caprisa002_1W_gold_standard-hiv-founder-id_-rP_resultDir/; mkdir caprisa002_1W_gold_standard-hiv-founder-id_-rP_resultDir/; perl ./identify_founders.pl -rP -O caprisa002_1W_gold_standard-hiv-founder-id_-rP_resultDir/ caprisa002_1W_gold_standard.list > caprisa002_1W_gold_standard-hiv-founder-id_-rP_resultDir/identify-founders.out
## This next one skips RAP because it seems to be exceptionally slow with these large numbers of sequences. Unsure how to proceed - maybe iterately evaluate subsets? Or use a different program.
## Or: mkdir caprisa002_1W_gold_standard-hiv-founder-id_resultDir/; perl ./identify_founders.pl -V -R -P -O caprisa002_1W_gold_standard-hiv-founder-id_resultDir/ caprisa002_1W_gold_standard.list > caprisa002_1W_gold_standard-hiv-founder-id_resultDir/identify-founders.out
## Or: mkdir CAPRISA002_ft_seqs-hiv-founder-id_resultDir/; perl ./identify_founders.pl -O CAPRISA002_ft_seqs-hiv-founder-id_resultDir/ ~/src/from-git/projects/tholzman/MorgansFounderIDMethod/CAPRISA002_ft_seqs.txt > CAPRISA002_ft_seqs-hiv-founder-id_resultDir/identify-founders.out
## Or: rm -r Abrahams-2009aa-hiv-founder-id_-fr_resultDir/; mkdir Abrahams-2009aa-hiv-founder-id_-fr_resultDir/; perl ./identify_founders.pl -fr -O Abrahams-2009aa-hiv-founder-id_-fr_resultDir/ Abrahams-2009aa/preparedFor_hiv-identify-founders.list > Abrahams-2009aa-hiv-founder-id_-fr_resultDir/identify-founders.out
###******************************************************************************
# Load a pipeline_dir variable to export it as an environment variable for the R scripts
# This is needed because of some weird interaction between
# R; perl and neovim's terminal emulator
use Cwd qw(cwd);
my $pipeline_dir = cwd;
use Getopt::Std; # for getopts
use File::Path qw( make_path );
use Path::Tiny;
require Sort::Fields; # for ??
use strict;
use vars qw( $opt_D $opt_V $opt_o $opt_O $opt_C $opt_P $opt_R $opt_F $opt_E $opt_H $opt_f $opt_w $opt_I $opt_i $opt_v $opt_T $opt_r $opt_t );
use vars qw( $VERBOSE $DEBUG );
sub splitFastaFileOnHeaderPatterns {
## NOTE: If there is only one sequence in a group, we don't bother writing out the file.
my $output_path_dir = shift @_;
my $input_fasta_file = shift @_;
my @header_patterns = @_;
## TODO: REMOVE/FIX MAGIC # $newline_column.
our $newline_column = 40;
## TODO: REMOVE/FIX MAGIC # $minimum_sequences_for_output_file.
our $minimum_sequences_for_output_file = 2;
my ( $input_fasta_file_path, $input_fasta_file_short ) =
( $input_fasta_file =~ /^(.*?)\/([^\/]+)$/ );
unless( $input_fasta_file_short ) {
$input_fasta_file_short = $input_fasta_file;
$input_fasta_file_path = ".";
}
my ( $input_fasta_file_short_nosuffix, $input_fasta_file_suffix ) =
( $input_fasta_file_short =~ /^([^\.]+)(\..+)?$/ );
if( $VERBOSE ) { print "Opening input Fasta file \"$input_fasta_file\".."; }
open( INPUT_FASTA_FH, $input_fasta_file ) or
die "Unable to open fasta file \"$input_fasta_file\": $!";
if( $VERBOSE ) { print ".done.\n"; }
if( $VERBOSE ) { print "Parsing Fasta file.."; }
my $header_patterns_together = join( "|", @header_patterns );
my $sequence;
my $sequence_header;
my $have_a_match = 0;
my %sequences_by_pattern;
while( <INPUT_FASTA_FH> ) {
if( /^>/ ) {
if( defined( $sequence ) ) {
foreach my $header_pattern ( @header_patterns ) {
my $header_pattern_copy = $header_pattern;
$header_pattern_copy =~ s/\|/\\\|/; # escape any bars
if( $sequence_header =~ m/$header_pattern_copy/ ) {
if( !exists( $sequences_by_pattern{ $header_pattern } ) ) {
$sequences_by_pattern{ $header_pattern } = {};
}
if( exists( $sequences_by_pattern{ $header_pattern }{ $sequence_header } ) ) {
die( "Found multiple sequences named '$sequence_header'!" );
}
$sequences_by_pattern{ $header_pattern }{ $sequence_header } = $sequence;
last; # So it can't match more than one pattern.
}
} # End foreach $header_pattern
undef $sequence;
}
$sequence_header = $_;
$have_a_match = ( $sequence_header =~ m/$header_patterns_together/ );
if( $DEBUG ) {
print "Found sequence header: $sequence_header is a match to a pattern:";
foreach my $header_pattern ( @header_patterns ) {
my $header_pattern_copy = $header_pattern;
$header_pattern_copy =~ s/\|/\\\|/; # escape any bars
if( $sequence_header =~ m/$header_pattern_copy/ ) {
print "$header_pattern_copy\n";
last; # So it can't match more than one pattern.
}
}
}
} elsif( $have_a_match ) {
# Get rid of all preexisting whitespace
$_ =~ s/\s//g;
$sequence .= $_;
}
} # End foreach $_ <input_fasta_fileFH>
if( defined( $sequence ) ) {
foreach my $header_pattern ( @header_patterns ) {
my $header_pattern_copy = $header_pattern;
$header_pattern_copy =~ s/\|/\\\|/; # escape any bars
if( $sequence_header =~ m/$header_pattern_copy/ ) {
if( !exists( $sequences_by_pattern{ $header_pattern } ) ) {
$sequences_by_pattern{ $header_pattern } = {};
}
if( exists( $sequences_by_pattern{ $header_pattern }{ $sequence_header } ) ) {
die( "Found multiple sequences named '$sequence_header'!" );
}
$sequences_by_pattern{ $header_pattern }{ $sequence_header } = $sequence;
last; # So it can't match more than one pattern.
}
} # End foreach $header_pattern
undef $sequence;
}
if( $VERBOSE ) { print ".done.\n"; }
if( $VERBOSE ) { print "Closing file \"$input_fasta_file\".."; }
close INPUT_FASTA_FH;
if( $VERBOSE ) { print ".done.\n"; }
sub print_sequence {
my $OUTFH = shift;
my $sequence = shift;
my $seq_len = length( $sequence );
for( my $seq_pos = 0; $seq_pos < $seq_len; $seq_pos += $newline_column ) {
if( ( $seq_pos + $newline_column ) > $seq_len ) {
print $OUTFH substr( $sequence, $seq_pos, ( $seq_len - $seq_pos ) ), "\n\n";
} else {
print $OUTFH substr( $sequence, $seq_pos, $newline_column ), "\n";
}
}
} # print_sequence (..)
my @output_files;
foreach my $header_pattern ( @header_patterns ) {
if( $DEBUG ) {
print "--- $header_pattern ---\n";
}
next unless ( exists( $sequences_by_pattern{ $header_pattern } ) );
if( scalar( keys %{ $sequences_by_pattern{ $header_pattern } } ) < $minimum_sequences_for_output_file ) {
if( $VERBOSE ) {
print "Skipping $header_pattern because only ", scalar( keys %{ $sequences_by_pattern{ $header_pattern } } ), " sequence matches it, which is not enough (we require a minimum of $minimum_sequences_for_output_file).\n";
}
next;
}
my $header_pattern_copy = $header_pattern;
$header_pattern_copy =~ s/\|//; # remove any bars from the pattern
my $output_file = $output_path_dir . '/' . $input_fasta_file_short_nosuffix . '_' . $header_pattern_copy . $input_fasta_file_suffix;
if( $VERBOSE ) { print "Opening file \"$output_file\" for writing.."; }
my $OUTPUT_FH;
unless( open $OUTPUT_FH, ">$output_file" ) {
warn "Unable to open output file \"$output_file\": $!";
return 1;
}
if( $VERBOSE ) { print ".done.\n"; }
if( $VERBOSE ) { print "Printing sequences with headers matching pattern \"$header_pattern\""; }
foreach my $sequence_header ( keys %{ $sequences_by_pattern{ $header_pattern } } ) {
if( $VERBOSE ) { print "."; }
print $OUTPUT_FH $sequence_header; # newline is still there.
print_sequence( $OUTPUT_FH, $sequences_by_pattern{ $header_pattern }{ $sequence_header } );
}
if( $VERBOSE ) { print ".done.\n"; }
if( $VERBOSE && $output_file ) { print "Closing file \"$output_file\".."; }
close $OUTPUT_FH;
if( $VERBOSE && $output_file ) { print ".done.\n"; }
push @output_files, $output_file;
} # End foreach $header_pattern
return( @output_files );
} # sub splitFastaFileOnHeaderPatterns (..)
sub identify_founders {
@ARGV = @_;
sub identify_founders_usage {
print "\tidentify_founders [-DV] [-CPRFEHfnIsr] [-i <insites_threshold>] [-v <insites_threshold_v3>] [-t <identify.founders.tab output filename>] [-(o|O) <output_dir>] <input_fasta_file1 or file_listing_input_files> [<input_fasta_file2> ...] \n";
exit;
}
# This means -D, -o, -O, -V, ... are ok, but nothin' else.
# opt_D means print debugging output.
# opt_o is an optional directory to put the output; default depends on input filename.
# opt_O is just like opt_o. Same thing.
# opt_V means be verbose.
# opt_C means skip (do not run) the PhyML diversity or InSites analysis and the primary founder call (which depends on those)
# opt_P means skip (do not run) profillic
# opt_R means skip (do not run) RAPR (alignment-internal recombination detection program)
# opt_F means skip (do not run) PFitter
# opt_E means skip (do not run) entropy statistics calculation
# opt_H means skip (do not run) HYPERMUT 2.0 hypermutation detection
# opt_f means fix hypermutated sequences, instead of removing them.
# opt_w is what we should fix hypermutated sequences with (default: R)
# opt_I means run inSites online (instead of offline)
# opt_i is the insites threshold to use for all regions except v3 (default: 0.85).
# opt_v is the insites threshold to use for v3 (default: 0.33).
# opt_T means don't train the profillic profile, just use the converted alignment directly.
# opt_r means recursively operate on clusters identified using the Informtive Sites method.
# opt_t is the name of the tab-delimited output file (default: "identify_founders.tab")
# But first reset the opt vars.
( $opt_D, $opt_V, $opt_o, $opt_O, $opt_C, $opt_P, $opt_R, $opt_F, $opt_E, $opt_H, $opt_f, $opt_w, $opt_I, $opt_i, $opt_v, $opt_T, $opt_r, $opt_t ) = ();
if( not getopts('DVo:O:CPRFEHfw:Ii:v:Trt:') ) {
identify_founders_usage();
}
$DEBUG ||= $opt_D;
$VERBOSE ||= $opt_V;
my $run_InSites_and_PhyML = !$opt_C;
my $run_profillic = !$opt_P;
my $run_RAP = !$opt_R;
my $run_PFitter = !$opt_F;
my $run_entropy = !$opt_E;
my $run_Hypermut = !$opt_H;
my $fix_hypermutated_sequences = $opt_f || 0;
my $fix_hypermutated_sequences_with = $opt_w || "R";
my $in_sites_ratio_threshold = $opt_i || 0.85; # For Abrahams and RV217
my $in_sites_ratio_threshold_v3 = $opt_v || 0.33; # For caprisa002
my $runInSites_online = $opt_I || 0;
my $train_profillic_profile = !$opt_T;
my $recurse_on_clusters = $opt_r || 0;
## TODO: DEHACKIFY MAGIC #s
my $mean_diversity_threshold = 0.001;
my $RAP_pValueThreshold = 0.0007; # Appears to be the suggestion from the output file "(summaryTable)"'s column header, which reads "Pvalues<0.0007".
my $hypermut2_pValueThreshold = 0.1; # Matches Abrahams 2009
my $old_autoflush;
if( $VERBOSE ) {
select STDOUT;
$old_autoflush = $|;
$| = 1; # Autoflush.
}
my @fasta_files_remaining_to_process = @ARGV;
my @all_input_fasta_files = @fasta_files_remaining_to_process;
unless( scalar @fasta_files_remaining_to_process ) { identify_founders_usage(); }
# Special case: if there is only one file, it might be a textfile
# containing names of files.
if( scalar( @fasta_files_remaining_to_process ) == 1 ) {
#print $fasta_files_remaining_to_process[ 0 ], "\n";
unless( ( $fasta_files_remaining_to_process[ 0 ] ) =~ /\.fast?a?$/ ) {
# Try opening it, see if it's a list of files.
if( $VERBOSE ) {
print "Reading file names from file \"", $fasta_files_remaining_to_process[ 0 ], "\"..";
}
my $input_fasta_file_contents = path( $fasta_files_remaining_to_process[ 0 ] )->slurp();
if( $VERBOSE ) {
print ".done\n";
}
if( $DEBUG ) {
print $input_fasta_file_contents;
}
@fasta_files_remaining_to_process = split( "\n", $input_fasta_file_contents );
@all_input_fasta_files = @fasta_files_remaining_to_process;
}
}
my $output_path_dir = $opt_o || $opt_O || undef;
# Remove the trailing "/" if any
if( defined( $output_path_dir ) ) {
( $output_path_dir ) = ( $output_path_dir =~ /^(.+)\/*$/ );
}
if( defined $output_path_dir ) {
make_path( $output_path_dir );
} else {
$output_path_dir = ".";
}
my $output_tab_file = $opt_t || "identify_founders.tab";
my $extra_flags = "";
if( $DEBUG ) {
$extra_flags .= "-D ";
}
if( $VERBOSE ) {
$extra_flags .= "-V ";
}
#my %subtable_sharing_original_short_name_stripped; # Arrays, one per file with the same prefix (for multi-region)
my $output_table_file = $output_path_dir . '/' . $output_tab_file;
if( $VERBOSE ) { print "Opening file \"$output_table_file\" for writing.."; }
unless( open OUTPUT_TABLE_FH, ">$output_table_file" ) {
warn "Unable to open output file \"$output_table_file\": $!";
return 1;
}
# Turn on autoflush for this too.
my $old_fh = select( OUTPUT_TABLE_FH );
$| = 1;
select( $old_fh );
if( $VERBOSE ) { print ".done.\n"; }
if( $VERBOSE ) { print "Writing results table to file \"$output_table_file\".."; }
my @table_column_headers =
(
"infile"
);
if( $run_Hypermut ) {
if( $fix_hypermutated_sequences ) {
push @table_column_headers, "fixed-hypermut";
} else {
push @table_column_headers, "removed-hypermut";
}
}
if( $run_RAP ) {
push @table_column_headers, "removed-recomb";
}
push @table_column_headers,
(
"file"
);
if( $run_InSites_and_PhyML ) {
push @table_column_headers,
(
"num.seqs", "num.diversity.seqs", "diversity",
"inf.sites","priv.sites","inf.to.priv.ratio",
"exceeds.diversity.threshold", "exceeds.ratio.threshold", "ratio.threshold",
"InSites.is.one.founder"
);
}
if( $run_entropy ) {
push @table_column_headers,
(
"mean.entropy", "sd.entropy"
);
}
if( $run_PFitter ) {
push @table_column_headers,
(
"PFitter.lambda",
"PFitter.se",
"PFitter.nseq",
"PFitter.nbases",
"PFitter.mean.hd",
"PFitter.max.hd",
"PFitter.time.est",
"PFitter.time.ci.low",
"PFitter.time.ci.high",
"PFitter.chi.sq.stat",
"PFitter.chi.sq.df",
"PFitter.chi.sq.p.value",
"PFitter.is.poisson",
"PFitter.is.starlike",
"DSStarPhyTest.assertion.low",
"DSStarPhyTest.assertion.high",
"DSStarPhyTest.fits",
"DSStarPhyTest.P",
"DSStarPhyTest.Q",
"DSStarPhyTest.R",
"Synonymous.PFitter.lambda",
"Synonymous.PFitter.se",
"Synonymous.PFitter.nseq",
"Synonymous.PFitter.nbases",
"Synonymous.PFitter.mean.hd",
"Synonymous.PFitter.max.hd",
"Synonymous.PFitter.time.est",
"Synonymous.PFitter.time.ci.low",
"Synonymous.PFitter.time.ci.high",
"Synonymous.PFitter.chi.sq.stat",
"Synonymous.PFitter.chi.sq.df",
"Synonymous.PFitter.chi.sq.p.value",
"Synonymous.PFitter.is.poisson",
"Synonymous.PFitter.is.starlike",
"Synonymous.DSStarPhyTest.assertion.low",
"Synonymous.DSStarPhyTest.assertion.high",
"Synonymous.DSStarPhyTest.fits",
"Synonymous.DSStarPhyTest.P",
"Synonymous.DSStarPhyTest.Q",
"Synonymous.DSStarPhyTest.R",
"StarPhy.is.one.founder"
);
push @table_column_headers,
(
"multifounder.PFitter.lambda",
"multifounder.PFitter.se",
"multifounder.PFitter.nseq",
"multifounder.PFitter.nbases",
"multifounder.PFitter.mean.hd",
"multifounder.PFitter.max.hd",
"multifounder.PFitter.time.est",
"multifounder.PFitter.time.ci.low",
"multifounder.PFitter.time.ci.high",
"multifounder.PFitter.chi.sq.stat",
"multifounder.PFitter.chi.sq.df",
"multifounder.PFitter.chi.sq.p.value",
"multifounder.PFitter.is.poisson",
# "multifounder.PFitter.is.starlike",
"multifounder.DSStarPhyTest.assertion.low",
"multifounder.DSStarPhyTest.assertion.high",
"multifounder.DSStarPhyTest.fits",
"multifounder.DSStarPhyTest.P",
"multifounder.DSStarPhyTest.Q",
"multifounder.DSStarPhyTest.R",
"multifounder.Synonymous.PFitter.lambda",
"multifounder.Synonymous.PFitter.se",
"multifounder.Synonymous.PFitter.nseq",
"multifounder.Synonymous.PFitter.nbases",
"multifounder.Synonymous.PFitter.mean.hd",
"multifounder.Synonymous.PFitter.max.hd",
"multifounder.Synonymous.PFitter.time.est",
"multifounder.Synonymous.PFitter.time.ci.low",
"multifounder.Synonymous.PFitter.time.ci.high",
"multifounder.Synonymous.PFitter.chi.sq.stat",
"multifounder.Synonymous.PFitter.chi.sq.df",
"multifounder.Synonymous.PFitter.chi.sq.p.value",
"multifounder.Synonymous.PFitter.is.poisson",
# "multifounder.Synonymous.PFitter.is.starlike",
"multifounder.Synonymous.DSStarPhyTest.assertion.low",
"multifounder.Synonymous.DSStarPhyTest.assertion.high",
"multifounder.Synonymous.DSStarPhyTest.fits",
"multifounder.Synonymous.DSStarPhyTest.P",
"multifounder.Synonymous.DSStarPhyTest.Q",
"multifounder.Synonymous.DSStarPhyTest.R"
);
} # End if $run_PFitter
if( $run_profillic ) {
push @table_column_headers, "profillic.clusters", "profillic.founders";
} # End if $run_profillic
if( $run_InSites_and_PhyML || $run_PFitter ) {
push @table_column_headers, "inf.sites.clusters";
}
if( $run_InSites_and_PhyML ) {
push @table_column_headers, "InSites.founders";
}
if( $run_PFitter ) {
push @table_column_headers, "StarPhy.founders";
}
my $table_header = join( "\t", @table_column_headers );
print OUTPUT_TABLE_FH $table_header, "\n";
# After removing/fixing hypermutated sequences and removing recombined sequences, run InSites (online) and get the informative:private site ratio stat; also run PhyML (locally) to get the mean pairwise diversity statistic, and also cluster the informative sites subalignments and compute the full consensus sequences for each cluster.
my $id_string = "";
my $output_path_dir_for_input_fasta_file;
my $R_output;
my $input_fasta_file;
my $fasta_file;
my $original_short_name_nosuffix_stripped;
my %final_input_fasta_file_short_names_by_original_short_name_stripped;
while ( scalar( @fasta_files_remaining_to_process ) > 0 ) {
$input_fasta_file = shift @fasta_files_remaining_to_process;
$fasta_file = $input_fasta_file;
if( $VERBOSE ) {
print $fasta_file, "\n";
}
my ( $fasta_file_path, $fasta_file_short ) =
( $fasta_file =~ /^(.*?)\/([^\/]+)$/ );
unless( $fasta_file_short ) {
$fasta_file_short = $fasta_file;
$fasta_file_path = ".";
}
my ( $fasta_file_short_nosuffix, $fasta_file_suffix ) =
( $fasta_file_short =~ /^([^\.]+)(\..+)?$/ );
if( defined $output_path_dir ) {
$output_path_dir_for_input_fasta_file = $output_path_dir;
} else {
$output_path_dir_for_input_fasta_file =
$fasta_file_path . "/" . $fasta_file_short_nosuffix . "_hiv-founder-id_resultsDir";
}
# remove trailing "/"
( $output_path_dir_for_input_fasta_file ) = ( $output_path_dir_for_input_fasta_file =~ /^(.*[^\/])\/*$/ );
if( $fasta_file_short_nosuffix =~ /^(.+)(?:(?:_[LR]H)|(?:_NFLG)|(?:_env))/ ) {
( $original_short_name_nosuffix_stripped ) =
( $fasta_file_short_nosuffix =~ /^(.+)(?:(?:_[LR]H)|(?:_NFLG)|(?:_env))/ );
} else {
$original_short_name_nosuffix_stripped =
$fasta_file_short_nosuffix;
}
# Duplicate the input file, possibly modifying it.
my $fasta_file_contents = path( $fasta_file )->slurp();
my ( @seq_headers ) = ( $fasta_file_contents =~ /^>(.*)$/mg );
if( scalar( @seq_headers ) == 1 ) {
# THE INPUT FASTA FILE CONTAINS ONLY ONE SEQUENCE. SKIP IT.
#if( $VERBOSE ) {
print( "\nThere's only one sequence in input file \"$fasta_file\". Skipping it.\n" );
#}
next;
}
## HACK: make sure there are no bangs in the input file (since sometimes there are, right now).
if( $fasta_file_contents =~ /\!/ ) {
if( $VERBOSE ) {
print( "Input file \"$fasta_file\" contains illegal characters \"!\"; changing them to gaps, saving to output directory.\n" );
## TODOL REMOVE
#print( $output_path_dir_for_input_fasta_file );
}
$fasta_file_contents =~ s/\!/-/g;
} else {
if( $VERBOSE ) {
print( "Copying input file \"$fasta_file\" to output directory.\n" );
}
}
# Now write it out to a temporary location in the output dir.
$fasta_file_path = $output_path_dir_for_input_fasta_file;
$fasta_file = "${fasta_file_path}/${fasta_file_short}";
if( $VERBOSE ) {
if( $fasta_file_contents =~ /\!/ ) {
print( "Writing out fixed input file \"$fasta_file\".." );
} else {
print( "Writing out copy of input file to \"$fasta_file\".." );
}
}
if( $VERBOSE ) { print "Opening file \"$fasta_file\" for writing..\n"; }
unless( open input_fasta_fileFH, ">$fasta_file" ) {
warn "Unable to open output file \"$fasta_file\": $!\n";
return 1;
}
( @seq_headers ) = map { s/\!/-/g } @seq_headers;
print input_fasta_fileFH $fasta_file_contents;
close( input_fasta_fileFH );
if( ( $fasta_file_contents =~ /RH\|/ ) && ( $fasta_file_contents =~ /LH\|/ ) ) {
## SPECIAL CASE: If the input file contains left-half and right-half genomes, separate them into separate input files and do both.
if( $VERBOSE ) {
print( "Input file contains left- and right- halves of the genome; separating them.\n" );
}
my @new_input_fasta_files =
splitFastaFileOnHeaderPatterns( $output_path_dir_for_input_fasta_file, $fasta_file, , "env\|", "NFLG\|", "LH\|", "RH\|" );
if( $VERBOSE ) {
print( "Queueing the new files ( ", join( ", ", @new_input_fasta_files ), " ).\n" );
}
push @all_input_fasta_files, @new_input_fasta_files;
unshift @fasta_files_remaining_to_process, @new_input_fasta_files;
next; # That's all we do with the original input file.
}
# This is the original unaltered fasta file (path removed), printed to the table out:
print OUTPUT_TABLE_FH $fasta_file_short;
print "\nInput Fasta file: $fasta_file_short\n";
# First fix/remove hypermutated sequences, using an implementation of the HYPERMUT 2.0 algorithm.
if( $run_Hypermut ) {
if( $VERBOSE ) {
if( $fix_hypermutated_sequences ) {
print "Calling R to fix hypermutated sequences (with $fix_hypermutated_sequences_with)..";
} else {
print "Calling R to remove hypermutated sequences..";
}
}
#my $R_hypermut_command = "\"export hiv_founder_pipeline_dir=\"$pipeline_dir\"; export removeHypermutatedSequences_fixWith=\"$fix_hypermutated_sequences_with\"; export removeHypermutatedSequences_fixInsteadOfRemove=\"$fix_hypermutated_sequences\"; export removeHypermutatedSequences_pValueThreshold=\"$hypermut2_pValueThreshold\"; export removeHypermutatedSequences_inputFilename=\"$fasta_file\"; export removeHypermutatedSequences_outputDir=\"$output_path_dir_for_input_fasta_file\"; R -f removeHypermutatedSequences.R --vanilla --slave; cd ..\"";
my $R_hypermut_command = "Rscript hypermutR.R --input_file=\"$fasta_file\" --p_value=\"$hypermut2_pValueThreshold\" --ancestor=\"consensus\"";
my ( $fasta_file_short_hypermut, $hypermut_output_file );
if( $fix_hypermutated_sequences ) {
$fasta_file_short_hypermut = "${fasta_file_short_nosuffix}_fixHypermutatedSequencesWith${fix_hypermutated_sequences_with}.fasta";
$hypermut_output_file = "${output_path_dir_for_input_fasta_file}/${fasta_file_short_hypermut}";
$R_hypermut_command .= " --output_file=\"$hypermut_output_file\" --fix_with=\"$fix_hypermutated_sequences_with\"";
} else {
$fasta_file_short_hypermut = "${fasta_file_short_nosuffix}_removeHypermutatedSequences.fasta";
$hypermut_output_file = "${output_path_dir_for_input_fasta_file}/${fasta_file_short_hypermut}";
$R_hypermut_command .= " --output_file=\"$hypermut_output_file\"";
}
my $hypermut_removed_fasta_file = $hypermut_output_file;
$hypermut_removed_fasta_file =~ s/\.fasta/\_hypermutants\.fasta/;
## TODO: REMOVE
#print( "\$hypermut_removed_fasta_file = $hypermut_removed_fasta_file\n" );
if( -e $hypermut_removed_fasta_file ) {
my $result_ignored = `rm $hypermut_removed_fasta_file`;
}
if( $VERBOSE ) {
print( "RUNNING:\n$R_hypermut_command\n" );
}
$R_output = `$R_hypermut_command`;
if( $VERBOSE ) {
print( "GOT:\n$R_output\n" );
}
## extract the number fixed/removed from the output
my $num_hypermut_sequences = 0;
if( -e $hypermut_removed_fasta_file ) {
# TODO:REMOVE
#print( "THE FILE EXISTS" );
my $hypermut_removed_fasta_file_contents = path( $hypermut_removed_fasta_file )->slurp();
my ( @hypermut_removed_fasta_file_seq_headers ) = ( $hypermut_removed_fasta_file_contents =~ /^>(.*)$/mg );
## TODO: Do we need to multiply these by the # if they were previously deduplicated?
$num_hypermut_sequences = scalar( @hypermut_removed_fasta_file_seq_headers );
}
# if( $VERBOSE ) {
if( $fix_hypermutated_sequences ) {
print( "The number of hypermutated sequences fixed is: $num_hypermut_sequences\n" );
} else {
print( "The number of hypermutated sequences removed is: $num_hypermut_sequences\n" );
}
# }
print OUTPUT_TABLE_FH "\t", $num_hypermut_sequences;
# Now use the output from that..
if( $num_hypermut_sequences > 0 ) {
$fasta_file_path = $output_path_dir_for_input_fasta_file;
$fasta_file_short = $fasta_file_short_hypermut;
if( !$fix_hypermutated_sequences ) {
# Recompute the seq_headers.
$fasta_file_contents = path( "${fasta_file_path}/${fasta_file_short}" )->slurp();
( @seq_headers ) = ( $fasta_file_contents =~ /^>(.*)$/mg );
}
( $fasta_file_short_nosuffix, $fasta_file_suffix ) =
( $fasta_file_short =~ /^([^\.]+)(\..+)?$/ );
$fasta_file = "${fasta_file_path}/${fasta_file_short}";
} # End if we need to change files..
if( $VERBOSE ) {
print ".done. \$fasta_file is now $fasta_file\n";
}
} # End if $run_Hypermut
if( $run_RAP ) {
## Run RAPR on LANL, which gives individual
## sequences that are recombinants of other individual sequences,
## allowing those to be flagged for removal just like hypermutated
## ones.
if( $VERBOSE ) {
print "Running RAPR at LANL to compute recombined sequences..";
}
my $removed_recombined_sequences = 0;
# Note we use verbosity for RAPR to parse its output.
my $RAP_result_stdout = `perl runRAPROnline.pl $extra_flags -V -i $fasta_file -o ${output_path_dir_for_input_fasta_file}/${fasta_file_short_nosuffix}_RAPR.fasta`;
if( 1 || $VERBOSE ) {
print "GOT FROM runRAPROnline.pl:\n$RAP_result_stdout\n";
}
my ( $ElimDups_output_file ) = ( $RAP_result_stdout =~ /^d => (.+)\s*$/m );
if( !defined( $ElimDups_output_file ) ) { # No duplicates.
$ElimDups_output_file = "";
}
## TODO: REMOVE
#print( "\$ElimDups_output_file is $ElimDups_output_file\n" );
my ( $RAPR_fasta_output_file ) = ( $RAP_result_stdout =~ /^f => (.+)\s*$/m );
if( !defined( $RAPR_fasta_output_file ) ) {
$RAPR_fasta_output_file = "";
}
## TODO: REMOVE
print( "\$RAPR_fasta_output_file is $RAPR_fasta_output_file\n" );
if( $RAP_result_stdout =~ m/Total number of recombinants found:/ ) {
## extract the number fixed/removed from the output
( $removed_recombined_sequences ) = ( $RAP_result_stdout =~ m/Total number of recombinants found: (.+)/ );
if( $VERBOSE ) {
print( ".done ($removed_recombined_sequences recombinants removed)." );
}
# Now use the output from that..
$fasta_file = $RAPR_fasta_output_file;
( $fasta_file_path, $fasta_file_short ) =
( $fasta_file =~ /^(.*?)\/([^\/]+)$/ );
( $fasta_file_short_nosuffix, $fasta_file_suffix ) =
( $fasta_file_short =~ /^([^\.]+)(\..+)?$/ );
# Recompute the seq_headers.
$fasta_file_contents = path( $RAPR_fasta_output_file )->slurp();
( @seq_headers ) = ( $fasta_file_contents =~ /^>(.*)$/mg );
} else {
if( $VERBOSE ) {
print( ".done." );
}
$removed_recombined_sequences = 0;
} # End if any recombinants were identified. .. else ..
print( "The number of recombined sequences removed is: $removed_recombined_sequences\n" );
print OUTPUT_TABLE_FH "\t", $removed_recombined_sequences;
} # End if $run_RAP
print "Fasta file for analysis: $fasta_file_short\n";
# This is the file for analysis (again, path stripped); maybe altered from the original input file.
print OUTPUT_TABLE_FH "\t", $fasta_file_short;
## TODO: REMOVE
#print( "SETTING \$final_input_fasta_file_short_names_by_original_short_name_stripped{ $original_short_name_nosuffix_stripped } = $fasta_file_short_nosuffix\n" );
if( exists( $final_input_fasta_file_short_names_by_original_short_name_stripped{ $original_short_name_nosuffix_stripped } ) ) {
push @{ $final_input_fasta_file_short_names_by_original_short_name_stripped{ $original_short_name_nosuffix_stripped } }, $fasta_file_short_nosuffix;
} else {
$final_input_fasta_file_short_names_by_original_short_name_stripped{ $original_short_name_nosuffix_stripped } = [ $fasta_file_short_nosuffix ];
}
#### NOTE: running insites on multi-timepoint input files is ok but might need more thought. For purposes of evaluating founder multiplicity, looking at just the first time point makes a lot of sense.
my $morgane_calls_one_cluster = 1;
my $diversity_threshold_exceeded = 0;
my $in_sites_ratio_threshold_exceeded = 0;
my $in_sites_founders_call = 1;
my $ds_starphy_founders_call = 1;
my $num_phyml_seqs = undef;
my $mean_diversity = undef;
if( $run_InSites_and_PhyML ) {
my $runInSites_output;
if( $runInSites_online ) {
$runInSites_output = `perl runInSitesOnline.pl $extra_flags $fasta_file $output_path_dir_for_input_fasta_file`;
} else {
$runInSites_output = `perl runInSitesOffline.pl $extra_flags $fasta_file $output_path_dir_for_input_fasta_file`;
}
if( $VERBOSE ) {
print $runInSites_output;
}
my $getInSitesStat_output = `perl getInSitesStat.pl $extra_flags -O ${output_path_dir_for_input_fasta_file}/${fasta_file_short_nosuffix}_inSitesRatioStat.txt ${output_path_dir_for_input_fasta_file}/${fasta_file_short_nosuffix}_informativeSites.txt ${output_path_dir_for_input_fasta_file}/${fasta_file_short_nosuffix}_privateSites.txt`;
if( $VERBOSE ) {
print $getInSitesStat_output;
}
my $in_sites_ratio_text = `cat ${output_path_dir_for_input_fasta_file}/${fasta_file_short_nosuffix}_inSitesRatioStat.txt`;
#print( "\$in_sites_ratio_text is $in_sites_ratio_text\n" );
my ( $inf_sites, $priv_sites, $in_sites_ratio ) = ( $in_sites_ratio_text =~ /^\s*(\S+)\s*\/\s*(\S+)\s*=\s*(\S+)\s*$/ ); # Strip trailing newline, and any other flanking whitespace.
# Run phyML, get stats
my $phyml_out = `perl runPhyML.pl $extra_flags ${fasta_file} ${output_path_dir_for_input_fasta_file}`;
if( $VERBOSE ) {
print $phyml_out;
}
my $pairwise_diversity_stats = `cat ${output_path_dir_for_input_fasta_file}/${fasta_file_short_nosuffix}.phylip_phyml_pwdiversity.txt`;
( $num_phyml_seqs, $mean_diversity ) =
( $pairwise_diversity_stats =~ /Max\s+(\d+)\s+([e\-\.\d]+)\s+/ );
unless( defined( $num_phyml_seqs ) ) {
warn( "UH OH: $fasta_file\nGOT:\n$pairwise_diversity_stats\n" );
$num_phyml_seqs = 0;
$mean_diversity = 0;
}
# unless( $num_phyml_seqs == scalar( @seq_headers ) ) {
# ## THIS IS BECAUSE phyml apparently only counts unique sequences.
# # print "WHY ARE THERE $num_seqs SEQS, when there are ", scalar( @seq_headers ), " sequences in the input file?\n";
# }
my $num_seqs = scalar( @seq_headers );
print "Number of sequences: $num_seqs\n";
if( $num_seqs != $num_phyml_seqs ) { print "BUT NOTE: Number of PhyML sequences: $num_phyml_seqs\n" };
print "Mean pairwise diversity: $mean_diversity\n";
print "Informative sites to private sites ratio: $in_sites_ratio = $inf_sites / $priv_sites\n"; # Newline is no longer on there
#print "ABOUT TO WRITE OUT \$num_seqs ($num_seqs) and \$num_phyml_seqs ($num_phyml_seqs)\n";
printf OUTPUT_TABLE_FH "\t%d\t%d\t%1.4f\t%d\t%d\t%1.4f", ( $num_seqs, $num_phyml_seqs, $mean_diversity, $inf_sites, $priv_sites, $in_sites_ratio );
#print "did it\n";
my $the_relevant_in_sites_ratio_threshold = $in_sites_ratio_threshold;
if( $fasta_file_short_nosuffix =~ /^(.+)(?:_v3)/ ) {
if( $VERBOSE ) {
print "Using v3-specific InSites ratio threshold: $in_sites_ratio_threshold_v3\n";
}
$the_relevant_in_sites_ratio_threshold = $in_sites_ratio_threshold_v3;
}
# Now cluster the informative sites (only relevant if one or both of the above exceeds a threshold.
if( $mean_diversity > $mean_diversity_threshold ) {
$diversity_threshold_exceeded = 1;
if( $in_sites_ratio > $the_relevant_in_sites_ratio_threshold ) {
$in_sites_ratio_threshold_exceeded = 1;
print( "Diversity threshold ($mean_diversity_threshold) exceeded\n" );
print( "Ratio threshold ($the_relevant_in_sites_ratio_threshold) exceeded too\n" );
$morgane_calls_one_cluster = 0;
} else {
print( "Diversity threshold ($mean_diversity_threshold) exceeded\n" );
}
} elsif( $in_sites_ratio > $the_relevant_in_sites_ratio_threshold ) {
$in_sites_ratio_threshold_exceeded = 1;
print( "Ratio threshold ($the_relevant_in_sites_ratio_threshold) exceeded\n" );
}
print OUTPUT_TABLE_FH "\t", $diversity_threshold_exceeded;
print OUTPUT_TABLE_FH "\t", $in_sites_ratio_threshold_exceeded;
print OUTPUT_TABLE_FH "\t", $the_relevant_in_sites_ratio_threshold;
## "InSites.is.one.founder":
print OUTPUT_TABLE_FH "\t", $morgane_calls_one_cluster;
if( $morgane_calls_one_cluster ) {
print "Number of founders estimated using informative:private sites ratio and diversity thresholding is: 1\n";
$in_sites_founders_call = 1; # go with Morgane's call if it is 1.
} else {
print "Number of founders estimated using informative:private sites ratio and diversity thresholding is: greater than 1\n";
}
} # End if $run_InSites_and_PhyML
if( $run_entropy ) {
$R_output = `export computeEntropyFromAlignedFasta_inputFilename="$fasta_file"; export computeEntropyFromAlignedFasta_outputDir="$output_path_dir_for_input_fasta_file"; R -f computeEntropyFromAlignedFasta.R --vanilla --slave`;
if( $VERBOSE ) {
print( $R_output );
}
my ( $entropy_stats_filename ) = ( $R_output =~ /^\[1\]\s*\"(.+)\"\s*$/m );
if( !-e $entropy_stats_filename ) {
warning( "UH OH: MISSING ENTROPY RESULT FILE \"$entropy_stats_filename\"\n" );
#stopifnot( -e $entropy_stats_filename );
}
my $entropy_stats = path( $entropy_stats_filename )->slurp();
my ( $entropy_seqs, $entropy_sites, $entropy_min, $entropy_q1, $entropy_median, $mean_entropy, $entropy_q3, $entropy_max, $sd_entropy ) =
( $entropy_stats =~ /\"SD\"\s+\"(\d*)\"\s+\"(\d*)\"\s+\"([^\"]*)\"\s+\"([^\"]*)\"\s+\"([^\"]*)\"\s+\"([^\"]*)\"\s+\"([^\"]*)\"\s+\"([^\"]*)\"\s+\"([^\"]*)\"\s*$/ );
my $entropy_IQR = sprintf( "%0.4f", ( $entropy_q3 - $entropy_q1 ) );
if( $VERBOSE ) {
print "Entropy: Mean = $mean_entropy (SD = $sd_entropy)\n";
#print "Entropy: Median = $entropy_median (IQR = $entropy_IQR)\n";
}
print OUTPUT_TABLE_FH "\t", $mean_entropy;
print OUTPUT_TABLE_FH "\t", $sd_entropy;
} # End if $run_entropy
## Now run PoissonFitter.
my $PFitter_lambda = 0;
my $PFitter_se = 0;
my $PFitter_nseq = 0;
my $PFitter_nbases = 0;
my $PFitter_mean_hd = 0;
my $PFitter_max_hd = 0;
my $PFitter_chi_sq_stat = 0;
my $PFitter_chi_sq_df = 0;
my $PFitter_chi_sq_p_value = 1;
my $PFitter_days_est_and_ci = "0 (0,0)";
my $PFitter_days_est = 0;
my $PFitter_days_ci_low = 0;
my $PFitter_days_ci_high = 0;
my $PFitter_is_poisson = 1;
my $PFitter_is_starlike = 1;
my $Bayesian_PFitter_lambda_est = 0;
my $Bayesian_PFitter_lambda_ci_low = 0;
my $Bayesian_PFitter_lambda_ci_high = 0;
my $Bayesian_PFitter_days_est = 0;
my $Bayesian_PFitter_days_ci_low = 0;
my $Bayesian_PFitter_days_ci_high = 0;
my $DSStarPhyTest_fitstext =
"DSStarPhyTest that intersequence rate = 2 x seq-consensus rate: OK";
my $DSStarPhyTest_fits = 1;
my $DSStarPhyTest_assertion_low = 1.5;
my $DSStarPhyTest_assertion_high = 2.5;
my $DSStarPhyTest_P = "0.0";
my $DSStarPhyTest_Q = "0.0";
my $DSStarPhyTest_R = "1.0";
my $PFitter_synonymous_lambda = 0;
my $PFitter_synonymous_se = 0;
my $PFitter_synonymous_nseq = 0;
my $PFitter_synonymous_nbases = 0;
my $PFitter_synonymous_mean_hd = 0;
my $PFitter_synonymous_max_hd = 0;
my $PFitter_synonymous_chi_sq_stat = 0;
my $PFitter_synonymous_chi_sq_df = 0;
my $PFitter_synonymous_chi_sq_p_value = 1;
my $PFitter_synonymous_days_est_and_ci = "0 (0,0)";
my $PFitter_synonymous_days_est = 0;
my $PFitter_synonymous_days_ci_low = 0;
my $PFitter_synonymous_days_ci_high = 0;
my $PFitter_synonymous_is_poisson = 1;
my $PFitter_synonymous_is_starlike = 1;
my $DSStarPhyTest_synonymous_fitstext =
"DSStarPhyTest that intersequence rate = 2 x seq-consensus rate: OK";
my $DSStarPhyTest_synonymous_fits = 1;
my $DSStarPhyTest_synonymous_assertion_low = 1.5;
my $DSStarPhyTest_synonymous_assertion_high = 2.5;
my $DSStarPhyTest_synonymous_P = "0.0";
my $DSStarPhyTest_synonymous_Q = "0.0";
my $DSStarPhyTest_synonymous_R = "1.0";
my ( $DSStarPhyTest_synonymous_starlike_text, $DSStarPhyTest_synonymous_lower_starlike_text, $DSStarPhyTest_synonymous_upper_starlike_text );
my $paul_calls_one_cluster = 1;
if( $run_PFitter ) {
if( $VERBOSE ) {
print "\nCalling R to run PoissonFitter..";
}
my $R_pfitter_command = "\"export hiv_founder_pipeline_dir=\"$pipeline_dir\"; export runPoissonFitter_inputFilename=\"$fasta_file\"; export runPoissonFitter_outputDir=\"$output_path_dir_for_input_fasta_file\"; export runPoissonFitter_runDSStarPhyTest=\"TRUE\"; export runPoissonFitter_maskOutNonsynonymousCodons=\"FALSE\"; R -f runPoissonFitter.R --vanilla --slave\"";
$R_output = `"$R_pfitter_command"`;
if( $VERBOSE ) {
print( $R_pfitter_command );
print( $R_output );
print( "done.\n" );
}
my $PFitter_fitter_stats_raw =
`cat ${output_path_dir_for_input_fasta_file}/${fasta_file_short_nosuffix}_PoissonFitterDir/LOG_LIKELIHOOD.results.txt`;
( $PFitter_lambda, $PFitter_se, $PFitter_nseq, $PFitter_nbases, $PFitter_mean_hd, $PFitter_max_hd, $PFitter_days_est_and_ci, $PFitter_chi_sq_stat, $PFitter_chi_sq_df, $PFitter_chi_sq_p_value ) =
(
$PFitter_fitter_stats_raw =~ /\n[^\t]+\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t(\S+)\s*$/ );
( $PFitter_days_est, $PFitter_days_ci_low, $PFitter_days_ci_high ) =
( $PFitter_days_est_and_ci =~ /(\S+) \((\S+), (\S+)\)/ );
$PFitter_is_poisson = ( defined( $PFitter_chi_sq_p_value ) && ( $PFitter_chi_sq_p_value > 0.05 ) ) || 0;
my $starlike_raw =
`cat ${output_path_dir_for_input_fasta_file}/${fasta_file_short_nosuffix}_PoissonFitterDir/CONVOLUTION.results.txt`;
if( $DEBUG ) {
## TODO: REMOVE
# print "PoissonFitter RAW: $starlike_raw\n";
}
my ( $PFitter_starlike_text ) = ( $starlike_raw =~ /^.+(FOLLOWS|DOES NOT FOLLOW) A STAR-PHYLOGENY\s*$/m );
$PFitter_is_starlike = "0";
if( $PFitter_starlike_text eq "FOLLOWS" ) {
$PFitter_is_starlike = "1";
}
# DS results
my $DSStarPhyTest_outfile = "${output_path_dir_for_input_fasta_file}/${fasta_file_short_nosuffix}_PoissonFitterDir/${fasta_file_short_nosuffix}_DSStarPhyTest.out";
if( !-e $DSStarPhyTest_outfile ) {
print( "UH OH: Missing DSStarPhyTest output file \"$DSStarPhyTest_outfile\"\n" );
} else {
my $DSStarPhyTest_fitter_stats_raw =
`cat $DSStarPhyTest_outfile`;
( $DSStarPhyTest_fitstext ) =
( $DSStarPhyTest_fitter_stats_raw =~ /^DSStarPhyTest that intersequence rate = 2 x seq-consensus rate: (BAD|OK)$/m );
$DSStarPhyTest_fits =
( ( $DSStarPhyTest_fitstext =~ /^OK$/ ) ? "1" : "0" );
( $DSStarPhyTest_assertion_low, $DSStarPhyTest_assertion_high, $DSStarPhyTest_P, $DSStarPhyTest_Q, $DSStarPhyTest_R ) =
( $DSStarPhyTest_fitter_stats_raw =~ /There is .*evidence against the assertion that the Poisson rate between sequences is between (\S+) and (\S+) times the rate of sequences to the consensus \(P \<?= (\S+), Q \<?= (\S+), R \<?= (\S+)\)/ );
} # End if the DSStarPhyTest output file exists.
print "PoissonFitter Determination: ";
if( $PFitter_is_starlike ) {
print "Star-Like Phylogeny";
} else {
print "Non-Star-Like Phylogeny";
}
print "\nPoissonFitter Poisson Fit: ";
if( $PFitter_is_poisson ) {
print "OK\n";
} else {
print "BAD (p = $PFitter_chi_sq_p_value)\n";
}
print "DS StarPhy Test: $DSStarPhyTest_fitstext (P=$DSStarPhyTest_P, Q=$DSStarPhyTest_Q, R=$DSStarPhyTest_R).\n";
print "PoissonFitter Poisson time estimate (95\% CI): $PFitter_days_est ($PFitter_days_ci_low, $PFitter_days_ci_high)\n";
#} # End if( $mean_diversity > 0 );
print OUTPUT_TABLE_FH "\t", $PFitter_lambda;
print OUTPUT_TABLE_FH "\t", $PFitter_se;
print OUTPUT_TABLE_FH "\t", $PFitter_nseq;
print OUTPUT_TABLE_FH "\t", $PFitter_nbases;
print OUTPUT_TABLE_FH "\t", $PFitter_mean_hd;
print OUTPUT_TABLE_FH "\t", $PFitter_max_hd;
print OUTPUT_TABLE_FH "\t", $PFitter_days_est;
print OUTPUT_TABLE_FH "\t", $PFitter_days_ci_low;
print OUTPUT_TABLE_FH "\t", $PFitter_days_ci_high;
print OUTPUT_TABLE_FH "\t", $PFitter_chi_sq_stat;
print OUTPUT_TABLE_FH "\t", $PFitter_chi_sq_df;
print OUTPUT_TABLE_FH "\t", $PFitter_chi_sq_p_value;
print OUTPUT_TABLE_FH "\t", $PFitter_is_poisson;
print OUTPUT_TABLE_FH "\t", $PFitter_is_starlike;
print OUTPUT_TABLE_FH "\t", $DSStarPhyTest_assertion_low;
print OUTPUT_TABLE_FH "\t", $DSStarPhyTest_assertion_high;
print OUTPUT_TABLE_FH "\t", $DSStarPhyTest_fits;
print OUTPUT_TABLE_FH "\t", $DSStarPhyTest_P;
print OUTPUT_TABLE_FH "\t", $DSStarPhyTest_Q;
print OUTPUT_TABLE_FH "\t", $DSStarPhyTest_R;
## TODO: Add support for using genecutter to handle NFLGs (and other non-codon-aligned and/or multi-gene input).
if( $VERBOSE ) {
print "\nCalling R to run PoissonFitter again, masking out nonsynonymous codons..";
}
my $R_pfitter_command2 = "\"export hiv_founder_pipeline_dir=\"$pipeline_dir\"; export runPoissonFitter_inputFilename=\"$fasta_file\"; export runPoissonFitter_outputDir=\"$output_path_dir_for_input_fasta_file\"; export runPoissonFitter_runDSStarPhyTest=\"TRUE\"; export runPoissonFitter_maskOutNonsynonymousCodons=\"TRUE\"; R -f runPoissonFitter.R --vanilla --slave\"";
$R_output = `"$R_pfitter_command2"`;
if( $VERBOSE ) {
print( $R_pfitter_command2 );
print( $R_output );
print( "done.\n" );
}
my $PFitter_synonymous_fitter_stats_raw =
`cat ${output_path_dir_for_input_fasta_file}/${fasta_file_short_nosuffix}_maskNonsynonymousCodons_PoissonFitterDir/LOG_LIKELIHOOD.results.txt`;
( $PFitter_synonymous_lambda, $PFitter_synonymous_se, $PFitter_synonymous_nseq, $PFitter_synonymous_nbases, $PFitter_synonymous_mean_hd, $PFitter_synonymous_max_hd, $PFitter_synonymous_days_est_and_ci, $PFitter_synonymous_chi_sq_stat, $PFitter_synonymous_chi_sq_df, $PFitter_synonymous_chi_sq_p_value ) =
(
$PFitter_synonymous_fitter_stats_raw =~ /\n[^\t]+\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t(\S+)\s*$/ );
( $PFitter_synonymous_days_est, $PFitter_synonymous_days_ci_low, $PFitter_synonymous_days_ci_high ) =
( $PFitter_synonymous_days_est_and_ci =~ /(\S+) \((\S+), (\S+)\)/ );
$PFitter_synonymous_is_poisson = ( defined( $PFitter_synonymous_chi_sq_p_value ) && ( $PFitter_synonymous_chi_sq_p_value > 0.05 ) ) || 0;
$starlike_raw =
`cat ${output_path_dir_for_input_fasta_file}/${fasta_file_short_nosuffix}_maskNonsynonymousCodons_PoissonFitterDir/CONVOLUTION.results.txt`;
if( $DEBUG ) {
## TODO: REMOVE
# print "PoissonFitter RAW: $starlike_raw\n";
}
my ( $PFitter_synonymous_starlike_text ) = ( $starlike_raw =~ /^.+(FOLLOWS|DOES NOT FOLLOW) A STAR-PHYLOGENY\s*$/m );
$PFitter_synonymous_is_starlike = "0";
if( $PFitter_synonymous_starlike_text eq "FOLLOWS" ) {
$PFitter_synonymous_is_starlike = "1";
}
# DS results