PhyloNetwork.pm

filter: Statementstmt 87.7% 613 / 699 Branchbran 76.6% 141 / 184 Conditioncond 78.4% 73 / 93 MC/DCmcdc 56.3% 31 / 55 Subroutinesub 92.1% 70 / 76 Podpod 47.6% 31 / 65 totaltotal 81.8% 959 / 1172 CC 167 SCAR 23.6
CC
167
Cov
85%
topological_restriction
110.0
tripartition_error
16.7
display
12.4
CRAP 10.6
« PhyloNetwork/TreeFactoryX.pm ↑ Summary PhyloNetwork/Factory.pm »
1 #
2 # Module for Bio::PhyloNetwork
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, Gabriel Valiente
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 - Module to compute with Phylogenetic Networks
17  
18 =head1 SYNOPSIS
19  
20 use Bio::PhyloNetwork;
21  
22 # Create a PhyloNetwork object from a eNewick string
23 my $net1=Bio::PhyloNetwork->new(
24 -eNewick=>'t0:((H1,(H2,l2)),H2); H1:((H3,l1)); H2:((H3,(l3,H1))); H3:(l4);'
25 );
26  
27 # Print all available data
28 print $net1;
29  
30 # Rebuild $net1 from its mu_data
31 my %mudata=$net1->mudata();
32 my $net2=Bio::PhyloNetwork->new(-mudata=>\%mudata,-numleaves=>4);
33 print $net2;
34 print "d=".$net1->mu_distance($net2)."\n";
35  
36 # Get another one and compute distance
37 my $net3=Bio::PhyloNetwork->new(
38 -eNewick=>'(l2,((l1,(H1,l4)),H1))r; (l3)H1;'
39 );
40 print "d=".$net1->mu_distance($net3)."\n";
41  
42 # ...and find an optimal alignment w.r.t. the Manhattan distance (default)
43 my ($weight,%alignment)=$net1->optimal_alignment($net3);
44 print "weight:$weight\n";
45 foreach my $node1 (keys %alignment) {
46 print "$node1 => ".$alignment{$node1}."\n";
47 }
48 # ...or the Hamming distance
49  
50 my ($weightH,%alignmentH)=$net1->optimal_alignment($net3,-metric=>'Hamming');
51 print "weight:$weightH\n";
52 foreach my $node1 (keys %alignmentH) {
53 print "$node1 => ".$alignmentH{$node1}."\n";
54 }
55  
56 # Test for time consistency of $net1
57 if ($net1->is_time_consistent) {
58 print "net1 is time consistent\n"
59 }
60 else {
61 print "net1 is not time consistent\n"
62 }
63  
64 # create a network from the list of edges
65 my $net4=Bio::PhyloNetwork->new(-edges=>
66 [qw(r s r t s u s c t c t v u b u l3 u b v b v l4 b l2 c l1)]);
67  
68 # Test for time consistency of $net3
69 if ($net4->is_time_consistent) {
70 print "net4 is time consistent\n"
71 }
72 else {
73 print "net4 is not time consistent\n"
74 }
75  
76 # And print all information on net4
77 print $net4;
78  
79 # Compute some tripartitions
80 my %triparts=$net1->tripartitions();
81  
82 # Now these are stored
83 print $net1;
84  
85 # And can compute the tripartition error
86 print "dtr=".$net1->tripartition_error($net3)."\n";
87  
88 =head1 DESCRIPTION
89  
90 =head2 Phylogenetic Networks
91  
92 This is a module to work with phylogenetic networks. Phylogenetic networks
93 have been studied over the last years as a richer model of the evolutionary
94 history of sets of organisms than phylogenetic trees, because they take not
95 only mutation events but also recombination and horizontal gene transfer
96 events into account.
97  
98 The natural model for describing the evolutionary
99 history of a set of sequences under recombination events is a DAG, hence
100 this package relies on the package Graph::Directed to represent the
101 underlying graph of a phylogenetic network. We refer the reader to [CRV1,CRV2]
102 for formal definitions related to phylogenetic networks.
103  
104 =head2 eNewick description
105  
106 With this package, phylogenetic networks can be given by its eNewick
107 string. This description appeared in other packages related to
108 phylogenetic networks (see [PhyloNet] and [NetGen]); in fact, these two
109 packages use different descriptions. The Bio::PhyloNetwork
110 package allows both of them, but uses the second one in its output.
111  
112 The first approach [PhyloNet] goes as follows: For each hybrid node H, say with
113 parents u_1,u_2,...,u_k and children v_1,v_2,...v_l: split H in k+1 different
114 nodes; let each of the first k copies be a child of one of the u_1,...,u_k
115 (one for each) and have no children (hence we will have k extra leaves);
116 as for the last copy, let it have no parents and have v_1,...,v_l be its
117 children. This way we get a forest; each of the trees will be rooted at either
118 one root of the phylogenetic network or a hybrid node of it; the set of leaves
119 (of the whole forest) will be the set of leaves of the original network
120 together with the set of hybrid nodes (each of them repeated as many times
121 as its in-degree). Then, the eNewick representation of the phylogenetic network
122 will be the Newick representation of all the trees in the obtained forest,
123 each of them with its root labeled.
124  
125 The second approach [NetGen] goes as follows: For each hybrid node H, say with
126 parents u_1,u_2,...,u_k and children v_1,v_2,...v_l: split H in k different
127 nodes; let the first copy be a child of u_1 and have all v_1,v_2,...v_l as
128 its children; let the other copies be child of u_2,...,u_k (one for each)
129 and have no children. This way, we get a tree whose set of leaves is the
130 set of leaves of the original network together with the set of hybrid nodes
131 (possibly repeated). Then the Newick string of the obtained tree (note that
132 some internal nodes will be labeled and some leaves will be repeated) is
133 the eNewick string of the phylogenetic network.
134  
135 For example, consider the network depicted below:
136  
137 r
138 / \
139 / \
140 U V
141 / \ / \
142 1 \ / 3
143 H
144 |
145 2
146  
147 If the first approach is taken, we get the forest:
148  
149 r
150 / \
151 / \
152 U V
153 / \ / \
154 1 H H 3
155 |
156 H
157 |
158 2
159  
160 Hence, the eNewick string is '((1,H),(H,3))r; (2)H;'.
161  
162 As for the second one, one gets the tree:
163  
164 r
165 / \
166 / \
167 U V
168 / \ / \
169 1 H | 3
170 H
171 |
172 2
173  
174 Hence, the eNewick string is '((1,H),((2)H,3))r;'.
175  
176 Note: when rooting a tree, this package allows the notations
177 '(subtree,subtree,...)root' as well as 'root:(subtree,subtree,...)', but
178 the first one is used when writing eNewick strings.
179  
180 =head2 Tree-child phylogenetic networks
181  
182 Tree-child (TC) phylogenetic networks are a special class of phylogenetic
183 networks for which a distance, called mu-distance, is defined [CRV2]
184 based on certain data (mu-data) associated to every node.
185 Moreover, this distance extends the
186 Robinson-Foulds on phylogenetic trees. This package allows testing for a
187 phylogenetic network if it is TC and computes mu-distances between networks
188 over the same set of leaves.
189  
190 Moreover, the mu-data allows one to define the optimal
191 (in some precise sense) alignment between networks
192 over the same set of leaves. This package also computes this optimal alignment.
193  
194 =head2 Tripartitions
195  
196 Although tripartitions (see [CRV1] and the references therein) do not allow
197 to define distances, this package outputs tripartitions and computes a weak
198 form of the tripartition error.
199  
200 =head2 Time-consistency
201  
202 Another useful property of Phylogenetic Networks that appears in the literature
203 is that of time-consistency or real-time hybrids [BSS]. Roughly speaking, a
204 network admits a temporal representation if it can be drawn in such a way
205 that tree arcs (those whose end is a tree node) are inclined downwards, while
206 hybridization arcs (those whose end is a hybrid node) are horizontal.
207 This package checks for time-consistency and, if so, a temporal representation
208 is provided.
209  
210 =head1 AUTHOR
211  
212 Gabriel Cardona, gabriel(dot)cardona(at)uib(dot)es
213 Gabriel Valiente, valiente(at)lsi(dot)upc(dot)edu
214  
215 =head1 SEE ALSO
216  
217 =over
218  
219 =item [CRV1]
220  
221 G. Cardona, F. Rossello, G. Valiente. Tripartitions do not always
222 discriminate phylogenetic networks. arXiv:0707.2376v1 [q-bio.PE]
223  
224 =item [CRV2]
225  
226 G. Cardona, F. Rossello, G. Valiente. A Distance Measure for
227 Tree-Child Phylogenetic Networks. Preprint.
228  
229 =item [NetGen]
230  
231 M.M. Morin, and B.M.E. Moret. NetGen: generating phylogenetic networks
232 with diploid hybrids. Bioinformatics 22 (2006), 1921-1923
233  
234 =item [PhyloNet]
235  
236 PhyloNet: "Phylogenetic Networks Toolkit".
237 http://bioinfo.cs.rice.edu/phylonet
238  
239 =item [BSS]
240  
241 M. Baroni, C. Semple, and M. Steel. Hybrids in Real
242 Time. Syst. Biol. 55(1):46-56, 2006
243  
244 =back
245  
246 =head1 APPENDIX
247  
248 The rest of the documentation details each of the object methods.
249  
250 =cut
251  
252 package Bio::PhyloNetwork;
253 12 $Bio::PhyloNetwork::VERSION = '1.7.3';
254 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
255 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
256  
257 12 ▶use base qw(Bio::Root::Root);
Subroutine BEGIN: called CC=1 SCAR=0.0
258  
259 12 ▶use Bio::PhyloNetwork::muVector;
Subroutine BEGIN: called CC=1 SCAR=0.0
260 12 ▶use Graph::Directed;
Subroutine BEGIN: called CC=1 SCAR=0.0
261 12 ▶use Bio::TreeIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
262 12 ▶use Bio::Tree::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
263 12 ▶use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
264 12 ▶use Array::Compare;
Subroutine BEGIN: called CC=1 SCAR=0.0
265 12 ▶use Algorithm::Munkres;
Subroutine BEGIN: called CC=1 SCAR=0.0
266  
267 # Creator
268  
269 =head2 new
270  
271 Title : new
272 Usage : my $obj = new Bio::PhyloNetwork();
273 Function: Creates a new Bio::PhyloNetwork object
274 Returns : Bio::PhyloNetwork
275 Args : none
276 OR
277 -eNewick => string
278 OR
279 -graph => Graph::Directed object
280 OR
281 -edges => reference to an array
282 OR
283 -tree => Bio::Tree::Tree object
284 OR
285 -mudata => reference to a hash,
286 -leaves => reference to an array
287 OR
288 -mudata => reference to a hash,
289 -numleaves => integer
290  
291 Returns a Bio::PhyloNetwork object, created according to the data given:
292  
293 =over 3
294  
295 =item new()
296  
297 creates an empty network.
298  
299 =item new(-eNewick =E<gt> $str)
300  
301 creates the network whose
302 Extended Newick representation (see description above) is the string $str.
303  
304 =item new(-graph =E<gt> $graph)
305  
306 creates the network with underlying
307 graph given by the Graph::Directed object $graph
308  
309 =item new(-tree =E<gt> $tree)
310  
311 creates a network as a copy of the
312 Bio::Tree::Tree object in $tree
313  
314 =item new(-mudata =E<gt> \%mudata, -leaves =E<gt> \@leaves)
315  
316 creates the network by reconstructing it from its mu-data stored in
317 \%mudata and with set of leaves in \@leaves.
318  
319 =item new(-mudata =E<gt> \%mudata, -numleaves =E<gt> $numleaves)
320  
321 creates the network by reconstructing it from its mu-data stored in
322 \%mudata and with set of leaves in ("l1".."l$numleaves").
323  
324 =back
325  
326 =cut
327  
328 868 ▶sub new {
Subroutine new: called CC=9 SCAR=22.9
Pod documented
329 868 my ($pkg,@args)=@_;
330 868 my $self=$pkg->SUPER::new(@args);
331 868 my ($eNewick,$edgesR,$leavesR,$numleaves,$graph,$tree,$mudataR)=
332 $self->_rearrange([qw(ENEWICK
333 EDGES
334 LEAVES
335 NUMLEAVES
336 GRAPH
337 TREE
338 MUDATA)],@args);
339 868 bless($self,$pkg);
340  
341 868 ▶ $self->build_from_eNewick($eNewick) if defined $eNewick;
Branch: 1 branch
BranchTrueFalseTotal
if defined $eNewick 154 714 868
342 868 ▶ $self->build_from_edges(@$edgesR) if defined $edgesR;
Branch: 1 branch
BranchTrueFalseTotal
if defined $edgesR 0 868 868
343 868 ▶ $self->build_from_graph($graph) if defined $graph;
Branch: 1 branch
BranchTrueFalseTotal
if defined $graph 183 685 868
344 868 ▶ $self->build_from_tree($tree) if defined $tree;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tree 530 338 868
345 868 ▶ if ((! defined $leavesR) && (defined $numleaves)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $leavesR and defined $numleaves) 0 868 868
Condition66%2/3
not defined $leavesR and defined $numleaves
!ll&&!rl&&r
18670
MC/DC0%0/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
ABresult
0X0
100
111
346 0 my @leaves=map {"l$_"} (1..$numleaves);
347 0 $leavesR=\@leaves;
348 }
349 868 ▶ $self->build_from_mudata($mudataR,$leavesR)
Branch: 1 branch
BranchTrueFalseTotal
if defined $mudataR and defined $leavesR 1 867 868
Condition66%2/3
defined $mudataR and defined $leavesR
!ll&&!rl&&r
86701
MC/DC50%1/2
defined $mudataR and defined $leavesR
defined $mudataR defined $leavesR
Truth table66%2/3
A and B
A= defined $mudataRB= defined $leavesR
ABresult
0X0
100
111
350 if ((defined $mudataR) && (defined $leavesR));
351 868 return $self;
352 }
353  
354 # Builders
355  
356 0 ▶sub build_from_edges {
Subroutine build_from_edges: not called CC=2 SCAR=17.9
Pod undocumented
357 0 my ($self,@edges)=@_;
358 0 my $graph=Graph::Directed->new();
359 0 $graph->add_edges(@edges);
360 0 $self->{graph}=$graph;
361 0 $self->recompute();
362 0 my $labels={};
363 0 foreach my $node ($self->nodes()) {
364 0 $labels->{$node}=$node;
365 }
366 0 $self->{labels}=$labels;
367 }
368  
369 890 ▶sub build_from_graph {
Subroutine build_from_graph: called CC=2 SCAR=6.9
Pod undocumented
370 890 my ($self,$graph)=@_;
371 890 my $graphcp=$graph->copy();
372 890 $self->{graph}=$graphcp;
373 890 $self->recompute();
374 890 my $labels={};
375 890 foreach my $node ($self->nodes()) {
376 6802 $labels->{$node}=$node;
377 }
378 890 $self->{labels}=$labels;
379 }
380  
381 12 my $_eN_index;
382  
383 684 ▶sub build_from_eNewick {
Subroutine build_from_eNewick: called CC=2 SCAR=6.9
Pod undocumented
384 684 my ($self,$string)=@_;
385 684 $_eN_index=0;
386 684 my $graph=Graph::Directed->new();
387 684 my $labels={};
388 684 my @blocks=split(/; */,$string);
389 684 foreach my $block (@blocks) {
390 1047 my ($rt,$str)=get_root_and_subtree($block);
391 1047 my ($rtlbl,$rttype,$rtid,$rtlng)=get_label_type_id_length($rt);
392 1047 process_block($graph,$labels,$block,$rtid);
393 1047 $labels->{$rtid}=$rtlbl.'';
394 }
395 684 $self->{graph}=$graph;
396 684 $self->{labels}=$labels;
397 684 $self->recompute();
398 }
399  
400 1978 ▶sub process_block {
Subroutine process_block: called CC=9 SCAR=23.2
Pod undocumented
401 1978 my ($graph,$labels,$block,$rtid)=@_;
402 1978 my ($rt,$str)=get_root_and_subtree($block);
403 1978 my @substrs=my_split($str);
404 1978 foreach my $substr (@substrs) {
405 3440 my ($subrt,$subblock)=get_root_and_subtree($substr);
406 3440 my ($subrtlbl,$subrttype,$subrtid,$subrtlng)=
407 get_label_type_id_length($subrt);
408 3440 ▶ if (! $subrtlng eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (!$subrtlng eq '') { } 2120 1320 3440
409 2120 $graph->add_weighted_edges($rtid,$subrtid,$subrtlng);
410 }
411 else {
412 1320 $graph->add_edges($rtid,$subrtid);
413 }
414 3440 ▶ if (! $subrttype eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (!$subrttype eq "") 444 2996 3440
415 444 $graph->set_edge_attribute($rtid,$subrtid,'type',$subrttype);
416 }
417 3440 $subrtlbl.='';
418 # if (! $subrtlbl eq '') {
419 3440 ▶ if ((! defined $labels->{$subrtid})||($labels->{$subrtid} eq '')){
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $labels->{$subrtid} or $labels->{$subrtid} eq '') { } 3440 0 3440
elsif ($labels->{$subrtid} ne $subrtlbl and $subrtlbl ne '') { } 0 0 0
Condition66%2/3
not defined $labels->{$subrtid} or $labels->{$subrtid} eq ""
l!l&&r!l&&!r
32162240
MC/DC0%0/2
not defined $labels->{$subrtid} or $labels->{$subrtid} eq ""
not defined $labels->{$subrtid} $labels->{$subrtid} eq ""
Truth table66%2/3
not(A) or B
A= not defined $labels->{$subrtid}B= $labels->{$subrtid} eq ""
ABresult
1X1
011
000
420 3440 $labels->{$subrtid}=$subrtlbl;
421 ▶ } elsif (( $labels->{$subrtid} ne $subrtlbl )&&($subrtlbl ne '')) {
Condition0%0/3
$labels->{$subrtid} ne $subrtlbl and $subrtlbl ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
$labels->{$subrtid} ne $subrtlbl and $subrtlbl ne ""
$labels->{$subrtid} ne $subrtlbl $subrtlbl ne ""
Truth table0%0/3
A and B
A= $labels->{$subrtid} ne $subrtlblB= $subrtlbl ne ""
ABresult
0X0
100
111
422 # error
423 0 die("Different labels for the same node (".$labels->{$subrtid}." and $subrtlbl)");
424 }
425 # }
426 3440 ▶ if ($subblock ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($subblock ne "") 931 2509 3440
427 931 process_block($graph,$labels,$subblock,$subrtid);
428 }
429 }
430 }
431  
432 6465 ▶sub get_root_and_subtree {
Subroutine get_root_and_subtree: called CC=4 SCAR=13.9
Pod undocumented
433 6465 my ($block)=@_;
434 6465 my ($rt,$str)=("","");
435 # ($rt,$str)=split(/:|=/,$block);
436 6465 ($rt,$str)=split(/=/,$block);
437 6465 ▶ if ($rt eq $block) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rt eq $block) 6461 4 6465
438 # try to look for root label at the end
439 6461 my $pos=length($rt)-1;
440 6461 ▶ while ((substr($rt,$pos,1) ne ")") && ($pos >=0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($rt, $pos, 1) ne ")" and $pos >= 0) 53299 6461 59760
Condition100%3/3
substr($rt, $pos, 1) ne ")" and $pos >= 0
!ll&&!rl&&r
3658280353299
MC/DC100%2/2
substr($rt, $pos, 1) ne ")" and $pos >= 0
substr($rt, $pos, 1) ne ")" $pos >= 0
Truth table100%3/3
A and B
A= substr($rt, $pos, 1) ne ")"B= $pos >= 0
ABresult
0X0
100
111
441 53299 $pos--;
442 }
443 6461 $rt=substr($block,$pos+1,length($block)-$pos);
444 6461 $str=substr($block,0,$pos+1);
445 }
446 6465 $rt=trim($rt);
447 6465 $str=trim($str);
448 6465 return ($rt,$str);
449 }
450  
451 4487 ▶sub get_label_type_id_length {
Subroutine get_label_type_id_length: called CC=4 SCAR=13.9
Pod undocumented
452 4487 my ($string) = @_;
453 4487 $string.='';
454 # print "$string\n";
455 4487 ▶ if (index($string,'#')==-1) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($string, '#') == -1) { } 3827 660 4487
456 # no hybrid
457 3827 my ($label,$length)=split(':',$string);
458 3827 $label.='';
459 3827 my $id;
460 3827 ▶ if ((! defined $label) || ($label eq '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $label or $label eq '') { } 682 3145 3827
Condition66%2/3
not defined $label or $label eq ""
l!l&&r!l&&!r
06823145
MC/DC50%1/2
not defined $label or $label eq ""
not defined $label $label eq ""
Truth table66%2/3
not(A) or B
A= not defined $labelB= $label eq ""
ABresult
1X1
011
000
461 # create id
462 682 $_eN_index++;
463 682 $id="T$_eN_index";
464 } else {
465 3145 $id=$label;
466 }
467 3827 return ($label,'',$id,$length);
468 }
469 else {
470 # hybrid
471 660 my ($label,$string2)=split('#',$string);
472 660 my ($typeid,$length)=split(':',$string2);
473 660 my $type=$typeid;
474 660 $type =~ s/\d//g;
475 660 my $id=$typeid;
476 660 $id =~ s/\D//g;
477 660 return ($label,$type,'#'.$id,$length);
478 }
479 }
480  
481 sub trim
482 12930 ▶{
Subroutine trim: called CC=1 SCAR=0.0
Pod undocumented
483 12930 my ($string) = @_;
484 12930 $string =~ s/^\s+//;
485 12930 $string =~ s/\s+$//;
486 12930 return $string;
487 }
488  
489 1978 ▶sub my_split {
Subroutine my_split: called CC=7 SCAR=19.5
Pod undocumented
490 1978 my ( $string ) = @_;
491 1978 my $temp="";
492 1978 my @substrings;
493 1978 my $level=1;
494 1978 for my $i ( 1 .. length( $string ) ) {
495 79188 my $char=substr($string,$i,1);
496 79188 ▶ if ($char eq "(") {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "(") 1166 78022 79188
497 1166 $level++;
498 }
499 79188 ▶ if ($char eq ")") {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq ")") 2997 76191 79188
500 2997 ▶ if ($level==1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level == 1) 1831 1166 2997
501 1831 push @substrings, $temp;
502 1831 $temp="";
503 }
504 2997 $level--;
505 }
506 79188 ▶ if (($char eq ",") && ($level==1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "," and $level == 1) 1609 77579 79188
Condition100%3/3
$char eq "," and $level == 1
!ll&&!rl&&r
7643111481609
MC/DC100%2/2
$char eq "," and $level == 1
$char eq "," $level == 1
Truth table100%3/3
A and B
A= $char eq ","B= $level == 1
ABresult
0X0
100
111
507 1609 push @substrings, $temp;
508 1609 $temp="";
509 1609 $char="";
510 }
511 79188 $temp = $temp.$char;
512 }
513 1978 return @substrings;
514 }
515  
516 1 ▶sub build_from_mudata {
Subroutine build_from_mudata: called CC=7 SCAR=19.5
Pod undocumented
517 1 my ($self,$mus,$leavesR)=@_;
518 1 my $graph=Graph::Directed->new();
519 1 my @nodes=keys %{$mus};
520 1 my @leaves=@{$leavesR};
521  
522 1 my %seen;
523 my @internal;
524  
525 1 @seen{@leaves} = ();
526  
527 1 foreach my $node (@nodes) {
528 13 ▶ push(@internal, $node) unless exists $seen{$node};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $seen{$node} 9 4 13
529 }
530  
531 1 @internal=sort {$mus->{$b} <=> $mus->{$a} } @internal;
532 1 @nodes=(@internal,@leaves);
533 1 my $numnodes=@nodes;
534 1 ▶ for (my $i=0;$i<$numnodes;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $numnodes) 13 1 14
535 13 my $mu=$mus->{$nodes[$i]};
536 13 my $j=$i+1;
537 13 ▶ while ($mu->is_positive() && $j<$numnodes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mu->is_positive and $j < $numnodes) 78 13 91
Condition66%2/3
$mu->is_positive and $j < $numnodes
!ll&&!rl&&r
01378
MC/DC50%1/2
$mu->is_positive and $j < $numnodes
$mu->is_positive $j < $numnodes
Truth table66%2/3
A and B
A= $mu->is_positiveB= $j < $numnodes
ABresult
0X0
100
111
538 78 ▶ if ($mu->geq_poset($mus->{$nodes[$j]})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mu->geq_poset($$mus{$nodes[$j]})) 15 63 78
539 15 $graph->add_edges(($nodes[$i],$nodes[$j]));
540 15 $mu = $mu - $mus->{$nodes[$j]};
541 }
542 78 $j++;
543 }
544 }
545 1 $self->build_from_graph($graph);
546 }
547  
548 # sub relabel_tree {
549 # my ($tree)=@_;
550 # my $i=1;
551 # my $j=1;
552 # my $root=$tree->get_root_node();
553 # foreach my $node ($tree->get_nodes()) {
554 # if ($node == $root) {
555 # $node->{'_id'}="r";
556 # }
557 # elsif (! $node->is_Leaf) {
558 # $node->{'_id'}="t$i";
559 # $i++;
560 # }
561 # else {
562 # if ($node->{'_id'} eq "") {
563 # $node->{'_id'}="l$j";
564 # $j++;
565 # }
566 # }
567 # }
568 # return $tree;
569 # }
570  
571 # sub build_subtree {
572 # my ($graph,$root)=@_;
573 # foreach my $child ($root->each_Descendent) {
574 # $graph->add_edge($root->id,$child->id);
575 # $graph=build_subtree($graph,$child);
576 # }
577 # return $graph;
578 # }
579  
580 530 ▶sub build_from_tree {
Subroutine build_from_tree: called CC=1 SCAR=0.0
Pod undocumented
581 530 my ($self,$tree)=@_;
582 # relabel_tree($tree);
583 # my $treeroot=$tree->get_root_node;
584 # my $graph=Graph::Directed->new();
585 # $graph=build_subtree($graph,$treeroot);
586 # $self->build_from_graph($graph);
587 530 my $str;
588 530 my $io=IO::String->new($str);
589 530 my $treeio=Bio::TreeIO->new(-format => 'newick', -fh => $io);
590 530 $treeio->write_tree($tree);
591 # print "intern: $str\n";
592 530 $self->build_from_eNewick($str);
593 }
594  
595 1574 ▶sub recompute {
Subroutine recompute: called CC=4 SCAR=13.9
Pod undocumented
596 1574 my ($self)=@_;
597 $self->throw("Graph is not DAG:".$self->{graph})
598 1574 ▶ unless $self->{graph}->is_dag();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"graph"}->is_dag 0 1574 1574
599 1574 my @leaves=$self->{graph}->successorless_vertices();
600 1574 @leaves=sort @leaves;
601 1574 my $numleaves=@leaves;
602 1574 my @roots=$self->{graph}->predecessorless_vertices();
603 1574 my $numroots=@roots;
604 #$self->throw("Graph is not rooted") unless ($numroots == 1);
605 1574 my @nodes=$self->{graph}->vertices();
606 1574 @nodes=sort @nodes;
607 1574 my $numnodes=@nodes;
608 1574 foreach my $node (@nodes) {
609 10704 ▶ if (! defined $self->{labels}->{$node}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"labels"}{$node}) 2642 8062 10704
610 2642 $self->{labels}->{$node}='';
611 }
612 }
613 1574 $self->{leaves}=\@leaves;
614 1574 $self->{numleaves}=$numleaves;
615 1574 $self->{roots}=\@roots;
616 1574 $self->{numroots}=$numroots;
617 1574 $self->{nodes}=\@nodes;
618 1574 $self->{numnodes}=$numnodes;
619 1574 $self->{mudata}={};
620 1574 $self->{h}={};
621 1574 $self->compute_height();
622 1574 $self->compute_mu();
623 1574 return $self;
624 }
625  
626 # Hybridizing
627  
628 3405 ▶sub is_attackable {
Subroutine is_attackable: called CC=8 SCAR=20.8
Pod undocumented
629 3405 my ($self,$u1,$v1,$u2,$v2)=@_;
630 3405 ▶ if ( $self->is_hybrid_node($v1) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_hybrid_node($v1) or $self->is_hybrid_node($v2) or $self->graph->is_reachable($v2, $u1) or $u1 eq $u2 and $v1 eq $v2 or not scalar grep({$_ ne $v2 and $self->is_tree_node($_);} $self->graph->successors($u2))) 2699 706 3405
Condition100%15/15
$self->is_hybrid_node($v1) or $self->is_hybrid_node($v2) or $self->graph->is_reachable($v2, $u1) or $u1 eq $u2 and $v1 eq $v2 or not scalar grep({$_ ne $v2 and $self->is_tree_node($_);} $self->graph->successors($u2))
l!l&&r!l&&!r
2199500706
$self->is_hybrid_node($v1) or $self->is_hybrid_node($v2) or $self->graph->is_reachable($v2, $u1) or $u1 eq $u2 and $v1 eq $v2
l!l&&r!l&&!r
17684311206
$self->is_hybrid_node($v1) or $self->is_hybrid_node($v2) or $self->graph->is_reachable($v2, $u1)
l!l&&r!l&&!r
13873811637
$self->is_hybrid_node($v1) or $self->is_hybrid_node($v2)
l!l&&r!l&&!r
8065812018
$u1 eq $u2 and $v1 eq $v2
!ll&&!rl&&r
947259431
MC/DC100%6/6
$self->is_hybrid_node($v1) or $self->is_hybrid_node($v2) or $self->graph->is_reachable($v2, $u1) or $u1 eq $u2 and $v1 eq $v2 or not scalar grep({$_ ne $v2 and $self->is_tree_node($_);} $self->graph->successors($u2))
$self->is_hybrid_node($v1) $self->is_hybrid_node($v2) $self->graph->is_reachable($v2, $u1) $u1 eq $u2 $v1 eq $v2 not scalar grep({$_ ne $v2 and $self->is_tree_node($_);} $self->graph->successors($u2))
Truth table87%7/8
A or B or C or D and E or not(F)
A= $self->is_hybrid_node($v1)B= $self->is_hybrid_node($v2)C= $self->graph->is_reachable($v2, $u1)D= $u1 eq $u2E= $v1 eq $v2F= not scalar grep({$_ ne $v2 and $self->is_tree_node($_);} $self->graph->successors($u2))
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
00011X1
0000X11
0001011
0000X00
0001000
631 $self->is_hybrid_node($v2) ||
632 $self->graph->is_reachable($v2,$u1) ||
633 (($u1 eq $u2)&&($v1 eq $v2)) ||
634 2210 ▶ (! scalar grep {($_ ne $v2) && ($self->is_tree_node($_))}
Condition100%3/3
$_ ne $v2 && $self->is_tree_node($_)
!ll&&!rl&&r
1206298706
MC/DC100%2/2
$_ ne $v2 && $self->is_tree_node($_)
$_ ne $v2 $self->is_tree_node($_)
Truth table100%3/3
A && B
A= $_ ne $v2B= $self->is_tree_node($_)
ABresult
0X0
100
111
635 $self->graph->successors($u2)))
636 {
637 2699 return 0;
638 }
639 706 return 1;
640 }
641  
642 706 ▶sub do_attack {
Subroutine do_attack: called CC=1 SCAR=0.0
Pod undocumented
643 706 my ($self,$u1,$v1,$u2,$v2,$lbl)=@_;
644 706 my $graph=$self->{graph};
645 706 $graph->delete_edge($u1,$v1);
646 706 $graph->delete_edge($u2,$v2);
647 706 $graph->add_edge($u1,"T$lbl");
648 706 $graph->add_edge("T$lbl",$v1);
649 706 $graph->add_edge($u2,"#H$lbl");
650 706 $graph->add_edge("#H$lbl",$v2);
651 706 $graph->add_edge("T$lbl","#H$lbl");
652 706 $self->build_from_graph($graph);
653 }
654  
655  
656 # Computation of mu-data
657  
658 1574 ▶sub compute_mu {
Subroutine compute_mu: called CC=5 SCAR=16.1
Pod undocumented
659 1574 my ($self)=@_;
660 1574 my $graph=$self->{graph};
661 1574 my $mudata=$self->{mudata};
662 1574 my @leaves=@{$self->{leaves}};
663 1574 my $numleaves=$self->{numleaves};
664 1574 ▶ for (my $i=0;$i<$numleaves;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $numleaves) 4759 1574 6333
665 4759 my $vec=Bio::PhyloNetwork::muVector->new($numleaves);
666 4759 $vec->[$i]=1;
667 4759 $mudata->{$leaves[$i]}=$vec;
668 }
669 1574 my $h=1;
670 1574 ▶ while (my @nodes=grep {$self->{h}->{$_} == $h} @{$self->{nodes}} )
Branch: 1 branch
BranchTrueFalseTotal
if (my(@nodes) = grep({$self->{"h"}{$_} == $h;} @{$$self{"nodes"};})) 5549 1574 7123
671 {
672 5549 foreach my $u (@nodes) {
673 5945 my $vec=Bio::PhyloNetwork::muVector->new($numleaves);
674 5945 foreach my $son ($graph->successors($u)) {
675 10510 $vec+=$mudata->{$son};
676 }
677 5945 $mudata->{$u}=$vec;
678 }
679 5549 $h++;
680 }
681 }
682  
683 1574 ▶sub compute_height {
Subroutine compute_height: called CC=6 SCAR=17.9
Pod undocumented
684 1574 my ($self)=@_;
685 1574 my $graph=$self->{graph};
686 1574 my @leaves=@{$self->{leaves}};
687 1574 foreach my $leaf (@leaves) {
688 4759 $self->{h}->{$leaf}=0;
689 }
690 1574 my $h=0;
691 1574 ▶ while (my @nodes=grep {(defined $self->{h}->{$_})&&($self->{h}->{$_} == $h)}
Branch: 1 branch
BranchTrueFalseTotal
if (my(@nodes) = grep({defined $self->{"h"}{$_} and $self->{"h"}{$_} == $h;} @{$$self{"nodes"};})) 7123 1574 8697
Condition100%3/3
defined $self->{'h'}{$_} && $self->{'h'}{$_} == $h
!ll&&!rl&&r
80513787016736
MC/DC100%2/2
defined $self->{'h'}{$_} && $self->{'h'}{$_} == $h
defined $self->{'h'}{$_} $self->{'h'}{$_} == $h
Truth table100%3/3
A && B
A= defined $self->{'h'}{$_}B= $self->{'h'}{$_} == $h
ABresult
0X0
100
111
692 8697 @{$self->{nodes}} )
693 {
694 7123 foreach my $node (@nodes) {
695 16736 foreach my $parent ($graph->predecessors($node)) {
696 13972 $self->{h}->{$parent}=$h+1;
697 }
698 }
699 7123 $h++;
700 }
701 }
702  
703 # Tests
704  
705 =head2 is_leaf
706  
707 Title : is_leaf
708 Usage : my $b=$net->is_leaf($u)
709 Function: tests if $u is a leaf in $net
710 Returns : boolean
711 Args : scalar
712  
713 =cut
714  
715 7184 ▶sub is_leaf {
Subroutine is_leaf: called CC=2 SCAR=6.9
Pod documented
716 7184 my ($self,$node)=@_;
717 7184 ▶ if ($self->{graph}->out_degree($node) == 0) {return 1;}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"graph"}->out_degree($node) == 0) 2797 4387 7184
718 4387 return 0;
719 }
720  
721 =head2 is_root
722  
723 Title : is_root
724 Usage : my $b=$net->is_root($u)
725 Function: tests if $u is the root of $net
726 Returns : boolean
727 Args : scalar
728  
729 =cut
730  
731 1 ▶sub is_root {
Subroutine is_root: called CC=2 SCAR=7.6
Pod documented
732 1 my ($self,$node)=@_;
733 1 ▶ if ($self->{graph}->in_degree($node) == 0) {return 1;}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"graph"}->in_degree($node) == 0) 1 0 1
734 0 return 0;
735 }
736  
737 =head2 is_tree_node
738  
739 Title : is_tree_node
740 Usage : my $b=$net->is_tree_node($u)
741 Function: tests if $u is a tree node in $net
742 Returns : boolean
743 Args : scalar
744  
745 =cut
746  
747 1250 ▶sub is_tree_node {
Subroutine is_tree_node: called CC=2 SCAR=6.9
Pod documented
748 1250 my ($self,$node)=@_;
749 1250 ▶ if ($self->{graph}->in_degree($node) <= 1) {return 1;}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"graph"}->in_degree($node) <= 1) 898 352 1250
750 352 return 0;
751 }
752  
753 =head2 is_hybrid_node
754  
755 Title : is_hybrid_node
756 Usage : my $b=$net->is_hybrid_node($u)
757 Function: tests if $u is a hybrid node in $net
758 Returns : boolean
759 Args : scalar
760  
761 =cut
762  
763 15855 ▶sub is_hybrid_node {
Subroutine is_hybrid_node: called CC=2 SCAR=6.9
Pod documented
764 15855 my ($self,$node)=@_;
765 15855 ▶ if ($self->{graph}->in_degree($node) > 1) {return 1;}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"graph"}->in_degree($node) > 1) 2990 12865 15855
766 12865 return 0;
767 }
768  
769 1505 ▶sub has_tree_child {
Subroutine has_tree_child: called CC=3 SCAR=11.0
Pod undocumented
770 # has_tree_child(g,u) returns 1 if u has a tree child in graph g
771 # and 0 otherwise
772 1505 my $g=shift(@_);
773 1505 my $node=shift(@_);
774 1505 my @Sons=$g->successors($node);
775 1505 foreach my $son (@Sons) {
776 1930 ▶ if ($g->in_degree($son)==1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g->in_degree($son) == 1) 1505 425 1930
777 1505 return 1;
778 }
779 }
780 0 return 0;
781 }
782  
783 =head2 is_tree_child
784  
785 Title : is_tree_child
786 Usage : my $b=$net->is_tree_child()
787 Function: tests if $net is a Tree-Child phylogenetic network
788 Returns : boolean
789 Args : Bio::PhyloNetwork
790  
791 =cut
792  
793 9095 ▶sub is_tree_child {
Subroutine is_tree_child: called CC=5 SCAR=16.2
Pod documented
794 9095 my ($self)=@_;
795 9095 ▶ if (defined $self->{is_tree_child}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"is_tree_child"}) 8794 301 9095
796 8794 return $self->{is_tree_child};
797 }
798 301 $self->{is_tree_child}=0;
799 301 my $graph=$self->{graph};
800 301 foreach my $node (@{$self->{nodes}}) {
801 2443 ▶ return 0 unless ($graph->out_degree($node)==0 ||
Branch: 1 branch
BranchTrueFalseTotal
unless $graph->out_degree($node) == 0 or has_tree_child($graph, $node) 0 2443 2443
Condition66%2/3
$graph->out_degree($node) == 0 or has_tree_child($graph, $node)
l!l&&r!l&&!r
93815050
MC/DC0%0/2
$graph->out_degree($node) == 0 or has_tree_child($graph, $node)
$graph->out_degree($node) == 0 has_tree_child($graph, $node)
Truth table66%2/3
A or B
A= $graph->out_degree($node) == 0B= has_tree_child($graph, $node)
ABresult
1X1
011
000
802 has_tree_child($graph,$node));
803 }
804 301 $self->{is_tree_child}=1;
805 301 return 1;
806 }
807  
808 # Accessors
809  
810 =head2 nodes
811  
812 Title : nodes
813 Usage : my @nodes=$net->nodes()
814 Function: returns the set of nodes of $net
815 Returns : array
816 Args : none
817  
818 =cut
819  
820 11788 ▶sub nodes {
Subroutine nodes: called CC=1 SCAR=0.0
Pod documented
821 11788 my ($self)=@_;
822 11788 return @{$self->{nodes}};
823 }
824  
825 =head2 leaves
826  
827 Title : leaves
828 Usage : my @leaves=$net->leaves()
829 Function: returns the set of leaves of $net
830 Returns : array
831 Args : none
832  
833 =cut
834  
835 109 ▶sub leaves {
Subroutine leaves: called CC=1 SCAR=0.0
Pod documented
836 109 my ($self)=@_;
837 109 return @{$self->{leaves}};
838 }
839  
840 =head2 roots
841  
842 Title : roots
843 Usage : my @roots=$net->roots()
844 Function: returns the set of roots of $net
845 Returns : array
846 Args : none
847  
848 =cut
849  
850 226 ▶sub roots {
Subroutine roots: called CC=1 SCAR=0.0
Pod documented
851 226 my ($self)=@_;
852 226 return @{$self->{roots}};
853 }
854  
855 =head2 internal_nodes
856  
857 Title : internal_nodes
858 Usage : my @internal_nodes=$net->internal_nodes()
859 Function: returns the set of internal nodes of $net
860 Returns : array
861 Args : none
862  
863 =cut
864  
865 691 ▶sub internal_nodes {
Subroutine internal_nodes: called CC=1 SCAR=0.0
Pod documented
866 691 my ($self)=@_;
867 691 return grep {! $self->is_leaf($_)} $self->nodes();
868 }
869  
870 =head2 tree_nodes
871  
872 Title : tree_nodes
873 Usage : my @tree_nodes=$net->tree_nodes()
874 Function: returns the set of tree nodes of $net
875 Returns : array
876 Args : none
877  
878 =cut
879  
880 4 ▶sub tree_nodes {
Subroutine tree_nodes: called CC=1 SCAR=0.0
Pod documented
881 4 my ($self)=@_;
882 4 return grep {$self->is_tree_node($_)} $self->nodes();
883 }
884  
885 =head2 hybrid_nodes
886  
887 Title : hybrid_nodes
888 Usage : my @hybrid_nodes=$net->hybrid_nodes()
889 Function: returns the set of hybrid nodes of $net
890 Returns : array
891 Args : none
892  
893 =cut
894  
895 1095 ▶sub hybrid_nodes {
Subroutine hybrid_nodes: called CC=1 SCAR=0.0
Pod documented
896 1095 my ($self)=@_;
897 1095 return grep {$self->is_hybrid_node($_)} $self->nodes();
898 }
899  
900 =head2 graph
901  
902 Title : graph
903 Usage : my $graph=$net->graph()
904 Function: returns the underlying graph of $net
905 Returns : Graph::Directed
906 Args : none
907  
908 =cut
909  
910 5718 ▶sub graph {
Subroutine graph: called CC=1 SCAR=0.0
Pod documented
911 5718 my ($self)=@_;
912 5718 return $self->{graph};
913 }
914  
915 =head2 edges
916  
917 Title : edges
918 Usage : my @edges=$net->edges()
919 Function: returns the set of edges of $net
920 Returns : array
921 Args : none
922  
923 Each element in the array is an anonimous array whose first element is the
924 head of the edge and the second one is the tail.
925  
926 =cut
927  
928 36 ▶sub edges {
Subroutine edges: called CC=1 SCAR=0.0
Pod documented
929 36 my ($self)=@_;
930 36 return $self->{graph}->edges();
931 }
932  
933 =head2 tree_edges
934  
935 Title : tree_edges
936 Usage : my @tree_edges=$net->tree_edges()
937 Function: returns the set of tree edges of $net
938 (those whose tail is a tree node)
939 Returns : array
940 Args : none
941  
942 =cut
943  
944 4 ▶sub tree_edges {
Subroutine tree_edges: called CC=1 SCAR=0.0
Pod documented
945 4 my ($self)=@_;
946 4 return grep {$self->is_tree_node($_->[1])} $self->edges();
947 }
948  
949 =head2 hybrid_edges
950  
951 Title : hybrid_edges
952 Usage : my @hybrid_edges=$net->hybrid_edges()
953 Function: returns the set of hybrid edges of $net
954 (those whose tail is a hybrid node)
955 Returns : array
956 Args : none
957  
958 =cut
959  
960 4 ▶sub hybrid_edges {
Subroutine hybrid_edges: called CC=1 SCAR=0.0
Pod documented
961 4 my ($self)=@_;
962 4 return grep {$self->is_hybrid_node($_->[1])} $self->edges();
963 }
964  
965 =head2 explode
966  
967 Title : explode
968 Usage : my @trees=$net->explode()
969 Function: returns the representation of $net by a set of
970 Bio::Tree:Tree objects
971 Returns : array
972 Args : none
973  
974 =cut
975  
976 1 ▶sub explode {
Subroutine explode: called CC=1 SCAR=0.0
Pod documented
977 1 my ($self)=@_;
978 1 my @trees;
979 1 $self->explode_rec(\@trees);
980 1 return @trees;
981 }
982  
983 15 ▶sub explode_rec {
Subroutine explode_rec: called CC=3 SCAR=11.0
Pod undocumented
984 15 my ($self,$trees)=@_;
985 15 my @h = $self->hybrid_nodes;
986 15 ▶ if (scalar @h) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @h) { } 7 8 15
987 7 my $v = shift @h;
988 7 for my $u ($self->{graph}->predecessors($v)) {
989 14 $self->{graph}->delete_edge($u,$v);
990 14 $self->explode_rec($trees);
991 14 $self->{graph}->add_edge($u,$v);
992 }
993 } else {
994 8 my $io = IO::String->new($self->eNewick);
995 8 my $treeio = Bio::TreeIO->new(-format => 'newick', -fh => $io);
996 8 my $tree = $treeio->next_tree;
997 8 $tree->contract_linear_paths;
998 8 push @{$trees}, $tree;
999 }
1000 }
1001  
1002 =head2 mudata
1003  
1004 Title : mudata
1005 Usage : my %mudata=$net->mudata()
1006 Function: returns the representation of $net by its mu-data
1007 Returns : hash
1008 Args : none
1009  
1010 $net-E<gt>mudata() returns a hash with keys the nodes of $net and each value is a
1011 muVector object holding its mu-vector.
1012  
1013 =cut
1014  
1015 1 ▶sub mudata {
Subroutine mudata: called CC=1 SCAR=0.0
Pod documented
1016 1 my ($self)=@_;
1017 1 return %{$self->{mudata}};
1018 }
1019  
1020 0 ▶sub mudata_node {
Subroutine mudata_node: not called CC=1 SCAR=6.9
Pod undocumented
1021 0 my ($self,$u)=@_;
1022 0 return $self->{mudata}{$u};
1023 }
1024  
1025 =head2 heights
1026  
1027 Title : heights
1028 Usage : my %heights=$net->heights()
1029 Function: returns the heights of the nodes of $net
1030 Returns : hash
1031 Args : none
1032  
1033 $net-E<gt>heights() returns a hash with keys the nodes of $net and each value
1034 is its height.
1035  
1036 =cut
1037  
1038 1 ▶sub heights {
Subroutine heights: called CC=1 SCAR=0.0
Pod documented
1039 1 my ($self)=@_;
1040 1 return %{$self->{h}};
1041 }
1042  
1043 0 ▶sub height_node {
Subroutine height_node: not called CC=1 SCAR=6.9
Pod undocumented
1044 0 my ($self,$u)=@_;
1045 0 return $self->{h}{$u};
1046 }
1047  
1048 =head2 mu_distance
1049  
1050 Title : mu_distance
1051 Usage : my $dist=$net1->mu_distance($net2)
1052 Function: Computes the mu-distance between the networks $net1 and $net2 on
1053 the same set of leaves
1054 Returns : scalar
1055 Args : Bio::PhyloNetwork
1056  
1057 =cut
1058  
1059 4546 ▶sub mu_distance {
Subroutine mu_distance: called CC=9 SCAR=22.2
Pod documented
1060 4546 my ($net1,$net2)=@_;
1061 4546 my @nodes1=$net1->nodes;
1062 4546 my @nodes2=$net2->nodes;
1063 4546 my $comp = Array::Compare->new;
1064 $net1->throw("Cannot compare phylogenetic networks on different set of leaves")
1065 4546 ▶ unless $comp->compare($net1->{leaves},$net2->{leaves});
Branch: 1 branch
BranchTrueFalseTotal
unless $comp->compare($net1->{"leaves"}, $net2->{"leaves"}) 0 4546 4546
1066 4546 ▶ $net1->warn("Not a tree-child phylogenetic network")
Branch: 1 branch
BranchTrueFalseTotal
unless $net1->is_tree_child 0 4546 4546
1067 unless $net1->is_tree_child();
1068 4546 ▶ $net2->warn("Not a tree-child phylogenetic network")
Branch: 1 branch
BranchTrueFalseTotal
unless $net2->is_tree_child 0 4546 4546
1069 unless $net2->is_tree_child();
1070 4546 my @leaves=@{$net1->{leaves}};
1071 4546 my %matched1;
1072 my %matched2;
1073 4546 OUTER: foreach my $node1 (@nodes1) {
1074 36890 foreach my $node2 (@nodes2) {
1075 230165 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $matched1{$node1} || exists $matched2{$node2} and $net1->{"mudata"}{$node1} == $net2->{"mudata"}{$node2}) 21218 208947 230165
Condition83%5/6
not exists $matched1{$node1} || exists $matched2{$node2} and $net1->{"mudata"}{$node1} == $net2->{"mudata"}{$node2}
!ll&&!rl&&r
6511114383621218
exists $matched1{$node1} || exists $matched2{$node2}
l!l&&r!l&&!r
065111165054
MC/DC66%2/3
not exists $matched1{$node1} || exists $matched2{$node2} and $net1->{"mudata"}{$node1} == $net2->{"mudata"}{$node2}
exists $matched1{$node1} exists $matched2{$node2} $net1->{"mudata"}{$node1} == $net2->{"mudata"}{$node2}
Truth table75%3/4
not(A || B) and C
A= exists $matched1{$node1}B= exists $matched2{$node2}C= $net1->{"mudata"}{$node1} == $net2->{"mudata"}{$node2}
ABCresult
1XX0
01X0
0000
0011
1076 (! exists $matched1{$node1}) && (! exists $matched2{$node2}) &&
1077 ($net1->{mudata}{$node1} == $net2->{mudata}{$node2})
1078 ) {
1079 21218 $matched1{$node1}=$node2;
1080 21218 $matched2{$node2}=$node1;
1081 21218 next OUTER;
1082 }
1083 }
1084 }
1085 4546 return (scalar @nodes1)+(scalar @nodes2)-2*(scalar keys %matched1);
1086 }
1087  
1088 =head2 mu_distance_generalized
1089  
1090 Title : mu_distance_generalized
1091 Usage : my $dist=$net1->mu_distance($net2)
1092 Function: Computes the mu-distance between the topological restrictions of
1093 networks $net1 and $net2 on its common set of leaves
1094 Returns : scalar
1095 Args : Bio::PhyloNetwork
1096  
1097 =cut
1098  
1099 0 ▶sub mu_distance_generalized {
Subroutine mu_distance_generalized: not called CC=1 SCAR=6.9
Pod documented
1100 0 my ($net1,$net2)=@_;
1101 0 my ($netr1,$netr2)=$net1->topological_restriction($net2);
1102 0 return $netr1->mu_distance($netr2);
1103 }
1104  
1105 # mudata_string (code mu_data in a string; useful for isomorphism testing)
1106  
1107 3036 ▶sub mudata_string_node {
Subroutine mudata_string_node: called CC=1 SCAR=0.0
Pod undocumented
1108 3036 my ($self,$u)=@_;
1109 3036 return $self->{mudata}->{$u}->display();
1110 }
1111  
1112 28978 ▶sub mudata_string {
Subroutine mudata_string: called CC=3 SCAR=11.0
Pod undocumented
1113 28978 my ($self)=@_;
1114 28978 ▶ return $self->{mudata_string} if defined $self->{mudata_string};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"mudata_string"} 28292 686 28978
1115 686 my @internal=$self->internal_nodes;
1116 686 my $mus=$self->{mudata};
1117 686 @internal=sort {$mus->{$b} <=> $mus->{$a} } @internal;
1118 686 my $str="";
1119 686 foreach my $node (@internal) {
1120 3036 $str=$str.$self->mudata_string_node($node);
1121 }
1122 686 $self->{mudata_string}=$str;
1123 686 return $str;
1124 }
1125  
1126 14489 ▶sub is_mu_isomorphic {
Subroutine is_mu_isomorphic: called CC=1 SCAR=0.0
Pod undocumented
1127 14489 my ($net1,$net2)=@_;
1128 14489 return ($net1->mudata_string() eq $net2->mudata_string());
1129 }
1130  
1131 # tripartitions
1132  
1133 12 ▶sub compute_tripartition_node {
Subroutine compute_tripartition_node: called CC=5 SCAR=16.1
Pod undocumented
1134 12 my ($self,$u)=@_;
1135 $self->warn("Cannot compute tripartitions on unrooted networks. Will assume one at random")
1136 12 ▶ unless ($self->{numroots} == 1);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"numroots"} == 1 0 12 12
1137 12 my $root=$self->{roots}->[0];
1138 12 my $graph=$self->{graph};
1139 12 my $graphPruned=$graph->copy();
1140 12 $graphPruned->delete_vertex($u);
1141 12 my $tripartition="";
1142 12 foreach my $leaf (@{$self->{leaves}}) {
1143 36 my $type;
1144 36 ▶ if ($graph->is_reachable($u,$leaf)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($graph->is_reachable($u, $leaf)) { } 19 17 36
1145 19 ▶ if ($graphPruned->is_reachable($root,$leaf)) {$type="B";}
Branch: 1 branch
BranchTrueFalseTotal
if ($graphPruned->is_reachable($root, $leaf)) { } 2 17 19
1146 17 else {$type="A";}
1147 }
1148 17 else {$type="C";}
1149 36 $tripartition .= $type;
1150 }
1151 12 $self->{tripartitions}->{$u}=$tripartition;
1152 }
1153  
1154 2 ▶sub compute_tripartitions {
Subroutine compute_tripartitions: called CC=2 SCAR=6.9
Pod undocumented
1155 2 my ($self)=@_;
1156 2 foreach my $node (@{$self->{nodes}}) {
1157 12 $self->compute_tripartition_node($node);
1158 }
1159 }
1160  
1161 =head2 tripartitions
1162  
1163 Title : tripartitions
1164 Usage : my %tripartitions=$net->tripartitions()
1165 Function: returns the set of tripartitions of $net
1166 Returns : hash
1167 Args : none
1168  
1169 $net-E<gt>tripartitions() returns a hash with keys the nodes of $net and each value
1170 is a string representing the tripartition of the leaves induced by the node.
1171 A string "BCA..." associated with a node u (e.g.) means, the first leaf is in
1172 the set B(u), the second one in C(u), the third one in A(u), and so on.
1173  
1174 =cut
1175  
1176 1 ▶sub tripartitions {
Subroutine tripartitions: called CC=2 SCAR=7.0
Pod documented
1177 1 my ($self)=@_;
1178 1 ▶ $self->compute_tripartitions() unless defined $self->{tripartitions};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"tripartitions"} 1 0 1
1179 1 return %{$self->{tripartitions}};
1180 }
1181  
1182 # to do: change to tri_distance and test for TC and time-cons
1183  
1184 1 ▶sub tripartition_error {
Subroutine tripartition_error: called CC=16 SCAR=28.1
Pod undocumented
1185 1 my ($net1,$net2)=@_;
1186 1 my $comp = Array::Compare->new;
1187 $net1->throw("Cannot compare phylogenetic networks on different set of leaves")
1188 1 ▶ unless $comp->compare($net1->{leaves},$net2->{leaves});
Branch: 1 branch
BranchTrueFalseTotal
unless $comp->compare($net1->{"leaves"}, $net2->{"leaves"}) 0 1 1
1189 1 ▶ $net1->warn("Not a tree-child phylogenetic network")
Branch: 1 branch
BranchTrueFalseTotal
unless $net1->is_tree_child 0 1 1
1190 unless $net1->is_tree_child();
1191 1 ▶ $net2->warn("Not a tree-child phylogenetic network")
Branch: 1 branch
BranchTrueFalseTotal
unless $net2->is_tree_child 0 1 1
1192 unless $net2->is_tree_child();
1193 1 ▶ $net1->warn("Not a time-consistent network")
Branch: 1 branch
BranchTrueFalseTotal
unless $net1->is_time_consistent 0 1 1
1194 unless $net1->is_time_consistent();
1195 1 ▶ $net2->warn("Not a time-consistent network")
Branch: 1 branch
BranchTrueFalseTotal
unless $net2->is_time_consistent 0 1 1
1196 unless $net2->is_time_consistent();
1197 1 ▶ $net1->compute_tripartitions() unless defined $net1->{tripartitions};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $net1->{"tripartitions"} 0 1 1
1198 1 ▶ $net2->compute_tripartitions() unless defined $net2->{tripartitions};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $net2->{"tripartitions"} 1 0 1
1199 1 my @edges1=$net1->{graph}->edges();
1200 1 my @edges2=$net2->{graph}->edges();
1201 1 my ($FN,$FP)=(0,0);
1202 1 foreach my $edge1 (@edges1) {
1203 7 my $matched=0;
1204 7 foreach my $edge2 (@edges2) {
1205 22 ▶ if ($net1->{tripartitions}->{$edge1->[1]} eq
Branch: 1 branch
BranchTrueFalseTotal
if ($net1->{"tripartitions"}{$edge1->[1]} eq $net2->{"tripartitions"}{$edge2->[1]}) 5 17 22
1206 $net2->{tripartitions}->{$edge2->[1]}) {
1207 5 $matched=1;
1208 5 last;
1209 }
1210 }
1211 7 ▶ if (! $matched) {$FN++;}
Branch: 1 branch
BranchTrueFalseTotal
unless ($matched) 2 5 7
1212 }
1213 1 foreach my $edge2 (@edges2) {
1214 4 my $matched=0;
1215 4 foreach my $edge1 (@edges1) {
1216 18 ▶ if ($net1->{tripartitions}->{$edge1->[1]} eq
Branch: 1 branch
BranchTrueFalseTotal
if ($net1->{"tripartitions"}{$edge1->[1]} eq $net2->{"tripartitions"}{$edge2->[1]}) 3 15 18
1217 $net2->{tripartitions}->{$edge2->[1]}) {
1218 3 $matched=1;
1219 3 last;
1220 }
1221 }
1222 4 ▶ if (! $matched) {$FP++;}
Branch: 1 branch
BranchTrueFalseTotal
unless ($matched) 1 3 4
1223 }
1224 1 return ($FN/(scalar @edges1)+$FP/(scalar @edges2))/2;
1225 }
1226  
1227 # Time-consistency
1228  
1229 # to do: add weak time consistency
1230  
1231 =head2 is_time_consistent
1232  
1233 Title : is_time_consistent
1234 Usage : my $b=$net->is_time_consistent()
1235 Function: tests if $net is (strong) time-consistent
1236 Returns : boolean
1237 Args : none
1238  
1239 =cut
1240  
1241 4 ▶sub is_time_consistent {
Subroutine is_time_consistent: called CC=2 SCAR=6.9
Pod documented
1242 4 my ($self)=@_;
1243 $self->compute_temporal_representation()
1244 4 ▶ unless exists $self->{has_temporal_representation};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"has_temporal_representation"} 3 1 4
1245 4 return $self->{has_temporal_representation};
1246 }
1247  
1248 =head2 temporal_representation
1249  
1250 Title : temporal_representation
1251 Usage : my %time=$net->temporal_representation()
1252 Function: returns a hash containing a temporal representation of $net, or 0
1253 if $net is not time-consistent
1254 Returns : hash
1255 Args : none
1256  
1257 =cut
1258  
1259 1 ▶sub temporal_representation {
Subroutine temporal_representation: called CC=2 SCAR=7.4
Pod documented
1260 1 my ($self)=@_;
1261 1 ▶ if ($self->is_time_consistent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_time_consistent) 1 0 1
1262 1 return %{$self->{temporal_representation}};
1263 }
1264 0 return 0;
1265 }
1266  
1267 3 ▶sub compute_temporal_representation {
Subroutine compute_temporal_representation: called CC=6 SCAR=17.9
Pod undocumented
1268 3 my ($self)=@_;
1269 3 my $quotient=Graph::Directed->new();
1270 3 my $classes=find_classes($self);
1271 3 my %repr;
1272 3 map {$repr{$_}=$classes->{$_}[0]} $self->nodes();
1273 3 foreach my $e ($self->tree_edges()) {
1274 14 $quotient->add_edge($repr{$e->[0]},$repr{$e->[1]});
1275 }
1276 3 my %temp;
1277 3 my $depth=0;
1278 3 ▶ while ($quotient->vertices()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quotient->vertices) 9 3 12
1279 9 ▶ if (my @svs=$quotient->predecessorless_vertices()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@svs) = $quotient->predecessorless_vertices) { } 9 0 9
1280 9 foreach my $sv (@svs) {
1281 15 $temp{$sv}=$depth;
1282 }
1283 9 $quotient->delete_vertices(@svs);
1284 } else {
1285 0 return 0;
1286 }
1287 9 $depth++;
1288 }
1289 3 foreach my $node (@{$self->{nodes}}) {
1290 19 $temp{$node}=$temp{$repr{$node}}
1291 }
1292 3 $self->{temporal_representation}=\%temp;
1293 3 $self->{has_temporal_representation}=1;
1294 }
1295  
1296 3 ▶sub find_classes {
Subroutine find_classes: called CC=2 SCAR=6.9
Pod undocumented
1297 3 my ($self)=@_;
1298 3 my $classes={};
1299 3 map {$classes->{$_}=[$_]} $self->nodes();
1300 3 foreach my $e ($self->hybrid_edges()) {
1301 4 $classes=join_classes($classes,$e->[0],$e->[1]);
1302 }
1303 3 return $classes;
1304 }
1305  
1306 4 ▶sub join_classes {
Subroutine join_classes: called CC=1 SCAR=0.0
Pod undocumented
1307 4 my ($classes,$u,$v)=@_;
1308 4 my @clu=@{$classes->{$u}};
1309 4 my @clv=@{$classes->{$v}};
1310 4 my @cljoin=(@clu,@clv);
1311 4 map {$classes->{$_}=\@cljoin} @cljoin;
1312 4 return $classes;
1313 }
1314  
1315 # alignment
1316  
1317 =head2 contract_elementary
1318  
1319  
1320 Title : contract_elementary
1321 Usage : my ($contracted,$blocks)=$net->contract_elementary();
1322 Function: Returns the network $contracted, obtained by contracting elementary
1323 paths of $net into edges. The reference $blocks points to a hash
1324 where, for each node of $contracted, gives the corresponding nodes
1325 of $net that have been deleted.
1326 Returns : Bio::PhyloNetwork,reference to hash
1327 Args : none
1328  
1329 =cut
1330  
1331 4 ▶sub contract_elementary {
Subroutine contract_elementary: called CC=5 SCAR=22.8
Pod documented
1332 4 my ($self)=@_;
1333  
1334 4 my $contracted=$self->graph->copy();
1335 4 my @nodes=$self->nodes();
1336 4 my $mus=$self->{mudata};
1337 4 my $hs=$self->{h};
1338 4 my %blocks;
1339 4 foreach my $u (@nodes) {
1340 44 $blocks{$u}=[$u];
1341 }
1342 4 my @elementary=grep { $contracted->out_degree($_) == 1} $self->tree_nodes();
1343 4 @elementary=sort {$mus->{$b} <=> $mus->{$a} ||
1344 0 ▶ $hs->{$b} <=> $hs->{$a}} @elementary;
Condition0%0/3
$mus->{$b} <=> $mus->{$a} || $hs->{$b} <=> $hs->{$a}
l!l&&r!l&&!r
000
MC/DC0%0/2
$mus->{$b} <=> $mus->{$a} || $hs->{$b} <=> $hs->{$a}
$mus->{$b} <=> $mus->{$a} $hs->{$b} <=> $hs->{$a}
Truth table0%0/3
A || B
A= $mus->{$b} <=> $mus->{$a}B= $hs->{$b} <=> $hs->{$a}
ABresult
1X1
011
000
1345 4 foreach my $elem (@elementary) {
1346 0 my @children=$contracted->successors($elem);
1347 0 my $child=$children[0];
1348 0 ▶ if ($contracted->in_degree($elem) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contracted->in_degree($elem) == 1) 0 0 0
1349 0 my @parents=$contracted->predecessors($elem);
1350 0 my $parent=$parents[0];
1351 0 $contracted->add_edge($parent,$child);
1352 }
1353 0 $contracted->delete_vertex($elem);
1354 0 my @blch=@{$blocks{$child}};
1355 0 my @blem=@{$blocks{$elem}};
1356 0 $blocks{$child}=[@blem,@blch];
1357 0 delete $blocks{$elem};
1358 }
1359 4 my $contr=Bio::PhyloNetwork->new(-graph => $contracted);
1360 4 return $contr,\%blocks;
1361 }
1362  
1363 =head2 optimal_alignment
1364  
1365 Title : optimal_alignment
1366 Usage : my ($weight,$alignment,$wgts)=$net->optimal_alignment($net2)
1367 Function: returns the total weight of an optimal alignment,
1368 the alignment itself, and partial weights
1369 between the networks $net1 and $net2 on the same set of leaves.
1370 An optional argument allows one to use the Manhattan (default) or the
1371 Hamming distance between mu-vectors.
1372 Returns : scalar,reference to hash,reference to hash
1373 Args : Bio::PhyloNetwork,
1374 -metric => string (optional)
1375  
1376 Supported strings for the -metric parameter are 'Manhattan' or 'Hamming'.
1377  
1378 =cut
1379  
1380 2 ▶sub optimal_alignment {
Subroutine optimal_alignment: called CC=4 SCAR=13.9
Pod documented
1381 2 my ($net1,$net2,%params)=@_;
1382  
1383 2 my ($net1cont,$blocks1)=contract_elementary($net1);
1384 2 my ($net2cont,$blocks2)=contract_elementary($net2);
1385 2 my ($wc,$alignc,$weightc)=
1386 optimal_alignment_noelementary($net1cont,$net2cont,%params);
1387 2 my %alignment=();
1388 2 my $totalweigth=0;
1389 2 my %weigths=();
1390 2 foreach my $u1 (keys %$alignc) {
1391 18 my $u2=$alignc->{$u1};
1392 18 my @block1=@{$blocks1->{$u1}};
1393 18 my @block2=@{$blocks2->{$u2}};
1394 18 ▶ while (@block1 && @block2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@block1 and @block2) 18 18 36
Condition66%2/3
@block1 and @block2
!ll&&!rl&&r
18018
MC/DC50%1/2
@block1 and @block2
@block1 @block2
Truth table66%2/3
A and B
A= @block1B= @block2
ABresult
0X0
100
111
1395 18 my $u1dc=pop @block1;
1396 18 my $u2dc=pop @block2;
1397 18 $alignment{$u1dc}=$u2dc;
1398 18 $weigths{$u1dc}=$weightc->{$u1};
1399 18 $totalweigth+=$weigths{$u1dc};
1400 }
1401 }
1402 2 return $totalweigth,\%alignment,\%weigths;
1403 }
1404  
1405 2 ▶sub optimal_alignment_noelementary {
Subroutine optimal_alignment_noelementary: called CC=9 SCAR=22.0
Pod undocumented
1406 2 my ($net1,$net2,%params)=@_;
1407  
1408 2 my $comp = Array::Compare->new;
1409 $net1->throw("Cannot align phylogenetic networks on different set of leaves")
1410 2 ▶ unless $comp->compare($net1->{leaves},$net2->{leaves});
Branch: 1 branch
BranchTrueFalseTotal
unless $comp->compare($net1->{"leaves"}, $net2->{"leaves"}) 0 2 2
1411 2 my $distance;
1412 2 ▶ if ((defined $params{-metric})and ($params{-metric} eq 'Hamming')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{'-metric'} and $params{'-metric'} eq 'Hamming') { } 1 1 2
Condition66%2/3
defined $params{"-metric"} and $params{"-metric"} eq "Hamming"
!ll&&!rl&&r
101
MC/DC50%1/2
defined $params{"-metric"} and $params{"-metric"} eq "Hamming"
defined $params{"-metric"} $params{"-metric"} eq "Hamming"
Truth table66%2/3
A and B
A= defined $params{"-metric"}B= $params{"-metric"} eq "Hamming"
ABresult
0X0
100
111
1413 1 $distance='Hamming';
1414 } else {
1415 1 $distance='Manhattan';
1416 }
1417 2 my $numleaves=$net1->{numleaves};
1418 2 my @nodes1=$net1->internal_nodes();
1419 2 my @nodes2=$net2->internal_nodes();
1420 2 my $numnodes1=@nodes1;
1421 2 my $numnodes2=@nodes2;
1422 2 my @matrix=();
1423 2 ▶ for (my $i=0;$i<$numnodes1;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $numnodes1) 18 2 20
1424 18 my @row=();
1425 18 ▶ for (my $j=0;$j<$numnodes2;$j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $numnodes2) 90 18 108
1426 90 push @row,weight($net1,$nodes1[$i],$net2,$nodes2[$j],$distance);
1427 }
1428 18 push @matrix,\@row;
1429 }
1430 2 my @alignment=();
1431 2 Algorithm::Munkres::assign(\@matrix,\@alignment);
1432 2 my %alignmenthash;
1433 my %weighthash;
1434 2 my $totalw=0;
1435 2 foreach my $leaf (@{$net1->{leaves}}) {
1436 8 $alignmenthash{$leaf}=$leaf;
1437 8 $weighthash{$leaf}=0;
1438 }
1439 2 ▶ for (my $i=0;$i<$numnodes1;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $numnodes1) 18 2 20
1440 18 ▶ if (defined $nodes2[$alignment[$i]]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nodes2[$alignment[$i]]) 10 8 18
1441 10 $alignmenthash{$nodes1[$i]}=$nodes2[$alignment[$i]];
1442 10 $weighthash{$nodes1[$i]}=$matrix[$i][$alignment[$i]];
1443 10 $totalw += $matrix[$i][$alignment[$i]];
1444 }
1445 }
1446 2 return $totalw,\%alignmenthash,\%weighthash;
1447 }
1448  
1449 =head2 optimal_alignment_generalized
1450  
1451 Title : optimal_alignment_generalized
1452 Usage : my ($weight,%alignment)=$net->optimal_alignment_generalized($net2)
1453 Function: returns the wieght of an optimal alignment, and the alignment itself,
1454 between the topological restriction of the networks $net1 and $net2
1455 on the set of common leaves.
1456 An optional argument allows one to use the Manhattan (default) or the
1457 Hamming distance between mu-vectors.
1458 Returns : scalar,hash
1459 Args : Bio::PhyloNetwork,
1460 -metric => string (optional)
1461  
1462 Supported strings for the -metric parameter are 'Manhattan' or 'Hamming'.
1463  
1464 =cut
1465  
1466 0 ▶sub optimal_alignment_generalized {
Subroutine optimal_alignment_generalized: not called CC=1 SCAR=6.9
Pod documented
1467 0 my ($net1,$net2,%params)=@_;
1468 0 my ($netr1,$netr2)=$net1->topological_restriction($net2);
1469 0 return $netr1->optimal_alignment($netr2,%params);
1470 }
1471  
1472 90 ▶sub weight {
Subroutine weight: called CC=7 SCAR=19.5
Pod undocumented
1473 90 my ($net1,$v1,$net2,$v2,$distance)=@_;
1474 90 my $w;
1475 90 ▶ if (! defined $distance) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $distance) 0 90 90
1476 0 $distance='Manhattan';
1477 }
1478 90 ▶ if ($distance eq 'Hamming') {
Branch: 1 branch
BranchTrueFalseTotal
if ($distance eq 'Hamming') { } 45 45 90
1479 45 $w=$net1->{mudata}->{$v1}->hamming($net2->{mudata}->{$v2});
1480 } else {
1481 45 $w=$net1->{mudata}->{$v1}->manhattan($net2->{mudata}->{$v2});
1482 }
1483 90 ▶ if (($net1->is_tree_node($v1) && $net2->is_hybrid_node($v2)) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($net1->is_tree_node($v1) and $net2->is_hybrid_node($v2) or $net2->is_tree_node($v2) and $net1->is_hybrid_node($v1)) 36 54 90
Condition100%9/9
$net1->is_tree_node($v1) and $net2->is_hybrid_node($v2) or $net2->is_tree_node($v2) and $net1->is_hybrid_node($v1)
l!l&&r!l&&!r
122454
$net1->is_tree_node($v1) and $net2->is_hybrid_node($v2)
!ll&&!rl&&r
304812
$net2->is_tree_node($v2) and $net1->is_hybrid_node($v1)
!ll&&!rl&&r
64824
MC/DC75%3/4
$net1->is_tree_node($v1) and $net2->is_hybrid_node($v2) or $net2->is_tree_node($v2) and $net1->is_hybrid_node($v1)
$net1->is_tree_node($v1) $net2->is_hybrid_node($v2) $net2->is_tree_node($v2) $net1->is_hybrid_node($v1)
Truth table57%4/7
A and B or C and D
A= $net1->is_tree_node($v1)B= $net2->is_hybrid_node($v2)C= $net2->is_tree_node($v2)D= $net1->is_hybrid_node($v1)
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
1484 ($net2->is_tree_node($v2) && $net1->is_hybrid_node($v1))
1485 )
1486 {
1487 36 $w +=1/(2*$net1->{numleaves});
1488 }
1489 90 return $w;
1490 }
1491  
1492  
1493 =head2 topological_restriction
1494  
1495 Title : topological_restriction
1496 Usage : my ($netr1,$netr2)=$net1->topological_restriction($net2)
1497 Function: returns the topological restriction of $net1 and $net2 on its
1498 common set of leaves
1499 Returns : Bio::PhyloNetwork, Bio::PhyloNetwork
1500 Args : Bio::PhyloNetwork
1501  
1502 =cut
1503  
1504 0 ▶sub topological_restriction {
Subroutine topological_restriction: not called CC=10 SCAR=47.0
Pod documented
1505 0 my ($net1,$net2)=@_;
1506  
1507 0 my @leaves1=$net1->leaves();
1508 0 my @leaves2=$net2->leaves();
1509 0 my $numleaves1=scalar @leaves1;
1510 0 my $numleaves2=scalar @leaves2;
1511 0 my %position1;
1512 0 ▶ for (my $i=0; $i<$numleaves1; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $numleaves1) 0 0 0
1513 0 $position1{$leaves1[$i]}=$i;
1514 }
1515 0 my %position2;
1516 0 my @commonleaves=();
1517 0 ▶ for (my $j=0; $j<$numleaves2; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $numleaves2) 0 0 0
1518 0 ▶ if (defined $position1{$leaves2[$j]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $position1{$leaves2[$j]}) 0 0 0
1519 0 push @commonleaves,$leaves2[$j];
1520 0 $position2{$leaves2[$j]}=$j;
1521 }
1522 }
1523 0 my $graphred1=$net1->{graph}->copy();
1524 0 my $graphred2=$net2->{graph}->copy();
1525 OUTER1:
1526 0 foreach my $u ($graphred1->vertices()) {
1527 0 my $mu=$net1->mudata_node($u);
1528 0 foreach my $leaf (@commonleaves) {
1529 0 ▶ if ($mu->[$position1{$leaf}]>0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$mu[$position1{$leaf}] > 0) 0 0 0
1530 0 next OUTER1;
1531 }
1532 }
1533 0 $graphred1->delete_vertex($u);
1534 }
1535 OUTER2:
1536 0 foreach my $u ($graphred2->vertices()) {
1537 0 my $mu=$net2->mudata_node($u);
1538 0 foreach my $leaf (@commonleaves) {
1539 0 ▶ if ($mu->[$position2{$leaf}]>0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$mu[$position2{$leaf}] > 0) 0 0 0
1540 0 next OUTER2;
1541 }
1542 }
1543 0 $graphred2->delete_vertex($u);
1544 }
1545 0 my $netr1=Bio::PhyloNetwork->new(-graph => $graphred1);
1546 0 my $netr2=Bio::PhyloNetwork->new(-graph => $graphred2);
1547 0 return ($netr1,$netr2);
1548 }
1549  
1550 # Functions for eNewick representation
1551  
1552 =head2 eNewick
1553  
1554 Title : eNewick
1555 Usage : my $str=$net->eNewick()
1556 Function: returns the eNewick representation of $net without labeling
1557 internal tree nodes
1558 Returns : string
1559 Args : none
1560  
1561 =cut
1562  
1563 117 ▶sub eNewick {
Subroutine eNewick: called CC=2 SCAR=6.9
Pod documented
1564 117 my ($self)=@_;
1565 117 my $str="";
1566 117 my $seen={};
1567 117 foreach my $root ($self->roots()) {
1568 117 $str=$str.$self->eNewick_aux($root,$seen,undef)."; ";
1569 }
1570 117 return $str;
1571 }
1572  
1573 1068 ▶sub eNewick_aux {
Subroutine eNewick_aux: called CC=4 SCAR=13.9
Pod undocumented
1574 1068 my ($self,$node,$seen,$parent)=@_;
1575 1068 my $str='';
1576 1068 ▶ if ($self->is_leaf($node) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_leaf($node) or defined $seen->{$node}) { } 504 564 1068
Condition100%3/3
$self->is_leaf($node) or defined $seen->{$node}
l!l&&r!l&&!r
375129564
MC/DC100%2/2
$self->is_leaf($node) or defined $seen->{$node}
$self->is_leaf($node) defined $seen->{$node}
Truth table100%3/3
A or B
A= $self->is_leaf($node)B= defined $seen->{$node}
ABresult
1X1
011
000
1577 (defined $seen->{$node}) )
1578 {
1579 504 $str=make_label($self,$parent,$node);
1580 }
1581 else {
1582 564 $seen->{$node}=1;
1583 564 my @sons=$self->{graph}->successors($node);
1584 564 $str="(";
1585 564 foreach my $son (@sons) {
1586 951 $str=$str.$self->eNewick_aux($son,$seen,$node).",";
1587 }
1588 564 chop($str);
1589 564 $str.=")".make_label($self,$parent,$node);
1590 }
1591 1068 return $str;
1592 }
1593  
1594 1068 ▶sub make_label {
Subroutine make_label: called CC=7 SCAR=19.6
Pod undocumented
1595 1068 my ($self,$parent,$node)=@_;
1596 1068 my $str='';
1597 1068 ▶ if ($self->is_hybrid_node($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_hybrid_node($node)) { } 258 810 1068
1598 258 my $lbl=$self->{labels}->{$node};
1599 258 ▶ if ($lbl =~ /#/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lbl =~ /#/) 252 6 258
1600 252 $lbl='';
1601 }
1602 258 $str.=$lbl; #$self->{labels}->{$node};
1603 258 $str.='#';
1604 258 ▶ if ((defined $parent) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent and $self->graph->has_edge_attribute($parent, $node, "type")) 6 252 258
Condition66%2/3
defined $parent and $self->graph->has_edge_attribute($parent, $node, "type")
!ll&&!rl&&r
02526
MC/DC50%1/2
defined $parent and $self->graph->has_edge_attribute($parent, $node, "type")
defined $parent $self->graph->has_edge_attribute($parent, $node, "type")
Truth table66%2/3
A and B
A= defined $parentB= $self->graph->has_edge_attribute($parent, $node, "type")
ABresult
0X0
100
111
1605 ($self->graph->has_edge_attribute($parent,$node,'type'))) {
1606 6 $str.=$self->graph->get_edge_attribute($parent,$node,'type');
1607 }
1608 258 $str.=substr $node,1;
1609 } else {
1610 810 $str.=$self->{labels}->{$node};
1611 }
1612 1068 ▶ if ((defined $parent) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent and $self->graph->has_edge_weight($parent, $node)) 0 1068 1068
Condition66%2/3
defined $parent and $self->graph->has_edge_weight($parent, $node)
!ll&&!rl&&r
1179510
MC/DC0%0/2
defined $parent and $self->graph->has_edge_weight($parent, $node)
defined $parent $self->graph->has_edge_weight($parent, $node)
Truth table66%2/3
A and B
A= defined $parentB= $self->graph->has_edge_weight($parent, $node)
ABresult
0X0
100
111
1613 ($self->graph->has_edge_weight($parent,$node))) {
1614 0 $str.=":".$self->graph->get_edge_weight($parent,$node);
1615 }
1616 1068 return $str;
1617 }
1618  
1619 =head2 eNewick_full
1620  
1621 Title : eNewick_full
1622 Usage : my $str=$net->eNewick_full()
1623 Function: returns the eNewick representation of $net labeling
1624 internal tree nodes
1625 Returns : string
1626 Args : none
1627  
1628 =cut
1629  
1630 109 ▶sub eNewick_full {
Subroutine eNewick_full: called CC=2 SCAR=6.9
Pod documented
1631 109 my ($self)=@_;
1632 109 my $str="";
1633 109 my $seen={};
1634 109 foreach my $root ($self->roots()) {
1635 109 $str=$str.$self->eNewick_full_aux($root,$seen,undef)."; ";
1636 }
1637 109 return $str;
1638 }
1639  
1640 964 ▶sub eNewick_full_aux {
Subroutine eNewick_full_aux: called CC=4 SCAR=13.9
Pod undocumented
1641 964 my ($self,$node,$seen,$parent)=@_;
1642 964 my $str='';
1643 964 ▶ if ($self->is_leaf($node) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_leaf($node) or defined $seen->{$node}) { } 472 492 964
Condition100%3/3
$self->is_leaf($node) or defined $seen->{$node}
l!l&&r!l&&!r
343129492
MC/DC100%2/2
$self->is_leaf($node) or defined $seen->{$node}
$self->is_leaf($node) defined $seen->{$node}
Truth table100%3/3
A or B
A= $self->is_leaf($node)B= defined $seen->{$node}
ABresult
1X1
011
000
1644 (defined $seen->{$node}) )
1645 {
1646 472 $str=make_label_full($self,$parent,$node);
1647 }
1648 else {
1649 492 $seen->{$node}=1;
1650 492 my @sons=$self->{graph}->successors($node);
1651 492 $str="(";
1652 492 foreach my $son (@sons) {
1653 855 $str=$str.$self->eNewick_full_aux($son,$seen,$node).",";
1654 }
1655 492 chop($str);
1656 492 $str.=")".make_label_full($self,$parent,$node);
1657 }
1658 964 return $str;
1659 }
1660  
1661 964 ▶sub make_label_full {
Subroutine make_label_full: called CC=9 SCAR=22.2
Pod undocumented
1662 964 my ($self,$parent,$node)=@_;
1663 964 my $str='';
1664 964 ▶ if ($self->is_hybrid_node($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_hybrid_node($node)) { } 258 706 964
1665 258 my $lbl=$self->{labels}->{$node};
1666 258 ▶ if ($lbl =~ /#/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lbl =~ /#/) 252 6 258
1667 252 $lbl='';
1668 }
1669 258 $str.=$lbl; #$self->{labels}->{$node};
1670 258 $str.='#';
1671 258 ▶ if ((defined $parent) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent and $self->graph->has_edge_attribute($parent, $node, "type")) 6 252 258
Condition66%2/3
defined $parent and $self->graph->has_edge_attribute($parent, $node, "type")
!ll&&!rl&&r
02526
MC/DC50%1/2
defined $parent and $self->graph->has_edge_attribute($parent, $node, "type")
defined $parent $self->graph->has_edge_attribute($parent, $node, "type")
Truth table66%2/3
A and B
A= defined $parentB= $self->graph->has_edge_attribute($parent, $node, "type")
ABresult
0X0
100
111
1672 ($self->graph->has_edge_attribute($parent,$node,'type'))) {
1673 6 $str.=$self->graph->get_edge_attribute($parent,$node,'type');
1674 }
1675 258 $str.=substr $node,1;
1676 } else {
1677 706 ▶ if ((defined $self->{labels}->{$node})&&($self->{labels}->{$node} ne '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'labels'}{$node} and $self->{'labels'}{$node} ne '') { } 702 4 706
Condition66%2/3
defined $self->{"labels"}{$node} and $self->{"labels"}{$node} ne ""
!ll&&!rl&&r
04702
MC/DC50%1/2
defined $self->{"labels"}{$node} and $self->{"labels"}{$node} ne ""
defined $self->{"labels"}{$node} $self->{"labels"}{$node} ne ""
Truth table66%2/3
A and B
A= defined $self->{"labels"}{$node}B= $self->{"labels"}{$node} ne ""
ABresult
0X0
100
111
1678 702 $str.=$self->{labels}->{$node};
1679 }
1680 else {
1681 4 $str.=$node;
1682 }
1683 }
1684 964 ▶ if ((defined $parent) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent and $self->graph->has_edge_weight($parent, $node)) 0 964 964
Condition66%2/3
defined $parent and $self->graph->has_edge_weight($parent, $node)
!ll&&!rl&&r
1098550
MC/DC0%0/2
defined $parent and $self->graph->has_edge_weight($parent, $node)
defined $parent $self->graph->has_edge_weight($parent, $node)
Truth table66%2/3
A and B
A= defined $parentB= $self->graph->has_edge_weight($parent, $node)
ABresult
0X0
100
111
1685 ($self->graph->has_edge_weight($parent,$node))) {
1686 0 $str.=":".$self->graph->get_edge_weight($parent,$node);
1687 }
1688 964 return $str;
1689 }
1690  
1691 # sub eNewick_full {
1692 # my ($self)=@_;
1693 # my $str="";
1694 # my $seen={};
1695 # foreach my $root ($self->roots()) {
1696 # $str=$str.$self->eNewick_full_aux($root,$seen,undef)."; ";
1697 # }
1698 # return $str;
1699 # }
1700  
1701 # sub eNewick_full_aux {
1702 # my ($self,$node,$seen,$parent)=@_;
1703 # my $str;
1704 # if ($self->is_leaf($node) ||
1705 # (defined $seen->{$node}) )
1706 # {
1707 # if ($self->is_hybrid_node($node)) {
1708 # my $tag=substr $node,1;
1709 # if ((defined $parent) &&
1710 # ($self->graph->has_edge_attribute($parent,$node,'type'))) {
1711 # $str='#'.$self->graph->get_edge_attribute($parent,$node,'type').$tag;
1712 # } else {
1713 # $str=$node;
1714 # }
1715 # } else {
1716 # $str=$node;
1717 # }
1718 # }
1719 # else {
1720 # $seen->{$node}=1;
1721 # my @sons=$self->{graph}->successors($node);
1722 # $str="(";
1723 # foreach my $son (@sons) {
1724 # $str=$str.$self->eNewick_full_aux($son,$seen,$node).",";
1725 # }
1726 # chop($str);
1727 # if ($self->is_hybrid_node($node)) {
1728 # my $tag=substr $node,1;
1729 # if ((defined $parent) &&
1730 # ($self->graph->has_edge_attribute($parent,$node,'type'))) {
1731 # $str.=')#'.$self->graph->get_edge_attribute($parent,$node,'type').$tag;
1732 # } else {
1733 # $str.=")$node";
1734 # }
1735 # } else {
1736 # $str.=")$node";
1737 # }
1738 # }
1739 # if ((defined $parent) &&
1740 # ($self->graph->has_edge_weight($parent,$node))) {
1741 # $str.=":".$self->graph->get_edge_weight($parent,$node);
1742 # }
1743 # return $str;
1744 # }
1745  
1746  
1747 # displaying data
1748  
1749 12 ▶use overload '""' => \&display;
Subroutine BEGIN: called CC=1 SCAR=0.0
1750  
1751 =head2 display
1752  
1753 Title : display
1754 Usage : my $str=$net->display()
1755 Function: returns a string containing all the available information on $net
1756 Returns : string
1757 Args : none
1758  
1759 =cut
1760  
1761 108 ▶sub display {
Subroutine display: called CC=8 SCAR=25.2
Pod documented
1762 108 my ($self)=@_;
1763 108 my $str="";
1764 108 my $graph=$self->{graph};
1765 108 my @leaves=$self->leaves();
1766 108 my @nodes=@{$self->{nodes}};
1767 108 $str.= "Leaves:\t@leaves\n";
1768 108 $str.= "Nodes:\t@nodes\n";
1769 108 $str.= "Graph:\t$graph\n";
1770 108 $str.= "eNewick:\t".$self->eNewick()."\n";
1771 108 $str.= "Full eNewick:\t".$self->eNewick_full()."\n";
1772 108 $str.= "Mu-data and heights:\n";
1773 108 foreach my $node (@nodes) {
1774 822 $str.= "v=$node: ";
1775 822 ▶ if (exists $self->{labels}->{$node}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'labels'}{$node}) { } 822 0 822
1776 822 $str.="\tlabel=".$self->{labels}->{$node}.",";
1777 } else {
1778 0 $str.="\tlabel=(none),";
1779 }
1780 822 $str.= "\th=".$self->{h}->{$node}.", \tmu=".$self->{mudata}->{$node}."\n";
1781 }
1782 108 ▶ if (exists $self->{has_temporal_representation}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"has_temporal_representation"}) 0 108 108
1783 0 $str.= "Temporal representation:\n";
1784 0 ▶ if ($self->{has_temporal_representation}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'has_temporal_representation'}) { } 0 0 0
1785 0 foreach my $node (@nodes) {
1786 0 $str.= "v=$node; ";
1787 0 $str.= "\tt=".$self->{temporal_representation}->{$node}."\n";
1788 }
1789 } else {
1790 0 $str.= "Does not exist.\n";
1791 }
1792 }
1793 108 ▶ if (exists $self->{tripartitions}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"tripartitions"}) 0 108 108
1794 0 $str.= "Tripartitions:\n";
1795 0 foreach my $node (@nodes) {
1796 0 $str.= "v=$node; ";
1797 0 $str.= "\ttheta=".$self->{tripartitions}->{$node}."\n";
1798 }
1799 }
1800 108 return $str;
1801 }
1802  
1803 12 1;
« PhyloNetwork/TreeFactoryX.pm ↑ Summary PhyloNetwork/Factory.pm »