-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_taxa_overlapping_newick_and_fasta.pl
729 lines (475 loc) · 18.2 KB
/
parse_taxa_overlapping_newick_and_fasta.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
#!/usr/bin/perl
###################################################################################################################################
#
#
#
# print_taxa_overlapping_newick_and_fasta.pl,
# Perl script that parses the taxa overlapping between a phylogeny and a file of DNA sequences
#
#
# Copyright (C) 2014 Douglas Chesters
#
# 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/>.
#
# contact address dc0357548934@live.co.uk
#
#
#
#
###################################################################################################################################
#
#
#
# Takes a file with a phylogeny and a file with DNA sequences,
# reads the taxa on each, then prints a new phylogeny and new sequence file of only the overlapping taxa
# Currently only works for terminals/fasta IDs in strict binomial format
# Script is entirely self-contained, does not load any modules for anything,
# so you can modify any aspect for whatever your purpose.
#
# To run:
# perl print_taxa_overlapping_newick_and_fasta.pl [newick_file_name] [fasta_file_name]
#
#
#
#
# change log:
# 03JUL2014: first release version
# 24JUL2014: option to include taxa in the fasta file that are not in the tree.
# these are included since a raxml constraint analysis can add them
# 05AUG2014: rm boot values from input reference tree. this was stopping correct functioning.
#
#
$script_version = 1.03;
#
#
#
#
#
###################################################################################################################################
$include_nonoverlapping_taxa_in_fasta_file = 1;
$verbose = 0;
$treefile = $ARGV[0];
$fastafile = $ARGV[1];
# check input files:
#########
check();#
#########
print "\n\n\n\tprint_taxa_overlapping_newick_and_fasta.pl (version:$script_version)
\tinput files, tree:$treefile sequences:$fastafile\n\n";
# some global variables:
%nodes;
$root_node;
%parent;
$count_terminals_from_current_node = 0;
# read the tree into memory
######################
read_file($treefile);#
######################
my @terminal_array = keys %terminals;@terminal_array = sort @terminal_array;
print scalar @terminal_array , " terminals parsed\n";
# read the sequence file and record entries that overlap with those found in the tree earlier
################
fasta_reader();#
################
my $notfound = 0;
my $found = 0;
foreach my $terminal(@terminal_array)
{
unless($terminal =~ /^[A-Z][a-z]+_[a-z]+$/)
{#print "non binomial:$terminal\n"
}
if($terminals{$terminal} == 2)
{
$found++; if($verbose ==1){ print "tree terminal:$terminal is found in sequence DB\n"};
}else{
$notfound++; if($verbose ==1){ print "tree terminal:$terminal is NOT found in sequence DB\n"};
}
}
print "\nOverlapping tree and DB:$found.
In parsed tree but not found in DB:$notfound\n\n";
# There are probably many ways to prune a tree, of which this is just one.....
# The tree (even when unrooted) has a natural dimension, root to tips.
# Reading the tree earlier is easy from tip to root due to size of the strings,
# but once the tree is in memory, it seems more natural to travel root to tip (only one starting point).
# So traverse in this way, but ignore the members that need pruning
# (by not printing them to the new newick string).
# However you dont want to wait until you actually reach these nodes,
# since what if the sister node also requires pruning,
# that means you have just processed a node (the parent) that is now also a non-required node,
# and you have to go back up and thats complicated.
# So, first check each node to see if its a 'dead-end',
# i.e. all decendends are to be pruned,
# that way, you can then traverse the tree and know all nodes to be pruned, not just all terminals.
################################
find_deadend_nodes($root_node);#
################################
# a new newick string will be developed in which non-overlapping members are ommited, start of this string:
$new_newick_string = "($root_node);";
##################################
build_tree_structure($root_node);#
##################################
# some minor processing of the new newick tree, then print to file:
$new_newick_string =~ s/^\((.+)\)(\;)/$1$2/;
open(OUT, ">$treefile.overlapping") || die "\nerror, cant open output file ($treefile.overlapping). \nquitting.\n";
print OUT "$new_newick_string\n";
close OUT;
# then print fasta entries which are overlapping, this is a lot simpler
my @overlapping_tax = keys %store_seqs_for_overlapping_species;@overlapping_tax =sort @overlapping_tax;
open(OUT, ">$fastafile.overlapping") || die "\nerror 100.\n";
foreach my $tax(@overlapping_tax)
{print OUT ">$tax\n$store_seqs_for_overlapping_species{$tax}\n"}
close OUT;
print "\nfin.\n";
exit;
# end of script, subroutines are below
#######################################################################################################################################
#
#
#
#
#
#######################################################################################################################################
sub read_file
{
my $file = shift;
open(IN , $file) || die "\nerror 24.\n";
my $tree;my $found_tree=0;
while (my $line = <IN>)
{
$line =~ s/\n//;$line =~ s/\r//;
if($line =~ /(.+\(.+)\;/)
{
$tree = $1;
$found_tree =1;
}
}
close(IN);
if($found_tree == 1)
{
# remove branchlengths, scientific notation, incl negative values for distance trees. example: -8.906e-05
$tree =~ s/\:\-*\d+\.\d+[eE]\-\d+([\(\)\,])/$1/g;
# remove regular branchlengths: 0.02048
$tree =~ s/\:\-*\d+\.\d+//g;
# remove 0 length branchlengths
$tree =~ s/\:\d+//g;
# remove these things
$tree =~ s/[\[\]]//g;
# tree from 'delayed rise of modern day mammals':
# don':5.0,Perameles_bougainville:12.4,(Perameles_gunnii:7.7,Perameles_nasuta:7.7)'2077_Perameles*':4.7)'2074_Peramelidae*':6.1)'2070':17.7,Macrotis
# remove these node labels
$tree =~ s/\)\'[^\']+\'/)/g;
# remove boot support values (05 aug 2014). not sure why this wasnt here before
while($tree =~ /\)\d+[\)\,]/){$tree =~ s/(\))\d+([\)\,])/$1$2/g};
print "\nNOTE:
tree file has been read. comments, branchlengths and support values have been removed.\n\n";
#print "tree:$tree\n";die;
######################
parse_newick ($tree);#
######################
}else{die "\nerror 229.\n"}
}
#######################################################################################################################################
#
#
#
#
#
#######################################################################################################################################
sub parse_newick
{
my $newick_string = shift;
#if($job == 1){$tree_copy = $newick_string}
#print "newick_string:$newick_string\n";
#print OUT "$newick_string\n";
$interal_node =0;
# tree is parsed inside-out, this means we are dealing with short simpler strings
# parse one node at a time, by looking for instances of ( ....... )
while ($newick_string =~ s/\(([^\(\)]+)\)/INTERNAL_NODE_$interal_node/)
{
# found a pair of parentheses (with no parentheses inside).
# this string is taken, and replaced with a single node (INTERNAL_NODE_\d)
# node is numbered (\d in variable $interal_node),
# the number being incremented at the end of the loop
my $node = $1;my $nodeID = "INTERNAL_NODE_$interal_node";
# find what is at the current node (decendents), by splitting at the commas
my @child_nodes = split /\,/ , $node;
$child_counts{$nodeID} = $#child_nodes;
for $i(0 .. $#child_nodes)
{
my $bl = "NA"; # branch length stuff, currently not relevent
if($child_nodes[$i] =~ /\:(.+)/)
{$bl = $1
}else{#die "error no banchlength"
};
$child_nodes[$i] =~ s/\:(.+)//;
#$node_branchlengths{$child_nodes[$i]} = $bl;
# record each decendent of the current node, in a hash nodes{ the current node }{ child number }
$nodes{$nodeID}{$i} = $child_nodes[$i];
$parent{$child_nodes[$i]} = $nodeID;
# print "node:$child_nodes[$i]\tbl:$bl\n";
# and record whether the current node is a terminal one
unless($child_nodes[$i] =~ /INTERNAL_NODE_/){$terminals{$child_nodes[$i]} =1}
}
# print "node:$interal_node\n\tchild nodes:@child_nodes\n";
$root_node = $nodeID; # this is a global and will keep the value in the final loop, so the root can be identified later.
$interal_node++;
}
#print "newick string has been read\n";
}
#######################################################################################################################################
#
#
#
#
#
#######################################################################################################################################
sub build_tree_structure
{
my $current_node = shift;
my $child_count_current_node = $child_counts{$current_node};
#print "\n\nNEW NODE:$current_node number of children:$child_count_current_node\n";
########################################################################################################################
# start string that will replace current node:
my $current_node_new_string = "";
for my $j(0 .. $child_count_current_node)
{
# retrieve decendends of the current node, go through each and check if it will be incorporated,
my $child_node = $nodes{$current_node}{$j};
if($terminals{$child_node} == 1) # this hash is filled as: $terminals{$binomial} = 2;
{ # current entry of fasta file ($speciesid) is from a species in the tree ($binomial)\n";
# otherwise, it would be 1
#print " ... is NOT recorded as overlapping\n";
}else{
#print " ... might be overlapping ... ";
if($deadendnode{$child_node}==1)
{
#print "but is a deadend node\n";
}else{
#print "appending to node replacemnt string\n";
# current child node is overlapping, so put into current string:
$current_node_new_string .= "$child_node,";
}
};
}
########################################################################################################################
# remove an unwanted comma at end of line
$current_node_new_string =~ s/\,$//;
if($current_node_new_string =~ /\,/)
{
# current node has more than one member, will need to be encased in parentheses
$current_node_new_string = "(" . $current_node_new_string . ")"
};
#print "\tcurrent node:$current_node becomes:$current_node_new_string\n";
if($current_node_new_string eq "")
{
print "NOTHING\n";die "\nerror 409\n";
}else{
# update newick string at the current node, replace old node with the decendents
$new_newick_string =~ s/([\(\)\,])$current_node([\(\)\,])/$1$current_node_new_string$2/;
};
########################################################################################################################
for my $j(0 .. $child_count_current_node)
{
# current node is now processed.
# go though the list of decendents again, and if appropriate, traverse to next node
my $child_node = $nodes{$current_node}{$j};#print "\tnode:$current_node child:$child_node\n";
if($child_node =~ /^INTERNAL_NODE_\d+/)
{
if($deadendnode{$child_node}==1)
{
#print "\tchild_node:$child_node has no terminals to be printed\n";
}else{
#print "\tchild_node:$child_node, recursing\n";
###################################
build_tree_structure($child_node);# current child node is not terminal, and not a dead-end, so keep going tip-ward
###################################
}
}else{
#print "\tchild_node:$child_node, end of the line\n";
}
}
}
#######################################################################################################################################
#
#
#
#
#
#######################################################################################################################################
sub fasta_reader
{
my $file_as_string = "";
open(IN_FILTER , $fastafile) || die "\nerror. cant open\n";
print "sub fasta reader, reading:$fastafile\n";
while (my $line = <IN_FILTER>)
{$file_as_string .= $line};
close(IN_FILTER);
# this is a simple fasta reader which reads all the file into memory,
# therefor not suitable for very very large sequence files.
my @all_lines = split />/, $file_as_string;
my $number_of_binomials_read=0;
for my $each_line(1 .. $#all_lines)
{
my $line = $all_lines[$each_line];
if($line =~ /^(.+)/)
{
my $speciesid = $1; #print "$speciesid\n";
$line =~ s/^.+\n//;
$line =~ s/\012\015?|\015\012?//g;$line =~ s/\n//g;$line =~ s/\r//g;
$line =~ s/[\s\t]//g;
if($speciesid =~ /^([A-Z][a-z]+_[a-z]+)/)
{
my $binomial = $1;$number_of_binomials_read++;
if(exists($terminals{$binomial}))
{
#print "current entry of fasta file ($speciesid) is from a species in the tree ($binomial)\n";
# current entry is overlapping, record the ID and store the DNA sequence:
$terminals{$binomial} = 2;
$store_seqs_for_overlapping_species{$binomial} = $line;
}else{
if($include_nonoverlapping_taxa_in_fasta_file == 1)
{
$store_seqs_for_overlapping_species{$binomial} = $line;#######################
}
# print "binomial ($binomial) wasnt recorded from tree\n";
}
}
}#if($line =~ /^(.+)/)
}#for my $each_line(1 .. $#all_lines)
print "
NOTE:
currently this script is just for use with identifiers in strict binomial format:Genus_species
if you need a more general implementation, send me you input files and i will modify the script accordingly\n\n";
print scalar @all_lines , " seqs in file\n";
unless($number_of_binomials_read >= 2)
{
print "\nerror parsing binomial fasta IDs. expecting:[A-Z][a-z]+_[a-z]+\nquitting.\n\n"
}
};
#######################################################################################################################################
#
#
#
#
#
#######################################################################################################################################
sub get_terminals_from_this_node
{
my $current_node_for_terminals = shift;
my $child_count2 = $child_counts{$current_node_for_terminals};
my $current_node_new_string = "";
for my $j(0 .. $child_count2)
{
my $child_node2 = $nodes{$current_node_for_terminals}{$j};#print "\tnode:$current_node child:$child_node\n";
if($child_node2 =~ /^INTERNAL_NODE_\d+/)
{
############################################
get_terminals_from_this_node($child_node2);#
############################################
}else{
# traverse through to a terminal node and check if it is overlapping
if($terminals{$child_node2} == 2)
{
$count_terminals_from_current_node++;
# print "terminal:$child_node2 overlapping:1 ";
}else{
# print "terminal:$child_node2 overlapping:0 ";
};
};
}
return();
}
#######################################################################################################################################
#
#
#
#
#
#######################################################################################################################################
sub find_deadend_nodes
{
my $current_node = shift;
my $child_count_current_node = $child_counts{$current_node};
#print "\nNODE1:$current_node number of children:$child_count_current_node\n";
# for current node check all terminals that decendend from it,
# and count how many are overlapping with the sequence file
$count_terminals_from_current_node =0;
############################################
get_terminals_from_this_node($current_node);#
############################################
#print "\tcount_terminals_from_current_node:$count_terminals_from_current_node\n";
if($count_terminals_from_current_node == 0)
{
# if non of the terminal are overlapping, record current node as a dead-end.
# then no point to proceed further, break from current recurse
$deadendnode{$current_node}=1;
}else{
# otherwise current node has decendent terminals that are overlapping
for my $j(0 .. $child_count_current_node)
{
my $child_node = $nodes{$current_node}{$j};#print "\tnode:$current_node child:$child_node\n";
if($child_node =~ /^INTERNAL_NODE_\d+/)
{
#################################
find_deadend_nodes($child_node);# not reached the tips yet, so recurse.
#################################
}
}
}
}
#######################################################################################################################################
#
#
#
#
#
#######################################################################################################################################
sub check
{
unless($treefile =~ /[\w\d]/ && $fastafile =~ /[\w\d]/)
{
print "\nerror in command. \nto run type:\nperl print_taxa_overlapping_newick_and_fasta.pl " ,
"[newick_file_name] [fasta_file_name]\n\n... quitting ...\n";die;
};
# check tree file looks like a tree file:
open(IN, $treefile) || die "\nerror. cannot open the tree file you specified ($treefile)\n\n" ,
"check this file is named correctly and in the working directory\n";
my $count_trees =0;
while(my $line=<IN>)
{
if($line =~ /\(/){$count_trees++}
}
close IN;
unless($count_trees == 1)
{die "\n\nerror, treefile ($treefile) doesnt look right. expecting newick format and a single tree. quitting.\n\n"}
# same for seq file:
open(IN, $fastafile) || die "\nerror. cannot open the fasta file you specified ($fastafile)\n\n" ,
"check this file is named correctly and in the working directory\n";
my $count_fas =0;
while(my $line=<IN>)
{
if($line =~ /^>.+/){$count_fas++}
}
close IN;
unless($count_fas >= 2)
{die "\n\nerror, sequence file ($fastafile) doesnt look right. expecting fasta format with multiple entries. quitting.\n\n"}
close IN;
};
#######################################################################################################################################
#
#
#
#
#
#######################################################################################################################################