| 1 | package Bio::Phylo::Parsers::Fasta; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base 'Bio::Phylo::Parsers::Abstract'; | ||
| 5 | use Bio::Phylo::Util::Exceptions 'throw'; | ||
| 6 | use Bio::Phylo::Util::CONSTANT ':objecttypes'; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Bio::Phylo::Parsers::Fasta - Parser used by Bio::Phylo::IO, no serviceable parts inside | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | A very symplistic FASTA file parser. To use it, you need to pass an argument | ||
| 15 | that specifies the data type of the FASTA records into the parse function, i.e. | ||
| 16 | |||
| 17 | my $project = parse( | ||
| 18 | -type => 'dna', # or rna, protein | ||
| 19 | -format => 'fasta', | ||
| 20 | -file => 'infile.fa', | ||
| 21 | -as_project => 1 | ||
| 22 | ); | ||
| 23 | |||
| 24 | For each FASTA record, the first "word" on the definition line is used as the | ||
| 25 | name of the produced datum object. The entire line is assigned to: | ||
| 26 | |||
| 27 | $datum->set_generic( 'fasta_def_line' => $line ) | ||
| 28 | |||
| 29 | So you can retrieve it by calling: | ||
| 30 | |||
| 31 | my $line = $datum->get_generic('fasta_def_line'); | ||
| 32 | |||
| 33 | BioPerl actually parses definition lines to get GIs and such out of there, so if | ||
| 34 | you're looking for that, use L<Bio::SeqIO> from the bioperl-live distribution. | ||
| 35 | You can always pass the resulting Bio::Seq objects to | ||
| 36 | Bio::Phylo::Matrices::Datum->new_from_bioperl to turn the L<Bio::Seq> objects | ||
| 37 | that Bio::SeqIO produces into L<Bio::Phylo::Matrices::Datum> objects. | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | sub _parse { | ||
| 42 | my $self = shift; | ||
| 43 | my $fh = $self->_handle; | ||
| 44 | my $fac = $self->_factory; | ||
| 45 | my $sh = $self->_handlers(_DATUM_); | ||
| 46 | my $type = $self->_args->{'-type'} or throw 'BadArgs' => 'No data type specified!'; | ||
| 47 | my $matrix = $fac->create_matrix( '-type' => $type ); | ||
| 48 | my ( $seq, $datum ); | ||
| 49 | while (<$fh>) { | ||
| 50 | chomp; | ||
| 51 | my $line = $_; | ||
| 52 | if ( $line =~ />(\S+)/ ) { | ||
| 53 | my $name = $1; | ||
| 54 | if ( $seq && $datum ) { | ||
| 55 | $datum->set_char($seq); | ||
| 56 | $sh->($datum) if $sh; | ||
| 57 | $matrix->insert($datum); | ||
| 58 | } | ||
| 59 | $datum = $fac->create_datum( | ||
| 60 | '-type' => $type, | ||
| 61 | '-name' => $name, | ||
| 62 | '-generic' => { 'fasta_def_line' => $line } | ||
| 63 | ); | ||
| 64 | $seq = ''; | ||
| 65 | } | ||
| 66 | else { | ||
| 67 | $seq .= $line; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | # within the loop, insertions are triggered by encountering the next definition line, | ||
| 72 | # hence, the last $datum needs to be inserted explicitly when we leave the loop | ||
| 73 | $datum->set_char($seq); | ||
| 74 | $sh->($datum) if $sh; | ||
| 75 | $matrix->insert($datum); | ||
| 76 | return $matrix; | ||
| 77 | } | ||
| 78 | |||
| 79 | # podinherit_insert_token | ||
| 80 | |||
| 81 | =head1 SEE ALSO | ||
| 82 | |||
| 83 | There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo> | ||
| 84 | for any user or developer questions and discussions. | ||
| 85 | |||
| 86 | =over | ||
| 87 | |||
| 88 | =item L<Bio::Phylo::IO> | ||
| 89 | |||
| 90 | The fasta parser is called by the L<Bio::Phylo::IO|Bio::Phylo::IO> object. | ||
| 91 | Look there to learn more about parsing. | ||
| 92 | |||
| 93 | =item L<Bio::Phylo::Manual> | ||
| 94 | |||
| 95 | Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com> | ||
| 96 | |||
| 97 | =back | ||
| 98 | |||
| 99 | =head1 CITATION | ||
| 100 | |||
| 101 | If you use Bio::Phylo in published research, please cite it: | ||
| 102 | |||
| 103 | B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen> | ||
| 104 | and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl. | ||
| 105 | I<BMC Bioinformatics> B<12>:63. | ||
| 106 | L<http://dx.doi.org/10.1186/1471-2105-12-63> | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | 1; |