| 1 | # BioPerl module for FAST::Bio::SeqIO::nexml | ||
| 2 | # | ||
| 3 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||
| 4 | # | ||
| 5 | # Cared for by Chase Miller <chmille4@gmail.com> | ||
| 6 | # | ||
| 7 | # Copyright Chase Miller | ||
| 8 | # | ||
| 9 | # You may distribute this module under the same terms as perl itself | ||
| 10 | # _history | ||
| 11 | # May, 2009 Largely written by Chase Miller | ||
| 12 | |||
| 13 | # POD documentation - main docs before the code | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | FAST::Bio::SeqIO::nexml - NeXML sequence input/output stream | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | Do not use this module directly. Use it via the FAST::Bio::SeqIO class. | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | This object can transform FAST::Bio::Seq objects to and from NeXML format. | ||
| 26 | For more information on the NeXML standard, visit L<http://www.nexml.org>. | ||
| 27 | |||
| 28 | =head1 FEEDBACK | ||
| 29 | |||
| 30 | =head2 Mailing Lists | ||
| 31 | |||
| 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. | ||
| 35 | |||
| 36 | bioperl-l@bioperl.org - General discussion | ||
| 37 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 38 | |||
| 39 | =head2 Support | ||
| 40 | |||
| 41 | Please direct usage questions or support issues to the mailing list: | ||
| 42 | |||
| 43 | I<bioperl-l@bioperl.org> | ||
| 44 | |||
| 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. | ||
| 49 | |||
| 50 | =head2 Reporting Bugs | ||
| 51 | |||
| 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: | ||
| 55 | |||
| 56 | https://redmine.open-bio.org/projects/bioperl/ | ||
| 57 | |||
| 58 | =head1 AUTHORS - Chase Miller | ||
| 59 | |||
| 60 | Email: chmille4@gmail.com | ||
| 61 | |||
| 62 | =head1 CONTRIBUTORS | ||
| 63 | |||
| 64 | Mark Jensen, maj@fortinbras.us | ||
| 65 | Rutger Vos, rutgeraldo@gmail.com | ||
| 66 | |||
| 67 | =head1 APPENDIX | ||
| 68 | |||
| 69 | The rest of the documentation details each of the object | ||
| 70 | methods. Internal methods are usually preceded with a _ | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | # Let the code begin... | ||
| 75 | |||
| 76 | package FAST::Bio::SeqIO::nexml; | ||
| 77 | |||
| 78 | use strict; | ||
| 79 | |||
| 80 | use lib '../..'; | ||
| 81 | use FAST::Bio::Seq; | ||
| 82 | use FAST::Bio::Seq::SeqFactory; | ||
| 83 | use FAST::Bio::Nexml::Factory; | ||
| 84 | use FAST::Bio::Phylo::IO qw (parse unparse); | ||
| 85 | |||
| 86 | use base qw(FAST::Bio::SeqIO); | ||
| 87 | |||
| 88 | sub _initialize { | ||
| 89 | my($self,@args) = @_; | ||
| 90 | $self->SUPER::_initialize(@args); | ||
| 91 | $self->{_doc} = undef; | ||
| 92 | } | ||
| 93 | |||
| 94 | =head2 next_seq | ||
| 95 | |||
| 96 | Title : next_seq | ||
| 97 | Usage : $seq = $stream->next_seq() | ||
| 98 | Function: returns the next sequence in the stream | ||
| 99 | Returns : L<FAST::Bio::Seq> object | ||
| 100 | Args : NONE | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 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; | ||
| 109 | } | ||
| 110 | return $self->{'_seqs'}->[ $self->{'_seqiter'}++ ]; | ||
| 111 | } | ||
| 112 | |||
| 113 | =head2 rewind | ||
| 114 | |||
| 115 | Title : rewind | ||
| 116 | Usage : $seqio->rewind | ||
| 117 | Function: Resets the stream | ||
| 118 | Returns : none | ||
| 119 | Args : none | ||
| 120 | |||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | sub rewind { | ||
| 125 | my $self = shift; | ||
| 126 | $self->{'_seqiter'} = 0; | ||
| 127 | } | ||
| 128 | |||
| 129 | =head2 doc | ||
| 130 | |||
| 131 | Title : doc | ||
| 132 | Usage : $treeio->doc | ||
| 133 | Function: Returns the biophylo nexml document object | ||
| 134 | Returns : FAST::Bio::Phylo::Project | ||
| 135 | Args : none or FAST::Bio::Phylo::Project object | ||
| 136 | |||
| 137 | =cut | ||
| 138 | |||
| 139 | sub doc { | ||
| 140 | my ($obj,$value) = @_; | ||
| 141 | if( defined $value) { | ||
| 142 | $obj->{'_doc'} = $value; | ||
| 143 | } | ||
| 144 | return $obj->{'_doc'}; | ||
| 145 | } | ||
| 146 | |||
| 147 | sub _parse { | ||
| 148 | my ($self) = @_; | ||
| 149 | my $fac = FAST::Bio::Nexml::Factory->new(); | ||
| 150 | |||
| 151 | $self->{'_parsed'} = 1; | ||
| 152 | $self->{'_seqiter'} = 0; | ||
| 153 | |||
| 154 | $self->doc(FAST::Bio::Phylo::IO->parse( | ||
| 155 | '-file' => $self->{'_file'}, | ||
| 156 | '-format' => 'nexml', | ||
| 157 | '-as_project' => '1' | ||
| 158 | )); | ||
| 159 | |||
| 160 | |||
| 161 | |||
| 162 | $self->{'_seqs'} = $fac->create_bperl_seq($self); | ||
| 163 | |||
| 164 | |||
| 165 | unless(@{ $self->{'_seqs'} } == 0) | ||
| 166 | { | ||
| 167 | # self->debug("no seqs in $self->{_file}"); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | |||
| 173 | |||
| 174 | =head2 write_seq | ||
| 175 | |||
| 176 | Title : write_seq | ||
| 177 | Usage : $stream->write_seq(@seq) | ||
| 178 | Function: Writes the $seq object into the stream | ||
| 179 | Returns : 1 for success and 0 for error | ||
| 180 | Args : Array of 1 or more L<FAST::Bio::PrimarySeqI> objects | ||
| 181 | |||
| 182 | =cut | ||
| 183 | |||
| 184 | sub write_seq { | ||
| 185 | |||
| 186 | my ($self, $bp_seq) = @_; | ||
| 187 | |||
| 188 | my $fac = FAST::Bio::Nexml::Factory->new(); | ||
| 189 | my $taxa = $fac->create_bphylo_taxa($bp_seq); | ||
| 190 | my ($seq) = $fac->create_bphylo_seq($bp_seq, $taxa); | ||
| 191 | |||
| 192 | my $matrix = FAST::Bio::Phylo::Factory->create_matrix('-type' => $seq->get_type()); | ||
| 193 | $matrix->insert($seq); | ||
| 194 | $matrix->set_taxa($taxa); | ||
| 195 | |||
| 196 | #set matrix label | ||
| 197 | my $feat = ($bp_seq->get_SeqFeatures())[0]; | ||
| 198 | $matrix->set_name($feat->get_tag_values('matrix_label')); | ||
| 199 | |||
| 200 | $self->doc(FAST::Bio::Phylo::Factory->create_project()); | ||
| 201 | |||
| 202 | $self->doc->insert($matrix); | ||
| 203 | |||
| 204 | my $ret = $self->_print($self->doc->to_xml()); | ||
| 205 | $self->flush; | ||
| 206 | return $ret | ||
| 207 | } | ||
| 208 | |||
| 209 | |||
| 210 | 1; |