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