Phylo/Parsers/Newick.pm

filter: Statementstmt 96.3% 182 / 189 Branchbran 90.0% 81 / 90 Conditioncond 83.5% 112 / 134 MC/DCmcdc 63.6% 42 / 66 Subroutinesub 92.8% 13 / 14 Podpod n/a totaltotal 87.2% 430 / 493 CC 96 SCAR 29.9
CC
96
Cov
91%
_split
34.4
_next_token
24.5
_prune_cherry
14.0
CRAP 19.9
« Phylo/Matrices/Datatype/Mixed.pm ↑ Summary Phylo/Treedrawer/Pdf.pm »
1 package Bio::Phylo::Parsers::Newick;
2 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use base 'Bio::Phylo::Parsers::Abstract';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 ▶no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Bio::Phylo::Parsers::Newick - Parser used by Bio::Phylo::IO, no serviceable parts inside
10  
11 =head1 DESCRIPTION
12  
13 This module parses tree descriptions in parenthetical format. It is called by the
14 L<Bio::Phylo::IO> facade, don't call it directly. Several additional flags can be
15 passed to the Bio::Phylo::IO parse and parse_tree functions to influence how to deal
16 with complex newick strings:
17  
18 -keep => [ ...list of taxa names... ]
19  
20 The C<-keep> flag allows you to only retain certain taxa of interest, ignoring others
21 while building the tree object.
22  
23 -ignore_comments => 1,
24  
25 This will treat comments in square brackets as if they are a normal taxon name character,
26 this so that names such as C<Choristoneura diversana|BC ZSM Lep 23401[05/*> are parsed
27 "successfully". (Note: square brackets should NOT be used in this way as it will break
28 many parsers).
29  
30 -keep_whitespace => 1,
31  
32 This will treat unescaped whitespace as if it is a normal taxon name character. Normally,
33 whitespace is only retained inside quoted strings (e.g. C<'Homo sapiens'>), otherwise it
34 is the convention to use underscores (C<Homo_sapiens>). This is because some programs
35 introduce whitespace to prettify a newick string, e.g. to indicate indentation/depth,
36 in which case you almost certainly want to ignore it. This is the default behaviour. The
37 option to keep it is provided for dealing with incorrectly formatted data.
38  
39 =cut
40  
41 83 ▶sub _return_is_scalar { 1 }
Subroutine _return_is_scalar: called CC=1 SCAR=0.0
42  
43  
44 56 ▶sub _simplify {
Subroutine _simplify: called CC=2 SCAR=6.9
45 # Simplify a Newick tree string by removing unneeded nodes. The leaves to
46 # keep are given as $ids, an arrayref of terminal node IDs. Note that only
47 # cherries are simplified to keep the function fast. Ternary or higher order
48 # branches are left alone. Quoted strings should be handled properly.
49 56 my ($string, $ids) = @_;
50 56 my %id_hash = map { $_ => undef } @$ids;
51  
52 # Setup some regular expressions:
53 # 1/ ID is anything but these characters (except when quoted): , ; : ( ) " '
54 56 my $id_re_simple = qr/[^)(,:"';]+/;
55 56 my $id_re_squote = qr/[^']+/;
56 56 my $id_re_dquote = qr/[^']+/;
57 56 my $id_re = qr/ (?: $id_re_simple | '$id_re_squote' | "$id_re_dquote" ) /x;
58 # 2/ Distance is a real number (regexp taken from Regexp::Common $RE{num}{real})
59 56 my $dist_re = qr/(?:(?i)(?:[+-]?)(?:(?=[.]?[0123456789])(?:[0123456789]*)(?:(?:[.])(?:[0123456789]{0,}))?)(?:(?:[E])(?:(?:[+-]?)(?:[0123456789]+))|))/;
60 # 3/ A pair of ID and distance (both optional)
61 56 my $pair_re = qr/ ($id_re)? (?: \: ($dist_re) )? /x;
62 # 4/ Cherry
63 56 my $cherry_re = qr/ ( \( $pair_re , $pair_re \) $pair_re ) /x;
64 # 5/ Whitespaces
65 56 my $ws_re = qr/ \s+ /msx;
66  
67 # Remove spaces and newlines (no spaces allowed in node names)
68 56 $string =~ s/$ws_re//g;
69  
70 # Prune cherries
71 56 my $prev_string = '';
72 56 ▶ while (not $string eq $prev_string) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($string eq $prev_string) 134 56 190
73 134 $prev_string = $string;
74 134 $string =~ s/ $cherry_re / _prune_cherry($1, $2, $3, $4, $5, $6, $7, \%id_hash) /gex;
75 }
76 56 __PACKAGE__->_logger->debug("simplified string by removing unneeded nodes");
77 56 return $string;
78 }
79  
80  
81 108 ▶sub _prune_cherry {
Subroutine _prune_cherry: called CC=14 SCAR=26.4
82 108 my ($match, $id1, $dist1, $id2, $dist2, $idp, $distp, $id_hash) = @_;
83 108 my $repl;
84 108 ▶ my $id1_exists = defined $id1 && exists $id_hash->{$id1};
Condition100%3/3
defined $id1 && exists $id_hash->{$id1}
!ll&&!rl&&r
242757
MC/DC100%2/2
defined $id1 && exists $id_hash->{$id1}
defined $id1 exists $id_hash->{$id1}
Truth table100%3/3
A && B
A= defined $id1B= exists $id_hash->{$id1}
ABresult
0X0
100
111
85 108 ▶ my $id2_exists = defined $id2 && exists $id_hash->{$id2};
Condition100%3/3
defined $id2 && exists $id_hash->{$id2}
!ll&&!rl&&r
244143
MC/DC100%2/2
defined $id2 && exists $id_hash->{$id2}
defined $id2 exists $id_hash->{$id2}
Truth table100%3/3
A && B
A= defined $id2B= exists $id_hash->{$id2}
ABresult
0X0
100
111
86 108 ▶ if ( $id1_exists && $id2_exists ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id1_exists and $id2_exists) { } 27 81 108
Condition100%3/3
$id1_exists and $id2_exists
!ll&&!rl&&r
513027
MC/DC100%2/2
$id1_exists and $id2_exists
$id1_exists $id2_exists
Truth table100%3/3
A and B
A= $id1_existsB= $id2_exists
ABresult
0X0
100
111
87 # Keep both leaves
88 27 $repl = $match;
89 } else {
90 # There are from zero to one leaves to keep. Delete one of them.
91 81 ▶ my ($id, $dist) = $id1_exists ? ($id1, $dist1) : ($id2, $dist2);
Branch: 1 branch
BranchTrueFalseTotal
$id1_exists ? : 30 51 81
92 81 ▶ if ( defined($dist) || defined($distp) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dist or defined $distp) 64 17 81
Condition100%3/3
defined $dist or defined $distp
l!l&&r!l&&!r
62217
MC/DC100%2/2
defined $dist or defined $distp
defined $dist defined $distp
Truth table100%3/3
A or B
A= defined $distB= defined $distp
ABresult
1X1
011
000
93 64 ▶ $dist = ':'.(($dist||0) + ($distp||0));
Condition100%4/4
$dist || 0
l!l
5410
$distp || 0
l!l
5113
MC/DC100%1/1
$dist || 0
$dist
MC/DC100%1/1
$distp || 0
$distp
Truth table100%2/2
A
A= $dist
Aresult
11
00
Truth table100%2/2
A
A= $distp
Aresult
11
00
94 }
95 81 ▶ $id ||= '' if not defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 24 57 81
Condition50%1/2
$id ||= ""
l!l
024
MC/DC0%0/1
$id ||= ""
$id
Truth table50%1/2
A
A= $id
Aresult
11
00
96 81 ▶ $dist ||= '' if not defined $dist;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dist 17 64 81
Condition50%1/2
$dist ||= ""
l!l
017
MC/DC0%0/1
$dist ||= ""
$dist
Truth table50%1/2
A
A= $dist
Aresult
11
00
97 81 $repl = $id.$dist;
98 }
99 108 return $repl;
100 }
101  
102  
103 84 ▶sub _parse {
Subroutine _parse: called CC=5 SCAR=16.6
104 84 my $self = shift;
105 84 my $fh = $self->_handle;
106 84 my $forest = $self->_factory->create_forest;
107  
108 84 my $string;
109 84 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 106 84 190
110 106 chomp;
111 106 $string .= $_;
112 }
113  
114 84 my $ids = $self->_args->{'-keep'};
115 84 my $ignore = $self->_args->{'-ignore_comments'};
116 84 my $whitespace = $self->_args->{'-keep_whitespace'};
117 84 my $quotes = $self->_args->{'-ignore_quotes'};
118  
119 # remove comments, split on tree descriptions
120 84 my $counter = 1;
121  
122 84 for my $newick ( $self->_split($string,$ignore,$whitespace,$quotes) ) {
123 104 $self->_logger->debug("going to process newick string " . $counter++);
124 # simplify tree
125 104 ▶ if ($ids) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ids) 1 103 104
126 1 $newick = _simplify($string, $ids);
127 }
128
129 # parse trees
130 104 my $tree = $self->_parse_string($newick);
131  
132 # adding labels to untagged nodes
133 104 ▶ if ( $self->_args->{'-label'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_args->{"-label"}) 0 104 104
134 0 my $i = 1;
135 $tree->visit(
136 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
137 0 my $n = shift;
138 0 ▶ $n->set_name( 'n' . $i++ ) unless $n->get_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $n->get_name 0 0 0
139 }
140 0 );
141 }
142 104 $forest->insert($tree);
143 }
144 84 return $forest;
145 }
146  
147 =begin comment
148  
149 Type : Parser
150 Title : _split($string)
151 Usage : my @strings = $newick->_split($string);
152 Function: Creates an array of (decommented) tree descriptions
153 Returns : A Bio::Phylo::Forest::Tree object.
154 Args : $string = concatenated tree descriptions
155  
156 =end comment
157  
158 =cut
159  
160 84 ▶sub _split {
Subroutine _split: called CC=33 SCAR=35.4
161 84 my ( $self, $string, $ignore, $whitespace, $quotes ) = @_;
162 84 my $log = $self->_logger;
163 84 my ( $QUOTED, $COMMENTED ) = ( 0, 0 );
164 84 my $decommented = '';
165 84 my @trees;
166 84 TOKEN: for my $i ( 0 .. length($string) ) {
167 85842 my $token = substr( $string, $i, 1 );
168  
169 # detect apostrophe as ' between two letters
170 85842 ▶ my $prev = $i > 0 ? substr( $string, $i-1, 1 ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$i > 0 ? : 85758 84 85842
171 85842 ▶ my $next = $i< length($string) ? substr( $string, $i+1, 1 ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$i < length $string ? : 85758 84 85842
172 85842 ▶ my $apostr = substr( $string, $i, 1 ) eq "'" && $prev=~/[a-z]/i && $next=~/[a-z]/i;
Condition50%3/6
substr($string, $i, 1) eq q['] && $prev =~ /[a-z]/i && $next =~ /[a-z]/i
!ll&&!rl&&r
8584200
substr($string, $i, 1) eq "'" and $prev =~ /[a-z]/i
!ll&&!rl&&r
8583660
MC/DC0%0/3
substr($string, $i, 1) eq q['] && $prev =~ /[a-z]/i && $next =~ /[a-z]/i
substr($string, $i, 1) eq "'" $prev =~ /[a-z]/i $next =~ /[a-z]/i
Truth table50%2/4
A and B && C
A= substr($string, $i, 1) eq "'"B= $prev =~ /[a-z]/iC= $next =~ /[a-z]/i
ABCresult
0XX0
10X0
1100
1111
173 85842 ▶ $log->debug("detected apostrophe") if $apostr;
Branch: 1 branch
BranchTrueFalseTotal
if $apostr 0 85842 85842
174  
175 85842 ▶ if ( !$QUOTED && !$COMMENTED && $token eq "'" && ! $quotes && ! $apostr ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $QUOTED || $COMMENTED and $token eq q['] and not $quotes and not $apostr) { } 3 85839 85842
elsif (not $QUOTED || $COMMENTED and $token eq '[' and not $ignore) { } 2 85837 85839
elsif (not $QUOTED and $COMMENTED and $token eq ']' and not $ignore) { } 2 85835 85837
elsif ($QUOTED and not $COMMENTED and $token eq q['] and substr($string, $i, 2) ne q[''] and not $quotes and not $apostr) { } 3 171664 171667
Condition83%10/12
not $QUOTED || $COMMENTED and $token eq "'" and not $quotes and not $apostr
!ll&&!rl&&r
8583903
not $QUOTED || $COMMENTED and $token eq "'" and not $quotes
!ll&&!rl&&r
8583903
not $QUOTED || $COMMENTED and $token eq "'"
!ll&&!rl&&r
133857063
$QUOTED || $COMMENTED
l!l&&r!l&&!r
1132085709
MC/DC60%3/5
not $QUOTED || $COMMENTED and $token eq "'" and not $quotes and not $apostr
$QUOTED $COMMENTED $token eq "'" not $quotes not $apostr
Truth table66%4/6
not(A || B) and C and not(D) and not(E)
A= $QUOTEDB= $COMMENTEDC= $token eq "'"D= not $quotesE= not $apostr
ABCDEresult
1XXXX0
01XXX0
000XX0
0010X0
001100
001111
176 3 $QUOTED++;
177 }
178 ▶ elsif ( !$QUOTED && !$COMMENTED && $token eq "[" && ! $ignore ) {
Condition100%9/9
not $QUOTED || $COMMENTED and $token eq "[" and not $ignore
!ll&&!rl&&r
855702672
not $QUOTED || $COMMENTED and $token eq "["
!ll&&!rl&&r
13385437269
$QUOTED || $COMMENTED
l!l&&r!l&&!r
1132085706
MC/DC100%4/4
not $QUOTED || $COMMENTED and $token eq "[" and not $ignore
$QUOTED $COMMENTED $token eq "[" not $ignore
Truth table100%5/5
not(A || B) and C and not(D)
A= $QUOTEDB= $COMMENTEDC= $token eq "["D= not $ignore
ABCDresult
1XXX0
01XX0
000X0
00100
00111
179 2 $COMMENTED++;
180 2 $log->debug("quote level changed to $COMMENTED");
181 2 next TOKEN;
182 }
183 ▶ elsif ( !$QUOTED && $COMMENTED && $token eq "]" && ! $ignore ) {
Condition88%8/9
not $QUOTED and $COMMENTED and $token eq "]" and not $ignore
!ll&&!rl&&r
8583502
not $QUOTED and $COMMENTED and $token eq "]"
!ll&&!rl&&r
85817182
not $QUOTED and $COMMENTED
!ll&&!rl&&r
1138570420
MC/DC75%3/4
not $QUOTED and $COMMENTED and $token eq "]" and not $ignore
not $QUOTED $COMMENTED $token eq "]" not $ignore
Truth table80%4/5
not(A) and B and C and not(D)
A= not $QUOTEDB= $COMMENTEDC= $token eq "]"D= not $ignore
ABCDresult
0XXX0
10XX0
110X0
11100
11111
184 2 $COMMENTED--;
185 2 next TOKEN;
186 }
187 ▶ elsif ($QUOTED
Condition73%11/15
$QUOTED and not $COMMENTED and $token eq "'" and substr($string, $i, 2) ne "''" and not $quotes and not $apostr
!ll&&!rl&&r
8583203
$QUOTED and not $COMMENTED and $token eq "'" and substr($string, $i, 2) ne "''" and not $quotes
!ll&&!rl&&r
8583203
$QUOTED and not $COMMENTED and $token eq "'" and substr($string, $i, 2) ne "''"
!ll&&!rl&&r
8583203
$QUOTED and not $COMMENTED and $token eq "'"
!ll&&!rl&&r
857221103
$QUOTED and not $COMMENTED
!ll&&!rl&&r
857220113
MC/DC33%2/6
$QUOTED and not $COMMENTED and $token eq "'" and substr($string, $i, 2) ne "''" and not $quotes and not $apostr
$QUOTED not $COMMENTED $token eq "'" substr($string, $i, 2) ne "''" not $quotes not $apostr
Truth table42%3/7
A and not(B) and C and D and not(E) and not(F)
A= $QUOTEDB= not $COMMENTEDC= $token eq "'"D= substr($string, $i, 2) ne "''"E= not $quotesF= not $apostr
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
188 && !$COMMENTED
189 && $token eq "'"
190 && substr( $string, $i, 2 ) ne "''" && ! $quotes && ! $apostr )
191 {
192 3 $QUOTED--;
193 }
194 85838 ▶ if ( !$QUOTED && $token eq ' ' && ! $whitespace ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $QUOTED and $token eq " " and not $whitespace) 18 85820 85838
Condition83%5/6
not $QUOTED and $token eq " " and not $whitespace
!ll&&!rl&&r
85820018
not $QUOTED and $token eq " "
!ll&&!rl&&r
1138570718
MC/DC66%2/3
not $QUOTED and $token eq " " and not $whitespace
not $QUOTED $token eq " " not $whitespace
Truth table75%3/4
not(A) and B and not(C)
A= not $QUOTEDB= $token eq " "C= not $whitespace
ABCresult
0XX0
10X0
1100
1111
195 18 next TOKEN;
196 }
197 85820 ▶ $decommented .= $token unless $COMMENTED;
Branch: 1 branch
BranchTrueFalseTotal
unless $COMMENTED 85804 16 85820
198 85820 ▶ if ( !$QUOTED && !$COMMENTED && substr( $string, $i, 1 ) eq ';' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $QUOTED || $COMMENTED and substr($string, $i, 1) eq ";") 104 85716 85820
Condition100%6/6
not $QUOTED || $COMMENTED and substr($string, $i, 1) eq ";"
!ll&&!rl&&r
12985587104
$QUOTED || $COMMENTED
l!l&&r!l&&!r
1131685691
MC/DC100%3/3
not $QUOTED || $COMMENTED and substr($string, $i, 1) eq ";"
$QUOTED $COMMENTED substr($string, $i, 1) eq ";"
Truth table100%4/4
not(A || B) and C
A= $QUOTEDB= $COMMENTEDC= substr($string, $i, 1) eq ";"
ABCresult
1XX0
01X0
0000
0011
199 104 push @trees, $decommented;
200 104 $decommented = '';
201 }
202  
203 }
204 84 $log->debug("removed comments, split on tree descriptions");
205 84 $log->debug("found ".scalar(@trees)." tree descriptions");
206 84 return @trees;
207 }
208  
209 =begin comment
210  
211 Type : Parser
212 Title : _parse_string($string)
213 Usage : my $tree = $newick->_parse_string($string);
214 Function: Creates a populated Bio::Phylo::Forest::Tree object from a newick
215 string.
216 Returns : A Bio::Phylo::Forest::Tree object.
217 Args : $string = a newick tree description
218  
219 =end comment
220  
221 =cut
222  
223 104 ▶sub _parse_string {
Subroutine _parse_string: called CC=6 SCAR=18.1
224 104 my ( $self, $string ) = @_;
225 104 my $fac = $self->_factory;
226 104 $self->_logger->debug("going to parse tree string '$string'");
227 104 my $tree = $fac->create_tree;
228 104 my $remainder = $string;
229 104 my $token;
230 my @tokens;
231 104 ▶ while ( ( $token, $remainder ) = $self->_next_token($remainder) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($token, $remainder) = $self->_next_token($remainder)) 18685 0 18685
232 18685 ▶ last if ( !defined $token || !defined $remainder );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token and defined $remainder 104 18581 18685
Condition66%2/3
defined $token and defined $remainder
!ll&&!rl&&r
010418581
MC/DC50%1/2
defined $token and defined $remainder
defined $token defined $remainder
Truth table66%2/3
A and B
A= defined $tokenB= defined $remainder
ABresult
0X0
100
111
233 18581 $self->_logger->debug("fetched token '$token'");
234  
235 18581 push @tokens, $token;
236 }
237 104 my $i;
238 104 ▶ for ( $i = $#tokens ; $i >= 0 ; $i-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 104 0 104
239 104 ▶ last if $tokens[$i] eq ';';
Branch: 1 branch
BranchTrueFalseTotal
if $tokens[$i] eq ";" 104 0 104
240 }
241 104 my $root = $fac->create_node;
242 104 $tree->insert($root);
243 104 $self->_parse_node_data( $root, @tokens[ 0 .. ( $i - 1 ) ] );
244 104 $self->_parse_clade( $tree, $root, @tokens[ 0 .. ( $i - 1 ) ] );
245 104 return $tree;
246 }
247  
248 4478 ▶sub _parse_clade {
Subroutine _parse_clade: called CC=8 SCAR=20.8
249 4478 my ( $self, $tree, $root, @tokens ) = @_;
250 4478 my $fac = $self->_factory;
251 4478 $self->_logger->debug("recursively parsing clade '@tokens'");
252 4478 my ( @clade, $depth, @remainder );
253 4478 TOKEN: for my $i ( 0 .. $#tokens ) {
254 289762 ▶ if ( $tokens[$i] eq '(' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($tokens[$i] eq '(') { } 31799 257963 289762
elsif ($tokens[$i] eq ',' and $depth == 1) { } 2250 255713 257963
elsif ($tokens[$i] eq ')') { } 31799 223914 255713
255 31799 ▶ if ( not defined $depth ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $depth) { } 2124 29675 31799
256 2124 $depth = 1;
257 2124 next TOKEN;
258 }
259 else {
260 29675 $depth++;
261 }
262 }
263 ▶ elsif ( $tokens[$i] eq ',' && $depth == 1 ) {
Condition100%3/3
$tokens[$i] eq "," and $depth == 1
!ll&&!rl&&r
225864298492250
MC/DC100%2/2
$tokens[$i] eq "," and $depth == 1
$tokens[$i] eq "," $depth == 1
Truth table100%3/3
A and B
A= $tokens[$i] eq ","B= $depth == 1
ABresult
0X0
100
111
264 2250 my $node = $fac->create_node;
265 2250 $root->set_child($node);
266 2250 $tree->insert($node);
267 2250 $self->_parse_node_data( $node, @clade );
268 2250 $self->_parse_clade( $tree, $node, @clade );
269 2250 @clade = ();
270 2250 next TOKEN;
271 }
272 elsif ( $tokens[$i] eq ')' ) {
273 31799 $depth--;
274 31799 ▶ if ( $depth == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == 0) 2124 29675 31799
275 2124 @remainder = @tokens[ ( $i + 1 ) .. $#tokens ];
276 2124 my $node = $fac->create_node;
277 2124 $root->set_child($node);
278 2124 $tree->insert($node);
279 2124 $self->_parse_node_data( $node, @clade );
280 2124 $self->_parse_clade( $tree, $node, @clade );
281 2124 last TOKEN;
282 }
283 }
284 283264 push @clade, $tokens[$i];
285 }
286 }
287  
288 4263 ▶sub _parse_node_data {
Subroutine _parse_node_data: called CC=10 SCAR=23.3
289 4263 my ( $self, $node, @clade ) = @_;
290 4263 $self->_logger->debug("parsing name and branch length for node");
291 4263 my @tail;
292 4263 ▶ PARSE_TAIL: for ( my $i = $#clade ; $i >= 0 ; $i-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 13351 2246 15597
293 13351 ▶ if ( $clade[$i] eq ')' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($clade[$i] eq ')') { } 2017 11334 13351
elsif ($i == 0) { } 2246 9088 11334
294 2017 @tail = @clade[ ( $i + 1 ) .. $#clade ];
295 2017 last PARSE_TAIL;
296 }
297 elsif ( $i == 0 ) {
298 2246 @tail = @clade;
299 }
300 }
301
302 4263 ▶ if ( defined($tail[-1]) and $tail[-1] =~ /(\[.+\])$/ and scalar @tail != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tail[-1] and $tail[-1] =~ /(\[.+\])$/ and scalar @tail != 1) 0 4263 4263
Condition83%5/6
defined $tail[-1] and $tail[-1] =~ /(\[.+\])$/ and scalar @tail != 1
!ll&&!rl&&r
426120
defined $tail[-1] and $tail[-1] =~ /(\[.+\])$/
!ll&&!rl&&r
19240692
MC/DC0%0/3
defined $tail[-1] and $tail[-1] =~ /(\[.+\])$/ and scalar @tail != 1
defined $tail[-1] $tail[-1] =~ /(\[.+\])$/ scalar @tail != 1
Truth table75%3/4
A and B and C
A= defined $tail[-1]B= $tail[-1] =~ /(\[.+\])$/C= scalar @tail != 1
ABCresult
0XX0
10X0
1100
1111
303 0 my $anno = $1;
304 0 $self->_logger->info("discarding branch comment $anno");
305 0 $tail[-1] =~ s/\Q$anno\E//;
306 }
307  
308 # name only
309 4263 ▶ if ( scalar @tail == 1 ) {
Branch: 3 branches
BranchTrueFalseTotal
if (scalar @tail == 1) { } 317 3946 4263
elsif (scalar @tail == 2) { } 245 3701 3946
elsif (scalar @tail == 3) { } 3509 192 3701
310 317 $node->set_name( $tail[0] );
311 }
312 elsif ( scalar @tail == 2 ) {
313 245 $node->set_branch_length( $tail[-1] );
314 }
315 elsif ( scalar @tail == 3 ) {
316 3509 $node->set_name( $tail[0] );
317 3509 $node->set_branch_length( $tail[-1] );
318 }
319 }
320  
321 18685 ▶sub _next_token {
Subroutine _next_token: called CC=24 SCAR=32.0
322 18685 my ( $self, $string ) = @_;
323 18685 $self->_logger->debug("tokenizing string '$string'");
324 18685 my $ignore = $self->_args->{'-ignore_comments'};
325 18685 my $QUOTED = 0;
326 18685 my $COMMENTED = 0;
327 18685 my $token = '';
328 18685 my $TOKEN_DELIMITER = qr/[():,;]/;
329 18685 TOKEN: for my $i ( 0 .. length($string) ) {
330 93777 $token .= substr( $string, $i, 1 );
331 93777 $self->_logger->debug("growing token: '$token'");
332
333 # detect apostrophe as ' between two letters
334 93777 ▶ my $prev = $i > 0 ? substr( $string, $i-1, 1 ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$i > 0 ? : 75092 18685 93777
335 93777 ▶ my $next = $i< length($string) ? substr( $string, $i+1, 1 ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$i < length $string ? : 93673 104 93777
336 93777 ▶ my $apostr = substr( $string, $i, 1 ) eq "'" && $prev=~/[a-z]/i && $next=~/[a-z]/i;
Condition50%3/6
substr($string, $i, 1) eq q['] && $prev =~ /[a-z]/i && $next =~ /[a-z]/i
!ll&&!rl&&r
9377700
substr($string, $i, 1) eq "'" and $prev =~ /[a-z]/i
!ll&&!rl&&r
9377160
MC/DC0%0/3
substr($string, $i, 1) eq q['] && $prev =~ /[a-z]/i && $next =~ /[a-z]/i
substr($string, $i, 1) eq "'" $prev =~ /[a-z]/i $next =~ /[a-z]/i
Truth table50%2/4
A and B && C
A= substr($string, $i, 1) eq "'"B= $prev =~ /[a-z]/iC= $next =~ /[a-z]/i
ABCresult
0XX0
10X0
1100
1111
337 93777 ▶ $self->_logger->debug("detected apostrophe") if $apostr;
Branch: 1 branch
BranchTrueFalseTotal
if $apostr 0 93777 93777
338  
339 # if -ignore_comments was specified the string can still contain comments
340 # that can contain token delimiters, so we still need to track
341 # whether we are inside a comment
342 93777 ▶ if ( $ignore && $token =~ /\[$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ignore and $token =~ /\[$/) 267 93510 93777
Condition100%3/3
$ignore and $token =~ /\[$/
!ll&&!rl&&r
6324830262267
MC/DC100%2/2
$ignore and $token =~ /\[$/
$ignore $token =~ /\[$/
Truth table100%3/3
A and B
A= $ignoreB= $token =~ /\[$/
ABresult
0X0
100
111
343 267 $COMMENTED++;
344 }
345 93777 ▶ if ( $ignore && $token =~ /\]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ignore and $token =~ /\]$/) 267 93510 93777
Condition100%3/3
$ignore and $token =~ /\]$/
!ll&&!rl&&r
6324830262267
MC/DC100%2/2
$ignore and $token =~ /\]$/
$ignore $token =~ /\]$/
Truth table100%3/3
A and B
A= $ignoreB= $token =~ /\]$/
ABresult
0X0
100
111
346 267 $COMMENTED--;
347 267 next TOKEN;
348 }
349 93510 ▶ if ( !$QUOTED && !$COMMENTED && $token =~ $TOKEN_DELIMITER ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $QUOTED || $COMMENTED and $token =~ /$TOKEN_DELIMITER/) 18581 74929 93510
Condition100%6/6
not $QUOTED || $COMMENTED and $token =~ /$TOKEN_DELIMITER/
!ll&&!rl&&r
233275160218581
$QUOTED || $COMMENTED
l!l&&r!l&&!r
1132321470183
MC/DC100%3/3
not $QUOTED || $COMMENTED and $token =~ /$TOKEN_DELIMITER/
$QUOTED $COMMENTED $token =~ /$TOKEN_DELIMITER/
Truth table100%4/4
not(A || B) and C
A= $QUOTEDB= $COMMENTEDC= $token =~ /$TOKEN_DELIMITER/
ABCresult
1XX0
01X0
0000
0011
350 18581 my $length = length($token);
351 18581 ▶ if ( $length == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length == 1) { } 10571 8010 18581
352 10571 $self->_logger->debug("single char token: '$token'");
353 10571 return $token, substr( $string, ( $i + 1 ) );
354 }
355 else {
356 8010 $self->_logger->debug(
357 sprintf( "range token: %s",
358 substr( $token, 0, $length - 1 ) )
359 );
360 8010 return substr( $token, 0, $length - 1 ),
361 substr( $token, $length - 1, 1 )
362 . substr( $string, ( $i + 1 ) );
363 }
364 }
365 74929 ▶ if ( !$QUOTED && !$COMMENTED && substr( $string, $i, 1 ) eq "'" && ! $apostr ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $QUOTED || $COMMENTED and substr($string, $i, 1) eq q['] and not $apostr) { } 3 74926 74929
elsif ($QUOTED and not $COMMENTED and substr($string, $i, 1) eq q['] and substr($string, $i, 2) ne q[''] and not $apostr) { } 3 149846 149849
Condition88%8/9
not $QUOTED || $COMMENTED and substr($string, $i, 1) eq "'" and not $apostr
!ll&&!rl&&r
7492603
not $QUOTED || $COMMENTED and substr($string, $i, 1) eq "'"
!ll&&!rl&&r
23327515993
$QUOTED || $COMMENTED
l!l&&r!l&&!r
1132321451602
MC/DC75%3/4
not $QUOTED || $COMMENTED and substr($string, $i, 1) eq "'" and not $apostr
$QUOTED $COMMENTED substr($string, $i, 1) eq "'" not $apostr
Truth table80%4/5
not(A || B) and C and not(D)
A= $QUOTEDB= $COMMENTEDC= substr($string, $i, 1) eq "'"D= not $apostr
ABCDresult
1XXX0
01XX0
000X0
00100
00111
366 3 $QUOTED++;
367 }
368 ▶ elsif ($QUOTED && !$COMMENTED
Condition75%9/12
$QUOTED and not $COMMENTED and substr($string, $i, 1) eq "'" and substr($string, $i, 2) ne "''" and not $apostr
!ll&&!rl&&r
7492303
$QUOTED and not $COMMENTED and substr($string, $i, 1) eq "'" and substr($string, $i, 2) ne "''"
!ll&&!rl&&r
7492303
$QUOTED and not $COMMENTED and substr($string, $i, 1) eq "'"
!ll&&!rl&&r
748131103
$QUOTED and not $COMMENTED
!ll&&!rl&&r
748130113
MC/DC40%2/5
$QUOTED and not $COMMENTED and substr($string, $i, 1) eq "'" and substr($string, $i, 2) ne "''" and not $apostr
$QUOTED not $COMMENTED substr($string, $i, 1) eq "'" substr($string, $i, 2) ne "''" not $apostr
Truth table50%3/6
A and not(B) and C and D and not(E)
A= $QUOTEDB= not $COMMENTEDC= substr($string, $i, 1) eq "'"D= substr($string, $i, 2) ne "''"E= not $apostr
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
369 && substr( $string, $i, 1 ) eq "'"
370 && substr( $string, $i, 2 ) ne "''" && ! $apostr)
371 {
372 3 $QUOTED--;
373 }
374 }
375 }
376  
377 # podinherit_insert_token
378  
379 =head1 SEE ALSO
380  
381 There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
382 for any user or developer questions and discussions.
383  
384 =over
385  
386 =item L<Bio::Phylo::IO>
387  
388 The newick parser is called by the L<Bio::Phylo::IO> object.
389 Look there to learn how to parse newick strings.
390  
391 =item L<Bio::Phylo::Manual>
392  
393 Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>.
394  
395 =back
396  
397 =head1 CITATION
398  
399 If you use Bio::Phylo in published research, please cite it:
400  
401 B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
402 and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
403 I<BMC Bioinformatics> B<12>:63.
404 L<http://dx.doi.org/10.1186/1471-2105-12-63>
405  
406 =cut
407  
408 24 1;
« Phylo/Matrices/Datatype/Mixed.pm ↑ Summary Phylo/Treedrawer/Pdf.pm »