File Coverage

blib/lib/Bio/Phylo/Unparsers/Pagel.pm
Criterion Covered Total %
statement 27 32 84.3
branch 5 6 83.3
condition n/a
subroutine 4 4 100.0
pod n/a
total 36 42 85.7


line stmt bran cond sub pod time code
1             package Bio::Phylo::Unparsers::Pagel;
2 1     1   7 use strict;
  1         2  
  1         33  
3 1     1   5 use base 'Bio::Phylo::Unparsers::Abstract';
  1         2  
  1         118  
4 1     1   6 use Bio::Phylo::Forest::Tree;
  1         2  
  1         7  
5              
6             =head1 NAME
7              
8             Bio::Phylo::Unparsers::Pagel - Serializer used by Bio::Phylo::IO, no serviceable parts inside
9              
10             =head1 DESCRIPTION
11              
12             This module unparses a Bio::Phylo data structure into an input file for
13             Discrete/Continuous/Multistate. The pagel file format (as it is interpreted
14             here) consists of:
15              
16             =over
17              
18             =item first line
19              
20             the number of tips, the number of characters
21              
22             =item subsequent lines
23              
24             offspring name, parent name, branch length, character state(s).
25              
26             =back
27              
28             Here is an example of what the output might look like:
29              
30             4 2
31             A,n1,0.000000,1,2
32             B,n1,0.000000,1,2
33             n1,n2,0.000000
34             C,n2,0.000000,2,2
35             n2,n3,0.000000
36             D,n3,0.000000,2,1
37              
38             To the unparse() function pass a tree object as value of the '-phylo'
39             argument. The tips in this tree must be linked to taxon objects, and
40             the taxon objects must be linked to datum objects whose character
41             state sequences are to be serialized.
42              
43             During unparsing, the tree is randomly resolved, and branch lengths are
44             formatted to %f floats (i.e. integers, decimal point, integers).
45              
46             The pagel module is called by the L<Bio::Phylo::IO> object, so
47             look there to learn about parsing and serializing in general.
48              
49             =begin comment
50              
51             Type : Unparser
52             Title : to_string($tree)
53             Usage : $pagel->to_string($tree);
54             Function: Unparses a Bio::Phylo::Tree object into a pagel formatted string.
55             Returns : SCALAR
56             Args : Bio::Phylo::Tree
57              
58             =end comment
59              
60             =cut
61              
62             sub _to_string {
63 2     2   4 my $self = shift;
64 2         11 my $tree = $self->{'PHYLO'};
65 2         14 $tree->resolve;
66 2         5 my ( $charcounter, $string ) = 0;
67 2         5 foreach my $node ( @{ $tree->get_entities } ) {
  2         5  
68 34 100       61 if ( $node->get_parent ) {
69 32         73 $string .= $node->get_internal_name . ','
70             . $node->get_parent->get_internal_name . ',';
71 32 100       67 if ( $node->get_branch_length ) {
72 14         27 $string .= sprintf( "%f", $node->get_branch_length );
73             }
74             else {
75 18         29 $string .= sprintf( "%f", 0 );
76             }
77 32 50       67 if ( $node->get_taxon ) {
78 0         0 my $taxon = $node->get_taxon;
79 0         0 foreach ( @{ $taxon->get_data } ) {
  0         0  
80 0         0 $string .= ',' . $_->get_char;
81 0         0 $charcounter++;
82             }
83             }
84 32         54 $string .= "\n";
85             }
86             else {
87 2         5 next;
88             }
89             }
90 2         14 my $header = $tree->calc_number_of_terminals . " ";
91 2         7 $header .= $charcounter / $tree->calc_number_of_terminals;
92 2         9 $string = $header . "\n" . $string;
93 2         8 return $string;
94             }
95              
96             # podinherit_insert_token
97              
98             =head1 SEE ALSO
99              
100             There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
101             for any user or developer questions and discussions.
102              
103             =over
104              
105             =item L<Bio::Phylo::IO>
106              
107             The pagel unparser is called by the L<Bio::Phylo::IO> object.
108             Look there to learn how to create pagel formatted files.
109              
110             =item L<Bio::Phylo::Manual>
111              
112             Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>.
113              
114             =back
115              
116             =head1 CITATION
117              
118             If you use Bio::Phylo in published research, please cite it:
119              
120             B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
121             and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
122             I<BMC Bioinformatics> B<12>:63.
123             L<http://dx.doi.org/10.1186/1471-2105-12-63>
124              
125             =cut
126              
127             1;