NexmlIO.pm

filter: Statementstmt 13.7% 24 / 174 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 33.3% 7 / 21 Podpod 100% 12 / 12 totaltotal 16.9% 43 / 254 CC 30 SCAR 42.2
CC
30
Cov
11%
write
210.0
extract_seqs
42.0
new
20.0
CRAP 68.2
« Nexml/Factory.pm ↑ Summary AlignIO/nexml.pm »
1 # $Id: Nexml.pm 15889 2009-07-29 13:35:29Z chmille4 $
2 # BioPerl module for Bio::NexmlIO
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Chase Miller <chmille4@gmail.com>
7 #
8 # Copyright Chase Miller
9 #
10 # You may distribute this module under the same terms as perl itself
11 #
12 # _history
13 # June 16, 2009 Largely rewritten by Chase Miller
14  
15 # POD documentation - main docs before the code
16  
17 =head1 NAME
18  
19 Bio::NexmlIO - stream handler for NeXML documents
20  
21 =head1 SYNOPSIS
22  
23 #Instantiate a Bio::Nexml object and link it to a file
24 my $in_nexml = Bio::Nexml->new(-file => 'nexml_doc.xml', -format => 'Nexml');
25  
26 #Read in some data
27 my $bptree1 = $in_nexml->next_tree();
28 my $bpaln1 = $in_nexml->next_aln();
29 my $bpseq1 = $in_nexml->next_seq();
30  
31 #Use/manipulate data
32 ...
33  
34 #Write data to nexml file
35 my $out_nexml = Bio::Nexml->new(-file => '>new_nexml_doc.xml', -format => 'Nexml');
36 $out_nexml->to_xml();
37
38  
39  
40 =head1 DESCRIPTION
41  
42 Bio::NexmlIO is an I/O handler for a NeXML document. A NeXML document can
43 represent three different data types: simple sequences, alignments,
44 and trees. NexmlIO has four main methods next_tree, next_seq,
45 next_aln, and write. NexmlIO returns bioperl seq, tree, and aln
46 objects which can be manipulated then passed to the write method of a
47 new NexmlIO instance to allow the creation of a NeXML document.
48  
49 Each bioperl object contains all the information necessary to recreate
50 a Bio::Phylo::Taxa object, so each time a bioperl object is converted
51 to a biophylo object, the bioperl object is checked to see if its
52 associated taxa has already been created (against a hash using the
53 NexmlIO_ID and Taxa_ID to create a unique string). If not, it is
54 created; if so, that taxa object is used to link the Bio::Phylo tree
55 or matrix.
56  
57 For more information on the NeXML format, see L<http://www.nexml.org>.
58  
59 =head1 FEEDBACK
60  
61 =head2 Mailing Lists
62  
63 User feedback is an integral part of the evolution of this and other
64 Bioperl modules. Send your comments and suggestions preferably to one
65 of the Bioperl mailing lists.
66  
67 Your participation is much appreciated.
68  
69 bioperl-l@bioperl.org - General discussion
70 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
71  
72 =head2 Support
73  
74 Please direct usage questions or support issues to the mailing list:
75  
76 I<bioperl-l@bioperl.org>
77  
78 rather than to the module maintainer directly. Many experienced and
79 reponsive experts will be able look at the problem and quickly
80 address it. Please include a thorough description of the problem
81 with code and data examples if at all possible.
82  
83 =head2 Reporting Bugs
84  
85 Report bugs to the Bioperl bug tracking system to help us keep track
86 the bugs and their resolution. Bug reports can be submitted via the
87 web:
88  
89 https://github.com/bioperl/bioperl-live/issues
90  
91 =head1 AUTHOR - Chase Miller
92  
93 Email chmille4@gmail.com
94  
95 =head1 CONTRIBUTORS
96  
97 Mark A. Jensen, maj -at- fortinbras -dot- com
98  
99 =head1 APPENDIX
100  
101 The rest of the documentation details each of the object
102 methods. Internal methods are usually preceded with a _
103  
104 =cut
105  
106 # Let the code begin...
107  
108  
109 package Bio::NexmlIO;
110 2 $Bio::NexmlIO::VERSION = '1.7.3';
111 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
112 #TODO Change this
113 2 ▶use lib '..';
Subroutine BEGIN: called CC=1 SCAR=0.0
114  
115 2 ▶use Bio::SeqIO::nexml;
Subroutine BEGIN: called CC=1 SCAR=0.0
116 2 ▶use Bio::AlignIO::nexml;
Subroutine BEGIN: called CC=1 SCAR=0.0
117 2 ▶use Bio::TreeIO::nexml;
Subroutine BEGIN: called CC=1 SCAR=0.0
118 2 ▶use Bio::Nexml::Factory;
Subroutine BEGIN: called CC=1 SCAR=0.0
119  
120 2 ▶use base qw(Bio::Root::IO);
Subroutine BEGIN: called CC=1 SCAR=0.0
121  
122 2 my $nexml_fac = Bio::Nexml::Factory->new();
123  
124 =head1 CONSTRUCTOR
125  
126 =head2 new
127  
128 Title : new
129 Usage : my $in_nexmlIO = Bio::NexmlIO->new(-file => 'data.nexml.xml');
130 Function: Creates a L<Bio::NexmlIO> object linked to a stream
131 Returns : a L<Bio::NexmlIO> object
132 Args : file name
133
134 See L<Bio::Root::IO>
135  
136 =cut
137  
138 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
139 0 my($class,@args) = @_;
140 0 my $self = $class->SUPER::new(@args);
141
142 0 my %params = @args;
143 0 my $file_string = $params{'-file'};
144
145 #create unique ID by creating a scalar and using the memory address
146 0 my $ID = bless \(my $dummy), "UniqueID";
147 0 ($self->{'_ID'}) = sprintf("%s",\$ID) =~ /(0x[0-9a-fA-F]+)/;
148
149 0 ▶ unless ($file_string =~ m/^\>/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file_string =~ /^\>/) 0 0 0
150 # Only pass filename if filehandle is not available,
151 # or "Bio::Phylo" will create a new filehandle that ends
152 # out of scope and can't be closed directly, leaving 2 open
153 # filehandles for the same file (so file can't be deleted)
154 0 my $file_arg;
155 my $file_value;
156 0 ▶ if ( exists $self->{'_filehandle'}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'_filehandle'} and defined $self->{'_filehandle'}) { } 0 0 0
Condition0%0/3
exists $self->{"_filehandle"} and defined $self->{"_filehandle"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"_filehandle"} and defined $self->{"_filehandle"}
exists $self->{"_filehandle"} defined $self->{"_filehandle"}
Truth table0%0/3
A and B
A= exists $self->{"_filehandle"}B= defined $self->{"_filehandle"}
ABresult
0X0
100
111
157 and defined $self->{'_filehandle'}
158 ) {
159 0 $file_arg = '-handle';
160 0 $file_value = $self->{'_filehandle'};
161 }
162 else {
163 0 $file_arg = '-file';
164 0 $file_value = $self->{'_file'};
165 }
166  
167 0 $self->{'_doc'} = Bio::Phylo::IO->parse($file_arg => $file_value,,
168 '-format' => 'nexml',
169 '-as_project' => '1');
170 }
171
172 0 return $self;
173 }
174  
175 =head2 doc
176  
177 Title : doc
178 Usage : my $nexml_doc = $in_nexmlIO->doc();
179 Function: returns a L<Bio::Phylo::Project> object that contains all the Bio::Phylo data objects parsed from the stream
180 Returns : a L<Bio::Phylo::Project> object
181 Args : none
182  
183 =cut
184  
185 0 ▶sub doc {
Subroutine doc: not called CC=1 SCAR=6.9
Pod documented
186 0 my $self = shift;
187 0 return $self->{'_doc'};
188 }
189  
190 # Takes the Bio::Phylo::Project object and creats BioPerl trees, alns, and seqs from it
191 0 ▶sub _parse {
Subroutine _parse: not called CC=1 SCAR=6.9
192 0 my ($self) = @_;
193
194 0 $self->{'_treeiter'} = 0;
195 0 $self->{'_seqiter'} = 0;
196 0 $self->{'_alniter'} = 0;
197
198 0 $self->{_trees} = $nexml_fac->create_bperl_tree($self);
199 0 $self->{_alns} = $nexml_fac->create_bperl_aln($self);
200 0 $self->{_seqs} = $nexml_fac->create_bperl_seq($self);
201 0 my $taxa_array = $self->doc->get_taxa();
202
203 0 $self->{'_parsed'} = 1; #success
204 }
205  
206 =head1 ITERATORS
207  
208 =head2 next_tree
209  
210 Title : next_tree
211 Usage : $tree = $stream->next_tree
212 Function: Reads the next tree object from the stream and returns it.
213 Returns : a L<Bio::Tree::Tree> object
214 Args : none
215  
216 See L<Bio::Root::IO>, L<Bio::Tree::Tree>
217  
218 =cut
219  
220 0 ▶sub next_tree {
Subroutine next_tree: not called CC=2 SCAR=17.9
Pod documented
221 0 my $self = shift;
222 0 ▶ $self->_parse unless $self->{'_parsed'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_parsed"} 0 0 0
223  
224 0 return $self->{'_trees'}->[ $self->{'_treeiter'}++ ];
225 }
226  
227 =head2 next_seq
228  
229 Title : next_seq
230 Usage : $seq = $stream->next_seq
231 Function: Reads the next seq object from the stream and returns it.
232 Returns : a L<Bio::Seq> object
233 Args : none
234  
235 See L<Bio::Root::IO>, L<Bio::Seq>
236  
237 =cut
238  
239 0 ▶sub next_seq {
Subroutine next_seq: not called CC=2 SCAR=17.9
Pod documented
240 0 my $self = shift;
241 0 ▶ unless ( $self->{'_parsed'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_parsed"}) 0 0 0
242 0 $self->_parse;
243 }
244 0 return $self->{'_seqs'}->[ $self->{'_seqiter'}++ ];
245 }
246  
247 =head2 next_aln
248  
249 Title : next_aln
250 Usage : $aln = $stream->next_aln
251 Function: Reads the next aln object from the stream and returns it.
252 Returns : a L<Bio::SimpleAlign> object
253 Args : none
254  
255 See L<Bio::Root::IO>, L<Bio::SimpleAlign>
256  
257 =cut
258  
259 0 ▶sub next_aln {
Subroutine next_aln: not called CC=2 SCAR=17.9
Pod documented
260 0 my $self = shift;
261 0 ▶ unless ( $self->{'_parsed'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_parsed"}) 0 0 0
262 0 $self->_parse;
263 }
264 0 return $self->{'_alns'}->[ $self->{'_alniter'}++ ];
265 }
266  
267 0 ▶sub _rewind {
Subroutine _rewind: not called CC=2 SCAR=17.9
268 0 my $self = shift;
269 0 my $elt = shift;
270 0 ▶ $self->{"_${elt}iter"} = 0 if defined $self->{"_${elt}iter"};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$self{"_${elt}iter"} 0 0 0
271 0 return 1;
272 }
273  
274 =head2 rewind_seq
275  
276 Title : rewind_seq
277 Usage : $stream->rewind_seq
278 Function: Resets the stream for seqs
279 Returns : none
280 Args : none
281  
282 See L<Bio::Root::IO>, L<Bio::Seq>
283  
284 =cut
285  
286 0 ▶sub rewind_seq { shift->_rewind('seq'); }
Subroutine rewind_seq: not called CC=1 SCAR=6.9
Pod documented
287  
288 =head2 rewind_aln
289  
290 Title : rewind_aln
291 Usage : $stream->rewind_aln
292 Function: Resets the stream for alns
293 Returns : none
294 Args : none
295  
296 See L<Bio::Root::IO>, L<Bio::Simple::Align>
297  
298 =cut
299  
300 0 ▶sub rewind_aln { shift->_rewind('aln'); }
Subroutine rewind_aln: not called CC=1 SCAR=6.9
Pod documented
301  
302 =head2 rewind_tree
303  
304 Title : rewind_tree
305 Usage : $stream->rewind_tree
306 Function: Resets the stream for trees
307 Returns : none
308 Args : none
309  
310 See L<Bio::Root::IO>, L<Bio::tree::tree>
311  
312 =cut
313  
314 0 ▶sub rewind_tree { shift->_rewind('tree'); }
Subroutine rewind_tree: not called CC=1 SCAR=6.9
Pod documented
315  
316 =head2 write
317  
318 Title : write
319 Usage : $stream->write(-alns => $alns,-seqs => $seqs,-trees => $trees)
320 Function: converts BioPerl seq, tree, and aln objects into Bio::Phylo
321 seq, tree, and aln objects, constructs a Bio::Phylo::Project
322 object made up of the newly created Bio::Phylo objects, and
323 writes the Bio::Phylo:Project object to the stream as a valid
324 nexml document
325 Returns : none
326 Args : \@L<Bio::Seq>, \@L<Bio::SimpleAlign>, \@L<Bio::Tree::Tree>
327  
328 See L<Bio::Root::IO>, L<Bio::tree::tree>, L<Bio::Seq>, L<Bio::SimpleAlign>
329  
330 =cut
331  
332 0 ▶sub write {
Subroutine write: not called CC=14 SCAR=53.5
Pod documented
333 0 my ($self, @args) = @_;
334
335 0 my %params = @args;
336
337 0 my ($trees, $alns, $seqs) = @params{qw( -trees -alns -seqs )};
338 0 my %taxa_hash = ();
339 0 my %seq_matrices = ();
340  
341 0 my $proj_doc = Bio::Phylo::Factory->create_project();
342
343 #convert trees to bio::Phylo objects
344 0 my $forest = Bio::Phylo::Factory->create_forest();
345 0 my @forests;
346 my @taxa_array;
347 0 my $ent;
348 0 my $taxa_o;
349 0 my $phylo_tree_o;
350
351 0 foreach my $tree (@$trees) {
352 0 my $nexml_id = $tree->get_tag_values('_NexmlIO_ID');
353 0 $taxa_o = undef;
354 0 ▶ if ( defined $taxa_hash{$nexml_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $taxa_hash{$nexml_id}) { } 0 0 0
355 0 $taxa_o = $taxa_hash{$nexml_id};
356 }
357 else {
358 0 ($taxa_o) = $nexml_fac->create_bphylo_taxa($tree);
359 0 ▶ $forest->set_taxa($taxa_o) if defined $taxa_o;
Branch: 1 branch
BranchTrueFalseTotal
if defined $taxa_o 0 0 0
360 0 $taxa_hash{$nexml_id} = $taxa_o;
361 }
362
363 0 ($phylo_tree_o) = $nexml_fac->create_bphylo_tree($tree, $taxa_o);
364
365 0 $forest->insert($phylo_tree_o);
366 }
367  
368 #convert matrices to Bio::Phylo objects
369 0 my $matrices = Bio::Phylo::Matrices->new();
370 0 my $phylo_matrix_o;
371
372 0 foreach my $aln (@$alns)
373 {
374 0 $taxa_o = undef;
375 0 ▶ if (defined $taxa_hash{ $aln->{_Nexml_ID} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $taxa_hash{$aln->{'_Nexml_ID'}}) { } 0 0 0
376 0 $taxa_o = $taxa_hash{$aln->{_Nexml_ID}};
377 }
378 else {
379 0 ($taxa_o) = $nexml_fac->create_bphylo_taxa($aln);
380 0 $taxa_hash{$aln->{_Nexml_ID}} = $taxa_o;
381 }
382
383 0 ($phylo_matrix_o) = $nexml_fac->create_bphylo_aln($aln, $taxa_o);
384
385 0 ▶ $phylo_matrix_o->set_taxa($taxa_o) if defined $taxa_o;
Branch: 1 branch
BranchTrueFalseTotal
if defined $taxa_o 0 0 0
386 0 $matrices->insert($phylo_matrix_o);
387 }
388
389 0 my $seq_matrix_o;
390 my $datum;
391 #convert sequences to Bio::Phylo objects
392 0 foreach my $seq (@$seqs)
393 {
394 0 $taxa_o = undef;
395 #check if this Bio::Phylo::Taxa obj has already been created
396 0 ▶ if (defined $taxa_hash{ $seq->{_Nexml_ID} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $taxa_hash{$seq->{'_Nexml_ID'}}) { } 0 0 0
397 0 $taxa_o = $taxa_hash{$seq->{_Nexml_ID}};
398 }
399 else {
400 0 ($taxa_o) = $nexml_fac->create_bphylo_taxa($seq);
401 0 $taxa_hash{$seq->{_Nexml_ID}} = $taxa_o;
402 }
403 0 $datum = $nexml_fac->create_bphylo_seq($seq, $taxa_o);
404 #check if this Bio::Phylo::Matrices::Matrix obj has already been created
405 0 ▶ if (defined $seq_matrices{ $seq->{_Nexml_matrix_ID} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $seq_matrices{$seq->{'_Nexml_matrix_ID'}}) { } 0 0 0
406 0 $seq_matrix_o = $seq_matrices{$seq->{_Nexml_matrix_ID}};
407 0 my $taxon_name = $datum->get_taxon()->get_name();
408 0 $datum->unset_taxon();
409 0 $seq_matrix_o->insert($datum);
410 0 $datum->set_taxon($seq_matrix_o->get_taxa()->get_by_name($taxon_name));
411 }
412 else {
413 0 $seq_matrix_o = Bio::Phylo::Factory->create_matrix('-type' => $datum->moltype);
414 0 $seq_matrices{$seq->{_Nexml_matrix_ID}} = $seq_matrix_o;
415 0 ▶ $seq_matrix_o->set_taxa($taxa_o) if defined $taxa_o;
Branch: 1 branch
BranchTrueFalseTotal
if defined $taxa_o 0 0 0
416 0 $seq_matrix_o->insert($datum);
417
418 #get matrix label
419 0 my $feat = ($seq->get_SeqFeatures())[0];
420 0 ▶ my $matrix_label = ($feat->get_tag_values('matrix_label'))[0] if $feat->has_tag('id');
Branch: 1 branch
BranchTrueFalseTotal
if $feat->has_tag("id") 0 0 0
421 0 $seq_matrix_o->set_name($matrix_label);
422
423 0 $matrices->insert($seq_matrix_o);
424 }
425 }
426
427 #Add matrices and forest objects to project object which represents a complete nexml document
428 0 ▶ if($forest->first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($forest->first) 0 0 0
429 0 $proj_doc->insert($forest);
430 }
431 0 ▶ while(my $curr_matrix = $matrices->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $curr_matrix = $matrices->next) 0 0 0
432 0 $proj_doc->insert($curr_matrix);
433 }
434
435 #write nexml document to stream
436 0 my $ret = $self->_print($proj_doc->to_xml(-compact=>1));
437 0 $self->flush;
438 0 return($ret);
439 }
440  
441 =head2 extract_seqs
442  
443 Title : extract_seqs
444 Usage : $nexmlIO->extract_seqs(-file => ">$outfile", -format => $format)
445 Function: converts BioPerl seqs stored in the NexmlIO object into the provided
446 format and writes it to the provided file. Uses L<Bio::SeqIO> to do
447 the conversion and writing.
448 Returns : none
449 Args : file to write to, format to be converted to
450  
451 See L<Bio::Seq>, L<Bio::SeqIO>
452  
453 =cut
454  
455 0 ▶sub extract_seqs {
Subroutine extract_seqs: not called CC=6 SCAR=37.4
Pod documented
456 0 my $self = shift;
457 0 ▶ unless ( $self->{'_parsed'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_parsed"}) 0 0 0
458 0 $self->_parse;
459 }
460
461 0 my %params = @_;
462 0 my $remove_spaces = 0;
463 0 my $ret = 0;
464 0 my ($format, $file) = @params{qw( -format -file)};
465
466 0 for ($format) {
467 0 ▶ /^fasta$/i && do {
Branch: 1 branch
BranchTrueFalseTotal
/^fasta$/i and do { $remove_spaces = 1; last } 0 0 0
468 # this is ok, flag so that the nexmlid gets converted;
469 0 $remove_spaces = 1;
470 0 last;
471 };
472 # default
473 0 do {
474 0 $self->throw("Format '$format' not yet supported for extraction");
475 };
476 }
477
478 0 my $seqIO = Bio::SeqIO->new(-format => $format, -file => $file);
479 0 my $seqs = $self->{_seqs};
480 0 foreach my $seq (@$seqs) {
481 0 ▶ if ($remove_spaces) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remove_spaces) 0 0 0
482 0 my $id = $seq->id;
483 0 $id =~ s/ /_/;
484 0 $seq->id($id);
485 }
486 0 $ret = $seqIO->write_seq($seq);
487 }
488 0 return $ret;
489 }
490  
491 =head2 extract_alns
492  
493 Title : extract_alns
494 Usage : $nexmlIO->extract_alns(-file => ">$outfile", -format => $format)
495 Function: converts BioPerl alns stored in the NexmlIO object into the provided
496 format and writes it to the provided file. Uses L<Bio::AlignIO> to do
497 the conversion and writing.
498 Returns : none
499 Args : file to write to, format to be converted to
500  
501 See L<Bio::SimpleAlign>, L<Bio::AlignIO>
502  
503 =cut
504  
505 0 ▶sub extract_alns {
Subroutine extract_alns: not called CC=3 SCAR=24.8
Pod documented
506 0 my $self = shift;
507 0 ▶ unless ( $self->{'_parsed'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_parsed"}) 0 0 0
508 0 $self->_parse;
509 }
510
511 0 my $ret = 0;
512 0 my %params = @_;
513 0 my ($format, $file) = @params{qw( -format -file)};
514
515 0 my $alignIO = Bio::AlignIO->new(-format => $format, -file => $file);
516 0 my $alns = $self->{_alns};
517 0 foreach my $aln (@$alns) {
518 0 $ret = $alignIO->write_aln($aln);
519 }
520 0 return $ret;
521 }
522  
523 =head2 extract_trees
524  
525 Title : extract_trees
526 Usage : $nexmlIO->extract_trees(-file => ">$outfile", -format => $format)
527 Function: converts BioPerl trees stored in the NexmlIO object into the provided
528 format and writes it to the provided file. Uses L<Bio::TreeIO> to do
529 the conversion and writing.
530 Returns : none
531 Args : file to write to, format to be converted to
532  
533 See L<Bio::Tree::Tree>, L<Bio::TreeIO>
534  
535 =cut
536  
537 0 ▶sub extract_trees {
Subroutine extract_trees: not called CC=3 SCAR=24.8
Pod documented
538 0 my $self = shift;
539 0 ▶ unless ( $self->{'_parsed'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_parsed"}) 0 0 0
540 0 $self->_parse;
541 }
542
543 0 my $ret = 0;
544 0 my %params = @_;
545 0 my ($format, $file) = @params{qw( -format -file)};
546
547 0 my $treeIO = Bio::TreeIO->new(-format => $format, -file => $file);
548 0 my $trees = $self->{_trees};
549 0 foreach my $tree (@$trees) {
550 0 $treeIO->write_tree($tree);
551 0 $ret = 1;
552 }
553 0 return $ret;
554 }
555  
556 2 1;
« Nexml/Factory.pm ↑ Summary AlignIO/nexml.pm »