-
Notifications
You must be signed in to change notification settings - Fork 4
/
burntrees.pl
executable file
·1327 lines (1045 loc) · 42.6 KB
/
burntrees.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
## Use 'perldoc burntrees.pl' or see the end of file for description.
use strict;
use warnings;
use Pod::Usage;
use Getopt::Long;
Getopt::Long::Configure("no_ignore_case");
## Globals
my $scriptname = $0;
my $VERSION = '0.3.3';
my $CHANGES = '15 Apr 2020';
my $DEBUG = 0;
my $burnin = q{};
my $close = q{};
my $end = q{};
my $figtree = q{};
my $format = q{};
my $getinfo = q{};
my $help = q{};
my $ifeellucky = q{};
my $infile = q{};
my $i = q{};
my $is_tree_file = q{};
my $j = q{};
my $jump = q{};
my $labels = q{};
my $lastgen = q{};
my $match = q{};
my $myr = q{};
my $nexttolastgen = q{};
my $noclose = q{};
my $nolabels = q{};
my $ntrees = q{};
my $outfile = q{};
my $pburnin = q{};
my $random_nr = q{};
my $rmbrlens = q{};
my $rmcomments = q{};
my $rmsupport = q{};
my $R = q{};
my $sci2norm = q{};
my $seed = q{};
my $start = q{};
my $treesonly = q{};
my $altnexus_treesonly = q{};
my $version = q{};
my $was_newick = q{};
my %translation_table = ();
my $PRINT_FH;
## "MAIN" routine
MAIN:
if (@ARGV < 1) {
print "\n Try '$scriptname --man' for full info\n\n";
exit(0);
}
else {
GetOptions(
'help' => sub { pod2usage(1); },
'version' => sub { print STDOUT " $scriptname version $VERSION\n Last changes $CHANGES\n"; exit(0) },
'man' => sub { pod2usage(-exitstatus => 0, -verbose => 2); },
'burnin:i' => \$burnin,
'close' => \$close,
'DEBUG+' => \$DEBUG,
'end:i' => \$end,
'format:s' => \$format,
'getinfo' => \$getinfo,
'ifeellucky:f' => \$ifeellucky,
'jump:i' => \$jump,
'labels' => \$labels,
'myr' => \$myr,
'noclose' => \$noclose,
'nolabels' => \$nolabels,
'outfile:s' => \$outfile,
'pburnin:i' => \$pburnin,
'rmbrlens' => \$rmbrlens,
'rmcomments' => \$rmcomments,
'rmsupport' => \$rmsupport,
'R' => \$R,
'sci2norm:-1' => \$sci2norm,
'seed:i' => \$seed,
'start:i' => \$start,
'treesonly' => \$treesonly,
);
}
## Some debug printing
print_debug(1) if ($DEBUG > 1);
## Set --jump default. 1 prints every tree.
if ($jump eq '') {
$jump = '1';
}
## --noclose is stronger than --close
if ($noclose) {
$close = 0;
}
## Get ONE infile from ARGV.
if (@ARGV > 0) {
$infile = shift(@ARGV);
}
COUNTANDCHECK:
## First: count trees and check file type
open (my $IN, '<', $infile) or die "$0 : failed to open input file $infile : $!\n";
$ntrees = 0;
$match = 'Peppes_Bodega'; # Dummy match pattern to fail a match on the first lines
while (<$IN>) {
chomp;
if (/$match/i) {
$ntrees++; # Count trees in .t files or lines in .p files
$nexttolastgen = $lastgen;
if ($is_tree_file) { # Collect info on generation number in t file
$_ =~ /\s+(STATE_|rep\.|gen\.)(\d+)[\s+|\[]/i; # BEAST 1.8.3 | MrBayes old | MrBayes 3.2
if ($2) { # If 'rep' is not found in tree file, then
$lastgen = $2;
}
else {
$lastgen = 1; # set lastgen to 1, and
$nexttolastgen = 0; # nexttolastgen to 0 (to be able to get reasonable
} # output from --getinfo)
}
else {
$_ =~ /(^\d+)\s+/; # Collect info on generation number in p file
$lastgen = $1;
}
}
elsif (/^\s*begin\s+trees/i) { # A .t file contains "begin trees"
$is_tree_file = 1;
$match = '^\s*tree';
next;
}
elsif (/\)\s*;\s*$/) { # A phylobase tree file have newick trees on one line
$is_tree_file = 1;
$was_newick = 1;
$match = '^\s*\(';
$ntrees++;
next;
}
elsif ((/^\s*Gen/i) or (/^\s*state/i) or (/^\s*#cycle/i) ) { # A .p file contains "Gen", a BEAST log file contains "STATE",
$is_tree_file = 0; # A phylobase .trace file contains "#cycle"
$match = '^\s*\d+';
next;
}
}
close ($IN) or warn "$0 : failed to close input file $infile : $!\n";
## Error if no match set (neither a .p nor a .t file)
if ($match eq 'Peppes_Bodega') {
die "\nInfile doesn't seem to be a tree file (or a p file)?\n"
};
BURNIN:
## Set --start, --burnin, and --fburnin defaults
if ($start eq '') {
if ($burnin) {
$start = $burnin + 1;
}
elsif ($pburnin) {
$start = int($pburnin * (1/100) * $ntrees) + 1;
}
else {
$start = '1';
}
}
## Set probability printing defaults
if ($ifeellucky eq '') {
$ifeellucky = 1;
$random_nr = 0;
}
elsif ($seed) {
srand $seed;
}
## Set --end default to $ntrees if no --end
if ($end eq '') {
$end = $ntrees;
}
BRLENS:
## Test brlens if rmbrlens
if ($rmbrlens) {
my $has_brlens = test_has_brlens($infile);
if ( $has_brlens == 0 ) {
die "\nWarning! the file doesn't seem to have branch lengths.\n\n";
}
}
SUPPORT:
## Test if support values
if ($rmsupport) {
my $has_support = test_has_support($infile);
if ( $has_support == 0 ) {
die "\nWarning! the file doesn't seem to have support values.\n\n";
}
}
REMOVEALL:
## Remove brlens, support, and comments. This needs to be checked after
## BRLENS and SUPPORT (i.e, don't check, just try to remove)!
if ($R) {
$rmbrlens = 1;
$rmcomments = 1;
$rmsupport = 1;
}
MYR:
## Test clockrate if myr
if ($myr) {
my $has_clockrate = test_has_clockrate($infile);
if ( $has_clockrate == 0 ) {
die "\nWarning! the file doesn't seem to have clock rates.\n\n";
}
}
## Set filehandle for printing.
if ($outfile eq '') {
$PRINT_FH = *STDOUT; # Using the typeglob notation in order to use STDOUT as a variable
}
else {
open ($PRINT_FH, '>', $outfile) or die "$0 : Failed to open output file $outfile : $!\n\n";
}
FORMAT:
## Set output format (altnexus or phylip)
if ($format ne '') {
if ($is_tree_file) { # --format only applicable on tree files
$figtree = test_figtree_format($infile);
if ($format =~ m/^a/i) {
$format = 'altnexus';
if ($nolabels) {
$labels = 0;
}
else {
$labels = 1;
}
if ($noclose) {
$close = 0;
}
if ($treesonly) {
$altnexus_treesonly = 1;
}
else {
print $PRINT_FH "#NEXUS\n[Trees from file $infile]\nBegin trees;\n";
$treesonly = 1;
}
}
elsif ($format =~ m/^p/i) {
$format = 'phylip';
$treesonly = 1;
$close = 0;
if ($nolabels) {
$labels = 0;
}
else {
$labels = 1;
}
}
else {
die "\nWarning! output format ($format) should either be 'altnexus' or 'phylip'.\n\n"
}
}
else { # Warn if --format is used with a *.p file
die "\nWarning! argument 'format' is only applicable on tree files.\n\n"
}
}
elsif ($was_newick) {
$format = 'phylip';
$treesonly = 1;
$close = 0;
if ($nolabels) {
$labels = 0;
}
else {
$labels = 1;
}
}
## Read the translation table
if ($labels) {
%translation_table = read_labels($infile);
}
GETINFO:
## If --getinfo, print number of trees etc and quit
if ($getinfo) {
my $thinning;
my $totgen;
if ($lastgen) {
$thinning = $lastgen - $nexttolastgen;
$totgen = $thinning * ($ntrees - 1);
}
my $word = 'lines';
if ($is_tree_file) {
$word = 'trees';
}
print $PRINT_FH "Total number of $word : $ntrees\n";
if ($burnin or $pburnin) {
my $Ntrees = $end - $start + 1;
print $PRINT_FH "Number of $word after burnin : $Ntrees\n";
}
if (($lastgen) && ($lastgen > 1)) { # If no 'rep' in tree file, $lastgen was set to 1 (then probably not a MrBayes .t file)
print $PRINT_FH "Thinning seems to have been : $thinning\n";
print $PRINT_FH "Number of generations was then : $totgen\n";
}
if ($burnin or $pburnin) {
my $burngen = ($start - 1) * $thinning;
print $PRINT_FH "Burnin in generations : $burngen\n";
}
if ($is_tree_file) {
my $has_brlens = test_has_brlens($infile);
if ($has_brlens) {
print $PRINT_FH "Trees appear to have branch lengths.\n";
}
else {
print $PRINT_FH "Trees appear to have no branch lengths.\n";
}
my $has_support = test_has_support($infile);
if ($has_support) {
print $PRINT_FH "Trees appear to have support values.\n";
}
else {
print $PRINT_FH "Trees appear to have no support values.\n";
}
my $is_figtree = test_figtree_format($infile);
if ($is_figtree) {
print $PRINT_FH "Trees appear to be in figtree format.\n";
}
}
exit(0);
}
## Then do some tests
die "\nWarning! end value: $end is greater than number of trees: $ntrees.\n\n"
if ($end > $ntrees);
die "\nWarning! start value: $start is greater than end value: $end.\n\n"
if ($start > $end);
die "\nWarning! start value: $start is greater than number of trees: $ntrees.\n\n"
if ($start > $ntrees);
die "\nWarning! probability value: $ifeellucky is greater than one.\n\n"
if ($ifeellucky > 1);
## Some debug printing
print_debug(2) if ($DEBUG > 1);
PRINT:
## Finally: do the printing
open (my $INFILE, '<', $infile) or die "$0 : failed to open input file $infile : $!\n";
$i = 1;
while (<$INFILE>) {
chomp;
if (/$match/i) { # If found a tree or a parameter line
if ($ifeellucky < 1) { # A value less than 1 needs a random number
$random_nr = rand;
}
if ($sci2norm) {
$_ = sci2norm($_,$sci2norm); # warning, this is done one the whole string, including tree name and newick string.
}
if ($myr) {
$_ = brlen2time($_);
}
if ($format) { # Print tree in correct format
if ($figtree) {
$_ = remove_figtree_comments($_);
}
if ($format eq 'phylip') {
$_ = remove_tree_name($_);
if ($labels) {
$_ = replace_numbers($_, \%translation_table);
}
}
elsif ($format eq 'altnexus') {
if ($labels) {
$_ = replace_numbers($_, \%translation_table);
}
if ($was_newick) {
$_ = " tree rep.$i = " . $_;
}
}
elsif ($labels) {
$_ = replace_numbers($_, \%translation_table);
}
}
if ($rmcomments) {
$_ = remove_figtree_comments($_);
}
if ($rmsupport) {
$_ = remove_support($_);
}
if ($i == $end) { # If last tree
if ($rmbrlens) {
print STDERR "\n=== print 1:(i:$i j:$j) ===" if $DEBUG;
if ($random_nr < $ifeellucky) {
strip_brlens_print($_) if ( ($i % $jump) == 0 ); # Print with no branch lengths if modulus is 0.
if ($close) {
my $l = $DEBUG ? "END; [DEBUG:close]\n" : "END;\n";
print $PRINT_FH $l unless ($format eq 'phylip');
last;
}
elsif ($noclose) {
last;
}
elsif ($format eq 'altnexus') {
my $l = $DEBUG ? "END; [DEBUG:altnex]\n" : "END;\n";
print $PRINT_FH $l unless ($altnexus_treesonly);
last;
}
elsif ($treesonly) {
last;
}
else {
my $l = $DEBUG ? "END; [DEBUG:default]\n" : "END;\n";
print $PRINT_FH $l unless ($format eq 'phylip');
last;
}
}
}
else {
print STDERR "\n=== print 2:(i:$i j:$j) ===" if $DEBUG;
if ($random_nr < $ifeellucky) {
my $l = $DEBUG ? "$_ [DEBUG:bpa]\n" : "$_\n";
print $PRINT_FH $l if ( ($i % $jump) == 0 ); # Print with branch lengths if modulus is 0.
if ($close) {
my $l = $DEBUG ? "END; [DEBUG:cpa]\n" : "END;\n";
print $PRINT_FH $l unless ($format eq 'phylip');
last;
}
elsif ($noclose) {
last;
}
elsif ($format eq 'altnexus') {
my $l = $DEBUG ? "END; [DEBUG:dpa]\n" : "END;\n";
print $PRINT_FH $l unless ($altnexus_treesonly);
last;
}
elsif ($treesonly) {
last;
}
elsif ($is_tree_file) {
my $l = $DEBUG ? "END; [DEBUG:epa]\n" : "END;\n";
print $PRINT_FH $l unless ($format eq 'phylip');
last;
}
}
}
}
if ($i < $end) {
if ($i == $start) {
if ($rmbrlens) {
print STDERR "\n=== print 3:(i:$i j:$j) ===" if $DEBUG;
if ($random_nr < $ifeellucky) {
strip_brlens_print($_); # Make sure to print the start tree.
}
$j = 1;
}
else {
print STDERR "\n=== print 4:(i:$i j:$j) ===" if $DEBUG;
if ($random_nr < $ifeellucky) {
my $l = $DEBUG ? "$_ [DEBUG:fpa]\n" : "$_\n";
print $PRINT_FH $l;
}
$j = 1;
}
}
elsif ($i > $start) {
if ($rmbrlens) {
print STDERR "\n=== print 5:(i:$i j:$j) ===" if $DEBUG;
if ($random_nr < $ifeellucky) {
strip_brlens_print($_) if ( ($j % $jump) == 0 );
}
}
else {
print STDERR "\n=== print 6:(i:$i j:$j) ===" if $DEBUG;
if ($random_nr < $ifeellucky) {
my $l = $DEBUG ? "$_ [DEBUG:gpa]\n" : "$_\n";
print $PRINT_FH $l if ( ($j % $jump) == 0 );
}
}
}
}
$i++;
$j++;
}
else { # If string is not a tree, it's either taxa descriptions or a trailing end
if ($i > $end) {
print STDERR "\n=== print 7:(i:$i j:$j) ===" if $DEBUG;
my $l = $DEBUG ? "$_ [DEBUG:hpa]\n" : "$_\n";
print $PRINT_FH $l unless ($noclose);
}
else {
my $l = $DEBUG ? "$_ [DEBUG:ipa]\n" : "$_\n";
print $PRINT_FH $l unless ($treesonly);
}
}
}
close ($INFILE) or warn "$0 : failed to close input file $infile : $!\n";
## Some debug printing
print_debug(3) if ($DEBUG > 1);
## Exit explicitly
exit(0);
## End of MAIN
#=== FUNCTION ================================================================
# NAME: brlen2time
# VERSION: 04/09/2013 01:49:02 PM
# DESCRIPTION: Transform branch lengths from substitutions per site to time units
# PARAMETERS: tree string
# tree gen.5000000[&B Igrbranchlens{all}] = [&R] [&clockrate=8.332129945298162e-04] (1:4.907239999154218e-02[&B Igrbranchlens{all} 5.967617061002457e-02],(...
# RETURNS: tree string
#===============================================================================
sub brlen2time {
my ($tree) = @_;
$tree =~ m/&clockrate\s*=\s*(\d+\.?\d*([eE][+-]?\d+)?)\]/i;
my $clockrate = $1;
## Iterate over tree and substitute all branch lengths with the age:
$tree =~ s/:(\d+\.?\d*([eE][+-]?\d+)?)/brlen2time_print($clockrate,$1)/ieg;
return($tree);
} # end brlen2time
#=== FUNCTION ================================================================
# NAME: brlen2time_print
# VERSION: 04/10/2013 09:34:53 AM
# DESCRIPTION: print branch lengths in time units PLUS an ad hoc ':'!
# PARAMETERS: clockrate, branch length
# RETURNS: string (decimal number) and a colon(!) :89.001
# TODO: Work around the ':' when used with brlen2time
#===============================================================================
sub brlen2time_print {
my($cr, $bl) = @_;
my $myr = sprintf ":%f", $bl/$cr;
return($myr);
} # end of brlen2time_print
#=== FUNCTION ================================================================
# NAME: fig2simple_with_pp
# VERSION: 08/11/2016 03:10:48 PM
# DESCRIPTION: Transform figtree format to "simple" format with posterior probabilities
# PARAMETERS: tree in figtree format with scientific dec format
# RETURNS: newick string in "simple" format
# TODO: Check if sci format?
# Testing!
# Note: look at the possibility to have the "--format" argument handle
# the figtree to simple conversion!!!
#===============================================================================
#sub fig2simple_with_pp {
#
# my ($tree) = @_;
#
# if ($tree =~ /&prob=/i) {
# $tree =~ s/\)\[&prob=(\d+\.\d+e[+-]?\d+)[^\]]+\]?/\)$1/g;
# }
#
# $tree = remove_figtree_comments($tree);
#
# #$tree = sci2norm($tree);
#
# return($tree);
#
#} # end of fig2simple_with_pp
#=== FUNCTION ================================================================
# NAME: print_debug
# VERSION: ons 15 apr 2020 17:00:09
# DESCRIPTION: debug printing
# PARAMETERS: number
# RETURNS: prints to STDERR
#===============================================================================
sub print_debug {
my ($number) = @_;
print STDERR "\n\n= $number ==============\n";
print STDERR "burnin:$burnin.\n";
print STDERR "close:$close.\n";
print STDERR "end:$end.\n";
print STDERR "figtree:$figtree.\n";
print STDERR "format:$format.\n";
print STDERR "getinfo:$getinfo.\n";
print STDERR "help:$help.\n";
print STDERR "i:$i.\n";
print STDERR "ifeellucky:$ifeellucky.\n";
print STDERR "infile:$infile.\n";
print STDERR "is_tree_file:$is_tree_file.\n";
print STDERR "j:$j.\n";
print STDERR "jump:$jump.\n";
print STDERR "labels:$labels.\n";
print STDERR "lastgen:$lastgen.\n";
print STDERR "match:$match.\n";
print STDERR "myr:$myr.\n";
print STDERR "nexttolastgen:$nexttolastgen.\n";
print STDERR "noclose:$noclose.\n";
print STDERR "nolabels:$nolabels.\n";
print STDERR "ntrees:$ntrees.\n";
print STDERR "outfile:$outfile.\n";
print STDERR "pburnin:$pburnin.\n";
print STDERR "random_nr:$random_nr.\n";
print STDERR "rmbrlens:$rmbrlens.\n";
print STDERR "rmcomments:$rmcomments.\n";
print STDERR "rmsupport:$rmsupport.\n";
print STDERR "R:$R.\n";
print STDERR "sci2norm:$sci2norm.\n";
print STDERR "seed:$seed.\n";
print STDERR "start:$start.\n";
print STDERR "treesonly:$treesonly.\n";
print STDERR "altnexus_treesonly:$altnexus_treesonly.\n";
print STDERR "==================\n";
warn "\n (hit return to continue)\n" and getc();
} # end of print_debug
#=== FUNCTION ================================================================
# NAME: read_labels
# VERSION: 02/03/2013 01:07:37 PM
# DESCRIPTION: reads sequence (taxon) labels and associates them with numbers
# PARAMETERS: file name
# RETURNS: Hash with taxon translation table
#===============================================================================
sub read_labels {
my ($file) = @_;
my %hash = (); # key: number, value: name
open (my $FILE, '<', $file) or die "$0 : failed to open input file $file : $!\n";
while(<$FILE>) {
my $line = $_;
chomp($line);
if($line =~ m/\s*tree\s+state/i) {
last;
}
elsif($line =~ m/^\s*(\d+)\s+([\w|\s|\W]+)$/) { # capture the number, and the
my $number = $1; # taxon name allowing for single-
my $name = $2; # quoted taxon names
$name =~ s/\s*,\s*$//;
$name =~ s/\s*;\s*$//;
$hash{$number} = $name;
}
}
close ($FILE) or warn "$0 : failed to close file $file : $!\n";
if ($DEBUG) {
print STDERR "\nhash in read_labels:\n";
for my $key ( keys %hash ) {
my $value = $hash{$key};
print STDERR "$key => $value\n";
}
}
return %hash;
} # end of read_labels
#=== FUNCTION ================================================================
# NAME: remove_figtree_comments
# VERSION: 08/12/2016 01:15:23 PM
# DESCRIPTION: Removes the figtree comments. If "&prob" are present in the
# description (as in MrBayes con.tre files), the probabilities
# are extracted and included in the newick string!
# Currently (mb3.2.7), prob numbers are in scientific format.
# PARAMETERS: tree string
# RETURNS: tree string without figtree comments
#===============================================================================
sub remove_figtree_comments {
my ($tree) = @_;
if ($tree =~ /&prob=\d+\.\d+e[+-]?/i) {
$tree =~ s/\)\[&prob=(\d+\.\d+e[+-]?\d+)[^\]]+\]?/\)$1/ig;
}
elsif ($tree =~ /&prob=\d+\.\d+[^eE]?/i) {
$tree =~ s/\)\[&prob=(\d+\.\d+?)[^\]]+\]?/\)$1/ig;
}
$tree =~ s/(\[.+?\])//g;
return($tree);
} # end of remove_figtree_comments
#=== FUNCTION ================================================================
# NAME: remove_support
# VERSION: 08/16/2016 01:20:53 PM
# DESCRIPTION: Removes the support values from simple newick string:
# )1.00000000e+00) -> ))
# )1.00000000e+00:3.231983e-02) -> ):3.231983e-02)
# )100) -> ))
# )100:0.001) -> ):0.001)
#
# PARAMETERS: tree string
# RETURNS: tree string without support values
# TODO: testing!
#===============================================================================
sub remove_support {
my ($tree) = @_;
$tree =~ s/\)[\d\.Ee+-]+/\)/g;
return($tree);
} # end of remove_support
#=== FUNCTION ================================================================
# NAME: remove_tree_name
# VERSION: 08/29/2016 11:20:18 AM
# DESCRIPTION: Removes the tree name
# PARAMETERS: Line with tree string. Uses global variable $infile
# RETURNS: Tree string
# TODO: Any spaces in the tree description will create
# truncated trees!
# Make sure the hack is no longer needed!
#===============================================================================
sub remove_tree_name {
my ($line) = (@_);
my $tree = '';
my @pieces = split /\s+/, $line;
foreach my $piece (@pieces) {
if ($piece =~ /^\([^;]+[\);]$/) { # if piece starts with '(' and ends in ')' or ';'
#$tree = "$piece\n"; # hack: needed to add line break
$tree = "$piece";
}
}
if ($tree eq '') {
die "Warning: Could not read tree format.\nAcceptable format: tree name = (1,(2,3));\n";
}
return $tree;
} # end of remove_tree_name
#=== FUNCTION ================================================================
# NAME: replace_numbers
# VERSION: 02/03/2013 04:13:04 PM
# DESCRIPTION: Replaces numbers with sequence (taxon) labels.
# Possible matches and replacements are
# ',123:' => ',foo:'
# ',123,' => ',foo,'
# ',123)' => ',foo)'
# ',123[' => ',foo['
# '(123:' => '(foo:'
# '(123,' => '(foo,'
# '(123[' => '(foo['
# PARAMETERS: tree string and reference to hash holding translation table.
# Uses global variable $infile
# RETURNS: tree string with seq labels instead of numbers
# TODO: Test FigTree format
#===============================================================================
sub replace_numbers {
my ($tree, $hash_reference) = @_;
foreach my $number (keys %$hash_reference) { # $key is number, $value is label
my $label = $hash_reference->{$number};
if ( $tree =~ /([,\(])$number([\),:\[])/ ) {
$tree =~ s/([,\(])$number([\),:\[])/$1$label$2/;
}
}
return $tree;
} # end of replace_numbers
#=== FUNCTION ================================================================
# NAME: sci2norm
# VERSION: 07/10/2014 04:16:09 PM
# DESCRIPTION: Translate tree string with branch lengths in scientific notation
# to normal notation.
# PARAMETERS: tree string
# RETURNS: tree string
# TODO: test on p files with scientific notation
#===============================================================================
sub sci2norm {
my ($tree,$dec) = (@_);
$tree =~ s/(\d+\.\d+e[+-]?\d+)/sci2norm_print($1,$dec)/ieg;
return($tree);
} # end of sci2norm
#=== FUNCTION ================================================================
# NAME: sci2norm_print
# VERSION: 08/12/2016 05:36:38 PM
# DESCRIPTION: Translate string of scientific number notation to decimal number.
# PARAMETERS: string (scientific notation, e.g. "1.600103158188143e-02")
# RETURNS: string (decimal number, e.g. "0.016001")
#===============================================================================
sub sci2norm_print {
my($sci,$dec) = (@_);
my $nr;
if ($dec == -1) {
$nr = sprintf "%f", $sci;
}
else {
$nr = sprintf "%.${dec}f", $sci;
}
return($nr);
} # end of sci2norm_print
#=== FUNCTION ================================================================
# NAME: strip_brlens_print
# VERSION: Mon 13 Apr 2020 12:59:44 PM CEST
# DESCRIPTION: Removes the branch lengths from a tree descriptions and print.
# PARAMETERS: tree string
# RETURNS: Void. Prints to $PRINT_FH
#===============================================================================
sub strip_brlens_print {
($_) = @_;
if (/e[-+]\d+/i) { # ":1.309506485347851e-01" or ":1.309506485347851E-01" or ":1.053210e+00"
$_ =~ s/:[e\d\.\-\+]+//gi;
}
else {
$_ =~ s/:[\d\.]+//g; # Search for any pattern such as ":0.033372" and replace with nothing
}
print $PRINT_FH "$_\n";
} # end of strip_brlens_print
#=== FUNCTION ================================================================
# NAME: test_has_brlens
# VERSION: 09/29/2016 05:31:03 PM
# DESCRIPTION: Tests for presence of branch lengths in the tree description.
# Warning: no error checking. Assumes Nexus or Newick tree format,
# and tree description without linebreaks.
# PARAMETERS: string containing tree description
# RETURNS: 1: brlens present, 0: brlens absent
#===============================================================================
sub test_has_brlens {
my ($file) = @_;
my $brl = 0;
open (my $FILE, '<', $file) or die "$0 : failed to open input file $file : $!\n";
while(<$FILE>) {
chomp;
if(/^\s*tree/i) {
if (/\[/) {
s/(\[.+?\])//g;
}
if ( $_ =~ /:/ ) {
$brl = 1;
}
last;
}
elsif (/\)\s*;\s*$/) {
if ( $_ =~ /:/ ) {
$brl = 1;
}
last;
}
}
close ($FILE) or warn "$0 : failed to close file $file : $!\n";
return $brl;
} # end of test_has_brlens
#=== FUNCTION ================================================================
# NAME: test_has_clockrate
# VERSION: ons 15 apr 2020 17:10:43
# DESCRIPTION: Tests for presence of clockrate in the tree description.
# Warning: no error checking. Assumes MrBayes clock tree format.
# tree gen.5000000[&B Igrbranchlens{all}] = [&R] [&clockrate=8.332129945298162e-04] (1:4.907...
# PARAMETERS: String containing tree description.
# RETURNS: 1: clockrate present, 0: clockrate absent
#===============================================================================
sub test_has_clockrate {
my ($file) = @_;
my $clr = 0;
open (my $FILE, '<', $file) or die "$0 : failed to open input file $file : $!\n";
while(<$FILE>) {
chomp;
if(/^\s*tree/i) {
if ( $_ =~ /&clockrate/i ) {
$clr = 1;
}
last;
}
}
close ($FILE) or warn "$0 : failed to close file $file : $!\n";
return $clr;
} # end of test_has_clockrate
#=== FUNCTION ================================================================
# NAME: test_has_support
# VERSION: 03/03/2017 12:10:35 PM
# DESCRIPTION: Tests for presence of support values in the tree description.
# Warning: no error checking.
# Assumes Nexus or Newick tree format (tree on single line).
# PARAMETERS: string containing tree description
# RETURNS: 1: support values present, 0: support values absent
#===============================================================================
sub test_has_support {
my ($file) = @_;
my $support = 0;
open (my $FILE, '<', $file) or die "$0 : failed to open input file $file : $!\n";
while(<$FILE>) {
chomp;
if((/^\s*tree/i) || (/\)\s*;\s*$/)) {
if (/\[/) {
if (/&prob=\d+/i) {
$support = 1;
last;
}
else {
s/(\[.+?\])//g;
}
}
if ( $_ =~ /\)\d+/ ) {
$support = 1;
}
last;
}
}
close ($FILE) or warn "$0 : failed to close file $file : $!\n";
return $support;
} # end of test_has_brlens
#=== FUNCTION ================================================================
# NAME: test_figtree_format
# VERSION: 02/02/2007 12:12:05 AM CET
# DESCRIPTION: Tests for figtree format, i.e., presence of comments in square
# brackets within the tree string.
# Warning: no extensive error checking. Looks only for 'tree' and '&'
# PARAMETERS: string containing tree description
# RETURNS: 1: figtree format, 0: not figtree format
#===============================================================================
sub test_figtree_format {
my ($file) = @_;
my $figtree = 0;
open (my $FILE, '<', $file) or die "$0 : failed to open input file $file : $!\n";
while(<$FILE>) {
chomp;
if(/^\s*tree/i) {
if ( $_ =~ /&/ ) { # Presence of '&' probably is a figtree tree
$figtree = 1;
}
last;
}
}
close ($FILE) or warn "$0 : failed to close file $file : $!\n";