lib/Bio/NEXUS/UnalignedBlock.pm

filter: Statementstmt 56.8% 95 / 167 Branchbran 41.3% 24 / 58 Conditioncond 14.2% 2 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 54.1% 13 / 24 Podpod 100% 11 / 11 totaltotal 51.4% 145 / 282 CC 44 SCAR 28.3
CC
44
Cov
51%
_write_matrix_info
72.0
_parse_matrix
24.7
_write_matrix
12.0
CRAP 17.0
« lib/Bio/NEXUS/CharactersBlock.pm ↑ Summary lib/Bio/NEXUS/Functions.pm »
1 #######################################################################
2 # UnalignedBlock.pm
3 #######################################################################
4 #
5 # thanks to Tom Hladish for the original version
6 #
7 # $Id: UnalignedBlock.pm,v 1.25 2012/02/10 13:28:28 astoltzfus Exp $
8  
9 #################### START POD DOCUMENTATION ##########################
10  
11 =head1 NAME
12  
13 Bio::NEXUS::UnalignedBlock - Represents an UNALIGNED block of a NEXUS file
14  
15 =head1 SYNOPSIS
16  
17 if ( $type =~ /unaligned/i ) {
18 $block_object = new Bio::NEXUS::UnalignedBlock($type, $block, $verbose);
19 }
20  
21 =head1 DESCRIPTION
22  
23 This is a class representing an unaligned block in NEXUS file
24  
25 =head1 FEEDBACK
26  
27 All feedback (bugs, feature enhancements, etc.) is greatly appreciated.
28  
29 =head1 AUTHORS
30  
31 Thomas Hladish (tjhladish at yahoo)
32  
33 =head1 VERSION
34  
35 $Id: UnalignedBlock.pm,v 1.25 2012/02/10 13:28:28 astoltzfus Exp $
36  
37 =head1 METHODS
38  
39 =cut
40  
41 package Bio::NEXUS::UnalignedBlock;
42  
43 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 #use Data::Dumper; # XXX this is not used, might as well not import it!
45 #use Carp;# XXX this is not used, might as well not import it!
46 35 ▶use Bio::NEXUS::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 35 ▶use Bio::NEXUS::TaxUnitSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 35 ▶use Bio::NEXUS::Matrix;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 35 ▶use Bio::NEXUS::Util::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 35 ▶use vars qw(@ISA $VERSION $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
51 35 ▶use Bio::NEXUS; $VERSION = $Bio::NEXUS::VERSION;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 35 @ISA = qw(Bio::NEXUS::Matrix);
53 35 my $logger = Bio::NEXUS::Util::Logger->new();
54  
55 =head2 new
56  
57 Title : new
58 Usage : block_object = new Bio::NEXUS::UnalignedBlock($block_type, $commands, $verbose, $taxlabels);
59 Function: Creates a new Bio::NEXUS::UnalignedBlock object
60 Returns : Bio::NEXUS::UnalignedBlock object
61 Args : type (string), the commands/comments to parse (array ref), and a verbose flag (0 or 1)
62  
63 =cut
64  
65 2 ▶sub new {
Subroutine new: called CC=4 SCAR=15.0
Pod documented
66 2 my ( $class, $type, $commands, $verbose, $taxa ) = @_;
67 0 ▶ unless ($type) { ( $type = lc $class ) =~ s/Bio::NEXUS::(.+)Block/$1/i; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 0 2 2
68 2 my $self = { type => $type };
69 2 bless $self, $class;
70 2 $self->set_taxlabels($taxa);
71 2 $self->{'otuset'} = new Bio::NEXUS::TaxUnitSet();
72 2 ▶ $self->_parse_block( $commands, $verbose )
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
73 if ( ( defined $commands ) and @$commands );
74 1 return $self;
75 }
76  
77 =begin comment
78  
79 Title : _parse_format
80 Usage : $format = $self->_parse_format($buffer); (private)
81 Function: Extracts format values from line and stores values in a hash
82 Returns : hash of formats
83 Args : buffer (string)
84 Methods : Separates formats by whitespace and creates hash containing
85 key = format name and value = format value.
86  
87 =end comment
88  
89 =cut
90  
91 1 ▶sub _parse_format {
Subroutine _parse_format: called CC=5 SCAR=18.7
92 1 my ( $self, $string ) = @_;
93  
94 1 my %format = ();
95  
96 1 ▶ while ( $string =~ s/(\S+\s*=\s*[\"|\'][^\"\']+[\"|\'])// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/(\S+\s*=\s*[\"|\'][^\"\']+[\"|\'])//) 0 1 1
97 0 my ( $name, $symbol ) = split /\s*=\s*/, $1;
98 0 $format{ lc $name } = $symbol;
99 }
100 1 ▶ while ( $string =~ s/(\S+\s*=\s*\S+)// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/(\S+\s*=\s*\S+)//) 1 1 2
101 1 my ( $name, $symbol ) = split /\s*=\s*/, $1;
102 1 $format{ lc $name } = lc $symbol;
103 }
104 1 for my $other ( split /\s+/, $string ) {
105 0 ▶ if ($other) { $format{ lc $other } = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($other) 0 0 0
106 }
107 1 return \%format;
108 }
109  
110 =begin comment
111  
112 Title : _parse_matrix
113 Usage : $self->_parse_matrix($buffer); (private)
114 Function: Processes buffer containing matrix data
115 Returns : none
116 Args : buffer (string)
117 Method : parse according to if name is quoted string or single word,
118 if each state is single character or multi-character (use token keyword)
119  
120 =end comment
121  
122 =cut
123  
124 1 ▶sub _parse_matrix {
Subroutine _parse_matrix: called CC=14 SCAR=32.1
125 1 my ( $self, $matrix, $verbose ) = @_;
126  
127 1 my @taxa;
128 1 my ( $name, $seq ) = ();
129  
130 # Build an array of hashrefs, where each hash has "name" and "seq" values
131 # corresponding to the name and sequence found in each row of the matrix
132 1 for my $row ( split /\n|\r/, $matrix ) {
133 0 ▶ if ( $row =~ /^\s*$/ ) { next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($row =~ /^\s*$/) 0 4 4
134
135 #for quoted taxon name
136 4 ▶ if ( $row =~ /^\s*[\"|\']([^\"\']+)[\"|\']\s*([^\[]*)(\[.*\]\s*)*/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row =~ /^\s*[\"|\']([^\"\']+)[\"|\']\s*([^\[]*)(\[.*\]\s*)*/) { } 0 4 4
137 0 ( $name, $seq ) = ( $1, $2 );
138 0 $name =~ s/\s+/_/g;
139 0 ▶ if ( !$self->find_taxon($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->find_taxon($name)) 0 0 0
140 0 Bio::NEXUS::Util::Exceptions::BadArgs->throw(
141 'error' => "Undefined Taxon: $name"
142 );
143 }
144 }
145 else {
146  
147 # for one-word non-quoted taxon name
148 4 $row =~ /^\s*(\S+)(\s*)([^\[]*)(\[.*\]\s*)*/;
149 4 ▶ if ( $self->find_taxon($1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->find_taxon($1)) { } 4 0 4
150 4 $name = $1;
151 4 $seq = $3;
152 #print Dumper $seq;
153 }
154 else {
155 0 ▶ print "taxon name $1 not found\n" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
156 0 $seq = $1 . $2 . $3;
157 }
158 }
159 #print "> row: $row\n";
160 #print "> name: $name\n";
161 #print "> seq: $seq\n";
162
163 4 my $newtaxon = 1;
164 4 for my $taxon (@taxa) {
165 6 ▶ if ( $taxon->{'name'} eq $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($taxon->{"name"} eq $name) 0 6 6
166 0 $taxon->{'seq'} .= ' ' . $seq;
167 0 $newtaxon = 0;
168 }
169 }
170 4 ▶ if ($newtaxon) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newtaxon) 4 0 4
171 4 push @taxa, { name => $name, seq => $seq };
172 }
173 }
174 #print '> @taxa: ';
175 # split each character
176 1 my @otus;
177 #print Dumper \@taxa;
178 1 for my $taxon (@taxa) {
179 4 $seq = $taxon->{'seq'};
180 4 $seq =~ s/^\s*(.*\S)\s*$/$1/;
181  
182 4 my @seq;
183 4 ▶ while ( $seq =~ s/([^\(]+)|\(([^\(]+)\)// ) { # for +-(+ -)+-
Branch: 1 branch
BranchTrueFalseTotal
if ($seq =~ s/([^\(]+)|\(([^\(]+)\)//) 4 4 8
184 4 ▶ if ($1) { # for +-
Branch: 2 branches
BranchTrueFalseTotal
if ($1) { } 4 0 4
elsif ($2) { } 0 0 0
185 ### The following 4 commented lines of code are implemented in CharactersBlock.pm; they allow data tokens to be space-delimited.
186 ### Unaligned blocks do not include the tokens or continuous formats according the Maddison et al. We
187 ### may decide that we don't want to restrict unaligned data to DNA/RNA/AA the way Maddison et al have.
188 # if ($self->get_format->{'tokens'} || lc $self->get_format->{'datatype'} eq 'continuous') { #LINE 1
189 # push @seq, split /\s+/, $1; #LINE 2
190 # } else { #LINE 3
191 4 push @seq, split /\s*/, $1;
192  
193 # } #LINE4
194 }
195 elsif ($2) {
196 0 push @seq, [ split /,\s*|\s+/, $2 ]; # for (+ -)
197 }
198 }
199  
200 4 push @otus, Bio::NEXUS::TaxUnit->new( $taxon->{'name'}, \@seq );
201 }
202
203 1 my $otuset = $self->get_otuset();
204 1 $otuset->set_otus( \@otus );
205 1 $self->set_taxlabels( $otuset->get_otu_names() );
206 1 return \@otus;
207 }
208  
209 =head2 find_taxon
210  
211 Title : find_taxon
212 Usage : my $is_taxon_present = $self->find_taxon($taxon_name);
213 Function: Finds whether the input taxon name is present in the taxon label.
214 Returns : 0 (not present) or 1 (if present).
215 Args : taxon label (as string)
216  
217 =cut
218  
219 7 ▶sub find_taxon {
Subroutine find_taxon: called CC=5 SCAR=16.5
Pod documented
220 7 my ( $self, $name ) = @_;
221 0 ▶ if ( @{ $self->get_taxlabels || [] } == 0 ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->get_taxlabels or [];} == 0) 0 7 7
Condition50%1/2
$self->get_taxlabels || []
l!l
70
MC/DC0%0/1
$self->get_taxlabels || []
$self->get_taxlabels
Truth table50%1/2
A
A= $self->get_taxlabels
Aresult
11
00
222 7 for my $taxon ( @{ $self->get_taxlabels() } ) {
223 21 ▶ if ( lc $taxon eq lc $name ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (lc $taxon eq lc $name) 5 16 21
224 }
225 2 return 0;
226 }
227  
228 =head2 set_format
229  
230 Title : set_format
231 Usage : $block->set_format(\%format);
232 Function: set the format of the characters
233 Returns : none
234 Args : hash of format values
235  
236 =cut
237  
238 0 ▶sub set_format {
Subroutine set_format: not called CC=1 SCAR=6.9
Pod documented
239 0 my ( $self, $format ) = @_;
240 0 $self->{'format'} = $format;
241 }
242  
243 =head2 get_format
244  
245 Title : get_format
246 Usage : $block->get_format();
247 Function: Returns the format of the characters
248 Returns : hash of format values
249 Args : none
250  
251 =cut
252  
253 0 ▶sub get_format { shift->{'format'} || {} }
Subroutine get_format: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"format"} or {} 0 0 0
254  
255 =head2 set_otuset
256  
257 Title : set_otuset
258 Usage : $block->set_otuset($otuset);
259 Function: Set the otus
260 Returns : none
261 Args : TaxUnitSet object
262  
263 =cut
264  
265 0 ▶sub set_otuset {
Subroutine set_otuset: not called CC=1 SCAR=6.9
Pod documented
266 0 my ( $self, $otuset ) = @_;
267 0 $self->{'otuset'} = $otuset;
268 0 $self->set_taxlabels( $otuset->get_otu_names() );
269 }
270  
271 =head2 set_charstatelabels
272  
273 Title : set_charstatelabels
274 Usage : $block->set_charstatelabels($labels);
275 Function: Set the character names and states
276 Returns : none
277 Args : array of character states
278  
279 =cut
280  
281 0 ▶sub set_charstatelabels {
Subroutine set_charstatelabels: not called CC=1 SCAR=6.9
Pod documented
282 0 my ( $self, $charstatelabels ) = @_;
283 0 $self->get_otuset->set_charstatelabels($charstatelabels);
284 }
285  
286 =head2 get_charstatelabels
287  
288 Title : get_charstatelabels
289 Usage : $set->get_charstatelabels();
290 Function: Returns an array of character states
291 Returns : character states
292 Args : none
293  
294 =cut
295  
296 0 ▶sub get_charstatelabels {
Subroutine get_charstatelabels: not called CC=1 SCAR=6.9
Pod documented
297 0 my ($self) = @_;
298 0 return $self->get_otuset->get_charstatelabels();
299 }
300  
301 =head2 get_ntax
302  
303 Title : get_ntax
304 Usage : $block->get_ntax();
305 Function: Returns the number of taxa of the block
306 Returns : # taxa
307 Args : none
308  
309 =cut
310  
311 0 ▶sub get_ntax {
Subroutine get_ntax: not called CC=1 SCAR=6.9
Pod documented
312 0 my $self = shift;
313 0 return $self->get_otuset()->get_ntax();
314 }
315  
316 =head2 rename_otus
317  
318 Title : rename_otus
319 Usage : $block->rename_otus(\%translation);
320 Function: Renames all the OTUs to something else
321 Returns : none
322 Args : hash containing translation
323  
324 =cut
325  
326 0 ▶sub rename_otus {
Subroutine rename_otus: not called CC=1 SCAR=6.9
Pod documented
327 0 my ( $self, $translation ) = @_;
328 0 $self->get_otuset()->rename_otus($translation);
329 }
330  
331 =head2 add_otu_clone
332  
333 Title : add_otu_clone
334 Usage : ...
335 Function: ...
336 Returns : ...
337 Args : ...
338  
339 =cut
340  
341 1 ▶sub add_otu_clone {
Subroutine add_otu_clone: called CC=6 SCAR=18.3
Pod documented
342 1 my ( $self, $original_otu_name, $copy_otu_name ) = @_;
343 # print "Warning: Bio::NEXUS::UnalignedBlock::add_otu_clone() method not fully implemented\n";
344
345 1 ▶ if ($self->find_taxon($copy_otu_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->find_taxon($copy_otu_name)) { } 0 1 1
346 0 print "Error: an OTU with that name [$copy_otu_name] already exists.\n";
347 }
348 else {
349 1 $self->add_taxlabel($copy_otu_name);
350 }
351
352 1 my @otu_set = ();
353 1 ▶ if (defined $self->{'otuset'}->{'otus'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"otuset"}{"otus"}) 1 0 1
354 1 @otu_set = @{ $self->{'otuset'}->{'otus'} };
355 }
356 1 foreach my $otu (@otu_set) {
357 4 ▶ if (defined $otu) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $otu) 4 0 4
358 4 ▶ if ($otu->get_name() eq $original_otu_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($otu->get_name eq $original_otu_name) 1 3 4
359 1 my $otu_clone = $otu->clone();
360 1 $otu_clone->set_name($copy_otu_name);
361 1 $self->{'otuset'}->add_otu($otu_clone);
362 }
363 }
364 }
365
366 }
367  
368 =head2 equals
369  
370 Name : equals
371 Usage : $block->equals($another);
372 Function: compare if two Bio::NEXUS::UnalignedBlock objects are equal
373 Returns : boolean
374 Args : a Bio::NEXUS::CharactersBlock object
375  
376 =cut
377  
378 0 ▶sub equals {
Subroutine equals: not called CC=2 SCAR=17.9
Pod documented
379 0 my ( $self, $block ) = @_;
380 0 ▶ if ( !Bio::NEXUS::Block::equals( $self, $block ) ) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
unless (Bio::NEXUS::Block::equals($self, $block)) 0 0 0
381 0 return $self->get_otuset()->equals( $block->get_otuset() );
382 }
383  
384 =begin comment
385  
386 Name : _write
387 Usage : $block->_write();
388 Function: Writes NEXUS block containing unaligned data
389 Returns : none
390 Args : file name (string)
391  
392 =end comment
393  
394 =cut
395  
396 0 ▶sub _write {
Subroutine _write: not called CC=2 SCAR=17.9
397 0 my ( $self, $fh, $verbose ) = @_;
398 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
399  
400 0 Bio::NEXUS::Block::_write( $self, $fh );
401 0 $self->_write_matrix_info( $fh, $verbose );
402 0 $self->_write_matrix( $fh, $verbose );
403 0 print $fh "END;\n";
404 0 return;
405 }
406  
407 =begin comment
408  
409 Name : _write_matrix_info
410 Usage : $self->_write_matrix_info($file_handle,$verbose);
411 Function: Writes UnalignedBlock info (all the block content except the matrix data) into the filehandle
412 Returns : none
413 Args : $file_handle and $verbose
414  
415 =end comment
416  
417 =cut
418  
419 0 ▶sub _write_matrix_info {
Subroutine _write_matrix_info: not called CC=8 SCAR=42.8
420 0 my ( $self, $fh, $verbose ) = @_;
421 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
422  
423 0 my $ntax = $self->get_ntax();
424 0 print $fh "\tDIMENSIONS ntax=$ntax;\n";
425  
426 0 my %formats = %{ $self->get_format() };
427 0 ▶ if ( scalar keys %formats ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %formats) 0 0 0
428 0 print $fh "\tFORMAT ";
429 0 ▶ if ( defined $formats{'datatype'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $formats{"datatype"}) 0 0 0
430 0 print $fh " datatype=$formats{'datatype'}";
431 }
432  
433 0 for my $format ( keys %formats ) {
434 0 ▶ if ( !$formats{$format} || $format =~ /datatype/i ) { next; }
Branch: 2 branches
BranchTrueFalseTotal
if (not $formats{$format} or $format =~ /datatype/i) { } 0 0 0
elsif ($formats{$format} eq '1') { } 0 0 0
Condition0%0/3
not $formats{$format} or $format =~ /datatype/i
l!l&&r!l&&!r
000
MC/DC0%0/2
not $formats{$format} or $format =~ /datatype/i
not $formats{$format} $format =~ /datatype/i
Truth table0%0/3
not(A) or B
A= not $formats{$format}B= $format =~ /datatype/i
ABresult
1X1
011
000
435 elsif ( $formats{$format} eq '1' ) {
436 0 print $fh " $format";
437 }
438 else {
439 0 print $fh " $format=$formats{$format}";
440 }
441 }
442 0 print $fh ";\n";
443 }
444 0 return;
445 }
446  
447 =begin comment
448  
449 Name : _write_matrix
450 Usage : $self->_write_matrix($file_handle,$verbose);
451 Function: Writes UnalignedBlock matrix( The data stored in the matrix command) into the filehandle
452 Returns : none
453 Args : $file_handle and $verbose
454  
455 =end comment
456  
457 =cut
458  
459 0 ▶sub _write_matrix {
Subroutine _write_matrix: not called CC=3 SCAR=24.8
460 0 my ( $self, $fh, $verbose ) = @_;
461 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
462  
463 0 my @otus = @{ $self->get_otuset()->get_otus() };
464 0 print $fh "\tMATRIX\n";
465 0 for my $otu (@otus) {
466 0 my $seq = $otu->get_seq_string();
467 0 print $fh "\t", $otu->get_name(), "\t", $seq, "\n";
468 }
469 0 print $fh "\t;\n";
470 0 return;
471 }
472  
473 3 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=12.0
474 3 ▶ return if $AUTOLOAD =~ /DESTROY$/;
Branch: 1 branch
BranchTrueFalseTotal
if $Bio::NEXUS::UnalignedBlock::AUTOLOAD =~ /DESTROY$/ 2 1 3
475 1 my $package_name = __PACKAGE__ . '::';
476  
477 # The following methods are deprecated and are temporarily supported
478 # via a warning and a redirection
479 1 my %synonym_for = (
480 "${package_name}set_charstates" => "${package_name}set_charstatelabels",
481 "${package_name}get_charstates" => "${package_name}get_charstatelabels",
482 );
483  
484 1 ▶ if ( defined $synonym_for{$AUTOLOAD} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $synonym_for{$Bio::NEXUS::UnalignedBlock::AUTOLOAD}) { } 0 1 1
485 0 $logger->warn( "$AUTOLOAD() is deprecated; use $synonym_for{$AUTOLOAD}() instead" );
486 0 goto &{ $synonym_for{$AUTOLOAD} };
487 }
488 else {
489 1 Bio::NEXUS::Util::Exceptions::UnknownMethod->throw(
490 'error' => "ERROR: Unknown method $AUTOLOAD called"
491 );
492 }
493 }
494  
495 35 1;
« lib/Bio/NEXUS/CharactersBlock.pm ↑ Summary lib/Bio/NEXUS/Functions.pm »