File Coverage

blib/lib/Bio/NEXUS/TaxUnit.pm
Criterion Covered Total %
statement 48 73 65.7
branch 4 22 18.1
condition n/a
subroutine 13 14 92.8
pod 7 7 100.0
total 72 116 62.0


line stmt bran cond sub pod time code
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 34     34   228 use strict;
  34         75  
  34         1144  
43 34     34   202 use Bio::NEXUS::Functions;
  34         72  
  34         6918  
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 34     34   200 use Bio::NEXUS::Util::Exceptions 'throw';
  34         67  
  34         1793  
47 34     34   201 use Bio::NEXUS::Util::Logger;
  34         1553  
  34         5661  
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 34     34   1713 use vars qw($VERSION $AUTOLOAD);
  34         83  
  34         4246  
52 34     34   3653 use Bio::NEXUS; $VERSION = $Bio::NEXUS::VERSION;
  34         1588  
  34         37539  
53              
54             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             sub new {
67 527     527 1 866 my ( $class, $name, $seq ) = @_;
68 527         3081 my $self = { name => $name, seq => $seq, };
69 527         1235 bless $self, $class;
70 527         2545 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             sub clone {
84 14     14 1 32 my ($self) = @_;
85 14         31 my $class = ref($self);
86 14         21 my $newtu = bless( { %{$self} }, $class );
  14         97  
87             # clone the sequence using Clone::PP
88 14 50       51 if (defined $self->{'seq'}) {
89 14         23 eval { require Clone::PP };
  14         131  
90 14 50       39 if ( $@ ) {
91 0         0 throw 'ExtensionError' => "Can't clone, no Clone::PP $@";
92             }
93 14         69 $newtu->{'seq'} = Clone::PP::clone($self->{'seq'});
94             }
95 14         2540 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             sub set_name {
109 17     17 1 907 my ( $self, $name ) = @_;
110 17         68 $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             sub get_name {
124 1078     1078 1 11629 my ($self) = @_;
125 1078         3563 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             sub set_seq {
139 3     3 1 44 my ( $self, $seq ) = @_;
140 3         23 $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             sub get_seq {
154 151     151 1 515 my ($self) = @_;
155 151         921 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             sub get_seq_string {
170 26     26 1 55 my ( $self, $tokens_flag ) = @_;
171 26         33 my @seq;
172 26         27 for my $token ( @{ $self->get_seq } ) {
  26         62  
173 2228 50       2685 if ( ref $token eq 'HASH' ) {
174 0         0 my $token_type = $token->{'type'};
175 0 0       0 if ( ref $token->{'states'} eq 'ARRAY' ) {
    0          
176 0         0 my @states = @{ $token->{'states'} };
  0         0  
177 0 0       0 if ( $token_type eq 'uncertainty' ) {
    0          
178 0         0 push @seq, '{', @states, '}';
179             }
180             elsif ( $token_type eq 'polymorphism' ) {
181 0         0 push @seq, '(', @states, ')';
182             }
183             else {
184 0         0 throw 'BadFormat' => "Unknown token type encountered: only 'uncertainty' and 'polymorphism' are valid";
185             }
186             }
187             elsif ( ref $token->{'states'} eq 'HASH' ) {
188 0         0 my %states = %{ $token->{'states'} };
  0         0  
189             my @polymorphism
190 0         0 ; # will contain something like ('A:0.2', 'G:0.4', 'P:0.4')
191 0 0       0 if ( $token_type eq 'polymorphism' ) {
192 0         0 while ( my ( $key, $val ) = each %states ) {
193 0         0 push @polymorphism, "$key:$val";
194             }
195 0         0 push @seq, join q{ }, '(', @polymorphism, ')';
196             }
197             else {
198 0         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         2735 push @seq, $token;
204             }
205             }
206 26 50       66 my $delimiter = $tokens_flag ? q{ } : q{};
207 26         461 return join $delimiter, @seq;
208             }
209              
210             sub AUTOLOAD {
211 0 0   0     return if $AUTOLOAD =~ /DESTROY$/;
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 0         if ( defined $synonym_for{$AUTOLOAD} ) {
222 0           $logger->warn("$AUTOLOAD() is deprecated; use $synonym_for{$AUTOLOAD}() instead");
223 0           goto &{ $synonym_for{$AUTOLOAD} };
  0            
224             }
225             else {
226 0           throw 'UnknownMethod' => "Unknown method $AUTOLOAD called";
227             }
228             }
229              
230             1;