lib/Bio/NEXUS.pm

filter: Statementstmt 74.3% 363 / 488 Branchbran 68.3% 123 / 180 Conditioncond 71.0% 59 / 83 MC/DCmcdc 50.0% 23 / 46 Subroutinesub 76.7% 43 / 56 Podpod 100% 35 / 35 totaltotal 72.7% 646 / 888 CC 145 SCAR 28.1
CC
145
Cov
73%
read
44.4
select_chars
30.0
get_comments
20.0
CRAP 16.5
« lib/Bio/NEXUS/Functions.pm ↑ Summary script/nextool.pl »
1 ######################################################
2 # NEXUS.pm
3 ######################################################
4 #
5 # $Id: NEXUS.pm,v 1.122 2012/02/10 13:28:28 astoltzfus Exp $
6 # $Revision: 1.122 $
7 #
8 #################### START POD DOCUMENTATION ##################
9  
10 =head1 NAME
11  
12 Bio::NEXUS - An object-oriented Perl Applications Programming Interface (API) for the NEXUS file format
13  
14 =head1 SYNOPSIS
15  
16 my $nexus =Bio::NEXUS->new($file);
17 # if $file is not provided, an empty Bio::NEXUS object will be created
18 $nexus->write($newfile);
19  
20 =head1 DESCRIPTION
21  
22 This is the base class for the Bio::NEXUS package, providing an object-oriented API to
23 the NEXUS file format of I<Maddison, et al.>, 1997. This module provides methods to
24 add/remove blocks, select blocks/trees/subtrees/OTUs/characters and so on. For a
25 tutorial illustrating how to use Bio::NEXUS, see L<doc/Tutorial.pod>.
26  
27 =head1 FEEDBACK
28  
29 All feedback (bugs, feature enhancements, etc.) are all greatly appreciated.
30  
31 =head1 AUTHORS
32  
33 Chengzhi Liang (liangc@umbi.umd.edu)
34 Weigang Qiu (weigang@genectr.hunter.cuny.edu)
35 Peter Yang (pyang@rice.edu)
36 Thomas Hladish (tjhladish at yahoo)
37 Arlin Stoltzfus (arlin.stoltzfus@nist.gov)
38  
39 =head1 METHODS
40  
41 =cut
42  
43 package Bio::NEXUS;
44  
45 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 # use Data::Dumper; # XXX this is not used, might as well not load it!
47 # use Carp; # XXX this is not used, might as well not load it!
48  
49 35 ▶use Bio::NEXUS::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 35 ▶use Bio::NEXUS::AssumptionsBlock;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 35 ▶use Bio::NEXUS::CharactersBlock;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 35 ▶use Bio::NEXUS::TreesBlock;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 35 ▶use Bio::NEXUS::HistoryBlock;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 35 ▶use Bio::NEXUS::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 35 ▶use Bio::NEXUS::TaxaBlock;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 35 ▶use Bio::NEXUS::SetsBlock;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 35 ▶use Bio::NEXUS::SpanBlock;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 35 ▶use Bio::NEXUS::UnalignedBlock;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 35 ▶use Bio::NEXUS::UnknownBlock;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 35 ▶use Bio::NEXUS::DataBlock;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 35 ▶use Bio::NEXUS::DistancesBlock;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 # in progress
63 35 ▶use Bio::NEXUS::CodonsBlock;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 35 ▶use Bio::NEXUS::NotesBlock;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 35 ▶use Bio::NEXUS::Util::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 35 ▶use Bio::NEXUS::Util::Exceptions 'throw';
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 #use Bio::NEXUS::CodonsBlock;
70 #use Bio::NEXUS::NotesBlock;
71  
72 # Version number is obtained cvs $Name tag (eg. release_1_05).
73 # ExtUtils::MakeMaker reads package global $VERSION
74  
75 35 ▶use vars qw($VERSION $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
76 35 ▶$VERSION = do { my @r = ( q$Name: $ =~ /\d+/g ); ( $#r < 0 ) ? '0.78' : sprintf " %d." . "%02d" x $#r, @r; };
Branch: 1 branch
BranchTrueFalseTotal
$#r < 0 ? : 35 0 35
77  
78 # a logger is an object that conditionally prints messages,
79 # so we don't need to add print statements and then comment
80 # them out anymore. You can leave the logging in the code, and
81 # make it invisible by lowering the log level.
82 35 my $logger = Bio::NEXUS::Util::Logger->new; # XXX
83  
84 =head2 new
85  
86 Title : new
87 Usage : my $nexus = Bio::NEXUS->new($filename, $verbose);
88 Function: Creates a new Bio::NEXUS object
89 Returns : Bio::NEXUS object
90 Args : $filename, $verbose, or none
91  
92 =cut
93  
94 83 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
95 83 my ( $class, $filename, $verbose ) = @_;
96
97 # XXX notify user conditionally
98 83 $logger->info( "Constructor for $class called" );
99 83 my $self = {};
100 83 bless( $self, $class );
101 83 ▶ if ($filename) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename) 69 14 83
102 69 $self->read_file( $filename, $verbose );
103 69 $filename =~ s/\.nex$//;
104 69 $self->set_name($filename);
105 }
106 83 return $self;
107 }
108  
109  
110 =head2 get_bionexus_version
111  
112 Title : get_bionexus_version
113 Usage : Bio::NEXUS->get_bionexus_version();
114 Function: gets the package version
115 Returns : value of \$VERSION
116 Args : none
117  
118 =cut
119  
120 0 ▶sub get_bionexus_version { return $VERSION; }
Subroutine get_bionexus_version: not called CC=1 SCAR=6.9
Pod documented
121  
122 =head2 read_file
123  
124 Title : read_file
125 Usage : Bio::NEXUS->read_file($filename, $verbose);
126 Function: Reads the contents of the NEXUS file and populate the data in the Bio::NEXUS object
127 Returns : None
128 Args : $filename, $verbose, or none
129  
130 =cut
131  
132 69 ▶sub read_file {
Subroutine read_file: called CC=2 SCAR=7.6
Pod documented
133 69 my ( $self, $filename, $verbose ) = @_;
134 69 ▶ if ( not -e $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $filename) 0 69 69
135
136 # XXX refer to Bio::NEXUS::Util::Exceptions -
137 # exceptions are generally more useful for
138 # tracking down problems
139 0 throw 'FileError' => "$filename is not a valid filename";
140 }
141 $self->read( {
142 69 'format' => 'filename',
143 'param' => $filename,
144 'verbose' => $verbose,
145 } );
146 }
147  
148 =head2 read
149  
150 Title : read
151 Usage : Bio::NEXUS->read({format => 'string', 'param' => $buffer, 'verbose' => $verbose});
152 Usage : Bio::NEXUS->read({format => 'file', 'param' => $filename, 'verbose' => $verbose});
153 Function: Reads the contents of the NEXUS file and populate the data in the NEXUS object
154 Returns : None
155 Args : $filename, $verbose, or none
156  
157 =cut
158  
159 87 ▶sub read {
Subroutine read: called CC=44 SCAR=37.9
Pod documented
160 87 my ( $self, $args ) = @_;
161 87 ▶ $args->{'format'} ||= 'string';
Condition50%1/2
$args->{"format"} ||= "string"
l!l
870
MC/DC0%0/1
$args->{"format"} ||= "string"
$args->{"format"}
Truth table50%1/2
A
A= $args->{"format"}
Aresult
11
00
162 87 ▶ $args->{'param'} ||= '';
Condition50%1/2
$args->{"param"} ||= ""
l!l
870
MC/DC0%0/1
$args->{"param"} ||= ""
$args->{"param"}
Truth table50%1/2
A
A= $args->{"param"}
Aresult
11
00
163 87 ▶ my $verbose = $args->{'verbose'} || 0;
Condition50%1/2
$args->{'verbose'} || 0
l!l
087
MC/DC0%0/1
$args->{'verbose'} || 0
$args->{'verbose'}
Truth table50%1/2
A
A= $args->{'verbose'}
Aresult
11
00
164 87 my $nexus_file;
165 my $filename;
166  
167 87 ▶ if ( lc $args->{'format'} eq 'string' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $args->{'format'} eq 'string') { } 18 69 87
168 18 $nexus_file = $args->{'param'};
169 18 $filename = 'INPUT';
170 }
171 else {
172 69 $filename = $args->{'param'};
173 69 $nexus_file = _slurp($filename);
174 }
175  
176 # Read entire file into scalar $nexus_file
177 87 $logger->info('Reading NEXUS file');
178 87 $self->{'filename'} = $filename;
179  
180 87 my $found_nexus_token = 0;
181 87 my $comment_level = 0;
182 87 my $quote_level = 0;
183 87 my $comment = '';
184 87 my $block_type = '';
185 87 my @command_level_strings = ();
186 87 my $command = '';
187 87 my $in_tree_string = 0;
188 87 my $prev_text_char = '';
189  
190 87 for my $text_char ( split //, $nexus_file ) {
191  
192 # if we're at the beginning of a single-quoted string
193 # (We're also supporting double quoting, since double quotes don't seem
194 # to be used for a different meaning, and we need to support double
195 # quotes in output from programs like clustal. We will not, however,
196 # output double quotes.)
197  
198 91684 ▶ $text_char = q{'} if $text_char eq q{"};
Branch: 1 branch
BranchTrueFalseTotal
if $text_char eq "\"" 10 91674 91684
199  
200 91684 ▶ if ( ( $text_char eq q{'} )
Branch: 6 branches
BranchTrueFalseTotal
if ($text_char eq q['] and $quote_level == 0 and $comment_level == 0 and $found_nexus_token) { } 212 91472 91684
elsif ($quote_level > 0) { } 4856 86616 91472
elsif ($text_char eq '[' || $comment_level > 0 and $in_tree_string == 0) { } 5434 81182 86616
elsif (not $found_nexus_token) { } 526 80656 81182
elsif ($command eq '') { } 2392 78264 80656
elsif (not $command =~ /;$/) { } 78264 0 78264
Condition88%8/9
$text_char eq "'" and $quote_level == 0 and $comment_level == 0 and $found_nexus_token
!ll&&!rl&&r
914720212
$text_char eq "'" and $quote_level == 0 and $comment_level == 0
!ll&&!rl&&r
9144032212
$text_char eq "'" and $quote_level == 0
!ll&&!rl&&r
91228212244
MC/DC75%3/4
$text_char eq "'" and $quote_level == 0 and $comment_level == 0 and $found_nexus_token
$text_char eq "'" $quote_level == 0 $comment_level == 0 $found_nexus_token
Truth table80%4/5
A and B and C and D
A= $text_char eq "'"B= $quote_level == 0C= $comment_level == 0D= $found_nexus_token
ABCDresult
0XXX0
10XX0
110X0
11100
11111
201 && $quote_level == 0
202 && $comment_level == 0
203 && $found_nexus_token )
204 {
205 212 $command .= $text_char;
206 212 $quote_level++;
207 }
208  
209 # if we're inside a single-quoted string
210 elsif ( $quote_level > 0 ) {
211 4856 $command .= $text_char;
212  
213 #turn off the quote flag if we're ending the quoted string
214 4856 ▶ if ( $text_char eq q{'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text_char eq "'") 212 4644 4856
215 212 ▶ $quote_level =
Branch: 1 branch
BranchTrueFalseTotal
$prev_text_char eq $text_char ? : 0 212 212
216 ( $prev_text_char eq $text_char )
217 ? $quote_level + 1
218 : $quote_level - 1;
219 }
220  
221 }
222  
223 # if we're entering a (possibly nested) comment, or we're already in
224 # one, but we're not looking at bracketed bootstraps in the tree string
225 ▶ elsif ( ( $text_char eq '[' || $comment_level > 0 )
Condition100%6/6
$text_char eq "[" || $comment_level > 0 and $in_tree_string == 0
!ll&&!rl&&r
81120625434
$text_char eq "[" or $comment_level > 0
l!l&&r!l&&!r
223527381120
MC/DC100%3/3
$text_char eq "[" || $comment_level > 0 and $in_tree_string == 0
$text_char eq "[" $comment_level > 0 $in_tree_string == 0
Truth table80%4/5
A or B and C
A= $text_char eq "["B= $comment_level > 0C= $in_tree_string == 0
ABCresult
00X0
1X00
0100
1X11
0111
226 && $in_tree_string == 0 )
227 {
228 5434 $comment .= $text_char;
229 5434 ▶ $comment_level++ if $text_char eq '[';
Branch: 1 branch
BranchTrueFalseTotal
if $text_char eq "[" 161 5273 5434
230  
231 # if we see the end of a (possibly nested) comment
232 5434 ▶ if ( $text_char eq ']' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text_char eq "]") 161 5273 5434
233 161 $comment_level--;
234  
235 # if we just closed found the last right bracket in the comment,
236 # then add the comment to the Bio::NEXUS obj
237 161 ▶ if ( $comment_level == 0 && @command_level_strings == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comment_level == 0 and @command_level_strings == 0) 34 127 161
Condition66%2/3
$comment_level == 0 and @command_level_strings == 0
!ll&&!rl&&r
012734
MC/DC50%1/2
$comment_level == 0 and @command_level_strings == 0
$comment_level == 0 @command_level_strings == 0
Truth table66%2/3
A and B
A= $comment_level == 0B= @command_level_strings == 0
ABresult
0X0
100
111
238 34 $self->add_comment($comment);
239 34 $comment = q{};
240 }
241 }
242 }
243  
244 # if we haven't found '#NEXUS' yet
245 elsif ( !$found_nexus_token ) {
246 526 $command .= $text_char;
247  
248 # if we've found the whole #NEXUS token that's supposed
249 # to start the file (though it may be broken by comments)
250 526 ▶ if ( $command =~ /^\s*#NEXUS/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($command =~ /^\s*#NEXUS/i) { } 86 440 526
elsif (not $command =~ /^\s*(?:#(?:N(?:E(?:X(?:U(?:S)?)?)?)?)?)?$/i) { } 1 439 440
251 86 $found_nexus_token = 1;
252 86 $command = q{};
253 }
254  
255 # If the file starts with something else, then throw.
256 # This regex will match '#NEX' and '#NEXUS', but not '#NEXT'
257 elsif ( $command !~ /^\s*(?:#(?:N(?:E(?:X(?:U(?:S)?)?)?)?)?)?$/i ) {
258 1 throw 'BadFormat' => "'$filename' does not begin with the \n'#NEXUS' token; it does not appear to be a NEXUS file.\n";
259 }
260 }
261  
262 # if we're at the beginning of a block/command
263 elsif ( $command eq q{} ) {
264 2392 ▶ if ( $comment ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comment) 25 2367 2392
265 25 push( @command_level_strings, $comment );
266 25 $comment = q{};
267 }
268 2392 ▶ if ( $text_char ne "\n" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text_char ne "\n") 1050 1342 2392
269 1050 $command .= $text_char;
270 }
271 }
272  
273 # if we're inside a block, but haven't gotten to the end of the command
274 elsif ( $command !~ /;$/ ) {
275 78264 $command .= $text_char;
276 78264 ▶ if ( ( $in_tree_string == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($in_tree_string == 0 and $block_type eq 'trees' || $block_type eq 'history' and $text_char eq '=') { } 88 78176 78264
Condition100%9/9
$in_tree_string == 0 and $block_type eq "trees" || $block_type eq "history" and $text_char eq "="
!ll&&!rl&&r
72675550188
$in_tree_string == 0 and $block_type eq "trees" || $block_type eq "history"
!ll&&!rl&&r
6555661205589
$block_type eq "trees" or $block_type eq "history"
l!l&&r!l&&!r
4435115466120
MC/DC100%4/4
$in_tree_string == 0 and $block_type eq "trees" || $block_type eq "history" and $text_char eq "="
$in_tree_string == 0 $block_type eq "trees" $block_type eq "history" $text_char eq "="
Truth table100%6/6
A and B or C and D
A= $in_tree_string == 0B= $block_type eq "trees"C= $block_type eq "history"D= $text_char eq "="
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
277 && ( $block_type eq 'trees' || $block_type eq 'history' )
278 && ( $text_char eq '=' ) )
279 {
280 88 ▶ $in_tree_string = 1 if ( $command =~ /tree\s.+=/i );
Branch: 1 branch
BranchTrueFalseTotal
if $command =~ /tree\s.+=/i 76 12 88
281 }
282 78176 ▶ else { $in_tree_string = 0 if $text_char eq ';' }
Branch: 1 branch
BranchTrueFalseTotal
if $text_char eq ";" 1050 77126 78176
283 }
284  
285 91683 $prev_text_char = $text_char;
286  
287 # Only process if we might genuinely have reached the end
288 # of a command or block
289 91683 ▶ if ( !$comment_level && !$quote_level && $text_char eq ';' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $comment_level || $quote_level and $text_char eq ";") 1050 90633 91683
Condition100%6/6
not $comment_level || $quote_level and $text_char eq ";"
!ll&&!rl&&r
10129805041050
$comment_level || $quote_level
l!l&&r!l&&!r
5273485681554
MC/DC100%3/3
not $comment_level || $quote_level and $text_char eq ";"
$comment_level $quote_level $text_char eq ";"
Truth table100%4/4
not(A || B) and C
A= $comment_levelB= $quote_levelC= $text_char eq ";"
ABCresult
1XX0
01X0
0000
0011
290  
291 # if we've read in the entire begin block command
292 1050 ▶ if ( $command =~ /begin\s+(.+)\s*;/i ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($command =~ /begin\s+(.+)\s*;/i) { } 231 819 1050
elsif ($command =~ /^\s*end(?:block)?\s*;/i) { } 231 588 819
elsif ($command =~ s/\s*;\s*$//) { } 588 0 588
293 231 $block_type = lc $1;
294 231 $logger->info("found 'begin' token for a $block_type block");
295 231 push( @command_level_strings, $command );
296 231 $command = q{};
297 231 ▶ if ($comment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comment) 0 231 231
298 0 push( @command_level_strings, $comment );
299 0 $comment = q{};
300 }
301 }
302  
303 # if we've found the end of the block
304 elsif ( $command =~ /^\s*end(?:block)?\s*;/i ) {
305 231 $logger->info("found 'end' token");
306 231 $command = 'end';
307 231 push( @command_level_strings, $command );
308 231 $command = q{};
309 231 ▶ if ($comment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comment) 1 230 231
310 1 push( @command_level_strings, $comment );
311 1 $comment = q{};
312 }
313  
314 # Send the commands [and comments] off to be turned into a block
315 231 my $block_obj =
316 $self->create_block( $block_type, \@command_level_strings,
317 $verbose );
318 222 $self->add_block($block_obj);
319 222 @command_level_strings = ();
320 222 $block_type = q{};
321  
322 }
323  
324 # if we've found the end of a command (but not an
325 # 'END BLOCK;' command, since we already asked that) remove the
326 # semicolon at the end, since the block parsers aren't expecting
327 # one, as well as surrounding white space. Two substitutions
328 # are faster than one, in this case.
329 elsif ( $command =~ s/\s*;\s*$// ) {
330 588 $command =~ s/^\s*//;
331 588 ▶ if ($comment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comment) 10 578 588
332 10 push( @command_level_strings, $comment );
333 10 $comment = q{};
334 }
335 588 push( @command_level_strings, $command );
336 588 $command = q{};
337 }
338 }
339 }
340  
341 # Create a taxa block if we didn't find one in the file
342 77 ▶ if ( !$self->get_block('taxa') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->get_block("taxa")) 6 71 77
343 6 $logger->info("No taxa block found, will create one");
344 6 $self->set_taxablock;
345 }
346  
347 77 my $counter = scalar @{ $self->get_blocks() };
348 77 $logger->info("$counter blocks read. NEXUS read complete.");
349 77 return $self;
350 }
351  
352 =head2 create_block
353  
354 Title : create_block
355 Usage : my $block = Bio::NEXUS->create_block($blocktype,$block_string, $verbose);
356 Function: Creates a block object based on the input block type and block content as string
357 Returns : A block object (If Block type is 'Characters' then 'Bio::NEXUS::CharactersBlock' is returned
358 Args : $block_type (as string), $block_content (as string), verbose
359  
360 =cut
361  
362 232 ▶sub create_block {
Subroutine create_block: called CC=6 SCAR=17.9
Pod documented
363 232 my ( $self, $block_type, $commands, $verbose ) = @_;
364 232 $logger->info("creating block $block_type");
365 232 my $block; # This will hold a block object, once one is constructed
366 232 my @args = ( $block_type, $commands, $verbose );
367  
368 232 my %block_types = (
369 'assumptions' => "Bio::NEXUS::AssumptionsBlock",
370 'characters' => "Bio::NEXUS::CharactersBlock",
371 # in progress: codons
372 # 'codons' => "Bio::NEXUS::CodonsBlock",
373 'data' => "Bio::NEXUS::DataBlock",
374 'distances' => "Bio::NEXUS::DistancesBlock",
375 'history' => "Bio::NEXUS::HistoryBlock",
376 # in progress: notes
377 # 'notes' => "Bio::NEXUS::NotesBlock",
378 'sets' => "Bio::NEXUS::SetsBlock",
379 'span' => "Bio::NEXUS::SpanBlock",
380 'taxa' => "Bio::NEXUS::TaxaBlock",
381 'trees' => "Bio::NEXUS::TreesBlock",
382 'unaligned' => "Bio::NEXUS::UnalignedBlock"
383 );
384 232 my $class = $block_types{$block_type};
385 232 ▶ if ( defined( $class ) ) { $logger->info("class: $class"); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class) 224 8 232
386  
387 232 my $taxlabels;
388 232 ▶ if ( defined $self->get_block('taxa') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->get_block("taxa")) 139 92 231
389 139 $taxlabels = $self->get_taxlabels();
390 }
391 231 ▶ if ( $class ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class) { } 223 8 231
392 223 $block = $class->new( @args, $taxlabels );
393 }
394 else {
395 8 $logger->info("An UnknownBlock is being created for block_type: $block_type");
396 8 $block = Bio::NEXUS::UnknownBlock->new( @args );
397 }
398  
399 222 ▶ if ( $block_type =~ m/taxa/i and my $title = $block->get_title() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_type =~ /taxa/i and my $title = $block->get_title) 6 216 222
Condition100%3/3
$block_type =~ /taxa/i and my $title = $block->get_title
!ll&&!rl&&r
144726
MC/DC100%2/2
$block_type =~ /taxa/i and my $title = $block->get_title
$block_type =~ /taxa/i my $title = $block->get_title
Truth table100%3/3
A and B
A= $block_type =~ /taxa/iB= my $title = $block->get_title
ABresult
0X0
100
111
400 6 $self->set_name( $title );
401 }
402  
403 # Check to make sure that if a Taxa Block is defined,
404 # that everything is included in it
405 222 $self->_validate_taxa($block);
406  
407 222 return $block;
408 }
409  
410 =begin comment
411  
412 Title : _validate_taxa
413 Usage :
414 Function:
415 Returns :
416 Args :
417  
418 =end comment
419  
420 =cut
421  
422 222 ▶sub _validate_taxa {
Subroutine _validate_taxa: called CC=10 SCAR=23.1
423 222 my ( $self, $block ) = @_;
424 222 my $block_type = $block->get_type();
425 222 my $taxablock = $self->get_block('taxa');
426 222 ▶ return unless $taxablock;
Branch: 1 branch
BranchTrueFalseTotal
unless $taxablock 90 132 222
427  
428 132 my @taxlabels = @{ $taxablock->get_taxlabels() };
429  
430 # Every taxon listed in the characters or trees blocks should be in the
431 # Taxa Block as well
432 132 ▶ if ( lc $block_type eq 'characters' || lc $block_type eq 'trees' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (lc $block_type eq 'characters' or lc $block_type eq 'trees') { } 112 20 132
elsif (lc $block_type eq 'sets') { } 1 19 20
Condition100%3/3
lc $block_type eq "characters" or lc $block_type eq "trees"
l!l&&r!l&&!r
644820
MC/DC100%2/2
lc $block_type eq "characters" or lc $block_type eq "trees"
lc $block_type eq "characters" lc $block_type eq "trees"
Truth table100%3/3
A or B
A= lc $block_type eq "characters"B= lc $block_type eq "trees"
ABresult
1X1
011
000
433 112 my @taxlabels = @{ $taxablock->get_taxlabels() };
434 112 my @block_taxa = @{ $block->get_taxlabels() };
435 LABEL:
436 112 for my $label (@block_taxa) {
437 372 my $match = 0;
438 372 ▶ next LABEL if grep { $label eq $_ } @taxlabels;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$label eq $_;} @taxlabels 372 0 372
439 0 throw 'ObjectMismatch' => "Taxon <$label> in $block_type block is not in the TAXA Block";
440 }
441 }
442  
443 # And every set element should be in the Taxa Block
444 elsif ( lc $block_type eq 'sets' ) {
445  
446 1 my %taxsets = %{ $block->get_taxsets() };
447 1 for my $setname ( keys %taxsets ) {
448 5 my @elements = @{ $taxsets{$setname} };
449 ELEMENT:
450 5 for my $element (@elements) {
451 8 ▶ next ELEMENT if grep { $element eq $_ } @taxlabels;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$element eq $_;} @taxlabels 8 0 8
452 0 throw 'ObjectMismatch' => "Element <$element> of set <$setname> is not in the TAXA Block";
453 }
454 }
455 }
456 132 return;
457 }
458  
459 =head2 clone
460  
461 Name : clone
462 Usage : my $newnexus = $nexus->clone();
463 Function: clone a NEXUS object; each block is also (shallow) cloned.
464 Returns : new Bio::NEXUS object
465 Args : none
466  
467 =cut
468  
469 3 ▶sub clone {
Subroutine clone: called CC=2 SCAR=6.9
Pod documented
470 3 my ($self) = @_;
471 3 my $class = ref($self);
472 3 my $newnexus = bless( { %{$self} }, $class );
473
474 # clone blocks
475 3 my @newblocks;
476 3 for my $block ( @{ $self->get_blocks() } ) {
477 8 push @newblocks, $block->clone();
478 }
479 3 $newnexus->set_blocks( \@newblocks );
480 3 return $newnexus;
481 }
482  
483 =head2 set_name
484  
485 Title : set_name
486 Usage : Bio::NEXUS->set_name($name);
487 Function: Sets name for the NEXUS object (usually the filename).
488 Returns : Nothing
489 Args : $name (as string)
490  
491 =cut
492  
493 75 ▶sub set_name {
Subroutine set_name: called CC=1 SCAR=0.0
Pod documented
494 75 my ( $self, $name ) = @_;
495 75 $self->{'name'} = $name;
496 }
497  
498 =head2 get_name
499  
500 Title : get_name
501 Usage : $name = Bio::NEXUS->get_name();
502 Function: Returns the name of the NEXUS object as string. (NEXUS filename).
503 Returns : NEXUS filename
504 Args : None
505  
506 =cut
507  
508 0 ▶sub get_name {
Subroutine get_name: not called CC=1 SCAR=6.9
Pod documented
509 0 my ($self) = @_;
510 0 return $self->{'name'};
511 }
512  
513 =head2 add_comment
514  
515 Name : add_comment
516 Usage : $nexus->add_comment($comment);
517 Function: add a block of comments.
518 Returns : none
519 Args : a string object
520  
521 =cut
522  
523 34 ▶sub add_comment {
Subroutine add_comment: called CC=1 SCAR=0.0
Pod documented
524 34 my ( $self, $comment ) = @_;
525 34 $self->add_block($comment);
526 }
527  
528 =head2 get_comments
529  
530 Name : get_comments
531 Usage : $nexus->get_comments();
532 Function: Retrieves all comments.
533 Returns : ref to an array of strings
534 Args : none
535  
536 =cut
537  
538 0 ▶sub get_comments {
Subroutine get_comments: not called CC=4 SCAR=30.0
Pod documented
539 0 my ($self) = @_;
540 0 my @blocks_and_comments = @{ $self->get_blocks_and_comments() };
541 0 my @comments;
542 0 for my $block_or_comment (@blocks_and_comments) {
543 0 ▶ if ( _is_comment($block_or_comment) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_comment($block_or_comment)) 0 0 0
544 0 push( @comments, $block_or_comment );
545 }
546 }
547 0 ▶ return \@comments || [];
Condition0%0/2
\@comments || []
l!l
00
MC/DC0%0/1
\@comments || []
\@comments
Truth table0%0/2
A
A= \@comments
Aresult
11
00
548 }
549  
550 =head2 get_filename
551  
552 Name : get_filename
553 Usage : $nexus->get_filename;
554 Function: get the NEXUS filename for this object.
555 Returns : A filename
556 Args : none
557  
558 =cut
559  
560 0 ▶sub get_filename {
Subroutine get_filename: not called CC=1 SCAR=6.9
Pod documented
561 0 my ($self) = @_;
562 0 return $self->{'filename'};
563 }
564  
565 =head2 set_blocks
566  
567 Name : set_blocks
568 Usage : $nexus->set_blocks($blocks);
569 Function: set the blocks in this nexus file.
570 Returns : none
571 Args : an array of Block objects
572  
573 =cut
574  
575 3 ▶sub set_blocks {
Subroutine set_blocks: called CC=1 SCAR=0.0
Pod documented
576 3 my ( $self, $blocks ) = @_;
577 3 $self->{'block_level'} = $blocks;
578 }
579  
580 =head2 add_block
581  
582 Name : add_block
583 Usage : $nexus->add_block($block_obj);
584 Function: add a block.
585 Returns : none
586 Args : a Bio::NEXUS::*Block object
587  
588 =cut
589  
590 265 ▶sub add_block {
Subroutine add_block: called CC=1 SCAR=0.0
Pod documented
591 265 my ( $self, $block ) = @_;
592 265 push @{ $self->{'block_level'} }, $block;
593 265 return;
594 }
595  
596 =head2 remove_block
597  
598 Name : remove_block
599 Usage : $nexus->remove_block($blocktype, $title);
600 Function: remove a block
601 Returns : none
602 Args : block type and block name (strings)
603  
604 =cut
605  
606 2 ▶sub remove_block {
Subroutine remove_block: called CC=8 SCAR=22.9
Pod documented
607 2 my ( $self, $blocktype, $title ) = @_;
608 2 my $items = $self->get_blocks_and_comments();
609 2 my $found_block = 0;
610 2 ITEM: for my $i ( 0 .. $#{ $items } ) {
611 5 my $item = $items->[$i];
612 5 ▶ next ITEM if _is_comment($item);
Branch: 1 branch
BranchTrueFalseTotal
if _is_comment($item) 0 5 5
613 5 ▶ if ( $item->get_type() =~ m/$blocktype/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->get_type =~ /$blocktype/i) 2 3 5
614
615 # if either no title was specified, or the title matches
616 2 ▶ if ( !$title || $item->get_title =~ m/$title/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $title or $item->get_title =~ /$title/i) 2 0 2
Condition33%1/3
not $title or $item->get_title =~ /$title/i
l!l&&r!l&&!r
020
MC/DC0%0/2
not $title or $item->get_title =~ /$title/i
not $title $item->get_title =~ /$title/i
Truth table33%1/3
not(A) or B
A= not $titleB= $item->get_title =~ /$title/i
ABresult
1X1
011
000
617 2 $logger->info("> found the block!");
618
619 # the next statement removes a reference
620 # from a copy array - but will it remove the
621 # reference from the actual array of blocks?
622 # XXX yes -- RAV
623 2 splice( @{ $items }, $i, 1 );
624
625 # sanity check
626 2 $logger->info('> blocks_and_comments.length: ' . scalar @{ $items } );
627 2 $logger->info('> self->get_blocks_and_comments.length: ' . scalar @{ $self->get_blocks_and_comments() } );
628
629 2 $found_block = 1;
630 }
631 }
632 5 $self->{'block_level'} = $items;
633 }
634  
635 2 ▶ if ( not $found_block ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $found_block) 0 2 2
636 0 my $blockname = $blocktype;
637 0 ▶ if ( $title ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title) 0 0 0
638 0 $blockname .= " ($title)"
639 }
640 0 $logger->warn("could not find a $blockname block");
641 }
642 }
643  
644 =head2 get_block
645  
646 Name : get_block
647 Usage : $nexus->get_block($blocktype, $blockname);
648 Function: Retrieves NEXUS block.
649 Returns : A Bio::NEXUS::*Block object
650 Args : none
651  
652 =cut
653  
654 798 ▶sub get_block {
Subroutine get_block: called CC=5 SCAR=16.2
Pod documented
655 798 my ( $self, $blocktype, $blockname ) = @_;
656  
657 798 for my $block ( @{ $self->get_blocks($blocktype) } ) {
658 602 ▶ if ( $block->get_type() =~ m/$blocktype/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block->get_type =~ /$blocktype/i) 602 0 602
659 602 ▶ if ( !$blockname ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $blockname) { } 597 5 602
elsif ($block->get_title =~ /$blockname/i) { } 5 0 5
660 597 return $block;
661 }
662 elsif ( $block->get_title() =~ m/$blockname/i ) {
663 5 return $block;
664 }
665 }
666 }
667 195 return undef;
668 }
669  
670 =head2 get_blocks
671  
672 Name : get_blocks
673 Usage : $nexus->get_blocks($blocktype);
674 Function: Retrieves list of blocks of some type or all blocks.
675 Returns : Array of Bio::NEXUS::Block objects
676 Args : $blocktype or none
677  
678 =cut
679  
680 919 ▶sub get_blocks {
Subroutine get_blocks: called CC=5 SCAR=16.1
Pod documented
681 919 my ( $self, $blocktype ) = @_;
682  
683 919 my @blocks;
684  
685 919 for my $item ( @{ $self->get_blocks_and_comments() } ) {
686  
687 # if it's actually a block object, and not a block-level comment
688 2124 ▶ if ( !_is_comment($item) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_is_comment($item)) 1768 356 2124
689 1768 ▶ if (!$blocktype || $item->get_type() =~ /$blocktype/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $blocktype or $item->get_type =~ /$blocktype/i) 1037 731 1768
Condition100%3/3
not $blocktype or $item->get_type =~ /$blocktype/i
l!l&&r!l&&!r
346691731
MC/DC100%2/2
not $blocktype or $item->get_type =~ /$blocktype/i
not $blocktype $item->get_type =~ /$blocktype/i
Truth table100%3/3
not(A) or B
A= not $blocktypeB= $item->get_type =~ /$blocktype/i
ABresult
1X1
011
000
690 1037 push @blocks, $item;
691 }
692 }
693 }
694  
695 918 return \@blocks;
696 }
697  
698 =head2 get_blocks_and_comments
699  
700 Name : get_blocks_and_comments
701 Usage : @blocks_and_comments = @{ $nexus->get_blocks_and_comments() };
702 Function: get all comments and blocks in the NEXUS object
703 Returns : array of strings and block objects
704 Args : none
705  
706 =cut
707  
708 925 ▶sub get_blocks_and_comments {
Subroutine get_blocks_and_comments: called CC=2 SCAR=6.9
Pod documented
709 925 my ($self) = @_;
710 925 ▶ return $self->{'block_level'} || [];
Condition100%2/2
$self->{'block_level'} || []
l!l
798126
MC/DC100%1/1
$self->{'block_level'} || []
$self->{'block_level'}
Truth table100%2/2
A
A= $self->{'block_level'}
Aresult
11
00
711 }
712  
713 =head2 get_weights
714  
715 Name : get_weights
716 Usage : $nexus->get_weights($charblockname);
717 Function: get all weights for a block.
718 Returns : the weights of alignments in a Characters Block
719 Args : an hash of weightset objects
720  
721 =cut
722  
723 0 ▶sub get_weights {
Subroutine get_weights: not called CC=3 SCAR=24.8
Pod documented
724 0 my ( $self, $characters ) = @_;
725 0 my $blocks = $self->get_blocks('assumptions');
726 0 my %weights;
727 0 for my $block (@$blocks) {
728 0 ▶ if ( lc $block->get_link('characters') eq lc $characters ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $block->get_link("characters") eq lc $characters) 0 0 0
729 0 push @{ $weights{ $block->get_title } },
730 0 @{ $block->get_assumptions };
731 }
732 }
733 0 return \%weights;
734 }
735  
736 =head2 get_taxlabels
737  
738 Name : get_taxlabels
739 Usage : $nexus->get_taxlabels();
740 Function: get the taxa labels of the NEXUS object (obtained from TAXA block).
741 Returns : an arrayreference of taxa labels.
742 Args : none
743  
744 =cut
745  
746 154 ▶sub get_taxlabels {
Subroutine get_taxlabels: called CC=1 SCAR=0.0
Pod documented
747 154 my $self = shift;
748 154 return $self->get_block('taxa')->get_taxlabels();
749 }
750  
751 =head2 get_otus
752  
753 Name : get_otus
754 Usage : $nexus->get_otus();
755 Function: Retrieves list of OTUs
756 Returns : Array of OTU names or Bio::NEXUS::TaxUnit objects
757 Args : none
758  
759 =cut
760  
761 5 ▶sub get_otus {
Subroutine get_otus: called CC=4 SCAR=22.9
Pod documented
762 5 my $self = shift;
763  
764 5 ▶ if ( my $taxablock = $self->get_block('taxa') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $taxablock = $self->get_block("taxa")) 5 0 5
765 5 return $taxablock->get_taxlabels();
766 }
767 0 ▶ if ( my $charblock = $self->get_block('characters') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $charblock = $self->get_block("characters")) 0 0 0
768 0 return $charblock->get_otus();
769 }
770 0 ▶ if ( my $treesblock = $self->get_block('trees') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $treesblock = $self->get_block("trees")) 0 0 0
771 0 return $treesblock->get_otus();
772 }
773 0 throw 'BadArgs' => 'no appropriate block exists to get the otus from';
774 }
775  
776 =head2 rename_otus
777  
778 Name : rename_otus
779 Usage : $nexus->rename_otus(\%translation);
780 Function: rename all OTUs
781 Returns : a new nexus object with new OTU names
782 Args : a ref to hash based on OTU name pairs
783  
784 =cut
785  
786 2 ▶sub rename_otus {
Subroutine rename_otus: called CC=3 SCAR=11.0
Pod documented
787 2 my ( $self, $translation ) = @_;
788 2 my $nexus = $self->clone();
789 2 for my $block ( @{ $nexus->get_blocks() } ) {
790 # XXX duck-typing is probably okay, no? -- RAV
791 # if ( $block->get_type()
792 # =~ /^(?:characters|taxa|sets|span|history|trees)$/i )
793 6 ▶ if ( $block->can('rename_otus') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block->can("rename_otus")) 6 0 6
794 6 $block->rename_otus($translation);
795 }
796 }
797 2 return $nexus;
798 }
799  
800 =head2 add_otu_clone
801  
802 Name : add_otu_clone
803 Usage : $nexus_object->add_otu_clone($original_otu_name, $copy_otu_name);
804 Function: creates a copy of a specified otu inside this Bio::NEXUS object
805 Returns : n/a
806 Args : $original_otu_name (string) - the name of the otu that will be cloned, $copy_otu_name (string) - the desired name for the new clone
807 Preconditions : $original_otu_name and $copy_otu_name are not equal, $original_otu_name is a valid otu name (existing otu)
808
809 =cut
810  
811 9 ▶sub add_otu_clone {
Subroutine add_otu_clone: called CC=9 SCAR=22.3
Pod documented
812 9 my ( $self, $original_otu_name, $copy_otu_name ) = @_;
813 9 $logger->warn( "not fully implemented!" );
814
815 9 ▶ if (! defined $original_otu_name || ! defined $copy_otu_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $original_otu_name and defined $copy_otu_name) 0 9 9
Condition33%1/3
defined $original_otu_name and defined $copy_otu_name
!ll&&!rl&&r
009
MC/DC0%0/2
defined $original_otu_name and defined $copy_otu_name
defined $original_otu_name defined $copy_otu_name
Truth table33%1/3
A and B
A= defined $original_otu_nameB= defined $copy_otu_name
ABresult
0X0
100
111
816 0 throw 'BadArgs' => 'missing argument!';
817 }
818  
819 9 ▶ if ($original_otu_name eq $copy_otu_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($original_otu_name eq $copy_otu_name) 1 8 9
820 1 throw 'BadArgs' => 'otu names should be different';
821 }
822
823 8 ▶ if ( grep {$_ eq $copy_otu_name} @{$self->get_taxlabels} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $copy_otu_name;} @{$self->get_taxlabels;}) 1 7 8
824 1 throw 'BadArgs' => "duplicate otu name [$copy_otu_name] already exists";
825 }
826 7 $logger->debug("orig: $original_otu_name; copy: $copy_otu_name");
827
828 # todo:
829 # a portion of the following code should be re-written as
830 # a stand-alone, utility method
831 7 my $contains_otu = "false";
832 7 foreach my $otu (@{ $self->get_taxlabels() }) {
833 24 $logger->debug("$otu");
834 24 ▶ if ($otu eq $original_otu_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($otu eq $original_otu_name) 7 17 24
835 7 $contains_otu = "true";
836 7 last;
837 }
838 }
839  
840 7 ▶ if ($contains_otu eq "true") {
Branch: 1 branch
BranchTrueFalseTotal
if ($contains_otu eq 'true') { } 7 0 7
841 # otu cloning happens here:
842 # - cycle through all blocks and call add_otu_clone() method on each block
843 7 foreach my $block ( @{ $self->get_blocks() } ) {
844 26 $logger->debug( "> Block: " . $block->get_type() );
845 26 $block->add_otu_clone($original_otu_name, $copy_otu_name);
846 }
847 }
848 else {
849 0 throw 'BadArgs' => "the specified otu [$original_otu_name] does not exist";
850 }
851
852 }
853  
854 =head2 select_blocks
855  
856 Name : select_blocks
857 Usage : $nexus->select_blocks(\@blocknames);
858 Function: select a subset of blocks
859 Returns : a new nexus object
860 Args : a ref to array of block names to be selected
861  
862 =cut
863  
864 0 ▶sub select_blocks {
Subroutine select_blocks: not called CC=2 SCAR=17.9
Pod documented
865 0 my ( $self, $blocknames ) = @_;
866 0 my $nexus = __PACKAGE__->new();
867 0 for my $blockname (@$blocknames) {
868 0 $nexus->add_block( $self->get_block($blockname) );
869 }
870 0 return $nexus;
871 }
872  
873 =head2 exclude_blocks
874  
875 Name : exclude_blocks
876 Usage : $nexus->exclude_blocks(\@blocknames);
877 Function: remove a subset of blocks
878 Returns : a new nexus object
879 Args : a ref to array of block names to be removed
880  
881 =cut
882  
883 0 ▶sub exclude_blocks {
Subroutine exclude_blocks: not called CC=2 SCAR=17.9
Pod documented
884 0 my ( $self, $blocknames ) = @_;
885 0 my $nexus = $self->clone();
886 0 for my $blockname (@$blocknames) {
887 0 $nexus->remove_block($blockname);
888 }
889 0 return $nexus;
890 }
891  
892 =head2 select_otus
893  
894 Name : select_otus
895 Usage : $nexus->select_otus(\@otunames);
896 Function: select a subset of OTUs
897 Returns : a new nexus object
898 Args : a ref to array of OTU names
899  
900 =cut
901  
902 1 ▶sub select_otus {
Subroutine select_otus: called CC=3 SCAR=11.0
Pod documented
903 1 my ( $self, $otunames ) = @_;
904 1 my $nexus = $self->clone();
905  
906 1 for my $block ( @{ $nexus->get_blocks() } ) {
907 # XXX duck-typing probably okay, no? -- RAV
908 #if ( $block->get_type() =~ /^(?:characters|taxa|sets|span|history)$/i )
909 2 ▶ if ( $block->can('select_otus') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block->can("select_otus")) 2 0 2
910 2 $block->select_otus($otunames);
911 }
912 }
913 1 return $nexus;
914 }
915  
916 =head2 exclude_otus
917  
918 Name : exclude_otus
919 Usage : $nexus->exclude_otus(\@otunames);
920 Function: remove a subset of OTUs
921 Returns : a new nexus object
922 Args : a ref to array of OTU names to be removed
923  
924 =cut
925  
926 1 ▶sub exclude_otus {
Subroutine exclude_otus: called CC=6 SCAR=17.9
Pod documented
927 1 my ( $self, $otus ) = @_;
928 1 my @OTUs;
929 1 for my $otu ( @{ $self->get_otus() } ) {
930 4 my $exclude = 0;
931 4 for my $name ( @{$otus} ) {
932 4 ▶ last if ( $otu eq $name ) && ( $exclude = 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $otu eq $name and $exclude = 1 1 3 4
Condition100%2/2
$otu eq $name and $exclude = 1
!ll
31
MC/DC100%1/1
$otu eq $name and $exclude = 1
$otu eq $name
Truth table100%2/2
A
A= $otu eq $name
Aresult
00
11
933 }
934 4 ▶ push( @OTUs, $otu ) unless ($exclude);
Branch: 1 branch
BranchTrueFalseTotal
unless $exclude 3 1 4
935 }
936 1 return $self->select_otus( \@OTUs );
937 }
938  
939 =head2 select_tree
940  
941 Name : select_tree
942 Usage : $nexus->select_tree($treename);
943 Function: select a tree
944 Returns : a new nexus object
945 Args : a tree name
946  
947 =cut
948  
949 0 ▶sub select_tree {
Subroutine select_tree: not called CC=1 SCAR=6.9
Pod documented
950 0 my ( $self, $treename ) = @_;
951 0 my $nexus = $self->clone();
952 0 $nexus->get_block('trees')->select($treename);
953 0 return $nexus;
954 }
955  
956 =head2 select_subtree
957  
958 Name : select_subtree
959 Usage : $nexus->select_subtree($inodename);
960 Function: select a subtree
961 Returns : a new nexus object
962 Args : an internal node name for subtree to be selected
963  
964 =cut
965  
966 0 ▶sub select_subtree {
Subroutine select_subtree: not called CC=4 SCAR=30.0
Pod documented
967 0 my ( $self, $nodename, $treename ) = @_;
968 0 ▶ if ( not defined $nodename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nodename) 0 0 0
969 0 throw 'BadArgs' => 'Need to specify an internal node name for subtree';
970 }
971  
972 0 my $nexus = $self->clone();
973 0 my $treesblock = $nexus->get_block("trees");
974 0 $treesblock->select_subtree( $nodename, $treename );
975 0 my $OTUnames = $treesblock->get_taxlabels();
976 0 $nexus->get_block('taxa')->select_otus($OTUnames);
977  
978 0 for my $block ( @{ $nexus->get_blocks() } ) {
979 # XXX duck-typing probably okay, no? -- RAV
980 #if ( $block->get_type() =~ /^(?:characters|taxa|sets|span|history)$/i )
981 0 ▶ if ( $block->can('select_otus') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block->can("select_otus")) 0 0 0
982 0 $block->select_otus($OTUnames);
983 }
984 }
985  
986 0 return $nexus;
987 }
988  
989 =head2 exclude_subtree
990  
991 Name : exclude_subtree
992 Usage : $nexus->exclude_subtree($inodename);
993 Function: remove a subtree
994 Returns : a new nexus object
995 Args : an internal node for subtree to be removed
996  
997 =cut
998  
999 0 ▶sub exclude_subtree {
Subroutine exclude_subtree: not called CC=4 SCAR=30.0
Pod documented
1000 0 my ( $self, $nodename, $treename ) = @_;
1001 0 ▶ if ( not defined $nodename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nodename) 0 0 0
1002 0 throw 'BadArgs' => 'Need to specify an internal node name for subtree';
1003 }
1004  
1005 0 my $nexus = $self->clone();
1006 0 my $treesblock = $nexus->get_block('trees');
1007 0 $treesblock->exclude_subtree( $nodename, $treename );
1008 0 my $OTUnames = $treesblock->get_taxlabels();
1009  
1010 0 for my $block ( @{ $nexus->get_blocks() } ) {
1011 # XXX duck-typing probably okay, no? --RAV
1012 #if ( $block->get_type() =~ /^(?:characters|taxa|sets|span|history)$/i )
1013 0 ▶ if ( $block->can('select_otus') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block->can("select_otus")) 0 0 0
1014 0 $block->select_otus($OTUnames);
1015 }
1016 }
1017  
1018 0 return $nexus;
1019 }
1020  
1021 =head2 select_chars
1022  
1023 Name : select_chars
1024 Usage : $nexus->select_chars(\@columns);
1025 Function: select a subset of characters
1026 Returns : a new nexus object
1027 Args : a ref to array of character columns
1028  
1029 =cut
1030  
1031 0 ▶sub select_chars {
Subroutine select_chars: not called CC=5 SCAR=34.0
Pod documented
1032 0 my ( $self, $columns, $title ) = @_;
1033 0 my @labels = ();
1034 0 my $nexus = $self->clone();
1035 0 my $block = $nexus->get_block( "characters", $title );
1036 0 $block->select_columns($columns);
1037  
1038 #
1039 # temp change by arlin
1040 # to do this right, we need to separate two systems, column numbers (index + 1)
1041 # and column labels. Default should be to select labels if they exist, and
1042 # otherwise to assigning old column numbers as new labels. An alternative
1043 # to the default would be to leave the new column labels unset (i.e., ignore
1044 # previous labels or numbers).
1045 # print &Dumper($columns);exit;
1046 0 for my $i ( 0 .. $#{ $columns } ) {
1047 0 $labels[$i] = $$columns[$i] + 1;
1048 }
1049  
1050 # use these to set labels
1051 0 ▶ if ( !$block->get_charlabels || @{ $block->get_charlabels } == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $block->get_charlabels or @{$block->get_charlabels;} == 0) 0 0 0
Condition0%0/3
not $block->get_charlabels or @{$block->get_charlabels;} == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not $block->get_charlabels or @{$block->get_charlabels;} == 0
not $block->get_charlabels @{$block->get_charlabels;} == 0
Truth table0%0/3
not(A) or B
A= not $block->get_charlabelsB= @{$block->get_charlabels;} == 0
ABresult
1X1
011
000
1052 0 $block->set_charlabels( \@labels );
1053 }
1054 0 $block = $nexus->get_block("assumptions");
1055 0 ▶ if ($block) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block) 0 0 0
1056 0 $block->select_assumptions($columns);
1057 }
1058  
1059 0 return $nexus;
1060 }
1061  
1062 =head2 exclude_chars
1063  
1064 Name : exclude_chars
1065 Usage : $nexus->exclude_chars($columns,block_type);
1066 Function: exclude specified columns from a block.
1067 Returns : new nexus object
1068 Args : column numbers to exclude as array reference, block_type as string
1069  
1070 =cut
1071  
1072 0 ▶sub exclude_chars {
Subroutine exclude_chars: not called CC=3 SCAR=24.8
Pod documented
1073 0 my ( $self, $columns, $title ) = @_;
1074 0 my $block = $self->get_block( "characters", $title );
1075  
1076 0 my $len = $block->get_dimensions()->{'nchar'};
1077 0 print "$len\n";
1078 0 my @columns = ( -1, @{$columns}, $len );
1079 0 my @select = ();
1080 0 for my $i ( 0 .. $#columns ) {
1081 0 ▶ for ( my $j = $columns[$i] + 1; $j < $columns[ $i + 1 ]; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $columns[$i + 1]) 0 0 0
1082 0 push @select, $j;
1083 }
1084 }
1085 0 print "@select\n";
1086 0 return $self->select_chars( \@select, $title );
1087 }
1088  
1089 =head2 reroot
1090  
1091 Name : reroot
1092 Usage : $nexus->reroot($outgroupname);
1093 Function: reroot the tree using the new outgroup
1094 Returns : a new nexus object
1095 Args : a OTU name as new outgroup
1096  
1097 =cut
1098  
1099 0 ▶sub reroot {
Subroutine reroot: not called CC=2 SCAR=17.9
Pod documented
1100 0 my ( $self, $outgroup, $root_position, $treename ) = @_;
1101 0 my $nexus = $self->clone();
1102 0 my $trees = $nexus->get_block('trees');
1103 0 ▶ if ( defined $treename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $treename) { } 0 0 0
1104 0 $trees->reroot_tree( $outgroup, $root_position, $treename );
1105 }
1106 else {
1107 0 $trees->reroot_all_trees( $outgroup, $root_position );
1108 }
1109 0 return $nexus;
1110 }
1111  
1112 =head2 equals
1113  
1114 Name : equals
1115 Usage : $nexus->equals($another);
1116 Function: compare if two Bio::NEXUS objects are equal
1117 Returns : boolean
1118 Args : a Bio::NEXUS object
1119  
1120 =cut
1121  
1122 1 ▶sub equals {
Subroutine equals: called CC=10 SCAR=26.2
Pod documented
1123 1 my ( $self, $nexus ) = @_;
1124 1 my @blocks1 = @{ $self->get_blocks() };
1125 1 my @blocks2 = @{ $nexus->get_blocks() };
1126 0 ▶ if ( @blocks1 != @blocks2 ) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (@blocks1 != @blocks2) 0 1 1
1127 @blocks1 = sort {
1128 1 ▶ $a->get_type()
Condition50%2/4
$a->get_title || ''
l!l
03
$b->get_title || ''
l!l
03
MC/DC0%0/1
$a->get_title || ''
$a->get_title
MC/DC0%0/1
$b->get_title || ''
$b->get_title
Truth table50%1/2
A
A= $a->get_title
Aresult
11
00
Truth table50%1/2
A
A= $b->get_title
Aresult
11
00
1129 . ( $a->get_title || '' ) cmp $b->get_type()
1130 . ( $b->get_title || '' )
1131 } @blocks1;
1132 @blocks2 = sort {
1133 1 ▶ $a->get_type()
Condition50%2/4
$a->get_title || ''
l!l
03
$b->get_title || ''
l!l
03
MC/DC0%0/1
$a->get_title || ''
$a->get_title
MC/DC0%0/1
$b->get_title || ''
$b->get_title
Truth table50%1/2
A
A= $a->get_title
Aresult
11
00
Truth table50%1/2
A
A= $b->get_title
Aresult
11
00
1134 . ( $a->get_title || '' ) cmp $b->get_type()
1135 . ( $b->get_title || '' )
1136 } @blocks2;
1137  
1138 1 ▶ for ( my $i = 0; $i < @blocks1; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @blocks1) 3 1 4
1139 0 ▶ if ( ( !$blocks1[$i] ) && ( !$blocks2[$i] ) ) { next; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($blocks1[$i] or $blocks2[$i]) 0 3 3
Condition33%1/3
$blocks1[$i] or $blocks2[$i]
l!l&&r!l&&!r
300
MC/DC0%0/2
$blocks1[$i] or $blocks2[$i]
$blocks1[$i] $blocks2[$i]
Truth table33%1/3
A or B
A= $blocks1[$i]B= $blocks2[$i]
ABresult
1X1
011
000
1140 3 ▶ if ( !$blocks1[$i]->equals( $blocks2[$i] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($blocks1[$i]->equals($blocks2[$i])) 0 3 3
1141  
1142 # print &Dumper($blocks1[$i]);
1143 # print &Dumper($blocks2[$i]);
1144 0 return 0;
1145 }
1146 }
1147 1 return 1;
1148 }
1149  
1150 =head2 write
1151  
1152 Name : write
1153 Usage : $nexus->write($filename, $verbose);
1154 Function: Writes to NEXUS file from stored NEXUS data
1155 Returns : none
1156 Args : file name (string) for output to file or '-' or 'STDOUT' for standard output
1157  
1158 =cut
1159  
1160 2 ▶sub write {
Subroutine write: called CC=11 SCAR=24.9
Pod documented
1161 2 my ( $self, $filename, $verbose ) = @_;
1162 2 my $fh;
1163  
1164 2 ▶ if ( ref($filename) eq "GLOB" ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $filename eq 'GLOB') { } 1 1 2
elsif ($filename eq '-' or $filename eq \*STDOUT) { } 0 1 1
1165 1 $fh = $filename;
1166 }
1167 ▶ elsif ( $filename eq "-" || $filename eq \*STDOUT ) {
Condition33%1/3
$filename eq "-" or $filename eq \*STDOUT
l!l&&r!l&&!r
001
MC/DC0%0/2
$filename eq "-" or $filename eq \*STDOUT
$filename eq "-" $filename eq \*STDOUT
Truth table33%1/3
A or B
A= $filename eq "-"B= $filename eq \*STDOUT
ABresult
1X1
011
000
1168 0 $fh = \*STDOUT;
1169 }
1170 else {
1171 1 ▶ open( $fh, ">$filename" ) || throw 'FileError' => $!;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">$filename" or &throw("FileError", $!) 1 0 1
1172 }
1173  
1174 2 print $fh "#NEXUS\n\n";
1175  
1176 2 my @blocks_and_comments = @{ $self->get_blocks_and_comments() };
1177  
1178 # First, print any comments that are at the top level
1179 2 ▶ for ( my $i = 0; $i < @blocks_and_comments; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @blocks_and_comments) 4 0 4
1180 4 ▶ if ( _is_comment( $blocks_and_comments[$i] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_comment($blocks_and_comments[$i])) { } 2 2 4
1181 2 print $fh "$blocks_and_comments[$i]\n\n";
1182 2 shift @blocks_and_comments;
1183 2 $i--;
1184 2 next;
1185 }
1186 else {
1187 2 last;
1188 }
1189 }
1190  
1191 # Then print the TAXA Block
1192 2 $self->set_taxablock;
1193 2 $self->get_block('taxa')->_write( $fh, $verbose );
1194 2 print $fh "\n";
1195  
1196 # And print whatever else there is
1197 2 for my $block_or_comment (@blocks_and_comments) {
1198 6 ▶ if ( _is_comment($block_or_comment) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_comment($block_or_comment)) 0 6 6
1199 0 print $fh "$block_or_comment\n\n";
1200 0 next;
1201 }
1202 6 my $type = $block_or_comment->get_type();
1203 6 ▶ if ( lc $type eq 'taxa' ) { next; }
Branch: 1 branch
BranchTrueFalseTotal
if (lc $type eq "taxa") 2 4 6
1204 4 $block_or_comment->_write($fh);
1205 4 print $fh "\n";
1206 }
1207
1208 # if $fh is STDOUT, don't close it!
1209 2 ▶ close($fh) unless ($fh == \*STDOUT);
Branch: 1 branch
BranchTrueFalseTotal
unless $fh == \*STDOUT 2 0 2
1210 }
1211  
1212 =head2 set_taxablock
1213  
1214 Name : set_taxablock
1215 Usage : $nexus->set_taxablock();
1216 Function: Sets taxablock if taxablock is not already defined in the nexus object
1217 Returns : none
1218 Args : none
1219  
1220 =cut
1221  
1222 9 ▶sub set_taxablock {
Subroutine set_taxablock: called CC=8 SCAR=21.2
Pod documented
1223 9 my $self = shift;
1224 9 ▶ if ( not defined $self->get_block('taxa') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->get_block("taxa")) 7 2 9
1225 7 for my $block ( @{ $self->get_blocks } ) {
1226 7 my $block_type = lc $block->get_type();
1227 7 ▶ if ( $block_type eq 'characters' || $block_type eq 'trees' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_type eq "characters" or $block_type eq "trees") 7 0 7
Condition66%2/3
$block_type eq "characters" or $block_type eq "trees"
l!l&&r!l&&!r
610
MC/DC0%0/2
$block_type eq "characters" or $block_type eq "trees"
$block_type eq "characters" $block_type eq "trees"
Truth table66%2/3
A or B
A= $block_type eq "characters"B= $block_type eq "trees"
ABresult
1X1
011
000
1228 7 my $taxlabels = $block->get_taxlabels();
1229 7 ▶ if ( ( not defined $taxlabels ) or ( not @$taxlabels ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $taxlabels and @$taxlabels) 1 6 7
Condition66%2/3
defined $taxlabels and @$taxlabels
!ll&&!rl&&r
016
MC/DC50%1/2
defined $taxlabels and @$taxlabels
defined $taxlabels @$taxlabels
Truth table66%2/3
A and B
A= defined $taxlabelsB= @$taxlabels
ABresult
0X0
100
111
1230 1 ▶ if ( $block_type eq 'trees' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_type eq 'trees') { } 1 0 1
1231 1 $block->set_taxlabels(
1232 $block->get_tree()->get_node_names() );
1233 }
1234 else {
1235 0 $block->set_taxlabels(
1236 $block->get_otuset->get_otu_names() );
1237 }
1238 1 $taxlabels = $block->get_taxlabels();
1239 }
1240 7 my $taxa_block = new Bio::NEXUS::TaxaBlock('taxa');
1241 7 $taxa_block->set_taxlabels($taxlabels);
1242 7 $self->add_block($taxa_block);
1243 7 return;
1244 }
1245 }
1246 }
1247 }
1248  
1249 86 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=21.1
1250 86 ▶ return if $AUTOLOAD =~ /DESTROY$/;
Branch: 1 branch
BranchTrueFalseTotal
if $Bio::NEXUS::AUTOLOAD =~ /DESTROY$/ 86 0 86
1251 0 my $package_name = __PACKAGE__ . '::';
1252  
1253 # The following methods are deprecated and are temporarily supported
1254 # via a warning and a redirection
1255 0 my %synonym_for =
1256 ( "${package_name}is_comment" => 'Bio::NEXUS::Functions::_is_comment',
1257 );
1258  
1259 0 ▶ if ( defined $synonym_for{$AUTOLOAD} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $synonym_for{$Bio::NEXUS::AUTOLOAD}) { } 0 0 0
1260 0 $logger->warn("$AUTOLOAD() is deprecated; use $synonym_for{$AUTOLOAD}() instead");
1261 0 goto &{ $synonym_for{$AUTOLOAD} };
1262 }
1263 else {
1264 0 throw 'UnknownMethod' => "Unknown method $AUTOLOAD called";
1265 }
1266 }
1267  
1268 35 1;
« lib/Bio/NEXUS/Functions.pm ↑ Summary script/nextool.pl »