| 1 | ######################################################################## | ||||||||||||||
| 2 | # TaxUnit.pm | ||||||||||||||
| 3 | ######################################################################## | ||||||||||||||
| 4 | # Author: Chengzhi Liang, Thomas Hladish | ||||||||||||||
| 5 | # $Id: TaxUnit.pm,v 1.23 2007/09/24 04:52:14 rvos Exp $ | ||||||||||||||
| 6 | |||||||||||||||
| 7 | #################### START POD DOCUMENTATION ################## | ||||||||||||||
| 8 | |||||||||||||||
| 9 | =head1 NAME | ||||||||||||||
| 10 | |||||||||||||||
| 11 | Bio::NEXUS::TaxUnit - Represents a taxon unit in a NEXUS file | ||||||||||||||
| 12 | |||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||
| 14 | |||||||||||||||
| 15 | $tu = new Bio::NEXUS::TaxUnit($name, $seq); | ||||||||||||||
| 16 | |||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||
| 18 | |||||||||||||||
| 19 | This module represents a taxon unit in a NEXUS file (in characters block or History block) | ||||||||||||||
| 20 | |||||||||||||||
| 21 | =head1 COMMENTS | ||||||||||||||
| 22 | |||||||||||||||
| 23 | =head1 FEEDBACK | ||||||||||||||
| 24 | |||||||||||||||
| 25 | All feedback (bugs, feature enhancements, etc.) are greatly appreciated. | ||||||||||||||
| 26 | |||||||||||||||
| 27 | =head1 AUTHORS | ||||||||||||||
| 28 | |||||||||||||||
| 29 | Chengzhi Liang (liangc@umbi.umd.edu) | ||||||||||||||
| 30 | Thomas Hladish (tjhladish at yahoo) | ||||||||||||||
| 31 | |||||||||||||||
| 32 | =head1 VERSION | ||||||||||||||
| 33 | |||||||||||||||
| 34 | $Revision: 1.23 $ | ||||||||||||||
| 35 | |||||||||||||||
| 36 | =head1 METHODS | ||||||||||||||
| 37 | |||||||||||||||
| 38 | =cut | ||||||||||||||
| 39 | |||||||||||||||
| 40 | package Bio::NEXUS::TaxUnit; | ||||||||||||||
| 41 | |||||||||||||||
| 42 | 35 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 43 | 35 | ▶ | use Bio::NEXUS::Functions; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 44 | #use Carp;# XXX this is not used, might as well not import it! | ||||||||||||||
| 45 | #use Data::Dumper; # XXX this is not used, might as well not import it! | ||||||||||||||
| 46 | 35 | ▶ | use Bio::NEXUS::Util::Exceptions 'throw'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 47 | 35 | ▶ | use Bio::NEXUS::Util::Logger; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 48 | # Note: This script uses Clone::PP to clone the | ||||||||||||||
| 49 | # nested perl data structures | ||||||||||||||
| 50 | #use Clone::PP; # XXX changed this to a lazy loading 'require' where it's needed, in the clone function | ||||||||||||||
| 51 | 35 | ▶ | use vars qw($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 | my $logger = Bio::NEXUS::Util::Logger->new(); | |||||||||||||
| 55 | |||||||||||||||
| 56 | =head2 new | ||||||||||||||
| 57 | |||||||||||||||
| 58 | Title : new | ||||||||||||||
| 59 | Usage : $otu = new Bio::NEXUS::TaxUnit($name, $seq); | ||||||||||||||
| 60 | Function: Creates a new Bio::NEXUS::TaxUnit object | ||||||||||||||
| 61 | Returns : Bio::NEXUS::TaxUnit object | ||||||||||||||
| 62 | Args : name and sequence of TaxUnit object | ||||||||||||||
| 63 | |||||||||||||||
| 64 | =cut | ||||||||||||||
| 65 | |||||||||||||||
| 66 | 528 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 67 | 528 | my ( $class, $name, $seq ) = @_; | |||||||||||||
| 68 | 528 | my $self = { name => $name, seq => $seq, }; | |||||||||||||
| 69 | 528 | bless $self, $class; | |||||||||||||
| 70 | 528 | return $self; | |||||||||||||
| 71 | } | ||||||||||||||
| 72 | |||||||||||||||
| 73 | =head2 clone | ||||||||||||||
| 74 | |||||||||||||||
| 75 | Title : clone | ||||||||||||||
| 76 | Usage : my $newtu = $set->clone(); | ||||||||||||||
| 77 | Function: clone an TaxUnit object | ||||||||||||||
| 78 | Returns : TaxUnit object | ||||||||||||||
| 79 | Args : none | ||||||||||||||
| 80 | |||||||||||||||
| 81 | =cut | ||||||||||||||
| 82 | |||||||||||||||
| 83 | 14 | ▶ | sub clone { | ||||||||||||
|
Subroutine
clone: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||
| 84 | 14 | my ($self) = @_; | |||||||||||||
| 85 | 14 | my $class = ref($self); | |||||||||||||
| 86 | 14 | my $newtu = bless( { %{$self} }, $class ); | |||||||||||||
| 87 | # clone the sequence using Clone::PP | ||||||||||||||
| 88 | 14 | ▶ | if (defined $self->{'seq'}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 89 | 14 | eval { require Clone::PP }; | |||||||||||||
| 90 | 14 | ▶ | if ( $@ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 91 | 0 | throw 'ExtensionError' => "Can't clone, no Clone::PP $@"; | |||||||||||||
| 92 | } | ||||||||||||||
| 93 | 14 | $newtu->{'seq'} = Clone::PP::clone($self->{'seq'}); | |||||||||||||
| 94 | } | ||||||||||||||
| 95 | 14 | return $newtu; | |||||||||||||
| 96 | } | ||||||||||||||
| 97 | |||||||||||||||
| 98 | =head2 set_name | ||||||||||||||
| 99 | |||||||||||||||
| 100 | Title : set_name | ||||||||||||||
| 101 | Usage : $tu->set_name($name); | ||||||||||||||
| 102 | Function: sets the name of OTU | ||||||||||||||
| 103 | Returns : none | ||||||||||||||
| 104 | Args : name | ||||||||||||||
| 105 | |||||||||||||||
| 106 | =cut | ||||||||||||||
| 107 | |||||||||||||||
| 108 | 17 | ▶ | sub set_name { | ||||||||||||
|
Subroutine
set_name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 109 | 17 | my ( $self, $name ) = @_; | |||||||||||||
| 110 | 17 | $self->{'name'} = $name; | |||||||||||||
| 111 | } | ||||||||||||||
| 112 | |||||||||||||||
| 113 | =head2 get_name | ||||||||||||||
| 114 | |||||||||||||||
| 115 | Title : get_name | ||||||||||||||
| 116 | Usage : $tu->get_name(); | ||||||||||||||
| 117 | Function: Returns name | ||||||||||||||
| 118 | Returns : name | ||||||||||||||
| 119 | Args : none | ||||||||||||||
| 120 | |||||||||||||||
| 121 | =cut | ||||||||||||||
| 122 | |||||||||||||||
| 123 | 1060 | ▶ | sub get_name { | ||||||||||||
|
Subroutine
get_name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 124 | 1060 | my ($self) = @_; | |||||||||||||
| 125 | 1060 | return $self->{'name'}; | |||||||||||||
| 126 | } | ||||||||||||||
| 127 | |||||||||||||||
| 128 | =head2 set_seq | ||||||||||||||
| 129 | |||||||||||||||
| 130 | Title : set_seq | ||||||||||||||
| 131 | Usage : $tu->set_seq($seq); | ||||||||||||||
| 132 | Function: sets the sequence of OTU | ||||||||||||||
| 133 | Returns : none | ||||||||||||||
| 134 | Args : sequence | ||||||||||||||
| 135 | |||||||||||||||
| 136 | =cut | ||||||||||||||
| 137 | |||||||||||||||
| 138 | 3 | ▶ | sub set_seq { | ||||||||||||
|
Subroutine
set_seq: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 139 | 3 | my ( $self, $seq ) = @_; | |||||||||||||
| 140 | 3 | $self->{'seq'} = $seq; | |||||||||||||
| 141 | } | ||||||||||||||
| 142 | |||||||||||||||
| 143 | =head2 get_seq | ||||||||||||||
| 144 | |||||||||||||||
| 145 | Title : get_seq | ||||||||||||||
| 146 | Usage : $tu->get_seq(); | ||||||||||||||
| 147 | Function: Returns sequence | ||||||||||||||
| 148 | Returns : sequence (an array of characters or tokens) | ||||||||||||||
| 149 | Args : none | ||||||||||||||
| 150 | |||||||||||||||
| 151 | =cut | ||||||||||||||
| 152 | |||||||||||||||
| 153 | 151 | ▶ | sub get_seq { | ||||||||||||
|
Subroutine
get_seq: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 154 | 151 | my ($self) = @_; | |||||||||||||
| 155 | 151 | return $self->{'seq'}; | |||||||||||||
| 156 | } | ||||||||||||||
| 157 | |||||||||||||||
| 158 | =head2 get_seq_string | ||||||||||||||
| 159 | |||||||||||||||
| 160 | Title : get_seq_string | ||||||||||||||
| 161 | Usage : $taxunit->get_seq_string($tokens_flag); | ||||||||||||||
| 162 | Function: Returns sequence | ||||||||||||||
| 163 | Returns : sequence (a string, wherein tokens or characters are space-delimited | ||||||||||||||
| 164 | if a true value has been passed in for $tokens) | ||||||||||||||
| 165 | Args : boolean tokens argument (optional) | ||||||||||||||
| 166 | |||||||||||||||
| 167 | =cut | ||||||||||||||
| 168 | |||||||||||||||
| 169 | 26 | ▶ | sub get_seq_string { | ||||||||||||
|
Subroutine
get_seq_string: called CC=10 SCAR=39.5
Pod
documented
| |||||||||||||||
| 170 | 26 | my ( $self, $tokens_flag ) = @_; | |||||||||||||
| 171 | 26 | my @seq; | |||||||||||||
| 172 | 26 | for my $token ( @{ $self->get_seq } ) { | |||||||||||||
| 173 | 2228 | ▶ | if ( ref $token eq 'HASH' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 174 | 0 | my $token_type = $token->{'type'}; | |||||||||||||
| 175 | 0 | ▶ | if ( ref $token->{'states'} eq 'ARRAY' ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 176 | 0 | my @states = @{ $token->{'states'} }; | |||||||||||||
| 177 | 0 | ▶ | if ( $token_type eq 'uncertainty' ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 178 | 0 | push @seq, '{', @states, '}'; | |||||||||||||
| 179 | } | ||||||||||||||
| 180 | elsif ( $token_type eq 'polymorphism' ) { | ||||||||||||||
| 181 | 0 | push @seq, '(', @states, ')'; | |||||||||||||
| 182 | } | ||||||||||||||
| 183 | else { | ||||||||||||||
| 184 | 0 | throw 'BadFormat' => "Unknown token type encountered: only 'uncertainty' and 'polymorphism' are valid"; | |||||||||||||
| 185 | } | ||||||||||||||
| 186 | } | ||||||||||||||
| 187 | elsif ( ref $token->{'states'} eq 'HASH' ) { | ||||||||||||||
| 188 | 0 | my %states = %{ $token->{'states'} }; | |||||||||||||
| 189 | my @polymorphism | ||||||||||||||
| 190 | 0 | ; # will contain something like ('A:0.2', 'G:0.4', 'P:0.4') | |||||||||||||
| 191 | 0 | ▶ | if ( $token_type eq 'polymorphism' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 192 | 0 | ▶ | while ( my ( $key, $val ) = each %states ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 193 | 0 | push @polymorphism, "$key:$val"; | |||||||||||||
| 194 | } | ||||||||||||||
| 195 | 0 | push @seq, join q{ }, '(', @polymorphism, ')'; | |||||||||||||
| 196 | } | ||||||||||||||
| 197 | else { | ||||||||||||||
| 198 | 0 | throw 'BadFormat' => "Unknown token type <$token_type> encountered: only 'polymorphism' is valid when explicit frequencies are included"; | |||||||||||||
| 199 | } | ||||||||||||||
| 200 | } | ||||||||||||||
| 201 | } | ||||||||||||||
| 202 | else { | ||||||||||||||
| 203 | 2228 | push @seq, $token; | |||||||||||||
| 204 | } | ||||||||||||||
| 205 | } | ||||||||||||||
| 206 | 26 | ▶ | my $delimiter = $tokens_flag ? q{ } : q{}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 207 | 26 | return join $delimiter, @seq; | |||||||||||||
| 208 | } | ||||||||||||||
| 209 | |||||||||||||||
| 210 | 542 | ▶ | sub AUTOLOAD { | ||||||||||||
|
Subroutine
AUTOLOAD: called CC=3 SCAR=21.1
| |||||||||||||||
| 211 | 542 | ▶ | return if $AUTOLOAD =~ /DESTROY$/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 212 | 0 | my $package_name = __PACKAGE__ . '::'; | |||||||||||||
| 213 | |||||||||||||||
| 214 | # The following methods are deprecated and are temporarily supported | ||||||||||||||
| 215 | # via a warning and a redirection | ||||||||||||||
| 216 | 0 | my %synonym_for = ( | |||||||||||||
| 217 | |||||||||||||||
| 218 | # "${package_name}parse" => "${package_name}_parse_tree", # example | ||||||||||||||
| 219 | ); | ||||||||||||||
| 220 | |||||||||||||||
| 221 | 0 | ▶ | if ( defined $synonym_for{$AUTOLOAD} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 222 | 0 | $logger->warn("$AUTOLOAD() is deprecated; use $synonym_for{$AUTOLOAD}() instead"); | |||||||||||||
| 223 | 0 | goto &{ $synonym_for{$AUTOLOAD} }; | |||||||||||||
| 224 | } | ||||||||||||||
| 225 | else { | ||||||||||||||
| 226 | 0 | throw 'UnknownMethod' => "Unknown method $AUTOLOAD called"; | |||||||||||||
| 227 | } | ||||||||||||||
| 228 | } | ||||||||||||||
| 229 | |||||||||||||||
| 230 | 35 | 1; | |||||||||||||