| 1 | package Bio::Phylo::Models::Substitution::Binary; | ||||||||||||||||||||
| 2 | 13 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 13 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 13 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 13 | ▶ | use Bio::Phylo::Util::Logger; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 13 | ▶ | use Bio::Phylo::Util::CONSTANT qw'/looks_like/ :objecttypes'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 13 | ▶ | use Bio::Phylo::Util::Exceptions qw'throw'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 13 | my $logger = Bio::Phylo::Util::Logger->new; | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | Bio::Phylo::Models::Substitution::Binary - Binary character substitution model | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | use Bio::Phylo::Models::Substitution::Binary; | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | # create a binary substitution model | ||||||||||||||||||||
| 20 | # by doing a modeltest | ||||||||||||||||||||
| 21 | my $model = Bio::Phylo::Models::Substitution::Binary->modeltest( | ||||||||||||||||||||
| 22 | -tree => $tree, # phylogeny | ||||||||||||||||||||
| 23 | -matrix => $matrix, # character state matrix, standard categorical data | ||||||||||||||||||||
| 24 | -model => 'ARD', # ace model | ||||||||||||||||||||
| 25 | -char => 'c1', # column ID in $matrix | ||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | # after model test, forward and reverse instantaneous transition | ||||||||||||||||||||
| 29 | # rates are available, e.g. for simulation | ||||||||||||||||||||
| 30 | print $model->forward, "\n"; | ||||||||||||||||||||
| 31 | print $model->reverse, "\n"; | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | =head1 DESCRIPTION | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | This is a class that encapsulates an instantaneous transition model | ||||||||||||||||||||
| 36 | for a binary character. The model is asymmetrical in that the forward | ||||||||||||||||||||
| 37 | and reverse rates (can) differ. The rates can be inferred from a | ||||||||||||||||||||
| 38 | character in a character state matrix by modeltesting. This is done | ||||||||||||||||||||
| 39 | by delegation to the R package C<ape>. For this to work, you therefore | ||||||||||||||||||||
| 40 | need to have R and C<ape> installed, as well as the bridge that allows | ||||||||||||||||||||
| 41 | Perl to communicate with R, which is done by the optional package | ||||||||||||||||||||
| 42 | L<Statistics::R>. | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | =head1 METHODS | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | =head2 CONSTRUCTOR | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | =over | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | =item new | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | Binary character model constructor. | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | Type : Constructor | ||||||||||||||||||||
| 55 | Title : new | ||||||||||||||||||||
| 56 | Usage : my $model = Bio::Phylo::Models::Substitution::Binary->new(%args); | ||||||||||||||||||||
| 57 | Function: Instantiates a Bio::Phylo::Models::Substitution::Binary object. | ||||||||||||||||||||
| 58 | Returns : A Bio::Phylo::Models::Substitution::Binary object. | ||||||||||||||||||||
| 59 | Args : Optional: | ||||||||||||||||||||
| 60 | -forward => sets forward rate | ||||||||||||||||||||
| 61 | -reverse => sets reverse rate | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 66 | 0 | my $class = shift; | |||||||||||||||||||
| 67 | 0 | my %args = looks_like_hash @_; | |||||||||||||||||||
| 68 | 0 | my $self = { '_fw' => undef, '_rev' => undef }; | |||||||||||||||||||
| 69 | 0 | bless $self, $class; | |||||||||||||||||||
| 70 | 0 | ▶ | while ( my ( $key, $value ) = each %args ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | 0 | $key =~ s/^-/set_/; | |||||||||||||||||||
| 72 | 0 | $self->$key($value); | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | 0 | return $self; | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | =item set_forward | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | Setter for forward transition rate | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | Type : method | ||||||||||||||||||||
| 82 | Title : set_forward | ||||||||||||||||||||
| 83 | Usage : $model->set_forward($rate); | ||||||||||||||||||||
| 84 | Function: Setter for forward transition rate | ||||||||||||||||||||
| 85 | Returns : $self | ||||||||||||||||||||
| 86 | Args : A rate (floating point number) | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | =cut | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 0 | ▶ | sub set_forward { | ||||||||||||||||||
|
Subroutine
set_forward: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 91 | 0 | my ( $self, $fw ) = @_; | |||||||||||||||||||
| 92 | 0 | $self->{'_fw'} = $fw; | |||||||||||||||||||
| 93 | 0 | return $self; | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | =item set_reverse | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | Setter for reverse transition rate | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | Type : method | ||||||||||||||||||||
| 101 | Title : set_reverse | ||||||||||||||||||||
| 102 | Usage : $model->set_reverse($rate); | ||||||||||||||||||||
| 103 | Function: Setter for reverse transition rate | ||||||||||||||||||||
| 104 | Returns : $self | ||||||||||||||||||||
| 105 | Args : A rate (floating point number) | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | =cut | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 0 | ▶ | sub set_reverse { | ||||||||||||||||||
|
Subroutine
set_reverse: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 110 | 0 | my ( $self, $rev ) = @_; | |||||||||||||||||||
| 111 | 0 | $self->{'_rev'} = $rev; | |||||||||||||||||||
| 112 | 0 | return $self; | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | =item get_forward | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | Setter for forward transition rate | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | Type : method | ||||||||||||||||||||
| 120 | Title : get_forward | ||||||||||||||||||||
| 121 | Usage : my $rate = $model->get_forward; | ||||||||||||||||||||
| 122 | Function: Getter for forward transition rate | ||||||||||||||||||||
| 123 | Returns : A rate (floating point number) | ||||||||||||||||||||
| 124 | Args : NONE | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | =cut | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 0 | ▶ | sub get_forward { shift->{'_fw'} } | ||||||||||||||||||
|
Subroutine
get_forward: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | =item get_reverse | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | Setter for reverse transition rate | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | Type : method | ||||||||||||||||||||
| 135 | Title : get_reverse | ||||||||||||||||||||
| 136 | Usage : my $rate = $model->get_reverse; | ||||||||||||||||||||
| 137 | Function: Getter for reverse transition rate | ||||||||||||||||||||
| 138 | Returns : A rate (floating point number) | ||||||||||||||||||||
| 139 | Args : NONE | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | =cut | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 0 | ▶ | sub get_reverse { shift->{'_rev'} } | ||||||||||||||||||
|
Subroutine
get_reverse: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =item modeltest | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | Performs a model test to infer transition rates | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | Type : method | ||||||||||||||||||||
| 150 | Title : modeltest | ||||||||||||||||||||
| 151 | Usage : my $model = $package->modeltest; | ||||||||||||||||||||
| 152 | Function: Performs a model test to infer transition rates | ||||||||||||||||||||
| 153 | Returns : A populated $model object | ||||||||||||||||||||
| 154 | Args : All required: | ||||||||||||||||||||
| 155 | -tree => $tree, # phylogeny | ||||||||||||||||||||
| 156 | -matrix => $matrix, # character state matrix, standard categorical data | ||||||||||||||||||||
| 157 | -model => 'ARD', # ace model | ||||||||||||||||||||
| 158 | -char => 'c1', # column ID in $matrix | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | =cut | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 0 | ▶ | sub modeltest { | ||||||||||||||||||
|
Subroutine
modeltest: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | # process arguments | ||||||||||||||||||||
| 165 | 0 | my ( $class, %args ) = @_; | |||||||||||||||||||
| 166 | 0 | ▶ | my $tree = $args{'-tree'} or throw 'BadArgs' => "Need -tree argument"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 167 | 0 | ▶ | my $char = $args{'-char'} or throw 'BadArgs' => "Need -char argument"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 168 | 0 | ▶ | my $matrix = $args{'-matrix'} or throw 'BadArgs' => "Need -matrix argument"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 169 | 0 | ▶ | my $model = $args{'-model'} || 'ARD'; | ||||||||||||||||||
|
Condition0%0/2
$args{'-model'} || 'ARD'
MC/DC0%0/1
$args{'-model'} || 'ARD'
$args{'-model'}
Truth table0%0/2
A
A= $args{'-model'}
| |||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | # we don't actually check if the character is binary here. perhaps we should, | ||||||||||||||||||||
| 172 | # and verify that the tips in the tree match the rows in the matrix, and | ||||||||||||||||||||
| 173 | # prune tips with missing data, and, and, and... | ||||||||||||||||||||
| 174 | 0 | ▶ | if ( $matrix->get_type !~ /standard/i ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 175 | 0 | throw 'BadArgs' => "Need standard categorical data"; | |||||||||||||||||||
| 176 | } | ||||||||||||||||||||
| 177 | 0 | ▶ | if ( looks_like_class 'Statistics::R' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | # start R, load library | ||||||||||||||||||||
| 180 | 0 | $logger->info("going to run 'ace'"); | |||||||||||||||||||
| 181 | 0 | my $R = Statistics::R->new; | |||||||||||||||||||
| 182 | 0 | $R->run(q[library("ape")]); | |||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | # insert data | ||||||||||||||||||||
| 185 | 0 | my $newick = $tree->to_newick; | |||||||||||||||||||
| 186 | 0 | my %hash = $class->_data_hash($char,$matrix); | |||||||||||||||||||
| 187 | 0 | $R->run(qq[phylo <- read.tree(text="$newick")]); | |||||||||||||||||||
| 188 | 0 | $R->set('chars', [values %hash]); | |||||||||||||||||||
| 189 | 0 | $R->set('labels', [keys %hash]); | |||||||||||||||||||
| 190 | 0 | $R->run(q[names(chars) <- labels]); | |||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | # do calculation | ||||||||||||||||||||
| 193 | 0 | $R->run(qq[ans <- ace(chars,phylo,type="d",model="$model")]); | |||||||||||||||||||
| 194 | 0 | $R->run(q[rates <- ans$rates]); | |||||||||||||||||||
| 195 | 0 | my $rates = $R->get(q[rates]); | |||||||||||||||||||
| 196 | 0 | $logger->info("Rates: ".Dumper($rates)); | |||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | # return instance | ||||||||||||||||||||
| 199 | 0 | return $class->new( | |||||||||||||||||||
| 200 | '-forward' => $rates->[1], | ||||||||||||||||||||
| 201 | '-reverse' => $rates->[0], | ||||||||||||||||||||
| 202 | ); | ||||||||||||||||||||
| 203 | } | ||||||||||||||||||||
| 204 | } | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | 0 | ▶ | sub _data_hash { | ||||||||||||||||||
|
Subroutine
_data_hash: not called CC=5 SCAR=34.0
| |||||||||||||||||||||
| 207 | 0 | my ( $self, $char, $matrix ) = @_; | |||||||||||||||||||
| 208 | 0 | my $cid = $char->get_id; | |||||||||||||||||||
| 209 | 0 | my $chars = $matrix->get_characters; | |||||||||||||||||||
| 210 | 0 | my $nchar = $matrix->get_nchar; | |||||||||||||||||||
| 211 | 0 | ▶ | my $name = $char->get_name || $cid; | ||||||||||||||||||
|
Condition0%0/3
$char->get_name || $cid
MC/DC0%0/2
$char->get_name || $cid
$char->get_name
$cid
Truth table0%0/3
A || B
A= $char->get_nameB= $cid
| |||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | # find index of character | ||||||||||||||||||||
| 214 | 0 | my $index; | |||||||||||||||||||
| 215 | 0 | CHAR: for my $i ( 0 .. $nchar - 1 ) { | |||||||||||||||||||
| 216 | 0 | my $c = $chars->get_by_index($i); | |||||||||||||||||||
| 217 | 0 | ▶ | if ( $c->get_id == $cid ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 218 | 0 | $index = $i; | |||||||||||||||||||
| 219 | 0 | $logger->info("index of character ${name}: ${index}"); | |||||||||||||||||||
| 220 | 0 | last CHAR; | |||||||||||||||||||
| 221 | } | ||||||||||||||||||||
| 222 | } | ||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | # get character states | ||||||||||||||||||||
| 225 | 0 | my %result; | |||||||||||||||||||
| 226 | 0 | for my $row ( @{ $matrix->get_entities } ) { | |||||||||||||||||||
| 227 | 0 | my @char = $row->get_char; | |||||||||||||||||||
| 228 | 0 | my $name = $row->get_name; | |||||||||||||||||||
| 229 | 0 | $result{$name} = $char[$index]; | |||||||||||||||||||
| 230 | } | ||||||||||||||||||||
| 231 | 0 | $logger->debug(Dumper(\%result)); | |||||||||||||||||||
| 232 | 0 | return %result; | |||||||||||||||||||
| 233 | } | ||||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | =back | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | =head1 SEE ALSO | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo> | ||||||||||||||||||||
| 240 | for any user or developer questions and discussions. | ||||||||||||||||||||
| 241 | |||||||||||||||||||||
| 242 | =over | ||||||||||||||||||||
| 243 | |||||||||||||||||||||
| 244 | =item L<Bio::Phylo::Manual> | ||||||||||||||||||||
| 245 | |||||||||||||||||||||
| 246 | Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>. | ||||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | =back | ||||||||||||||||||||
| 249 | |||||||||||||||||||||
| 250 | =head1 CITATION | ||||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | If you use Bio::Phylo in published research, please cite it: | ||||||||||||||||||||
| 253 | |||||||||||||||||||||
| 254 | B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen> | ||||||||||||||||||||
| 255 | and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl. | ||||||||||||||||||||
| 256 | I<BMC Bioinformatics> B<12>:63. | ||||||||||||||||||||
| 257 | L<http://dx.doi.org/10.1186/1471-2105-12-63> | ||||||||||||||||||||
| 258 | |||||||||||||||||||||
| 259 | =cut | ||||||||||||||||||||
| 260 | |||||||||||||||||||||
| 261 | 13 | 1; | |||||||||||||||||||