Phylo/Unparsers/Phylip.pm untested

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