PhyloNetwork/RandomFactory.pm

filter: Statementstmt 96.9% 64 / 66 Branchbran 68.7% 11 / 16 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 8 / 8 Podpod 66.6% 2 / 3 totaltotal 86.1% 87 / 101 CC 12 SCAR 14.9
CC
12
Cov
89%
new
6.5
next_network
6.0
random_attack
2.0
CRAP 4.4
« PhyloNetwork/Factory.pm ↑ Summary PhyloNetwork/TreeFactory.pm »
1 #
2 # Module for Bio::PhyloNetwork::RandomFactory
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::RandomFactory - Module to generate random
17 Phylogenetic Networks
18  
19 =head1 SYNOPSIS
20  
21 use strict;
22 use warnings;
23  
24 use Bio::PhyloNetwork;
25 use Bio::PhyloNetwork::RandomFactory;
26  
27 # Will generate at random all the 66 binary tree-child phylogenetic
28 # networks with 3 leaves
29  
30 my $factory=Bio::PhyloNetwork::RandomFactory->new(-numleaves=>3,-norepeat=>1);
31  
32 my @nets;
33  
34 for (my $i=0; $i<66; $i++) {
35 my $net=$factory->next_network();
36 push @nets,$net;
37 print "".(scalar @nets).": ".$net->eNewick()."\n";
38 }
39  
40 =head1 DESCRIPTION
41  
42 Builds a random (binary tree-child) phylogenetic network each time
43 next_network is called.
44  
45 =head1 AUTHOR
46  
47 Gabriel Cardona, gabriel(dot)cardona(at)uib(dot)es
48  
49 =head1 SEE ALSO
50  
51 L<Bio::PhyloNetwork>
52  
53 =head1 APPENDIX
54  
55 The rest of the documentation details each of the object methods.
56  
57 =cut
58  
59 package Bio::PhyloNetwork::RandomFactory;
60 2 $Bio::PhyloNetwork::RandomFactory::VERSION = '1.7.3';
61 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 2 ▶use base qw(Bio::Root::Root);
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 2 ▶use Bio::PhyloNetwork;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 2 ▶use Bio::Tree::RandomFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 =head2 new
70  
71 Title : new
72 Usage : my $factory = new Bio::PhyloNetwork::RandomFactory();
73 Function: Creates a new Bio::PhyloNetwork::RandomFactory
74 Returns : Bio::PhyloNetwork::RandomFactory
75 Args : -numleaves => integer
76 OR
77 -leaves => reference to an array (of leaves names)
78 -numhybrids => integer [optional]
79 -norepeat => boolean [optional]
80  
81 Returns a Bio::PhyloNetwork::RandomFactory object. Such an object will create
82 random binary tree-child phylogenetic networks each time next_network
83 is called.
84  
85 If the parameter -leaves=E<gt>\@leaves is given, then the set of leaves of
86 these networks will be @leaves. If it is given the parameter
87 -numleaves=E<gt>$numleaves, then the set of leaves will be "l1"..."l$numleaves".
88  
89 If the parameter -numhybrids=E<gt>$numhybrids is given, then the generated
90 networks will have exactly $numhybrids hybrid nodes. Note that, necessarily,
91 $numhybrids E<lt> $numleaves. Otherwise, the number of hybrid nodes will be chosen
92 at random for each call of next_network.
93  
94 If the parameter -norepeat=E<gt>1 is given, then successive calls of next_network
95 will give non-isomorphic networks.
96  
97 =cut
98  
99 1 ▶sub new {
Subroutine new: called CC=6 SCAR=18.7
Pod documented
100 1 my ($pkg,@args)=@_;
101  
102 1 my $self=$pkg->SUPER::new(@args);
103  
104 1 my ($leavesR,$numleaves,$numhybrids,$norepeat)=
105 $self->_rearrange([qw(LEAVES
106 NUMLEAVES
107 NUMHYBRIDS
108 NOREPEAT)],@args);
109 1 my @leaves;
110 1 ▶ if ((! defined $leavesR) && (defined $numleaves)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $leavesR and defined $numleaves) 1 0 1
Condition33%1/3
not defined $leavesR and defined $numleaves
!ll&&!rl&&r
001
MC/DC0%0/2
not defined $leavesR and defined $numleaves
not defined $leavesR defined $numleaves
Truth table33%1/3
not(A) and B
A= not defined $leavesRB= defined $numleaves
ABresult
0X0
100
111
111 1 @leaves=map {"l$_"} (1..$numleaves);
112 1 $leavesR=\@leaves;
113 }
114 1 ▶ if (! defined $leavesR) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $leavesR) 0 1 1
115 0 $self->throw("No leaves set neither numleaves given");
116 }
117 1 ▶ $norepeat ||= 0;
Condition50%1/2
$norepeat ||= 0
l!l
10
MC/DC0%0/1
$norepeat ||= 0
$norepeat
Truth table50%1/2
A
A= $norepeat
Aresult
11
00
118  
119 1 $self->{leaves}=\@leaves;
120 1 $self->{numleaves}=$numleaves;
121 1 ▶ $self->{numhybrids}=$numhybrids if defined $numhybrids;
Branch: 1 branch
BranchTrueFalseTotal
if defined $numhybrids 0 1 1
122 1 $self->{norepeat}=$norepeat;
123 1 $self->{found}=[];
124 1 $self->{tree_factory}=Bio::Tree::RandomFactory->new(-taxa => \@leaves);
125 1 bless($self,$pkg);
126 }
127  
128 =head2 next_network
129  
130 Title : next_network
131 Usage : my $net=$factory->next_network()
132 Function: returns a random network
133 Returns : Bio::PhyloNetwork
134 Args : none
135  
136 =cut
137  
138 66 ▶sub next_network {
Subroutine next_network: called CC=6 SCAR=18.0
Pod documented
139 66 my ($self)=@_;
140  
141 66 my $numleaves=$self->{numleaves};
142 66 my @found=@{$self->{found}};
143 66 my $numhybrids;
144 START:
145 530 ▶ if (! defined $self->{numhybrids}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{'numhybrids'}) { } 530 0 530
146 530 $numhybrids=int(rand($numleaves));
147 }
148 else {
149 0 $numhybrids=$self->{numhybrids};
150 }
151 530 my $tf=$self->{tree_factory};
152 530 my $tree=$tf->next_tree;
153 530 my $net=Bio::PhyloNetwork->new(-tree=>$tree);
154 530 ▶ for (my $i=1; $i<=$numhybrids; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $numhybrids) 550 530 1080
155 550 $net=random_attack($net,$i);
156 }
157 530 ▶ if ($self->{norepeat}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"norepeat"}) 530 0 530
158 530 foreach my $ant (@found) {
159 11511 ▶ goto START if $net->is_mu_isomorphic($ant);
Branch: 1 branch
BranchTrueFalseTotal
if $net->is_mu_isomorphic($ant) 464 11047 11511
160 }
161 66 push @found,$net;
162 66 $self->{found}=\@found;
163 }
164 66 return $net;
165 }
166  
167 550 ▶sub random_attack {
Subroutine random_attack: called CC=2 SCAR=6.9
Pod undocumented
168 550 my ($net,$lbl)=@_;
169  
170 550 my $graph=$net->{graph};
171 550 my ($u1,$v1,$u2,$v2);
172 550 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$net->is_attackable($u1, $v1, $u2, $v2, $lbl) or do { do { my $e1 = $graph->random_edge; my $e2 = $graph->random_edge; $u1 = $e1->[0]; $v1 = $e1->[1]; $u2 = $e2->[0]; $v2 = $e2->[1] } } 550 1778 2328
173 2328 my $e1=$graph->random_edge;
174 2328 my $e2=$graph->random_edge;
175 2328 $u1=$e1->[0];
176 2328 $v1=$e1->[1];
177 2328 $u2=$e2->[0];
178 2328 $v2=$e2->[1];
179 } while (! $net->is_attackable($u1,$v1,$u2,$v2,$lbl));
180 550 $net->do_attack($u1,$v1,$u2,$v2,$lbl);
181 550 return $net;
182 }
183  
184 2 1;
« PhyloNetwork/Factory.pm ↑ Summary PhyloNetwork/TreeFactory.pm »