Simplify some code
[bioperl-live.git] / Bio / TreeIO / nexml.pm
blob67e7f0c1a36db1e9c465882d2fed546f45884b63
2 # BioPerl module for Bio::TreeIO::nexml
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Chase Miller <chmille4@gmail.com>
8 # Copyright Chase Miller
10 # You may distribute this module under the same terms as perl itself
12 # POD documentation - main docs before the code
14 =head1 NAME
16 Bio::TreeIO::nexml - A TreeIO driver module for parsing NeXML tree files
18 =head1 SYNOPSIS
20 use Bio::TreeIO;
21 my $in = Bio::TreeIO->new(-file => 'data.nexml' -format => 'Nexml');
22 while( my $tree = $in->next_tree ) {
25 =head1 DESCRIPTION
27 This is a driver module for parsing tree data in a NeXML format. For
28 more information on NeXML, visit L<http://www.nexml.org>.
30 =head1 FEEDBACK
32 =head2 Mailing Lists
34 User feedback is an integral part of the evolution of this and other
35 Bioperl modules. Send your comments and suggestions preferably to
36 the Bioperl mailing list. Your participation is much appreciated.
38 bioperl-l@bioperl.org - General discussion
39 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
41 =head2 Support
43 Please direct usage questions or support issues to the mailing list:
45 I<bioperl-l@bioperl.org>
47 rather than to the module maintainer directly. Many experienced and
48 reponsive experts will be able look at the problem and quickly
49 address it. Please include a thorough description of the problem
50 with code and data examples if at all possible.
52 =head2 Reporting Bugs
54 Report bugs to the Bioperl bug tracking system to help us keep track
55 of the bugs and their resolution. Bug reports can be submitted via
56 the web:
58 https://github.com/bioperl/bioperl-live/issues
60 =head1 AUTHOR - Chase Miller
62 Email chmille4@gmail.com
64 =head1 APPENDIX
66 The rest of the documentation details each of the object methods.
67 Internal methods are usually preceded with a _
69 =cut
71 # Let the code begin...
73 package Bio::TreeIO::nexml;
74 use strict;
76 use lib '../..';
77 use Bio::Event::EventGeneratorI;
78 use IO::String;
79 use Bio::Nexml::Factory;
80 use Bio::Phylo::IO qw (parse unparse);
83 use base qw(Bio::TreeIO);
86 sub _initialize {
87 my $self = shift;
88 $self->SUPER::_initialize(@_);
89 $self->{_doc} = undef;
92 =head2 next_tree
94 Title : next_tree
95 Usage : my $tree = $treeio->next_tree
96 Function: Gets the next tree in the stream
97 Returns : L<Bio::Tree::TreeI>
98 Args : none
101 =cut
103 sub next_tree {
104 my ($self) = @_;
105 unless ( $self->{'_parsed'} ) {
106 $self->_parse;
108 return $self->{'_trees'}->[ $self->{'_treeiter'}++ ];
111 =head2 doc
113 Title : doc
114 Usage : $treeio->doc
115 Function: Returns the biophylo nexml document object
116 Returns : Bio::Phylo::Project
117 Args : none or Bio::Phylo::Project object
119 =cut
121 sub doc {
122 my ($obj,$value) = @_;
123 if( defined $value) {
124 $obj->{'_doc'} = $value;
126 return $obj->{'_doc'};
130 =head2 rewind
132 Title : rewind
133 Usage : $treeio->rewind
134 Function: Resets the stream
135 Returns : none
136 Args : none
138 =cut
140 sub rewind {
141 my $self = shift;
142 $self->{'_treeiter'} = 0;
145 sub _parse {
146 my ($self) = @_;
148 $self->{'_parsed'} = 1;
149 $self->{'_treeiter'} = 0;
150 my $fac = Bio::Nexml::Factory->new();
152 # Only pass filename if filehandle is not available,
153 # or "Bio::Phylo" will create a new filehandle that ends
154 # out of scope and can't be closed directly, leaving 2 open
155 # filehandles for the same file (so file can't be deleted)
156 my $file_arg;
157 my $file_value;
158 if ( exists $self->{'_filehandle'}
159 and defined $self->{'_filehandle'}
161 $file_arg = '-handle';
162 $file_value = $self->{'_filehandle'};
164 else {
165 $file_arg = '-file';
166 $file_value = $self->{'_file'};
169 $self->doc(parse(
170 $file_arg => $file_value,
171 '-format' => 'nexml',
172 '-as_project' => '1'
175 $self->{'_trees'} = $fac->create_bperl_tree($self);
178 =head2 write_tree
180 Title : write_tree
181 Usage : $treeio->write_tree($tree);
182 Function: Writes a tree onto the stream
183 Returns : none
184 Args : L<Bio::Tree::TreeI>
187 =cut
189 sub write_tree {
190 my ($self, $bp_tree) = @_;
192 my $fac = Bio::Nexml::Factory->new();
193 my $taxa = $fac->create_bphylo_taxa($bp_tree);
194 my ($tree) = $fac->create_bphylo_tree($bp_tree, $taxa);
196 my $forest = Bio::Phylo::Factory->create_forest();
197 $self->doc(Bio::Phylo::Factory->create_project());
199 $forest->set_taxa($taxa);
200 $forest->insert($tree);
202 $self->doc->insert($forest);
204 my $ret = $self->_print($self->doc->to_xml());
205 $self->flush;
206 return $ret;