Phylo/Unparsers/Figtree.pm

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