Bio/TreeIO.pm

filter: Statementstmt 74.7% 77 / 103 Branchbran 46.8% 15 / 32 Conditioncond 38.4% 5 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 72.7% 16 / 22 Podpod 36.3% 4 / 11 totaltotal 62.2% 117 / 188 CC 23 SCAR 19.3
CC
23
Cov
66%
READLINE
20.0
_guess_format
16.5
new
7.7
CRAP 6.9
« Bio/Tools/Genewise.pm ↑ Summary Bio/SeqFeature/Computation.pm »
1 #
2 # BioPerl module for Bio::TreeIO
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Jason Stajich <jason@bioperl.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 - Parser for Tree files
17  
18 =head1 SYNOPSIS
19  
20 {
21 use Bio::TreeIO;
22 my $treeio = Bio::TreeIO->new(-format => 'newick',
23 -file => 'globin.dnd');
24 while( my $tree = $treeio->next_tree ) {
25 print "Tree is ", $tree->number_nodes, "\n";
26 }
27 }
28  
29 =head1 DESCRIPTION
30  
31 This is the driver module for Tree reading from data streams and
32 flatfiles. This is intended to be able to create Bio::Tree::TreeI
33 objects.
34  
35 =head1 FEEDBACK
36  
37 =head2 Mailing Lists
38  
39 User feedback is an integral part of the evolution of this and other
40 Bioperl modules. Send your comments and suggestions preferably to
41 the Bioperl mailing list. Your participation is much appreciated.
42  
43 bioperl-l@bioperl.org - General discussion
44 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
45  
46 =head2 Support
47  
48 Please direct usage questions or support issues to the mailing list:
49  
50 I<bioperl-l@bioperl.org>
51  
52 rather than to the module maintainer directly. Many experienced and
53 reponsive experts will be able look at the problem and quickly
54 address it. Please include a thorough description of the problem
55 with code and data examples if at all possible.
56  
57 =head2 Reporting Bugs
58  
59 Report bugs to the Bioperl bug tracking system to help us keep track
60 of the bugs and their resolution. Bug reports can be submitted via the
61 web:
62  
63 https://github.com/bioperl/bioperl-live/issues
64  
65 =head1 AUTHOR - Jason Stajich
66  
67 Email jason-at-bioperl-dot-org
68  
69 =head1 CONTRIBUTORS
70  
71 Allen Day E<lt>allenday@ucla.eduE<gt>
72  
73 =head1 APPENDIX
74  
75 The rest of the documentation details each of the object methods.
76 Internal methods are usually preceded with a _
77  
78 =cut
79  
80  
81 # Let the code begin...
82  
83  
84 package Bio::TreeIO;
85 27 $Bio::TreeIO::VERSION = '1.7.8';
86 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 # Object preamble - inherits from Bio::Root::Root
89  
90 27 ▶use Bio::TreeIO::TreeEventBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 27 ▶use base qw(Bio::Root::Root Bio::Root::IO Bio::Event::EventGeneratorI Bio::Factory::TreeFactoryI);
Subroutine BEGIN: called CC=1 SCAR=0.0
93  
94 =head2 new
95  
96 Title : new
97 Usage : my $obj = Bio::TreeIO->new();
98 Function: Builds a new Bio::TreeIO object
99 Returns : Bio::TreeIO
100 Args : a hash. useful keys:
101 -format : Specify the format of the file. Supported formats:
102  
103 newick Newick tree format
104 nexus Nexus tree format
105 nhx NHX tree format
106 svggraph SVG graphical representation of tree
107 tabtree ASCII text representation of tree
108 lintree lintree output format
109  
110 =cut
111  
112 328 ▶sub new {
Subroutine new: called CC=7 SCAR=20.4
Pod documented
113 328 my($caller,@args) = @_;
114 328 ▶ my $class = ref($caller) || $caller;
Condition33%1/3
ref $caller || $caller
l!l&&r!l&&!r
03280
MC/DC0%0/2
ref $caller || $caller
ref $caller $caller
Truth table33%1/3
A || B
A= ref $callerB= $caller
ABresult
1X1
011
000
115  
116 # or do we want to call SUPER on an object if $caller is an
117 # object?n
118  
119 328 my $obj;
120 328 ▶ if( $class =~ /Bio::TreeIO::(\S+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /Bio::TreeIO::(\S+)/) { } 185 143 328
121 185 $obj = $class->SUPER::new(@args);
122 185 $obj->_initialize(@args);
123 } else {
124 143 my %param = @args;
125 143 @param{ map { lc $_ } keys %param } = values %param; # lowercase keys
126 my $format = $param{'-format'} ||
127 143 ▶ $class->_guess_format( $param{'-file'} || $ARGV[0] ) ||
Condition50%4/8
$param{'-format'} || $class->_guess_format($param{'-file'} || $ARGV[0]) || 'newick'
l!l
1430
$param{"-format"} or $class->_guess_format($param{"-file"} || $ARGV[0])
l!l&&r!l&&!r
14120
$param{'-file'} || $ARGV[0]
l!l&&r!l&&!r
200
MC/DC0%0/2
$param{'-format'} || $class->_guess_format($param{'-file'} || $ARGV[0]) || 'newick'
$param{"-format"} $class->_guess_format($param{"-file"} || $ARGV[0])
MC/DC0%0/2
$param{'-file'} || $ARGV[0]
$param{'-file'} $ARGV[0]
Truth table66%2/3
A or B
A= $param{"-format"}B= $class->_guess_format($param{"-file"} || $ARGV[0])
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $param{'-file'}B= $ARGV[0]
ABresult
1X1
011
000
128 'newick';
129 143 $format = "\L$format"; # normalize capitalization to lower case
130
131 # normalize capitalization
132 143 ▶ return undef unless( $class->_load_format_module($format) );
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_load_format_module($format) 0 143 143
133 143 $obj = "Bio::TreeIO::$format"->new(@args);
134 }
135 328 return $obj;
136 }
137  
138  
139 =head2 format
140  
141 Title : format
142 Usage : $format = $obj->format()
143 Function: Get the tree format
144 Returns : tree format
145 Args : none
146  
147 =cut
148  
149 # format() method inherited from Bio::Root::IO
150  
151  
152 =head2 next_tree
153  
154 Title : next_tree
155 Usage : my $tree = $treeio->next_tree;
156 Function: Gets the next tree off the stream
157 Returns : Bio::Tree::TreeI or undef if no more trees
158 Args : none
159  
160 =cut
161  
162 0 ▶sub next_tree{
Subroutine next_tree: not called CC=1 SCAR=6.9
Pod documented
163 0 my ($self) = @_;
164 0 $self->throw("Cannot call method next_tree on Bio::TreeIO object must use a subclass");
165 }
166  
167 =head2 write_tree
168  
169 Title : write_tree
170 Usage : $treeio->write_tree($tree);
171 Function: Writes a tree onto the stream
172 Returns : none
173 Args : Bio::Tree::TreeI
174  
175  
176 =cut
177  
178 0 ▶sub write_tree{
Subroutine write_tree: not called CC=1 SCAR=6.9
Pod documented
179 0 my ($self,$tree) = @_;
180 0 $self->throw("Cannot call method write_tree on Bio::TreeIO object must use a subclass");
181 }
182  
183  
184 =head2 attach_EventHandler
185  
186 Title : attach_EventHandler
187 Usage : $parser->attatch_EventHandler($handler)
188 Function: Adds an event handler to listen for events
189 Returns : none
190 Args : Bio::Event::EventHandlerI
191  
192 =cut
193  
194 185 ▶sub attach_EventHandler{
Subroutine attach_EventHandler: called CC=3 SCAR=11.8
Pod documented
195 185 my ($self,$handler) = @_;
196 185 ▶ return if( ! $handler );
Branch: 1 branch
BranchTrueFalseTotal
unless $handler 0 185 185
197 185 ▶ if( ! $handler->isa('Bio::Event::EventHandlerI') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handler->isa("Bio::Event::EventHandlerI")) 0 185 185
198 0 $self->warn("Ignoring request to attach handler ".ref($handler). ' because it is not a Bio::Event::EventHandlerI');
199 }
200 185 $self->{'_handler'} = $handler;
201 185 return;
202 }
203  
204 =head2 _eventHandler
205  
206 Title : _eventHandler
207 Usage : private
208 Function: Get the EventHandler
209 Returns : Bio::Event::EventHandlerI
210 Args : none
211  
212  
213 =cut
214  
215 34997 ▶sub _eventHandler{
Subroutine _eventHandler: called CC=1 SCAR=0.0
216 34997 my ($self) = @_;
217 34997 return $self->{'_handler'};
218 }
219  
220 185 ▶sub _initialize {
Subroutine _initialize: called CC=1 SCAR=0.0
221 185 my($self, @args) = @_;
222 185 $self->{'_handler'} = undef;
223  
224 185 $self->get_params; # Initialize the default parameters.
225  
226 185 my ($nen,$ini) = $self->_rearrange
227 ([qw(NEWLINE_EACH_NODE INTERNAL_NODE_ID)],@args);
228 185 $self->set_param('newline_each_node',$nen);
229 185 $self->set_param('internal_node_id',$ini);
230  
231 185 $self->attach_EventHandler(Bio::TreeIO::TreeEventBuilder->new
232 (-verbose => $self->verbose(), @args));
233 185 $self->_initialize_io(@args);
234 #$self->debug_params;
235 }
236  
237 =head2 _load_format_module
238  
239 Title : _load_format_module
240 Usage : *INTERNAL TreeIO stuff*
241 Function: Loads up (like use) a module at run time on demand
242 Example :
243 Returns :
244 Args :
245  
246 =cut
247  
248 143 ▶sub _load_format_module {
Subroutine _load_format_module: called CC=2 SCAR=7.1
249 143 my ($self,$format) = @_;
250 143 my $module = "Bio::TreeIO::" . $format;
251 143 my $ok;
252
253 143 eval {
254 143 $ok = $self->_load_module($module);
255 };
256  
257 143 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 143 143
258 0 print STDERR <<END;
259 $self: $format cannot be found
260 Exception $@
261 For more information about the TreeIO system please see the TreeIO docs.
262 This includes ways of checking for formats at compile time, not run time
263 END
264 ;
265 }
266 143 return $ok;
267 }
268  
269 91 ▶sub param {
Subroutine param: called CC=2 SCAR=7.2
Pod undocumented
270 91 my $self = shift;
271 91 my $param = shift;
272 91 my $value = shift;
273  
274 91 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 91 91
275 0 $self->get_params->{$param} = $value;
276 }
277 91 return $self->get_params->{$param};
278 }
279  
280 370 ▶sub set_param {
Subroutine set_param: called CC=2 SCAR=6.9
Pod undocumented
281 370 my $self = shift;
282 370 my $param = shift;
283 370 my $value = shift;
284  
285 #print STDERR "[$param] -> [undef]\n" if (!defined $value);
286 370 ▶ return unless (defined $value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 368 2 370
287 #print STDERR "[$param] -> [$value]\n";
288  
289 2 $self->get_params->{$param} = $value;
290 2 return $self->param($param);
291 }
292  
293 5 ▶sub params {
Subroutine params: called CC=1 SCAR=0.0
Pod undocumented
294 5 my $self = shift;
295 5 return $self->get_params;
296 }
297 414 ▶sub get_params {
Subroutine get_params: called CC=2 SCAR=6.9
Pod undocumented
298 414 my $self = shift;
299  
300 414 ▶ if (!defined $self->{_params}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_params"}) 185 229 414
301 185 $self->{_params} = $self->get_default_params;
302 }
303  
304 414 return $self->{_params};
305 }
306  
307 42 ▶sub set_params {
Subroutine set_params: called CC=1 SCAR=0.0
Pod undocumented
308 42 my $self = shift;
309 42 my $params = shift;
310  
311 # Apply all the passed parameters to our internal parm hashref.
312 42 my $cur_params = $self->get_params;
313 42 $self->{_params} = { %$cur_params, %$params };
314  
315 42 return $self->get_params;
316 }
317  
318 34 ▶sub get_default_params {
Subroutine get_default_params: called CC=1 SCAR=0.0
Pod undocumented
319 34 my $self = shift;
320
321 34 return {};
322 }
323  
324 0 ▶sub debug_params {
Subroutine debug_params: not called CC=2 SCAR=17.9
Pod undocumented
325 0 my $self = shift;
326  
327 0 my $params = $self->get_params;
328  
329 0 print STDERR "{\n";
330 0 foreach my $param (keys %$params) {
331 0 my $value = $params->{$param};
332 0 print STDERR " [$param] -> [$value]\n";
333 }
334 0 print STDERR "}\n";
335 }
336  
337 =head2 _guess_format
338  
339 Title : _guess_format
340 Usage : $obj->_guess_format($filename)
341 Function:
342 Example :
343 Returns : guessed format of filename (lower case)
344 Args :
345  
346 =cut
347  
348 2 ▶sub _guess_format {
Subroutine _guess_format: called CC=7 SCAR=28.0
349 2 my $class = shift;
350 2 ▶ return unless $_ = shift;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ = shift() 0 2 2
351 2 ▶ return 'newick' if /\.(dnd|newick|nh)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if /\.(dnd|newick|nh)$/i 1 1 2
352 1 ▶ return 'nhx' if /\.(nhx)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if /\.(nhx)$/i 1 0 1
353 0 ▶ return 'phyloxml' if /\.(xml)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if /\.(xml)$/i 0 0 0
354 0 ▶ return 'svggraph' if /\.svg$/i;
Branch: 1 branch
BranchTrueFalseTotal
if /\.svg$/i 0 0 0
355 0 ▶ return 'lintree' if( /\.(lin|lintree)$/i );
Branch: 1 branch
BranchTrueFalseTotal
if /\.(lin|lintree)$/i 0 0 0
356 }
357  
358 185 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
359 185 my $self = shift;
360  
361 185 $self->close();
362 }
363  
364 0 ▶sub TIEHANDLE {
Subroutine TIEHANDLE: not called CC=1 SCAR=6.9
365 0 my $class = shift;
366 0 return bless {'treeio' => shift},$class;
367 }
368  
369 0 ▶sub READLINE {
Subroutine READLINE: not called CC=4 SCAR=30.0
370 0 my $self = shift;
371 0 ▶ return $self->{'treeio'}->next_tree() || undef unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
Condition0%0/2
$self->{'treeio'}->next_tree || undef
l!l
00
MC/DC0%0/1
$self->{'treeio'}->next_tree || undef
$self->{'treeio'}->next_tree
Truth table0%0/2
A
A= $self->{'treeio'}->next_tree
Aresult
11
00
372 0 my (@list,$obj);
373 0 ▶ push @list,$obj while $obj = $self->{'treeio'}->next_tree();
Branch: 1 branch
BranchTrueFalseTotal
$obj = $self->{"treeio"}->next_tree and do { push @list, $obj } 0 0 0
374 0 return @list;
375 }
376  
377 0 ▶sub PRINT {
Subroutine PRINT: not called CC=1 SCAR=6.9
378 0 my $self = shift;
379 0 $self->{'treeio'}->write_tree(@_);
380 }
381  
382 27 1;
« Bio/Tools/Genewise.pm ↑ Summary Bio/SeqFeature/Computation.pm »