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
|
/*
* seqerrorcommand.cpp
* Mothur
*
* Created by Pat Schloss on 7/15/10.
* Copyright 2010 Schloss Lab. All rights reserved.
*
*/
#include "seqerrorcommand.h"
#include "reportfile.h"
#include "qualityscores.h"
#include "refchimeratest.h"
#include "myPerseus.h"
#include "filterseqscommand.h"
//**********************************************************************************************************************
vector<string> SeqErrorCommand::setParameters(){
try {
CommandParameter pquery("fasta", "InputTypes", "", "", "none", "none", "none","errorType",false,true,true); parameters.push_back(pquery);
CommandParameter preference("reference", "InputTypes", "", "", "none", "none", "none","",false,true,true); parameters.push_back(preference);
CommandParameter pqfile("qfile", "InputTypes", "", "", "none", "none", "QualReport","",false,false); parameters.push_back(pqfile);
CommandParameter preport("report", "InputTypes", "", "", "none", "none", "QualReport","",false,false); parameters.push_back(preport);
CommandParameter pname("name", "InputTypes", "", "", "namecount", "none", "none","",false,false,true); parameters.push_back(pname);
CommandParameter pcount("count", "InputTypes", "", "", "namecount", "none", "none","",false,false,true); parameters.push_back(pcount);
CommandParameter pignorechimeras("ignorechimeras", "Boolean", "", "T", "", "", "","",false,false); parameters.push_back(pignorechimeras);
CommandParameter pthreshold("threshold", "Number", "", "1.0", "", "", "","",false,false); parameters.push_back(pthreshold);
CommandParameter paligned("aligned", "Boolean", "", "T", "", "", "","",false,false); parameters.push_back(paligned);
CommandParameter pprocessors("processors", "Number", "", "1", "", "", "","",false,false,true); parameters.push_back(pprocessors);
CommandParameter psave("save", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(psave);
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, "SeqErrorCommand", "setParameters");
exit(1);
}
}
//**********************************************************************************************************************
string SeqErrorCommand::getHelpString(){
try {
string helpString = "";
helpString += "The seq.error command reads a query alignment file and a reference alignment file and creates .....\n";
helpString += "The fasta parameter...\n";
helpString += "The reference parameter...\n";
helpString += "The qfile parameter...\n";
helpString += "The report parameter...\n";
helpString += "The name parameter allows you to provide a name file associated with the fasta file.\n";
helpString += "The count parameter allows you to provide a count file associated with the fasta file.\n";
helpString += "The ignorechimeras parameter...\n";
helpString += "The threshold parameter...\n";
helpString += "The processors parameter...\n";
helpString += "If the save parameter is set to true the reference sequences will be saved in memory, to clear them later you can use the clear.memory command. Default=f.";
helpString += "Example seq.error(...).\n";
helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
helpString += "For more details please check out the wiki http://www.mothur.org/wiki/seq.error .\n";
return helpString;
}
catch(exception& e) {
m->errorOut(e, "SeqErrorCommand", "getHelpString");
exit(1);
}
}
//**********************************************************************************************************************
string SeqErrorCommand::getOutputPattern(string type) {
try {
string pattern = "";
if (type == "errorsummary") { pattern = "[filename],error.summary"; }
else if (type == "errorseq") { pattern = "[filename],error.seq"; }
else if (type == "errorquality") { pattern = "[filename],error.quality"; }
else if (type == "errorqualforward") { pattern = "[filename],error.qual.forward"; }
else if (type == "errorqualreverse") { pattern = "[filename],error.qual.reverse"; }
else if (type == "errorforward") { pattern = "[filename],error.seq.forward"; }
else if (type == "errorreverse") { pattern = "[filename],error.seq.reverse"; }
else if (type == "errorcount") { pattern = "[filename],error.count"; }
else if (type == "errormatrix") { pattern = "[filename],error.matrix"; }
else if (type == "errorchimera") { pattern = "[filename],error.chimera"; }
else if (type == "errorref-query") { pattern = "[filename],error.ref-query"; }
else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
return pattern;
}
catch(exception& e) {
m->errorOut(e, "SeqErrorCommand", "getOutputPattern");
exit(1);
}
}
//**********************************************************************************************************************
SeqErrorCommand::SeqErrorCommand(){
try {
abort = true; calledHelp = true;
setParameters();
vector<string> tempOutNames;
outputTypes["errorsummary"] = tempOutNames;
outputTypes["errorseq"] = tempOutNames;
outputTypes["errorquality"] = tempOutNames;
outputTypes["errorqualforward"] = tempOutNames;
outputTypes["errorqualreverse"] = tempOutNames;
outputTypes["errorforward"] = tempOutNames;
outputTypes["errorreverse"] = tempOutNames;
outputTypes["errorcount"] = tempOutNames;
outputTypes["errormatrix"] = tempOutNames;
outputTypes["errorchimera"] = tempOutNames;
outputTypes["errorref-query"] = tempOutNames;
}
catch(exception& e) {
m->errorOut(e, "SeqErrorCommand", "SeqErrorCommand");
exit(1);
}
}
//***************************************************************************************************************
SeqErrorCommand::SeqErrorCommand(string option) {
try {
abort = false; calledHelp = false;
rdb = ReferenceDB::getInstance();
//allow user to run help
if(option == "help") { help(); abort = true; calledHelp = true; }
else if(option == "citation") { citation(); abort = true; calledHelp = true;}
else {
string temp;
vector<string> myArray = setParameters();
OptionParser parser(option);
map<string,string> parameters = parser.getParameters();
ValidParameters validParameter;
map<string,string>::iterator it;
//check to make sure all parameters are valid for command
for (it = parameters.begin(); it != parameters.end(); it++) {
if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
}
//initialize outputTypes
vector<string> tempOutNames;
outputTypes["errorsummary"] = tempOutNames;
outputTypes["errorseq"] = tempOutNames;
outputTypes["errorquality"] = tempOutNames;
outputTypes["errorqualforward"] = tempOutNames;
outputTypes["errorqualreverse"] = tempOutNames;
outputTypes["errorforward"] = tempOutNames;
outputTypes["errorreverse"] = tempOutNames;
outputTypes["errorcount"] = tempOutNames;
outputTypes["errormatrix"] = tempOutNames;
outputTypes["errorchimera"] = tempOutNames;
outputTypes["errorref-query"] = tempOutNames;
//if the user changes the input directory command factory will send this info to us in the output parameter
string inputDir = validParameter.validFile(parameters, "inputdir", false);
if (inputDir == "not found"){ inputDir = ""; }
else {
string path;
it = parameters.find("fasta");
//user has given a template file
if(it != parameters.end()){
path = m->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("reference");
//user has given a template file
if(it != parameters.end()){
path = m->hasPath(it->second);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["reference"] = inputDir + it->second; }
}
it = parameters.find("name");
//user has given a names file
if(it != parameters.end()){
path = m->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("count");
//user has given a names file
if(it != parameters.end()){
path = m->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("qfile");
//user has given a quality score file
if(it != parameters.end()){
path = m->hasPath(it->second);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["qfile"] = inputDir + it->second; }
}
it = parameters.find("report");
//user has given a alignment report file
if(it != parameters.end()){
path = m->hasPath(it->second);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["report"] = inputDir + it->second; }
}
}
//check for required parameters
queryFileName = validParameter.validFile(parameters, "fasta", true);
if (queryFileName == "not found") {
queryFileName = m->getFastaFile();
if (queryFileName != "") { m->mothurOut("Using " + queryFileName + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
else { m->mothurOut("You have no current fasta file and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
}
else if (queryFileName == "not open") { queryFileName = ""; abort = true; }
else { m->setFastaFile(queryFileName); }
referenceFileName = validParameter.validFile(parameters, "reference", true);
if (referenceFileName == "not found") { m->mothurOut("reference is a required parameter for the seq.error command."); m->mothurOutEndLine(); abort = true; }
else if (referenceFileName == "not open") { abort = true; }
//check for optional parameters
namesFileName = validParameter.validFile(parameters, "name", true);
if(namesFileName == "not found"){ namesFileName = ""; }
else if (namesFileName == "not open") { namesFileName = ""; abort = true; }
else { m->setNameFile(namesFileName); }
//check for optional parameters
countfile = validParameter.validFile(parameters, "count", true);
if(countfile == "not found"){ countfile = ""; }
else if (countfile == "not open") { countfile = ""; abort = true; }
else { m->setCountTableFile(countfile); }
qualFileName = validParameter.validFile(parameters, "qfile", true);
if(qualFileName == "not found"){ qualFileName = ""; }
else if (qualFileName == "not open") { qualFileName = ""; abort = true; }
else { m->setQualFile(qualFileName); }
reportFileName = validParameter.validFile(parameters, "report", true);
if(reportFileName == "not found"){ reportFileName = ""; }
else if (reportFileName == "not open") { reportFileName = ""; abort = true; }
outputDir = validParameter.validFile(parameters, "outputdir", false);
if (outputDir == "not found"){
outputDir = "";
outputDir += m->hasPath(queryFileName); //if user entered a file with a path then preserve it
}
if ((countfile != "") && (namesFileName != "")) { m->mothurOut("You must enter ONLY ONE of the following: count or name."); m->mothurOutEndLine(); abort = true; }
//check for optional parameter and set defaults
// ...at some point should added some additional type checking...
temp = validParameter.validFile(parameters, "threshold", false); if (temp == "not found") { temp = "1.00"; }
m->mothurConvert(temp, threshold);
temp = validParameter.validFile(parameters, "save", false); if (temp == "not found"){ temp = "f"; }
save = m->isTrue(temp);
rdb->save = save;
if (save) { //clear out old references
rdb->clearMemory();
}
//this has to go after save so that if the user sets save=t and provides no reference we abort
referenceFileName = validParameter.validFile(parameters, "reference", true);
if (referenceFileName == "not found") {
//check for saved reference sequences
if (rdb->referenceSeqs.size() != 0) {
referenceFileName = "saved";
}else {
m->mothurOut("[ERROR]: You don't have any saved reference sequences and the reference parameter is a required.");
m->mothurOutEndLine();
abort = true;
}
}else if (referenceFileName == "not open") { abort = true; }
else { if (save) { rdb->setSavedReference(referenceFileName); } }
temp = validParameter.validFile(parameters, "ignorechimeras", false); if (temp == "not found") { temp = "T"; }
ignoreChimeras = m->isTrue(temp);
temp = validParameter.validFile(parameters, "aligned", false); if (temp == "not found"){ temp = "t"; }
aligned = m->isTrue(temp);
temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
m->setProcessors(temp);
m->mothurConvert(temp, processors);
substitutionMatrix.resize(6);
for(int i=0;i<6;i++){ substitutionMatrix[i].resize(6,0); }
if ((namesFileName == "") && (queryFileName != "")){
vector<string> files; files.push_back(queryFileName);
parser.getNameFile(files);
}
if(aligned == true){
if((reportFileName != "" && qualFileName == "") || (reportFileName == "" && qualFileName != "")){
m->mothurOut("if you use either a qual file or a report file, you have to have both.");
m->mothurOutEndLine();
abort = true;
}
}
else{
if(reportFileName != ""){
m->mothurOut("we are ignoring the report file if your sequences are not aligned. we will check that the sequences in your fasta and and qual file are the same length.");
m->mothurOutEndLine();
}
}
}
}
catch(exception& e) {
m->errorOut(e, "SeqErrorCommand", "SeqErrorCommand");
exit(1);
}
}
//***************************************************************************************************************
int SeqErrorCommand::execute(){
try{
if (abort == true) { if (calledHelp) { return 0; } return 2; }
int start = time(NULL);
maxLength = 5000;
totalBases = 0;
totalMatches = 0;
string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(queryFileName));
map<string, string> variables;
variables["[filename]"] = fileNameRoot;
string errorSummaryFileName = getOutputFileName("errorsummary",variables);
outputNames.push_back(errorSummaryFileName); outputTypes["errorsummary"].push_back(errorSummaryFileName);
string errorSeqFileName = getOutputFileName("errorseq",variables);
outputNames.push_back(errorSeqFileName); outputTypes["errorseq"].push_back(errorSeqFileName);
string errorChimeraFileName = getOutputFileName("errorchimera",variables);
outputNames.push_back(errorChimeraFileName); outputTypes["errorchimera"].push_back(errorChimeraFileName);
getReferences(); //read in reference sequences - make sure there's no ambiguous bases
if(namesFileName != "") { weights = getWeights(); }
else if (countfile != "") {
CountTable ct;
ct.readTable(countfile, false, false);
weights = ct.getNameMap();
}
vector<unsigned long long> fastaFilePos;
vector<unsigned long long> qFilePos;
vector<unsigned long long> reportFilePos;
setLines(queryFileName, qualFileName, reportFileName, fastaFilePos, qFilePos, reportFilePos);
if (m->control_pressed) { return 0; }
for (int i = 0; i < (fastaFilePos.size()-1); i++) {
lines.push_back(linePair(fastaFilePos[i], fastaFilePos[(i+1)]));
if (qualFileName != "") { qLines.push_back(linePair(qFilePos[i], qFilePos[(i+1)])); }
if (reportFileName != "" && aligned == true) { rLines.push_back(linePair(reportFilePos[i], reportFilePos[(i+1)])); }
}
if(qualFileName == "") { qLines = lines; rLines = lines; } //fills with duds
if(aligned == false){ rLines = lines; }
int numSeqs = 0;
#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
if(processors == 1){
numSeqs = driver(queryFileName, qualFileName, reportFileName, errorSummaryFileName, errorSeqFileName, errorChimeraFileName, lines[0], qLines[0], rLines[0]);
}else{
numSeqs = createProcesses(queryFileName, qualFileName, reportFileName, errorSummaryFileName, errorSeqFileName, errorChimeraFileName);
}
#else
numSeqs = driver(queryFileName, qualFileName, reportFileName, errorSummaryFileName, errorSeqFileName, errorChimeraFileName, lines[0], qLines[0], rLines[0]);
#endif
if(qualFileName != ""){
printErrorQuality(qScoreErrorMap);
printQualityFR(qualForwardMap, qualReverseMap);
}
printErrorFRFile(errorForward, errorReverse);
if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
string errorCountFileName = getOutputFileName("errorcount",variables);
ofstream errorCountFile;
m->openOutputFile(errorCountFileName, errorCountFile);
outputNames.push_back(errorCountFileName); outputTypes["errorcount"].push_back(errorCountFileName);
m->mothurOut("Overall error rate:\t" + toString((double)(totalBases - totalMatches) / (double)totalBases) + "\n");
m->mothurOut("Errors\tSequences\n");
errorCountFile << "Errors\tSequences\n";
for(int i=0;i<misMatchCounts.size();i++){
m->mothurOut(toString(i) + '\t' + toString(misMatchCounts[i]) + '\n');
errorCountFile << i << '\t' << misMatchCounts[i] << endl;
}
errorCountFile.close();
// if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
printSubMatrix();
string megAlignmentFileName = getOutputFileName("errorref-query",variables);
ofstream megAlignmentFile;
m->openOutputFile(megAlignmentFileName, megAlignmentFile);
outputNames.push_back(megAlignmentFileName); outputTypes["errorref-query"].push_back(megAlignmentFileName);
for(int i=0;i<numRefs;i++){
megAlignmentFile << referenceSeqs[i].getInlineSeq() << endl;
megAlignmentFile << megaAlignVector[i] << endl;
}
megAlignmentFile.close();
m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences.");
m->mothurOutEndLine();
//set fasta file as new current fastafile
string current = "";
itTypes = outputTypes.find("errorseq");
if (itTypes != outputTypes.end()) {
if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
}
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;
}
catch(exception& e) {
m->errorOut(e, "SeqErrorCommand", "execute");
exit(1);
}
}
//**********************************************************************************************************************
int SeqErrorCommand::createProcesses(string filename, string qFileName, string rFileName, string summaryFileName, string errorOutputFileName, string chimeraOutputFileName) {
try {
int process = 1;
processIDS.clear();
map<char, vector<int> >::iterator it;
int num = 0;
#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
//loop through and create all the processes you want
while (process != processors) {
int pid = fork();
if (pid > 0) {
processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
process++;
}else if (pid == 0){
num = driver(filename, qFileName, rFileName, summaryFileName + toString(getpid()) + ".temp", errorOutputFileName+ toString(getpid()) + ".temp", chimeraOutputFileName + toString(getpid()) + ".temp", lines[process], qLines[process], rLines[process]);
//pass groupCounts to parent
ofstream out;
string tempFile = filename + toString(getpid()) + ".info.temp";
m->openOutputFile(tempFile, out);
//output totalBases and totalMatches
out << num << '\t' << totalBases << '\t' << totalMatches << endl << endl;
//output substitutionMatrix
for(int i = 0; i < substitutionMatrix.size(); i++) {
for (int j = 0; j < substitutionMatrix[i].size(); j++) {
out << substitutionMatrix[i][j] << '\t';
}
out << endl;
}
out << endl;
//output qScoreErrorMap
for (it = qScoreErrorMap.begin(); it != qScoreErrorMap.end(); it++) {
vector<int> thisScoreErrorMap = it->second;
out << it->first << '\t';
for (int i = 0; i < thisScoreErrorMap.size(); i++) {
out << thisScoreErrorMap[i] << '\t';
}
out << endl;
}
out << endl;
//output qualForwardMap
for(int i = 0; i < qualForwardMap.size(); i++) {
for (int j = 0; j < qualForwardMap[i].size(); j++) {
out << qualForwardMap[i][j] << '\t';
}
out << endl;
}
out << endl;
//output qualReverseMap
for(int i = 0; i < qualReverseMap.size(); i++) {
for (int j = 0; j < qualReverseMap[i].size(); j++) {
out << qualReverseMap[i][j] << '\t';
}
out << endl;
}
out << endl;
//output errorForward
for (it = errorForward.begin(); it != errorForward.end(); it++) {
vector<int> thisErrorForward = it->second;
out << it->first << '\t';
for (int i = 0; i < thisErrorForward.size(); i++) {
out << thisErrorForward[i] << '\t';
}
out << endl;
}
out << endl;
//output errorReverse
for (it = errorReverse.begin(); it != errorReverse.end(); it++) {
vector<int> thisErrorReverse = it->second;
out << it->first << '\t';
for (int i = 0; i < thisErrorReverse.size(); i++) {
out << thisErrorReverse[i] << '\t';
}
out << endl;
}
out << endl;
//output misMatchCounts
out << misMatchCounts.size() << endl;
for (int j = 0; j < misMatchCounts.size(); j++) {
out << misMatchCounts[j] << '\t';
}
out << endl;
//output megaAlignVector
for (int j = 0; j < megaAlignVector.size(); j++) {
out << megaAlignVector[j] << endl;
}
out << endl;
out.close();
exit(0);
}else {
m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
exit(0);
}
}
//do my part
num = driver(filename, qFileName, rFileName, summaryFileName, errorOutputFileName, chimeraOutputFileName, lines[0], qLines[0], rLines[0]);
//force parent to wait until all the processes are done
for (int i=0;i<processIDS.size();i++) {
int temp = processIDS[i];
wait(&temp);
}
//append files
for(int i=0;i<processIDS.size();i++){
m->mothurOut("Appending files from process " + toString(processIDS[i])); m->mothurOutEndLine();
m->appendFiles((summaryFileName + toString(processIDS[i]) + ".temp"), summaryFileName);
m->mothurRemove((summaryFileName + toString(processIDS[i]) + ".temp"));
m->appendFiles((errorOutputFileName + toString(processIDS[i]) + ".temp"), errorOutputFileName);
m->mothurRemove((errorOutputFileName + toString(processIDS[i]) + ".temp"));
m->appendFiles((chimeraOutputFileName + toString(processIDS[i]) + ".temp"), chimeraOutputFileName);
m->mothurRemove((chimeraOutputFileName + toString(processIDS[i]) + ".temp"));
ifstream in;
string tempFile = filename + toString(processIDS[i]) + ".info.temp";
m->openInputFile(tempFile, in);
//input totalBases and totalMatches
int tempBases, tempMatches, tempNumSeqs;
in >> tempNumSeqs >> tempBases >> tempMatches; m->gobble(in);
totalBases += tempBases; totalMatches += tempMatches; num += tempNumSeqs;
//input substitutionMatrix
int tempNum;
for(int i = 0; i < substitutionMatrix.size(); i++) {
for (int j = 0; j < substitutionMatrix[i].size(); j++) {
in >> tempNum; substitutionMatrix[i][j] += tempNum;
}
m->gobble(in);
}
m->gobble(in);
//input qScoreErrorMap
char first;
for (int i = 0; i < qScoreErrorMap.size(); i++) {
in >> first;
vector<int> thisScoreErrorMap = qScoreErrorMap[first];
for (int i = 0; i < thisScoreErrorMap.size(); i++) {
in >> tempNum; thisScoreErrorMap[i] += tempNum;
}
qScoreErrorMap[first] = thisScoreErrorMap;
m->gobble(in);
}
m->gobble(in);
//input qualForwardMap
for(int i = 0; i < qualForwardMap.size(); i++) {
for (int j = 0; j < qualForwardMap[i].size(); j++) {
in >> tempNum; qualForwardMap[i][j] += tempNum;
}
m->gobble(in);
}
m->gobble(in);
//input qualReverseMap
for(int i = 0; i < qualReverseMap.size(); i++) {
for (int j = 0; j < qualReverseMap[i].size(); j++) {
in >> tempNum; qualReverseMap[i][j] += tempNum;
}
m->gobble(in);
}
m->gobble(in);
//input errorForward
for (int i = 0; i < errorForward.size(); i++) {
in >> first;
vector<int> thisErrorForward = errorForward[first];
for (int i = 0; i < thisErrorForward.size(); i++) {
in >> tempNum; thisErrorForward[i] += tempNum;
}
errorForward[first] = thisErrorForward;
m->gobble(in);
}
m->gobble(in);
//input errorReverse
for (int i = 0; i < errorReverse.size(); i++) {
in >> first;
vector<int> thisErrorReverse = errorReverse[first];
for (int i = 0; i < thisErrorReverse.size(); i++) {
in >> tempNum; thisErrorReverse[i] += tempNum;
}
errorReverse[first] = thisErrorReverse;
m->gobble(in);
}
m->gobble(in);
//input misMatchCounts
int misMatchSize;
in >> misMatchSize; m->gobble(in);
if (misMatchSize > misMatchCounts.size()) { misMatchCounts.resize(misMatchSize, 0); }
for (int j = 0; j < misMatchSize; j++) {
in >> tempNum; misMatchCounts[j] += tempNum;
}
m->gobble(in);
//input megaAlignVector
string thisLine;
for (int j = 0; j < megaAlignVector.size(); j++) {
thisLine = m->getline(in); m->gobble(in); megaAlignVector[j] += thisLine + '\n';
}
m->gobble(in);
in.close(); m->mothurRemove(tempFile);
}
#endif
return num;
}
catch(exception& e) {
m->errorOut(e, "SeqErrorCommand", "createProcesses");
exit(1);
}
}
//**********************************************************************************************************************
int SeqErrorCommand::driver(string filename, string qFileName, string rFileName, string summaryFileName, string errorOutputFileName, string chimeraOutputFileName, linePair line, linePair qline, linePair rline) {
try {
ReportFile report;
QualityScores quality;
misMatchCounts.resize(11, 0);
int maxMismatch = 0;
int numSeqs = 0;
map<string, int>::iterator it;
qScoreErrorMap['m'].assign(101, 0);
qScoreErrorMap['s'].assign(101, 0);
qScoreErrorMap['i'].assign(101, 0);
qScoreErrorMap['a'].assign(101, 0);
errorForward['m'].assign(maxLength,0);
errorForward['s'].assign(maxLength,0);
errorForward['i'].assign(maxLength,0);
errorForward['d'].assign(maxLength,0);
errorForward['a'].assign(maxLength,0);
errorReverse['m'].assign(maxLength,0);
errorReverse['s'].assign(maxLength,0);
errorReverse['i'].assign(maxLength,0);
errorReverse['d'].assign(maxLength,0);
errorReverse['a'].assign(maxLength,0);
//open inputfiles and go to beginning place for this processor
ifstream queryFile;
m->openInputFile(filename, queryFile);
queryFile.seekg(line.start);
ifstream reportFile;
ifstream qualFile;
if((qFileName != "" && rFileName != "" && aligned)){
m->openInputFile(qFileName, qualFile);
qualFile.seekg(qline.start);
//gobble headers
if (rline.start == 0) { report = ReportFile(reportFile, rFileName); }
else{
m->openInputFile(rFileName, reportFile);
reportFile.seekg(rline.start);
}
qualForwardMap.resize(maxLength);
qualReverseMap.resize(maxLength);
for(int i=0;i<maxLength;i++){
qualForwardMap[i].assign(101,0);
qualReverseMap[i].assign(101,0);
}
}
else if(qFileName != "" && !aligned){
m->openInputFile(qFileName, qualFile);
qualFile.seekg(qline.start);
qualForwardMap.resize(maxLength);
qualReverseMap.resize(maxLength);
for(int i=0;i<maxLength;i++){
qualForwardMap[i].assign(101,0);
qualReverseMap[i].assign(101,0);
}
}
ofstream outChimeraReport;
m->openOutputFile(chimeraOutputFileName, outChimeraReport);
RefChimeraTest chimeraTest;
chimeraTest = RefChimeraTest(referenceSeqs, aligned);
if (line.start == 0) { chimeraTest.printHeader(outChimeraReport); }
ofstream errorSummaryFile;
m->openOutputFile(summaryFileName, errorSummaryFile);
if (line.start == 0) { printErrorHeader(errorSummaryFile); }
ofstream errorSeqFile;
m->openOutputFile(errorOutputFileName, errorSeqFile);
megaAlignVector.assign(numRefs, "");
int index = 0;
bool ignoreSeq = 0;
bool moreSeqs = 1;
while (moreSeqs) {
Sequence query(queryFile);
Sequence reference;
int numParentSeqs = -1;
int closestRefIndex = -1;
string querySeq = query.getAligned();
if (!aligned) { querySeq = query.getUnaligned(); }
numParentSeqs = chimeraTest.analyzeQuery(query.getName(), querySeq, outChimeraReport);
closestRefIndex = chimeraTest.getClosestRefIndex();
reference = referenceSeqs[closestRefIndex];
reference.setAligned(chimeraTest.getClosestRefAlignment());
query.setAligned(chimeraTest.getQueryAlignment());
if(numParentSeqs > 1 && ignoreChimeras == 1) { ignoreSeq = 1; }
else { ignoreSeq = 0; }
Compare minCompare;
getErrors(query, reference, minCompare);
if((namesFileName != "") || (countfile != "")){
it = weights.find(query.getName());
minCompare.weight = it->second;
}
else{ minCompare.weight = 1; }
printErrorData(minCompare, numParentSeqs, errorSummaryFile, errorSeqFile);
if(!ignoreSeq){
for(int i=0;i<minCompare.sequence.length();i++){
char letter = minCompare.sequence[i];
if(letter != 'r'){
errorForward[letter][i] += minCompare.weight;
errorReverse[letter][minCompare.total-i-1] += minCompare.weight;
}
}
}
if(aligned && qualFileName != "" && reportFileName != ""){
report = ReportFile(reportFile);
// int origLength = report.getQueryLength();
int startBase = report.getQueryStart();
int endBase = report.getQueryEnd();
quality = QualityScores(qualFile);
if(!ignoreSeq){
quality.updateQScoreErrorMap(qScoreErrorMap, minCompare.sequence, startBase, endBase, minCompare.weight);
quality.updateForwardMap(qualForwardMap, startBase, endBase, minCompare.weight);
quality.updateReverseMap(qualReverseMap, startBase, endBase, minCompare.weight);
}
}
else if(aligned == false && qualFileName != ""){
quality = QualityScores(qualFile);
int qualityLength = quality.getLength();
if(qualityLength != query.getNumBases()){ cout << "warning - quality and fasta sequence files do not match at " << query.getName() << '\t' << qualityLength <<'\t' << query.getNumBases() << endl; }
int startBase = 1;
int endBase = qualityLength;
if(!ignoreSeq){
quality.updateQScoreErrorMap(qScoreErrorMap, minCompare.sequence, startBase, endBase, minCompare.weight);
quality.updateForwardMap(qualForwardMap, startBase, endBase, minCompare.weight);
quality.updateReverseMap(qualReverseMap, startBase, endBase, minCompare.weight);
}
}
if(minCompare.errorRate <= threshold && !ignoreSeq){
totalBases += (minCompare.total * minCompare.weight);
totalMatches += minCompare.matches * minCompare.weight;
if(minCompare.mismatches > maxMismatch){
maxMismatch = minCompare.mismatches;
misMatchCounts.resize(maxMismatch + 1, 0);
}
misMatchCounts[minCompare.mismatches] += minCompare.weight;
numSeqs++;
megaAlignVector[closestRefIndex] += query.getInlineSeq() + '\n';
}
index++;
#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
unsigned long long pos = queryFile.tellg();
if ((pos == -1) || (pos >= line.end)) { break; }
#else
if (queryFile.eof()) { break; }
#endif
if(index % 100 == 0){ m->mothurOutJustToScreen(toString(index)+"\n"); }
}
queryFile.close();
outChimeraReport.close();
errorSummaryFile.close();
errorSeqFile.close();
if(qFileName != "" && rFileName != "") { reportFile.close(); qualFile.close(); }
else if(qFileName != "" && aligned == false){ qualFile.close(); }
//report progress
m->mothurOutJustToScreen(toString(index)+"\n");
return index;
}
catch(exception& e) {
m->errorOut(e, "SeqErrorCommand", "driver");
exit(1);
}
}
//***************************************************************************************************************
void SeqErrorCommand::getReferences(){
try {
int numAmbigSeqs = 0;
int maxStartPos = 0;
int minEndPos = 100000;
if (referenceFileName == "saved") {
int start = time(NULL);
m->mothurOutEndLine(); m->mothurOut("Using sequences from " + rdb->getSavedReference() + " that are saved in memory."); m->mothurOutEndLine();
for (int i = 0; i < rdb->referenceSeqs.size(); i++) {
int numAmbigs = rdb->referenceSeqs[i].getAmbigBases();
if(numAmbigs > 0){ numAmbigSeqs++; }
// int startPos = rdb->referenceSeqs[i].getStartPos();
// if(startPos > maxStartPos) { maxStartPos = startPos; }
//
// int endPos = rdb->referenceSeqs[i].getEndPos();
// if(endPos < minEndPos) { minEndPos = endPos; }
if (rdb->referenceSeqs[i].getNumBases() == 0) {
m->mothurOut("[WARNING]: " + rdb->referenceSeqs[i].getName() + " is blank, ignoring.");m->mothurOutEndLine();
}else {
referenceSeqs.push_back(rdb->referenceSeqs[i]);
}
}
referenceFileName = rdb->getSavedReference();
m->mothurOut("It took " + toString(time(NULL) - start) + " to load " + toString(referenceSeqs.size()) + " sequences.");m->mothurOutEndLine();
}else {
int start = time(NULL);
ifstream referenceFile;
m->openInputFile(referenceFileName, referenceFile);
while(referenceFile){
Sequence currentSeq(referenceFile);
int numAmbigs = currentSeq.getAmbigBases();
if(numAmbigs > 0){ numAmbigSeqs++; }
// int startPos = currentSeq.getStartPos();
// if(startPos > maxStartPos) { maxStartPos = startPos; }
//
// int endPos = currentSeq.getEndPos();
// if(endPos < minEndPos) { minEndPos = endPos; }
if (currentSeq.getNumBases() == 0) {
m->mothurOut("[WARNING]: " + currentSeq.getName() + " is blank, ignoring.");m->mothurOutEndLine();
}else {
referenceSeqs.push_back(currentSeq);
if (rdb->save) { rdb->referenceSeqs.push_back(currentSeq); }
}
m->gobble(referenceFile);
}
referenceFile.close();
m->mothurOut("It took " + toString(time(NULL) - start) + " to read " + toString(referenceSeqs.size()) + " sequences.");m->mothurOutEndLine();
}
numRefs = referenceSeqs.size();
for(int i=0;i<numRefs;i++){
referenceSeqs[i].padToPos(maxStartPos);
referenceSeqs[i].padFromPos(minEndPos);
}
if(numAmbigSeqs != 0){
m->mothurOut("Warning: " + toString(numAmbigSeqs) + " reference sequences have ambiguous bases, these bases will be ignored\n");
}
}
catch(exception& e) {
m->errorOut(e, "SeqErrorCommand", "getReferences");
exit(1);
}
}
//***************************************************************************************************************
int SeqErrorCommand::getErrors(Sequence query, Sequence reference, Compare& errors){
try {
if(query.getAlignLength() != reference.getAlignLength()){
m->mothurOut("Warning: " + toString(query.getName()) + " and " + toString(reference.getName()) + " are different lengths\n");
}
int alignLength = query.getAlignLength();
string q = query.getAligned();
string r = reference.getAligned();
int started = 0;
//Compare errors;
errors.sequence = "";
for(int i=0;i<alignLength;i++){
if(q[i] != '.' && r[i] != '.' && (q[i] != '-' || r[i] != '-')){ // no missing data and no double gaps
if(r[i] != 'N'){
started = 1;
if(q[i] == 'A'){
if(r[i] == 'A'){ errors.AA++; errors.matches++; errors.sequence += 'm'; }
if(r[i] == 'T'){ errors.AT++; errors.sequence += 's'; }
if(r[i] == 'G'){ errors.AG++; errors.sequence += 's'; }
if(r[i] == 'C'){ errors.AC++; errors.sequence += 's'; }
if(r[i] == '-'){ errors.Ai++; errors.sequence += 'i'; }
}
else if(q[i] == 'T'){
if(r[i] == 'A'){ errors.TA++; errors.sequence += 's'; }
if(r[i] == 'T'){ errors.TT++; errors.matches++; errors.sequence += 'm'; }
if(r[i] == 'G'){ errors.TG++; errors.sequence += 's'; }
if(r[i] == 'C'){ errors.TC++; errors.sequence += 's'; }
if(r[i] == '-'){ errors.Ti++; errors.sequence += 'i'; }
}
else if(q[i] == 'G'){
if(r[i] == 'A'){ errors.GA++; errors.sequence += 's'; }
if(r[i] == 'T'){ errors.GT++; errors.sequence += 's'; }
if(r[i] == 'G'){ errors.GG++; errors.matches++; errors.sequence += 'm'; }
if(r[i] == 'C'){ errors.GC++; errors.sequence += 's'; }
if(r[i] == '-'){ errors.Gi++; errors.sequence += 'i'; }
}
else if(q[i] == 'C'){
if(r[i] == 'A'){ errors.CA++; errors.sequence += 's'; }
if(r[i] == 'T'){ errors.CT++; errors.sequence += 's'; }
if(r[i] == 'G'){ errors.CG++; errors.sequence += 's'; }
if(r[i] == 'C'){ errors.CC++; errors.matches++; errors.sequence += 'm'; }
if(r[i] == '-'){ errors.Ci++; errors.sequence += 'i'; }
}
else if(q[i] == 'N'){
if(r[i] == 'A'){ errors.NA++; errors.sequence += 'a'; }
if(r[i] == 'T'){ errors.NT++; errors.sequence += 'a'; }
if(r[i] == 'G'){ errors.NG++; errors.sequence += 'a'; }
if(r[i] == 'C'){ errors.NC++; errors.sequence += 'a'; }
if(r[i] == '-'){ errors.Ni++; errors.sequence += 'a'; }
}
else if(q[i] == '-' && r[i] != '-'){
if(r[i] == 'A'){ errors.dA++; errors.sequence += 'd'; }
if(r[i] == 'T'){ errors.dT++; errors.sequence += 'd'; }
if(r[i] == 'G'){ errors.dG++; errors.sequence += 'd'; }
if(r[i] == 'C'){ errors.dC++; errors.sequence += 'd'; }
}
errors.total++;
}
else{
if(q[i] == '-'){
errors.sequence += 'd'; errors.total++;
}
else{
errors.sequence += 'r';
}
}
}
else if(q[i] == '.' && r[i] != '.'){ // reference extends beyond query
if(started == 1){ break; }
}
else if(q[i] != '.' && r[i] == '.'){ // query extends beyond reference
if(started == 1){ break; }
}
else if(q[i] == '.' && r[i] == '.'){ // both are missing data
if(started == 1){ break; }
}
}
errors.mismatches = errors.total-errors.matches;
if(errors.total != 0){ errors.errorRate = (double)(errors.total-errors.matches) / (double)errors.total; }
else{ errors.errorRate = 0; }
errors.queryName = query.getName();
errors.refName = reference.getName();
return 0;
}
catch(exception& e) {
m->errorOut(e, "SeqErrorCommand", "getErrors");
exit(1);
}
}
//***************************************************************************************************************
map<string, int> SeqErrorCommand::getWeights(){
ifstream nameFile;
m->openInputFile(namesFileName, nameFile);
string seqName;
string redundantSeqs;
map<string, int> nameCountMap;
while(nameFile){
nameFile >> seqName >> redundantSeqs;
nameCountMap[seqName] = m->getNumNames(redundantSeqs);
m->gobble(nameFile);
}
nameFile.close();
return nameCountMap;
}
//***************************************************************************************************************
void SeqErrorCommand::printErrorHeader(ofstream& errorSummaryFile){
try {
errorSummaryFile << "query\treference\tweight\t";
errorSummaryFile << "AA\tAT\tAG\tAC\tTA\tTT\tTG\tTC\tGA\tGT\tGG\tGC\tCA\tCT\tCG\tCC\tNA\tNT\tNG\tNC\tAi\tTi\tGi\tCi\tNi\tdA\tdT\tdG\tdC\t";
errorSummaryFile << "insertions\tdeletions\tsubstitutions\tambig\tmatches\tmismatches\ttotal\terror\tnumparents\n";
errorSummaryFile << setprecision(6);
errorSummaryFile.setf(ios::fixed);
}
catch(exception& e) {
m->errorOut(e, "SeqErrorCommand", "printErrorHeader");
exit(1);
}
}
//***************************************************************************************************************
void SeqErrorCommand::printErrorData(Compare error, int numParentSeqs, ofstream& errorSummaryFile, ofstream& errorSeqFile){
try {
errorSummaryFile << error.queryName << '\t' << error.refName << '\t' << error.weight << '\t';
errorSummaryFile << error.AA << '\t' << error.AT << '\t' << error.AG << '\t' << error.AC << '\t';
errorSummaryFile << error.TA << '\t' << error.TT << '\t' << error.TG << '\t' << error.TC << '\t';
errorSummaryFile << error.GA << '\t' << error.GT << '\t' << error.GG << '\t' << error.GC << '\t';
errorSummaryFile << error.CA << '\t' << error.CT << '\t' << error.CG << '\t' << error.CC << '\t';
errorSummaryFile << error.NA << '\t' << error.NT << '\t' << error.NG << '\t' << error.NC << '\t';
errorSummaryFile << error.Ai << '\t' << error.Ti << '\t' << error.Gi << '\t' << error.Ci << '\t' << error.Ni << '\t';
errorSummaryFile << error.dA << '\t' << error.dT << '\t' << error.dG << '\t' << error.dC << '\t';
errorSummaryFile << error.Ai + error.Ti + error.Gi + error.Ci << '\t'; //insertions
errorSummaryFile << error.dA + error.dT + error.dG + error.dC << '\t'; //deletions
errorSummaryFile << error.mismatches - (error.Ai + error.Ti + error.Gi + error.Ci) - (error.dA + error.dT + error.dG + error.dC) - (error.NA + error.NT + error.NG + error.NC + error.Ni) << '\t'; //substitutions
errorSummaryFile << error.NA + error.NT + error.NG + error.NC + error.Ni << '\t'; //ambiguities
errorSummaryFile << error.matches << '\t' << error.mismatches << '\t' << error.total << '\t' << error.errorRate << '\t' << numParentSeqs << endl;
errorSeqFile << '>' << error.queryName << "\tref:" << error.refName << '\n' << error.sequence << endl;
int a=0; int t=1; int g=2; int c=3;
int gap=4; int n=5;
if(numParentSeqs == 1 || ignoreChimeras == 0){
substitutionMatrix[a][a] += error.weight * error.AA;
substitutionMatrix[a][t] += error.weight * error.TA;
substitutionMatrix[a][g] += error.weight * error.GA;
substitutionMatrix[a][c] += error.weight * error.CA;
substitutionMatrix[a][gap] += error.weight * error.dA;
substitutionMatrix[a][n] += error.weight * error.NA;
substitutionMatrix[t][a] += error.weight * error.AT;
substitutionMatrix[t][t] += error.weight * error.TT;
substitutionMatrix[t][g] += error.weight * error.GT;
substitutionMatrix[t][c] += error.weight * error.CT;
substitutionMatrix[t][gap] += error.weight * error.dT;
substitutionMatrix[t][n] += error.weight * error.NT;
substitutionMatrix[g][a] += error.weight * error.AG;
substitutionMatrix[g][t] += error.weight * error.TG;
substitutionMatrix[g][g] += error.weight * error.GG;
substitutionMatrix[g][c] += error.weight * error.CG;
substitutionMatrix[g][gap] += error.weight * error.dG;
substitutionMatrix[g][n] += error.weight * error.NG;
substitutionMatrix[c][a] += error.weight * error.AC;
substitutionMatrix[c][t] += error.weight * error.TC;
substitutionMatrix[c][g] += error.weight * error.GC;
substitutionMatrix[c][c] += error.weight * error.CC;
substitutionMatrix[c][gap] += error.weight * error.dC;
substitutionMatrix[c][n] += error.weight * error.NC;
substitutionMatrix[gap][a] += error.weight * error.Ai;
substitutionMatrix[gap][t] += error.weight * error.Ti;
substitutionMatrix[gap][g] += error.weight * error.Gi;
substitutionMatrix[gap][c] += error.weight * error.Ci;
substitutionMatrix[gap][n] += error.weight * error.Ni;
}
}
catch(exception& e) {
m->errorOut(e, "SeqErrorCommand", "printErrorData");
exit(1);
}
}
//***************************************************************************************************************
void SeqErrorCommand::printSubMatrix(){
try {
string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(queryFileName));
map<string, string> variables;
variables["[filename]"] = fileNameRoot;
string subMatrixFileName = getOutputFileName("errormatrix",variables);
ofstream subMatrixFile;
m->openOutputFile(subMatrixFileName, subMatrixFile);
outputNames.push_back(subMatrixFileName); outputTypes["errormatrix"].push_back(subMatrixFileName);
vector<string> bases(6);
bases[0] = "A";
bases[1] = "T";
bases[2] = "G";
bases[3] = "C";
bases[4] = "Gap";
bases[5] = "N";
vector<int> refSums(5,1);
for(int i=0;i<5;i++){
subMatrixFile << "\tr" << bases[i];
for(int j=0;j<6;j++){
refSums[i] += substitutionMatrix[i][j];
}
}
subMatrixFile << endl;
for(int i=0;i<6;i++){
subMatrixFile << 'q' << bases[i];
for(int j=0;j<5;j++){
subMatrixFile << '\t' << substitutionMatrix[j][i];
}
subMatrixFile << endl;
}
subMatrixFile << "total";
for(int i=0;i<5;i++){
subMatrixFile << '\t' << refSums[i];
}
subMatrixFile << endl;
subMatrixFile.close();
}
catch(exception& e) {
m->errorOut(e, "SeqErrorCommand", "printSubMatrix");
exit(1);
}
}
//***************************************************************************************************************
void SeqErrorCommand::printErrorFRFile(map<char, vector<int> > errorForward, map<char, vector<int> > errorReverse){
try{
string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(queryFileName));
map<string, string> variables;
variables["[filename]"] = fileNameRoot;
string errorForwardFileName = getOutputFileName("errorforward",variables);
ofstream errorForwardFile;
m->openOutputFile(errorForwardFileName, errorForwardFile);
outputNames.push_back(errorForwardFileName); outputTypes["errorforward"].push_back(errorForwardFileName);
errorForwardFile << "position\ttotalseqs\tmatch\tsubstitution\tinsertion\tdeletion\tambiguous" << endl;
for(int i=0;i<maxLength;i++){
float match = (float)errorForward['m'][i];
float subst = (float)errorForward['s'][i];
float insert = (float)errorForward['i'][i];
float del = (float)errorForward['d'][i];
float amb = (float)errorForward['a'][i];
float total = match + subst + insert + del + amb;
if(total == 0){ break; }
errorForwardFile << i+1 << '\t' << total << '\t' << match/total << '\t' << subst/total << '\t' << insert/total << '\t' << del/total << '\t' << amb/total << endl;
}
errorForwardFile.close();
string errorReverseFileName = getOutputFileName("errorreverse",variables);
ofstream errorReverseFile;
m->openOutputFile(errorReverseFileName, errorReverseFile);
outputNames.push_back(errorReverseFileName); outputTypes["errorreverse"].push_back(errorReverseFileName);
errorReverseFile << "position\ttotalseqs\tmatch\tsubstitution\tinsertion\tdeletion\tambiguous" << endl;
for(int i=0;i<maxLength;i++){
float match = (float)errorReverse['m'][i];
float subst = (float)errorReverse['s'][i];
float insert = (float)errorReverse['i'][i];
float del = (float)errorReverse['d'][i];
float amb = (float)errorReverse['a'][i];
float total = match + subst + insert + del + amb;
if(total == 0){ break; }
errorReverseFile << i+1 << '\t' << total << '\t' << match/total << '\t' << subst/total << '\t' << insert/total << '\t' << del/total << '\t' << amb/total << endl;
}
errorReverseFile.close();
}
catch(exception& e) {
m->errorOut(e, "SeqErrorCommand", "printErrorFRFile");
exit(1);
}
}
//***************************************************************************************************************
void SeqErrorCommand::printErrorQuality(map<char, vector<int> > qScoreErrorMap){
try{
string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(queryFileName));
map<string, string> variables;
variables["[filename]"] = fileNameRoot;
string errorQualityFileName = getOutputFileName("errorquality",variables);
ofstream errorQualityFile;
m->openOutputFile(errorQualityFileName, errorQualityFile);
outputNames.push_back(errorQualityFileName); outputTypes["errorquality"].push_back(errorQualityFileName);
errorQualityFile << "qscore\tmatches\tsubstitutions\tinsertions\tambiguous" << endl;
for(int i=0;i<101;i++){
errorQualityFile << i << '\t' << qScoreErrorMap['m'][i] << '\t' << qScoreErrorMap['s'][i] << '\t' << qScoreErrorMap['i'][i] << '\t'<< qScoreErrorMap['a'][i] << endl;
}
errorQualityFile.close();
}
catch(exception& e) {
m->errorOut(e, "SeqErrorCommand", "printErrorQuality");
exit(1);
}
}
//***************************************************************************************************************
void SeqErrorCommand::printQualityFR(vector<vector<int> > qualForwardMap, vector<vector<int> > qualReverseMap){
try{
int numRows = 0;
int numColumns = qualForwardMap[0].size();
for(int i=0;i<qualForwardMap.size();i++){
for(int j=0;j<numColumns;j++){
if(qualForwardMap[i][j] != 0){
if(numRows < i) { numRows = i+20; }
}
}
}
string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(queryFileName));
map<string, string> variables;
variables["[filename]"] = fileNameRoot;
string qualityForwardFileName = getOutputFileName("errorqualforward",variables);
ofstream qualityForwardFile;
m->openOutputFile(qualityForwardFileName, qualityForwardFile);
outputNames.push_back(qualityForwardFileName); outputTypes["errorqualforward"].push_back(qualityForwardFileName);
for(int i=0;i<numColumns;i++){ qualityForwardFile << '\t' << i; } qualityForwardFile << endl;
for(int i=0;i<numRows;i++){
qualityForwardFile << i+1;
for(int j=0;j<numColumns;j++){
qualityForwardFile << '\t' << qualForwardMap[i][j];
}
qualityForwardFile << endl;
}
qualityForwardFile.close();
string qualityReverseFileName = getOutputFileName("errorqualreverse",variables);
ofstream qualityReverseFile;
m->openOutputFile(qualityReverseFileName, qualityReverseFile);
outputNames.push_back(qualityReverseFileName); outputTypes["errorqualreverse"].push_back(qualityReverseFileName);
for(int i=0;i<numColumns;i++){ qualityReverseFile << '\t' << i; } qualityReverseFile << endl;
for(int i=0;i<numRows;i++){
qualityReverseFile << i+1;
for(int j=0;j<numColumns;j++){
qualityReverseFile << '\t' << qualReverseMap[i][j];
}
qualityReverseFile << endl;
}
qualityReverseFile.close();
}
catch(exception& e) {
m->errorOut(e, "SeqErrorCommand", "printQualityFR");
exit(1);
}
}
/**************************************************************************************************/
int SeqErrorCommand::setLines(string filename, string qfilename, string rfilename, vector<unsigned long long>& fastaFilePos, vector<unsigned long long>& qfileFilePos, vector<unsigned long long>& rfileFilePos) {
try {
#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
//set file positions for fasta file
fastaFilePos = m->divideFile(filename, processors);
if (qfilename == "") { return processors; }
//get name of first sequence in each chunk
map<string, int> firstSeqNames;
for (int i = 0; i < (fastaFilePos.size()-1); i++) {
ifstream in;
m->openInputFile(filename, in);
in.seekg(fastaFilePos[i]);
Sequence temp(in);
firstSeqNames[temp.getName()] = i;
in.close();
}
//make copy to use below
map<string, int> firstSeqNamesReport = firstSeqNames;
//seach for filePos of each first name in the qfile and save in qfileFilePos
ifstream inQual;
m->openInputFile(qfilename, inQual);
string input;
while(!inQual.eof()){
input = m->getline(inQual);
if (input.length() != 0) {
if(input[0] == '>'){ //this is a sequence name line
istringstream nameStream(input);
string sname = ""; nameStream >> sname;
sname = sname.substr(1);
m->checkName(sname);
map<string, int>::iterator it = firstSeqNames.find(sname);
if(it != firstSeqNames.end()) { //this is the start of a new chunk
unsigned long long pos = inQual.tellg();
qfileFilePos.push_back(pos - input.length() - 1);
firstSeqNames.erase(it);
}
}
}
if (firstSeqNames.size() == 0) { break; }
}
inQual.close();
if (firstSeqNames.size() != 0) {
for (map<string, int>::iterator it = firstSeqNames.begin(); it != firstSeqNames.end(); it++) {
m->mothurOut(it->first + " is in your fasta file and not in your quality file, aborting."); m->mothurOutEndLine();
}
m->control_pressed = true;
return processors;
}
//get last file position of qfile
FILE * pFile;
unsigned long long size;
//get num bytes in file
pFile = fopen (qfilename.c_str(),"rb");
if (pFile==NULL) perror ("Error opening file");
else{
fseek (pFile, 0, SEEK_END);
size=ftell (pFile);
fclose (pFile);
}
qfileFilePos.push_back(size);
if(aligned){
//seach for filePos of each first name in the rfile and save in rfileFilePos
string junk;
ifstream inR;
m->openInputFile(rfilename, inR);
//read column headers
for (int i = 0; i < 16; i++) {
if (!inR.eof()) { inR >> junk; }
else { break; }
}
while(!inR.eof()){
input = m->getline(inR);
if (input.length() != 0) {
istringstream nameStream(input);
string sname = ""; nameStream >> sname;
m->checkName(sname);
map<string, int>::iterator it = firstSeqNamesReport.find(sname);
if(it != firstSeqNamesReport.end()) { //this is the start of a new chunk
unsigned long long pos = inR.tellg();
rfileFilePos.push_back(pos - input.length() - 1);
firstSeqNamesReport.erase(it);
}
}
if (firstSeqNamesReport.size() == 0) { break; }
m->gobble(inR);
}
inR.close();
if (firstSeqNamesReport.size() != 0) {
for (map<string, int>::iterator it = firstSeqNamesReport.begin(); it != firstSeqNamesReport.end(); it++) {
m->mothurOut(it->first + " is in your fasta file and not in your report file, aborting."); m->mothurOutEndLine();
}
m->control_pressed = true;
return processors;
}
//get last file position of qfile
FILE * rFile;
unsigned long long sizeR;
//get num bytes in file
rFile = fopen (rfilename.c_str(),"rb");
if (rFile==NULL) perror ("Error opening file");
else{
fseek (rFile, 0, SEEK_END);
sizeR=ftell (rFile);
fclose (rFile);
}
rfileFilePos.push_back(sizeR);
}
return processors;
#else
fastaFilePos.push_back(0); qfileFilePos.push_back(0);
//get last file position of fastafile
FILE * pFile;
unsigned long long size;
//get num bytes in file
pFile = fopen (filename.c_str(),"rb");
if (pFile==NULL) perror ("Error opening file");
else{
fseek (pFile, 0, SEEK_END);
size=ftell (pFile);
fclose (pFile);
}
fastaFilePos.push_back(size);
//get last file position of fastafile
FILE * qFile;
//get num bytes in file
qFile = fopen (qfilename.c_str(),"rb");
if (qFile==NULL) perror ("Error opening file");
else{
fseek (qFile, 0, SEEK_END);
size=ftell (qFile);
fclose (qFile);
}
qfileFilePos.push_back(size);
return 1;
#endif
}
catch(exception& e) {
m->errorOut(e, "SeqErrorCommand", "setLines");
exit(1);
}
}
//***************************************************************************************************************
|