Phylo/Parsers/Figtree.pm

filter: Statementstmt 96.8% 91 / 94 Branchbran 83.3% 20 / 24 Conditioncond 88.8% 8 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 93.7% 134 / 143 CC 17 SCAR 18.6
CC
17
Cov
94%
__ANON__
9.3
_parse
9.0
_post_process
1.0
CRAP 6.4
« Phylo/PhyloWS/Service/UbioNameBank.pm ↑ Summary Phylo/Models/Substitution/Dna/HKY85.pm »
1 package Bio::Phylo::Parsers::Figtree;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use base 'Bio::Phylo::Parsers::Abstract';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Bio::Phylo::Util::CONSTANT qw':namespaces :objecttypes';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Bio::Phylo::Factory;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Bio::Phylo::IO 'parse_tree';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Bio::Phylo::Util::Logger ':levels';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 my $fac = Bio::Phylo::Factory->new;
11 2 my $log = Bio::Phylo::Util::Logger->new;
12 2 my $ns = _NS_FIGTREE_;
13 2 my $pre = 'fig';
14  
15 =head1 NAME
16  
17 Bio::Phylo::Parsers::Figtree - Parser used by Bio::Phylo::IO, no serviceable parts inside
18  
19 =head1 DESCRIPTION
20  
21 This module parses annotated trees in NEXUS format as interpreted by FigTree
22 (L<http://tree.bio.ed.ac.uk/software/figtree/>), i.e. trees where nodes have
23 additional 'hot comments' attached to them in the tree description. The
24 implementation assumes syntax as follows:
25  
26 [&minmax={0.1231,0.3254},rate=0.0075583392800736]
27
28 I.e. the first token inside the comments is an ampersand, the annotations are
29 comma-separated key/value pairs, where ranges are between curly parentheses.
30  
31 The annotations are stored as meta objects, e.g.:
32  
33 $node->get_meta_object('fig:rate'); # 0.0075583392800736
34 $node->get_meta_object('fig:minmax_min'); # 0.1231
35 $node->get_meta_object('fig:minmax_max'); # 0.3254
36  
37 Annotations that have non-alphanumerical symbols in them will have these removed
38 from them. For example, C<rate_95%_HPD={}> becomes two annotations:
39 C<rate_95_HPD_min> and C<rate_95_HPD_max>.
40  
41 =cut
42  
43 2 ▶sub _parse {
Subroutine _parse: called CC=9 SCAR=22.0
44 2 my $self = shift;
45 2 my $fh = $self->_handle;
46 2 my $forest = $fac->create_forest;
47 2 $forest->set_namespaces( $pre => $ns );
48 2 my $tree_block;
49 my $tree_string;
50 2 my %translate;
51 2 ▶ while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 53 2 55
52 53 ▶ $tree_block++ if /BEGIN TREES;/i;
Branch: 1 branch
BranchTrueFalseTotal
if /BEGIN TREES;/i 2 51 53
53 53 ▶ if ( /^\s*TREE (\S+) = \[&([RU])\] (.+)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*TREE (\S+) = \[&([RU])\] (.+)$/i) 2 51 53
54 2 my ( $name, $rooted, $newick ) = ( $1, $2, $3 );
55 2 $tree_string++;
56 2 my $tree = parse_tree(
57 '-format' => 'newick',
58 '-string' => $newick,
59 '-ignore_comments' => 1,
60 );
61 2 ▶ $tree->set_as_unrooted if $rooted eq 'U';
Branch: 1 branch
BranchTrueFalseTotal
if $rooted eq "U" 1 1 2
62 2 $tree->set_name( $name );
63 2 $self->_post_process( $tree );
64 2 for my $tip ( @{ $tree->get_terminals } ) {
65 122 my $name = $tip->get_name;
66 122 $tip->set_name( $translate{$name} );
67 }
68 2 $forest->insert($tree);
69 }
70 53 ▶ if ( $tree_block and not $tree_string and /\s+(\d+)\s+(.+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree_block and not $tree_string and /\s+(\d+)\s+(.+)/) 18 35 53
Condition100%6/6
$tree_block and not $tree_string and /\s+(\d+)\s+(.+)/
!ll&&!rl&&r
31418
$tree_block and not $tree_string
!ll&&!rl&&r
27422
MC/DC100%3/3
$tree_block and not $tree_string and /\s+(\d+)\s+(.+)/
$tree_block not $tree_string /\s+(\d+)\s+(.+)/
Truth table100%4/4
A and not(B) and C
A= $tree_blockB= not $tree_stringC= /\s+(\d+)\s+(.+)/
ABCresult
0XX0
10X0
1100
1111
71 18 my ( $id, $name ) = ( $1, $2 );
72 18 $name =~ s/[,;]$//;
73 18 $translate{$id} = $name;
74 }
75 }
76 2 return $forest;
77 }
78  
79 2 ▶sub _post_process {
Subroutine _post_process: called CC=1 SCAR=0.0
80 2 my ( $self, $tree ) = @_;
81 2 $log->debug("going to post-process tree");
82 156 ▶ $tree->visit(sub{
Subroutine __ANON__: called CC=9 SCAR=22.3
83 156 my $n = shift;
84 156 my $name = $n->get_name;
85 156 $name =~ s/\\//g;
86 156 $log->debug("name: $name");
87 156 ▶ if ( $name =~ /\[/ and $name =~ /^([^\[]*?)\[(.+?)\]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /\[/ and $name =~ /^([^\[]*?)\[(.+?)\]$/) 52 104 156
Condition66%2/3
$name =~ /\[/ and $name =~ /^([^\[]*?)\[(.+?)\]$/
!ll&&!rl&&r
104052
MC/DC50%1/2
$name =~ /\[/ and $name =~ /^([^\[]*?)\[(.+?)\]$/
$name =~ /\[/ $name =~ /^([^\[]*?)\[(.+?)\]$/
Truth table66%2/3
A and B
A= $name =~ /\[/B= $name =~ /^([^\[]*?)\[(.+?)\]$/
ABresult
0X0
100
111
88 52 my ( $trimmed, $comments ) = ( $1, $2 );
89 52 $n->set_name( $trimmed );
90 52 $log->debug("trimmed name: $trimmed");
91
92 # "hot comments" start with ampersand. ignore if not.
93 52 ▶ if ( $comments =~ /^&(.+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comments =~ /^&(.+)/) { } 52 0 52
94 52 $log->debug("hot comments: $comments");
95 52 $comments = $1;
96
97 # string needs to be fully eaten up
98 52 ▶ COMMENT: while( my $old_length = length($comments) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $old_length = length $comments) 463 52 515
99
100 # grab the next key
101 463 ▶ if ( $comments =~ /^(.+?)=/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comments =~ /^(.+?)=/) 463 0 463
102 463 my $key = $1;
103
104 # remove the key and the =
105 463 $comments =~ s/^\Q$key\E=//;
106 463 $key =~ s/\%//;
107
108 # value is a comma separated range
109 463 ▶ if ( $comments =~ /^{([^}]+)}/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($comments =~ /^{([^}]+)}/) { } 206 257 463
elsif ($comments =~ /^([^,]+)/) { } 257 0 257
110 206 my $value = $1;
111 206 my ( $min, $max ) = split /,/, $value;
112 206 _meta( $n, "${key}_min" => $min );
113 206 _meta( $n, "${key}_max" => $max );
114 206 $log->debug("$key: $min .. $max");
115
116 # remove the range
117 206 $value = "{$value}";
118 206 $comments =~ s/^\Q$value\E//;
119 }
120
121 # value is a scalar
122 elsif ( $comments =~ /^([^,]+)/ ) {
123 257 my $value = $1;
124 257 _meta( $n, $key => $value );
125 257 $comments =~ s/^\Q$value\E//;
126 257 $log->debug("$key: $value");
127 }
128
129 # remove trailing comma, if any
130 463 $comments =~ s/^,//;
131 }
132 463 ▶ if ( $old_length == length($comments) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_length == length $comments) 0 463 463
133 0 $log->warn("couldn't parse newick comment: $comments");
134 0 last COMMENT;
135 }
136 }
137 }
138 else {
139 0 $log->debug("not hot: $comments");
140 }
141 }
142 2 });
143 }
144  
145 669 ▶sub _meta {
Subroutine _meta: called CC=1 SCAR=0.0
146 669 my ( $node, $key, $value ) = @_;
147 #if ( $key =~ /[()+]/ ) {
148 669 $log->info("cleaning up CURIE candidate $key");
149 669 $key =~ s/\(/_/g;
150 669 $key =~ s/\)/_/g;
151 669 $key =~ s/\+/_/g;
152 669 $key =~ s/\!//;
153 #}
154 669 $node->add_meta(
155 $fac->create_meta( '-triple' => { "${pre}:${key}" => $value } )
156 );
157 }
158  
159  
160 # podinherit_insert_token
161  
162 =head1 SEE ALSO
163  
164 There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
165 for any user or developer questions and discussions.
166  
167 =over
168  
169 =item L<Bio::Phylo::IO>
170  
171 The figtree parser is called by the L<Bio::Phylo::IO> object.
172 Look there to learn how to parse phylogenetic data files in general.
173  
174 =item L<Bio::Phylo::Manual>
175  
176 Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>
177  
178 =back
179  
180 =head1 CITATION
181  
182 If you use Bio::Phylo in published research, please cite it:
183  
184 B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
185 and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
186 I<BMC Bioinformatics> B<12>:63.
187 L<http://dx.doi.org/10.1186/1471-2105-12-63>
188  
189 =cut
190  
191 2 1;
« Phylo/PhyloWS/Service/UbioNameBank.pm ↑ Summary Phylo/Models/Substitution/Dna/HKY85.pm »