| 1 | package Bio::Phylo::Unparsers::Fasta; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base 'Bio::Phylo::Unparsers::Abstract'; | ||
| 5 | use Bio::Phylo::Util::Exceptions 'throw'; | ||
| 6 | use Bio::Phylo::Util::CONSTANT qw':objecttypes looks_like_object'; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Bio::Phylo::Unparsers::Fasta - Serializer used by Bio::Phylo::IO, no serviceable parts inside | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | Definition lines for each FASTA records are either (first choice) created from | ||
| 15 | the generic 'fasta_def_line' annotation which can be manipulated like so: | ||
| 16 | |||
| 17 | $datum->set_generic( 'fasta_def_line' => $line ) | ||
| 18 | |||
| 19 | So you can retrieve it by calling: | ||
| 20 | |||
| 21 | my $line = $datum->get_generic('fasta_def_line'); | ||
| 22 | |||
| 23 | Alternatively the name of the $datum is used, or, lacking that, the name | ||
| 24 | of the associated taxon, if any. | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | sub _to_string { | ||
| 29 | my $self = shift; | ||
| 30 | my $obj = $self->{'PHYLO'}; | ||
| 31 | my $matrix; | ||
| 32 | eval { $matrix = $obj if looks_like_object $obj, _MATRIX_; }; | ||
| 33 | if ($@) { | ||
| 34 | undef($@); | ||
| 35 | eval { | ||
| 36 | ($matrix) = @{ $obj->get_matrices } | ||
| 37 | if looks_like_object $obj, _PROJECT_; | ||
| 38 | }; | ||
| 39 | if ( $@ or not $matrix ) { | ||
| 40 | throw 'ObjectMismatch' => 'Invalid object!'; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | my $string = ''; | ||
| 44 | for my $seq ( @{ $matrix->get_entities } ) { | ||
| 45 | my $taxon_name = ''; | ||
| 46 | if ( my $taxon = $seq->get_taxon ) { | ||
| 47 | $taxon_name = $taxon->get_name; | ||
| 48 | } | ||
| 49 | my $name = $seq->get_generic('fasta_def_line') || $seq->get_name || $taxon_name; | ||
| 50 | my $def = '>' . $name . "\n"; | ||
| 51 | my $char = $seq->get_char; | ||
| 52 | my $n = 80; # $n is group size. | ||
| 53 | my @groups = unpack "a$n" x (length($char)/$n) . "a*", $char; | ||
| 54 | $string .= $def . join("\n", @groups) . "\n"; | ||
| 55 | } | ||
| 56 | return $string; | ||
| 57 | } | ||
| 58 | |||
| 59 | # podinherit_insert_token | ||
| 60 | |||
| 61 | =head1 SEE ALSO | ||
| 62 | |||
| 63 | There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo> | ||
| 64 | for any user or developer questions and discussions. | ||
| 65 | |||
| 66 | =over | ||
| 67 | |||
| 68 | =item L<Bio::Phylo::IO> | ||
| 69 | |||
| 70 | The fasta unparser is called by the L<Bio::Phylo::IO> object. | ||
| 71 | Look there to learn how to create phylip formatted files. | ||
| 72 | |||
| 73 | =item L<Bio::Phylo::Manual> | ||
| 74 | |||
| 75 | Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>. | ||
| 76 | |||
| 77 | =back | ||
| 78 | |||
| 79 | =head1 CITATION | ||
| 80 | |||
| 81 | If you use Bio::Phylo in published research, please cite it: | ||
| 82 | |||
| 83 | B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen> | ||
| 84 | and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl. | ||
| 85 | I<BMC Bioinformatics> B<12>:63. | ||
| 86 | L<http://dx.doi.org/10.1186/1471-2105-12-63> | ||
| 87 | |||
| 88 | =cut | ||
| 89 | |||
| 90 | 1; |