File Coverage

blib/lib/Bio/Phylo/Unparsers/Pagel.pm
Criterion Covered Total %
statement 30 35 85.7
branch 5 6 83.3
condition n/a
subroutine 5 5 100.0
pod n/a
total 40 46 86.9


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