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