Phylo/Unparsers/Nwmsrdf.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 119 CC 11 SCAR 45.1
CC
11
Cov
0%
_to_string
132.0
BEGIN
2.0
BEGIN
2.0
CRAP 91.4
1 package Bio::Phylo::Unparsers::Nwmsrdf;
2 use strict;
3 use warnings;
4 use base 'Bio::Phylo::Unparsers::Abstract';
5 use Bio::Phylo::Util::Exceptions 'throw';
6 use Bio::Phylo::Util::CONSTANT qw':objecttypes looks_like_object';
7  
8 =head1 NAME
9  
10 Bio::Phylo::Unparsers::Nwmsrdf - Serializer used by Bio::Phylo::IO, no serviceable parts inside
11  
12 =head1 DESCRIPTION
13  
14 This unparser produces multistate character state matrices for the "Network"
15 (L<http://www.fluxus-engineering.com/sharenet.htm>) program. These files by Network's
16 conventions have the .rdf extension, which has nothing to do with RDF. The matrices are
17 represented as follows:
18  
19 =over
20  
21 =item Only variable columns are shown. The header includes the column number.
22  
23 =item The end of each character row has the frequency of the haplotype. By default this
24 is 1, other values can be specified by adding an annotation to the row in question:
25  
26 $row->set_meta_object( 'bp:haplotype_frequency' => 2 );
27  
28 =item The bottom of the file lists the weight of each column. By default this is 10, other
29 values can be specified by adding a weight to the character:
30  
31 $char->set_weight( 15 );
32  
33 =item Taxon names must be variable in the first 6 characters as they are truncated to
34 this length.
35  
36 =back
37  
38 =cut
39  
40 sub _to_string {
41 my $self = shift;
42
43 # get the matrix object
44 my $obj = $self->{'PHYLO'};
45 my $matrix;
46 eval { $matrix = $obj if looks_like_object $obj, _MATRIX_; };
47 if ($@) {
48 undef($@);
49 eval {
50 ($matrix) = @{ $obj->get_matrices }
51 if looks_like_object $obj, _PROJECT_;
52 };
53 if ( $@ or not $matrix ) {
54 throw 'ObjectMismatch' => 'Invalid object!';
55 }
56 }
57  
58 # build the matrix hash, record column numbers
59 my $nchar = $matrix->get_nchar;
60 my $raw = $matrix->get_raw;
61 my %result = map { $_->[0] => '' } @{ $raw };
62 my @cols;
63 my ( $missing, $gap ) = ( $matrix->get_missing, $matrix->get_gap );
64 for my $i ( 1 .. $nchar ) {
65 my %seen;
66 my %state;
67 for my $row ( @{ $raw } ) {
68 my $state = $row->[$i];
69 $state{$row->[0]} = $state;
70 $seen{$state}++ if $state ne $missing and $state ne $gap;
71 }
72 if ( scalar(keys(%seen)) > 1 ) {
73 push @cols, $i;
74 $result{$_} .= $state{$_} for keys %state;
75 }
76 }
77
78 # build the header with column numbers
79 my $result = '';
80 my @header;
81 push @header, ' ' x 7 for 1 .. 6;
82 for my $c ( @cols ) {
83 my @parts = split //, $c;
84 for my $i ( 0 .. $#header ) {
85 my $val = defined($parts[$i]) ? $parts[$i] : ' ';
86 $header[$i] .= $val;
87 }
88 }
89 $result .= join "\n", @header;
90 $result .= "\n";
91
92 # build the matrix
93 for my $name ( map { $_->[0] } @{ $raw } ) {
94 if ( length($name) <= 6 ) {
95 $result .= $name . ( ' ' x ( 7 - length($name) ) );
96 }
97 else {
98 $result .= substr( $name, 0, 6 ) . ' ';
99 }
100 $result .= $result{$name} . ' ';
101 my $freq = $matrix->get_by_name($name)->get_meta_object('bp:haplotype_frequency') || 1;
102 $result .= $freq . "\n";
103 }
104 $result .= "\n";
105
106 # build the character weights
107 my $characters = $matrix->get_characters;
108 my @weights;
109 for my $i ( @cols ) {
110 my $weight = $characters->get_by_index($i-1)->get_weight;
111 $weight = 10 if not defined $weight;
112 push @weights, $weight;
113 }
114 for ( my $i = 0; $i <= $#weights; $i += 124 ) {
115 my $max = $i+124 < $#weights ? $i+124 : $#weights;
116 $result .= join '', @weights[$i..$max];
117 $result .= "\n";
118 }
119 return $result;
120 }
121  
122 # podinherit_insert_token
123  
124 =head1 SEE ALSO
125  
126 There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
127 for any user or developer questions and discussions.
128  
129 =over
130  
131 =item L<Bio::Phylo::IO>
132  
133 The nwmsrdf unparser is called by the L<Bio::Phylo::IO> object.
134 Look there to learn how to create phylip formatted files.
135  
136 =item L<Bio::Phylo::Manual>
137  
138 Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>.
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;