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