| 1 | # | ||||||||||||||||||||||||||||
| 2 | # Module for Bio::PhyloNetwork::Factory | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # Cared for by Gabriel Cardona <gabriel(dot)cardona(at)uib(dot)es> | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # Copyright Gabriel Cardona | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | # You may distribute this module under the same terms as perl itself | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # POD documentation - main docs before the code | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | Bio::PhyloNetwork::Factory - Module to sequentially generate | ||||||||||||||||||||||||||||
| 17 | Phylogenetic Networks | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | use strict; | ||||||||||||||||||||||||||||
| 22 | use warnings; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | use Bio::PhyloNetwork; | ||||||||||||||||||||||||||||
| 25 | use Bio::PhyloNetwork::Factory; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # Will generate sequentially all the 4059 binary tree-child phylogenetic | ||||||||||||||||||||||||||||
| 28 | # networks with 4 leaves | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | my $factory=Bio::PhyloNetwork::Factory->new(-numleaves=>4); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | my @nets; | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | while (my $net=$factory->next_network()) { | ||||||||||||||||||||||||||||
| 35 | push @nets,$net; | ||||||||||||||||||||||||||||
| 36 | print "".(scalar @nets).": ".$net->eNewick()."\n"; | ||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | Sequentially builds a (binary tree-child) phylogenetic network each time | ||||||||||||||||||||||||||||
| 42 | next_network is called. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | Gabriel Cardona, gabriel(dot)cardona(at)uib(dot)es | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | L<Bio::PhyloNetwork> | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =head1 APPENDIX | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | The rest of the documentation details each of the object methods. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =cut | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | package Bio::PhyloNetwork::Factory; | ||||||||||||||||||||||||||||
| 59 | 2 | $Bio::PhyloNetwork::Factory::VERSION = '1.7.3'; | |||||||||||||||||||||||||||
| 60 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 2 | ▶ | use base qw(Bio::Root::Root); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 2 | ▶ | use Bio::PhyloNetwork; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 66 | 2 | ▶ | use Bio::PhyloNetwork::TreeFactory; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head2 new | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | Title : new | ||||||||||||||||||||||||||||
| 71 | Usage : my $factory = new Bio::PhyloNetwork::Factory(); | ||||||||||||||||||||||||||||
| 72 | Function: Creates a new Bio::PhyloNetwork::Factory | ||||||||||||||||||||||||||||
| 73 | Returns : Bio::PhyloNetwork::RandomFactory | ||||||||||||||||||||||||||||
| 74 | Args : -numleaves => integer | ||||||||||||||||||||||||||||
| 75 | OR | ||||||||||||||||||||||||||||
| 76 | -leaves => reference to an array (of leaves names) | ||||||||||||||||||||||||||||
| 77 | -numhybrids => integer [default = numleaves -1] | ||||||||||||||||||||||||||||
| 78 | -recurse => boolean [optional] | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | Returns a Bio::PhyloNetwork::Factory object. Such an object will | ||||||||||||||||||||||||||||
| 81 | sequentially create binary tree-child phylogenetic networks | ||||||||||||||||||||||||||||
| 82 | each time next_network is called. | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | If the parameter -leaves=E<gt>\@leaves is given, then the set of leaves of | ||||||||||||||||||||||||||||
| 85 | these networks will be @leaves. If it is given the parameter | ||||||||||||||||||||||||||||
| 86 | -numleaves=E<gt>$numleaves, then the set of leaves will be "l1"..."l$numleaves". | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | If the parameter -numhybrids=E<gt>$numhybrids is given, then the generated | ||||||||||||||||||||||||||||
| 89 | networks will have at most $numhybrids hybrid nodes. Note that, necessarily, | ||||||||||||||||||||||||||||
| 90 | $numhybrids E<lt> $numleaves. | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | If the parameter -recurse=E<gt>1 is given, then all networks with number of hybrid | ||||||||||||||||||||||||||||
| 93 | nodes less or equal to $numhybrids will be given; otherwise only those with | ||||||||||||||||||||||||||||
| 94 | exactly $numhybrids hybrid nodes. | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =cut | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 2 | my ($pkg,@args)=@_; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 2 | my $self=$pkg->SUPER::new(@args); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 2 | my ($leavesR,$numleaves,$numhybrids,$recurse)= | |||||||||||||||||||||||||||
| 104 | $self->_rearrange([qw(LEAVES | ||||||||||||||||||||||||||||
| 105 | NUMLEAVES | ||||||||||||||||||||||||||||
| 106 | NUMHYBRIDS | ||||||||||||||||||||||||||||
| 107 | RECURSE)],@args); | ||||||||||||||||||||||||||||
| 108 | 2 | my @leaves; | |||||||||||||||||||||||||||
| 109 | 2 | ▶ | if ((! defined $leavesR) && (defined $numleaves)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $leavesR and defined $numleaves
MC/DC50%1/2
not defined $leavesR and defined $numleaves
not defined $leavesR
defined $numleaves
Truth table66%2/3
not(A) and B
A= not defined $leavesRB= defined $numleaves
| |||||||||||||||||||||||||||||
| 110 | 1 | @leaves=map {"l$_"} (1..$numleaves); | |||||||||||||||||||||||||||
| 111 | 1 | $leavesR=\@leaves; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 2 | ▶ | if (! defined $leavesR) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 0 | $self->throw("No leaves set neither numleaves given"); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 2 | @leaves=@$leavesR; | |||||||||||||||||||||||||||
| 117 | 2 | $self->{leaves}=$leavesR; | |||||||||||||||||||||||||||
| 118 | 2 | $numleaves=@leaves; | |||||||||||||||||||||||||||
| 119 | 2 | $self->{numleaves}=$numleaves; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 2 | ▶ | $recurse ||= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$recurse ||= 0
MC/DC100%1/1
$recurse ||= 0
$recurse
Truth table100%2/2
A
A= $recurse
| |||||||||||||||||||||||||||||
| 122 | 2 | ▶ | if (! defined $numhybrids) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 1 | $numhybrids=$numleaves-1; | |||||||||||||||||||||||||||
| 124 | 1 | $recurse=1; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 2 | $self->{recurse}=$recurse; | |||||||||||||||||||||||||||
| 127 | 2 | $self->{numhybrids}=$numhybrids; | |||||||||||||||||||||||||||
| 128 | 2 | ▶ | if ($numhybrids ==0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | return Bio::PhyloNetwork::TreeFactory->new(-leaves=>\@leaves); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 2 | my $parent; | |||||||||||||||||||||||||||
| 132 | 2 | ▶ | if ($numhybrids > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 1 | $parent=new($pkg,'-leaves'=>\@leaves, | |||||||||||||||||||||||||||
| 134 | '-numhybrids'=>($numhybrids-1), | ||||||||||||||||||||||||||||
| 135 | '-recurse'=>($recurse)); | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | else { | ||||||||||||||||||||||||||||
| 138 | 1 | $parent=Bio::PhyloNetwork::TreeFactory->new(-leaves=>\@leaves); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 2 | $self->{parent}=$parent; | |||||||||||||||||||||||||||
| 141 | 2 | my $oldnet=$parent->next_network(); | |||||||||||||||||||||||||||
| 142 | 2 | $self->{oldnet}=$oldnet; | |||||||||||||||||||||||||||
| 143 | 2 | $self->update(); | |||||||||||||||||||||||||||
| 144 | 2 | $self->{found}=[]; | |||||||||||||||||||||||||||
| 145 | 2 | bless($self,$pkg); | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 27 | ▶ | sub update { | ||||||||||||||||||||||||||
|
Subroutine
update: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 149 | 27 | my ($self)=@_; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 27 | my @candidates=$self->{oldnet}->edges(); | |||||||||||||||||||||||||||
| 152 | 27 | $self->{candidates}=\@candidates; | |||||||||||||||||||||||||||
| 153 | 27 | $self->{numcandidates}=(scalar @candidates); | |||||||||||||||||||||||||||
| 154 | 27 | $self->{index1}=-$self->{recurse}; | |||||||||||||||||||||||||||
| 155 | 27 | $self->{index2}=0; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =head2 next_network | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | Title : next_network | ||||||||||||||||||||||||||||
| 161 | Usage : my $net=$factory->next_network() | ||||||||||||||||||||||||||||
| 162 | Function: returns a network | ||||||||||||||||||||||||||||
| 163 | Returns : Bio::PhyloNetwork | ||||||||||||||||||||||||||||
| 164 | Args : none | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =cut | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 92 | ▶ | sub next_network { | ||||||||||||||||||||||||||
|
Subroutine
next_network: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 169 | 92 | my ($self)=@_; | |||||||||||||||||||||||||||
| 170 | 92 | my $numleaves=$self->{numleaves}; | |||||||||||||||||||||||||||
| 171 | 92 | my $numhybrids=$self->{numhybrids}; | |||||||||||||||||||||||||||
| 172 | START: | ||||||||||||||||||||||||||||
| 173 | 1134 | ▶ | if ($self->{index1}==-1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 27 | $self->{index1}++; | |||||||||||||||||||||||||||
| 175 | 27 | return $self->{oldnet}; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | 1107 | ▶ | if ($self->{index1} >= $self->{numcandidates}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 162 | $self->{index2}++; | |||||||||||||||||||||||||||
| 179 | 162 | $self->{index1}=0; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | 1107 | ▶ | if ($self->{index2} >= $self->{numcandidates}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 27 | my $oldnet=$self->{parent}->next_network(); | |||||||||||||||||||||||||||
| 183 | 27 | ▶ | if (! $oldnet) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 2 | return 0; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | 25 | $self->{oldnet}=$oldnet; | |||||||||||||||||||||||||||
| 187 | 25 | $self->update(); | |||||||||||||||||||||||||||
| 188 | 25 | goto START; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | 1080 | ▶ | if ((scalar $self->{oldnet}->hybrid_nodes())< $self->{numhybrids}-1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 3 | $self->{candidates}=[]; | |||||||||||||||||||||||||||
| 192 | 3 | $self->{numcandidates}=0; | |||||||||||||||||||||||||||
| 193 | 3 | goto START; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | 1077 | my $u1=$self->{candidates}->[$self->{index1}]->[0]; | |||||||||||||||||||||||||||
| 196 | 1077 | my $v1=$self->{candidates}->[$self->{index1}]->[1]; | |||||||||||||||||||||||||||
| 197 | 1077 | my $u2=$self->{candidates}->[$self->{index2}]->[0]; | |||||||||||||||||||||||||||
| 198 | 1077 | my $v2=$self->{candidates}->[$self->{index2}]->[1]; | |||||||||||||||||||||||||||
| 199 | 1077 | my $lbl=$self->{numhybrids}; | |||||||||||||||||||||||||||
| 200 | 1077 | ▶ | if ($self->{oldnet}->is_attackable($u1,$v1,$u2,$v2)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 156 | my $net=Bio::PhyloNetwork->new(-graph=>$self->{oldnet}->graph); | |||||||||||||||||||||||||||
| 202 | 156 | $net->do_attack($u1,$v1,$u2,$v2,$lbl); | |||||||||||||||||||||||||||
| 203 | 156 | $self->{index1}++; | |||||||||||||||||||||||||||
| 204 | 156 | my @found=@{$self->{found}}; | |||||||||||||||||||||||||||
| 205 | 156 | foreach my $netant (@found) { | |||||||||||||||||||||||||||
| 206 | 2978 | ▶ | if ($net->is_mu_isomorphic($netant) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 93 | goto START; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | 63 | push @found,$net; | |||||||||||||||||||||||||||
| 211 | 63 | $self->{found}=\@found; | |||||||||||||||||||||||||||
| 212 | 63 | return $net; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | else { | ||||||||||||||||||||||||||||
| 215 | 921 | $self->{index1}++; | |||||||||||||||||||||||||||
| 216 | 921 | goto START; | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 2 | 1; | |||||||||||||||||||||||||||