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
|
/*
* clustersplitcommand.cpp
* Mothur
*
* Created by westcott on 5/19/10.
* Copyright 2010 Schloss Lab. All rights reserved.
*
*/
#include "clustersplitcommand.h"
#include "systemcommand.h"
#include "sensspeccommand.h"
#include "mcc.hpp"
#include "sensitivity.hpp"
#include "specificity.hpp"
#include "fdr.hpp"
#include "npv.hpp"
#include "ppv.hpp"
#include "f1score.hpp"
#include "tp.hpp"
#include "fp.hpp"
#include "fpfn.hpp"
#include "tptn.hpp"
#include "tn.hpp"
#include "fn.hpp"
#include "accuracy.hpp"
//**********************************************************************************************************************
vector<string> ClusterSplitCommand::setParameters(){
try {
CommandParameter pfile("file", "InputTypes", "", "", "PhylipColumnFasta", "PhylipColumnFasta", "none","",false,false,true); parameters.push_back(pfile);
CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "none", "FastaTaxName","",false,false,true); parameters.push_back(ptaxonomy);
CommandParameter pphylip("phylip", "InputTypes", "", "", "PhylipColumnFasta", "PhylipColumnFasta", "none","list",false,false,true); parameters.push_back(pphylip);
CommandParameter pfasta("fasta", "InputTypes", "", "", "PhylipColumnFasta", "PhylipColumnFasta", "FastaTaxName","list",false,false,true); parameters.push_back(pfasta);
CommandParameter pname("name", "InputTypes", "", "", "NameCount", "none", "ColumnName-FastaTaxName","rabund-sabund",false,false,true); parameters.push_back(pname);
CommandParameter pcount("count", "InputTypes", "", "", "NameCount", "none", "","",false,false,true); parameters.push_back(pcount);
CommandParameter pcolumn("column", "InputTypes", "", "", "PhylipColumnFasta", "PhylipColumnFasta", "ColumnName","list",false,false,true); parameters.push_back(pcolumn);
CommandParameter ptaxlevel("taxlevel", "Number", "", "3", "", "", "","",false,false,true); parameters.push_back(ptaxlevel);
CommandParameter psplitmethod("splitmethod", "Multiple", "classify-fasta-distance", "distance", "", "", "","",false,false,true); parameters.push_back(psplitmethod);
CommandParameter plarge("large", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(plarge);
CommandParameter pshowabund("showabund", "Boolean", "", "T", "", "", "","",false,false); parameters.push_back(pshowabund);
CommandParameter prunspenspec("runsensspec", "Boolean", "", "T", "", "", "","",false,false); parameters.push_back(prunspenspec);
CommandParameter pcluster("cluster", "Boolean", "", "T", "", "", "","",false,false); parameters.push_back(pcluster);
CommandParameter ptiming("timing", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(ptiming);
CommandParameter pprocessors("processors", "Number", "", "1", "", "", "","",false,false,true); parameters.push_back(pprocessors);
CommandParameter pcutoff("cutoff", "Number", "", "0.03", "", "", "","",false,false,true); parameters.push_back(pcutoff);
CommandParameter pmetriccutoff("delta", "Number", "", "0.0001", "", "", "","",false,false,true); parameters.push_back(pmetriccutoff);
CommandParameter piters("iters", "Number", "", "100", "", "", "","",false,false,true); parameters.push_back(piters);
CommandParameter pinitialize("initialize", "Multiple", "oneotu-singleton", "singleton", "", "", "","",false,false,true); parameters.push_back(pinitialize);
CommandParameter pprecision("precision", "Number", "", "100", "", "", "","",false,false); parameters.push_back(pprecision);
CommandParameter pmethod("method", "Multiple", "furthest-nearest-average-weighted-agc-dgc-opti", "opti", "", "", "","",false,false,true); parameters.push_back(pmethod);
CommandParameter pmetric("metric", "Multiple", "mcc-sens-spec-tptn-fpfn-tp-tn-fp-fn-f1score-accuracy-ppv-npv-fdr", "mcc", "", "", "","",false,false,true); parameters.push_back(pmetric);
CommandParameter pdist("dist", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(pdist);
CommandParameter pislist("islist", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(pislist);
CommandParameter pclassic("classic", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(pclassic);
CommandParameter pseed("seed", "Number", "", "0", "", "", "","",false,false); parameters.push_back(pseed);
CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
vector<string> myArray;
for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
return myArray;
}
catch(exception& e) {
m->errorOut(e, "ClusterSplitCommand", "setParameters");
exit(1);
}
}
//**********************************************************************************************************************
string ClusterSplitCommand::getHelpString(){
try {
string helpString = "";
helpString += "The cluster.split command parameter options are file, fasta, phylip, column, name, count, cutoff, precision, method, splitmethod, taxonomy, taxlevel, showabund, timing, large, cluster, iters, delta, initialize, dist, processors, runsensspec. Fasta or Phylip or column and name are required.\n";
helpString += "The cluster.split command can split your files in 3 ways. Splitting by distance file, by classification, or by classification also using a fasta file. \n";
helpString += "For the distance file method, you need only provide your distance file and mothur will split the file into distinct groups. \n";
helpString += "For the classification method, you need to provide your distance file and taxonomy file, and set the splitmethod to classify. \n";
helpString += "You will also need to set the taxlevel you want to split by. mothur will split the sequences into distinct taxonomy groups, and split the distance file based on those groups. \n";
helpString += "For the classification method using a fasta file, you need to provide your fasta file, names file and taxonomy file. \n";
helpString += "You will also need to set the taxlevel you want to split by. mothur will split the sequence into distinct taxonomy groups, and create distance files for each grouping. \n";
helpString += "The file option allows you to enter your file containing your list of column and names/count files as well as the singleton file. This file is mothur generated, when you run cluster.split() with the cluster=f parameter. This can be helpful when you have a large dataset that you may be able to use all your processors for the splitting step, but have to reduce them for the cluster step due to RAM constraints. For example: cluster.split(fasta=yourFasta, taxonomy=yourTax, count=yourCount, taxlevel=3, cluster=f, processors=8) then cluster.split(file=yourFile, processors=4). This allows your to maximize your processors during the splitting step. Also, if you are unsure if the cluster step will have RAM issue with multiple processors, you can avoid running the first part of the command multiple times.\n";
helpString += "The phylip and column parameter allow you to enter your distance file. \n";
helpString += "The fasta parameter allows you to enter your aligned fasta file. \n";
helpString += "The name parameter allows you to enter your name file. \n";
helpString += "The count parameter allows you to enter your count file. \n A count or name file is required if your distance file is in column format";
helpString += "The cluster parameter allows you to indicate whether you want to run the clustering or just split the distance matrix, default=t";
helpString += "The dist parameter allows you to indicate whether you want a column formatted distance matrix outputted along with the list file. Default=F.";
helpString += "The cutoff parameter allow you to set the distance you want to cluster to, default is 0.03. \n";
helpString += "The precision parameter allows you specify the precision of the precision of the distances outputted, default=100, meaning 2 decimal places. \n";
helpString += "The iters parameter allow you to set the maxiters for the opticluster method. \n";
helpString += "The metric parameter allows to select the metric in the opticluster method. Options are Matthews correlation coefficient (mcc), sensitivity (sens), specificity (spec), true positives + true negatives (tptn), false positives + false negatives (fpfn), true positives (tp), true negative (tn), false positive (fp), false negative (fn), f1score (f1score), accuracy (accuracy), positive predictive value (ppv), negative predictive value (npv), false discovery rate (fdr). Default=mcc.\n";
helpString += "The delta parameter allows to set the stable value for the metric in the opticluster method. Default=0.0001\n";
helpString += "The initialize parameter allows to select the initial randomization for the opticluster method. Options are singleton, meaning each sequence is randomly assigned to its own OTU, or oneotu meaning all sequences are assigned to one otu. Default=singleton.\n";
helpString += "The runsensspec parameter allows to run the sens.spec command on the completed list file. Default=true.\n";
helpString += "The method parameter allows you to enter your clustering mothod. Options are furthest, nearest, average, weighted, agc, dgc and opti. Default=opti. The agc and dgc methods require a fasta file.";
helpString += "The splitmethod parameter allows you to specify how you want to split your distance file before you cluster, default=distance, options distance, classify or fasta. \n";
helpString += "The taxonomy parameter allows you to enter the taxonomy file for your sequences, this is only valid if you are using splitmethod=classify. Be sure your taxonomy file does not include the probability scores. \n";
helpString += "The taxlevel parameter allows you to specify the taxonomy level you want to use to split the distance file, default=3, meaning use the first taxon in each list. \n";
helpString += "The large parameter allows you to indicate that your distance matrix is too large to fit in RAM. The default value is false.\n";
helpString += "The classic parameter allows you to indicate that you want to run your files with cluster.classic. It is only valid with splitmethod=fasta. Default=f.\n";
helpString += "The processors parameter allows you to specify the number of processors to use. The default is 1.\n";
helpString += "The cluster.split command should be in the following format: \n";
helpString += "cluster.split(column=youDistanceFile, name=yourNameFile, method=yourMethod, cutoff=yourCutoff, precision=yourPrecision, splitmethod=yourSplitmethod, taxonomy=yourTaxonomyfile, taxlevel=yourtaxlevel) \n";
helpString += "Example: cluster.split(column=abrecovery.dist, name=abrecovery.names, method=opti, cutoff=0.10, precision=1000, splitmethod=classify, taxonomy=abrecovery.silva.slv.taxonomy, taxlevel=5) \n";
return helpString;
}
catch(exception& e) {
m->errorOut(e, "ClusterSplitCommand", "getHelpString");
exit(1);
}
}
//**********************************************************************************************************************
string ClusterSplitCommand::getOutputPattern(string type) {
try {
string pattern = "";
if (type == "list") { pattern = "[filename],[clustertag],list-[filename],[clustertag],[tag2],list"; }
else if (type == "rabund") { pattern = "[filename],[clustertag],rabund"; }
else if (type == "sabund") { pattern = "[filename],[clustertag],sabund"; }
else if (type == "sensspec") { pattern = "[filename],[clustertag],sensspec"; }
else if (type == "column") { pattern = "[filename],dist"; }
else if (type == "file") { pattern = "[filename],file"; }
else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->setControl_pressed(true); }
return pattern;
}
catch(exception& e) {
m->errorOut(e, "ClusterSplitCommand", "getOutputPattern");
exit(1);
}
}
//**********************************************************************************************************************
ClusterSplitCommand::ClusterSplitCommand(){
try {
abort = true; calledHelp = true;
setParameters();
vector<string> tempOutNames;
outputTypes["list"] = tempOutNames;
outputTypes["rabund"] = tempOutNames;
outputTypes["sabund"] = tempOutNames;
outputTypes["column"] = tempOutNames;
outputTypes["name"] = tempOutNames;
outputTypes["file"] = tempOutNames;
outputTypes["sensspec"] = tempOutNames;
}
catch(exception& e) {
m->errorOut(e, "ClusterSplitCommand", "ClusterSplitCommand");
exit(1);
}
}
//**********************************************************************************************************************
//This function checks to make sure the cluster command has no errors and then clusters based on the method chosen.
ClusterSplitCommand::ClusterSplitCommand(string option) {
try{
abort = false; calledHelp = false;
format = "";
//allow user to run help
if(option == "help") { help(); abort = true; calledHelp = true; }
else if(option == "citation") { citation(); abort = true; calledHelp = true;}
else {
vector<string> myArray = setParameters();
OptionParser parser(option);
map<string,string> parameters = parser.getParameters();
ValidParameters validParameter("cluster.split");
//check to make sure all parameters are valid for command
map<string,string>::iterator it;
for (it = parameters.begin(); it != parameters.end(); it++) {
if (!validParameter.isValidParameter(it->first, myArray, it->second)) {
abort = true;
}
}
//initialize outputTypes
vector<string> tempOutNames;
outputTypes["list"] = tempOutNames;
outputTypes["rabund"] = tempOutNames;
outputTypes["sabund"] = tempOutNames;
outputTypes["column"] = tempOutNames;
outputTypes["file"] = tempOutNames;
outputTypes["sensspec"] = tempOutNames;
//if the user changes the output directory command factory will send this info to us in the output parameter
outputDir = validParameter.valid(parameters, "outputdir"); if (outputDir == "not found"){ outputDir = ""; }
//if the user changes the input directory command factory will send this info to us in the output parameter
inputDir = validParameter.valid(parameters, "inputdir");
if (inputDir == "not found"){ inputDir = ""; }
else {
string path;
it = parameters.find("phylip");
//user has given a template file
if(it != parameters.end()){
path = util.hasPath(it->second);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["phylip"] = inputDir + it->second; }
}
it = parameters.find("column");
//user has given a template file
if(it != parameters.end()){
path = util.hasPath(it->second);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["column"] = inputDir + it->second; }
}
it = parameters.find("name");
//user has given a template file
if(it != parameters.end()){
path = util.hasPath(it->second);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["name"] = inputDir + it->second; }
}
it = parameters.find("taxonomy");
//user has given a template file
if(it != parameters.end()){
path = util.hasPath(it->second);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
}
it = parameters.find("fasta");
//user has given a template file
if(it != parameters.end()){
path = util.hasPath(it->second);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["fasta"] = inputDir + it->second; }
}
it = parameters.find("count");
//user has given a template file
if(it != parameters.end()){
path = util.hasPath(it->second);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["count"] = inputDir + it->second; }
}
it = parameters.find("file");
//user has given a template file
if(it != parameters.end()){
path = util.hasPath(it->second);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["file"] = inputDir + it->second; }
}
}
//check for required parameters
file = validParameter.validFile(parameters, "file");
if (file == "not open") { file = ""; abort = true; }
else if (file == "not found") { file = ""; }
else { distfile = file; }
phylipfile = validParameter.validFile(parameters, "phylip");
if (phylipfile == "not open") { abort = true; }
else if (phylipfile == "not found") { phylipfile = ""; }
else { distfile = phylipfile; format = "phylip"; current->setPhylipFile(phylipfile); }
columnfile = validParameter.validFile(parameters, "column");
if (columnfile == "not open") { abort = true; }
else if (columnfile == "not found") { columnfile = ""; }
else { distfile = columnfile; format = "column"; current->setColumnFile(columnfile); }
namefile = validParameter.validFile(parameters, "name");
if (namefile == "not open") { abort = true; namefile = "";}
else if (namefile == "not found") { namefile = ""; }
else { current->setNameFile(namefile); }
countfile = validParameter.validFile(parameters, "count");
if (countfile == "not open") { abort = true; countfile = "";}
else if (countfile == "not found") { countfile = ""; }
else { current->setCountFile(countfile); }
fastafile = validParameter.validFile(parameters, "fasta");
if (fastafile == "not open") { abort = true; }
else if (fastafile == "not found") { fastafile = ""; }
else { distfile = fastafile; splitmethod = "fasta"; current->setFastaFile(fastafile); }
taxFile = validParameter.validFile(parameters, "taxonomy");
if (taxFile == "not open") { taxFile = ""; abort = true; }
else if (taxFile == "not found") { taxFile = ""; }
else {
current->setTaxonomyFile(taxFile);
if (splitmethod != "fasta") { splitmethod = "classify"; }
}
if ((phylipfile == "") && (columnfile == "") && (fastafile == "") && (file == "")) {
//is there are current file available for either of these?
//give priority to column, then phylip, then fasta
columnfile = current->getColumnFile();
if (columnfile != "") { format = "column"; m->mothurOut("Using " + columnfile + " as input file for the column parameter."); m->mothurOutEndLine(); }
else {
phylipfile = current->getPhylipFile();
if (phylipfile != "") { format = "phylip"; m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
else {
fastafile = current->getFastaFile();
if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
else {
m->mothurOut("No valid current files. When executing a cluster.split command you must enter a file, phylip or a column or fastafile."); m->mothurOutEndLine();
abort = true;
}
}
}
}
else if ((phylipfile != "") && (columnfile != "") && (fastafile != "") && (file != "")) { m->mothurOut("When executing a cluster.split command you must enter ONLY ONE of the following: file, fasta, phylip or column."); m->mothurOutEndLine(); abort = true; }
if ((countfile != "") && (namefile != "")) { m->mothurOut("When executing a cluster.split command you must enter ONLY ONE of the following: count or name."); m->mothurOutEndLine(); abort = true; }
if (columnfile != "") {
if ((namefile == "") && (countfile == "")) {
namefile = current->getNameFile();
if (namefile != "") { m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
else {
countfile = current->getCountFile();
if (countfile != "") { m->mothurOut("Using " + countfile + " as input file for the count parameter."); m->mothurOutEndLine(); }
else {
m->mothurOut("You need to provide a namefile or countfile if you are going to use the column format."); m->mothurOutEndLine();
abort = true;
}
}
}
}
if (fastafile != "") {
if (taxFile == "") {
taxFile = current->getTaxonomyFile();
if (taxFile != "") { m->mothurOut("Using " + taxFile + " as input file for the taxonomy parameter."); m->mothurOutEndLine(); }
else {
m->mothurOut("You need to provide a taxonomy file if you are if you are using a fasta file to generate the split."); m->mothurOutEndLine();
abort = true;
}
}
if ((namefile == "") && (countfile == "")) {
namefile = current->getNameFile();
if (namefile != "") { m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
else {
countfile = current->getCountFile();
if (countfile != "") { m->mothurOut("Using " + countfile + " as input file for the count parameter."); m->mothurOutEndLine(); }
else {
m->mothurOut("You need to provide a namefile or countfile if you are going to use the fasta file to generate the split."); m->mothurOutEndLine();
abort = true;
}
}
}
}
//check for optional parameter and set defaults
// ...at some point should added some additional type checking...
//get user cutoff and precision or use defaults
string temp;
temp = validParameter.valid(parameters, "precision");
if (temp == "not found") { temp = "100"; }
//saves precision legnth for formatting below
length = temp.length();
util.mothurConvert(temp, precision);
temp = validParameter.valid(parameters, "large"); if (temp == "not found") { temp = "F"; }
large = util.isTrue(temp);
temp = validParameter.valid(parameters, "processors"); if (temp == "not found"){ temp = current->getProcessors(); }
processors = current->setProcessors(temp);
temp = validParameter.valid(parameters, "splitmethod");
if ((splitmethod != "fasta") && (splitmethod != "classify")) {
if (temp == "not found") { splitmethod = "distance"; }
else { splitmethod = temp; }
}
temp = validParameter.valid(parameters, "classic"); if (temp == "not found") { temp = "F"; }
classic = util.isTrue(temp);
temp = validParameter.valid(parameters, "runsensspec"); if (temp == "not found") { temp = "T"; }
runsensSpec = util.isTrue(temp);
//not using file option and don't have fasta method with classic
if (((splitmethod != "fasta") && classic) && (file == "")) { m->mothurOut("[ERROR]: splitmethod must be fasta to use cluster.classic, or you must use the file option.\n"); abort=true; }
temp = validParameter.valid(parameters, "taxlevel"); if (temp == "not found") { temp = "3"; }
util.mothurConvert(temp, taxLevelCutoff);
temp = validParameter.valid(parameters, "iters"); if (temp == "not found") { temp = "100"; }
util.mothurConvert(temp, maxIters);
temp = validParameter.valid(parameters, "delta"); if (temp == "not found") { temp = "0.0001"; }
util.mothurConvert(temp, stableMetric);
metricName = validParameter.valid(parameters, "metric"); if (metricName == "not found") { metricName = "mcc"; }
if ((metricName == "mcc") || (metricName == "sens") || (metricName == "spec") || (metricName == "tptn") || (metricName == "tp") || (metricName == "tn") || (metricName == "fp") || (metricName == "fn") || (metricName == "f1score") || (metricName == "accuracy") || (metricName == "ppv") || (metricName == "npv") || (metricName == "fdr") || (metricName == "fpfn") ){ }
else { m->mothurOut("[ERROR]: Not a valid metric. Valid metrics are mcc, sens, spec, tp, tn, fp, fn, tptn, fpfn, f1score, accuracy, ppv, npv, fdr."); m->mothurOutEndLine(); abort = true; }
initialize = validParameter.valid(parameters, "initialize"); if (initialize == "not found") { initialize = "singleton"; }
if ((initialize == "singleton") || (initialize == "oneotu")){ }
else { m->mothurOut("[ERROR]: Not a valid initialization. Valid initializations are singleton and oneotu."); m->mothurOutEndLine(); abort = true; }
method = validParameter.valid(parameters, "method"); if (method == "not found") { method = "opti"; }
if ((method == "furthest") || (method == "nearest") || (method == "average") || (method == "weighted") || (method == "agc") || (method == "dgc") || (method == "opti")) { }
else { m->mothurOut("[ERROR]: Not a valid clustering method. Valid clustering algorithms are furthest, nearest, average, weighted, agc, dgc and opti."); m->mothurOutEndLine(); abort = true; }
if ((method == "agc") || (method == "dgc")) {
if (fastafile == "") { m->mothurOut("[ERROR]: You must provide a fasta file when using the agc or dgc clustering methods, aborting\n."); abort = true;}
if (classic) { m->mothurOut("[ERROR]: You cannot use cluster.classic with the agc or dgc clustering methods, aborting\n."); abort = true; }
}
cutoffNotSet = false;
temp = validParameter.valid(parameters, "cutoff");
if (temp == "not found") { cutoffNotSet = true; if ((method == "opti") || (method == "agc") || (method == "dgc")) { temp = "0.03"; }else { temp = "0.15"; } }
util.mothurConvert(temp, cutoff);
if ((splitmethod == "distance") || (splitmethod == "classify") || (splitmethod == "fasta")) { }
else { m->mothurOut("[ERROR]: " + splitmethod + " is not a valid splitting method. Valid splitting algorithms are distance, classify or fasta."); m->mothurOutEndLine(); abort = true; }
if ((splitmethod == "classify") && (taxFile == "")) { m->mothurOut("[ERROR]: You need to provide a taxonomy file if you are going to use the classify splitmethod."); m->mothurOutEndLine(); abort = true; }
temp = validParameter.valid(parameters, "showabund");
if (temp == "not found") { temp = "T"; }
showabund = util.isTrue(temp);
temp = validParameter.valid(parameters, "cluster"); if (temp == "not found") { temp = "T"; }
runCluster = util.isTrue(temp);
temp = validParameter.valid(parameters, "islist"); if (temp == "not found") { temp = "F"; }
isList = util.isTrue(temp);
temp = validParameter.valid(parameters, "dist"); if (temp == "not found") { temp = "F"; }
makeDist = util.isTrue(temp);
if (method == "opti") { makeDist = true; }
if (((phylipfile != "") || (columnfile != "")) && (method == "opti")) { makeDist = false; }
if (((phylipfile != "") || (columnfile != "")) && makeDist) { m->mothurOut("[ERROR]: You already provided a distance matrix. Mothur will ignore the dist parameter.\n"); makeDist = false; }
if (classic && makeDist) { m->mothurOut("[ERROR]: You cannot use the dist parameter with the classic parameter. Mothur will ignore the dist parameter.\n"); makeDist = false; }
timing = validParameter.valid(parameters, "timing");
if (timing == "not found") { timing = "F"; }
}
}
catch(exception& e) {
m->errorOut(e, "ClusterSplitCommand", "ClusterSplitCommand");
exit(1);
}
}
//**********************************************************************************************************************
int ClusterSplitCommand::execute(){
try {
if (abort) { if (calledHelp) { return 0; } return 2; }
time_t estart;
vector<string> listFileNames;
vector< map<string, string> > distName;
set<string> labels;
string singletonName = "";
double saveCutoff = cutoff;
if (file != "") {
deleteFiles = false; estart = time(NULL);
singletonName = readFile(distName);
if (isList) {
//set list file as new current listfile
string currentName = "";
itTypes = outputTypes.find("list");
if (itTypes != outputTypes.end()) {
if ((itTypes->second).size() != 0) { currentName = (itTypes->second)[0]; current->setListFile(currentName); }
}
m->mothurOutEndLine();
m->mothurOut("Output File Names: "); m->mothurOutEndLine();
for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
m->mothurOutEndLine();
return 0;
}
}else {
//****************** file prep work ******************************//
//if user gave a phylip file convert to column file
if (format == "phylip") {
estart = time(NULL);
m->mothurOut("Converting to column format..."); m->mothurOutEndLine();
ReadCluster* convert = new ReadCluster(distfile, cutoff, outputDir, false);
NameAssignment* nameMap = NULL;
convert->setFormat("phylip");
convert->read(nameMap);
if (m->getControl_pressed()) { delete convert; return 0; }
distfile = convert->getOutputFile();
//if no names file given with phylip file, create it
ListVector* listToMakeNameFile = convert->getListVector();
if ((namefile == "") && (countfile == "")) { //you need to make a namefile for split matrix
ofstream out;
namefile = phylipfile + ".names";
util.openOutputFile(namefile, out);
for (int i = 0; i < listToMakeNameFile->getNumBins(); i++) {
string bin = listToMakeNameFile->get(i);
out << bin << '\t' << bin << endl;
}
out.close();
}
delete listToMakeNameFile;
delete convert;
m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to convert the distance file."); m->mothurOutEndLine();
}
if (m->getControl_pressed()) { return 0; }
estart = time(NULL);
m->mothurOut("Splitting the file..."); m->mothurOutEndLine();
current->setMothurCalling(true);
//split matrix into non-overlapping groups
SplitMatrix* split;
if (splitmethod == "distance") { split = new SplitMatrix(distfile, namefile, countfile, taxFile, cutoff, splitmethod, large); }
else if (splitmethod == "classify") { split = new SplitMatrix(distfile, namefile, countfile, taxFile, taxLevelCutoff, splitmethod, large); }
else if (splitmethod == "fasta") {
if ((method == "agc") || (method == "dgc")) {
if (!findVsearch()) { m->mothurOut("[ERROR] cannot find vsearch executable, aborting.\n"); return 0; }
split = new SplitMatrix(fastafile, namefile, countfile, taxFile, taxLevelCutoff, cutoff, "vsearch", processors, classic, outputDir, "fasta");
}else{
split = new SplitMatrix(fastafile, namefile, countfile, taxFile, taxLevelCutoff, cutoff, splitmethod, processors, classic, outputDir, "distance");
}
}
else { m->mothurOut("Not a valid splitting method. Valid splitting algorithms are distance, classify or fasta."); m->mothurOutEndLine(); return 0; }
split->split();
if (fastafile != "") { current->setFastaFile(fastafile); }
if (m->getControl_pressed()) { delete split; return 0; }
singletonName = split->getSingletonNames();
numSingletons = split->getNumSingleton();
distName = split->getDistanceFiles(); //returns map of distance files -> namefile sorted by distance file size
delete split;
current->setMothurCalling(false);
if (m->getDebug()) { m->mothurOut("[DEBUG]: distName.size() = " + toString(distName.size()) + ".\n"); }
//output a merged distance file
if (makeDist) { createMergedDistanceFile(distName); }
if (m->getControl_pressed()) { return 0; }
m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to split the distance file."); m->mothurOutEndLine();
estart = time(NULL);
if (!runCluster) {
string filename = printFile(singletonName, distName);
m->mothurOutEndLine();
m->mothurOut("Output File Names: "); m->mothurOutEndLine();
m->mothurOutEndLine(); m->mothurOut(filename); m->mothurOutEndLine();
for (int i = 0; i < distName.size(); i++) { m->mothurOut(distName[i].begin()->first); m->mothurOutEndLine(); m->mothurOut(distName[i].begin()->second); m->mothurOutEndLine(); }
m->mothurOutEndLine();
return 0;
}
deleteFiles = true;
}
//****************** break up files between processes and cluster each file set ******************************//
listFileNames = createProcesses(distName, labels);
if (m->getControl_pressed()) { for (int i = 0; i < listFileNames.size(); i++) { util.mothurRemove(listFileNames[i]); } return 0; }
if (!util.isEqual(saveCutoff, cutoff)) { m->mothurOut("\nCutoff was " + toString(saveCutoff) + " changed cutoff to " + toString(cutoff)); m->mothurOutEndLine(); }
m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); m->mothurOutEndLine();
//****************** merge list file and create rabund and sabund files ******************************//
estart = time(NULL);
m->mothurOut("Merging the clustered files..."); m->mothurOutEndLine();
ListVector* listSingle;
map<double, int> labelBins = completeListFile(listFileNames, singletonName, labels, listSingle); //returns map of label to numBins
if (m->getControl_pressed()) { if (listSingle != NULL) { delete listSingle; } for (int i = 0; i < outputNames.size(); i++) { util.mothurRemove(outputNames[i]); } return 0; }
mergeLists(listFileNames, labelBins, listSingle);
if (m->getControl_pressed()) { for (int i = 0; i < outputNames.size(); i++) { util.mothurRemove(outputNames[i]); } return 0; }
//delete after all are complete incase a crash happens
if (!deleteFiles) { for (int i = 0; i < distName.size(); i++) { util.mothurRemove(distName[i].begin()->first); util.mothurRemove(distName[i].begin()->second); } }
m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to merge."); m->mothurOutEndLine();
if ((method == "opti") && (runsensSpec)) { runSensSpec(); }
if (m->getControl_pressed()) { for (int i = 0; i < outputNames.size(); i++) { util.mothurRemove(outputNames[i]); } return 0; }
//set list file as new current listfile
string currentName = "";
itTypes = outputTypes.find("list");
if (itTypes != outputTypes.end()) {
if ((itTypes->second).size() != 0) { currentName = (itTypes->second)[0]; current->setListFile(currentName); }
}
//set rabund file as new current rabundfile
itTypes = outputTypes.find("rabund");
if (itTypes != outputTypes.end()) { if ((itTypes->second).size() != 0) { currentName = (itTypes->second)[0]; current->setRabundFile(currentName); } }
//set sabund file as new current sabundfile
itTypes = outputTypes.find("sabund");
if (itTypes != outputTypes.end()) { if ((itTypes->second).size() != 0) { currentName = (itTypes->second)[0]; current->setSabundFile(currentName); } }
//set sabund file as new current sabundfile
itTypes = outputTypes.find("column");
if (itTypes != outputTypes.end()) { if ((itTypes->second).size() != 0) { currentName = (itTypes->second)[0]; current->setColumnFile(currentName); } }
m->mothurOut("\nOutput File Names: \n");
for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i] +"\n"); } m->mothurOutEndLine();
return 0;
}
catch(exception& e) {
m->errorOut(e, "ClusterSplitCommand", "execute");
exit(1);
}
}
//**********************************************************************************************************************
map<double, int> ClusterSplitCommand::completeListFile(vector<string> listNames, string singleton, set<string>& userLabels, ListVector*& listSingle){
try {
map<double, int> labelBin;
vector<double> orderFloat;
int numSingleBins;
//read in singletons
if (singleton != "none") {
ifstream in;
util.openInputFile(singleton, in);
string firstCol, secondCol;
listSingle = new ListVector();
if (countfile != "") { util.getline(in); util.gobble(in); }
while (!in.eof()) {
in >> firstCol >> secondCol;
util.getline(in);
if (countfile == "") { listSingle->push_back(secondCol); }
else { listSingle->push_back(firstCol); }
util.gobble(in);
}
in.close();
util.mothurRemove(singleton);
numSingleBins = listSingle->getNumBins();
}else{ listSingle = NULL; numSingleBins = 0; }
//go through users set and make them floats so we can sort them
for(set<string>::iterator it = userLabels.begin(); it != userLabels.end(); ++it) {
double temp = -10.0;
if ((*it != "unique") && (convertTestFloat(*it, temp) )) { util.mothurConvert(*it, temp); }
else if (*it == "unique") { temp = -1.0; }
if ((temp < cutoff) || util.isEqual(cutoff, temp)) {
orderFloat.push_back(temp);
labelBin[temp] = numSingleBins; //initialize numbins
}
}
//sort order
sort(orderFloat.begin(), orderFloat.end());
userLabels.clear();
//get the list info from each file
for (int k = 0; k < listNames.size(); k++) {
if (m->getControl_pressed()) {
if (listSingle != NULL) { delete listSingle; listSingle = NULL; util.mothurRemove(singleton); }
for (int i = 0; i < listNames.size(); i++) { util.mothurRemove(listNames[i]); }
return labelBin;
}
InputData* input = new InputData(listNames[k], "list", nullVector);
ListVector* list = input->getListVector();
string lastLabel = list->getLabel();
string filledInList = listNames[k] + "filledInTemp";
ofstream outFilled;
util.openOutputFile(filledInList, outFilled);
bool printHeaders = true;
//for each label needed
for(int l = 0; l < orderFloat.size(); l++){
string thisLabel;
if (util.isEqual(orderFloat[l],-1)) { thisLabel = "unique"; }
else { thisLabel = toString(orderFloat[l], length-1); }
//this file has reached the end
if (list == NULL) {
list = input->getListVector(lastLabel, true);
}else{ //do you have the distance, or do you need to fill in
float labelFloat;
if (list->getLabel() == "unique") { labelFloat = -1.0; }
else { convert(list->getLabel(), labelFloat); }
//check for missing labels
if (labelFloat > orderFloat[l]) { //you are missing the label, get the next smallest one
//if its bigger get last label, otherwise keep it
delete list;
list = input->getListVector(lastLabel, true); //get last list vector to use, you actually want to move back in the file
}
lastLabel = list->getLabel();
}
//print to new file
list->setLabel(thisLabel);
list->setPrintedLabels(printHeaders);
list->print(outFilled, true); printHeaders = false;
//update labelBin
labelBin[orderFloat[l]] += list->getNumBins();
delete list;
list = input->getListVector();
}
if (list != NULL) { delete list; }
delete input;
outFilled.close();
util.mothurRemove(listNames[k]);
rename(filledInList.c_str(), listNames[k].c_str());
}
return labelBin;
}
catch(exception& e) {
m->errorOut(e, "ClusterSplitCommand", "completeListFile");
exit(1);
}
}
//**********************************************************************************************************************
int ClusterSplitCommand::mergeLists(vector<string> listNames, map<double, int> userLabels, ListVector* listSingle){
try {
if (outputDir == "") { outputDir += util.hasPath(distfile); }
fileroot = outputDir + util.getRootName(util.getSimpleName(distfile));
map<string, string> variables;
variables["[filename]"] = fileroot;
variables["[clustertag]"] = tag;
string sabundFileName = getOutputFileName("sabund", variables);
string rabundFileName = getOutputFileName("rabund", variables);
//if (countfile != "") { variables["[tag2]"] = "unique_list"; }
string listFileName = getOutputFileName("list", variables);
map<string, int> counts;
ofstream outList, outRabund, outSabund;
if (countfile == "") {
util.openOutputFile(sabundFileName, outSabund);
util.openOutputFile(rabundFileName, outRabund);
outputNames.push_back(sabundFileName); outputTypes["sabund"].push_back(sabundFileName);
outputNames.push_back(rabundFileName); outputTypes["rabund"].push_back(rabundFileName);
}else {
if (file == "") {
CountTable ct;
ct.readTable(countfile, false, false);
counts = ct.getNameMap();
}
}
util.openOutputFile(listFileName, outList);
outputNames.push_back(listFileName); outputTypes["list"].push_back(listFileName);
bool printHeaders = true;
//for each label needed
for(map<double, int>::iterator itLabel = userLabels.begin(); itLabel != userLabels.end(); itLabel++) {
string thisLabel;
if (util.isEqual(itLabel->first,-1)) { thisLabel = "unique"; }
else { thisLabel = toString(itLabel->first, length-1); }
//outList << thisLabel << '\t' << itLabel->second << '\t';
RAbundVector* rabund = NULL;
ListVector completeList;
completeList.setLabel(thisLabel);
if (countfile == "") {
rabund = new RAbundVector();
rabund->setLabel(thisLabel);
}
//add in singletons
if (listSingle != NULL) {
for (int j = 0; j < listSingle->getNumBins(); j++) {
//outList << listSingle->get(j) << '\t';
completeList.push_back(listSingle->get(j));
if (countfile == "") { rabund->push_back(util.getNumNames(listSingle->get(j))); }
}
}
//get the list info from each file
for (int k = 0; k < listNames.size(); k++) {
if (m->getControl_pressed()) { if (listSingle != NULL) { delete listSingle; } for (int i = 0; i < listNames.size(); i++) { util.mothurRemove(listNames[i]); } if (rabund != NULL) { delete rabund; } return 0; }
InputData* input = new InputData(listNames[k], "list", nullVector);
ListVector* list = input->getListVector(thisLabel);
//this file has reached the end
if (list == NULL) { m->mothurOut("Error merging listvectors in file " + listNames[k]); m->mothurOutEndLine(); }
else {
for (int j = 0; j < list->getNumBins(); j++) {
completeList.push_back(list->get(j));
if (countfile == "") { rabund->push_back(util.getNumNames(list->get(j))); }
}
delete list;
}
delete input;
}
if (countfile == "") {
SAbundVector sabund = rabund->getSAbundVector();
sabund.print(outSabund);
rabund->print(outRabund);
}
completeList.setPrintedLabels(printHeaders);
if (countfile == "") { completeList.print(outList); printHeaders = false; }
else if ((file == "") && (countfile != "")) { completeList.print(outList, counts); printHeaders = false; }
else { completeList.print(outList); printHeaders = false; }
if (rabund != NULL) { delete rabund; }
}
outList.close();
if (countfile == "") {
outRabund.close();
outSabund.close();
}
if (listSingle != NULL) { delete listSingle; }
for (int i = 0; i < listNames.size(); i++) { util.mothurRemove(listNames[i]); }
return 0;
}
catch(exception& e) {
m->errorOut(e, "ClusterSplitCommand", "mergeLists");
exit(1);
}
}
/**************************************************************************************************/
struct clusterData {
MothurOut* m;
Utils util;
int count, precision, length, numSingletons, maxIters;
bool showabund, classic, useName, useCount, deleteFiles, cutoffNotSet;
double cutoff, stableMetric;
ofstream outList, outRabund, outSabund;
string tag, method, outputDir, vsearchLocation, metricName, initialize;
vector< map<string, string> > distNames;
set<string> labels;
vector<string> listFileNames;
clusterData(){}
clusterData(bool showab, bool cla, bool df, vector< map<string, string> > dN, bool cns, double cu, int prec, int len, string meth, string opd, string vl) {
showabund = showab;
distNames = dN;
cutoff = cu;
classic = cla;
method = meth;
precision = prec;
length = len;
outputDir = opd;
vsearchLocation = vl;
deleteFiles = df;
cutoffNotSet = cns;
m = MothurOut::getInstance();
count = 0;
useName = false;
useCount = false;
numSingletons = 0;
}
void setOptiOptions(int numSingles, string metn, double stabMet, string init, int mxi ) {
numSingletons = numSingles;
metricName = metn;
stableMetric = stabMet;
maxIters = mxi;
initialize = init;
}
void setNamesCount(string nmf, string cnf) {
useName = false;
useCount = false;
if (nmf != "") { useName = true; }
if (cnf != "") { useCount = true; }
}
};
//**********************************************************************************************************************
int createRabund(CountTable*& ct, ListVector*& list, RAbundVector*& rabund, clusterData* params){
try {
rabund->setLabel(list->getLabel());
for(int i = 0; i < list->getNumBins(); i++) {
if (params->m->getControl_pressed()) { break; }
vector<string> binNames;
string bin = list->get(i);
params->util.splitAtComma(bin, binNames);
int total = 0;
for (int j = 0; j < binNames.size(); j++) { total += ct->getNumSeqs(binNames[j]); }
rabund->push_back(total);
}
return 0;
}
catch(exception& e) {
params->m->errorOut(e, "ClusterSplitCommand", "createRabund");
exit(1);
}
}
//**********************************************************************************************************************
string clusterClassicFile(string thisDistFile, string thisNamefile, double& smallestCutoff, clusterData* params){
try {
string listFileName = "";
ListVector* list = NULL;
ListVector oldList;
RAbundVector* rabund = NULL;
params->m->mothurOut("\nReading " + thisDistFile + "\n");
//reads phylip file storing data in 2D vector, also fills list and rabund
bool sim = false;
ClusterClassic cluster(params->cutoff, params->method, sim);
NameAssignment* nameMap = NULL;
CountTable* ct = NULL;
if(params->useName){
nameMap = new NameAssignment(thisNamefile);
nameMap->readMap();
cluster.readPhylipFile(thisDistFile, nameMap);
}else if (params->useCount) {
ct = new CountTable();
ct->readTable(thisNamefile, false, false);
cluster.readPhylipFile(thisDistFile, ct);
}
params->tag = cluster.getTag();
if (params->m->getControl_pressed()) { if(params->useName){ delete nameMap; }
else if (params->useCount) { delete ct; } return listFileName; }
list = cluster.getListVector();
rabund = cluster.getRAbundVector();
string thisOutputDir = params->outputDir;
if (params->outputDir == "") { thisOutputDir += params->util.hasPath(thisDistFile); }
string fileroot = thisOutputDir + params->util.getRootName(params->util.getSimpleName(thisDistFile));
listFileName = fileroot+ params->tag + ".list";
ofstream listFile;
params->util.openOutputFile(fileroot+ params->tag + ".list", listFile);
float previousDist = 0.00000;
float rndPreviousDist = 0.00000;
bool printHeaders = true;
oldList = *list;
params->m->mothurOut("\nClustering " + thisDistFile + "\n");
while ((cluster.getSmallDist() < params->cutoff) && (cluster.getNSeqs() > 1)){
if (params->m->getControl_pressed()) { delete list; delete rabund; listFile.close(); if(params->useName){ delete nameMap; }
else if (params->useCount) { delete ct; } return listFileName; }
cluster.update(params->cutoff);
float dist = cluster.getSmallDist();
float rndDist = params->util.ceilDist(dist, params->precision);
if(previousDist <= 0.0000 && !params->util.isEqual(dist, previousDist)){
oldList.setLabel("unique");
oldList.setPrintedLabels(printHeaders);
oldList.print(listFile); printHeaders = false;
if (params->labels.count("unique") == 0) { params->labels.insert("unique"); }
}
else if(!params->util.isEqual(rndDist, rndPreviousDist)){
oldList.setLabel(toString(rndPreviousDist, params->length-1));
oldList.setPrintedLabels(printHeaders);
oldList.print(listFile); printHeaders = false;
if (params->labels.count(toString(rndPreviousDist, params->length-1)) == 0) { params->labels.insert(toString(rndPreviousDist, params->length-1)); }
}
previousDist = dist;
rndPreviousDist = rndDist;
oldList = *list;
}
if(previousDist <= 0.0000){
oldList.setLabel("unique");
oldList.setPrintedLabels(printHeaders);
oldList.print(listFile); printHeaders = false;
if (params->labels.count("unique") == 0) { params->labels.insert("unique"); }
}
else if(rndPreviousDist<params->cutoff){
oldList.setLabel(toString(rndPreviousDist, params->length-1));
oldList.setPrintedLabels(printHeaders);
oldList.print(listFile); printHeaders = false;
if (params->labels.count(toString(rndPreviousDist, params->length-1)) == 0) { params->labels.insert(toString(rndPreviousDist, params->length-1)); }
}
listFile.close();
delete list; delete rabund;
if(params->useName) { delete nameMap; }
else if (params->useCount) { delete ct; }
if (params->deleteFiles) {
params->util.mothurRemove(thisDistFile);
params->util.mothurRemove(thisNamefile);
}
return listFileName;
}
catch(exception& e) {
params->m->errorOut(e, "ClusterSplitCommand", "clusterClassicFile");
exit(1);
}
}
//**********************************************************************************************************************
string runOptiCluster(string thisDistFile, string thisNamefile, double& smallestCutoff, clusterData* params){
try {
if (params->cutoffNotSet) { params->m->mothurOut("\nYou did not set a cutoff, using 0.03.\n"); params->cutoff = 0.03; }
string nameOrCount = "count";
if (params->useName) { nameOrCount = "name"; }
OptiMatrix matrix(thisDistFile, thisNamefile, nameOrCount, "column", params->cutoff, false);
ClusterMetric* metric = NULL;
if (params->metricName == "mcc") { metric = new MCC(); }
else if (params->metricName == "sens") { metric = new Sensitivity(); }
else if (params->metricName == "spec") { metric = new Specificity(); }
else if (params->metricName == "tptn") { metric = new TPTN(); }
else if (params->metricName == "tp") { metric = new TP(); }
else if (params->metricName == "tn") { metric = new TN(); }
else if (params->metricName == "fp") { metric = new FP(); }
else if (params->metricName == "fn") { metric = new FN(); }
else if (params->metricName == "f1score") { metric = new F1Score(); }
else if (params->metricName == "accuracy") { metric = new Accuracy(); }
else if (params->metricName == "ppv") { metric = new PPV(); }
else if (params->metricName == "npv") { metric = new NPV(); }
else if (params->metricName == "fdr") { metric = new FDR(); }
else if (params->metricName == "fpfn") { metric = new FPFN(); }
OptiCluster cluster(&matrix, metric, params->numSingletons);
params->tag = cluster.getTag();
params->m->mothurOut("\nClustering " + thisDistFile + "\n");
string thisOutputDir = params->outputDir;
if (params->outputDir == "") { thisOutputDir += params->util.hasPath(thisDistFile); }
string fileroot = thisOutputDir + params->util.getRootName(params->util.getSimpleName(thisDistFile));
string listFileName = fileroot+ params->tag + ".list";
int iters = 0;
double listVectorMetric = 0; //worst state
double delta = 1;
cluster.initialize(listVectorMetric, true, params->initialize);
while ((delta > params->stableMetric) && (iters < params->maxIters)) {
if (params->m->getControl_pressed()) { if (params->deleteFiles) { params->util.mothurRemove(thisDistFile); params->util.mothurRemove(thisNamefile); } return listFileName; }
double oldMetric = listVectorMetric;
cluster.update(listVectorMetric);
delta = abs(oldMetric - listVectorMetric);
iters++;
}
if (params->m->getControl_pressed()) { delete metric; metric = NULL; return 0; }
ListVector* list = cluster.getList();
list->setLabel(toString(smallestCutoff));
//params->cutoff = params->util.ceilDist(params->cutoff, params->precision);
params->labels.insert(toString(smallestCutoff));
ofstream listFile;
params->util.openOutputFile(listFileName, listFile);
list->print(listFile);
listFile.close();
if (params->deleteFiles) {
params->util.mothurRemove(thisDistFile);
params->util.mothurRemove(thisNamefile);
}
long long tp, tn, fp, fn;
params->m->mothurOut("\ntp\ttn\tfp\tfn\tsensitivity\tspecificity\tppv\tnpv\tfdr\taccuracy\tmcc\tf1score\n");
vector<double> results = cluster.getStats(tp, tn, fp, fn);
params->m->mothurOut(toString(tp) + "\t" + toString(tn) + "\t" + toString(fp) + "\t" + toString(fn) + "\t");
for (int i = 0; i < results.size(); i++) { params->m->mothurOut(toString(results[i]) + "\t"); }
params->m->mothurOut("\n\n");
return listFileName;
}
catch(exception& e) {
params->m->errorOut(e, "ClusterSplitCommand", "runOptiCluster");
exit(1);
}
}
//**********************************************************************************************************************
int vsearchDriver(string inputFile, string ucClusteredFile, string logfile, double cutoff, clusterData* params){
try {
//vsearch --maxaccepts 16 --usersort --id 0.97 --minseqlength 30 --wordlength 8 --uc $ROOT.clustered.uc --cluster_smallmem $ROOT.sorted.fna --maxrejects 64 --strand both --log $ROOT.clustered.log --sizeorder
ucClusteredFile = params->util.getFullPathName(ucClusteredFile);
inputFile = params->util.getFullPathName(inputFile);
logfile = params->util.getFullPathName(logfile);
//to allow for spaces in the path
ucClusteredFile = "\"" + ucClusteredFile + "\"";
inputFile = "\"" + inputFile + "\"";
logfile = "\"" + logfile + "\"";
vector<char*> cPara;
string vsearchCommand = params->vsearchLocation;
vsearchCommand = "\"" + vsearchCommand + "\" ";
vector<char*> vsearchParameters;
char* vsearchParameter = new char[vsearchCommand.length()+1]; vsearchParameter[0] = '\0'; strncat(vsearchParameter, vsearchCommand.c_str(), vsearchCommand.length());
vsearchParameters.push_back(vsearchParameter);
//--maxaccepts=16
char* maxaccepts = new char[16]; maxaccepts[0] = '\0'; strncat(maxaccepts, "--maxaccepts=16", 15);
vsearchParameters.push_back(maxaccepts);
//--threads=1
char* threads = new char[12]; threads[0] = '\0'; strncat(threads, "--threads=1", 11);
vsearchParameters.push_back(threads);
//--usersort
char* usersort = new char[11]; usersort[0] = '\0'; strncat(usersort, "--usersort", 10);
vsearchParameters.push_back(usersort);
//--id=0.97
cutoff = abs(1.0 - cutoff); string cutoffString = toString(cutoff);
if (cutoffString.length() > 4) { cutoffString = cutoffString.substr(0, 4); }
else if (cutoffString.length() < 4) { for (int i = cutoffString.length(); i < 4; i++) { cutoffString += "0"; } }
cutoffString = "--id=" + cutoffString;
char* cutoffParameter = new char[cutoffString.length()+1]; cutoffParameter[0] = '\0'; strncat(cutoffParameter, cutoffString.c_str(), cutoffString.length());
vsearchParameters.push_back(cutoffParameter);
//--minseqlength=30
char* minseqlength = new char[18]; minseqlength[0] = '\0'; strncat(minseqlength, "--minseqlength=30", 17);
vsearchParameters.push_back(minseqlength);
//--wordlength=8
char* wordlength = new char[15]; wordlength[0] = '\0'; strncat(wordlength, "--wordlength=8", 14);
vsearchParameters.push_back(wordlength);
//--uc=$ROOT.clustered.uc
string tempIn = "--uc=" + ucClusteredFile;
char* uc = new char[tempIn.length()+1]; uc[0] = '\0'; strncat(uc, tempIn.c_str(), tempIn.length());
vsearchParameters.push_back(uc);
//--cluster_smallmem $ROOT.sorted.fna
string tempSorted = "--cluster_smallmem=" + inputFile;
char* cluster_smallmen = new char[tempSorted.length()+1]; cluster_smallmen[0] = '\0'; strncat(cluster_smallmen, tempSorted.c_str(), tempSorted.length());
vsearchParameters.push_back(cluster_smallmen);
//--maxrejects=64
char* maxrejects = new char[16]; maxrejects[0] = '\0'; strncat(maxrejects, "--maxrejects=64", 15);
vsearchParameters.push_back(maxrejects);
//--strand=both
char* strand = new char[14]; strand[0] = '\0'; strncat(strand, "--strand=both", 13);
vsearchParameters.push_back(strand);
//--log=$ROOT.clustered.log
string tempLog = "--log=" + logfile;
char* log = new char[tempLog.length()+1]; log[0] = '\0'; strncat(log, tempLog.c_str(), tempLog.length());
vsearchParameters.push_back(log);
if (params->method == "agc") {
//--sizeorder
char* sizeorder = new char[12]; sizeorder[0] = '\0'; strncat(sizeorder, "--sizeorder", 11);
vsearchParameters.push_back(sizeorder);
}
if (params->m->getDebug()) { params->m->mothurOut("[DEBUG]: "); for(int i = 0; i < vsearchParameters.size(); i++) { params->m->mothurOut(toString(vsearchParameters[i]) + "\t"); } params->m->mothurOut("\n"); }
string commandString = "";
for (int i = 0; i < vsearchParameters.size(); i++) { commandString += toString(vsearchParameters[i]) + " "; }
#if defined NON_WINDOWS
#else
commandString = "\"" + commandString + "\"";
#endif
if (params->m->getDebug()) { params->m->mothurOut("[DEBUG]: vsearch cluster command = " + commandString + ".\n"); }
system(commandString.c_str());
//free memory
for(int i = 0; i < vsearchParameters.size(); i++) { delete vsearchParameters[i]; }
//remove "" from filenames
ucClusteredFile = ucClusteredFile.substr(1, ucClusteredFile.length()-2);
inputFile = inputFile.substr(1, inputFile.length()-2);
logfile = logfile.substr(1, logfile.length()-2);
return 0;
}
catch(exception& e) {
params->m->errorOut(e, "ClusterSplitCommand", "vsearchDriver");
exit(1);
}
}
//**********************************************************************************************************************
string runVsearchCluster(string thisDistFile, string thisNamefile, double& smallestCutoff, clusterData* params){
try {
params->m->mothurOut("\nClustering " + thisDistFile + "\n");
string vsearchFastafile = ""; VsearchFileParser* vParse;
if (params->useName) { vParse = new VsearchFileParser(thisDistFile, thisNamefile, "name"); }
else if (params->useCount) { vParse = new VsearchFileParser(thisDistFile, thisNamefile, "count"); }
else { params->m->mothurOut("[ERROR]: Opps, should never get here. ClusterSplitCommand::runVsearchCluster() \n"); params->m->setControl_pressed(true); }
if (params->m->getControl_pressed()) { return ""; }
vsearchFastafile = vParse->getVsearchFile();
if (params->cutoff > 1.0) { params->m->mothurOut("You did not set a cutoff, using 0.03.\n"); params->cutoff = 0.03; }
//Run vsearch
string ucVsearchFile = params->util.getSimpleName(vsearchFastafile) + ".clustered.uc";
string logfile = params->util.getSimpleName(vsearchFastafile) + ".clustered.log";
vsearchDriver(vsearchFastafile, ucVsearchFile, logfile, smallestCutoff, params);
if (params->m->getControl_pressed()) { params->util.mothurRemove(ucVsearchFile); params->util.mothurRemove(logfile); params->util.mothurRemove(vsearchFastafile); return ""; }
string thisOutputDir = params->outputDir;
if (params->outputDir == "") { thisOutputDir += params->util.hasPath(thisDistFile); }
params->tag = params->method;
string listFileName = thisOutputDir + params->util.getRootName(params->util.getSimpleName(thisDistFile)) + params->tag + ".list";
//Convert outputted *.uc file into a list file
map<string, int> counts;
ListVector list = vParse->createListFile(ucVsearchFile, vParse->getNumBins(logfile), toString(params->cutoff), counts);
ofstream out;
params->util.openOutputFile(listFileName, out);
list.DataVector::printHeaders(out);
if (params->useCount) { list.print(out, counts); }
else { list.print(out); } delete vParse;
//remove temp files
params->util.mothurRemove(ucVsearchFile); params->util.mothurRemove(logfile); params->util.mothurRemove(vsearchFastafile);
if (params->deleteFiles) {
params->util.mothurRemove(thisDistFile);
params->util.mothurRemove(thisNamefile);
}
params->labels.insert(toString(params->cutoff));
return listFileName;
}
catch(exception& e) {
params->m->errorOut(e, "ClusterSplitCommand", "runVsearchCluster");
exit(1);
}
}
//**********************************************************************************************************************
string clusterFile(string thisDistFile, string thisNamefile, double& smallestCutoff, clusterData* params){
try {
string listFileName = "";
if ((params->method == "agc") || (params->method == "dgc")) { listFileName = runVsearchCluster(thisDistFile, thisNamefile, smallestCutoff, params); }
else if (params->method == "opti") { listFileName = runOptiCluster(thisDistFile, thisNamefile, smallestCutoff, params); }
else {
Cluster* cluster = NULL;
SparseDistanceMatrix* matrix = NULL;
ListVector* list = NULL;
ListVector oldList;
RAbundVector* rabund = NULL;
if (params->m->getControl_pressed()) { return listFileName; }
params->m->mothurOut("\nReading " + thisDistFile + "\n");
ReadMatrix* read = new ReadColumnMatrix(thisDistFile);
read->setCutoff(params->cutoff);
NameAssignment* nameMap = NULL;
CountTable* ct = NULL;
if(params->useName){
nameMap = new NameAssignment(thisNamefile);
nameMap->readMap();
read->read(nameMap);
}else if (params->useCount) {
ct = new CountTable();
ct->readTable(thisNamefile, false, false);
read->read(ct);
}else { read->read(nameMap); }
list = read->getListVector();
matrix = read->getDMatrix();
if(params->useCount) {
rabund = new RAbundVector();
createRabund(ct, list, rabund, params); //creates an rabund that includes the counts for the unique list
delete ct;
}else { rabund = new RAbundVector(list->getRAbundVector()); }
delete read; read = NULL;
if (params->useName) { delete nameMap; nameMap = NULL; }
params->m->mothurOut("\nClustering " + thisDistFile + "\n");
//create cluster
float adjust = -1.0;
if (params->method == "furthest") { cluster = new CompleteLinkage(rabund, list, matrix, params->cutoff, params->method, adjust); }
else if(params->method == "nearest"){ cluster = new SingleLinkage(rabund, list, matrix, params->cutoff, params->method, adjust); }
else if(params->method == "average"){ cluster = new AverageLinkage(rabund, list, matrix, params->cutoff, params->method, adjust); }
params->tag = cluster->getTag();
string thisOutputDir = params->outputDir;
if (params->outputDir == "") { thisOutputDir += params->util.hasPath(thisDistFile); }
string fileroot = thisOutputDir + params->util.getRootName(params->util.getSimpleName(thisDistFile));
listFileName = fileroot+ params->tag + ".list";
ofstream listFile;
params->util.openOutputFile(listFileName, listFile);
float previousDist = 0.00000;
float rndPreviousDist = 0.00000;
bool printHeaders = true;
oldList = *list;
double saveCutoff = params->cutoff;
while (matrix->getSmallDist() < params->cutoff && matrix->getNNodes() > 0){
if (params->m->getControl_pressed()) { //clean up
delete matrix; delete list; delete cluster; delete rabund;
listFile.close();
params->util.mothurRemove(listFileName);
return listFileName;
}
cluster->update(saveCutoff);
float dist = matrix->getSmallDist();
float rndDist = params->util.ceilDist(dist, params->precision);
if(previousDist <= 0.0000 && !params->util.isEqual(dist, previousDist)){
oldList.setLabel("unique");
oldList.setPrintedLabels(printHeaders);
oldList.print(listFile); printHeaders = false;
if (params->labels.count("unique") == 0) { params->labels.insert("unique"); }
}
else if(!params->util.isEqual(rndDist, rndPreviousDist)){
oldList.setPrintedLabels(printHeaders);
oldList.setLabel(toString(rndPreviousDist, params->length-1));
oldList.setPrintedLabels(printHeaders);
oldList.print(listFile); printHeaders = false;
if (params->labels.count(toString(rndPreviousDist, params->length-1)) == 0) { params->labels.insert(toString(rndPreviousDist, params->length-1)); }
}
previousDist = dist;
rndPreviousDist = rndDist;
oldList = *list;
}
if(previousDist <= 0.0000){
oldList.setLabel("unique");
oldList.setPrintedLabels(printHeaders);
oldList.print(listFile); printHeaders = false;
if (params->labels.count("unique") == 0) { params->labels.insert("unique"); }
}
else if(rndPreviousDist<params->cutoff){
oldList.setLabel(toString(rndPreviousDist, params->length-1));
oldList.setPrintedLabels(printHeaders);
oldList.print(listFile); printHeaders = false;
if (params->labels.count(toString(rndPreviousDist, params->length-1)) == 0) { params->labels.insert(toString(rndPreviousDist, params->length-1)); }
}
delete matrix; delete list; delete cluster; delete rabund;
matrix = NULL; list = NULL; cluster = NULL; rabund = NULL;
listFile.close();
if (params->m->getControl_pressed()) { //clean up
params->util.mothurRemove(listFileName);
return listFileName;
}
if (params->deleteFiles) {
params->util.mothurRemove(thisDistFile);
params->util.mothurRemove(thisNamefile);
}
if (!params->util.isEqual(saveCutoff, params->cutoff)) {
saveCutoff = params->util.ceilDist(saveCutoff, params->precision);
params->m->mothurOut("Cutoff was " + toString(params->cutoff) + " changed cutoff to " + toString(saveCutoff) + "\n");
}
if (saveCutoff < smallestCutoff) { smallestCutoff = saveCutoff; }
}
return listFileName;
}
catch(exception& e) {
params->m->errorOut(e, "ClusterSplitCommand", "clusterFile");
exit(1);
}
}
//**********************************************************************************************************************
void cluster(clusterData* params){
try {
vector<string> listFileNames;
double smallestCutoff = params->cutoff;
//cluster each distance file
for (int i = 0; i < params->distNames.size(); i++) {
string thisNamefile = params->distNames[i].begin()->second;
string thisDistFile = params->distNames[i].begin()->first;
string listFileName = "";
if (params->classic) { listFileName = clusterClassicFile(thisDistFile, thisNamefile, smallestCutoff, params); }
else { listFileName = clusterFile(thisDistFile, thisNamefile, smallestCutoff, params); }
if (params->m->getControl_pressed()) { //clean up
for (int i = 0; i < listFileNames.size(); i++) { params->util.mothurRemove(listFileNames[i]); }
params->listFileNames.clear(); break;
}
params->listFileNames.push_back(listFileName);
}
params->cutoff = smallestCutoff;
}
catch(exception& e) {
params->m->errorOut(e, "ClusterSplitCommand", "cluster");
exit(1);
}
}
//**********************************************************************************************************************
void printData(ListVector* oldList, clusterData* params){
try {
string label = oldList->getLabel();
RAbundVector oldRAbund = oldList->getRAbundVector();
oldRAbund.setLabel(label);
if (params->showabund) {
oldRAbund.getSAbundVector().print(cout);
}
oldRAbund.print(params->outRabund);
oldRAbund.getSAbundVector().print(params->outSabund);
oldList->print(params->outList, true);
}
catch(exception& e) {
params->m->errorOut(e, "ClusterSplitCommand", "printData");
exit(1);
}
}
//**********************************************************************************************************************
vector<string> ClusterSplitCommand::createProcesses(vector< map<string, string> > distName, set<string>& labels){
try {
//sanity check
if (processors > distName.size()) { processors = distName.size(); }
deleteFiles = false; //so if we need to recalc the processors the files are still there
vector<string> listFiles;
vector < vector < map<string, string> > > dividedNames; //distNames[1] = vector of filenames for process 1...
dividedNames.resize(processors);
//for each file group figure out which process will complete it
//want to divide the load intelligently so the big files are spread between processes
for (int i = 0; i < distName.size(); i++) {
int processToAssign = (i+1) % processors;
if (processToAssign == 0) { processToAssign = processors; }
dividedNames[(processToAssign-1)].push_back(distName[i]);
if ((processToAssign-1) == 1) { m->mothurOut(distName[i].begin()->first + "\n"); }
}
//now lets reverse the order of ever other process, so we balance big files running with little ones
for (int i = 0; i < processors; i++) {
int remainder = ((i+1) % processors);
if (remainder) { reverse(dividedNames[i].begin(), dividedNames[i].end()); }
}
if (m->getControl_pressed()) { return listFiles; }
//create array of worker threads
vector<thread*> workerThreads;
vector<clusterData*> data;
//Lauch worker threads
for (int i = 0; i < processors-1; i++) {
clusterData* dataBundle = new clusterData(showabund, classic, deleteFiles, dividedNames[i+1], cutoffNotSet, cutoff, precision, length, method, outputDir, vsearchLocation);
dataBundle->setOptiOptions(numSingletons, metricName, stableMetric, initialize, maxIters);
dataBundle->setNamesCount(namefile, countfile);
data.push_back(dataBundle);
workerThreads.push_back(new thread(cluster, dataBundle));
}
clusterData* dataBundle = new clusterData(showabund, classic, deleteFiles, dividedNames[0], cutoffNotSet, cutoff, precision, length, method, outputDir, vsearchLocation);
dataBundle->setOptiOptions(numSingletons, metricName, stableMetric, initialize, maxIters);
dataBundle->setNamesCount(namefile, countfile);
cluster(dataBundle);
listFiles = dataBundle->listFileNames;
tag = dataBundle->tag;
cutoff = dataBundle->cutoff;
labels = dataBundle->labels;
for (int i = 0; i < processors-1; i++) {
workerThreads[i]->join();
listFiles.insert(listFiles.end(), data[i]->listFileNames.begin(), data[i]->listFileNames.end());
labels.insert(data[i]->labels.begin(), data[i]->labels.end());
if (data[i]->cutoff < cutoff) { cutoff = data[i]->cutoff; }
delete data[i];
delete workerThreads[i];
}
delete dataBundle;
deleteFiles = true;
if (deleteFiles) {
//delete the temp files now that we are done
for (int i = 0; i < distName.size(); i++) {
string thisNamefile = distName[i].begin()->second;
string thisDistFile = distName[i].begin()->first;
util.mothurRemove(thisNamefile);
util.mothurRemove(thisDistFile);
}
}
return listFiles;
}
catch(exception& e) {
m->errorOut(e, "ClusterSplitCommand", "createProcesses");
exit(1);
}
}
//**********************************************************************************************************************
int ClusterSplitCommand::createMergedDistanceFile(vector< map<string, string> > distNames) {
try{
string thisOutputDir = outputDir;
if (outputDir == "") { thisOutputDir = util.hasPath(fastafile); }
map<string, string> variables;
variables["[filename]"] = thisOutputDir + util.getRootName(util.getSimpleName(fastafile));
string outputFileName = getOutputFileName("column", variables);
util.mothurRemove(outputFileName);
for (int i = 0; i < distNames.size(); i++) {
if (m->getControl_pressed()) { return 0; }
string thisDistFile = distNames[i].begin()->first;
util.appendFiles(thisDistFile, outputFileName);
}
outputTypes["column"].push_back(outputFileName); outputNames.push_back(outputFileName);
return 0;
}
catch(exception& e) {
m->errorOut(e, "ClusterSplitCommand", "createMergedDistanceFile");
exit(1);
}
}
//**********************************************************************************************************************
int ClusterSplitCommand::runSensSpec() {
try{
string listFile = "";
itTypes = outputTypes.find("list");
if (itTypes != outputTypes.end()) {
if ((itTypes->second).size() != 0) { listFile = (itTypes->second)[0]; }
}
string columnFile = ""; string phylipFile = "";
if (makeDist) {
itTypes = outputTypes.find("column");
if (itTypes != outputTypes.end()) {
if ((itTypes->second).size() != 0) { columnFile = (itTypes->second)[0]; }
}
}else if (columnfile != "") { columnFile = columnfile; }
else { phylipFile = phylipfile; }
string inputString = "cutoff=" + toString(cutoff) + ", list=" + listFile;
if (columnFile != "") { inputString += ", column=" + columnFile; }
else if (phylipfile != "") { inputString += ", phylip=" + phylipfile; }
else { m->mothurOut("[WARNING]: Cannot run sens.spec analysis without a phylip or column file, skipping."); return 0; }
if (namefile != "") { inputString += ", name=" + namefile; }
else if (countfile != "") { inputString += ", count=" + countfile; }
else { m->mothurOut("[WARNING]: Cannot run sens.spec analysis without a name or count file, skipping."); return 0; }
m->mothurOut("/******************************************/"); m->mothurOutEndLine();
m->mothurOut("Running command: sens.spec(" + inputString + ")"); m->mothurOutEndLine();
current->setMothurCalling(true);
Command* sensspecCommand = new SensSpecCommand(inputString);
sensspecCommand->execute();
map<string, vector<string> > filenames = sensspecCommand->getOutputFiles();
delete sensspecCommand;
current->setMothurCalling(false);
string outputFileName = filenames["sensspec"][0];
outputTypes["sensspec"].push_back(outputFileName); outputNames.push_back(outputFileName);
m->mothurOut("/******************************************/"); m->mothurOutEndLine();
m->mothurOut("Done.\n\n"); m->mothurOutEndLine();
ifstream in;
util.openInputFile(outputFileName, in);
while(!in.eof()){
if (m->getControl_pressed()) { break; }
m->mothurOut(util.getline(in)+"\n"); util.gobble(in);
}
in.close();
return 0;
}
catch(exception& e) {
m->errorOut(e, "ClusterSplitCommand", "runSensSpec");
exit(1);
}
}
//**********************************************************************************************************************
string ClusterSplitCommand::printFile(string singleton, vector< map<string, string> >& distName){
try {
ofstream out;
map<string, string> variables;
string thisOutputDir = outputDir;
if (outputDir == "") { thisOutputDir = util.hasPath(distfile); }
variables["[filename]"] = thisOutputDir + util.getRootName(util.getSimpleName(distfile));
string outputFileName = getOutputFileName("file", variables);
util.openOutputFile(outputFileName, out);
outputTypes["file"].push_back(outputFileName); outputNames.push_back(outputFileName);
current->setFileFile(outputFileName);
out << singleton << endl;
if (namefile != "") { out << "name" << endl; }
else if (countfile != "") { out << "count" << endl; }
else { out << "unknown" << endl; }
for (int i = 0; i < distName.size(); i++) { out << distName[i].begin()->first << '\t' << distName[i].begin()->second << endl; }
out.close();
return outputFileName;
}
catch(exception& e) {
m->errorOut(e, "ClusterSplitCommand", "printFile");
exit(1);
}
}
//**********************************************************************************************************************
string ClusterSplitCommand::readFile(vector< map<string, string> >& distName){
try {
string singleton, thiscolumn, thisname, type;
ifstream in;
util.openInputFile(file, in);
in >> singleton; util.gobble(in);
string path = util.hasPath(singleton);
if (path == "") { singleton = inputDir + singleton; }
in >> type; util.gobble(in);
if (type == "name") { namefile = "name"; }
else if (type == "count") { countfile = "count"; }
else { m->mothurOut("[ERROR]: unknown file type. Are the files in column 2 of the file name files or count files? Please change unknown to name or count.\n"); m->setControl_pressed(true); }
if (isList) {
vector<string> listFileNames;
string thisListFileName = "";
set<string> listLabels;
while(!in.eof()) {
if (m->getControl_pressed()) { break; }
in >> thisListFileName; util.gobble(in);
string path = util.hasPath(thisListFileName);
if (path == "") { thisListFileName = inputDir + thisListFileName; }
getLabels(thisListFileName, listLabels);
listFileNames.push_back(thisListFileName);
}
ListVector* listSingle;
map<double, int> labelBins = completeListFile(listFileNames, singleton, listLabels, listSingle);
mergeLists(listFileNames, labelBins, listSingle);
}else {
while(!in.eof()) {
if (m->getControl_pressed()) { break; }
in >> thiscolumn; util.gobble(in);
in >> thisname; util.gobble(in);
map<string, string> temp;
temp[thiscolumn] = thisname;
distName.push_back(temp);
}
}
in.close();
return singleton;
}
catch(exception& e) {
m->errorOut(e, "ClusterSplitCommand", "readFile");
exit(1);
}
}
//**********************************************************************************************************************
int ClusterSplitCommand::getLabels(string file, set<string>& listLabels){
try {
ifstream in;
util.openInputFile(file, in);
//read headers
util.getline(in); util.gobble(in);
string label;
while(!in.eof()) {
if (m->getControl_pressed()) { break; }
in >> label; util.getline(in); util.gobble(in);
listLabels.insert(label);
}
in.close();
return 0;
}
catch(exception& e) {
m->errorOut(e, "ClusterSplitCommand", "getLabels");
exit(1);
}
}
//**********************************************************************************************************************
bool ClusterSplitCommand::findVsearch(){
try {
abort = false;
if (cutoffNotSet) { m->mothurOut("\nYou did not set a cutoff, using 0.03.\n"); cutoff = 0.03; }
//look for vsearch exe
string path = current->getProgramPath();
string vsearchCommand = path + PATH_SEPARATOR;
vsearchCommand += "vsearch"; vsearchCommand += EXECUTABLE_EXT;
#if defined NON_WINDOWS
if (m->getDebug()) {
m->mothurOut("[DEBUG]: vsearch location using \"which vsearch\" = ");
Command* newCommand = new SystemCommand("which vsearch"); m->mothurOutEndLine();
newCommand->execute(); delete newCommand;
m->mothurOut("[DEBUG]: Mothur's location using \"which mothur\" = ");
newCommand = new SystemCommand("which mothur"); m->mothurOutEndLine();
newCommand->execute(); delete newCommand;
}
#endif
//test to make sure vsearch exists
ifstream in;
vsearchCommand = util.getFullPathName(vsearchCommand);
bool ableToOpen = util.openInputFile(vsearchCommand, in, "no error"); in.close();
if(!ableToOpen) {
m->mothurOut(vsearchCommand + " file does not exist. Checking path... \n");
ifstream in2;
string programName = "vsearch"; programName += EXECUTABLE_EXT;
string uLocation = util.findProgramPath(programName);
ableToOpen = util.openInputFile(uLocation+programName, in2, "no error"); in2.close();
if(!ableToOpen) { m->mothurOut("[ERROR]: " + uLocation + " file does not exist. mothur requires the vsearch executable.\n"); m->setControl_pressed(true); }
else { m->mothurOut("Found vsearch in your path, using " + uLocation + "\n");vsearchLocation = uLocation+programName; }
}else { vsearchLocation = vsearchCommand; }
vsearchLocation = util.getFullPathName(vsearchLocation);
if (m->getDebug()) { m->mothurOut("[DEBUG]: vsearch location using " + vsearchLocation + "\n"); }
if (!abort) { return true; }
return false;
}
catch(exception& e) {
m->errorOut(e, "ClusterSplitCommand", "findVsearch");
exit(1);
}
}
//**********************************************************************************************************************
|