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 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620
|
/***************************************************************************
* Copyright (C) 2006 by BUI Quang Minh, Steffen Klaere, Arndt von Haeseler *
* minh.bui@univie.ac.at *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iqtree_config.h>
#if defined WIN32 || defined _WIN32 || defined __WIN32__
//#include <winsock2.h>
//#include <windows.h>
//extern __declspec(dllexport) int gethostname(char *name, int namelen);
#else
#include <sys/resource.h>
#endif
//#include "Eigen/Core"
#include <stdio.h>
#include "phylotree.h"
#include <signal.h>
#include <cstdio>
#include <streambuf>
#include <iostream>
#include <cstdlib>
#include <errno.h>
#include "greedy.h"
#include "pruning.h"
//#include "naivegreedy.h"
#include "splitgraph.h"
#include "circularnetwork.h"
#include "mtreeset.h"
#include "mexttree.h"
#include "ncl/ncl.h"
#include "msetsblock.h"
#include "myreader.h"
#include "phyloanalysis.h"
#include "matree.h"
#include "ngs.h"
#include "parsmultistate.h"
#include "gss.h"
#include "maalignment.h" //added by MA
#include "ncbitree.h"
#include "ecopd.h"
#include "upperbounds.h"
#include "ecopdmtreeset.h"
#include "gurobiwrapper.h"
#include "timeutil.h"
//#include <unistd.h>
#include <stdlib.h>
#include "vectorclass/instrset.h"
#include "MPIHelper.h"
#ifdef _IQTREE_MPI
#include <mpi.h>
#endif
#ifdef _OPENMP
#include <omp.h>
#endif
using namespace std;
void generateRandomTree(Params ¶ms)
{
if (params.sub_size < 3 && !params.aln_file) {
outError(ERR_FEW_TAXA);
}
if (!params.user_file) {
outError("Please specify an output tree file name");
}
////cout << "Random number seed: " << params.ran_seed << endl << endl;
SplitGraph sg;
try {
if (params.tree_gen == YULE_HARDING || params.tree_gen == CATERPILLAR ||
params.tree_gen == BALANCED || params.tree_gen == UNIFORM || params.tree_gen == STAR_TREE) {
if (!overwriteFile(params.user_file)) return;
ofstream out;
out.open(params.user_file);
MTree itree;
if (params.second_tree) {
cout << "Generating random branch lengths on tree " << params.second_tree << " ..." << endl;
itree.readTree(params.second_tree, params.is_rooted);
} else
switch (params.tree_gen) {
case YULE_HARDING:
cout << "Generating random Yule-Harding tree..." << endl;
break;
case UNIFORM:
cout << "Generating random uniform tree..." << endl;
break;
case CATERPILLAR:
cout << "Generating random caterpillar tree..." << endl;
break;
case BALANCED:
cout << "Generating random balanced tree..." << endl;
break;
case STAR_TREE:
cout << "Generating star tree with random external branch lengths..." << endl;
break;
default: break;
}
ofstream out2;
if (params.num_zero_len) {
cout << "Setting " << params.num_zero_len << " internal branches to zero length..." << endl;
string str = params.user_file;
str += ".collapsed";
out2.open(str.c_str());
}
for (int i = 0; i < params.repeated_time; i++) {
MExtTree mtree;
if (itree.root) {
mtree.copyTree(&itree);
mtree.generateRandomBranchLengths(params);
} else {
mtree.generateRandomTree(params.tree_gen, params);
}
if (params.num_zero_len) {
mtree.setZeroInternalBranches(params.num_zero_len);
MExtTree collapsed_tree;
collapsed_tree.copyTree(&mtree);
collapsed_tree.collapseZeroBranches();
collapsed_tree.printTree(out2);
out2 << endl;
}
mtree.printTree(out);
out << endl;
}
out.close();
cout << params.repeated_time << " tree(s) printed to " << params.user_file << endl;
if (params.num_zero_len) {
out2.close();
cout << params.repeated_time << " collapsed tree(s) printed to " << params.user_file << ".collapsed" << endl;
}
}
// Generate random trees if optioned
else if (params.tree_gen == CIRCULAR_SPLIT_GRAPH) {
cout << "Generating random circular split network..." << endl;
if (!overwriteFile(params.user_file)) return;
sg.generateCircular(params);
} else if (params.tree_gen == TAXA_SET) {
sg.init(params);
cout << "Generating random taxa set of size " << params.sub_size <<
" overlap " << params.overlap << " with " << params.repeated_time << " times..." << endl;
if (!overwriteFile(params.pdtaxa_file)) return;
sg.generateTaxaSet(params.pdtaxa_file, params.sub_size, params.overlap, params.repeated_time);
}
} catch (bad_alloc) {
outError(ERR_NO_MEMORY);
} catch (ios::failure) {
outError(ERR_WRITE_OUTPUT, params.user_file);
}
// calculate the distance
if (params.run_mode == CALC_DIST) {
if (params.tree_gen == CIRCULAR_SPLIT_GRAPH) {
cout << "Calculating distance matrix..." << endl;
sg.calcDistance(params.dist_file);
cout << "Distances printed to " << params.dist_file << endl;
}// else {
//mtree.calcDist(params.dist_file);
//}
}
}
inline void separator(ostream &out, int type = 0) {
switch (type) {
case 0:
out << endl << "==============================================================================" << endl;
break;
case 1:
out << endl << "-----------------------------------------------------------" << endl;
break;
default:
break;
}
}
void printCopyright(ostream &out) {
#ifdef IQ_TREE
out << "IQ-TREE";
#ifdef _IQTREE_MPI
out << " MPI";
#endif
#ifdef _OPENMP
out << " multicore";
#endif
out << " version ";
#else
out << "PDA - Phylogenetic Diversity Analyzer version ";
#endif
out << iqtree_VERSION_MAJOR << "." << iqtree_VERSION_MINOR << "." << iqtree_VERSION_PATCH;
#if defined _WIN32 || defined WIN32
out << " for Windows";
#elif defined __APPLE__ || defined __MACH__
out << " for Mac OS X";
#elif defined __linux__
out << " for Linux";
#elif defined __unix__ || defined __unix
out << " for Unix";
#else
out << " for unknown platform"
#endif
out << " " << 8*sizeof(void*) << "-bit" << " built " << __DATE__;
#if defined DEBUG
out << " - debug mode";
#endif
#ifdef IQ_TREE
out << endl << "Copyright (c) 2011-2016 Nguyen Lam Tung, Olga Chernomor, Arndt von Haeseler and Bui Quang Minh." << endl << endl;
#else
out << endl << "Copyright (c) 2006-2014 Olga Chernomor, Arndt von Haeseler and Bui Quang Minh." << endl << endl;
#endif
}
void printRunMode(ostream &out, RunMode run_mode) {
switch (run_mode) {
case DETECTED: out << "Detected"; break;
case GREEDY: out << "Greedy"; break;
case PRUNING: out << "Pruning"; break;
case BOTH_ALG: out << "Greedy and Pruning"; break;
case EXHAUSTIVE: out << "Exhaustive"; break;
case DYNAMIC_PROGRAMMING: out << "Dynamic Programming"; break;
case LINEAR_PROGRAMMING: out << "Integer Linear Programming"; break;
default: outError(ERR_INTERNAL);
}
}
/**
summarize the running with header
*/
void summarizeHeader(ostream &out, Params ¶ms, bool budget_constraint, InputType analysis_type) {
printCopyright(out);
out << "Input tree/split network file name: " << params.user_file << endl;
if(params.eco_dag_file)
out << "Input food web file name: "<<params.eco_dag_file<<endl;
out << "Input file format: " << ((params.intype == IN_NEWICK) ? "Newick" : ( (params.intype == IN_NEXUS) ? "Nexus" : "Unknown" )) << endl;
if (params.initial_file != NULL)
out << "Initial taxa file: " << params.initial_file << endl;
if (params.param_file != NULL)
out << "Parameter file: " << params.param_file << endl;
out << endl;
out << "Type of measure: " << ((params.root != NULL || params.is_rooted) ? "Rooted": "Unrooted") <<
(analysis_type== IN_NEWICK ? " phylogenetic diversity (PD)" : " split diversity (SD)");
if (params.root != NULL) out << " at " << params.root;
out << endl;
if (params.run_mode != CALC_DIST && params.run_mode != PD_USER_SET) {
out << "Search objective: " << ((params.find_pd_min) ? "Minimum" : "Maximum") << endl;
out << "Search algorithm: ";
printRunMode(out, params.run_mode);
if (params.run_mode == DETECTED) {
out << " -> ";
printRunMode(out, params.detected_mode);
}
out << endl;
out << "Search option: " << ((params.find_all) ? "Multiple optimal sets" : "Single optimal set") << endl;
}
out << endl;
out << "Type of analysis: ";
switch (params.run_mode) {
case PD_USER_SET: out << "PD/SD of user sets";
if (params.pdtaxa_file) out << " (" << params.pdtaxa_file << ")"; break;
case CALC_DIST: out << "Distance matrix computation"; break;
default:
out << ((budget_constraint) ? "Budget constraint " : "Subset size k ");
if (params.intype == IN_NEWICK)
out << ((analysis_type == IN_NEWICK) ? "on tree" : "on tree -> split network");
else
out << "on split network";
}
out << endl;
//out << "Random number seed: " << params.ran_seed << endl;
}
void summarizeFooter(ostream &out, Params ¶ms) {
separator(out);
time_t beginTime;
time (&beginTime);
char *date;
date = ctime(&beginTime);
out << "Time used: " << params.run_time << " seconds." << endl;
out << "Finished time: " << date << endl;
}
int getMaxNameLen(vector<string> &setName) {
int len = 0;
for (vector<string>::iterator it = setName.begin(); it != setName.end(); it++)
if (len < (*it).length())
len = (*it).length();
return len;
}
void printPDUser(ostream &out, Params ¶ms, PDRelatedMeasures &pd_more) {
out << "List of user-defined sets of taxa with PD score computed" << endl << endl;
int maxlen = getMaxNameLen(pd_more.setName)+2;
out.width(maxlen);
out << "Name" << " PD";
if (params.exclusive_pd) out << " excl.-PD";
if (params.endemic_pd) out << " PD-Endem.";
if (params.complement_area) out << " PD-Compl. given area " << params.complement_area;
out << endl;
int cnt;
for (cnt = 0; cnt < pd_more.setName.size(); cnt++) {
out.width(maxlen);
out << pd_more.setName[cnt] << " ";
out.width(7);
out << pd_more.PDScore[cnt] << " ";
if (params.exclusive_pd) {
out.width(7);
out << pd_more.exclusivePD[cnt] << " ";
}
if (params.endemic_pd) {
out.width(7);
out << pd_more.PDEndemism[cnt] << " ";
}
if (params.complement_area) {
out.width(8);
out << pd_more.PDComplementarity[cnt];
}
out << endl;
}
separator(out, 1);
}
void summarizeTree(Params ¶ms, PDTree &tree, vector<PDTaxaSet> &taxa_set,
PDRelatedMeasures &pd_more) {
string filename;
if (params.out_file == NULL) {
filename = params.out_prefix;
filename += ".pda";
} else
filename = params.out_file;
try {
ofstream out;
out.exceptions(ios::failbit | ios::badbit);
out.open(filename.c_str());
summarizeHeader(out, params, false, IN_NEWICK);
out << "Tree size: " << tree.leafNum-params.is_rooted << " taxa, " <<
tree.nodeNum-1-params.is_rooted << " branches" << endl;
separator(out);
vector<PDTaxaSet>::iterator tid;
if (params.run_mode == PD_USER_SET) {
printPDUser(out, params, pd_more);
}
else if (taxa_set.size() > 1)
out << "Optimal PD-sets with k = " << params.min_size-params.is_rooted <<
" to " << params.sub_size-params.is_rooted << endl << endl;
int subsize = params.min_size-params.is_rooted;
if (params.run_mode == PD_USER_SET) subsize = 1;
for (tid = taxa_set.begin(); tid != taxa_set.end(); tid++, subsize++) {
if (tid != taxa_set.begin())
separator(out, 1);
if (params.run_mode == PD_USER_SET) {
out << "Set " << subsize << " has PD score of " << tid->score << endl;
}
else {
out << "For k = " << subsize << " the optimal PD score is " << (*tid).score << endl;
out << "The optimal PD set has " << subsize << " taxa:" << endl;
}
for (NodeVector::iterator it = (*tid).begin(); it != (*tid).end(); it++)
if ((*it)->name != ROOT_NAME){
out << (*it)->name << endl;
}
if (!tid->tree_str.empty()) {
out << endl << "Corresponding sub-tree: " << endl;
out << tid->tree_str << endl;
}
tid->clear();
}
taxa_set.clear();
summarizeFooter(out, params);
out.close();
cout << endl << "Results are summarized in " << filename << endl << endl;
} catch (ios::failure) {
outError(ERR_WRITE_OUTPUT, filename);
}
}
void printTaxaSet(Params ¶ms, vector<PDTaxaSet> &taxa_set, RunMode cur_mode) {
int subsize = params.min_size-params.is_rooted;
ofstream out;
ofstream scoreout;
string filename;
filename = params.out_prefix;
filename += ".score";
scoreout.open(filename.c_str());
if (!scoreout.is_open())
outError(ERR_WRITE_OUTPUT, filename);
cout << "PD scores printed to " << filename << endl;
if (params.nr_output == 1) {
filename = params.out_prefix;
filename += ".pdtaxa";
out.open(filename.c_str());
if (!out.is_open())
outError(ERR_WRITE_OUTPUT, filename);
}
for (vector<PDTaxaSet>::iterator tid = taxa_set.begin(); tid != taxa_set.end(); tid++, subsize++) {
if (params.nr_output > 10) {
filename = params.out_prefix;
filename += ".";
filename += subsize;
if (params.run_mode == BOTH_ALG) {
if (cur_mode == GREEDY)
filename += ".greedy";
else
filename += ".pruning";
} else {
filename += ".pdtree";
}
(*tid).printTree((char*)filename.c_str());
filename = params.out_prefix;
filename += ".";
filename += subsize;
filename += ".pdtaxa";
(*tid).printTaxa((char*)filename.c_str());
} else {
out << subsize << " " << (*tid).score << endl;
scoreout << subsize << " " << (*tid).score << endl;
(*tid).printTaxa(out);
}
}
if (params.nr_output == 1) {
out.close();
cout << "All taxa list(s) printed to " << filename << endl;
}
scoreout.close();
}
/**
run PD algorithm on trees
*/
void runPDTree(Params ¶ms)
{
if (params.run_mode == CALC_DIST) {
bool is_rooted = false;
MExtTree tree(params.user_file, is_rooted);
cout << "Tree contains " << tree.leafNum << " taxa." << endl;
cout << "Calculating distance matrix..." << endl;
tree.calcDist(params.dist_file);
cout << "Distances printed to " << params.dist_file << endl;
return;
}
double t_begin, t_end;
//char filename[300];
//int idx;
vector<PDTaxaSet> taxa_set;
if (params.run_mode == PD_USER_SET) {
// compute score of user-defined sets
t_begin = getCPUTime();
cout << "Computing PD score for user-defined set of taxa..." << endl;
PDTree tree(params);
PDRelatedMeasures pd_more;
tree.computePD(params, taxa_set, pd_more);
if (params.endemic_pd)
tree.calcPDEndemism(taxa_set, pd_more.PDEndemism);
if (params.complement_area != NULL)
tree.calcPDComplementarity(taxa_set, params.complement_area, pd_more.PDComplementarity);
t_end = getCPUTime();
params.run_time = (t_end-t_begin);
summarizeTree(params, tree, taxa_set, pd_more);
return;
}
/*********************************************
run greedy algorithm
*********************************************/
if (params.sub_size < 2) {
outError(ERR_NO_K);
}
bool detected_greedy = (params.run_mode != PRUNING);
Greedy test_greedy;
test_greedy.init(params);
if (params.root == NULL && !params.is_rooted)
cout << endl << "Running PD algorithm on UNROOTED tree..." << endl;
else
cout << endl << "Running PD algorithm on ROOTED tree..." << endl;
if (verbose_mode >= VB_DEBUG)
test_greedy.drawTree(cout, WT_INT_NODE + WT_BR_SCALE + WT_BR_LEN);
if (params.run_mode == GREEDY || params.run_mode == BOTH_ALG ||
(params.run_mode == DETECTED)) {
if (params.run_mode == DETECTED && params.sub_size >= test_greedy.leafNum * 7 / 10
&& params.min_size < 2)
detected_greedy = false;
if (detected_greedy) {
params.detected_mode = GREEDY;
t_begin=getCPUTime();
cout << endl << "Greedy Algorithm..." << endl;
taxa_set.clear();
test_greedy.run(params, taxa_set);
t_end=getCPUTime();
params.run_time = (t_end-t_begin);
cout << "Time used: " << params.run_time << " seconds." << endl;
if (params.min_size == params.sub_size)
cout << "Resulting tree length = " << taxa_set[0].score << endl;
if (params.nr_output > 0)
printTaxaSet(params, taxa_set, GREEDY);
PDRelatedMeasures pd_more;
summarizeTree(params, test_greedy, taxa_set, pd_more);
}
}
/*********************************************
run pruning algorithm
*********************************************/
if (params.run_mode == PRUNING || params.run_mode == BOTH_ALG ||
(params.run_mode == DETECTED)) {
Pruning test_pruning;
if (params.run_mode == PRUNING || params.run_mode == BOTH_ALG) {
//Pruning test_pruning(params);
test_pruning.init(params);
} else if (!detected_greedy) {
test_pruning.init(test_greedy);
} else {
return;
}
params.detected_mode = PRUNING;
t_begin=getCPUTime();
cout << endl << "Pruning Algorithm..." << endl;
taxa_set.clear();
test_pruning.run(params, taxa_set);
t_end=getCPUTime();
params.run_time = (t_end-t_begin) ;
cout << "Time used: " << params.run_time << " seconds.\n";
if (params.min_size == params.sub_size)
cout << "Resulting tree length = " << taxa_set[0].score << endl;
if (params.nr_output > 0)
printTaxaSet(params, taxa_set, PRUNING);
PDRelatedMeasures pd_more;
summarizeTree(params, test_pruning, taxa_set, pd_more);
}
}
void checkSplitDistance(ostream &out, PDNetwork &sg) {
matrix(double) dist;
sg.calcDistance(dist);
int ntaxa = sg.getNTaxa();
int i, j;
bool found = false;
for (i = 0; i < ntaxa-1; i++) {
bool first = true;
for (j = i+1; j < ntaxa; j++)
if (abs(dist[i][j]) <= 1e-5) {
if (!found) {
out << "The following sets of taxa (each set in a line) have very small split-distance" << endl;
out << "( <= 1e-5) as computed from the split system. To avoid a lot of multiple" << endl;
out << "optimal PD sets to be reported, one should only keep one taxon from each set" << endl;
out << "and exclude the rest from the analysis." << endl << endl;
}
if (first)
out << sg.getTaxa()->GetTaxonLabel(i);
found = true;
first = false;
out << ", " << sg.getTaxa()->GetTaxonLabel(j);
}
if (!first) out << endl;
}
if (found)
separator(out);
}
/**
check if the set are nested and there are no multiple optimal sets.
If yes, return the ranking as could be produced by a greedy algorithm
*/
bool makeRanking(vector<SplitSet> &pd_set, IntVector &indices, IntVector &ranking) {
vector<SplitSet>::iterator it;
IntVector::iterator inti;
ranking.clear();
bool nested = true;
Split *cur_sp = NULL;
int id = 1;
for (it = pd_set.begin(); it != pd_set.end(); it++) {
if ((*it).empty()) continue;
if ((*it).size() > 1) {
nested = false;
ranking.push_back(-10);
indices.push_back(0);
}
Split *sp = (*it)[0];
if (!cur_sp) {
IntVector sp_tax;
sp->getTaxaList(sp_tax);
ranking.insert(ranking.end(), sp_tax.begin(), sp_tax.end());
for (inti = sp_tax.begin(); inti != sp_tax.end(); inti++)
indices.push_back(id++);
} else {
if ( !cur_sp->subsetOf(*sp)) {
ranking.push_back(-1);
indices.push_back(0);
nested = false;
}
Split sp_diff(*sp);
sp_diff -= *cur_sp;
Split sp_diff2(*cur_sp);
sp_diff2 -= *sp;
IntVector sp_tax;
sp_diff2.getTaxaList(sp_tax);
ranking.insert(ranking.end(), sp_tax.begin(), sp_tax.end());
for (inti = sp_tax.begin(); inti != sp_tax.end(); inti++)
indices.push_back(-id);
sp_diff.getTaxaList(sp_tax);
ranking.insert(ranking.end(), sp_tax.begin(), sp_tax.end());
for (inti = sp_tax.begin(); inti != sp_tax.end(); inti++)
indices.push_back(id);
if ( !cur_sp->subsetOf(*sp)) {
ranking.push_back(-2);
indices.push_back(0);
}
id++;
}
cur_sp = sp;
}
return nested;
}
void printNexusSets(const char *filename, PDNetwork &sg, vector<SplitSet> &pd_set) {
try {
ofstream out;
out.open(filename);
out << "#NEXUS" << endl << "BEGIN Sets;" << endl;
vector<SplitSet>::iterator it;
for (it = pd_set.begin(); it != pd_set.end(); it++) {
int id = 1;
for (SplitSet::iterator sit = (*it).begin(); sit != (*it).end(); sit++, id++) {
IntVector taxa;
(*sit)->getTaxaList(taxa);
out << " TAXSET Opt_" << taxa.size() << "_" << id << " =";
for (IntVector::iterator iit = taxa.begin(); iit != taxa.end(); iit++) {
if (sg.isPDArea())
out << " '" << sg.getSetsBlock()->getSet(*iit)->name << "'";
else
out << " '" << sg.getTaxa()->GetTaxonLabel(*iit) << "'";
}
out << ";" << endl;
}
}
out << "END; [Sets]" << endl;
out.close();
cout << endl << "Optimal sets are written to nexus file " << filename << endl;
} catch (ios::failure) {
outError(ERR_WRITE_OUTPUT, filename);
}
}
void computeTaxaFrequency(SplitSet &taxa_set, DoubleVector &freq) {
assert(taxa_set.size());
int ntaxa = taxa_set[0]->getNTaxa();
int i;
freq.resize(ntaxa, 0);
for (SplitSet::iterator it2 = taxa_set.begin(); it2 != taxa_set.end(); it2++) {
for ( i = 0; i < ntaxa; i++)
if ((*it2)->containTaxon(i)) freq[i] += 1.0;
}
for ( i = 0; i < ntaxa; i++)
freq[i] /= taxa_set.size();
}
/**
summarize the running results
*/
void summarizeSplit(Params ¶ms, PDNetwork &sg, vector<SplitSet> &pd_set, PDRelatedMeasures &pd_more, bool full_report) {
int i;
if (params.nexus_output) {
string nex_file = params.out_prefix;
nex_file += ".pdsets.nex";
printNexusSets(nex_file.c_str(), sg, pd_set);
}
string filename;
if (params.out_file == NULL) {
filename = params.out_prefix;
filename += ".pda";
} else
filename = params.out_file;
try {
ofstream out;
out.open(filename.c_str());
/****************************/
/********** HEADER **********/
/****************************/
summarizeHeader(out, params, sg.isBudgetConstraint(), IN_NEXUS);
out << "Network size: " << sg.getNTaxa()-params.is_rooted << " taxa, " <<
sg.getNSplits()-params.is_rooted << " splits (of which " <<
sg.getNTrivialSplits() << " are trivial splits)" << endl;
out << "Network type: " << ((sg.isCircular()) ? "Circular" : "General") << endl;
separator(out);
checkSplitDistance(out, sg);
int c_num = 0;
//int subsize = (sg.isBudgetConstraint()) ? params.budget : (params.sub_size-params.is_rooted);
//subsize -= pd_set.size()-1;
int subsize = (sg.isBudgetConstraint()) ? params.min_budget : params.min_size-params.is_rooted;
int stepsize = (sg.isBudgetConstraint()) ? params.step_budget : params.step_size;
if (params.detected_mode != LINEAR_PROGRAMMING) stepsize = 1;
vector<SplitSet>::iterator it;
SplitSet::iterator it2;
if (params.run_mode == PD_USER_SET) {
printPDUser(out, params, pd_more);
}
/****************************/
/********** SUMMARY *********/
/****************************/
if (params.run_mode != PD_USER_SET && !params.num_bootstrap_samples) {
out << "Summary of the PD-score and the number of optimal PD-sets with the same " << endl << "optimal PD-score found." << endl;
if (sg.isBudgetConstraint())
out << endl << "Budget PD-score %PD-score #PD-sets" << endl;
else
out << endl << "Size-k PD-score %PD-score #PD-sets" << endl;
int sizex = subsize;
double total = sg.calcWeight();
for (it = pd_set.begin(); it != pd_set.end(); it++, sizex+=stepsize) {
out.width(6);
out << right << sizex << " ";
out.width(10);
out << right << (*it).getWeight() << " ";
out.width(10);
out << right << ((*it).getWeight()/total)*100.0 << " ";
out.width(6);
out << right << (*it).size();
out << endl;
}
out << endl;
if (!params.find_all)
out << "Note: You did not choose the option to find multiple optimal PD sets." << endl <<
"That's why we only reported one PD-set per size-k or budget. If you want" << endl <<
"to determine all multiple PD-sets, use the '-a' option.";
else {
out << "Note: The number of multiple optimal PD sets to be reported is limited to " << params.pd_limit << "." << endl <<
"There might be cases where the actual #PD-sets exceeds that upper-limit but" << endl <<
"won't be listed here. Please refer to the above list to identify such cases." << endl <<
"To increase the upper-limit, use the '-lim <limit_number>' option.";
}
out << endl;
separator(out);
}
if (!full_report) {
out.close();
return;
}
/****************************/
/********* BOOTSTRAP ********/
/****************************/
if (params.run_mode != PD_USER_SET && params.num_bootstrap_samples) {
out << "Summary of the bootstrap analysis " << endl;
for (it = pd_set.begin(); it != pd_set.end(); it++) {
DoubleVector freq;
computeTaxaFrequency((*it), freq);
out << "For k/budget = " << subsize << " the " << ((sg.isPDArea()) ? "areas" : "taxa")
<< " supports are: " << endl;
for (i = 0; i < freq.size(); i++)
out << ((sg.isPDArea()) ? sg.getSetsBlock()->getSet(i)->name : sg.getTaxa()->GetTaxonLabel(i))
<< "\t" << freq[i] << endl;
if ((it+1) != pd_set.end()) separator(out, 1);
}
out << endl;
separator(out);
}
/****************************/
/********** RANKING *********/
/****************************/
if (params.run_mode != PD_USER_SET && !params.num_bootstrap_samples) {
IntVector ranking;
IntVector index;
out << "Ranking based on the optimal sets" << endl;
if (!makeRanking(pd_set, index, ranking)) {
out << "WARNING: Optimal sets are not nested, so ranking should not be considered stable" << endl;
}
if (subsize > 1) {
out << "WARNING: The first " << subsize << " ranks should be treated equal" << endl;
}
out << endl << "Rank* ";
if (!sg.isPDArea())
out << "Taxon names" << endl;
else
out << "Area names" << endl;
for (IntVector::iterator intv = ranking.begin(), intid = index.begin(); intv != ranking.end(); intv ++, intid++) {
if (*intv == -10)
out << "<--- multiple optimal set here --->" << endl;
else if (*intv == -1)
out << "<--- BEGIN: greedy does not work --->" << endl;
else if (*intv == -2)
out << "<--- END --->" << endl;
else {
out.width(5);
out << right << *intid << " ";
if (sg.isPDArea())
out << sg.getSetsBlock()->getSet(*intv)->name << endl;
else
out << sg.getTaxa()->GetTaxonLabel(*intv) << endl;
}
}
out << endl;
out << "(*) Negative ranks indicate the point at which the greedy algorithm" << endl <<
" does not work. In that case, the corresponding taxon/area names" << endl <<
" should be deleted from the optimal set of the same size" << endl;
separator(out);
}
int max_len = sg.getTaxa()->GetMaxTaxonLabelLength();
/****************************/
/***** DETAILED SETS ********/
/****************************/
if (params.run_mode != PD_USER_SET)
out << "Detailed information of all taxa found in the optimal PD-sets" << endl;
if (pd_set.size() > 1) {
if (sg.isBudgetConstraint())
out << "with budget = " << params.min_budget <<
" to " << params.budget << endl << endl;
else
out << "with k = " << params.min_size-params.is_rooted <<
" to " << params.sub_size-params.is_rooted << endl << endl;
}
if (params.run_mode != PD_USER_SET)
separator(out,1);
for (it = pd_set.begin(); it != pd_set.end(); it++, subsize+=stepsize) {
// check if the pd-sets are the same as previous one
if (sg.isBudgetConstraint() && it != pd_set.begin()) {
vector<SplitSet>::iterator prev, next;
for (next=it, prev=it-1; next != pd_set.end() && next->getWeight() == (*prev).getWeight() &&
next->size() == (*prev).size(); next++ ) ;
if (next != it) {
// found something in between!
out << endl;
//out << endl << "**************************************************************" << endl;
out << "For budget = " << subsize << " -> " << subsize+(next-it-1)*stepsize <<
" the optimal PD score and PD sets" << endl;
out << "are identical to the case when budget = " << subsize-stepsize << endl;
//out << "**************************************************************" << endl;
subsize += (next-it)*stepsize;
it = next;
if (it == pd_set.end()) break;
}
}
if (it != pd_set.begin()) separator(out, 1);
int num_sets = (*it).size();
double weight = (*it).getWeight();
if (params.run_mode != PD_USER_SET) {
out << "For " << ((sg.isBudgetConstraint()) ? "budget" : "k") << " = " << subsize;
out << " the optimal PD score is " << weight << endl;
if (num_sets == 1) {
if (!sg.isBudgetConstraint())
out << "The optimal PD set has " << (*it)[0]->countTaxa()-params.is_rooted <<
((sg.isPDArea()) ? " areas" : " taxa");
else
out << "The optimal PD set has " << (*it)[0]->countTaxa()-params.is_rooted <<
((sg.isPDArea()) ? " areas" : " taxa") << " and requires " << sg.calcCost(*(*it)[0]) << " budget";
if (!sg.isPDArea()) out << " and covers " << sg.countSplits(*(*it)[0]) <<
" splits (of which " << sg.countInternalSplits(*(*it)[0]) << " are internal splits)";
out << endl;
}
else
out << "Found " << num_sets << " PD sets with the same optimal score." << endl;
}
for (it2 = (*it).begin(), c_num=1; it2 != (*it).end(); it2++, c_num++){
Split *this_set = *it2;
if (params.run_mode == PD_USER_SET && it2 != (*it).begin())
separator(out, 1);
if (params.run_mode == PD_USER_SET) {
if (!sg.isBudgetConstraint())
out << "Set " << c_num << " has PD score of " << this_set->getWeight();
else
out << "Set " << c_num << " has PD score of " << this_set->getWeight() <<
" and requires " << sg.calcCost(*this_set) << " budget";
} else if (num_sets > 1) {
if (!sg.isBudgetConstraint())
out << endl << "PD set " << c_num;
else
out << endl << "PD set " << c_num << " has " << this_set->countTaxa()-params.is_rooted <<
" taxa and requires " << sg.calcCost(*this_set) << " budget";
}
if (!sg.isPDArea() && (num_sets > 1 || params.run_mode == PD_USER_SET ))
out << " and covers " << sg.countSplits(*(*it)[0]) << " splits (of which "
<< sg.countInternalSplits(*(*it)[0]) << " are internal splits)";
out << endl;
if (params.run_mode != PD_USER_SET && sg.isPDArea()) {
for (i = 0; i < sg.getSetsBlock()->getNSets(); i++)
if (this_set->containTaxon(i)) {
if (sg.isBudgetConstraint()) {
out.width(max_len);
out << left << sg.getSetsBlock()->getSet(i)->name << "\t";
out.width(10);
out << right << sg.getPdaBlock()->getCost(i);
out << endl;
} else {
out << sg.getSetsBlock()->getSet(i)->name << endl;
}
}
Split sp(sg.getNTaxa());
for (i = 0; i < sg.getSetsBlock()->getNSets(); i++)
if (this_set->containTaxon(i))
sp += *(sg.area_taxa[i]);
out << endl << "which contains " << sp.countTaxa() - params.is_rooted << " taxa: " << endl;
for (i = 0; i < sg.getNTaxa(); i++)
if (sg.getTaxa()->GetTaxonLabel(i) != ROOT_NAME && sp.containTaxon(i))
out << sg.getTaxa()->GetTaxonLabel(i) << endl;
} else
for ( i = 0; i < sg.getNTaxa(); i++)
if (sg.getTaxa()->GetTaxonLabel(i) != ROOT_NAME && this_set->containTaxon(i)) {
if (sg.isBudgetConstraint()) {
out.width(max_len);
out << left << sg.getTaxa()->GetTaxonLabel(i) << "\t";
out.width(10);
out << right << sg.getPdaBlock()->getCost(i);
out << endl;
} else {
out << sg.getTaxa()->GetTaxonLabel(i) << endl;
}
}
}
}
/****************************/
/********** FOOTER **********/
/****************************/
summarizeFooter(out, params);
out.close();
cout << endl << "Results are summarized in " << filename << endl << endl;
} catch (ios::failure) {
outError(ERR_WRITE_OUTPUT, filename);
}
}
void printGainMatrix(char *filename, matrix(double) &delta_gain, int start_k) {
try {
ofstream out;
out.exceptions(ios::failbit | ios::badbit);
out.open(filename);
int k = start_k;
for (matrix(double)::iterator it = delta_gain.begin(); it != delta_gain.end(); it++, k++) {
out << k;
for (int i = 0; i < (*it).size(); i++)
out << " " << (*it)[i];
out << endl;
}
out.close();
cout << "PD gain matrix printed to " << filename << endl;
} catch (ios::failure) {
outError(ERR_WRITE_OUTPUT, filename);
}
}
/**
run PD algorithm on split networks
*/
void runPDSplit(Params ¶ms) {
cout << "Using NCL - Nexus Class Library" << endl << endl;
// init a split graph class from the parameters
CircularNetwork sg(params);
int i;
// this vector of SplitSet store all the optimal PD sets
vector<SplitSet> pd_set;
// this define an order of taxa (circular order in case of circular networks)
vector<int> taxa_order;
// this store a particular taxa set
Split taxa_set;
if (sg.isCircular()) {
// is a circular network, get circular order
for (i = 0; i < sg.getNTaxa(); i++)
taxa_order.push_back(sg.getCircleId(i));
} else
// otherwise, get the incremental order
for (i = 0; i < sg.getNTaxa(); i++)
taxa_order.push_back(i);
PDRelatedMeasures pd_more;
// begining time of the algorithm run
double time_begin = getCPUTime();
//time(&time_begin);
// check parameters
if (sg.isPDArea()) {
if (sg.isBudgetConstraint()) {
int budget = (params.budget >= 0) ? params.budget : sg.getPdaBlock()->getBudget();
if (budget < 0 && params.pd_proportion == 0.0) params.run_mode = PD_USER_SET;
} else {
int sub_size = (params.sub_size >= 1) ? params.sub_size : sg.getPdaBlock()->getSubSize();
if (sub_size < 1 && params.pd_proportion == 0.0) params.run_mode = PD_USER_SET;
}
}
if (params.run_mode == PD_USER_SET) {
// compute score of user-defined sets
cout << "Computing PD score for user-defined set of taxa..." << endl;
pd_set.resize(1);
sg.computePD(params, pd_set[0], pd_more);
if (params.endemic_pd)
sg.calcPDEndemism(pd_set[0], pd_more.PDEndemism);
if (params.complement_area != NULL)
sg.calcPDComplementarity(pd_set[0], params.complement_area, pd_more.setName, pd_more.PDComplementarity);
} else {
// otherwise, call the main function
if (params.num_bootstrap_samples) {
cout << endl << "======= START BOOTSTRAP ANALYSIS =======" << endl;
MTreeSet *mtrees = sg.getMTrees();
if (mtrees->size() < 100)
cout << "WARNING: bootstrap may be unstable with less than 100 trees" << endl;
vector<string> taxname;
sg.getTaxaName(taxname);
i = 1;
for (MTreeSet::iterator it = mtrees->begin(); it != mtrees->end(); it++, i++) {
cout << "---------- TREE " << i << " ----------" << endl;
// convert tree into split sytem
SplitGraph sg2;
(*it)->convertSplits(taxname, sg2);
// change the current split system
for (SplitGraph::reverse_iterator it = sg.rbegin(); it != sg.rend(); it++) {
delete *it;
}
sg.clear();
sg.insert(sg.begin(), sg2.begin(), sg2.end());
sg2.clear();
// now findPD on the converted tree-split system
sg.findPD(params, pd_set, taxa_order);
}
cout << "======= DONE BOOTSTRAP ANALYSIS =======" << endl << endl;
} else {
sg.findPD(params, pd_set, taxa_order);
}
}
// ending time
double time_end = getCPUTime();
//time(&time_end);
params.run_time = time_end - time_begin;
cout << "Time used: " << (double) (params.run_time) << " seconds." << endl;
if (verbose_mode >= VB_DEBUG && !sg.isPDArea()) {
cout << "PD set(s) with score(s): " << endl;
for (vector<SplitSet>::iterator it = pd_set.begin(); it != pd_set.end(); it++)
for (SplitSet::iterator it2 = (*it).begin(); it2 != (*it).end(); it2++ ){
//(*it)->report(cout);
cout << " " << (*it2)->getWeight() << " ";
for (i = 0; i < sg.getNTaxa(); i++)
if ((*it2)->containTaxon(i))
cout << sg.getTaxa()->GetTaxonLabel(i) << " ";
if (sg.isBudgetConstraint())
cout << " (budget = " << sg.calcCost(*(*it2)) << ")";
cout << endl;
}
}
sg.printOutputSetScore(params, pd_set);
summarizeSplit(params, sg, pd_set, pd_more, true);
if (params.calc_pdgain) {
matrix(double) delta_gain;
sg.calcPDGain(pd_set, delta_gain);
string filename = params.out_prefix;
filename += ".pdgain";
printGainMatrix((char*)filename.c_str(), delta_gain, pd_set.front().front()->countTaxa());
//cout << delta_gain;
}
//for (i = pd_set.size()-1; i >= 0; i--)
// delete pd_set[i];
}
void printSplitSet(SplitGraph &sg, SplitIntMap &hash_ss) {
/*
for (SplitIntMap::iterator it = hash_ss.begin(); it != hash_ss.end(); it++) {
if ((*it)->getWeight() > 50 && (*it)->countTaxa() > 1)
(*it)->report(cout);
}*/
sg.getTaxa()->Report(cout);
for (SplitGraph::iterator it = sg.begin(); it != sg.end(); it++) {
if ((*it)->getWeight() > 50 && (*it)->countTaxa() > 1)
(*it)->report(cout);
}
}
void readTaxaOrder(char *taxa_order_file, StrVector &taxa_order) {
}
void calcTreeCluster(Params ¶ms) {
assert(params.taxa_order_file);
MExtTree tree(params.user_file, params.is_rooted);
// StrVector taxa_order;
//readTaxaOrder(params.taxa_order_file, taxa_order);
NodeVector taxa;
matrix(int) clusters;
clusters.reserve(tree.leafNum - 3);
tree.getTaxa(taxa);
sort(taxa.begin(), taxa.end(), nodenamecmp);
tree.createCluster(taxa, clusters);
int cnt = 1;
string treename = params.out_prefix;
treename += ".clu-id";
tree.printTree(treename.c_str());
for (matrix(int)::iterator it = clusters.begin(); it != clusters.end(); it++, cnt++) {
ostringstream filename;
filename << params.out_prefix << "." << cnt << ".clu";
ofstream out(filename.str().c_str());
ostringstream filename2;
filename2 << params.out_prefix << "." << cnt << ".name-clu";
ofstream out2(filename2.str().c_str());
out << "w" << endl << "c" << endl << "4" << endl << "b" << endl << "g" << endl << 4-params.is_rooted << endl;
IntVector::iterator it2;
NodeVector::iterator it3;
for (it2 = (*it).begin(), it3 = taxa.begin(); it2 != (*it).end(); it2++, it3++)
if ((*it3)->name != ROOT_NAME) {
out << char((*it2)+'a') << endl;
out2 << (*it3)->name << " " << char((*it2)+'a') << endl;
}
out << "y" << endl;
out.close();
out2.close();
cout << "Cluster " << cnt << " printed to " << filename.rdbuf() << " and " << filename2.rdbuf() << endl;
}
}
void printTaxa(Params ¶ms) {
MTree mytree(params.user_file, params.is_rooted);
vector<string> taxname;
taxname.resize(mytree.leafNum);
mytree.getTaxaName(taxname);
sort(taxname.begin(), taxname.end());
string filename = params.out_prefix;
filename += ".taxa";
try {
ofstream out;
out.exceptions(ios::failbit | ios::badbit);
out.open(filename.c_str());
for (vector<string>::iterator it = taxname.begin(); it != taxname.end(); it++) {
if ((*it) != ROOT_NAME) out << (*it);
out << endl;
}
out.close();
cout << "All taxa names printed to " << filename << endl;
} catch (ios::failure) {
outError(ERR_WRITE_OUTPUT, filename);
}
}
void printAreaList(Params ¶ms) {
MSetsBlock *sets;
sets = new MSetsBlock();
cout << "Reading input file " << params.user_file << "..." << endl;
MyReader nexus(params.user_file);
nexus.Add(sets);
MyToken token(nexus.inf);
nexus.Execute(token);
//sets->Report(cout);
TaxaSetNameVector *allsets = sets->getSets();
string filename = params.out_prefix;
filename += ".names";
try {
ofstream out;
out.exceptions(ios::failbit | ios::badbit);
out.open(filename.c_str());
for (TaxaSetNameVector::iterator it = allsets->begin(); it != allsets->end(); it++) {
out << (*it)->name;
out << endl;
}
out.close();
cout << "All area names printed to " << filename << endl;
} catch (ios::failure) {
outError(ERR_WRITE_OUTPUT, filename);
}
delete sets;
}
void scaleBranchLength(Params ¶ms) {
params.is_rooted = true;
PDTree tree(params);
if (params.run_mode == SCALE_BRANCH_LEN) {
cout << "Scaling branch length with a factor of " << params.scaling_factor << " ..." << endl;
tree.scaleLength(params.scaling_factor, false);
} else {
cout << "Scaling clade support with a factor of " << params.scaling_factor << " ..." << endl;
tree.scaleCladeSupport(params.scaling_factor, false);
}
if (params.out_file != NULL)
tree.printTree(params.out_file);
else {
tree.printTree(cout);
cout << endl;
}
}
void calcDistribution(Params ¶ms) {
PDTree mytree(params);
string filename = params.out_prefix;
filename += ".randompd";
try {
ofstream out;
out.exceptions(ios::failbit | ios::badbit);
out.open(filename.c_str());
for (int size = params.min_size; size <= params.sub_size; size += params.step_size) {
out << size;
for (int sample = 0; sample < params.sample_size; sample++) {
Split taxset(mytree.leafNum);
taxset.randomize(size);
mytree.calcPD(taxset);
out << " " << taxset.getWeight();
}
out << endl;
}
out.close();
cout << "PD distribution is printed to " << filename << endl;
} catch (ios::failure) {
outError(ERR_WRITE_OUTPUT, filename);
}
}
void printRFDist(ostream &out, int *rfdist, int n, int m, int rf_dist_mode) {
int i, j;
if (rf_dist_mode == RF_ADJACENT_PAIR) {
out << "XXX ";
out << 1 << " " << n << endl;
for (i = 0; i < n; i++)
out << " " << rfdist[i];
out << endl;
} else {
// all pairs
out << n << " " << m << endl;
for (i = 0; i < n; i++) {
out << "Tree" << i << " ";
for (j = 0; j < m; j++)
out << " " << rfdist[i*m+j];
out << endl;
}
}
}
void computeRFDistExtended(const char *trees1, const char *trees2, const char *filename) {
cout << "Reading input trees 1 file " << trees1 << endl;
int ntrees = 0, ntrees2 = 0;
int *rfdist_raw = NULL;
try {
ifstream in;
in.exceptions(ios::failbit | ios::badbit);
in.open(trees1);
IntVector rfdist;
for (ntrees = 1; !in.eof(); ntrees++) {
MTree tree;
bool is_rooted = false;
// read in the tree and convert into split system for indexing
tree.readTree(in, is_rooted);
if (verbose_mode >= VB_DEBUG)
cout << ntrees << " " << endl;
IntVector dist;
tree.computeRFDist(trees2, dist);
ntrees2 = dist.size();
rfdist.insert(rfdist.end(), dist.begin(), dist.end());
char ch;
in.exceptions(ios::goodbit);
(in) >> ch;
if (in.eof()) break;
in.unget();
in.exceptions(ios::failbit | ios::badbit);
}
in.close();
assert(ntrees * ntrees2 == rfdist.size());
rfdist_raw = new int[rfdist.size()];
copy(rfdist.begin(), rfdist.end(), rfdist_raw);
} catch (ios::failure) {
outError(ERR_READ_INPUT, trees1);
}
try {
ofstream out;
out.exceptions(ios::failbit | ios::badbit);
out.open(filename);
printRFDist(out, rfdist_raw, ntrees, ntrees2, RF_TWO_TREE_SETS_EXTENDED);
out.close();
cout << "Robinson-Foulds distances printed to " << filename << endl;
} catch (ios::failure) {
outError(ERR_WRITE_OUTPUT, filename);
}
}
void computeRFDist(Params ¶ms) {
if (!params.user_file) outError("User tree file not provided");
string filename = params.out_prefix;
filename += ".rfdist";
if (params.rf_dist_mode == RF_TWO_TREE_SETS_EXTENDED) {
computeRFDistExtended(params.user_file, params.second_tree, filename.c_str());
return;
}
MTreeSet trees(params.user_file, params.is_rooted, params.tree_burnin, params.tree_max_count);
int n = trees.size(), m = trees.size();
int *rfdist;
int *incomp_splits = NULL;
string infoname = params.out_prefix;
infoname += ".rfinfo";
string treename = params.out_prefix;
treename += ".rftree";
if (params.rf_dist_mode == RF_TWO_TREE_SETS) {
MTreeSet treeset2(params.second_tree, params.is_rooted, params.tree_burnin, params.tree_max_count);
cout << "Computing Robinson-Foulds distances between two sets of trees" << endl;
m = treeset2.size();
rfdist = new int [n*m];
memset(rfdist, 0, n*m* sizeof(int));
if (verbose_mode >= VB_MAX) {
incomp_splits = new int [n*m];
memset(incomp_splits, 0, n*m* sizeof(int));
}
if (verbose_mode >= VB_MED)
trees.computeRFDist(rfdist, &treeset2, infoname.c_str(),treename.c_str(), incomp_splits);
else
trees.computeRFDist(rfdist, &treeset2);
} else {
rfdist = new int [n*n];
memset(rfdist, 0, n*n* sizeof(int));
trees.computeRFDist(rfdist, params.rf_dist_mode, params.split_weight_threshold);
}
if (verbose_mode >= VB_MED) printRFDist(cout, rfdist, n, m, params.rf_dist_mode);
try {
ofstream out;
out.exceptions(ios::failbit | ios::badbit);
out.open(filename.c_str());
printRFDist(out, rfdist, n, m, params.rf_dist_mode);
out.close();
cout << "Robinson-Foulds distances printed to " << filename << endl;
} catch (ios::failure) {
outError(ERR_WRITE_OUTPUT, filename);
}
if (incomp_splits)
try {
filename = params.out_prefix;
filename += ".incomp";
ofstream out;
out.exceptions(ios::failbit | ios::badbit);
out.open(filename.c_str());
printRFDist(out, incomp_splits, n, m, params.rf_dist_mode);
out.close();
cout << "Number of incompatible splits in printed to " << filename << endl;
} catch (ios::failure) {
outError(ERR_WRITE_OUTPUT, filename);
}
if (incomp_splits) delete [] incomp_splits;
delete [] rfdist;
}
void testInputFile(Params ¶ms) {
SplitGraph sg(params);
if (sg.isWeaklyCompatible())
cout << "The split system is weakly compatible." << endl;
else
cout << "The split system is NOT weakly compatible." << endl;
}
/**MINH ANH: for some statistics about the branches on the input tree*/
void branchStats(Params ¶ms){
MaTree mytree(params.user_file, params.is_rooted);
mytree.drawTree(cout,WT_TAXON_ID + WT_INT_NODE);
//report to output file
string output;
if (params.out_file)
output = params.out_file;
else {
if (params.out_prefix)
output = params.out_prefix;
else
output = params.user_file;
output += ".stats";
}
try {
ofstream out;
out.exceptions(ios::failbit | ios::badbit);
out.open(output.c_str());
mytree.printBrInfo(out);
} catch (ios::failure) {
outError(ERR_WRITE_OUTPUT, output);
}
cout << "Information about branch lengths of the tree is printed to: " << output << endl;
/***** Following added by BQM to print internal branch lengths */
NodeVector nodes1, nodes2;
mytree.generateNNIBraches(nodes1, nodes2);
output = params.out_prefix;
output += ".inlen";
try {
ofstream out;
out.exceptions(ios::failbit | ios::badbit);
out.open(output.c_str());
for (int i = 0; i < nodes1.size(); i++)
out << nodes1[i]->findNeighbor(nodes2[i])->length << " ";
out << endl;
} catch (ios::failure) {
outError(ERR_WRITE_OUTPUT, output);
}
cout << "Internal branch lengths printed to: " << output << endl;
}
/**MINH ANH: for comparison between the input tree and each tree in a given set of trees*/
void compare(Params ¶ms){
MaTree mytree(params.second_tree, params.is_rooted);
//sort taxon names and update nodeID, to be consistent with MTreeSet
NodeVector taxa;
mytree.getTaxa(taxa);
sort(taxa.begin(), taxa.end(), nodenamecmp);
int i;
NodeVector::iterator it;
for (it = taxa.begin(), i = 0; it != taxa.end(); it++, i++)
(*it)->id = i;
string drawFile = params.second_tree;
drawFile += ".draw";
try {
ofstream out1;
out1.exceptions(ios::failbit | ios::badbit);
out1.open(drawFile.c_str());
mytree.drawTree(out1,WT_TAXON_ID + WT_INT_NODE);
} catch (ios::failure) {
outError(ERR_WRITE_OUTPUT, drawFile);
}
cout << "Tree with branchID (nodeID) was printed to: " << drawFile << endl;
MTreeSet trees(params.user_file,params.is_rooted, params.tree_burnin, params.tree_max_count);
DoubleMatrix brMatrix;
DoubleVector BSDs;
IntVector RFs;
mytree.comparedTo(trees, brMatrix, RFs, BSDs);
int numTree = trees.size();
int numNode = mytree.nodeNum;
string output;
if (params.out_file)
output = params.out_file;
else {
if (params.out_prefix)
output = params.out_prefix;
else
output = params.user_file;
output += ".compare";
}
try {
ofstream out;
out.exceptions(ios::failbit | ios::badbit);
out.open(output.c_str());
//print the header
out << "tree " ;
for (int nodeID = 0; nodeID < numNode; nodeID++ )
if ( brMatrix[0][nodeID] != -2 )
out << "br_" << nodeID << " ";
out << "RF BSD" << endl;
for ( int treeID = 0; treeID < numTree; treeID++ )
{
out << treeID << " ";
for (int nodeID = 0; nodeID < numNode; nodeID++ )
if ( brMatrix[treeID][nodeID] != -2 )
out << brMatrix[treeID][nodeID] << " ";
out << RFs[treeID] << " " << BSDs[treeID] << endl;
}
} catch (ios::failure) {
outError(ERR_WRITE_OUTPUT, output);
}
cout << "Comparison with the given set of trees is printed to: " << output << endl;
}
/**MINH ANH: to compute 'guided bootstrap' alignment*/
void guidedBootstrap(Params ¶ms)
{
MaAlignment inputAlign(params.aln_file,params.sequence_type, params.intype);
inputAlign.readLogLL(params.siteLL_file);
string outFre_name = params.out_prefix;
outFre_name += ".patInfo";
inputAlign.printPatObsExpFre(outFre_name.c_str());
string gboAln_name = params.out_prefix;
gboAln_name += ".gbo";
MaAlignment gboAlign;
double prob;
gboAlign.generateExpectedAlignment(&inputAlign, prob);
gboAlign.printPhylip(gboAln_name.c_str());
string outProb_name = params.out_prefix;
outProb_name += ".gbo.logP";
try {
ofstream outProb;
outProb.exceptions(ios::failbit | ios::badbit);
outProb.open(outProb_name.c_str());
outProb.precision(10);
outProb << prob << endl;
outProb.close();
} catch (ios::failure) {
outError(ERR_WRITE_OUTPUT, outProb_name);
}
cout << "Information about patterns in the input alignment is printed to: " << outFre_name << endl;
cout << "A 'guided bootstrap' alignment is printed to: " << gboAln_name << endl;
cout << "Log of the probability of the new alignment is printed to: " << outProb_name << endl;
}
/**MINH ANH: to compute the probability of an alignment given the multinomial distribution of patterns frequencies derived from a reference alignment*/
void computeMulProb(Params ¶ms)
{
Alignment refAlign(params.second_align, params.sequence_type, params.intype);
Alignment inputAlign(params.aln_file, params.sequence_type, params.intype);
double prob;
inputAlign.multinomialProb(refAlign,prob);
//Printing
string outProb_name = params.out_prefix;
outProb_name += ".mprob";
try {
ofstream outProb;
outProb.exceptions(ios::failbit | ios::badbit);
outProb.open(outProb_name.c_str());
outProb.precision(10);
outProb << prob << endl;
outProb.close();
} catch (ios::failure) {
outError(ERR_WRITE_OUTPUT, outProb_name);
}
cout << "Probability of alignment " << params.aln_file << " given alignment " << params.second_align << " is: " << prob << endl;
cout << "The probability is printed to: " << outProb_name << endl;
}
void processNCBITree(Params ¶ms) {
NCBITree tree;
Node *dad = tree.readNCBITree(params.user_file, params.ncbi_taxid, params.ncbi_taxon_level, params.ncbi_ignore_level);
if (params.ncbi_names_file) tree.readNCBINames(params.ncbi_names_file);
cout << "Dad ID: " << dad->name << " Root ID: " << tree.root->name << endl;
string str = params.user_file;
str += ".tree";
if (params.out_file) str = params.out_file;
//tree.printTree(str.c_str(), WT_SORT_TAXA | WT_BR_LEN);
cout << "NCBI tree printed to " << str << endl;
try {
ofstream out;
out.exceptions(ios::failbit | ios::badbit);
out.open(str.c_str());
tree.printTree(out, WT_SORT_TAXA | WT_BR_LEN | WT_TAXON_ID, tree.root, dad);
out << ";" << endl;
out.close();
} catch (ios::failure) {
outError(ERR_WRITE_OUTPUT, str);
}
}
/* write simultaneously to cout/cerr and a file */
class outstreambuf : public streambuf {
public:
outstreambuf* open( const char* name, ios::openmode mode = ios::out);
outstreambuf* close();
~outstreambuf() { close(); }
streambuf *get_fout_buf() {
return fout_buf;
}
ofstream *get_fout() {
return &fout;
}
protected:
ofstream fout;
streambuf *cout_buf;
streambuf *fout_buf;
virtual int overflow( int c = EOF);
virtual int sync();
};
outstreambuf* outstreambuf::open( const char* name, ios::openmode mode) {
if (!(Params::getInstance().suppress_output_flags & OUT_LOG) && MPIHelper::getInstance().isMaster()) {
fout.open(name, mode);
if (!fout.is_open()) {
cout << "Could not open " << name << " for logging" << endl;
return NULL;
}
fout_buf = fout.rdbuf();
}
cout_buf = cout.rdbuf();
cout.rdbuf(this);
return this;
}
outstreambuf* outstreambuf::close() {
cout.rdbuf(cout_buf);
if ( fout.is_open()) {
sync();
fout.close();
return this;
}
return NULL;
}
int outstreambuf::overflow( int c) { // used for output buffer only
if ((verbose_mode >= VB_MIN && MPIHelper::getInstance().isMaster()) || verbose_mode >= VB_MED)
if (cout_buf->sputc(c) == EOF) return EOF;
if (Params::getInstance().suppress_output_flags & OUT_LOG)
return c;
if (!MPIHelper::getInstance().isMaster())
return c;
if (fout_buf->sputc(c) == EOF) return EOF;
return c;
}
int outstreambuf::sync() { // used for output buffer only
if ((verbose_mode >= VB_MIN && MPIHelper::getInstance().isMaster()) || verbose_mode >= VB_MED)
cout_buf->pubsync();
if ((Params::getInstance().suppress_output_flags & OUT_LOG) || !MPIHelper::getInstance().isMaster())
return 0;
return fout_buf->pubsync();
}
class errstreambuf : public streambuf {
public:
void init(streambuf *fout_buf) {
this->fout_buf = fout_buf;
cerr_buf = cerr.rdbuf();
cerr.rdbuf(this);
}
~errstreambuf() {
cerr.rdbuf(cerr_buf);
}
protected:
streambuf *cerr_buf;
streambuf *fout_buf;
virtual int overflow( int c = EOF) {
if (cerr_buf->sputc(c) == EOF) return EOF;
if ((Params::getInstance().suppress_output_flags & OUT_LOG))
return c;
if (fout_buf->sputc(c) == EOF) return EOF;
return c;
}
virtual int sync() {
cerr_buf->pubsync();
if (Params::getInstance().suppress_output_flags & OUT_LOG)
return 0;
return fout_buf->pubsync();
}
};
/*********************************************************************************
* GLOBAL VARIABLES
*********************************************************************************/
outstreambuf _out_buf;
errstreambuf _err_buf;
string _log_file;
int _exit_wait_optn = FALSE;
extern "C" void startLogFile(bool append_log) {
if (append_log)
_out_buf.open(_log_file.c_str(), ios::app);
else
_out_buf.open(_log_file.c_str());
_err_buf.init(_out_buf.get_fout_buf());
}
extern "C" void endLogFile() {
_out_buf.close();
}
void funcExit(void) {
if(_exit_wait_optn) {
printf("\npress [return] to finish: ");
fflush(stdout);
while (getchar() != '\n');
}
endLogFile();
}
extern "C" void funcAbort(int signal_number)
{
/*Your code goes here. You can output debugging info.
If you return from this function, and it was called
because abort() was called, your program will exit or crash anyway
(with a dialog box on Windows).
*/
#if (defined(__GNUC__) || defined(__clang__)) && !defined(WIN32) && !defined(__CYGWIN__)
print_stacktrace(cerr);
#endif
cerr << endl << "*** IQ-TREE CRASHES WITH SIGNAL ";
switch (signal_number) {
case SIGABRT: cerr << "ABORTED"; break;
case SIGFPE: cerr << "ERRONEOUS NUMERIC"; break;
case SIGILL: cerr << "ILLEGAL INSTRUCTION"; break;
case SIGSEGV: cerr << "SEGMENTATION FAULT"; break;
#if !defined WIN32 && !defined _WIN32 && !defined __WIN32__
case SIGBUS: cerr << "BUS ERROR"; break;
#endif
}
cerr << endl;
cerr << "*** For bug report please send to developers:" << endl << "*** Log file: " << _log_file;
cerr << endl << "*** Alignment files (if possible)" << endl;
funcExit();
signal(signal_number, SIG_DFL);
}
extern "C" void getintargv(int *argc, char **argv[])
{
int done;
int count;
int n;
int l;
char ch;
char *argtmp;
char **argstr;
argtmp = (char *)calloc(10100, sizeof(char));
argstr = (char **)calloc(100, sizeof(char*));
for(n=0; n<100; n++) {
argstr[n] = &(argtmp[n * 100]);
}
n=1;
fprintf(stdout, "\nYou seem to have click-started this program,");
fprintf(stdout, "\ndo you want to enter commandline parameters: [y]es, [n]o: ");
fflush(stdout);
/* read one char */
ch = getc(stdin);
if (ch != '\n') {
do ;
while (getc(stdin) != '\n');
}
ch = (char) tolower((int) ch);
if (ch == 'y') {
done=FALSE;
fprintf(stdout, "\nEnter single parameter [! for none]: ");
fflush(stdout);
count = fscanf(stdin, "%s", argstr[n]);
do ;
while (getc(stdin) != '\n');
if(argstr[0][0] == '!') {
count = 0;
} else {
if (strlen(argstr[n]) > 100) {
fprintf(stdout, "\nParameter too long!!!\n");
} else {
n++;
}
}
while(!done) {
fprintf(stdout, "\nCurrent commandline: ");
for(l=1; l<n; l++) {
fprintf(stdout, "%s ", argstr[l]);
}
fprintf(stdout, "\nQuit [q]; confirm [y]%s%s%s: ",
(n<99 ? ", extend [e]" : ""),
(n>1 ? ", delete last [l]" : ""),
(n>1 ? ", delete all [a]" : ""));
fflush(stdout);
/* read one char */
ch = getc(stdin);
/* ch = getchar(); */
if (ch != '\n') {
do ;
while (getc(stdin) != '\n');
/* while (getchar() != '\n'); */
}
ch = (char) tolower((int) ch);
switch (ch) {
case 'y':
done=TRUE;
break;
case 'e':
fprintf(stdout, "\nEnter single parameter [! for none]: ");
fflush(stdout);
count = fscanf(stdin, "%s", argstr[n]);
do ;
while (getc(stdin) != '\n');
if(argstr[0][0] == '!') {
count = 0;
} else {
if (strlen(argstr[n]) > 100) {
fprintf(stdout, "\nParameter too long!!!\n");
} else {
n++;
}
}
break;
case 'l':
if (n>1) n--;
break;
case 'a':
n=1;
break;
case 'q':
// tp_exit(0, NULL, FALSE, __FILE__, __LINE__, _exit_wait_optn);
if(_exit_wait_optn) {
printf("\npress [return] to finish: ");
fflush(stdout);
while (getchar() != '\n');
}
exit(0);
break;
}
}
}
*argc = n;
*argv = argstr;
} /* getintargv */
/*********************************************************************************************************************************
Olga: ECOpd - phylogenetic diversity with ecological constraint: choosing a viable subset of species which maximizes PD/SD
*********************************************************************************************************************************/
void processECOpd(Params ¶ms) {
double startTime = getCPUTime();
params.detected_mode = LINEAR_PROGRAMMING;
cout<<"----------------------------------------------------------------------------------------"<<endl;
int i;
double score;
double *variables;
int threads = params.gurobi_threads;
params.gurobi_format=true;
string model_file,subFoodWeb,outFile;
model_file = params.out_prefix;
model_file += ".lp";
subFoodWeb = params.out_prefix;
subFoodWeb += ".subFoodWeb";
outFile = params.out_prefix;
outFile += ".pda";
//Defining the input phylo type: t - rooted/unrooted tree, n - split network
params.intype=detectInputFile(params.user_file);
if(params.intype == IN_NEWICK){
params.eco_type = "t";
} else if(params.intype == IN_NEXUS){
params.eco_type = "n";
}
// Checking whether to treat the food web as weighted or non weighted
if(params.diet_max == 0){
params.eco_weighted = false;
}else if(params.diet_max > 100 || params.diet_max < 0){
cout<<"The minimum percentage of the diet to be conserved for each predator"<<endl;
cout<<"d = "<<params.diet_max<<endl;
cout<<"ERROR: Wrong value of parameter d. It must be within the range 0 <= d <= 100"<<endl;
exit(0);
}else{
params.eco_weighted = true;
}
if(strcmp(params.eco_type,"t")==0){
/*--------------------------------- EcoPD Trees ---------------------------------*/
ECOpd tree(params.user_file,params.is_rooted);
// Setting all the information-----------------
tree.phyloType = "t";
tree.TaxaNUM = tree.leafNum;
if(verbose_mode == VB_MAX){
cout<<"TaxaNUM = "<<tree.TaxaNUM<<endl;
cout<<"LeafNUM = "<<tree.leafNum<<endl;
cout<<"root_id = "<<tree.root->id<<" root_name = "<<tree.root->name<<endl;
for(i=0; i<tree.leafNum; i++){
cout<<i<<" "<<tree.findNodeID(i)->name <<endl;
}
}
//Getting Species Names from tree
for(i = 0; i < tree.TaxaNUM; i++)
(tree.phyloNames).push_back(tree.findNodeID(i)->name);
//for(i=0;i<tree.phyloNames.size();i++)
// cout<<"["<<i<<"] "<<tree.phyloNames[i]<<endl;
// Full species list including info from tree and food web. Here adding names from phyloInput.
for(i=0; i<tree.TaxaNUM; i++)
tree.names.push_back(&(tree.phyloNames[i]));
// Read the taxa to be included in the final optimal subset
if(params.initial_file)
tree.readInitialTaxa(params.initial_file);
// Read the DAG file, Synchronize species on the Tree and in the Food Web
tree.weighted = params.eco_weighted;
tree.T = params.diet_max*0.01;
tree.readDAG(params.eco_dag_file);
tree.defineK(params);
// IP formulation
cout<<"Formulating an IP problem..."<<endl;
if(tree.rooted){
tree.printECOlpRooted(model_file.c_str(),tree);
} else {
tree.printECOlpUnrooted(model_file.c_str(),tree);
}
// Solve IP problem
cout<<"Solving the problem..."<<endl;
variables = new double[tree.nvar];
int g_return = gurobi_solve((char*)model_file.c_str(), tree.nvar, &score, variables, verbose_mode, threads);
if(verbose_mode == VB_MAX){
cout<<"GUROBI finished with "<<g_return<<" return."<<endl;
for(i=0; i<tree.nvar; i++)
cout<<"x"<<i<<" = "<<variables[i]<<endl;
cout<<"score = "<<score<<endl;
}
tree.dietConserved(variables);
params.run_time = getCPUTime() - startTime;
tree.printResults((char*)outFile.c_str(),variables,score,params);
tree.printSubFoodWeb((char*)subFoodWeb.c_str(),variables);
delete[] variables;
} else if(strcmp(params.eco_type,"n")==0){
/*----------------------------- EcoPD SplitNetwork ------------------------------*/
params.intype=detectInputFile(params.user_file);
PDNetwork splitSYS(params);
ECOpd ecoInfDAG;
// Get the species names from SplitNetwork
splitSYS.speciesList(&(ecoInfDAG.phyloNames));
//for(i=0;i<ecoInfDAG.phyloNames.size();i++)
// cout<<"["<<i<<"] "<<ecoInfDAG.phyloNames[i]<<endl;
ecoInfDAG.phyloType = "n";
ecoInfDAG.TaxaNUM = splitSYS.getNTaxa();
// Full species list including info from tree and food web
for(i=0; i<ecoInfDAG.TaxaNUM; i++)
ecoInfDAG.names.push_back(&(ecoInfDAG.phyloNames[i]));
ecoInfDAG.weighted = params.eco_weighted;
// Read the taxa to be included in the final optimal subset
if(params.initial_file)
ecoInfDAG.readInitialTaxa(params.initial_file);
ecoInfDAG.T = params.diet_max*0.01;
ecoInfDAG.readDAG(params.eco_dag_file);
ecoInfDAG.defineK(params);
cout<<"Formulating an IP problem..."<<endl;
splitSYS.transformEcoLP(params, model_file.c_str(), 0);
/**
* (subset_size-4) - influences constraints for conserved splits.
* should be less than taxaNUM in the split system.
* With 0 prints all the constraints.
* Values different of 0 reduce the # of constraints.
**/
ecoInfDAG.printInfDAG(model_file.c_str(),splitSYS,params);
cout<<"Solving the problem..."<<endl;
variables = new double[ecoInfDAG.nvar];
int g_return = gurobi_solve((char*)model_file.c_str(), ecoInfDAG.nvar, &score, variables, verbose_mode, threads);
if(verbose_mode == VB_MAX){
cout<<"GUROBI finished with "<<g_return<<" return."<<endl;
for(i=0; i<ecoInfDAG.nvar; i++)
cout<<"x"<<i<<" = "<<variables[i]<<endl;
cout<<"score = "<<score<<endl;
}
ecoInfDAG.splitsNUM = splitSYS.getNSplits();
ecoInfDAG.totalSD = splitSYS.calcWeight();
ecoInfDAG.dietConserved(variables);
params.run_time = getCPUTime() - startTime;
ecoInfDAG.printResults((char*)outFile.c_str(),variables, score,params);
ecoInfDAG.printSubFoodWeb((char*)subFoodWeb.c_str(),variables);
delete[] variables;
}
}
void collapseLowBranchSupport(char *user_file, char *split_threshold_str) {
DoubleVector minsup;
convert_double_vec(split_threshold_str, minsup, '/');
if (minsup.empty())
outError("wrong -minsupnew argument, please use back-slash separated string");
MExtTree tree;
bool isrooted = false;
tree.readTree(user_file, isrooted);
tree.collapseLowBranchSupport(minsup);
tree.collapseZeroBranches();
if (verbose_mode >= VB_MED)
tree.drawTree(cout);
string outfile = (string)user_file + ".collapsed";
tree.printTree(outfile.c_str());
cout << "Tree with collapsed branches written to " << outfile << endl;
}
/********************************************************
main function
********************************************************/
/*
int main(){
IQTree tree;
char * str = "(1, (2, 345));";
string k;
tree.pllConvertTaxaID2IQTreeForm(str, k);
cout << str << endl;
cout << k << endl;
cout << "WHAT" << endl;
return 0;
}
*/
/*
Instruction set ID reported by vectorclass::instrset_detect
0 = 80386 instruction set
1 or above = SSE (XMM) supported by CPU (not testing for O.S. support)
2 or above = SSE2
3 or above = SSE3
4 or above = Supplementary SSE3 (SSSE3)
5 or above = SSE4.1
6 or above = SSE4.2
7 or above = AVX supported by CPU and operating system
8 or above = AVX2
9 or above = AVX512F
*/
int instruction_set;
int main(int argc, char *argv[]) {
#ifdef _IQTREE_MPI
double time_initial, time_current;
int n_tasks, task_id;
if (MPI_Init(&argc, &argv) != MPI_SUCCESS) {
outError("MPI initialization failed!");
}
MPI_Comm_size(MPI_COMM_WORLD, &n_tasks);
MPI_Comm_rank(MPI_COMM_WORLD, &task_id);
MPIHelper::getInstance().setNumProcesses(n_tasks);
MPIHelper::getInstance().setProcessID(task_id);
MPIHelper::getInstance().setNumTreeReceived(0);
MPIHelper::getInstance().setNumTreeSent(0);
MPIHelper::getInstance().setNumNNISearch(0);
#endif
/*************************/
{ /* local scope */
int found = FALSE; /* "click" found in cmd name? */
int n, dummyint;
char *tmpstr;
int intargc;
char **intargv;
intargc = 0;
intargv = NULL;
for (n = strlen(argv[0]) - 5;
(n >= 0) && !found && (argv[0][n] != '/')
&& (argv[0][n] != '\\'); n--) {
tmpstr = &(argv[0][n]);
dummyint = 0;
(void) sscanf(tmpstr, "click%n", &dummyint);
if (dummyint == 5) found = TRUE;
else {
dummyint = 0;
(void) sscanf(tmpstr, "CLICK%n", &dummyint);
if (dummyint == 5) found = TRUE;
else {
dummyint = 0;
(void) sscanf(tmpstr, "Click%n", &dummyint);
if (dummyint == 5) found = TRUE;
}
}
}
if (found) _exit_wait_optn = TRUE;
if (_exit_wait_optn) { // get commandline parameters from keyboard
getintargv(&intargc, &intargv);
fprintf(stdout, "\n\n");
if (intargc > 1) { // if there were option entered, use them as argc/argv
argc = intargc;
argv = intargv;
}
}
} /* local scope */
/*************************/
parseArg(argc, argv, Params::getInstance());
// 2015-12-05
Checkpoint *checkpoint = new Checkpoint;
string filename = (string)Params::getInstance().out_prefix +".ckp.gz";
checkpoint->setFileName(filename);
bool append_log = false;
if (!Params::getInstance().ignore_checkpoint && fileExists(filename)) {
checkpoint->load();
if (checkpoint->hasKey("finished")) {
if (checkpoint->getBool("finished")) {
if (Params::getInstance().force_unfinished) {
cout << "NOTE: Continue analysis although a previous run already finished" << endl;
} else {
outError("Checkpoint (" + filename + ") indicates that a previous run successfully finished\n" +
"Use `-redo` option if you really want to redo the analysis and overwrite all output files.");
delete checkpoint;
return EXIT_FAILURE;
}
} else {
append_log = true;
}
} else {
outWarning("Ignore invalid checkpoint file " + filename);
checkpoint->clear();
}
}
// after loading, workers are not allowed to write checkpoint anymore
if (MPIHelper::getInstance().isWorker())
checkpoint->setFileName("");
_log_file = Params::getInstance().out_prefix;
_log_file += ".log";
startLogFile(append_log);
time_t start_time;
if (append_log) {
cout << endl << "******************************************************"
<< endl << "CHECKPOINT: Resuming analysis from " << filename << endl << endl;
}
#ifdef _IQTREE_MPI
cout << "************************************************" << endl;
cout << "* START TREE SEARCH USING MPI WITH " << MPIHelper::getInstance().getNumProcesses() << " PROCESSES *" << endl;
cout << "************************************************" << endl;
unsigned int rndSeed;
if (MPIHelper::getInstance().isMaster()) {
rndSeed = Params::getInstance().ran_seed;
cout << "Random seed of master = " << rndSeed << endl;
}
// Broadcast random seed
MPI_Bcast(&rndSeed, 1, MPI_INT, PROC_MASTER, MPI_COMM_WORLD);
if (MPIHelper::getInstance().isWorker()) {
// Params::getInstance().ran_seed = rndSeed + task_id * 100000;
Params::getInstance().ran_seed = rndSeed;
// printf("Process %d: random_seed = %d\n", task_id, Params::getInstance().ran_seed);
}
#endif
atexit(funcExit);
signal(SIGABRT, &funcAbort);
signal(SIGFPE, &funcAbort);
signal(SIGILL, &funcAbort);
signal(SIGSEGV, &funcAbort);
#if !defined WIN32 && !defined _WIN32 && !defined __WIN32__
signal(SIGBUS, &funcAbort);
#endif
printCopyright(cout);
/*
double x=1e-100;
double y=1e-101;
if (x > y) cout << "ok!" << endl;
else cout << "shit!" << endl;
*/
//FILE *pfile = popen("hostname","r");
char hostname[100];
#if defined WIN32 || defined _WIN32 || defined __WIN32__
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
gethostname(hostname, sizeof(hostname));
WSACleanup();
#else
gethostname(hostname, sizeof(hostname));
#endif
//fgets(hostname, sizeof(hostname), pfile);
//pclose(pfile);
instruction_set = instrset_detect();
#if defined(BINARY32) || defined(__NOAVX__)
instruction_set = min(instruction_set, 6);
#endif
if (instruction_set < 3) outError("Your CPU does not support SSE3!");
bool has_fma3 = (instruction_set >= 7) && hasFMA3();
// bool has_fma4 = (instruction_set >= 7) && hasFMA4();
#ifdef __FMA__
bool has_fma = has_fma3;
if (!has_fma) {
outError("Your CPU does not support FMA instruction, quiting now...");
}
#endif
cout << "Host: " << hostname << " (";
switch (instruction_set) {
case 0: cout << "80386, "; break;
case 1: cout << "SSE, "; break;
case 2: cout << "SSE2, "; break;
case 3: cout << "SSE3, "; break;
case 4: cout << "SSSE3, "; break;
case 5: cout << "SSE4.1, "; break;
case 6: cout << "SSE4.2, "; break;
case 7: cout << "AVX, "; break;
case 8: cout << "AVX2, "; break;
default: cout << "AVX512, "; break;
}
if (has_fma3) cout << "FMA3, ";
// if (has_fma4) cout << "FMA4, ";
//#if defined __APPLE__ || defined __MACH__
cout << (int)(((getMemorySize()/1024.0)/1024)/1024) << " GB RAM)" << endl;
//#else
// cout << (int)(((getMemorySize()/1000.0)/1000)/1000) << " GB RAM)" << endl;
//#endif
cout << "Command:";
int i;
for (i = 0; i < argc; i++)
cout << " " << argv[i];
cout << endl;
checkpoint->get("iqtree.seed", Params::getInstance().ran_seed);
cout << "Seed: " << Params::getInstance().ran_seed << " ";
init_random(Params::getInstance().ran_seed + MPIHelper::getInstance().getProcessID(), true);
time(&start_time);
cout << "Time: " << ctime(&start_time);
if (Params::getInstance().lk_no_avx == 1)
instruction_set = min(instruction_set, 6);
cout << "Kernel: ";
if (Params::getInstance().lk_safe_scaling) {
cout << "Safe ";
}
if (Params::getInstance().pll) {
#ifdef __AVX__
cout << "PLL-AVX";
#else
cout << "PLL-SSE3";
#endif
} else {
bool has_fma = (has_fma3) && (instruction_set >= 7) && Params::getInstance().lk_no_avx != 2;
switch (Params::getInstance().SSE) {
case LK_EIGEN: cout << "No SSE"; break;
case LK_EIGEN_SSE:
if (has_fma) {
cout << "AVX+FMA";
} else if (instruction_set >= 7) {
cout << "AVX";
} else {
cout << "SSE3";
}
#ifdef __FMA__
cout << "+FMA";
#endif
break;
}
}
#ifdef _OPENMP
if (Params::getInstance().num_threads < 0) {
cout << endl << endl;
outError("Please specify number of cores via -nt option. Use '-nt AUTO' to automatically determine the best number of cores");
}
if (Params::getInstance().num_threads >= 1) {
omp_set_num_threads(Params::getInstance().num_threads);
Params::getInstance().num_threads = omp_get_max_threads();
}
// int max_threads = omp_get_max_threads();
int max_procs = countPhysicalCPUCores();
cout << " - ";
if (Params::getInstance().num_threads > 0)
cout << Params::getInstance().num_threads << " threads";
else
cout << "auto-detect";
cout << "(" << max_procs << " CPU cores detected)";
if (Params::getInstance().num_threads > max_procs) {
cout << endl;
outError("You have specified more threads than CPU cores available");
}
omp_set_nested(false); // don't allow nested OpenMP parallelism
#else
if (Params::getInstance().num_threads != 1) {
cout << endl << endl;
outError("Number of threads must be 1 for sequential version.");
}
int num_procs = countPhysicalCPUCores();
#ifndef _IQTREE_MPI
if (num_procs > 1) {
cout << endl << endl << "NOTE: Consider using the multicore version because your CPU has " << num_procs << " cores!";
}
#endif
#endif
//cout << "sizeof(int)=" << sizeof(int) << endl;
cout << endl << endl;
cout.precision(3);
cout.setf(ios::fixed);
// checkpoint general run information
checkpoint->startStruct("iqtree");
string command;
if (CKP_RESTORE_STRING(command)) {
// compare command between saved and current commands
stringstream ss(command);
string str;
bool mismatch = false;
for (i = 1; i < argc; i++) {
if (!(ss >> str)) {
outWarning("Number of command-line arguments differs from checkpoint");
mismatch = true;
break;
}
if (str != argv[i]) {
outWarning((string)"Command-line argument `" + argv[i] + "` differs from checkpoint `" + str + "`");
mismatch = true;
}
}
if (mismatch) {
outWarning("Command-line differs from checkpoint!");
}
command = "";
}
for (i = 1; i < argc; i++)
command += string(" ") + argv[i];
CKP_SAVE(command);
int seed = Params::getInstance().ran_seed;
CKP_SAVE(seed);
CKP_SAVE(start_time);
stringstream sversion;
sversion << iqtree_VERSION_MAJOR << "." << iqtree_VERSION_MINOR << "." << iqtree_VERSION_PATCH;
string version = sversion.str();
CKP_SAVE(version);
checkpoint->endStruct();
if (MPIHelper::getInstance().getNumProcesses() > 1) {
if (Params::getInstance().aln_file || Params::getInstance().partition_file) {
runPhyloAnalysis(Params::getInstance(), checkpoint);
} else {
outError("Please use one MPI process! The feature you wanted does not need parallelization.");
}
} else
// call the main function
if (Params::getInstance().tree_gen != NONE) {
generateRandomTree(Params::getInstance());
} else if (Params::getInstance().do_pars_multistate) {
doParsMultiState(Params::getInstance());
} else if (Params::getInstance().rf_dist_mode != 0) {
computeRFDist(Params::getInstance());
} else if (Params::getInstance().test_input != TEST_NONE) {
Params::getInstance().intype = detectInputFile(Params::getInstance().user_file);
testInputFile(Params::getInstance());
} else if (Params::getInstance().run_mode == PRINT_TAXA) {
printTaxa(Params::getInstance());
} else if (Params::getInstance().run_mode == PRINT_AREA) {
printAreaList(Params::getInstance());
} else if (Params::getInstance().run_mode == SCALE_BRANCH_LEN || Params::getInstance().run_mode == SCALE_NODE_NAME) {
scaleBranchLength(Params::getInstance());
} else if (Params::getInstance().run_mode == PD_DISTRIBUTION) {
calcDistribution(Params::getInstance());
} else if (Params::getInstance().run_mode == STATS){ /**MINH ANH: for some statistics on the input tree*/
branchStats(Params::getInstance()); // MA
} else if (Params::getInstance().branch_cluster > 0) {
calcTreeCluster(Params::getInstance());
} else if (Params::getInstance().ncbi_taxid) {
processNCBITree(Params::getInstance());
} else if (Params::getInstance().user_file && Params::getInstance().eco_dag_file) { /**ECOpd analysis*/
processECOpd(Params::getInstance());
} else if (Params::getInstance().aln_file || Params::getInstance().partition_file) {
if ((Params::getInstance().siteLL_file || Params::getInstance().second_align) && !Params::getInstance().gbo_replicates)
{
if (Params::getInstance().siteLL_file)
guidedBootstrap(Params::getInstance());
if (Params::getInstance().second_align)
computeMulProb(Params::getInstance());
} else {
runPhyloAnalysis(Params::getInstance(), checkpoint);
}
} else if (Params::getInstance().ngs_file || Params::getInstance().ngs_mapped_reads) {
runNGSAnalysis(Params::getInstance());
} else if (Params::getInstance().pdtaxa_file && Params::getInstance().gene_scale_factor >=0.0 && Params::getInstance().gene_pvalue_file) {
runGSSAnalysis(Params::getInstance());
} else if (Params::getInstance().consensus_type != CT_NONE) {
MExtTree tree;
switch (Params::getInstance().consensus_type) {
case CT_CONSENSUS_TREE:
computeConsensusTree(Params::getInstance().user_file, Params::getInstance().tree_burnin, Params::getInstance().tree_max_count, Params::getInstance().split_threshold,
Params::getInstance().split_weight_threshold, Params::getInstance().out_file, Params::getInstance().out_prefix, Params::getInstance().tree_weight_file, &Params::getInstance());
break;
case CT_CONSENSUS_NETWORK:
computeConsensusNetwork(Params::getInstance().user_file, Params::getInstance().tree_burnin, Params::getInstance().tree_max_count, Params::getInstance().split_threshold,
Params::getInstance().split_weight_summary, Params::getInstance().split_weight_threshold, Params::getInstance().out_file, Params::getInstance().out_prefix, Params::getInstance().tree_weight_file);
break;
case CT_ASSIGN_SUPPORT:
assignBootstrapSupport(Params::getInstance().user_file, Params::getInstance().tree_burnin, Params::getInstance().tree_max_count,
Params::getInstance().second_tree, Params::getInstance().is_rooted, Params::getInstance().out_file,
Params::getInstance().out_prefix, tree, Params::getInstance().tree_weight_file, &Params::getInstance());
break;
case CT_ASSIGN_SUPPORT_EXTENDED:
assignBranchSupportNew(Params::getInstance());
break;
case CT_NONE: break;
/**MINH ANH: for some comparison*/
case COMPARE: compare(Params::getInstance()); break; //MA
}
} else if (Params::getInstance().split_threshold_str) {
// for Ricardo: keep those splits from input tree above given support threshold
collapseLowBranchSupport(Params::getInstance().user_file, Params::getInstance().split_threshold_str);
} else {
Params::getInstance().intype = detectInputFile(Params::getInstance().user_file);
if (Params::getInstance().intype == IN_NEWICK && Params::getInstance().pdtaxa_file && Params::getInstance().tree_gen == NONE) {
if (Params::getInstance().budget_file) {
//if (Params::getInstance().budget < 0) Params::getInstance().run_mode = PD_USER_SET;
} else {
if (Params::getInstance().sub_size < 1 && Params::getInstance().pd_proportion == 0.0)
Params::getInstance().run_mode = PD_USER_SET;
}
// input is a tree, check if it is a reserve selection -> convert to splits
if (Params::getInstance().run_mode != PD_USER_SET) Params::getInstance().multi_tree = true;
}
if (Params::getInstance().intype == IN_NEWICK && !Params::getInstance().find_all && Params::getInstance().budget_file == NULL &&
Params::getInstance().find_pd_min == false && Params::getInstance().calc_pdgain == false &&
Params::getInstance().run_mode != LINEAR_PROGRAMMING && Params::getInstance().multi_tree == false)
runPDTree(Params::getInstance());
else if (Params::getInstance().intype == IN_NEXUS || Params::getInstance().intype == IN_NEWICK) {
if (Params::getInstance().run_mode == LINEAR_PROGRAMMING && Params::getInstance().find_pd_min)
outError("Current linear programming does not support finding minimal PD sets!");
if (Params::getInstance().find_all && Params::getInstance().run_mode == LINEAR_PROGRAMMING)
Params::getInstance().binary_programming = true;
runPDSplit(Params::getInstance());
} else {
outError("Unknown file input format");
}
}
time(&start_time);
cout << "Date and Time: " << ctime(&start_time);
delete checkpoint;
finish_random();
#ifdef _IQTREE_MPI
MPI_Finalize();
#endif
return EXIT_SUCCESS;
}
|