| 1 | ###################################################### | ||||||||||
| 2 | # WeightSet.pm | ||||||||||
| 3 | ###################################################### | ||||||||||
| 4 | # Author: Chengzhi Liang, Weigang Qiu, Peter Yang, Thomas Hladish | ||||||||||
| 5 | # $Id: WeightSet.pm,v 1.26 2007/09/24 04:52:11 rvos Exp $ | ||||||||||
| 6 | |||||||||||
| 7 | #################### START POD DOCUMENTATION ################## | ||||||||||
| 8 | |||||||||||
| 9 | =head1 NAME | ||||||||||
| 10 | |||||||||||
| 11 | Bio::NEXUS::WeightSet - Represents column weights in alignment ( for each character) | ||||||||||
| 12 | |||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||
| 14 | |||||||||||
| 15 | new Bio::NEXUS::WeightSet($name, \@weights, $iswt); | ||||||||||
| 16 | |||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||
| 18 | |||||||||||
| 19 | A module representing column weights in alignment (for each character) | ||||||||||
| 20 | |||||||||||
| 21 | =head1 FEEDBACK | ||||||||||
| 22 | |||||||||||
| 23 | All feedback (bugs, feature enhancements, etc.) are all greatly appreciated. | ||||||||||
| 24 | |||||||||||
| 25 | =head1 AUTHOR | ||||||||||
| 26 | |||||||||||
| 27 | Chengzhi Liang (liangc@umbi.umd.edu) | ||||||||||
| 28 | Weigang Qiu (weigang@genectr.hunter.cuny.edu) | ||||||||||
| 29 | Thomas Hladish (tjhladish at yahoo) | ||||||||||
| 30 | |||||||||||
| 31 | =head1 CONTRIBUTORS | ||||||||||
| 32 | |||||||||||
| 33 | Peter Yang (pyang@rice.edu) | ||||||||||
| 34 | |||||||||||
| 35 | =head1 METHODS | ||||||||||
| 36 | |||||||||||
| 37 | =cut | ||||||||||
| 38 | |||||||||||
| 39 | package Bio::NEXUS::WeightSet; | ||||||||||
| 40 | |||||||||||
| 41 | 35 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 35 | ▶ | use Bio::NEXUS::Functions; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | #use Data::Dumper; # XXX this is not used, might as well not import it! | ||||||||||
| 44 | #use Carp; # XXX this is not used, might as well not import it! | ||||||||||
| 45 | 35 | ▶ | use Bio::NEXUS::Util::Exceptions; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 46 | 35 | ▶ | use Bio::NEXUS::Util::Logger; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 47 | 35 | ▶ | use vars qw($VERSION $AUTOLOAD); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 35 | ▶ | use Bio::NEXUS; $VERSION = $Bio::NEXUS::VERSION; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 49 | |||||||||||
| 50 | 35 | my $logger = Bio::NEXUS::Util::Logger->new(); | |||||||||
| 51 | |||||||||||
| 52 | =head2 new | ||||||||||
| 53 | |||||||||||
| 54 | Title : new | ||||||||||
| 55 | Usage : $node = new Bio::NEXUS::WeightSet($name, \@weights); | ||||||||||
| 56 | Function: Creates a new Bio::NEXUS::WeightSet object | ||||||||||
| 57 | Returns : Bio::NEXUS::WeightSet object | ||||||||||
| 58 | Args : none | ||||||||||
| 59 | |||||||||||
| 60 | =cut | ||||||||||
| 61 | |||||||||||
| 62 | 8 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 63 | 8 | my ( $class, $name, $weights, $iswt, $tokens, $type ) = @_; | |||||||||
| 64 | 8 | my $self = { | |||||||||
| 65 | 'name' => $name, | ||||||||||
| 66 | 'weights' => $weights, | ||||||||||
| 67 | 'is_wt' => $iswt, | ||||||||||
| 68 | '_is_tokens' => $tokens, | ||||||||||
| 69 | 'type' => $type | ||||||||||
| 70 | }; | ||||||||||
| 71 | 8 | bless $self, $class; | |||||||||
| 72 | 8 | return $self; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | =begin comment | ||||||||||
| 76 | |||||||||||
| 77 | Title : _parse_weights | ||||||||||
| 78 | Usage : $self->_parse_weights(weight_string); | ||||||||||
| 79 | Function: parses the weight string and store the contents to the object ($self) | ||||||||||
| 80 | Returns : none | ||||||||||
| 81 | Args : weight-string from the WeightSet block in the NEXUS file | ||||||||||
| 82 | |||||||||||
| 83 | =end comment | ||||||||||
| 84 | |||||||||||
| 85 | =cut | ||||||||||
| 86 | |||||||||||
| 87 | 0 | ▶ | sub _parse_weights { | ||||||||
|
Subroutine
_parse_weights: not called CC=2 SCAR=17.9
| |||||||||||
| 88 | 0 | my ( $self, $wt_string ) = @_; | |||||||||
| 89 | 0 | $wt_string =~ s/^\s+//; | |||||||||
| 90 | |||||||||||
| 91 | 0 | my $delimiter = ''; | |||||||||
| 92 | 0 | ▶ | if ( $self->_is_tokens() ) { $delimiter = '\s+' } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | |||||||||||
| 94 | 0 | my @weights = split /$delimiter/, $wt_string; | |||||||||
| 95 | 0 | $self->{'weights'} = [@weights]; | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | =head2 set_weights | ||||||||||
| 99 | |||||||||||
| 100 | Title : set_weights | ||||||||||
| 101 | Usage : $weight->set_weights(\@weights); | ||||||||||
| 102 | Function: stores it in the list weights | ||||||||||
| 103 | Returns : none | ||||||||||
| 104 | Args : list of weights | ||||||||||
| 105 | |||||||||||
| 106 | =cut | ||||||||||
| 107 | |||||||||||
| 108 | 0 | ▶ | sub set_weights { | ||||||||
|
Subroutine
set_weights: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 109 | 0 | my ( $self, $weights ) = @_; | |||||||||
| 110 | 0 | $self->{'weights'} = $weights; | |||||||||
| 111 | } | ||||||||||
| 112 | |||||||||||
| 113 | =head2 get_weights | ||||||||||
| 114 | |||||||||||
| 115 | Title : get_weights | ||||||||||
| 116 | Usage : @wts=@{$weightset->get_weights()}; | ||||||||||
| 117 | Function: Returns the weights array | ||||||||||
| 118 | Returns : reference to array containing weights | ||||||||||
| 119 | Args : none | ||||||||||
| 120 | |||||||||||
| 121 | =cut | ||||||||||
| 122 | |||||||||||
| 123 | 3 | ▶ | sub get_weights { shift->{'weights'} } | ||||||||
|
Subroutine
get_weights: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 124 | |||||||||||
| 125 | =head2 select_weights | ||||||||||
| 126 | |||||||||||
| 127 | Title : select_weights | ||||||||||
| 128 | Usage : $set->select_weights($columns); | ||||||||||
| 129 | Function: select a subset of characters | ||||||||||
| 130 | Returns : new self with subset of weights | ||||||||||
| 131 | Args : column numbers | ||||||||||
| 132 | |||||||||||
| 133 | =cut | ||||||||||
| 134 | |||||||||||
| 135 | 0 | ▶ | sub select_weights { | ||||||||
|
Subroutine
select_weights: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 136 | 0 | my ( $self, $columns ) = @_; | |||||||||
| 137 | 0 | my @weights = @{ $self->{'weights'} }; | |||||||||
| 138 | 0 | my @newweights = (); | |||||||||
| 139 | 0 | for my $i ( @{$columns} ) { | |||||||||
| 140 | 0 | push @newweights, $weights[$i]; | |||||||||
| 141 | } | ||||||||||
| 142 | 0 | $self->{'weights'} = \@newweights; | |||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | =head2 is_wt | ||||||||||
| 146 | |||||||||||
| 147 | Title : is_wt | ||||||||||
| 148 | Usage : croak unless $weight->is_wt(); | ||||||||||
| 149 | Function: Returns if object has weights (1 yes, 0 no) | ||||||||||
| 150 | Returns : weight existence (integer) | ||||||||||
| 151 | Args : none | ||||||||||
| 152 | |||||||||||
| 153 | =cut | ||||||||||
| 154 | |||||||||||
| 155 | 2 | ▶ | sub is_wt { !!shift->{'is_wt'} } | ||||||||
|
Subroutine
is_wt: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 156 | |||||||||||
| 157 | =begin comment | ||||||||||
| 158 | |||||||||||
| 159 | Title : _is_tokens | ||||||||||
| 160 | Usage : if ( $weight->_is_tokens() ) {} | ||||||||||
| 161 | Function: tests whether tokens attribute is set to true | ||||||||||
| 162 | Returns : boolean | ||||||||||
| 163 | Args : none | ||||||||||
| 164 | |||||||||||
| 165 | =end comment | ||||||||||
| 166 | |||||||||||
| 167 | =cut | ||||||||||
| 168 | |||||||||||
| 169 | 2 | ▶ | sub _is_tokens { !!shift->{'_is_tokens'} } | ||||||||
|
Subroutine
_is_tokens: called CC=1 SCAR=0.0
| |||||||||||
| 170 | |||||||||||
| 171 | =begin comment | ||||||||||
| 172 | |||||||||||
| 173 | Title : _is_vector | ||||||||||
| 174 | Usage : if ( $weight->_is_vector() ) {} | ||||||||||
| 175 | Function: tests whether type attribute is set to vector | ||||||||||
| 176 | Returns : boolean | ||||||||||
| 177 | Args : none | ||||||||||
| 178 | |||||||||||
| 179 | =end comment | ||||||||||
| 180 | |||||||||||
| 181 | =cut | ||||||||||
| 182 | |||||||||||
| 183 | 2 | ▶ | sub _is_vector { uc( shift->{'type'} ) eq 'VECTOR' } | ||||||||
|
Subroutine
_is_vector: called CC=1 SCAR=0.0
| |||||||||||
| 184 | |||||||||||
| 185 | =head2 set_name | ||||||||||
| 186 | |||||||||||
| 187 | Title : set_name | ||||||||||
| 188 | Usage : $weight->set_name($name); | ||||||||||
| 189 | Function: Sets the name of the weightset | ||||||||||
| 190 | Returns : none | ||||||||||
| 191 | Args : name (string) | ||||||||||
| 192 | |||||||||||
| 193 | =cut | ||||||||||
| 194 | |||||||||||
| 195 | 0 | ▶ | sub set_name { | ||||||||
|
Subroutine
set_name: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 196 | 0 | my ( $self, $name ) = @_; | |||||||||
| 197 | 0 | $self->{'name'} = $name; | |||||||||
| 198 | } | ||||||||||
| 199 | |||||||||||
| 200 | =head2 get_name | ||||||||||
| 201 | |||||||||||
| 202 | Title : get_name | ||||||||||
| 203 | Usage : $name=$weight->get_name(); | ||||||||||
| 204 | Function: Returns the name of the weightset | ||||||||||
| 205 | Returns : name (string) | ||||||||||
| 206 | Args : none | ||||||||||
| 207 | |||||||||||
| 208 | =cut | ||||||||||
| 209 | |||||||||||
| 210 | 4 | ▶ | sub get_name { shift->{'name'} } | ||||||||
|
Subroutine
get_name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 211 | |||||||||||
| 212 | =head2 equals | ||||||||||
| 213 | |||||||||||
| 214 | Name : equals | ||||||||||
| 215 | Usage : $set->equals($another); | ||||||||||
| 216 | Function: compare if two WeightSet objects are equal | ||||||||||
| 217 | Returns : boolean | ||||||||||
| 218 | Args : an WeightSet object | ||||||||||
| 219 | |||||||||||
| 220 | =cut | ||||||||||
| 221 | |||||||||||
| 222 | 0 | ▶ | sub equals { | ||||||||
|
Subroutine
equals: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||
| 223 | 0 | my ( $self, $weights ) = @_; | |||||||||
| 224 | 0 | ▶ | if ( $self->get_name() ne $weights->get_name() ) { return 0; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 225 | 0 | my @weights1 = @{ $self->get_weights() }; | |||||||||
| 226 | 0 | my @weights2 = @{ $weights->get_weights() }; | |||||||||
| 227 | 0 | ▶ | if ( @weights1 != @weights2 ) { return 0; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 228 | 0 | ▶ | for ( my $i = 0; $i < @weights1; $i++ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 229 | 0 | ▶ | if ( $weights1[$i] eq $weights2[$i] ) { return 0; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 230 | } | ||||||||||
| 231 | 0 | return 1; | |||||||||
| 232 | } | ||||||||||
| 233 | |||||||||||
| 234 | 8 | ▶ | sub AUTOLOAD { | ||||||||
|
Subroutine
AUTOLOAD: called CC=3 SCAR=21.1
| |||||||||||
| 235 | 8 | ▶ | return if $AUTOLOAD =~ /DESTROY$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 236 | 0 | my $package_name = __PACKAGE__ . '::'; | |||||||||
| 237 | |||||||||||
| 238 | # The following methods are deprecated and are temporarily supported | ||||||||||
| 239 | # via a warning and a redirection | ||||||||||
| 240 | 0 | my %synonym_for = ( | |||||||||
| 241 | "${package_name}is_tokens" => "${package_name}_is_tokens", | ||||||||||
| 242 | "${package_name}is_vector" => "${package_name}_is_vector", | ||||||||||
| 243 | ); | ||||||||||
| 244 | |||||||||||
| 245 | 0 | ▶ | if ( defined $synonym_for{$AUTOLOAD} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 246 | 0 | $logger->warn( "$AUTOLOAD() is deprecated; use $synonym_for{$AUTOLOAD}() instead" ); | |||||||||
| 247 | 0 | goto &{ $synonym_for{$AUTOLOAD} }; | |||||||||
| 248 | } | ||||||||||
| 249 | else { | ||||||||||
| 250 | 0 | Bio::NEXUS::Util::Exceptions::UnknownMethod->throw( | |||||||||
| 251 | 'error' => "ERROR: Unknown method $AUTOLOAD called" | ||||||||||
| 252 | ); | ||||||||||
| 253 | } | ||||||||||
| 254 | } | ||||||||||
| 255 | |||||||||||
| 256 | 35 | 1; | |||||||||