Bio/TreeIO/nexus.pm

filter: Statementstmt 50.0% 70 / 140 Branchbran 26.7% 23 / 86 Conditioncond 9.5% 2 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 75.0% 9 / 12 Podpod 80.0% 4 / 5 totaltotal 38.8% 108 / 278 CC 56 SCAR 54.7
CC
56
Cov
38%
_write_tree_Helper
600.0
write_tree
110.0
_parse
23.1
CRAP 238.3
« Bio/SeqIO/gbdriver.pm ↑ Summary Bio/Search/BlastUtils.pm »
1 #
2 # BioPerl module for Bio::TreeIO::nexus
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Jason Stajich <jason-at-open-bio-dot-org>
7 #
8 # Copyright Jason Stajich
9 #
10 # You may distribute this module under the same terms as perl itself
11  
12 # POD documentation - main docs before the code
13  
14 =head1 NAME
15  
16 Bio::TreeIO::nexus - A TreeIO driver module for parsing Nexus tree output from PAUP
17  
18 =head1 SYNOPSIS
19  
20 use Bio::TreeIO;
21 my $in = Bio::TreeIO->new(-file => 't/data/cat_tre.tre');
22 while( my $tree = $in->next_tree ) {
23 }
24  
25 =head1 DESCRIPTION
26  
27 This is a driver module for parsing PAUP Nexus tree format which
28 basically is just a remapping of trees.
29  
30 =head2 Comments
31  
32 The nexus format allows node comments that are placed inside square
33 brackets. Usually the comments (implemented as tags for nodes) are
34 used to give a name for an internal node or record the bootstrap value,
35 but other uses are possible.
36  
37 The FigTree program by Andrew Rambaut adds various rendering
38 parameters inside comments and flags these comments by starting them
39 with '&!'. The parameters implemented here are 'label' and 'color'.
40  
41 =head1 FEEDBACK
42  
43 =head2 Mailing Lists
44  
45 User feedback is an integral part of the evolution of this and other
46 Bioperl modules. Send your comments and suggestions preferably to
47 the Bioperl mailing list. Your participation is much appreciated.
48  
49 bioperl-l@bioperl.org - General discussion
50 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
51  
52 =head2 Support
53  
54 Please direct usage questions or support issues to the mailing list:
55  
56 I<bioperl-l@bioperl.org>
57  
58 rather than to the module maintainer directly. Many experienced and
59 reponsive experts will be able look at the problem and quickly
60 address it. Please include a thorough description of the problem
61 with code and data examples if at all possible.
62  
63 =head2 Reporting Bugs
64  
65 Report bugs to the Bioperl bug tracking system to help us keep track
66 of the bugs and their resolution. Bug reports can be submitted via
67 the web:
68  
69 https://github.com/bioperl/bioperl-live/issues
70  
71 =head1 AUTHOR - Jason Stajich
72  
73 Email jason-at-open-bio-dot-org
74  
75 =head1 APPENDIX
76  
77 The rest of the documentation details each of the object methods.
78 Internal methods are usually preceded with a _
79  
80 =cut
81  
82 # Let the code begin...
83  
84 package Bio::TreeIO::nexus;
85 4 $Bio::TreeIO::nexus::VERSION = '1.7.8';
86 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 4 ▶use Bio::Event::EventGeneratorI;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 4 ▶use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 4 ▶use base qw(Bio::TreeIO);
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 =head2 new
94  
95 Title : new
96 Args : -header => boolean default is true
97 print/do not print #NEXUS header
98 -translate => boolean default is true
99 print/do not print Node Id translation to a number
100  
101 =cut
102  
103 9 ▶sub _initialize {
Subroutine _initialize: called CC=3 SCAR=11.3
104 9 my $self = shift;
105 9 $self->SUPER::_initialize(@_);
106 9 my ( $hdr, $trans ) = $self->_rearrange(
107 [
108 qw(HEADER
109 TRANSLATE)
110 ],
111 @_
112 );
113 9 ▶ $self->header( defined $hdr ? $hdr : 1 );
Branch: 1 branch
BranchTrueFalseTotal
defined $hdr ? : 0 9 9
114 9 ▶ $self->translate_node( defined $trans ? $trans : 1 );
Branch: 1 branch
BranchTrueFalseTotal
defined $trans ? : 0 9 9
115 }
116  
117 =head2 next_tree
118  
119 Title : next_tree
120 Usage : my $tree = $treeio->next_tree
121 Function: Gets the next tree in the stream
122 Returns : Bio::Tree::TreeI
123 Args : none
124  
125  
126 =cut
127  
128 39 ▶sub next_tree {
Subroutine next_tree: called CC=2 SCAR=6.9
Pod documented
129 39 my ($self) = @_;
130 39 ▶ unless ( $self->{'_parsed'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_parsed"}) 9 30 39
131 9 $self->_parse;
132 }
133 39 return $self->{'_trees'}->[ $self->{'_treeiter'}++ ];
134 }
135  
136 0 ▶sub rewind {
Subroutine rewind: not called CC=1 SCAR=6.9
Pod undocumented
137 0 shift->{'_treeiter'} = 0;
138 }
139  
140 9 ▶sub _parse {
Subroutine _parse: called CC=19 SCAR=31.4
141 9 my ($self) = @_;
142  
143 9 $self->{'_parsed'} = 1;
144 9 $self->{'_treeiter'} = 0;
145  
146 9 ▶ while ( defined( $_ = $self->_readline ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $self->_readline)) 9 0 9
147 9 ▶ next if /^\s+$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s+$/ 0 9 9
148 9 last;
149 }
150 9 ▶ return unless ( defined $_ );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 9 9
151  
152 9 ▶ unless (/^\#NEXUS/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^\#NEXUS/i) 0 9 9
153 0 $self->warn("File does not start with #NEXUS"); #'
154 0 return;
155 }
156  
157 9 my $line;
158 9 ▶ while ( defined( $_ = $self->_readline ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $self->_readline)) 283 9 292
159 283 $line .= $_;
160 }
161 9 my @sections = split( /#NEXUS/i, $line );
162 9 for my $s (@sections) {
163 13 my %translate;
164 13 ▶ if ( $self->verbose > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose > 0) { } 0 13 13
165 0 ▶ while ( $s =~ s/(\[[^\]]+\])// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s/(\[[^\]]+\])//) 0 0 0
166 0 $self->debug("removing comment $1\n");
167 }
168 }
169 else {
170 13 $s =~ s/(\[[^\]]+\])//g;
171 }
172
173 13 ▶ if ( $s =~ /begin trees;(.+)(end;)?/si ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /begin trees;(.+)(end;)?/is) { } 13 0 13
174 13 my $trees = $1;
175 13 ▶ if ( $trees =~ s/\s+translate\s+([^;]+);//i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trees =~ s/\s+translate\s+([^;]+);//i) { } 12 1 13
176 12 my @trans;
177 12 my $tr = $1;
178  
179 12 ▶ while ($tr =~ m{\s*([^,\s]+?\s+(?:'[^']+'|[^'\s]+)),?}gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tr =~ /\s*([^,\s]+?\s+(?:'[^']+'|[^'\s]+)),?/cg) 158 12 170
180 158 push @trans, $1;
181 }
182 12 for my $n ( @trans ) {
183 158 ▶ if ($n =~ /^\s*(\S+)\s+(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ /^\s*(\S+)\s+(.+)$/) 158 0 158
184 158 my ($id,$tag) = ($1,$2);
185 158 $tag =~ s/[\s,]+$//; # remove the extra spaces of the last taxon
186 158 $translate{$id} = $tag;
187 }
188 }
189 }
190 else {
191 1 $self->debug("no translate in: $trees\n");
192 }
193 13 ▶ while ($trees =~ /\s+tree\s+\*?\s*(\S+)\s*\=
Branch: 1 branch
BranchTrueFalseTotal
if ($trees =~ /\s+tree\s+\*?\s*(\S+)\s*\= \s*(?:\[\S+\])?\s*([^\;]+;)/gix) 37 13 50
194 \s*(?:\[\S+\])?\s*([^\;]+;)/igx)
195 {
196 37 my ( $tree_name, $tree_str ) = ( $1, $2 );
197  
198 # MrBayes does not print colons for node label
199 # $tree_str =~ s/\)(\d*\.\d+)\)/:$1/g;
200 37 my $buf = IO::String->new($tree_str);
201 37 my $treeio = Bio::TreeIO->new(
202 -format => 'newick',
203 -fh => $buf
204 );
205 37 my $tree = $treeio->next_tree;
206 37 foreach my $node ( grep { $_->is_Leaf } $tree->get_nodes ) {
207 1470 my $id = $node->id;
208 1470 my $lookup = $translate{$id};
209 1470 ▶ $node->id( $lookup || $id );
Condition66%2/3
$lookup || $id
l!l&&r!l&&!r
1454160
MC/DC0%0/2
$lookup || $id
$lookup $id
Truth table66%2/3
A || B
A= $lookupB= $id
ABresult
1X1
011
000
210 }
211 37 ▶ $tree->id($tree_name) if defined $tree_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tree_name 37 0 37
212 37 push @{ $self->{'_trees'} }, $tree;
213 }
214 }
215 else {
216 0 $self->debug("begin_trees failed: $s\n");
217 }
218 }
219 9 ▶ if ( !@sections ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @sections) 0 9 9
220 0 $self->debug("warn no sections: $line\n");
221 }
222 }
223  
224 =head2 write_tree
225  
226 Title : write_tree
227 Usage : $treeio->write_tree($tree);
228 Function: Writes a tree onto the stream
229 Returns : none
230 Args : Bio::Tree::TreeI
231  
232  
233 =cut
234  
235 0 ▶sub write_tree {
Subroutine write_tree: not called CC=10 SCAR=47.0
Pod documented
236 0 my ( $self, @trees ) = @_;
237 0 ▶ if ( $self->header ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->header) 0 0 0
238 0 $self->_print("#NEXUS\n\n");
239 }
240 0 my $translate = $self->translate_node;
241 0 my $time = localtime();
242 0 $self->_print( sprintf( "Begin trees; [Treefile created %s]\n", $time ) );
243  
244 0 my ( $first, $nodecter, %node2num ) = ( 0, 1 );
245 0 foreach my $tree (@trees) {
246  
247 0 ▶ if ( $first == 0
Branch: 1 branch
BranchTrueFalseTotal
if ($first == 0 and $translate) 0 0 0
Condition0%0/3
$first == 0 and $translate
!ll&&!rl&&r
000
MC/DC0%0/2
$first == 0 and $translate
$first == 0 $translate
Truth table0%0/3
A and B
A= $first == 0B= $translate
ABresult
0X0
100
111
248 && $translate )
249 {
250 0 $self->_print("\tTranslate\n");
251 $self->_print(
252 join(
253 ",\n",
254 map {
255 0 $node2num{ $_->id } = $nodecter;
256 0 sprintf( "\t\t%d %s", $nodecter++, $_->id )
257 }
258 0 grep { $_->is_Leaf } $tree->get_nodes
259 ),
260 "\n;\n"
261 );
262 }
263 0 my @data = _write_tree_Helper( $tree->get_root_node, \%node2num );
264 0 ▶ if ( $data[-1] !~ /\)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data[-1] =~ /\)$/) 0 0 0
265 0 $data[0] = "(" . $data[0];
266 0 $data[-1] .= ")";
267 }
268  
269 # by default all trees in bioperl are currently rooted
270 # something we'll try and fix one day....
271 $self->_print(
272 0 ▶ sprintf(
Branch: 1 branch
BranchTrueFalseTotal
$tree->get_root_node ? : 0 0 0
Condition0%0/3
$tree->id || sprintf('Bioperl_%d', $first + 1)
l!l&&r!l&&!r
000
MC/DC0%0/2
$tree->id || sprintf('Bioperl_%d', $first + 1)
$tree->id sprintf('Bioperl_%d', $first + 1)
Truth table0%0/3
A || B
A= $tree->idB= sprintf('Bioperl_%d', $first + 1)
ABresult
1X1
011
000
273 "\t tree %s = [&%s] %s;\n",
274 ( $tree->id || sprintf( "Bioperl_%d", $first + 1 ) ),
275 ( $tree->get_root_node ) ? 'R' : 'U',
276 join( ',', @data )
277 )
278 );
279 0 $first++;
280 }
281 0 $self->_print("End;\n");
282 0 ▶ $self->flush if $self->_flush_on_write && defined $self->_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_flush_on_write and defined $self->_fh 0 0 0
Condition0%0/3
$self->_flush_on_write and defined $self->_fh
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_flush_on_write and defined $self->_fh
$self->_flush_on_write defined $self->_fh
Truth table0%0/3
A and B
A= $self->_flush_on_writeB= defined $self->_fh
ABresult
0X0
100
111
283 0 return;
284 }
285  
286 0 ▶sub _write_tree_Helper {
Subroutine _write_tree_Helper: not called CC=24 SCAR=64.0
287 0 my ( $node, $node2num ) = @_;
288 0 ▶ return () if ( !defined $node );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 0 0
289 0 my @data;
290  
291 0 foreach my $n ( $node->each_Descendent() ) {
292 0 push @data, _write_tree_Helper( $n, $node2num );
293 }
294 0 ▶ if ( @data > 1 ) { # internal node
Branch: 1 branch
BranchTrueFalseTotal
if (@data > 1) { } 0 0 0
295 0 $data[0] = "(" . $data[0];
296 0 $data[-1] .= ")";
297  
298 # FigTree comments start
299 0 my $comment_flag;
300 0 ▶ $comment_flag = 0
Branch: 1 branch
BranchTrueFalseTotal
if $node->has_tag("color") or $node->has_tag("label") 0 0 0
Condition0%0/3
$node->has_tag("color") or $node->has_tag("label")
l!l&&r!l&&!r
000
MC/DC0%0/2
$node->has_tag("color") or $node->has_tag("label")
$node->has_tag("color") $node->has_tag("label")
Truth table0%0/3
A or B
A= $node->has_tag("color")B= $node->has_tag("label")
ABresult
1X1
011
000
301 if ( $node->has_tag('color') or $node->has_tag('label') );
302
303 0 ▶ $data[-1] .= '[&!' if defined $comment_flag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $comment_flag 0 0 0
304
305 0 ▶ if ( $node->has_tag('color')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->has_tag("color")) 0 0 0
306 0 my $color = $node->get_tag_values('color');
307 0 $data[-1] .= "color=$color";
308 0 $comment_flag++;
309 }
310 0 ▶ if ( $node->has_tag('label')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->has_tag("label")) 0 0 0
311 0 my $label = $node->get_tag_values('label');
312 0 ▶ $data[-1] .= ',' if $comment_flag;
Branch: 1 branch
BranchTrueFalseTotal
if $comment_flag 0 0 0
313 0 $data[-1] .= 'label="'. $label. '"';
314 }
315 0 ▶ $data[-1] .= ']' if defined $comment_flag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $comment_flag 0 0 0
316 # FigTree comments end
317
318 # let's explicitly write out the bootstrap if we've got it
319 0 my $b;
320  
321 0 my $bl = $node->branch_length;
322 0 ▶ if ( !defined $bl ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $bl) { } 0 0 0
elsif ($bl =~ /\#/) { } 0 0 0
323 }
324 elsif ( $bl =~ /\#/ ) {
325 0 $data[-1] .= $bl;
326 }
327 else {
328 0 $data[-1] .= ":$bl";
329 }
330 0 ▶ if ( defined( $b = $node->bootstrap ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined($b = $node->bootstrap)) { } 0 0 0
elsif (defined($b = $node->id)) { } 0 0 0
331 0 $data[-1] .= sprintf( "[%s]", $b );
332 }
333 elsif ( defined( $b = $node->id ) ) {
334 0 ▶ $b = $node2num->{$b} if ( $node2num->{$b} ); # translate node2num
Branch: 1 branch
BranchTrueFalseTotal
if $node2num->{$b} 0 0 0
335 0 ▶ $data[-1] .= sprintf( "[%s]", $b ) if defined $b;
Branch: 1 branch
BranchTrueFalseTotal
if defined $b 0 0 0
336 }
337  
338 }
339 else { # leaf node
340 0 ▶ if ( defined $node->id || defined $node->branch_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node->id or defined $node->branch_length) 0 0 0
Condition0%0/3
defined $node->id or defined $node->branch_length
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $node->id or defined $node->branch_length
defined $node->id defined $node->branch_length
Truth table0%0/3
A or B
A= defined $node->idB= defined $node->branch_length
ABresult
1X1
011
000
341 0 ▶ my $id = defined $node->id ? $node->id : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $node->id ? : 0 0 0
342 0 ▶ if ( length($id) && $node2num->{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $id and $node2num->{$id}) 0 0 0
Condition0%0/3
length $id and $node2num->{$id}
!ll&&!rl&&r
000
MC/DC0%0/2
length $id and $node2num->{$id}
length $id $node2num->{$id}
Truth table0%0/3
A and B
A= length $idB= $node2num->{$id}
ABresult
0X0
100
111
343 0 $id = $node2num->{$id};
344 }
345 0 ▶ if ( $node->has_tag('color')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->has_tag("color")) 0 0 0
346 0 my ($color) = $node->get_tag_values('color');
347 0 $id .= "[&!color=$color\]";
348 }
349 0 ▶ push @data,
Branch: 1 branch
BranchTrueFalseTotal
defined $node->branch_length ? : 0 0 0
350 sprintf( "%s%s",
351 $id,
352 defined $node->branch_length
353 ? ":" . $node->branch_length
354 : '' );
355 }
356 }
357 0 return @data;
358 }
359  
360 =head2 header
361  
362 Title : header
363 Usage : $obj->header($newval)
364 Function:
365 Example :
366 Returns : value of header (a scalar)
367 Args : on set, new value (a scalar or undef, optional)
368  
369  
370 =cut
371  
372 9 ▶sub header {
Subroutine header: called CC=2 SCAR=8.1
Pod documented
373 9 my $self = shift;
374  
375 9 ▶ return $self->{'header'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 9 0 9
376 0 return $self->{'header'};
377 }
378  
379 =head2 translate_node
380  
381 Title : translate_node
382 Usage : $obj->translate_node($newval)
383 Function:
384 Example :
385 Returns : value of translate_node (a scalar)
386 Args : on set, new value (a scalar or undef, optional)
387  
388  
389 =cut
390  
391 9 ▶sub translate_node {
Subroutine translate_node: called CC=2 SCAR=8.1
Pod documented
392 9 my $self = shift;
393  
394 9 ▶ return $self->{'translate_node'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 9 0 9
395 0 return $self->{'translate_node'};
396 }
397  
398 4 1;
« Bio/SeqIO/gbdriver.pm ↑ Summary Bio/Search/BlastUtils.pm »