merge upstream
[bioperl-live.git] / Bio / PhyloNetwork / TreeFactory.pm
blobe3fc90b144d3a31cce7a86d0a9aeddf54ad2df70
2 # Module for Bio::PhyloNetwork::TreeFactory
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Gabriel Cardona <gabriel(dot)cardona(at)uib(dot)es>
8 # Copyright Gabriel Cardona
10 # You may distribute this module under the same terms as perl itself
12 # POD documentation - main docs before the code
14 =head1 NAME
16 Bio::PhyloNetwork::TreeFactory - Module to sequentially generate
17 Phylogenetic Trees
19 =head1 SYNOPSIS
21 use strict;
22 use warnings;
24 use Bio::PhyloNetwork;
25 use Bio::PhyloNetwork::TreeFactory;
27 # Will generate sequentially all the 15 binary phylogetic
28 # trees with 4 leaves
30 my $factory=Bio::PhyloNetwork::TreeFactory->new(-numleaves=>4);
32 my @nets;
34 while (my $net=$factory->next_network()) {
35 push @nets,$net;
36 print "".(scalar @nets).": ".$net->eNewick()."\n";
39 =head1 DESCRIPTION
41 Sequentially builds a (binary) phylogenetic tree each time
42 next_network is called.
44 =head1 AUTHOR
46 Gabriel Cardona, gabriel(dot)cardona(at)uib(dot)es
48 =head1 SEE ALSO
50 L<Bio::PhyloNetwork>
52 =head1 APPENDIX
54 The rest of the documentation details each of the object methods.
56 =cut
58 package Bio::PhyloNetwork::TreeFactory;
60 use strict;
61 use warnings;
63 use base qw(Bio::Root::Root);
65 use Bio::PhyloNetwork;
67 =head2 new
69 Title : new
70 Usage : my $factory = new Bio::PhyloNetwork::TreeFactory();
71 Function: Creates a new Bio::PhyloNetwork::TreeFactory
72 Returns : Bio::PhyloNetwork::RandomFactory
73 Args : -numleaves => integer
75 -leaves => reference to an array (of leaves names)
77 Returns a Bio::PhyloNetwork::TreeFactory object. Such an object will
78 sequentially create binary phylogenetic trees
79 each time next_network is called.
81 If the parameter -leaves=E<gt>\@leaves is given, then the set of leaves of
82 these networks will be @leaves. If it is given the parameter
83 -numleaves=E<gt>$numleaves, then the set of leaves will be "l1"..."l$numleaves".
85 =cut
87 sub new {
88 my ($pkg,@args)=@_;
90 my $self=$pkg->SUPER::new(@args);
92 my ($leavesR,$numleaves,$numhybrids)=
93 $self->_rearrange([qw(LEAVES
94 NUMLEAVES
95 NUMHYBRIDS)],@args);
97 my @leaves;
98 if ((! defined $leavesR) && (defined $numleaves)) {
99 @leaves=map {"l$_"} (1..$numleaves);
100 $leavesR=\@leaves;
102 if (! defined $leavesR) {
103 $self->throw("No leaves set neither numleaves given");
105 @leaves=@$leavesR;
106 $self->{leaves}=$leavesR;
108 $numleaves=@leaves;
109 $self->{numleaves}=$numleaves;
110 if ($numleaves > 2) {
111 my @leavesparent=@leaves;
112 my $newleaf=pop @leavesparent;
113 $self->{newleaf}=$newleaf;
114 $self->{parent}=
115 new($pkg,-leaves=>\@leavesparent);
116 my $oldnet=$self->{parent}->next_network();
117 $self->{oldnet}=$oldnet;
118 my @candidates=$oldnet->nodes();
119 $self->{candidates}=\@candidates;
121 $self->{index}=0;
123 bless($self,$pkg);
126 =head2 next_network
128 Title : next_network
129 Usage : my $net=$factory->next_network()
130 Function: returns a tree
131 Returns : Bio::PhyloNetwork
132 Args : none
134 =cut
136 sub next_network {
137 my ($self)=@_;
139 my $n=$self->{numleaves};
140 if ($self->{numleaves} == 2) {
141 if ($self->{index} == 0) {
142 my $graph=Graph::Directed->new();
143 $graph->add_edges("t0",$self->{leaves}->[0],"t0",$self->{leaves}->[1]);
144 my $net=Bio::PhyloNetwork->new(-graph=>$graph);
145 $self->{index}++;
146 return $net;
148 else {
149 return 0;
152 else {
153 if ($self->{index} == (scalar @{$self->{candidates}})) {
154 my $oldnet=$self->{parent}->next_network();
155 if (! $oldnet) {
156 return 0;
158 $self->{oldnet}=$oldnet;
159 my @candidates=$oldnet->nodes();
160 $self->{candidates}=\@candidates;
161 $self->{index}=0;
163 my $graph=$self->{oldnet}->{graph}->copy();
164 my $u=$self->{candidates}->[$self->{index}];
165 foreach my $w ($graph->predecessors($u)) {
166 $graph->delete_edge($w,$u);
167 $graph->add_edge($w,"t$n");
169 $graph->add_edge("t$n",$u);
170 $graph->add_edge("t$n",$self->{newleaf});
171 my $net=Bio::PhyloNetwork->new(-graph=>$graph);
172 $self->{index}++;
173 return $net;