| 1 | package Bio::Phylo::Unparsers::Figtree; | ||||||||||||||
| 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::Nexus'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 1 | ▶ | use Bio::Phylo::Util::Logger ':levels'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | ▶ | use Bio::Phylo::Util::Exceptions 'throw'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 1 | ▶ | use Bio::Phylo::Util::CONSTANT qw':objecttypes :namespaces'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 1 | ▶ | use Data::Dumper; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 1 | my $log = Bio::Phylo::Util::Logger->new; | |||||||||||||
| 11 | 1 | my $ns = _NS_FIGTREE_; | |||||||||||||
| 12 | 1 | my $pre = 'fig'; | |||||||||||||
| 13 | |||||||||||||||
| 14 | =head1 NAME | ||||||||||||||
| 15 | |||||||||||||||
| 16 | Bio::Phylo::Unparsers::Figtree - Serializer used by Bio::Phylo::IO, no serviceable parts inside | ||||||||||||||
| 17 | |||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||
| 19 | |||||||||||||||
| 20 | This module turns objects into a nexus-formatted string that uses additional | ||||||||||||||
| 21 | syntax for Figtree. It is called by the L<Bio::Phylo::IO> facade, don't call it | ||||||||||||||
| 22 | directly. You can pass the following additional arguments to the unparse call: | ||||||||||||||
| 23 | |||||||||||||||
| 24 | =begin comment | ||||||||||||||
| 25 | |||||||||||||||
| 26 | Type : Wrapper | ||||||||||||||
| 27 | Title : _to_string($obj) | ||||||||||||||
| 28 | Usage : $figtree->_to_string($obj); | ||||||||||||||
| 29 | Function: Stringifies an object into | ||||||||||||||
| 30 | a nexus/figtree formatted string. | ||||||||||||||
| 31 | Alias : | ||||||||||||||
| 32 | Returns : SCALAR | ||||||||||||||
| 33 | Args : Bio::Phylo::* | ||||||||||||||
| 34 | |||||||||||||||
| 35 | =end comment | ||||||||||||||
| 36 | |||||||||||||||
| 37 | =cut | ||||||||||||||
| 38 | |||||||||||||||
| 39 | 1 | ▶ | sub _to_string { | ||||||||||||
|
Subroutine
_to_string: called CC=1 SCAR=0.0
| |||||||||||||||
| 40 | 1 | my $self = shift; | |||||||||||||
| 41 | 1 | $self->{'FOREST_ARGS'} = { | |||||||||||||
| 42 | '-nodelabels' => \&_figtree_handler, | ||||||||||||||
| 43 | '-figtree' => 1, | ||||||||||||||
| 44 | }; | ||||||||||||||
| 45 | 1 | return $self->SUPER::_to_string(@_); | |||||||||||||
| 46 | } | ||||||||||||||
| 47 | |||||||||||||||
| 48 | 35 | ▶ | sub _figtree_handler { | ||||||||||||
|
Subroutine
_figtree_handler: called CC=7 SCAR=19.8
| |||||||||||||||
| 49 | |||||||||||||||
| 50 | # node object, translation table ID, if any | ||||||||||||||
| 51 | 35 | my ( $node, $id ) = @_; | |||||||||||||
| 52 | |||||||||||||||
| 53 | # fetch Meta objects, filter out the ones that are _NS_FIGTREE_, | ||||||||||||||
| 54 | # turn them into a hash without the fig prefix | ||||||||||||||
| 55 | 35 | my @meta = @{ $node->get_meta }; | |||||||||||||
| 56 | 637 | my %meta = map { $_->get_predicate_local => $_->get_object } | |||||||||||||
| 57 | 35 | grep { $_->get_predicate_namespace eq $ns } @meta; | |||||||||||||
| 58 | 35 | $log->debug( Dumper(\%meta) ); | |||||||||||||
| 59 | |||||||||||||||
| 60 | # there can be separate annotations that are _min and _max for | ||||||||||||||
| 61 | # the same variable name stem. We combine these into a range | ||||||||||||||
| 62 | # between curly braces. Also add % percentage symbol for 95% | ||||||||||||||
| 63 | # HPD ranges - the % symbol is disallowed in CURIEs, hence we | ||||||||||||||
| 64 | # have to bring it back here. | ||||||||||||||
| 65 | 35 | my %merged; | |||||||||||||
| 66 | 35 | KEY: for my $key ( keys %meta ) { | |||||||||||||
| 67 | 637 | ▶ | if ( $key =~ /^(.+?)_min$/ ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 68 | 206 | my $stem = $1; | |||||||||||||
| 69 | 206 | my $max_key = $stem . '_max'; | |||||||||||||
| 70 | 206 | $stem =~ s/95/95%/; | |||||||||||||
| 71 | 206 | $merged{$stem} = '{'.$meta{$key}.','.$meta{$max_key}.'}'; | |||||||||||||
| 72 | } | ||||||||||||||
| 73 | elsif ( $key =~ /^(.+?)_max$/ ) { | ||||||||||||||
| 74 | 206 | next KEY; | |||||||||||||
| 75 | } | ||||||||||||||
| 76 | else { | ||||||||||||||
| 77 | 225 | $key =~ s/95/95%/; | |||||||||||||
| 78 | 225 | $merged{$key} = $meta{$key}; | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | } | ||||||||||||||
| 81 | |||||||||||||||
| 82 | # create the concatenated annotation string | ||||||||||||||
| 83 | 35 | my $anno = '[&' . join( ',',map { $_.'='.$merged{$_} } keys %merged ) . ']'; | |||||||||||||
| 84 | |||||||||||||||
| 85 | # construct the name: | ||||||||||||||
| 86 | 35 | my $name; | |||||||||||||
| 87 | |||||||||||||||
| 88 | # case 1 - a translation table index was provided, this now replaces the name | ||||||||||||||
| 89 | 35 | ▶ | if ( defined $id ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 90 | 35 | $name = $id; | |||||||||||||
| 91 | } | ||||||||||||||
| 92 | |||||||||||||||
| 93 | # case 2 - no translation table index, use the node name | ||||||||||||||
| 94 | elsif ( defined $node->get_name ) { | ||||||||||||||
| 95 | 0 | $name = $node->get_name; | |||||||||||||
| 96 | } | ||||||||||||||
| 97 | |||||||||||||||
| 98 | # case 3 - use the empty string, to avoid uninitialized warnings. | ||||||||||||||
| 99 | else { | ||||||||||||||
| 100 | 0 | $name = ''; | |||||||||||||
| 101 | } | ||||||||||||||
| 102 | |||||||||||||||
| 103 | # append the annotation string, if we have it | ||||||||||||||
| 104 | 35 | ▶ | my $annotated = $anno ne '[&]' ? $name . $anno : $name; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 105 | 35 | $log->debug($annotated); | |||||||||||||
| 106 | 35 | return $annotated; | |||||||||||||
| 107 | } | ||||||||||||||
| 108 | |||||||||||||||
| 109 | # podinherit_insert_token | ||||||||||||||
| 110 | |||||||||||||||
| 111 | =head1 SEE ALSO | ||||||||||||||
| 112 | |||||||||||||||
| 113 | There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo> | ||||||||||||||
| 114 | for any user or developer questions and discussions. | ||||||||||||||
| 115 | |||||||||||||||
| 116 | =over | ||||||||||||||
| 117 | |||||||||||||||
| 118 | =item L<Bio::Phylo::IO> | ||||||||||||||
| 119 | |||||||||||||||
| 120 | The nexus serializer is called by the L<Bio::Phylo::IO> object. | ||||||||||||||
| 121 | |||||||||||||||
| 122 | =item L<Bio::Phylo::Manual> | ||||||||||||||
| 123 | |||||||||||||||
| 124 | Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>. | ||||||||||||||
| 125 | |||||||||||||||
| 126 | =back | ||||||||||||||
| 127 | |||||||||||||||
| 128 | =head1 CITATION | ||||||||||||||
| 129 | |||||||||||||||
| 130 | If you use Bio::Phylo in published research, please cite it: | ||||||||||||||
| 131 | |||||||||||||||
| 132 | B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen> | ||||||||||||||
| 133 | and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl. | ||||||||||||||
| 134 | I<BMC Bioinformatics> B<12>:63. | ||||||||||||||
| 135 | L<http://dx.doi.org/10.1186/1471-2105-12-63> | ||||||||||||||
| 136 | |||||||||||||||
| 137 | =cut | ||||||||||||||
| 138 | |||||||||||||||
| 139 | 1 | 1; | |||||||||||||