| 1 | ###################################################### | ||||||||||||||||||||||||||||
| 2 | # TaxaBlock.pm | ||||||||||||||||||||||||||||
| 3 | ###################################################### | ||||||||||||||||||||||||||||
| 4 | # original version thanks to Chengzhi, Weigang, Eugene, Peter and Tom | ||||||||||||||||||||||||||||
| 5 | # $Id: TaxaBlock.pm,v 1.45 2012/02/07 21:38:09 astoltzfus Exp $ | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | #################### START POD DOCUMENTATION ################## | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | Bio::NEXUS::TaxaBlock - Represents TAXA block of a NEXUS file | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | if ( $type =~ /taxa/i ) { | ||||||||||||||||||||||||||||
| 16 | $block_object = new Bio::NEXUS::TaxaBlock($type, $block, $verbose); | ||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | If a NEXUS block is a taxa block, this module parses the block and stores the taxonomic data. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 FEEDBACK | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | All feedback (bugs, feature enhancements, etc.) are greatly appreciated. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head1 VERSION | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | $Id: TaxaBlock.pm,v 1.45 2012/02/07 21:38:09 astoltzfus Exp $ | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =head1 METHODS | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =cut | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | package Bio::NEXUS::TaxaBlock; | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 35 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 35 | ▶ | use Bio::NEXUS::Functions; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 35 | ▶ | use Bio::NEXUS::Node; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 35 | ▶ | use Bio::NEXUS::Block; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 35 | ▶ | use Bio::NEXUS::TaxUnit; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 35 | ▶ | use Bio::NEXUS::Util::Logger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 35 | ▶ | use Bio::NEXUS::Util::Exceptions 'throw'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 35 | ▶ | use vars qw(@ISA $VERSION $AUTOLOAD); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 35 | ▶ | use Bio::NEXUS; $VERSION = $Bio::NEXUS::VERSION; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 35 | @ISA = qw(Bio::NEXUS::Block); | |||||||||||||||||||||||||||
| 48 | 35 | my $logger = Bio::NEXUS::Util::Logger->new(); | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =head2 new | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | Title : new | ||||||||||||||||||||||||||||
| 53 | Usage : block_object = new Bio::NEXUS::TaxaBlock($block_type, $commands, $verbose); | ||||||||||||||||||||||||||||
| 54 | Function: Creates a new Bio::NEXUS::TaxaBlock object | ||||||||||||||||||||||||||||
| 55 | Returns : Bio::NEXUS::TaxaBlock object | ||||||||||||||||||||||||||||
| 56 | Args : type (string), the commands/comments to parse (array ref), and a verbose flag (0 or 1; optional) | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 86 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 86 | my ( $class, $type, $commands, $verbose ) = @_; | |||||||||||||||||||||||||||
| 62 | 86 | ▶ | if ( not $type ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | ( $type = lc $class ) =~ s/Bio::NEXUS::(.+)Block/$1/i; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 86 | my $self = { | |||||||||||||||||||||||||||
| 66 | 'type' => $type, | ||||||||||||||||||||||||||||
| 67 | }; | ||||||||||||||||||||||||||||
| 68 | 86 | bless $self, $class; | |||||||||||||||||||||||||||
| 69 | 86 | ▶ | if ( defined $commands and @$commands ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $commands and @$commands
MC/DC50%1/2
defined $commands and @$commands
defined $commands
@$commands
Truth table66%2/3
A and B
A= defined $commandsB= @$commands
| |||||||||||||||||||||||||||||
| 70 | 79 | $self->_parse_block( $commands, $verbose ); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | 85 | return $self; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =head2 is_taxon | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | Title : is_taxon | ||||||||||||||||||||||||||||
| 78 | Usage : $block->is_taxon($query_taxonlabel); | ||||||||||||||||||||||||||||
| 79 | Function: Validates OTU names/taxlabels | ||||||||||||||||||||||||||||
| 80 | Returns : Returns taxlabel if true, undef if false | ||||||||||||||||||||||||||||
| 81 | Args : Query taxon label | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub is_taxon { | ||||||||||||||||||||||||||
|
Subroutine
is_taxon: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 0 | my ( $self, $query_taxon, $verbose ) = @_; | |||||||||||||||||||||||||||
| 87 | 0 | my $taxlabels = $self->get_taxlabels(); | |||||||||||||||||||||||||||
| 88 | 0 | for my $taxlabel (@$taxlabels) { | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | if ( $taxlabel eq $query_taxon ) { return $taxlabel } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 0 | $logger->info("$query_taxon is not a valid OTU name"); | |||||||||||||||||||||||||||
| 92 | 0 | return undef; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head2 get_ntax | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | Title : get_ntax | ||||||||||||||||||||||||||||
| 98 | Usage : $block->get_ntax(); | ||||||||||||||||||||||||||||
| 99 | Function: Returns the dimensions (that is, ntax) of the block | ||||||||||||||||||||||||||||
| 100 | Returns : dimensions (integer) | ||||||||||||||||||||||||||||
| 101 | Args : none | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =cut | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 5 | ▶ | sub get_ntax { | ||||||||||||||||||||||||||
|
Subroutine
get_ntax: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 107 | 5 | return scalar @{ $self->get_taxlabels() }; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head2 rename_otus | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Title : rename_otus | ||||||||||||||||||||||||||||
| 113 | Usage : $block->rename_otus(\%translation); | ||||||||||||||||||||||||||||
| 114 | Function: Renames all the OTUs to something else | ||||||||||||||||||||||||||||
| 115 | Returns : none | ||||||||||||||||||||||||||||
| 116 | Args : hash containing translation | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =cut | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 2 | ▶ | sub rename_otus { | ||||||||||||||||||||||||||
|
Subroutine
rename_otus: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 2 | my ( $self, $translate ) = @_; | |||||||||||||||||||||||||||
| 122 | 2 | my $taxlabels = $self->get_taxlabels(); | |||||||||||||||||||||||||||
| 123 | 2 | my $newtaxlabels; | |||||||||||||||||||||||||||
| 124 | 2 | for my $taxlabel (@$taxlabels) { | |||||||||||||||||||||||||||
| 125 | 8 | ▶ | $taxlabel = $$translate{$taxlabel} if $$translate{$taxlabel}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 8 | push( @$newtaxlabels, $taxlabel ); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 2 | $self->set_taxlabels($newtaxlabels); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =head2 add_otu_clone | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | Title : add_otu_clone | ||||||||||||||||||||||||||||
| 134 | Usage : ... | ||||||||||||||||||||||||||||
| 135 | Function: ... | ||||||||||||||||||||||||||||
| 136 | Returns : ... | ||||||||||||||||||||||||||||
| 137 | Args : ... | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =cut | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 7 | ▶ | sub add_otu_clone { | ||||||||||||||||||||||||||
|
Subroutine
add_otu_clone: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 142 | # todo: | ||||||||||||||||||||||||||||
| 143 | # rename the method | ||||||||||||||||||||||||||||
| 144 | 7 | my ( $self, $original_otu_name, $copy_otu_name ) = @_; | |||||||||||||||||||||||||||
| 145 | # print "Warning: Bio::NEXUS::TaxaBlock::add_otu_clone() method not fully implemented\n"; | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 7 | ▶ | if (defined $self->{'dimensions'}{'ntax'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 7 | $self->{'dimensions'}{'ntax'}++; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | else { | ||||||||||||||||||||||||||||
| 151 | # the execution should never reach this point, | ||||||||||||||||||||||||||||
| 152 | # b/c if an OTU is being cloned, ntax should | ||||||||||||||||||||||||||||
| 153 | # be > or = '1' | ||||||||||||||||||||||||||||
| 154 | 0 | throw 'BadArgs' => "add_otu_clone(): at least 1 otu exists, but 'ntax' is not initialized"; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | 7 | $self->add_taxlabel($copy_otu_name); | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =head2 equals | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | Name : equals | ||||||||||||||||||||||||||||
| 162 | Usage : $taxa->equals($another); | ||||||||||||||||||||||||||||
| 163 | Function: compare if two Bio::NEXUS::TaxaBlock objects are equal | ||||||||||||||||||||||||||||
| 164 | Returns : boolean | ||||||||||||||||||||||||||||
| 165 | Args : a Bio::NEXUS::TaxaBlock object | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =cut | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 2 | ▶ | sub equals { | ||||||||||||||||||||||||||
|
Subroutine
equals: called CC=5 SCAR=16.9
Pod
documented
| |||||||||||||||||||||||||||||
| 170 | 2 | my ( $self, $block ) = @_; | |||||||||||||||||||||||||||
| 171 | 2 | ▶ | if ( ! $self->SUPER::equals( $block ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 0 | return 0; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 2 | my @labels1 = @{ $self->get_taxlabels() }; | |||||||||||||||||||||||||||
| 176 | 2 | my @labels2 = @{ $block->get_taxlabels() }; | |||||||||||||||||||||||||||
| 177 | 0 | ▶ | if ( @labels1 != @labels2 ) { return 0; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 2 | @labels1 = sort { $a cmp $b } @labels1; | |||||||||||||||||||||||||||
| 179 | 2 | @labels2 = sort { $a cmp $b } @labels2; | |||||||||||||||||||||||||||
| 180 | 2 | for my $i ( 0 .. $#labels1 ) { | |||||||||||||||||||||||||||
| 181 | 16 | ▶ | if ( $labels1[$i] ne $labels2[$i] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 0 | return 0; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | 2 | return 1; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =begin comment | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | Name : _write | ||||||||||||||||||||||||||||
| 191 | Usage : $taxa->_write($filehandle, $verbose); | ||||||||||||||||||||||||||||
| 192 | Function: Writes NEXUS block from stored data | ||||||||||||||||||||||||||||
| 193 | Returns : none | ||||||||||||||||||||||||||||
| 194 | Args : none | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =end comment | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =cut | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 2 | ▶ | sub _write { | ||||||||||||||||||||||||||
|
Subroutine
_write: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 201 | 2 | my ( $self, $fh, $verbose ) = @_; | |||||||||||||||||||||||||||
| 202 | 2 | ▶ | $fh ||= \*STDOUT; | ||||||||||||||||||||||||||
|
Condition50%1/2
$fh ||= \*STDOUT
MC/DC0%0/1
$fh ||= \*STDOUT
$fh
Truth table50%1/2
A
A= $fh
| |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 2 | my $ntax = $self->get_ntax(); | |||||||||||||||||||||||||||
| 205 | 2 | $self->SUPER::_write($fh); | |||||||||||||||||||||||||||
| 206 | 2 | print $fh "\tDIMENSIONS ntax=$ntax;\n"; | |||||||||||||||||||||||||||
| 207 | 2 | print $fh "\tTAXLABELS "; | |||||||||||||||||||||||||||
| 208 | 2 | for my $OTU ( @{ $self->get_taxlabels() } ) { | |||||||||||||||||||||||||||
| 209 | 12 | $OTU = _nexus_formatted($OTU); | |||||||||||||||||||||||||||
| 210 | 12 | print $fh " $OTU"; | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | 2 | print $fh ";\nEND;\n"; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 90 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||
| 216 | 90 | ▶ | return if $AUTOLOAD =~ /DESTROY$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | 1 | my $package_name = __PACKAGE__ . '::'; | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | # The following methods are deprecated and are temporarily supported | ||||||||||||||||||||||||||||
| 220 | # via a warning and a redirection | ||||||||||||||||||||||||||||
| 221 | 1 | my %synonym_for = | |||||||||||||||||||||||||||
| 222 | ( "${package_name}parse_labels" => "${package_name}_parse_taxlabels", ); | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 1 | ▶ | if ( defined $synonym_for{$AUTOLOAD} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | 0 | $logger->warn("$AUTOLOAD() is deprecated; use $synonym_for{$AUTOLOAD}() instead"); | |||||||||||||||||||||||||||
| 226 | 0 | goto &{ $synonym_for{$AUTOLOAD} }; | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | else { | ||||||||||||||||||||||||||||
| 229 | 1 | Bio::NEXUS::Util::Exceptions::UnknownMethod->throw( | |||||||||||||||||||||||||||
| 230 | 'UnknownMethod' => "ERROR: Unknown method $AUTOLOAD called" | ||||||||||||||||||||||||||||
| 231 | ); | ||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 35 | 1; | |||||||||||||||||||||||||||