lib/Bio/NEXUS/HistoryBlock.pm

filter: Statementstmt 63.2% 74 / 117 Branchbran 33.3% 8 / 24 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 76.4% 13 / 17 Podpod 100% 4 / 4 totaltotal 58.8% 100 / 170 CC 20 SCAR 20.2
CC
20
Cov
57%
equals
20.0
rename_otus
12.0
_write
12.0
CRAP 7.5
« lib/Bio/NEXUS/Matrix.pm ↑ Summary lib/Bio/NEXUS/Block.pm »
1 #######################################################################
2 # HistoryBlock.pm
3 #######################################################################
4 # Author: Chengzhi Liang, Justin Reese, Thomas Hladish
5 # $Id: HistoryBlock.pm,v 1.28 2007/09/21 23:09:09 rvos Exp $
6  
7 #################### START POD DOCUMENTATION ##########################
8  
9 =head1 NAME
10  
11 Bio::NEXUS::HistoryBlock - Represents a HISTORY block of a NEXUS file
12  
13 =head1 SYNOPSIS
14  
15 $block_object = new Bio::NEXUS::HistoryBlock('history', $block, $verbose);
16  
17 =head1 DESCRIPTION
18  
19 This is a class representing a history block in NEXUS file
20  
21 =head1 FEEDBACK
22  
23 All feedback (bugs, feature enhancements, etc.) are greatly appreciated.
24  
25 =head1 AUTHORS
26  
27 Chengzhi Liang (liangc@umbi.umd.edu)
28 Justin Reese
29 Tom Hladish (tjhladish at yahoo)
30  
31 =head1 VERSION
32  
33 $Revision: 1.28 $
34  
35 =head1 METHODS
36  
37 =cut
38  
39 package Bio::NEXUS::HistoryBlock;
40  
41 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 #use Data::Dumper; # XXX this is not used, might as well not import it!
43 #use Carp; # XXX this is not used, might as well not import it!
44 35 ▶use Bio::NEXUS::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 35 ▶use Bio::NEXUS::TaxUnitSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 35 ▶use Bio::NEXUS::Block;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 35 ▶use Bio::NEXUS::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 35 ▶use Bio::NEXUS::Tree;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 35 ▶use Bio::NEXUS::Util::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 35 ▶use Bio::NEXUS::Util::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 35 ▶use vars qw(@ISA $VERSION $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
52 35 ▶use Bio::NEXUS; $VERSION = $Bio::NEXUS::VERSION;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 35 @ISA = qw(Bio::NEXUS::CharactersBlock Bio::NEXUS::TreesBlock);
55 35 my $logger = Bio::NEXUS::Util::Logger->new();
56  
57 =head2 new
58  
59 Title : new
60 Usage : block_object = new Bio::NEXUS::HistoryBlock($block_type, $commands, $verbose);
61 Function: Creates a new Bio::NEXUS::HistoryBlock object
62 Returns : Bio::NEXUS::HistoryBlock object
63 Args : type (string), the commands/comments to parse (array ref), and a verbose flag (0 or 1; optional)
64 Comments:
65  
66 =cut
67  
68 2 ▶sub new {
Subroutine new: called CC=4 SCAR=15.0
Pod documented
69 2 my ( $class, $type, $commands, $verbose ) = @_;
70 2 ▶ if ( not $type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 0 2 2
71 0 ( $type = lc $class ) =~ s/Bio::NEXUS::(.+)Block/$1/i;
72 }
73 2 my $self = {
74 'type' => $type
75 };
76 2 bless $self, $class;
77 2 $self->{'otuset'} = new Bio::NEXUS::TaxUnitSet();
78 2 ▶ if ( ( defined $commands ) and @$commands ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $commands and @$commands) 2 0 2
Condition33%1/3
defined $commands and @$commands
!ll&&!rl&&r
002
MC/DC0%0/2
defined $commands and @$commands
defined $commands @$commands
Truth table33%1/3
A and B
A= defined $commandsB= @$commands
ABresult
0X0
100
111
79 2 $self->_parse_block( $commands, $verbose )
80 }
81 2 return $self;
82 }
83  
84 =begin comment
85  
86 Name :_parse_nodelabels
87 Usage : $block->nodelabels($label_text);
88 Function: Parse node labels like taxlabels in taxa block
89 Returns : Labels as the array reference
90 Args : $labels_text as string
91  
92 =end comment
93  
94 =cut
95  
96 0 ▶sub _parse_nodelabels {
Subroutine _parse_nodelabels: not called CC=1 SCAR=6.9
97 0 my ( $self, $labeltext ) = @_;
98 0 my @labels = split( /\s+/, $labeltext );
99 0 return \@labels;
100 }
101  
102 =head2 equals
103  
104 Name : equals
105 Usage : $block->equals($another);
106 Function: compare if two Block objects are equal
107 Returns : boolean
108 Args : a Block object
109  
110 =cut
111  
112 0 ▶sub equals {
Subroutine equals: not called CC=4 SCAR=30.0
Pod documented
113 0 my ( $self, $block ) = @_;
114 0 ▶ if ( !Bio::NEXUS::Block::equals( $self, $block ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Bio::NEXUS::Block::equals($self, $block)) 0 0 0
115 0 $logger->warn("First equals failed");
116 0 return 0;
117 }
118 0 my $historytree1 = $self->get_tree();
119 0 my $historytree2 = $block->get_tree();
120 0 ▶ if ( !$historytree1->equals($historytree2) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($historytree1->equals($historytree2)) 0 0 0
121 0 $logger->warn("Trees do not appear to be the same, failing equals");
122 0 return 0;
123 }
124  
125 # check otus
126  
127 0 ▶ if ( !$self->get_otuset()->equals( $block->get_otuset() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->get_otuset->equals($block->get_otuset)) 0 0 0
128 0 $logger->warn("otusets do not appear to be the same, failing equals");
129 0 return 0;
130 }
131  
132 0 return 1;
133 }
134  
135 =head2 rename_otus
136  
137 Name : rename_otus
138 Usage : $nexus->rename_otus(\%translation);
139 Function: rename all OTUs
140 Returns : a new nexus object with new OTU names
141 Args : a ref to hash based on OTU name pairs
142  
143 =cut
144  
145 0 ▶sub rename_otus {
Subroutine rename_otus: not called CC=3 SCAR=24.8
Pod documented
146 0 my ( $self, $translation ) = @_;
147 0 for my $parent (@ISA) {
148 0 ▶ if ( my $coderef = $self->can( $parent . "::rename_otus" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $coderef = $self->can($parent . '::rename_otus')) 0 0 0
149 0 $self->$coderef($translation);
150 }
151 }
152 }
153  
154 =head2 add_otu_clone
155  
156 Title : add_otu_clone
157 Usage : ...
158 Function: ...
159 Returns : ...
160 Args : ...
161  
162 =cut
163  
164 2 ▶sub add_otu_clone {
Subroutine add_otu_clone: called CC=8 SCAR=20.9
Pod documented
165 2 my ( $self, $original_otu_name, $copy_otu_name ) = @_;
166 # print "Warning: Bio::NEXUS::HistoryBlock::add_otu_clone() method not fully implemented\n";
167 # add the clone to the taxlabels list
168 2 $self->add_taxlabel($copy_otu_name);
169  
170 # add the clone to the list
171 2 my @otus = @{ $self->{'otuset'}->get_otus() };
172 2 for my $otu (@otus) {
173 15 ▶ if (defined $otu) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $otu) 15 0 15
174 15 ▶ if ($otu->get_name() eq $original_otu_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($otu->get_name eq $original_otu_name) 2 13 15
175 2 my $otu_clone = $otu->clone();
176 2 $otu_clone->set_name($copy_otu_name);
177 2 $self->{'otuset'}->add_otu($otu_clone);
178 }
179 }
180 }
181
182 # . iterate through all trees:
183 2 for my $tree ( @{ $self->{'blockTrees'} }) {
184 # . find the original node
185 # if not found, something must be done !
186 2 my $original_node = $tree->find($original_otu_name);
187 2 ▶ if (! defined $original_node) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $original_node) 0 2 2
188 0 $logger->info("TreesBlock::add_otu_clone(): original otu [$original_otu_name] was not found");
189 }
190 # . clone the node
191 2 my $cloned_node = $original_node->clone();
192 # . rename the new node
193 2 $cloned_node->set_name($copy_otu_name);
194
195 # find the parent of the original node, add to it a new
196 # child that will be parent of both original and
197 # clone nodes. Remove the original node from the
198 # list of children of its original parent
199 2 my $original_parent = $original_node->get_parent();
200
201 2 for my $child ( @{ $original_parent->get_children() }) {
202 # print "Child name: ", $child->get_name(), "\n";
203 2 ▶ if ($child->get_name() eq $original_otu_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->get_name eq $original_otu_name) 2 0 2
204 2 my $new_parent = new Bio::NEXUS::Node();
205  
206 2 $new_parent->set_length($original_node->get_length());
207
208 2 $cloned_node->set_length(0);
209 2 $original_node->set_length(0);
210
211 2 $new_parent->add_child($cloned_node);
212 2 $cloned_node->set_parent_node($new_parent);
213 2 $new_parent->add_child($original_node);
214 2 $original_node->set_parent_node($new_parent);
215  
216 2 $child = $new_parent;
217 2 $new_parent->set_parent_node($original_parent);
218 2 last;
219 }
220 }
221 }
222 }
223  
224 =begin comment
225  
226 Name : _write
227 Usage : $block->_write();
228 Function: Writes NEXUS block containing history data
229 Returns : none
230 Args : file name (string)
231  
232 =end comment
233  
234 =cut
235  
236 0 ▶sub _write {
Subroutine _write: not called CC=3 SCAR=24.8
237 0 my ( $self, $fh, $verbose ) = @_;
238 0 ▶ $fh ||= \*STDOUT;
Condition0%0/2
$fh ||= \*STDOUT
l!l
00
MC/DC0%0/1
$fh ||= \*STDOUT
$fh
Truth table0%0/2
A
A= $fh
Aresult
11
00
239  
240 0 Bio::NEXUS::Block::_write( $self, $fh );
241 0 $self->_write_dimensions( $fh, $verbose );
242 0 $self->_write_format( $fh, $verbose );
243 0 $self->_write_labels( $fh, $verbose );
244 0 print $fh "\tNODELABELS ";
245 0 for my $label ( @{ $self->get_otuset->get_otu_names } ) {
246 0 print $fh _nexus_formatted($label) . ' ';
247 }
248 0 print $fh ";\n";
249 0 $self->_write_matrix( $fh, $verbose );
250 0 $self->_write_trees( $fh, $verbose );
251 0 print $fh "END;\n";
252 }
253  
254 2 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=21.1
255 2 ▶ return if $AUTOLOAD =~ /DESTROY$/;
Branch: 1 branch
BranchTrueFalseTotal
if $Bio::NEXUS::HistoryBlock::AUTOLOAD =~ /DESTROY$/ 2 0 2
256 0 my $package_name = __PACKAGE__ . '::';
257  
258 # The following methods are deprecated and are temporarily supported
259 # via a warning and a redirection
260 0 my %synonym_for = (
261  
262 # "${package_name}parse" => "${package_name}_parse_tree", # example
263 );
264  
265 0 ▶ if ( defined $synonym_for{$AUTOLOAD} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $synonym_for{$Bio::NEXUS::HistoryBlock::AUTOLOAD}) { } 0 0 0
266 0 $logger->warn("$AUTOLOAD() is deprecated; use $synonym_for{$AUTOLOAD}() instead");
267 0 goto &{ $synonym_for{$AUTOLOAD} };
268 }
269 else {
270 0 Bio::NEXUS::Util::Exceptions::UnknownMethod->throw(
271 'error' => "ERROR: Unknown method $AUTOLOAD called"
272 );
273 }
274 }
275  
276 35 1;
« lib/Bio/NEXUS/Matrix.pm ↑ Summary lib/Bio/NEXUS/Block.pm »