Bio/TreeIO/NewickParser.pm

filter: Statementstmt 95.1% 117 / 123 Branchbran 82.2% 51 / 62 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 2 totaltotal 89.7% 183 / 204 CC 36 SCAR 31.1
CC
36
Cov
91%
parse_newick
30.2
next_token
8.0
BEGIN
1.0
CRAP 22.5
« Bio/SeqIO/kegg.pm ↑ Summary Bio/Align/ProteinStatistics.pm »
1 # POD documentation - main docs before the code
2  
3 =head1 NAME
4  
5 Bio::TreeIO::NewickParser - newick string parser
6  
7 =head1 SYNOPSIS
8  
9 # From newick.pm
10 use base qw(Bio::TreeIO Bio::TreeIO::NewickParser);
11  
12 # in the next_tree method...
13 $self->parse_newick($_);
14  
15 =head1 DESCRIPTION
16  
17 This module correctly parses the Newick and NHX formats, sending calls
18 to the BioPerl TreeEventHandler when appropriate in order to build and
19 populate the node objects.
20  
21 Module which implements a newick string parser as a finite state
22 machine which enables it to parse the full Newick specification.
23  
24 Taken largely from the Ensembl Compara file with the same name
25 (Bio::EnsEMBL::Compara::Graph::NewickParser), this module adapts the
26 parser to work with BioPerl's event handler-based parsing scheme.
27  
28 This module is used by nhx.pm and newick.pm, and is NOT called
29 directly. Instead, both of those parsing modules extend this module in
30 order to gain access to the main parsing method.
31  
32 =head1 FEEDBACK
33  
34 =head2 Mailing Lists
35  
36 User feedback is an integral part of the evolution of this and other
37 Bioperl modules. Send your comments and suggestions preferably to the
38 Bioperl mailing list. Your participation is much appreciated.
39  
40 bioperl-l@bioperl.org - General discussion
41 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
42  
43 =head2 Support
44  
45 Please direct usage questions or support issues to the mailing list:
46  
47 I<bioperl-l@bioperl.org>
48  
49 rather than to the module maintainer directly. Many experienced and
50 reponsive experts will be able look at the problem and quickly
51 address it. Please include a thorough description of the problem
52 with code and data examples if at all possible.
53  
54 =head2 Reporting Bugs
55  
56 Report bugs to the Bioperl bug tracking system to help us keep track
57 of the bugs and their resolution. Bug reports can be submitted via the
58 web:
59  
60 https://github.com/bioperl/bioperl-live/issues
61  
62 =head1 AUTHOR - Jessica Severin (EnsEMBL implementation), Greg Jordan (BioPerl adaptation)
63  
64 =cut
65  
66 package Bio::TreeIO::NewickParser;
67 15 $Bio::TreeIO::NewickParser::VERSION = '1.7.8';
68 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 15 ▶use base qw(Bio::Root::Root);
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 118 ▶sub parse_newick {
Subroutine parse_newick: called CC=29 SCAR=34.1
Pod undocumented
72 118 my $self = shift;
73 118 my $newick = shift;
74  
75 118 ▶ $newick = $newick . ";" unless ($newick =~ m/;/);
Branch: 1 branch
BranchTrueFalseTotal
unless $newick =~ /;/ 3 115 118
76  
77 118 my $count=1;
78 118 my $debug = $self->verbose;
79 118 my $token = next_token(\$newick, "(;");
80 118 my $state=1;
81 118 my $bracket_level = 0;
82  
83 118 $self->_start('tree');
84  
85 118 my $leaf_flag = 0;
86  
87 118 ▶ while(defined($token)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $token) 22615 118 22733
88 # backwards-compat. with 5.8.1, no Switch (but we hate if-elsif-ad-infinitum
89 22615 ▶ if ($state == 1) { #new node
Branch: 6 branches
BranchTrueFalseTotal
if ($state == 1) { } 4523 18092 22615
elsif ($state == 2) { } 4523 13569 18092
elsif ($state == 3) { } 4523 9046 13569
elsif ($state == 4) { } 4523 4523 9046
elsif ($state == 5) { } 4523 0 4523
elsif ($state == 13) { } 0 0 0
90  
91 4523 $self->_start('node');
92  
93 4523 $self->debug(" -> [$token]\n");
94 4523 ▶ if($token eq '(') { #create new set
Branch: 1 branch
BranchTrueFalseTotal
if ($token eq '(') { } 2151 2372 4523
95 2151 ▶ $self->debug(" create set\n") if($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 2151 2151
96 2151 $token = next_token(\$newick, "[(:,)");
97 2151 $state = 1;
98 2151 $bracket_level++;
99 } else {
100 2372 $state = 2;
101 2372 $leaf_flag = 1;
102 }
103 } elsif ($state == 2) { #naming a node
104 4523 ▶ if(!($token =~ /[\[\:\,\)\;]/)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($token =~ /[\[\:\,\)\;]/) 2440 2083 4523
105  
106 2440 ▶ if (!$leaf_flag && $self->param('internal_node_id') eq 'bootstrap') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $leaf_flag and $self->param("internal_node_id") eq "bootstrap") 4 2436 2440
Condition100%3/3
not $leaf_flag and $self->param("internal_node_id") eq "bootstrap"
!ll&&!rl&&r
2351854
MC/DC100%2/2
not $leaf_flag and $self->param("internal_node_id") eq "bootstrap"
not $leaf_flag $self->param("internal_node_id") eq "bootstrap"
Truth table100%3/3
not(A) and B
A= not $leaf_flagB= $self->param("internal_node_id") eq "bootstrap"
ABresult
0X0
100
111
107 4 $self->_start('bootstrap');
108 4 $self->_chars($token);
109 4 $self->_end('bootstrap');
110 4 $token = '';
111 }
112  
113 2440 $self->_start('id');
114 2440 $self->_chars($token);
115 2440 $self->_end('id');
116  
117 2440 ▶ $self->debug(" naming leaf\n") if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 2440 2440
118 2440 $token = next_token(\$newick, "[:,);");
119 }
120 4523 $state = 3;
121 } elsif ($state == 3) { # optional : and distance
122 4523 ▶ if($token eq ':') {
Branch: 2 branches
BranchTrueFalseTotal
if ($token eq ':') { } 4131 392 4523
elsif ($token eq '[') { } 15 377 392
123 4131 $token = next_token(\$newick, "[,);");
124  
125 4131 $self->_start('branch_length');
126 4131 $self->_chars($token);
127 4131 $self->_end('branch_length');
128  
129 4131 $token = next_token(\$newick, ",);"); #move to , or )
130 } elsif ($token eq '[') { # NHX tag without previous blength
131 15 $token .= next_token(\$newick, ",);");
132 }
133 4523 $state = 4;
134 } elsif ($state == 4) { # optional NHX tags
135 4523 ▶ if($token =~ /\[\&\&NHX/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token =~ /\[\&\&NHX/) { } 478 4045 4523
elsif ($token =~ /\[/) { } 4 4041 4045
136 # careful: this regexp gets rid of all NHX wrapping in one step
137  
138 478 $self->_start('nhx_tag');
139 478 $token =~ /\[\&\&NHX\:(\S+)\]/;
140 478 ▶ if ($1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1) 478 0 478
141 # NHX may be empty, presumably at end of file, just before ";"
142 478 my @attributes = split ':', $1;
143 478 foreach my $attribute (@attributes) {
144 778 $attribute =~ s/\s+//;
145 778 my($key,$value) = split '=', $attribute;
146  
147 778 $self->_start('tag_name');
148 778 $self->_chars($key);
149 778 $self->_end('tag_name');
150  
151 778 $self->_start('tag_value');
152 778 $self->_chars($value);
153 778 $self->_end('tag_value');
154 }
155 }
156 478 $self->_end('nhx_tag');
157  
158 478 $token = next_token(\$newick, ",);"); #move to , or )
159 } elsif ($token =~ /\[/) {
160 # This is a hack to make AMPHORA2 work
161 4 ▶ if ($token =~ /\[(\S+)\]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /\[(\S+)\]/) 4 0 4
162 4 $self->_start('bootstrap');
163 4 $self->_chars($1);
164 4 $self->_end('bootstrap');
165 }
166 4 $token = next_token(\$newick, ",);"); }
167 4523 $state = 5;
168 } elsif ($state == 5) { # end node
169 4523 ▶ if($token eq ')') {
Branch: 3 branches
BranchTrueFalseTotal
if ($token eq ')') { } 2151 2372 4523
elsif ($token eq ',') { } 2254 118 2372
elsif ($token eq ';') { } 118 0 118
170  
171 2151 $self->_end('node');
172  
173 2151 $token = next_token(\$newick, "[:,);");
174 2151 ▶ if (defined $token && $token eq '[') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $token and $token eq '[') { } 8 2143 2151
Condition66%2/3
defined $token and $token eq "["
!ll&&!rl&&r
021438
MC/DC50%1/2
defined $token and $token eq "["
defined $token $token eq "["
Truth table66%2/3
A and B
A= defined $tokenB= $token eq "["
ABresult
0X0
100
111
175 # It is possible to have anonymous internal nodes w/ no name
176 # and no blength but with NHX tags
177 #
178 # We use leaf_flag=0 to let the parser know that it's labeling an internal
179 # node. This affects how potential bootstrap values are handled in state 2.
180 8 $leaf_flag = 0;
181 8 $state = 2;
182 } else {
183 2143 $leaf_flag = 0;
184 2143 $state = 2;
185 }
186 2151 $bracket_level--;
187 } elsif($token eq ',') {
188  
189 2254 $self->_end('node');
190  
191 2254 $token = next_token(\$newick, "[(:,)"); #can be un_blengthed nhx nodes
192 2254 $state=1;
193 } elsif($token eq ';') {
194 #done with tree
195 118 ▶ $self->throw("parse error: unbalanced ()\n") if($bracket_level ne 0);
Branch: 1 branch
BranchTrueFalseTotal
if $bracket_level ne 0 0 118 118
196  
197 118 $self->_end('node');
198 118 $self->_end('tree');
199  
200 118 $token = next_token(\$newick, "(");
201 118 $state=13;
202 } else {
203 0 $self->debug("[$token]]\n");
204 0 $self->throw("parse error: expected ; or ) or ,\n");
205 }
206 } elsif ($state == 13) {
207 0 $self->throw("parse error: nothing expected after ;");
208 }
209 }
210  
211 118 ▶ if ($self->_eventHandler->within_element('tree')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_eventHandler->within_element("tree")) 0 118 118
212 0 $self->_end('node');
213 0 $self->_end('tree');
214 }
215 }
216  
217 8135 ▶sub _chars {
Subroutine _chars: called CC=1 SCAR=0.0
218 8135 my $self = shift;
219 8135 my $chars = shift;
220  
221 8135 $self->_eventHandler->characters($chars);
222 }
223  
224 13254 ▶sub _start {
Subroutine _start: called CC=1 SCAR=0.0
225 13254 my $self = shift;
226 13254 my $name = shift;
227  
228 13254 $self->_eventHandler->start_element({Name=>$name});
229 }
230  
231 13254 ▶sub _end {
Subroutine _end: called CC=1 SCAR=0.0
232 13254 my $self = shift;
233 13254 my $name = shift;
234  
235 13254 $self->_eventHandler->end_element({Name=>$name});
236 }
237  
238 17991 ▶sub next_token {
Subroutine next_token: called CC=8 SCAR=20.8
Pod undocumented
239 17991 my $string = shift;
240 17991 my $delim = shift;
241
242 17991 $$string =~ s/^(\s)+//;
243  
244 17991 ▶ return undef unless(length($$string));
Branch: 1 branch
BranchTrueFalseTotal
unless length $$string 118 17873 17991
245
246 #print("input =>$$string\n");
247 #print("delim =>$delim\n");
248 17873 my $index=undef;
249  
250 17873 my @delims = split(/ */, $delim);
251 17873 foreach my $dl (@delims) {
252 75624 my $pos = index($$string, $dl);
253 75624 ▶ if($pos>=0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos >= 0) 62101 13523 75624
254 62101 ▶ $index = $pos unless(defined($index));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 17873 44228 62101
255 62101 ▶ $index = $pos if($pos<$index);
Branch: 1 branch
BranchTrueFalseTotal
if $pos < $index 6715 55386 62101
256 }
257 }
258 17873 ▶ unless(defined($index)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $index) 0 17873 17873
259 # have to call as class here (this is not an instance method)
260 0 Bio::Root::Root->throw("couldn't find delimiter $delim\n $$string");
261 }
262  
263 17873 my $token ='';
264  
265 17873 ▶ if($index==0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index == 0) { } 10820 7053 17873
266 10820 $token = substr($$string,0,1);
267 10820 $$string = substr($$string, 1);
268 } else {
269 7053 $token = substr($$string, 0, $index);
270 7053 $$string = substr($$string, $index);
271 }
272  
273 #print(" token =>$token\n");
274 #print(" outstring =>$$string\n\n");
275
276 17873 return $token;
277 }
278  
279  
280  
281 15 1;
« Bio/SeqIO/kegg.pm ↑ Summary Bio/Align/ProteinStatistics.pm »