| 1 | package Bio::Phylo::Unparsers::Phylip; | ||
| 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::Phylip - Serializer used by Bio::Phylo::IO, no serviceable parts inside | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | This module unparses a Bio::Phylo data structure into an input file for | ||
| 15 | PHYLIP and RAxML. The file format (as it is interpreted | ||
| 16 | here) consists of: | ||
| 17 | |||
| 18 | =over | ||
| 19 | |||
| 20 | =item first line | ||
| 21 | |||
| 22 | the number of species, a space, the number of characters | ||
| 23 | |||
| 24 | =item subsequent lines | ||
| 25 | |||
| 26 | ten-character species name, sequence | ||
| 27 | |||
| 28 | =back | ||
| 29 | |||
| 30 | Here is an example of what the output might look like: | ||
| 31 | |||
| 32 | 4 2 | ||
| 33 | Species_1 AC | ||
| 34 | Species_2 AG | ||
| 35 | Species_3 GT | ||
| 36 | Species_4 GG | ||
| 37 | |||
| 38 | To the unparse() function pass a matrix object or a project | ||
| 39 | (whose first matrix will be serialized) as value of the '-phylo' | ||
| 40 | argument. After serialization, any shortened phylip-specific | ||
| 41 | names (which need to be 10 characters long) will have been assigned | ||
| 42 | to the 'phylip_name' slot of set_generic. Example: | ||
| 43 | |||
| 44 | my $phylip_string = unparse( | ||
| 45 | -format => 'phylip', | ||
| 46 | -phylo => $matrix, | ||
| 47 | ); | ||
| 48 | for my $seq ( @{ $matrix->get_entities } ) { | ||
| 49 | # this returns the shortened name, which is unique to the matrix | ||
| 50 | my $phylip_name = $seq->get_generic('phylip_name'); | ||
| 51 | } | ||
| 52 | |||
| 53 | This default behavior enforces strict compliance with the phylip | ||
| 54 | rule for 10-character row names. It is possible to turn this off | ||
| 55 | by passing in the optional C<-relaxed> flag with a true value, e.g.: | ||
| 56 | |||
| 57 | my $phylip_string = unparse( | ||
| 58 | -format => 'phylip', | ||
| 59 | -phylo => $matrix, | ||
| 60 | -relaxed => 1, | ||
| 61 | ); | ||
| 62 | |||
| 63 | The phylip module is called by the L<Bio::Phylo::IO> object, so | ||
| 64 | look there to learn about parsing and serializing in general. | ||
| 65 | |||
| 66 | =begin comment | ||
| 67 | |||
| 68 | Type : Unparser | ||
| 69 | Title : _to_string | ||
| 70 | Usage : my $str = $phylip->_to_string; | ||
| 71 | Function: Unparses a Bio::Phylo::Matrices::Matrix object into a phylip formatted string. | ||
| 72 | Returns : SCALAR | ||
| 73 | Args : Bio::Phylo::Matrices::Matrix | ||
| 74 | |||
| 75 | =end comment | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | sub _to_string { | ||
| 80 | my $self = shift; | ||
| 81 | my $obj = $self->{'PHYLO'}; | ||
| 82 | my $matrix; | ||
| 83 | eval { $matrix = $obj if looks_like_object $obj, _MATRIX_; }; | ||
| 84 | if ($@) { | ||
| 85 | undef($@); | ||
| 86 | eval { | ||
| 87 | ($matrix) = @{ $obj->get_matrices } | ||
| 88 | if looks_like_object $obj, _PROJECT_; | ||
| 89 | }; | ||
| 90 | if ( $@ or not $matrix ) { | ||
| 91 | throw 'ObjectMismatch' => 'Invalid object!'; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | my $string = $matrix->get_ntax() . ' ' . $matrix->get_nchar() . "\n"; | ||
| 95 | my ( %seq_for_id, %phylip_name_for_id, @ids, %seen_name ); | ||
| 96 | |||
| 97 | # iterate over matrix rows | ||
| 98 | for my $seq ( @{ $matrix->get_entities } ) { | ||
| 99 | |||
| 100 | # store seq keyed on row id | ||
| 101 | my $id = $seq->get_id; | ||
| 102 | $seq_for_id{$id} = $seq->get_char; | ||
| 103 | my $name = $seq->get_internal_name; | ||
| 104 | push @ids, $id; | ||
| 105 | |||
| 106 | # relaxed phylip names may exceed 10 characters | ||
| 107 | if ( $self->{'RELAXED'} ) { | ||
| 108 | $phylip_name_for_id{$id} = $name; | ||
| 109 | } | ||
| 110 | |||
| 111 | # strict phylip names may not exceed 10 characters | ||
| 112 | else { | ||
| 113 | if ( length($name) <= 10 ) { | ||
| 114 | |||
| 115 | # pad name with spaces until 10 characters | ||
| 116 | my $phylip_name = $name . ( ( 10 - length($name) ) x ' ' ); | ||
| 117 | |||
| 118 | # not yet seen name, use as as | ||
| 119 | if ( !$seen_name{$phylip_name} ) { | ||
| 120 | $seen_name{$phylip_name}++; | ||
| 121 | $phylip_name_for_id{$id} = $phylip_name; | ||
| 122 | } | ||
| 123 | |||
| 124 | # have seen name | ||
| 125 | else { | ||
| 126 | |||
| 127 | # attach incrementing integer until name is new | ||
| 128 | my $counter = 1; | ||
| 129 | while ( $seen_name{$phylip_name} ) { | ||
| 130 | $phylip_name = | ||
| 131 | substr( $phylip_name, 0, ( 10 - length($counter) ) ); | ||
| 132 | $phylip_name .= $counter; | ||
| 133 | $counter++; | ||
| 134 | } | ||
| 135 | $seen_name{$phylip_name}++; | ||
| 136 | $phylip_name_for_id{$id} = $phylip_name; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | elsif ( length($name) > 10 ) { | ||
| 140 | my $phylip_name = substr( $name, 0, 10 ); | ||
| 141 | if ( !$seen_name{$phylip_name} ) { | ||
| 142 | $seen_name{$phylip_name}++; | ||
| 143 | $phylip_name_for_id{$id} = $phylip_name; | ||
| 144 | } | ||
| 145 | else { | ||
| 146 | my $counter = 1; | ||
| 147 | while ( $seen_name{$phylip_name} ) { | ||
| 148 | $phylip_name = | ||
| 149 | substr( $phylip_name, 0, ( 10 - length($counter) ) ); | ||
| 150 | $phylip_name .= $counter; | ||
| 151 | $counter++; | ||
| 152 | } | ||
| 153 | $phylip_name_for_id{$id} = $phylip_name; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | } | ||
| 157 | $seq->set_generic( 'phylip_name' => $phylip_name_for_id{$id} ); | ||
| 158 | } | ||
| 159 | for my $id (@ids) { | ||
| 160 | $string .= $phylip_name_for_id{$id} . ' ' . $seq_for_id{$id} . "\n"; | ||
| 161 | } | ||
| 162 | return $string; | ||
| 163 | } | ||
| 164 | |||
| 165 | # podinherit_insert_token | ||
| 166 | |||
| 167 | =head1 SEE ALSO | ||
| 168 | |||
| 169 | There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo> | ||
| 170 | for any user or developer questions and discussions. | ||
| 171 | |||
| 172 | =over | ||
| 173 | |||
| 174 | =item L<Bio::Phylo::IO> | ||
| 175 | |||
| 176 | The phylip unparser is called by the L<Bio::Phylo::IO> object. | ||
| 177 | Look there to learn how to create phylip formatted files. | ||
| 178 | |||
| 179 | =item L<Bio::Phylo::Manual> | ||
| 180 | |||
| 181 | Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>. | ||
| 182 | |||
| 183 | =back | ||
| 184 | |||
| 185 | =head1 CITATION | ||
| 186 | |||
| 187 | If you use Bio::Phylo in published research, please cite it: | ||
| 188 | |||
| 189 | B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen> | ||
| 190 | and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl. | ||
| 191 | I<BMC Bioinformatics> B<12>:63. | ||
| 192 | L<http://dx.doi.org/10.1186/1471-2105-12-63> | ||
| 193 | |||
| 194 | =cut | ||
| 195 | |||
| 196 | 1; |