3 # Module for Bio::PhyloNetwork::TreeFactoryX
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
7 # Cared for by Gabriel Cardona <gabriel(dot)cardona(at)uib(dot)es>
9 # Copyright Gabriel Cardona
11 # You may distribute this module under the same terms as perl itself
13 # POD documentation - main docs before the code
17 Bio::PhyloNetwork::TreeFactoryX - Module to sequentially generate
25 use Bio::PhyloNetwork;
26 use Bio::PhyloNetwork::TreeFactory;
28 # Will generate sequentially all the 15 binary phylogetic
31 my $factory=Bio::PhyloNetwork::TreeFactory->new(-numleaves=>4);
35 while (my $net=$factory->next_network()) {
37 print "".(scalar @nets).": ".$net->eNewick()."\n";
42 Sequentially builds a (binary) phylogenetic tree each time
43 next_network is called.
47 Gabriel Cardona, gabriel(dot)cardona(at)uib(dot)es
55 The rest of the documentation details each of the object methods.
59 package Bio
::PhyloNetwork
::TreeFactoryX
;
64 use base
qw(Bio::Root::Root);
66 use Bio
::PhyloNetwork
;
71 Usage : my $factory = new Bio::PhyloNetwork::TreeFactory();
72 Function: Creates a new Bio::PhyloNetwork::TreeFactory
73 Returns : Bio::PhyloNetwork::RandomFactory
74 Args : -numleaves => integer
76 -leaves => reference to an array (of leaves names)
78 Returns a Bio::PhyloNetwork::TreeFactory object. Such an object will
79 sequentially create binary phylogenetic trees
80 each time next_network is called.
82 If the parameter -leaves=E<gt>\@leaves is given, then the set of leaves of
83 these networks will be @leaves. If it is given the parameter
84 -numleaves=E<gt>$numleaves, then the set of leaves will be "l1"..."l$numleaves".
91 my $self=$pkg->SUPER::new
(@args);
93 my ($leavesR,$numleaves,$numhybrids)=
94 $self->_rearrange([qw(LEAVES
99 if ((! defined $leavesR) && (defined $numleaves)) {
100 @leaves=map {"l$_"} (1..$numleaves);
103 if (! defined $leavesR) {
104 $self->throw("No leaves set neither numleaves given");
107 $self->{leaves
}=$leavesR;
110 $self->{numleaves
}=$numleaves;
111 if ($numleaves > 2) {
112 my @leavesparent=@leaves;
113 my $newleaf=pop @leavesparent;
114 $self->{newleaf
}=$newleaf;
116 new
($pkg,-leaves
=>\
@leavesparent);
117 my $oldnet=$self->{parent
}->next_network_new();
118 $self->{oldnet
}=$oldnet;
119 my @candidates=$oldnet->nodes();
120 $self->{candidates
}=\
@candidates;
131 Usage : my $net=$factory->next_network()
132 Function: returns a tree
133 Returns : Bio::PhyloNetwork
138 sub next_network_new
{
141 my $n=$self->{numleaves
};
142 if ($self->{numleaves
} == 2) {
143 if ($self->{index} == 0) {
144 my $graph=Graph
::Directed
->new();
145 $graph->add_edges("t0",$self->{leaves
}->[0],"t0",$self->{leaves
}->[1]);
146 my $net=Bio
::PhyloNetwork
->new(-graph
=>$graph);
148 $self->{found
}=[$net];
156 if ($self->{index} == (scalar @
{$self->{candidates
}})) {
157 my $oldnet=$self->{parent
}->next_network_new();
161 $self->{oldnet
}=$oldnet;
162 my @candidates=$oldnet->nodes();
163 $self->{candidates
}=\
@candidates;
166 my $graph=$self->{oldnet
}->{graph
}->copy();
167 my $u=$self->{candidates
}->[$self->{index}];
168 foreach my $w ($graph->predecessors($u)) {
169 $graph->delete_edge($w,$u);
170 $graph->add_edge($w,"t$n");
172 $graph->add_edge("t$n",$u);
173 $graph->add_edge("t$n",$self->{newleaf
});
174 my $net=Bio
::PhyloNetwork
->new(-graph
=>$graph);
176 my @found=@
{$self->{found
}};
178 $self->{found
}=\
@found;
183 sub next_network_repeated
{
186 return 0 if ($self->{thrown
} >= (scalar @
{$self->{found
}}));
187 $self->{thrown
}=$self->{thrown
}+1;
188 return $self->{found
}->[$self->{thrown
}-1];
193 return $self->next_network_new();