bump rc version
[bioperl-live.git] / Bio / SeqIO / nexml.pm
blob02299cacbd11942e04ba999f5f6617c44137adda
1 # BioPerl module for Bio::SeqIO::nexml
3 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 # Cared for by Chase Miller <chmille4@gmail.com>
7 # Copyright Chase Miller
9 # You may distribute this module under the same terms as perl itself
10 # _history
11 # May, 2009 Largely written by Chase Miller
13 # POD documentation - main docs before the code
15 =head1 NAME
17 Bio::SeqIO::nexml - NeXML sequence input/output stream
19 =head1 SYNOPSIS
21 Do not use this module directly. Use it via the Bio::SeqIO class.
23 =head1 DESCRIPTION
25 This object can transform Bio::Seq objects to and from NeXML format.
26 For more information on the NeXML standard, visit L<http://www.nexml.org>.
28 =head1 FEEDBACK
30 =head2 Mailing Lists
32 User feedback is an integral part of the evolution of this and other
33 Bioperl modules. Send your comments and suggestions preferably to one
34 of the Bioperl mailing lists. Your participation is much appreciated.
36 bioperl-l@bioperl.org - General discussion
37 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
39 =head2 Support
41 Please direct usage questions or support issues to the mailing list:
43 I<bioperl-l@bioperl.org>
45 rather than to the module maintainer directly. Many experienced and
46 reponsive experts will be able look at the problem and quickly
47 address it. Please include a thorough description of the problem
48 with code and data examples if at all possible.
50 =head2 Reporting Bugs
52 Report bugs to the Bioperl bug tracking system to help us keep track
53 the bugs and their resolution. Bug reports can be submitted via the
54 web:
56 https://github.com/bioperl/bioperl-live/issues
58 =head1 AUTHORS - Chase Miller
60 Email: chmille4@gmail.com
62 =head1 CONTRIBUTORS
64 Mark Jensen, maj@fortinbras.us
65 Rutger Vos, rutgeraldo@gmail.com
67 =head1 APPENDIX
69 The rest of the documentation details each of the object
70 methods. Internal methods are usually preceded with a _
72 =cut
74 # Let the code begin...
76 package Bio::SeqIO::nexml;
78 use strict;
80 use lib '../..';
81 use Bio::Seq;
82 use Bio::Seq::SeqFactory;
83 use Bio::Nexml::Factory;
84 use Bio::Phylo::IO qw (parse unparse);
86 use base qw(Bio::SeqIO);
88 sub _initialize {
89 my($self,@args) = @_;
90 $self->SUPER::_initialize(@args);
91 $self->{_doc} = undef;
94 =head2 next_seq
96 Title : next_seq
97 Usage : $seq = $stream->next_seq()
98 Function: returns the next sequence in the stream
99 Returns : L<Bio::Seq> object
100 Args : NONE
102 =cut
104 sub next_seq {
105 my ($self) = @_;
106 unless ( $self->{'_parsed'} ) {
107 #use a parse function to load all the sequence objects found in the nexml file at once
108 $self->_parse;
110 return $self->{'_seqs'}->[ $self->{'_seqiter'}++ ];
113 =head2 rewind
115 Title : rewind
116 Usage : $seqio->rewind
117 Function: Resets the stream
118 Returns : none
119 Args : none
122 =cut
124 sub rewind {
125 my $self = shift;
126 $self->{'_seqiter'} = 0;
129 =head2 doc
131 Title : doc
132 Usage : $treeio->doc
133 Function: Returns the biophylo nexml document object
134 Returns : Bio::Phylo::Project
135 Args : none or Bio::Phylo::Project object
137 =cut
139 sub doc {
140 my ($obj,$value) = @_;
141 if( defined $value) {
142 $obj->{'_doc'} = $value;
144 return $obj->{'_doc'};
147 sub _parse {
148 my ($self) = @_;
150 $self->{'_parsed'} = 1;
151 $self->{'_seqiter'} = 0;
152 my $fac = Bio::Nexml::Factory->new();
154 # Only pass filename if filehandle is not available,
155 # or "Bio::Phylo" will create a new filehandle that ends
156 # out of scope and can't be closed directly, leaving 2 open
157 # filehandles for the same file (so file can't be deleted)
158 my $file_arg;
159 my $file_value;
160 if ( exists $self->{'_filehandle'}
161 and defined $self->{'_filehandle'}
163 $file_arg = '-handle';
164 $file_value = $self->{'_filehandle'};
166 else {
167 $file_arg = '-file';
168 $file_value = $self->{'_file'};
171 $self->doc(Bio::Phylo::IO->parse(
172 $file_arg => $file_value,
173 '-format' => 'nexml',
174 '-as_project' => '1'
177 $self->{'_seqs'} = $fac->create_bperl_seq($self);
179 unless(@{ $self->{'_seqs'} } == 0) {
180 # self->debug("no seqs in $self->{_file}");
184 =head2 write_seq
186 Title : write_seq
187 Usage : $stream->write_seq(@seq)
188 Function: Writes the $seq object into the stream
189 Returns : 1 for success and 0 for error
190 Args : Array of 1 or more L<Bio::PrimarySeqI> objects
192 =cut
194 sub write_seq {
196 my ($self, $bp_seq) = @_;
198 my $fac = Bio::Nexml::Factory->new();
199 my $taxa = $fac->create_bphylo_taxa($bp_seq);
200 my ($seq) = $fac->create_bphylo_seq($bp_seq, $taxa);
202 my $matrix = Bio::Phylo::Factory->create_matrix('-type' => $seq->get_type());
203 $matrix->insert($seq);
204 $matrix->set_taxa($taxa);
206 #set matrix label
207 my $feat = ($bp_seq->get_SeqFeatures())[0];
208 $matrix->set_name($feat->get_tag_values('matrix_label'));
210 $self->doc(Bio::Phylo::Factory->create_project());
212 $self->doc->insert($matrix);
214 my $ret = $self->_print($self->doc->to_xml());
215 $self->flush;
216 return $ret