| 1 | # | ||||||||||||||||||||||||||||
| 2 | # Module for Bio::PhyloNetwork::TreeFactory | ||||||||||||||||||||||||||||
| 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::TreeFactory - Module to sequentially generate | ||||||||||||||||||||||||||||
| 17 | Phylogenetic Trees | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | use strict; | ||||||||||||||||||||||||||||
| 22 | use warnings; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | use Bio::PhyloNetwork; | ||||||||||||||||||||||||||||
| 25 | use Bio::PhyloNetwork::TreeFactory; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # Will generate sequentially all the 15 binary phylogetic | ||||||||||||||||||||||||||||
| 28 | # trees with 4 leaves | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | my $factory=Bio::PhyloNetwork::TreeFactory->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) phylogenetic tree 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::TreeFactory; | ||||||||||||||||||||||||||||
| 59 | 4 | $Bio::PhyloNetwork::TreeFactory::VERSION = '1.7.3'; | |||||||||||||||||||||||||||
| 60 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 4 | ▶ | use base qw(Bio::Root::Root); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 4 | ▶ | use Bio::PhyloNetwork; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =head2 new | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||
| 74 | OR | ||||||||||||||||||||||||||||
| 75 | -leaves => reference to an array (of leaves names) | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | Returns a Bio::PhyloNetwork::TreeFactory object. Such an object will | ||||||||||||||||||||||||||||
| 78 | sequentially create binary phylogenetic trees | ||||||||||||||||||||||||||||
| 79 | each time next_network is called. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 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". | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =cut | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 5 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 5 | my ($pkg,@args)=@_; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 5 | my $self=$pkg->SUPER::new(@args); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 5 | my ($leavesR,$numleaves,$numhybrids)= | |||||||||||||||||||||||||||
| 93 | $self->_rearrange([qw(LEAVES | ||||||||||||||||||||||||||||
| 94 | NUMLEAVES | ||||||||||||||||||||||||||||
| 95 | NUMHYBRIDS)],@args); | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 5 | my @leaves; | |||||||||||||||||||||||||||
| 98 | 5 | ▶ | 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
| |||||||||||||||||||||||||||||
| 99 | 1 | @leaves=map {"l$_"} (1..$numleaves); | |||||||||||||||||||||||||||
| 100 | 1 | $leavesR=\@leaves; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 5 | ▶ | if (! defined $leavesR) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | $self->throw("No leaves set neither numleaves given"); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 5 | @leaves=@$leavesR; | |||||||||||||||||||||||||||
| 106 | 5 | $self->{leaves}=$leavesR; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 5 | $numleaves=@leaves; | |||||||||||||||||||||||||||
| 109 | 5 | $self->{numleaves}=$numleaves; | |||||||||||||||||||||||||||
| 110 | 5 | ▶ | if ($numleaves > 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 3 | my @leavesparent=@leaves; | |||||||||||||||||||||||||||
| 112 | 3 | my $newleaf=pop @leavesparent; | |||||||||||||||||||||||||||
| 113 | 3 | $self->{newleaf}=$newleaf; | |||||||||||||||||||||||||||
| 114 | $self->{parent}= | ||||||||||||||||||||||||||||
| 115 | 3 | new($pkg,-leaves=>\@leavesparent); | |||||||||||||||||||||||||||
| 116 | 3 | my $oldnet=$self->{parent}->next_network(); | |||||||||||||||||||||||||||
| 117 | 3 | $self->{oldnet}=$oldnet; | |||||||||||||||||||||||||||
| 118 | 3 | my @candidates=$oldnet->nodes(); | |||||||||||||||||||||||||||
| 119 | 3 | $self->{candidates}=\@candidates; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 5 | $self->{index}=0; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 5 | bless($self,$pkg); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head2 next_network | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | Title : next_network | ||||||||||||||||||||||||||||
| 129 | Usage : my $net=$factory->next_network() | ||||||||||||||||||||||||||||
| 130 | Function: returns a tree | ||||||||||||||||||||||||||||
| 131 | Returns : Bio::PhyloNetwork | ||||||||||||||||||||||||||||
| 132 | Args : none | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =cut | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 28 | ▶ | sub next_network { | ||||||||||||||||||||||||||
|
Subroutine
next_network: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 137 | 28 | my ($self)=@_; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 28 | my $n=$self->{numleaves}; | |||||||||||||||||||||||||||
| 140 | 28 | ▶ | if ($self->{numleaves} == 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 4 | ▶ | if ($self->{index} == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 2 | my $graph=Graph::Directed->new(); | |||||||||||||||||||||||||||
| 143 | 2 | $graph->add_edges("t0",$self->{leaves}->[0],"t0",$self->{leaves}->[1]); | |||||||||||||||||||||||||||
| 144 | 2 | my $net=Bio::PhyloNetwork->new(-graph=>$graph); | |||||||||||||||||||||||||||
| 145 | 2 | $self->{index}++; | |||||||||||||||||||||||||||
| 146 | 2 | return $net; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | else { | ||||||||||||||||||||||||||||
| 149 | 2 | return 0; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | else { | ||||||||||||||||||||||||||||
| 153 | 24 | ▶ | if ($self->{index} == (scalar @{$self->{candidates}})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 5 | my $oldnet=$self->{parent}->next_network(); | |||||||||||||||||||||||||||
| 155 | 5 | ▶ | if (! $oldnet) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 3 | return 0; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | 2 | $self->{oldnet}=$oldnet; | |||||||||||||||||||||||||||
| 159 | 2 | my @candidates=$oldnet->nodes(); | |||||||||||||||||||||||||||
| 160 | 2 | $self->{candidates}=\@candidates; | |||||||||||||||||||||||||||
| 161 | 2 | $self->{index}=0; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 21 | my $graph=$self->{oldnet}->{graph}->copy(); | |||||||||||||||||||||||||||
| 164 | 21 | my $u=$self->{candidates}->[$self->{index}]; | |||||||||||||||||||||||||||
| 165 | 21 | foreach my $w ($graph->predecessors($u)) { | |||||||||||||||||||||||||||
| 166 | 16 | $graph->delete_edge($w,$u); | |||||||||||||||||||||||||||
| 167 | 16 | $graph->add_edge($w,"t$n"); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | 21 | $graph->add_edge("t$n",$u); | |||||||||||||||||||||||||||
| 170 | 21 | $graph->add_edge("t$n",$self->{newleaf}); | |||||||||||||||||||||||||||
| 171 | 21 | my $net=Bio::PhyloNetwork->new(-graph=>$graph); | |||||||||||||||||||||||||||
| 172 | 21 | $self->{index}++; | |||||||||||||||||||||||||||
| 173 | 21 | return $net; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 4 | 1; | |||||||||||||||||||||||||||