PhyloNetwork/FactoryX.pm

filter: Statementstmt 17.8% 20 / 112 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 54.5% 6 / 11 Podpod 40.0% 2 / 5 totaltotal 17.1% 28 / 163 CC 18 SCAR 34.6
CC
18
Cov
14%
new
56.0
next_network_new
56.0
next_network
20.0
CRAP 31.9
« PhyloNetwork/TreeFactoryMulti.pm ↑ Summary PhyloNetwork/TreeFactoryX.pm »
1 #
2 # Module for Bio::PhyloNetwork::FactoryX
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::FactoryX - 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::FactoryX;
59 1 $Bio::PhyloNetwork::FactoryX::VERSION = '1.7.3';
60 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 1 ▶use base qw(Bio::Root::Root);
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 1 ▶use Bio::PhyloNetwork;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 ▶use Bio::PhyloNetwork::TreeFactoryX;
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 =head2 new
71  
72 Title : new
73 Usage : my $factory = new Bio::PhyloNetwork::Factory();
74 Function: Creates a new Bio::PhyloNetwork::Factory
75 Returns : Bio::PhyloNetwork::RandomFactory
76 Args : -numleaves => integer
77 OR
78 -leaves => reference to an array (of leaves names)
79 -numhybrids => integer [default = numleaves -1]
80 -recurse => boolean [optional]
81  
82 Returns a Bio::PhyloNetwork::Factory object. Such an object will
83 sequentially create binary tree-child phylogenetic networks
84 each time next_network is called.
85  
86 If the parameter -leaves=E<gt>\@leaves is given, then the set of leaves of
87 these networks will be @leaves. If it is given the parameter
88 -numleaves=E<gt>$numleaves, then the set of leaves will be "l1"..."l$numleaves".
89  
90 If the parameter -numhybrids=E<gt>$numhybrids is given, then the generated
91 networks will have at most $numhybrids hybrid nodes. Note that, necessarily,
92 $numhybrids E<lt> $numleaves.
93  
94 If the parameter -recurse=E<gt>1 is given, then all networks with number of hybrid
95 nodes less or equal to $numhybrids will be given; otherwise only those with
96 exactly $numhybrids hybrid nodes.
97  
98 =cut
99  
100 0 ▶sub new {
Subroutine new: not called CC=7 SCAR=40.3
Pod documented
101 0 my ($pkg,@args)=@_;
102 0 my $self=$pkg->SUPER::new(@args);
103  
104 0 my ($leavesR,$numleaves,$numhybrids)=
105 $self->_rearrange([qw(LEAVES
106 NUMLEAVES
107 NUMHYBRIDS)],@args);
108 0 my @leaves;
109 0 ▶ if ((! defined $leavesR) && (defined $numleaves)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $leavesR and defined $numleaves) 0 0 0
Condition0%0/3
not defined $leavesR and defined $numleaves
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $leavesR and defined $numleaves
not defined $leavesR defined $numleaves
Truth table0%0/3
not(A) and B
A= not defined $leavesRB= defined $numleaves
ABresult
0X0
100
111
110 0 @leaves=map {"l$_"} (1..$numleaves);
111 0 $leavesR=\@leaves;
112 }
113 0 ▶ if (! defined $leavesR) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $leavesR) 0 0 0
114 0 $self->throw("No leaves set neither numleaves given");
115 }
116 0 @leaves=@$leavesR;
117 0 $self->{leaves}=$leavesR;
118 0 $numleaves=@leaves;
119 0 $self->{numleaves}=$numleaves;
120  
121 0 ▶ if (! defined $numhybrids) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $numhybrids) 0 0 0
122 0 $numhybrids=$numleaves-1;
123 }
124 0 $self->{numhybrids}=$numhybrids;
125 0 ▶ if ($numhybrids ==0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numhybrids == 0) 0 0 0
126 0 return Bio::PhyloNetwork::TreeFactoryX->new(-leaves=>\@leaves);
127 }
128 0 my $parent;
129 0 ▶ if ($numhybrids > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numhybrids > 1) { } 0 0 0
130 0 $parent=new($pkg,'-leaves'=>\@leaves,
131 '-numhybrids'=>($numhybrids-1)
132 );
133 0 my @subfactories=@{$parent->{subfactories}};
134 0 push @subfactories,$parent;
135 # print "$numhybrids : ".(scalar @subfactories);
136 # print "\n";
137 0 $self->{subfactories}=\@subfactories;
138 # print "$numhybrids: ".(scalar @subfactories)."\n";
139 }
140 else {
141 0 $parent=Bio::PhyloNetwork::TreeFactoryX->new(-leaves=>\@leaves);
142 0 $self->{subfactories}=[$parent];
143 }
144 0 $self->{parent}=$parent;
145 0 $self->update();
146 0 $self->{found}=[];
147 0 $self->{thrown}=0;
148 0 $self->{hybnow}=0;
149 0 bless($self,$pkg);
150 }
151  
152 0 ▶sub update {
Subroutine update: not called CC=2 SCAR=17.9
Pod undocumented
153 0 my ($self)=@_;
154  
155 0 ▶ if (defined $self->{oldnet}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'oldnet'}) { } 0 0 0
156 0 my @candidates=$self->{oldnet}->edges();
157 0 $self->{candidates}=\@candidates;
158 0 $self->{numcandidates}=(scalar @candidates);
159 0 $self->{index1}=0;
160 0 $self->{index2}=0;
161 } else {
162 0 $self->{candidates}=[];
163 0 $self->{numcandidates}=0;
164 0 $self->{index1}=0;
165 0 $self->{index2}=0;
166 }
167 }
168  
169 0 ▶sub next_network_repeated {
Subroutine next_network_repeated: not called CC=2 SCAR=17.9
Pod undocumented
170 0 my ($self)=@_;
171  
172 0 ▶ return 0 if ($self->{thrown} >= (scalar @{$self->{found}}));
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"thrown"} >= scalar @{$self->{"found"};} 0 0 0
173 0 $self->{thrown}=$self->{thrown}+1;
174 0 return $self->{found}->[$self->{thrown}-1];
175 }
176  
177 0 ▶sub next_network_new {
Subroutine next_network_new: not called CC=7 SCAR=40.3
Pod undocumented
178 0 my ($self)=@_;
179 START:
180 # print $self->{index1}.",".$self->{index2}.":".$self->{numcandidates}."\n";
181 0 ▶ if ($self->{index1} >= $self->{numcandidates}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"index1"} >= $self->{"numcandidates"}) 0 0 0
182 0 $self->{index2}++;
183 0 $self->{index1}=0;
184 }
185 # print $self->{index1}.",".$self->{index2}.":".$self->{numcandidates}."\n";
186 0 ▶ if ($self->{index2} >= $self->{numcandidates}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"index2"} >= $self->{"numcandidates"}) 0 0 0
187 0 my $oldnet=$self->{parent}->next_network_repeated();
188 0 ▶ if (! $oldnet) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($oldnet) 0 0 0
189 # print "notoldnet\n";
190 0 return 0;
191 }
192 0 $self->{oldnet}=$oldnet;
193 0 $self->update();
194 }
195 0 my $u1=$self->{candidates}->[$self->{index1}]->[0];
196 0 my $v1=$self->{candidates}->[$self->{index1}]->[1];
197 0 my $u2=$self->{candidates}->[$self->{index2}]->[0];
198 0 my $v2=$self->{candidates}->[$self->{index2}]->[1];
199 0 my $lbl=$self->{numhybrids};
200 0 ▶ if ($self->{oldnet}->is_attackable($u1,$v1,$u2,$v2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'oldnet'}->is_attackable($u1, $v1, $u2, $v2)) { } 0 0 0
201 0 my $net=Bio::PhyloNetwork->new(-graph=>$self->{oldnet}->graph);
202 0 $net->do_attack($u1,$v1,$u2,$v2,$lbl);
203 0 $self->{index1}++;
204 0 my @found=@{$self->{found}};
205 0 foreach my $netant (@found) {
206 0 ▶ if ($net->is_mu_isomorphic($netant) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($net->is_mu_isomorphic($netant)) 0 0 0
207 0 goto START;
208 }
209 }
210 0 push @found,$net;
211 0 $self->{found}=\@found;
212 0 return $net;
213 }
214 else {
215 0 $self->{index1}++;
216 0 goto START;
217 }
218 }
219  
220 =head2 next_network
221  
222 Title : next_network
223 Usage : my $net=$factory->next_network()
224 Function: returns a network
225 Returns : Bio::PhyloNetwork
226 Args : none
227  
228 =cut
229  
230 0 ▶sub next_network {
Subroutine next_network: not called CC=4 SCAR=30.0
Pod documented
231 0 my ($self)=@_;
232 0 my $hybnow;
233 WTF:
234 0 $hybnow=$self->{hybnow};
235 # print $hybnow;
236 # print Dumper($self->{subfactories}->[$hybnow]);
237 # print "$hybnow\n";
238 # print (scalar @{$self->{subfactories}});
239 # print "\n";
240 0 my $net;
241 0 ▶ if ($hybnow < $self->{numhybrids}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hybnow < $self->{'numhybrids'}) { } 0 0 0
242 0 $net=$self->{subfactories}->[$hybnow]->next_network_new();
243 } else {
244 0 $net=$self->next_network_new();
245 }
246 0 ▶ if (! $net) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($net) 0 0 0
247 0 ▶ if ($hybnow < $self->{numhybrids}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hybnow < $self->{"numhybrids"}) 0 0 0
248 0 $self->{hybnow}=$self->{hybnow}+1;
249 0 goto WTF;
250 }
251 0 return 0;
252 }
253 0 return $net;
254 }
255  
256 1 1;
257  
« PhyloNetwork/TreeFactoryMulti.pm ↑ Summary PhyloNetwork/TreeFactoryX.pm »