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