File Coverage

blib/lib/Bio/Phylo/Unparsers/Nexus.pm
Criterion Covered Total %
statement 27 63 42.8
branch 9 36 25.0
condition 4 12 33.3
subroutine 5 5 100.0
pod n/a
total 45 116 38.7


line stmt bran cond sub pod time code
1             package Bio::Phylo::Unparsers::Nexus;
2 2     2   13 use strict;
  2         3  
  2         54  
3 2     2   8 use base 'Bio::Phylo::Unparsers::Abstract';
  2         4  
  2         529  
4 2     2   12 use Bio::Phylo::Util::CONSTANT ':objecttypes';
  2         5  
  2         383  
5 2     2   11 use Bio::Phylo::Util::Exceptions 'throw';
  2         4  
  2         749  
6              
7             =head1 NAME
8              
9             Bio::Phylo::Unparsers::Nexus - Serializer used by Bio::Phylo::IO, no serviceable parts inside
10              
11             =head1 DESCRIPTION
12              
13             This module turns a L<Bio::Phylo::Matrices::Matrix> object into a nexus
14             formatted matrix. It is called by the L<Bio::Phylo::IO> facade, don't call it
15             directly. You can pass the following additional arguments to the unparse call:
16            
17             # an array reference of matrix, forest and taxa objects:
18             -phylo => [ $block1, $block2 ]
19            
20             # the arguments that can be passed for matrix objects,
21             # refer to Bio::Phylo::Matrices::Matrix::to_nexus:
22             -matrix_args => {}
23              
24             # the arguments that can be passed for forest objects,
25             # refer to Bio::Phylo::Forest::to_nexus:
26             -forest_args => {}
27              
28             # the arguments that can be passed for taxa objects,
29             # refer to Bio::Phylo::Taxa::to_nexus:
30             -taxa_args => {}
31            
32             OR:
33            
34             # for backward compatibility:
35             -phylo => $matrix
36              
37             =begin comment
38              
39             Type : Wrapper
40             Title : _to_string($matrix)
41             Usage : $nexus->_to_string($matrix);
42             Function: Stringifies a matrix object into
43             a nexus formatted table.
44             Alias :
45             Returns : SCALAR
46             Args : Bio::Phylo::Matrices::Matrix;
47              
48             =end comment
49              
50             =cut
51              
52             sub _to_string {
53 2     2   3 my $self = shift;
54 2         8 my $blocks = $self->{'PHYLO'};
55 2         4 my $nexus = "#NEXUS\n";
56 2         3 my $type;
57 2         2 eval { $type = $blocks->_type };
  2         8  
58              
59             # array?
60 2 50 33     30 if ($@) {
    50 33        
    50 33        
    50 33        
    50          
61 0         0 for my $block (@$blocks) {
62 0         0 eval { $type = $block->_type };
  0         0  
63 0         0 my %args;
64 0 0       0 if ( $type == _FOREST_ ) {
    0          
    0          
    0          
65 0 0       0 if ( exists $self->{'FOREST_ARGS'} ) {
66 0         0 %args = %{ $self->{'FOREST_ARGS'} };
  0         0  
67             }
68             }
69             elsif ( $type == _TAXA_ ) {
70 0 0       0 if ( exists $self->{'TAXA_ARGS'} ) {
71 0         0 %args = %{ $self->{'TAXA_ARGS'} };
  0         0  
72             }
73             }
74             elsif ( $type == _MATRIX_ ) {
75 0 0       0 if ( exists $self->{'MATRIX_ARGS'} ) {
76 0         0 %args = %{ $self->{'MATRIX_ARGS'} };
  0         0  
77             }
78             }
79             elsif ($@) {
80 0         0 throw 'ObjectMismatch' => "Can't unparse this object: $blocks";
81             }
82 0         0 $nexus .= $block->to_nexus(%args);
83             }
84             }
85            
86             # taxa?
87             elsif ( defined $type and $type == _TAXA_ ) {
88 0         0 my %args;
89 0 0       0 if ( exists $self->{'TAXA_ARGS'} ) {
90 0         0 %args = %{ $self->{'TAXA_ARGS'} };
  0         0  
91             }
92 0         0 $nexus .= $blocks->to_nexus(%args);
93             }
94            
95             # matrix?
96             elsif ( defined $type and $type == _MATRIX_ ) {
97 0         0 my %args;
98 0 0       0 if ( exists $self->{'MATRIX_ARGS'} ) {
99 0         0 %args = %{ $self->{'MATRIX_ARGS'} };
  0         0  
100             }
101 0         0 $nexus .= $blocks->to_nexus(%args);
102             }
103            
104             # forest?
105             elsif ( defined $type and $type == _FOREST_ ) {
106 0         0 my %args;
107 0 0       0 if ( exists $self->{'FOREST_ARGS'} ) {
108 0         0 %args = %{ $self->{'FOREST_ARGS'} };
  0         0  
109             }
110 0         0 $nexus .= $blocks->to_nexus(%args);
111             }
112              
113             # project?
114             elsif ( defined $type and $type == _PROJECT_ ) {
115 2         3 my ( %farg, %marg, %targ );
116 2 50       6 if ( exists $self->{'TAXA_ARGS'} ) {
117 0         0 %targ = %{ $self->{'TAXA_ARGS'} };
  0         0  
118             }
119 2 50       4 if ( exists $self->{'MATRIX_ARGS'} ) {
120 0         0 %marg = %{ $self->{'MATRIX_ARGS'} };
  0         0  
121             }
122 2 100       18 if ( exists $self->{'FOREST_ARGS'} ) {
123 1         2 %farg = %{ $self->{'FOREST_ARGS'} };
  1         4  
124             }
125 2         10 $nexus = $blocks->to_nexus( %farg, %marg, %targ );
126             }
127              
128             # wrong!
129             else {
130 0         0 throw 'ObjectMismatch' => "Can't unparse this object: $blocks";
131             }
132 2         8 return $nexus;
133             }
134              
135             # podinherit_insert_token
136              
137             =head1 SEE ALSO
138              
139             There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
140             for any user or developer questions and discussions.
141              
142             =over
143              
144             =item L<Bio::Phylo::IO>
145              
146             The nexus serializer is called by the L<Bio::Phylo::IO> object.
147              
148             =item L<Bio::Phylo::Manual>
149              
150             Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>.
151              
152             =back
153              
154             =head1 CITATION
155              
156             If you use Bio::Phylo in published research, please cite it:
157              
158             B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
159             and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
160             I<BMC Bioinformatics> B<12>:63.
161             L<http://dx.doi.org/10.1186/1471-2105-12-63>
162              
163             =cut
164              
165             1;