File Coverage

blib/lib/Bio/Phylo/Unparsers/Hennig86.pm
Criterion Covered Total %
statement 50 54 92.5
branch 4 8 50.0
condition 1 3 33.3
subroutine 9 9 100.0
pod n/a
total 64 74 86.4


line stmt bran cond sub pod time code
1             package Bio::Phylo::Unparsers::Hennig86;
2 1     1   11 use strict;
  1         3  
  1         45  
3 1     1   9 use warnings;
  1         4  
  1         56  
4 1     1   9 use base 'Bio::Phylo::Unparsers::Abstract';
  1         3  
  1         430  
5 1     1   11 use Bio::Phylo;
  1         3  
  1         12  
6 1     1   10 use Bio::Phylo::Util::CONSTANT qw'/looks_like/ :objecttypes';
  1         5  
  1         543  
7 1     1   11 use Bio::Phylo::Util::Exceptions 'throw';
  1         4  
  1         782  
8              
9             my $MATRIX = _MATRIX_;
10             my $PROJECT = _PROJECT_;
11             my %typemap = (
12             'continuous' => 'cont',
13             'dna' => 'dna',
14             'protein' => 'prot',
15             'restriction'=> 'num',
16             'rna' => 'rna',
17             'standard' => 'num',
18             );
19              
20              
21             =head1 NAME
22              
23             Bio::Phylo::Unparsers::Hennig86 - Serializer used by Bio::Phylo::IO, no serviceable
24             parts inside
25              
26             =head1 DESCRIPTION
27              
28             This module turns the supplied object into a Hennig86 string. The supplied
29             object has to either be a L<Bio::Phylo::Matrices::Matrix> object or a
30             L<Bio::Phylo::Project> object, whose first matrix is exported to Hennig86. In
31             other words, this only works on things that are or contain character state
32             matrices.
33              
34             =begin comment
35              
36             Type : Wrapper
37             Title : _to_string
38             Usage : my $hennig_string = $obj->_to_string;
39             Function: Stringifies a Bio::Phylo object into a Hennig86 string
40             Alias :
41             Returns : SCALAR
42             Args : Bio::Phylo::* object
43              
44             =end comment
45              
46             =cut
47              
48             sub _to_string {
49 3     3   9 my $self = shift;
50 3         22 my $obj = $self->{'PHYLO'};
51 3         8 my $matrix;
52 3 50       15 if ( looks_like_implementor $obj, '_type' ) {
53 3 50       18 if ( $obj->_type == $MATRIX ) {
    0          
54 3         8 $matrix = $obj;
55             }
56             elsif ( $obj->_type == $PROJECT ) {
57 0         0 ($matrix) = @{ $obj->get_items(_MATRIX_) };
  0         0  
58             }
59             else {
60 0         0 throw 'ObjectMismatch' => "Can't serialize ".ref($obj)." objects as Hennig86";
61             }
62 3         15 return $self->_serialize_matrix($matrix);
63             }
64             else {
65 0         0 throw 'ObjectMismatch' => "Can't serialize supplied argument as Hennig86";
66             }
67             }
68              
69             sub _serialize_matrix {
70 3     3   12 my ( $self, $matrix ) = @_;
71 3         15 my $hennig86 = $self->_create_header($matrix);
72 3         17 my $to = $matrix->get_type_object;
73 3         10 for my $row ( @{ $matrix->get_entities } ) {
  3         14  
74 15         83 $hennig86 .= $row->get_nexus_name . "\t";
75 15         71 my @char = $row->get_char;
76 15         34 my @encoded;
77 15         36 for my $c ( @char ) {
78 65 100       235 if ( $to->is_ambiguous($c) ) {
79 7         18 my @states = @{ $to->get_states_for_symbol($c) };
  7         28  
80 7         31 push @encoded, '[' . $to->join(\@states) . ']';
81             }
82             else {
83 58         209 push @encoded, $c;
84             }
85             }
86 15         77 $hennig86 .= $to->join(\@encoded) . "\n";
87             }
88 3         25 return $hennig86 .= ";\n";
89             }
90              
91             sub _create_header {
92 3     3   9 my ( $self, $matrix ) = @_;
93            
94 3         32 my $comment = "Hennig86 matrix written by ".ref($self)." ".Bio::Phylo->VERSION." on ".localtime();
95            
96             # calculate nstates
97 3         13 my $nstates = scalar keys %{ $matrix->calc_state_counts };
  3         31  
98            
99             # calculate ntax and nchar
100 3         30 my ( $ntax, $nchar ) = ( $matrix->get_ntax, $matrix->get_nchar );
101            
102             # map type to hennig86 tokens
103 3         29 my $type = lc $matrix->get_type;
104 3   33     18 my $hennig86type = $typemap{ $type } || throw 'BadFormat' => "Can't write $type matrices to Hennig86";
105              
106 3         8 my $template = << 'TEMPLATE';
107             nstates %d
108             xread
109             '%s'
110             %d %d
111             & [%s]
112             TEMPLATE
113              
114 3         31 return sprintf $template, $nstates, $comment, $nchar, $ntax, $hennig86type;
115             }
116              
117             # podinherit_insert_token
118              
119             =head1 SEE ALSO
120              
121             There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
122             for any user or developer questions and discussions.
123              
124             =over
125              
126             =item L<Bio::Phylo::IO>
127              
128             The hennig86 unparser is called by the L<Bio::Phylo::IO> object.
129             Look there to learn how to unparse objects.
130              
131             =item L<Bio::Phylo::Manual>
132              
133             Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>.
134              
135             =item Hennig86 file format
136              
137             To learn more about the Hennig86 format, visit
138             L<http://www.phylo.org/tools/hennig.html>.
139              
140             =back
141              
142             =head1 CITATION
143              
144             If you use Bio::Phylo in published research, please cite it:
145              
146             B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
147             and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
148             I<BMC Bioinformatics> B<12>:63.
149             L<http://dx.doi.org/10.1186/1471-2105-12-63>
150              
151             =cut
152              
153             1;