| 1 | package Bio::Phylo::Unparsers::Hennig86; | ||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use base 'Bio::Phylo::Unparsers::Abstract'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use Bio::Phylo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use Bio::Phylo::Util::CONSTANT qw'/looks_like/ :objecttypes'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | use Bio::Phylo::Util::Exceptions 'throw'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 1 | my $MATRIX = _MATRIX_; | |||||||||||||||||||
| 10 | 1 | my $PROJECT = _PROJECT_; | |||||||||||||||||||
| 11 | 1 | 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 | 3 | ▶ | sub _to_string { | ||||||||||||||||||
|
Subroutine
_to_string: called CC=4 SCAR=17.3
| |||||||||||||||||||||
| 49 | 3 | my $self = shift; | |||||||||||||||||||
| 50 | 3 | my $obj = $self->{'PHYLO'}; | |||||||||||||||||||
| 51 | 3 | my $matrix; | |||||||||||||||||||
| 52 | 3 | ▶ | if ( looks_like_implementor $obj, '_type' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | 3 | ▶ | if ( $obj->_type == $MATRIX ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 54 | 3 | $matrix = $obj; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | elsif ( $obj->_type == $PROJECT ) { | ||||||||||||||||||||
| 57 | 0 | ($matrix) = @{ $obj->get_items(_MATRIX_) }; | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | else { | ||||||||||||||||||||
| 60 | 0 | throw 'ObjectMismatch' => "Can't serialize ".ref($obj)." objects as Hennig86"; | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | 3 | return $self->_serialize_matrix($matrix); | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | else { | ||||||||||||||||||||
| 65 | 0 | throw 'ObjectMismatch' => "Can't serialize supplied argument as Hennig86"; | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 3 | ▶ | sub _serialize_matrix { | ||||||||||||||||||
|
Subroutine
_serialize_matrix: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 70 | 3 | my ( $self, $matrix ) = @_; | |||||||||||||||||||
| 71 | 3 | my $hennig86 = $self->_create_header($matrix); | |||||||||||||||||||
| 72 | 3 | my $to = $matrix->get_type_object; | |||||||||||||||||||
| 73 | 3 | for my $row ( @{ $matrix->get_entities } ) { | |||||||||||||||||||
| 74 | 15 | $hennig86 .= $row->get_nexus_name . "\t"; | |||||||||||||||||||
| 75 | 15 | my @char = $row->get_char; | |||||||||||||||||||
| 76 | 15 | my @encoded; | |||||||||||||||||||
| 77 | 15 | for my $c ( @char ) { | |||||||||||||||||||
| 78 | 65 | ▶ | if ( $to->is_ambiguous($c) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 7 | my @states = @{ $to->get_states_for_symbol($c) }; | |||||||||||||||||||
| 80 | 7 | push @encoded, '[' . $to->join(\@states) . ']'; | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | else { | ||||||||||||||||||||
| 83 | 58 | push @encoded, $c; | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | 15 | $hennig86 .= $to->join(\@encoded) . "\n"; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | 3 | return $hennig86 .= ";\n"; | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 3 | ▶ | sub _create_header { | ||||||||||||||||||
|
Subroutine
_create_header: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 92 | 3 | my ( $self, $matrix ) = @_; | |||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 3 | my $comment = "Hennig86 matrix written by ".ref($self)." ".Bio::Phylo->VERSION." on ".localtime(); | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | # calculate nstates | ||||||||||||||||||||
| 97 | 3 | my $nstates = scalar keys %{ $matrix->calc_state_counts }; | |||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | # calculate ntax and nchar | ||||||||||||||||||||
| 100 | 3 | my ( $ntax, $nchar ) = ( $matrix->get_ntax, $matrix->get_nchar ); | |||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | # map type to hennig86 tokens | ||||||||||||||||||||
| 103 | 3 | my $type = lc $matrix->get_type; | |||||||||||||||||||
| 104 | 3 | ▶ | my $hennig86type = $typemap{ $type } || throw 'BadFormat' => "Can't write $type matrices to Hennig86"; | ||||||||||||||||||
|
Condition33%1/3
$typemap{$type} || &throw('BadFormat', "Can't write $type matrices to Hennig86")
MC/DC0%0/2
$typemap{$type} || &throw('BadFormat', "Can't write $type matrices to Hennig86")
$typemap{$type}
&throw('BadFormat', "Can't write $type matrices to Hennig86")
Truth table33%1/3
A || B
A= $typemap{$type}B= &throw('BadFormat', "Can't write $type matrices to Hennig86")
| |||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 3 | my $template = << 'TEMPLATE'; | |||||||||||||||||||
| 107 | nstates %d | ||||||||||||||||||||
| 108 | xread | ||||||||||||||||||||
| 109 | '%s' | ||||||||||||||||||||
| 110 | %d %d | ||||||||||||||||||||
| 111 | & [%s] | ||||||||||||||||||||
| 112 | TEMPLATE | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 3 | 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 | 1; | |||||||||||||||||||