From: Sarah Westcott Date: Fri, 21 Dec 2012 13:26:37 +0000 (-0500) Subject: working on make.contigs. fixed trim.seqs filename issue X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=commitdiff_plain;h=f12174bc43f9e8ad536f2a37fb3a763b1ac90ba9 working on make.contigs. fixed trim.seqs filename issue --- diff --git a/makecontigscommand.cpp b/makecontigscommand.cpp index 7a84f88..15ffb07 100644 --- a/makecontigscommand.cpp +++ b/makecontigscommand.cpp @@ -87,7 +87,7 @@ string MakeContigsCommand::getOutputPattern(string type) { if (type == "fasta") { pattern = "[filename],[tag],contigs.fasta"; } else if (type == "qfile") { pattern = "[filename],[tag],contigs.qual"; } - else if (type == "group") { pattern = "[filename],[tag],groups"; } + else if (type == "group") { pattern = "[filename],[tag],contigs.groups"; } else if (type == "mismatch") { pattern = "[filename],[tag],contigs.mismatch"; } else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; } @@ -243,9 +243,18 @@ MakeContigsCommand::MakeContigsCommand(string option) { if (file == "not open") { abort = true; } else if (file == "not found") { file = ""; } - if ((file == "") && (ffastafile == "") && (ffastqfile == "")) { abort = true; m->mothurOut("[ERROR]: ffastq and rfastq parameters are required.\n"); } - if ((ffastqfile != "") && (rfastqfile == "")) { abort = true; } - if ((ffastqfile == "") && (rfastqfile != "")) { abort = true; } + //provide at least + if ((file == "") && (ffastafile == "") && (ffastqfile == "")) { abort = true; m->mothurOut("[ERROR]: The file, ffastq and rfastq or ffasta and rfasta parameters are required.\n"); } + if ((file != "") && ((ffastafile != "") || (ffastqfile != ""))) { abort = true; m->mothurOut("[ERROR]: The file, ffastq and rfastq or ffasta and rfasta parameters are required.\n"); } + if ((ffastqfile != "") && (rfastqfile == "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the ffastq, you must provide a rfastq file.\n"); } + if ((ffastqfile == "") && (rfastqfile != "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the rfastq, you must provide a ffastq file.\n"); } + if ((ffastafile != "") && (rfastafile == "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the ffasta, you must provide a rfasta file.\n"); } + if ((ffastafile == "") && (rfastafile != "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the rfasta, you must provide a ffasta file.\n"); } + if ((fqualfile != "") && (rqualfile == "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the fqfile, you must provide a rqfile file.\n"); } + if ((fqualfile == "") && (rqualfile != "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the rqfile, you must provide a fqfile file.\n"); } + if (((fqualfile != "") || (rqualfile != "")) && ((ffastafile == "") || (rfastafile == ""))) { + abort = true; m->mothurOut("[ERROR]: If you provide use the rqfile or fqfile file, you must provide the ffasta and rfasta parameters.\n"); + } oligosfile = validParameter.validFile(parameters, "oligos", true); if (oligosfile == "not found") { oligosfile = ""; } @@ -253,7 +262,9 @@ MakeContigsCommand::MakeContigsCommand(string option) { else { m->setOligosFile(oligosfile); } //if the user changes the output directory command factory will send this info to us in the output parameter - outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(ffastqfile); } + outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ + outputDir = ""; + } //check for optional parameter and set defaults @@ -321,112 +332,167 @@ int MakeContigsCommand::execute(){ //read ffastq and rfastq files creating fasta and qual files. //this function will create a forward and reverse, fasta and qual files for each processor. - //files has an entry for each processor. files[i][0] = forwardFasta, files[i][1] = forwardQual, files[i][2] = reverseFasta, files[i][3] = reverseQual + //files has an entry for each processor. files[i][0] = forwardFasta, files[i][1] = forwardQual, files[i][2] = reverseFasta, files[i][3] = reverseQual. filesToProcess is for each filepair in the file parameter file. for ffastq and rfastq this will be size 1. unsigned long int numReads = 0; int start = time(NULL); longestBase = 1000; m->mothurOut("Reading fastq data...\n"); - vector< vector > files = readFastqFiles(numReads); + vector < vector< vector > > filesToProcess = preProcessData(numReads); m->mothurOut("Done.\n"); if (m->control_pressed) { return 0; } - vector > fastaFileNames; - vector > qualFileNames; - createGroup = false; - string outputGroupFileName; - map variables; - variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(ffastqfile)); - variables["[tag]"] = ""; - if(oligosfile != ""){ - createGroup = getOligos(fastaFileNames, qualFileNames); - if (createGroup) { - outputGroupFileName = getOutputFileName("group",variables); - outputNames.push_back(outputGroupFileName); outputTypes["group"].push_back(outputGroupFileName); - } - } - - variables["[tag]"] = "trim"; - string outFastaFile = getOutputFileName("fasta",variables); - string outQualFile = getOutputFileName("qfile",variables); - variables["[tag]"] = "scrap"; - string outScrapFastaFile = getOutputFileName("fasta",variables); - string outScrapQualFile = getOutputFileName("qfile",variables); - - variables["[tag]"] = ""; - string outMisMatchFile = getOutputFileName("mismatch",variables); - outputNames.push_back(outFastaFile); outputTypes["fasta"].push_back(outFastaFile); - outputNames.push_back(outQualFile); outputTypes["qfile"].push_back(outQualFile); - outputNames.push_back(outScrapFastaFile); outputTypes["fasta"].push_back(outScrapFastaFile); - outputNames.push_back(outScrapQualFile); outputTypes["qfile"].push_back(outScrapQualFile); - outputNames.push_back(outMisMatchFile); outputTypes["mismatch"].push_back(outMisMatchFile); - - m->mothurOut("Making contigs...\n"); - createProcesses(files, outFastaFile, outQualFile, outScrapFastaFile, outScrapQualFile, outMisMatchFile, fastaFileNames, qualFileNames); - m->mothurOut("Done.\n"); - - //remove temp fasta and qual files - for (int i = 0; i < processors; i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } } + map cvars; + string compOutputDir = outputDir; + if (outputDir == "") { compOutputDir = m->hasPath(file); } + cvars["[filename]"] = compOutputDir + m->getRootName(m->getSimpleName(file)); + cvars["[tag]"] = ""; + string compositeGroupFile = getOutputFileName("group",cvars); + cvars["[tag]"] = "trim"; + string compositeFastaFile = getOutputFileName("fasta",cvars); + string compositeQualFile = getOutputFileName("qfile",cvars); + cvars["[tag]"] = "scrap"; + string compositeScrapFastaFile = getOutputFileName("fasta",cvars); + string compositeScrapQualFile = getOutputFileName("qfile",cvars); + cvars["[tag]"] = ""; + string compositeMisMatchFile = getOutputFileName("mismatch",cvars); - if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; } + if (filesToProcess.size() > 1) { //clear files for append below + ofstream outCTFasta, outCTQual, outCSFasta, outCSQual, outCMisMatch; + m->openOutputFile(compositeFastaFile, outCTFasta); outCTFasta.close(); + m->openOutputFile(compositeScrapFastaFile, outCSFasta); outCSFasta.close(); + m->openOutputFile(compositeMisMatchFile, outCMisMatch); outCMisMatch.close(); + m->openOutputFile(compositeQualFile, outCTQual); outCTQual.close(); + m->openOutputFile(compositeScrapQualFile, outCSQual); outCSQual.close(); + outputNames.push_back(compositeFastaFile); outputTypes["fasta"].push_back(compositeFastaFile); + outputNames.push_back(compositeQualFile); outputTypes["qfile"].push_back(compositeQualFile); + outputNames.push_back(compositeMisMatchFile); outputTypes["mismatch"].push_back(compositeMisMatchFile); + outputNames.push_back(compositeScrapFastaFile); outputTypes["fasta"].push_back(compositeScrapFastaFile); + outputNames.push_back(compositeScrapQualFile); outputTypes["qfile"].push_back(compositeScrapQualFile); + } - if(allFiles){ - map uniqueFastaNames;// so we don't add the same groupfile multiple times - map::iterator it; - set namesToRemove; - for(int i=0;iisBlank(fastaFileNames[i][j])){ - m->mothurRemove(fastaFileNames[i][j]); - namesToRemove.insert(fastaFileNames[i][j]); - - m->mothurRemove(qualFileNames[i][j]); - namesToRemove.insert(qualFileNames[i][j]); - }else{ - it = uniqueFastaNames.find(fastaFileNames[i][j]); - if (it == uniqueFastaNames.end()) { - uniqueFastaNames[fastaFileNames[i][j]] = barcodeNameVector[i]; - } - } - } - } - } - } - - //remove names for outputFileNames, just cleans up the output - vector outputNames2; - for(int i = 0; i < outputNames.size(); i++) { if (namesToRemove.count(outputNames[i]) == 0) { outputNames2.push_back(outputNames[i]); } } - outputNames = outputNames2; - - for (it = uniqueFastaNames.begin(); it != uniqueFastaNames.end(); it++) { - ifstream in; - m->openInputFile(it->first, in); + for (int l = 0; l < filesToProcess.size(); l++) { + + m->mothurOut("\n>>>>>\tProcessing " + filesToProcess[l][0][0] + " (file " + toString(l+1) + " of " + toString(filesToProcess.size()) + ")\t<<<<<\n"); + + vector > fastaFileNames; + vector > qualFileNames; + createGroup = false; + string outputGroupFileName; + map variables; + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir = m->hasPath(filesToProcess[l][0][0]); } + variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(filesToProcess[l][0][0])); + variables["[tag]"] = ""; + if(oligosfile != ""){ + createGroup = getOligos(fastaFileNames, qualFileNames, variables["[filename]"]); + if (createGroup) { + outputGroupFileName = getOutputFileName("group",variables); + outputNames.push_back(outputGroupFileName); outputTypes["group"].push_back(outputGroupFileName); + } + } + + variables["[tag]"] = "trim"; + string outFastaFile = getOutputFileName("fasta",variables); + string outQualFile = getOutputFileName("qfile",variables); + variables["[tag]"] = "scrap"; + string outScrapFastaFile = getOutputFileName("fasta",variables); + string outScrapQualFile = getOutputFileName("qfile",variables); + variables["[tag]"] = ""; + string outMisMatchFile = getOutputFileName("mismatch",variables); + outputNames.push_back(outFastaFile); outputTypes["fasta"].push_back(outFastaFile); + outputNames.push_back(outScrapFastaFile); outputTypes["fasta"].push_back(outScrapFastaFile); + if (filesToProcess[l][0][1] != "") { + outputNames.push_back(outQualFile); outputTypes["qfile"].push_back(outQualFile); + outputNames.push_back(outScrapQualFile); outputTypes["qfile"].push_back(outScrapQualFile); + } + outputNames.push_back(outMisMatchFile); outputTypes["mismatch"].push_back(outMisMatchFile); + + m->mothurOut("Making contigs...\n"); + createProcesses(filesToProcess[l], outFastaFile, outQualFile, outScrapFastaFile, outScrapQualFile, outMisMatchFile, fastaFileNames, qualFileNames); + m->mothurOut("Done.\n"); + + //remove temp fasta and qual files + for (int i = 0; i < processors; i++) { for(int j = 0; j < filesToProcess[l][i].size(); j++) { m->mothurRemove(filesToProcess[l][i][j]); } } + + if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; } + + if(allFiles){ + map uniqueFastaNames;// so we don't add the same groupfile multiple times + map::iterator it; + set namesToRemove; + for(int i=0;iisBlank(fastaFileNames[i][j])){ + m->mothurRemove(fastaFileNames[i][j]); + namesToRemove.insert(fastaFileNames[i][j]); + + if (filesToProcess[l][0][1] != "") { + m->mothurRemove(qualFileNames[i][j]); + namesToRemove.insert(qualFileNames[i][j]); + } + }else{ + it = uniqueFastaNames.find(fastaFileNames[i][j]); + if (it == uniqueFastaNames.end()) { + uniqueFastaNames[fastaFileNames[i][j]] = barcodeNameVector[i]; + } + } + } + } + } + } - ofstream out; - string thisGroupName = outputDir + m->getRootName(m->getSimpleName(it->first)); - thisGroupName += getOutputFileName("group",variables); outputNames.push_back(thisGroupName); outputTypes["group"].push_back(thisGroupName); - m->openOutputFile(thisGroupName, out); - - while (!in.eof()){ - if (m->control_pressed) { break; } + //remove names for outputFileNames, just cleans up the output + vector outputNames2; + for(int i = 0; i < outputNames.size(); i++) { if (namesToRemove.count(outputNames[i]) == 0) { outputNames2.push_back(outputNames[i]); } } + outputNames = outputNames2; + + for (it = uniqueFastaNames.begin(); it != uniqueFastaNames.end(); it++) { + ifstream in; + m->openInputFile(it->first, in); + + ofstream out; + string thisGroupName = thisOutputDir + m->getRootName(m->getSimpleName(it->first)); + thisGroupName += getOutputFileName("group",variables); outputNames.push_back(thisGroupName); outputTypes["group"].push_back(thisGroupName); + m->openOutputFile(thisGroupName, out); - Sequence currSeq(in); m->gobble(in); - out << currSeq.getName() << '\t' << it->second << endl; + while (!in.eof()){ + if (m->control_pressed) { break; } + + Sequence currSeq(in); m->gobble(in); + out << currSeq.getName() << '\t' << it->second << endl; + } + in.close(); + out.close(); } - in.close(); - out.close(); } - } - - if (createGroup) { - ofstream outGroup; - m->openOutputFile(outputGroupFileName, outGroup); - for (map::iterator itGroup = groupMap.begin(); itGroup != groupMap.end(); itGroup++) { - outGroup << itGroup->first << '\t' << itGroup->second << endl; + + if (createGroup) { + ofstream outGroup; + m->openOutputFile(outputGroupFileName, outGroup); + for (map::iterator itGroup = groupMap.begin(); itGroup != groupMap.end(); itGroup++) { + outGroup << itGroup->first << '\t' << itGroup->second << endl; + } + outGroup.close(); + } + + if (filesToProcess.size() > 1) { //merge into large combo files + if (createGroup) { + if (l == 0) { + ofstream outCGroup; + m->openOutputFile(compositeGroupFile, outCGroup); outCGroup.close(); + outputNames.push_back(compositeGroupFile); outputTypes["group"].push_back(compositeGroupFile); + } + m->appendFiles(outputGroupFileName, compositeGroupFile); + } + m->appendFiles(outMisMatchFile, compositeMisMatchFile); + m->appendFiles(outFastaFile, compositeFastaFile); + m->appendFiles(outQualFile, compositeQualFile); + m->appendFiles(outScrapFastaFile, compositeScrapFastaFile); + m->appendFiles(outScrapQualFile, compositeScrapQualFile); } - outGroup.close(); } m->mothurOut("It took " + toString(time(NULL) - start) + " secs to process " + toString(numReads) + " sequences.\n"); @@ -475,6 +541,66 @@ int MakeContigsCommand::execute(){ } } //********************************************************************************************************************** +vector< vector< vector > > MakeContigsCommand::preProcessData(unsigned long int& numReads) { + try { + vector< vector< vector > > filesToProcess; + + if (ffastqfile != "") { //reading one file + vector< vector > files = readFastqFiles(numReads, ffastqfile, rfastqfile); + //adjust for really large processors or really small files + if (numReads == 0) { m->mothurOut("[ERROR]: no good reads.\n"); m->control_pressed = true; } + if (numReads < processors) { + for (int i = numReads; i < processors; i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } files[i].clear(); } + files.resize(numReads); + processors = numReads; + } + filesToProcess.push_back(files); + }else if (file != "") { //reading multiple files + //return only valid pairs + vector< vector > filePairsToProcess = readFileNames(file); + + if (m->control_pressed) { return filesToProcess; } + + if (filePairsToProcess.size() != 0) { + for (int i = 0; i < filePairsToProcess.size(); i++) { + + if (m->control_pressed) { for (int l = 0; l < filesToProcess.size(); l++) { for (int k = 0; k < filesToProcess[l].size(); k++) { for(int j = 0; j < filesToProcess[l][k].size(); j++) { m->mothurRemove(filesToProcess[l][k][j]); } filesToProcess[l][k].clear(); } return filesToProcess; } } + + unsigned long int thisFilesReads; + vector< vector > files = readFastqFiles(thisFilesReads, filePairsToProcess[i][0], filePairsToProcess[i][1]); + + //adjust for really large processors or really small files + if (thisFilesReads < processors) { + m->mothurOut("[ERROR]: " + filePairsToProcess[i][0] + " has less than " + toString(processors) + " good reads, skipping\n"); + for (int k = 0; k < files.size(); k++) { for(int j = 0; j < files[k].size(); j++) { m->mothurRemove(files[k][j]); } files[k].clear(); } + }else { + filesToProcess.push_back(files); + numReads += thisFilesReads; + } + } + //all files are bad + if (numReads == 0) { m->control_pressed = true; } + } + }else if (ffastafile != "") { + vector< vector > files = readFastaFiles(numReads, ffastafile, rfastafile); + //adjust for really large processors or really small files + if (numReads == 0) { m->mothurOut("[ERROR]: no good reads.\n"); m->control_pressed = true; } + if (numReads < processors) { + for (int i = numReads; i < processors; i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } files[i].clear(); } + files.resize(numReads); + processors = numReads; + } + filesToProcess.push_back(files); + }else { m->control_pressed = true; } //should not get here + + return filesToProcess; + } + catch(exception& e) { + m->errorOut(e, "MakeContigsCommand", "preProcessData"); + exit(1); + } +} +//********************************************************************************************************************** int MakeContigsCommand::createProcesses(vector< vector > files, string outputFasta, string outputQual, string outputScrapFasta, string outputScrapQual, string outputMisMatches, vector > fastaFileNames, vector > qualFileNames) { try { int num = 0; @@ -547,9 +673,11 @@ int MakeContigsCommand::createProcesses(vector< vector > files, string o ofstream temp; m->openOutputFile(outputFasta, temp); temp.close(); - m->openOutputFile(outputQual, temp); temp.close(); m->openOutputFile(outputScrapFasta, temp); temp.close(); - m->openOutputFile(outputScrapQual, temp); temp.close(); + if (files[processors-1][1] != "") { + m->openOutputFile(outputScrapQual, temp); temp.close(); + m->openOutputFile(outputQual, temp); temp.close(); + } //do my part num = driver(files[processors-1], outputFasta, outputQual, outputScrapFasta, outputScrapQual, outputMisMatches, fastaFileNames, qualFileNames); @@ -661,10 +789,11 @@ int MakeContigsCommand::createProcesses(vector< vector > files, string o //parent do my part ofstream temp; m->openOutputFile(outputFasta, temp); temp.close(); - m->openOutputFile(outputQual, temp); temp.close(); m->openOutputFile(outputScrapFasta, temp); temp.close(); - m->openOutputFile(outputScrapQual, temp); temp.close(); - + if (files[processors-1][1] != "") { + m->openOutputFile(outputScrapQual, temp); temp.close(); + m->openOutputFile(outputQual, temp); temp.close(); + } //do my part num = driver(files[processors-1], (outputFasta+ toString(processors-1) + ".temp"), (outputQual+ toString(processors-1) + ".temp"), (outputScrapFasta+ toString(processors-1) + ".temp"), (outputScrapQual+ toString(processors-1) + ".temp"), (outputMisMatches+ toString(processors-1) + ".temp"), tempFASTAFileNames, tempPrimerQualFileNames); @@ -695,14 +824,16 @@ int MakeContigsCommand::createProcesses(vector< vector > files, string o m->appendFiles((outputFasta + toString(processIDS[i]) + ".temp"), outputFasta); m->mothurRemove((outputFasta + toString(processIDS[i]) + ".temp")); - m->appendFiles((outputQual + toString(processIDS[i]) + ".temp"), outputQual); - m->mothurRemove((outputQual + toString(processIDS[i]) + ".temp")); - - m->appendFiles((outputScrapFasta + toString(processIDS[i]) + ".temp"), outputScrapFasta); + m->appendFiles((outputScrapFasta + toString(processIDS[i]) + ".temp"), outputScrapFasta); m->mothurRemove((outputScrapFasta + toString(processIDS[i]) + ".temp")); - m->appendFiles((outputScrapQual + toString(processIDS[i]) + ".temp"), outputScrapQual); - m->mothurRemove((outputScrapQual + toString(processIDS[i]) + ".temp")); + if (files[processors-1][1] != "") { + m->appendFiles((outputScrapQual + toString(processIDS[i]) + ".temp"), outputScrapQual); + m->mothurRemove((outputScrapQual + toString(processIDS[i]) + ".temp")); + + m->appendFiles((outputQual + toString(processIDS[i]) + ".temp"), outputQual); + m->mothurRemove((outputQual + toString(processIDS[i]) + ".temp")); + } m->appendFiles((outputMisMatches + toString(processIDS[i]) + ".temp"), outputMisMatches); m->mothurRemove((outputMisMatches + toString(processIDS[i]) + ".temp")); @@ -714,8 +845,10 @@ int MakeContigsCommand::createProcesses(vector< vector > files, string o m->appendFiles((fastaFileNames[j][k] + toString(processIDS[i]) + ".temp"), fastaFileNames[j][k]); m->mothurRemove((fastaFileNames[j][k] + toString(processIDS[i]) + ".temp")); - m->appendFiles((qualFileNames[j][k] + toString(processIDS[i]) + ".temp"), qualFileNames[j][k]); - m->mothurRemove((qualFileNames[j][k] + toString(processIDS[i]) + ".temp")); + if (files[processors-1][1] != "") { + m->appendFiles((qualFileNames[j][k] + toString(processIDS[i]) + ".temp"), qualFileNames[j][k]); + m->mothurRemove((qualFileNames[j][k] + toString(processIDS[i]) + ".temp")); + } } } } @@ -746,22 +879,23 @@ int MakeContigsCommand::driver(vector files, string outputFasta, string if (m->debug) { m->mothurOut("[DEBUG]: ffasta = " + thisffastafile + ".\n[DEBUG]: fqual = " + thisfqualfile + ".\n[DEBUG]: rfasta = " + thisrfastafile + ".\n[DEBUG]: rqual = " + thisrqualfile + ".\n"); } ifstream inFFasta, inRFasta, inFQual, inRQual; + ofstream outFasta, outQual, outMisMatch, outScrapFasta, outScrapQual; m->openInputFile(thisffastafile, inFFasta); - m->openInputFile(thisfqualfile, inFQual); m->openInputFile(thisrfastafile, inRFasta); - m->openInputFile(thisrqualfile, inRQual); - - ofstream outFasta, outQual, outMisMatch, outScrapFasta, outScrapQual; + if (thisfqualfile != "") { + m->openInputFile(thisfqualfile, inFQual); + m->openInputFile(thisrqualfile, inRQual); + m->openOutputFile(outputScrapQual, outScrapQual); + m->openOutputFile(outputQual, outQual); + } m->openOutputFile(outputFasta, outFasta); - m->openOutputFile(outputQual, outQual); m->openOutputFile(outputScrapFasta, outScrapFasta); - m->openOutputFile(outputScrapQual, outScrapQual); m->openOutputFile(outputMisMatches, outMisMatch); outMisMatch << "Name\tLength\tMisMatches\n"; TrimOligos trimOligos(pdiffs, bdiffs, 0, 0, primers, barcodes); - while ((!inFQual.eof()) && (!inFFasta.eof()) && (!inRFasta.eof()) && (!inRQual.eof())) { + while ((!inFFasta.eof()) && (!inRFasta.eof())) { if (m->control_pressed) { break; } @@ -772,20 +906,31 @@ int MakeContigsCommand::driver(vector files, string outputFasta, string //read seqs and quality info Sequence fSeq(inFFasta); m->gobble(inFFasta); Sequence rSeq(inRFasta); m->gobble(inRFasta); - QualityScores fQual(inFQual); m->gobble(inFQual); - QualityScores rQual(inRQual); m->gobble(inRQual); + QualityScores* fQual = NULL; QualityScores* rQual = NULL; + if (thisfqualfile != "") { + fQual = new QualityScores(inFQual); m->gobble(inFQual); + rQual = new QualityScores(inRQual); m->gobble(inRQual); + } int barcodeIndex = 0; int primerIndex = 0; if(barcodes.size() != 0){ - success = trimOligos.stripBarcode(fSeq, rSeq, fQual, rQual, barcodeIndex); + if (thisfqualfile != "") { + success = trimOligos.stripBarcode(fSeq, rSeq, *fQual, *rQual, barcodeIndex); + }else { + success = trimOligos.stripBarcode(fSeq, rSeq, barcodeIndex); + } if(success > bdiffs) { trashCode += 'b'; } else{ currentSeqsDiffs += success; } } if(primers.size() != 0){ - success = trimOligos.stripForward(fSeq, rSeq, fQual, rQual, primerIndex); + if (thisfqualfile != "") { + success = trimOligos.stripForward(fSeq, rSeq, *fQual, *rQual, primerIndex); + }else { + success = trimOligos.stripForward(fSeq, rSeq, primerIndex); + } if(success > pdiffs) { trashCode += 'f'; } else{ currentSeqsDiffs += success; } } @@ -794,7 +939,7 @@ int MakeContigsCommand::driver(vector files, string outputFasta, string //flip the reverse reads rSeq.reverseComplement(); - rQual.flipQScores(); + if (thisfqualfile != "") { rQual->flipQScores(); } //pairwise align alignment->align(fSeq.getUnaligned(), rSeq.getUnaligned()); @@ -810,8 +955,12 @@ int MakeContigsCommand::driver(vector files, string outputFasta, string int numMismatches = 0; string seq1 = fSeq.getAligned(); string seq2 = rSeq.getAligned(); - vector scores1 = fQual.getQualityScores(); - vector scores2 = rQual.getQualityScores(); + vector scores1, scores2; + if (thisfqualfile != "") { + scores1 = fQual->getQualityScores(); + scores2 = rQual->getQualityScores(); + delete fQual; delete rQual; + } // if (num < 5) { cout << fSeq.getStartPos() << '\t' << fSeq.getEndPos() << '\t' << rSeq.getStartPos() << '\t' << rSeq.getEndPos() << endl; } int overlapStart = fSeq.getStartPos(); @@ -821,12 +970,12 @@ int MakeContigsCommand::driver(vector files, string outputFasta, string overlapStart = seq2Start; for (int i = 0; i < overlapStart; i++) { contig += seq1[i]; - contigScores.push_back(scores1[ABaseMap[i]]); + if (thisfqualfile != "") { contigScores.push_back(scores1[ABaseMap[i]]); } } }else { //seq1 starts later so take from 0 to overlapStart from seq2 for (int i = 0; i < overlapStart; i++) { contig += seq2[i]; - contigScores.push_back(scores2[BBaseMap[i]]); + if (thisfqualfile != "") { contigScores.push_back(scores2[BBaseMap[i]]); } } } @@ -838,26 +987,34 @@ int MakeContigsCommand::driver(vector files, string outputFasta, string for (int i = overlapStart; i < overlapEnd; i++) { if (seq1[i] == seq2[i]) { //match, add base and choose highest score contig += seq1[i]; - contigScores.push_back(scores1[ABaseMap[i]]); - if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { contigScores[contigScores.size()-1] = scores2[BBaseMap[i]]; } - }else if (((seq1[i] == '.') || (seq1[i] == '-')) && ((seq2[i] != '-') && (seq2[i] != '.'))) { //seq1 is a gap and seq2 is a base, choose seq2, unless quality score for base is below threshold. In that case eliminate base - if (scores2[BBaseMap[i]] < threshold) { } // - else { - contig += seq2[i]; - contigScores.push_back(scores2[BBaseMap[i]]); + if (thisfqualfile != "") { + contigScores.push_back(scores1[ABaseMap[i]]); + if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { contigScores[contigScores.size()-1] = scores2[BBaseMap[i]]; } } + }else if (((seq1[i] == '.') || (seq1[i] == '-')) && ((seq2[i] != '-') && (seq2[i] != '.'))) { //seq1 is a gap and seq2 is a base, choose seq2, unless quality score for base is below threshold. In that case eliminate base + if (thisfqualfile != "") { + if (scores2[BBaseMap[i]] < threshold) { } // + else { + contig += seq2[i]; + contigScores.push_back(scores2[BBaseMap[i]]); + } + }else { contig += seq2[i]; } //with no quality info, then we keep it? }else if (((seq2[i] == '.') || (seq2[i] == '-')) && ((seq1[i] != '-') && (seq1[i] != '.'))) { //seq2 is a gap and seq1 is a base, choose seq1, unless quality score for base is below threshold. In that case eliminate base - if (scores1[ABaseMap[i]] < threshold) { } // - else { - contig += seq1[i]; - contigScores.push_back(scores1[ABaseMap[i]]); - } + if (thisfqualfile != "") { + if (scores1[ABaseMap[i]] < threshold) { } // + else { + contig += seq1[i]; + contigScores.push_back(scores1[ABaseMap[i]]); + } + }else { contig += seq1[i]; } //with no quality info, then we keep it? }else if (((seq1[i] != '-') && (seq1[i] != '.')) && ((seq2[i] != '-') && (seq2[i] != '.'))) { //both bases choose one with better quality - char c = seq1[i]; - contigScores.push_back(scores1[ABaseMap[i]]); - if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { contigScores[contigScores.size()-1] = scores2[BBaseMap[i]]; c = seq2[i]; } - contig += c; - numMismatches++; + if (thisfqualfile != "") { + char c = seq1[i]; + contigScores.push_back(scores1[ABaseMap[i]]); + if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { contigScores[contigScores.size()-1] = scores2[BBaseMap[i]]; c = seq2[i]; } + contig += c; + numMismatches++; + }else { numMismatches++; } //cant decide, so eliminate and mark as mismatch }else { //should never get here m->mothurOut("[ERROR]: case I didn't think of seq1 = " + toString(seq1[i]) + " and seq2 = " + toString(seq2[i]) + "\n"); } @@ -866,12 +1023,12 @@ int MakeContigsCommand::driver(vector files, string outputFasta, string if (seq1End < seq2End) { //seq1 ends before seq2 so take from overlap to length from seq2 for (int i = overlapEnd; i < length; i++) { contig += seq2[i]; - contigScores.push_back(scores2[BBaseMap[i]]); + if (thisfqualfile != "") { contigScores.push_back(scores2[BBaseMap[i]]); } } }else { //seq2 ends before seq1 so take from overlap to length from seq1 for (int i = overlapEnd; i < length; i++) { contig += seq1[i]; - contigScores.push_back(scores1[ABaseMap[i]]); + if (thisfqualfile != "") { contigScores.push_back(scores1[ABaseMap[i]]); } } } @@ -907,25 +1064,31 @@ int MakeContigsCommand::driver(vector files, string outputFasta, string output << ">" << fSeq.getName() << endl << contig << endl; output.close(); - m->openOutputFileAppend(qualFileNames[barcodeIndex][primerIndex], output); - output << ">" << fSeq.getName() << endl; - for (int i = 0; i < contigScores.size(); i++) { output << contigScores[i] << ' '; } - output << endl; - output.close(); + if (thisfqualfile != "") { + m->openOutputFileAppend(qualFileNames[barcodeIndex][primerIndex], output); + output << ">" << fSeq.getName() << endl; + for (int i = 0; i < contigScores.size(); i++) { output << contigScores[i] << ' '; } + output << endl; + output.close(); + } } //output outFasta << ">" << fSeq.getName() << endl << contig << endl; - outQual << ">" << fSeq.getName() << endl; - for (int i = 0; i < contigScores.size(); i++) { outQual << contigScores[i] << ' '; } - outQual << endl; + if (thisfqualfile != "") { + outQual << ">" << fSeq.getName() << endl; + for (int i = 0; i < contigScores.size(); i++) { outQual << contigScores[i] << ' '; } + outQual << endl; + } outMisMatch << fSeq.getName() << '\t' << contig.length() << '\t' << numMismatches << endl; }else { //output outScrapFasta << ">" << fSeq.getName() << " | " << trashCode << endl << contig << endl; - outScrapQual << ">" << fSeq.getName() << " | " << trashCode << endl; - for (int i = 0; i < contigScores.size(); i++) { outScrapQual << contigScores[i] << ' '; } - outScrapQual << endl; + if (thisfqualfile != "") { + outScrapQual << ">" << fSeq.getName() << " | " << trashCode << endl; + for (int i = 0; i < contigScores.size(); i++) { outScrapQual << contigScores[i] << ' '; } + outScrapQual << endl; + } } num++; @@ -937,18 +1100,20 @@ int MakeContigsCommand::driver(vector files, string outputFasta, string if((num) % 1000 != 0){ m->mothurOut(toString(num)); m->mothurOutEndLine(); } inFFasta.close(); - inFQual.close(); inRFasta.close(); - inRQual.close(); outFasta.close(); - outQual.close(); outScrapFasta.close(); - outScrapQual.close(); outMisMatch.close(); + if (thisfqualfile != "") { + inFQual.close(); + inRQual.close(); + outQual.close(); + outScrapQual.close(); + } delete alignment; - if (m->control_pressed) { m->mothurRemove(outputQual); m->mothurRemove(outputFasta); m->mothurRemove(outputScrapQual); m->mothurRemove(outputScrapFasta);m->mothurRemove(outputMisMatches);} - + if (m->control_pressed) { m->mothurRemove(outputFasta); m->mothurRemove(outputScrapFasta);m->mothurRemove(outputMisMatches); if (thisfqualfile != "") { m->mothurRemove(outputQual); m->mothurRemove(outputScrapQual); } } + return num; } catch(exception& e) { @@ -957,10 +1122,9 @@ int MakeContigsCommand::driver(vector files, string outputFasta, string } } //********************************************************************************************************************** -vector< vector > MakeContigsCommand::readFastqFiles(unsigned long int& count){ +vector< vector > MakeContigsCommand::readFastqFiles(unsigned long int& count, string ffastq, string rfastq){ try { vector< vector > files; - //maps processors number to file pointer map > tempfiles; //tempfiles[0] = forwardFasta, [1] = forwardQual, [2] = reverseFasta, [3] = reverseQual map >::iterator it; @@ -975,10 +1139,12 @@ vector< vector > MakeContigsCommand::readFastqFiles(unsigned long int& c tempfiles[i] = temp; vector names; - string ffastafilename = outputDir + m->getRootName(m->getSimpleName(ffastqfile)) + toString(i) + "ffasta.temp"; - string rfastafilename = outputDir + m->getRootName(m->getSimpleName(rfastqfile)) + toString(i) + "rfasta.temp"; - string fqualfilename = outputDir + m->getRootName(m->getSimpleName(ffastqfile)) + toString(i) + "fqual.temp"; - string rqualfilename = outputDir + m->getRootName(m->getSimpleName(rfastqfile)) + toString(i) + "rqual.temp"; + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir = m->hasPath(ffastq); } + string ffastafilename = thisOutputDir + m->getRootName(m->getSimpleName(ffastq)) + toString(i) + "ffastatemp"; + string rfastafilename = thisOutputDir + m->getRootName(m->getSimpleName(rfastq)) + toString(i) + "rfastatemp"; + string fqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(ffastq)) + toString(i) + "fqualtemp"; + string rqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(rfastq)) + toString(i) + "rqualtemp"; names.push_back(ffastafilename); names.push_back(fqualfilename); names.push_back(rfastafilename); names.push_back(rqualfilename); files.push_back(names); @@ -999,10 +1165,10 @@ vector< vector > MakeContigsCommand::readFastqFiles(unsigned long int& c } ifstream inForward; - m->openInputFile(ffastqfile, inForward); + m->openInputFile(ffastq, inForward); ifstream inReverse; - m->openInputFile(rfastqfile, inReverse); + m->openInputFile(rfastq, inReverse); count = 0; map uniques; @@ -1027,7 +1193,7 @@ vector< vector > MakeContigsCommand::readFastqFiles(unsigned long int& c if (m->debug) { m->mothurOut(toString(count) + '\t' + fread.name + '\t' + rread.name + '\n'); } - if (checkReads(fread, rread)) { + if (checkReads(fread, rread, ffastq, rfastq)) { if (m->control_pressed) { for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } } for (int i = 0; i < files.size(); i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } } inForward.close(); inReverse.close(); return files; } //if the reads are okay write to output files @@ -1064,18 +1230,155 @@ vector< vector > MakeContigsCommand::readFastqFiles(unsigned long int& c inForward.close(); inReverse.close(); - //adjust for really large processors or really small files - if (count == 0) { m->mothurOut("[ERROR]: no good reads.\n"); m->control_pressed = true; } - if (count < processors) { - for (int i = count; i < processors; i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } files[i].clear(); } - files.resize(count); - processors = count; + return files; + } + catch(exception& e) { + m->errorOut(e, "MakeContigsCommand", "readFastqFiles"); + exit(1); + } +} +//********************************************************************************************************************** +vector< vector > MakeContigsCommand::readFastaFiles(unsigned long int& count, string ffasta, string rfasta){ + try { + vector< vector > files; + //maps processors number to file pointer + map > tempfiles; //tempfiles[0] = forwardFasta, [1] = forwardQual, [2] = reverseFasta, [3] = reverseQual + map >::iterator it; + + //create files to write to + for (int i = 0; i < processors; i++) { + vector temp; + ofstream* outFF = new ofstream; temp.push_back(outFF); + ofstream* outFQ = new ofstream; temp.push_back(outFQ); + ofstream* outRF = new ofstream; temp.push_back(outRF); + ofstream* outRQ = new ofstream; temp.push_back(outRQ); + tempfiles[i] = temp; + + vector names; + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir = m->hasPath(ffasta); } + string ffastafilename = thisOutputDir + m->getRootName(m->getSimpleName(ffasta)) + toString(i) + "ffastatemp"; + string rfastafilename = thisOutputDir + m->getRootName(m->getSimpleName(rfasta)) + toString(i) + "rfastatemp"; + string fqualfilename = ""; + if (fqualfile != "") { fqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(fqualfile)) + toString(i) + "fqual.temp"; m->openOutputFile(fqualfilename, *outFQ); } + string rqualfilename = ""; + if (rqualfile != "") { rqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(rqualfile)) + toString(i) + "rqual.temp"; m->openOutputFile(rqualfilename, *outRQ); } + names.push_back(ffastafilename); names.push_back(fqualfilename); + names.push_back(rfastafilename); names.push_back(rqualfilename); + files.push_back(names); + + m->openOutputFile(ffastafilename, *outFF); + m->openOutputFile(rfastafilename, *outRF); + } + + if (m->control_pressed) { + //close files, delete ofstreams + for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } } + //remove files + for (int i = 0; i < files.size(); i++) { + for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } + } } + ifstream inForwardFasta; + m->openInputFile(ffasta, inForwardFasta); + + ifstream inReverseFasta; + m->openInputFile(rfasta, inReverseFasta); + + ifstream inForwardQual, inReverseQual; + if (fqualfile != "") { m->openInputFile(fqualfile, inForwardQual); m->openInputFile(rqualfile, inReverseQual); } + + count = 0; + map uniques; + map::iterator itUniques; + while ((!inForwardFasta.eof()) || (!inReverseFasta.eof())) { + + if (m->control_pressed) { for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } } for (int i = 0; i < files.size(); i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } } inReverseFasta.close(); inForwardFasta.close(); if (fqualfile != "") { inReverseQual.close(); inReverseQual.close(); } return files; } + + //get a reads from forward and reverse fasta files + bool ignoref, ignorer; + fastqRead thisFread, thisRread; + if (!inForwardFasta.eof()) { + ignoref = false; + Sequence temp(inForwardFasta); m->gobble(inForwardFasta); + thisFread.name = temp.getName(); + thisFread.sequence = temp.getUnaligned(); + }else { ignoref = true; } + if (!inReverseFasta.eof()) { + ignorer = false; + Sequence temp(inReverseFasta); m->gobble(inReverseFasta); + thisRread.name = temp.getName(); + thisRread.sequence = temp.getUnaligned(); + }else { ignorer = true; } + + //get qual reads if given + if (fqualfile != "") { + if (!inForwardQual.eof() && !ignoref) { + QualityScores temp(inForwardQual); m->gobble(inForwardQual); + //if forward files dont match ignore read + if (thisFread.name != temp.getName()) { ignoref = true; } + else { thisFread.scores = temp.getQualityScores(); } + }else { ignoref = true; } + if (!inReverseQual.eof() && !ignorer) { + QualityScores temp(inReverseQual); m->gobble(inReverseQual); + //if reverse files dont match ignore read + if (thisRread.name != temp.getName()) { ignorer = true; } + else { thisRread.scores = temp.getQualityScores(); } + }else { ignorer = true; } + } + + vector reads = getReads(ignoref, ignorer, thisFread, thisRread, uniques); + + for (int i = 0; i < reads.size(); i++) { + fastqRead fread = reads[i].forward; + fastqRead rread = reads[i].reverse; + + if (m->debug) { m->mothurOut(toString(count) + '\t' + fread.name + '\t' + rread.name + '\n'); } + + if (checkReads(fread, rread, ffasta, rfasta)) { + if (m->control_pressed) { for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } } for (int i = 0; i < files.size(); i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } } inReverseFasta.close(); inForwardFasta.close(); if (fqualfile != "") { inReverseQual.close(); inReverseQual.close(); } return files; } + + //if the reads are okay write to output files + int process = count % processors; + + *(tempfiles[process][0]) << ">" << fread.name << endl << fread.sequence << endl; + *(tempfiles[process][2]) << ">" << rread.name << endl << rread.sequence << endl; + if (fqualfile != "") { //if you have quality info, print it + *(tempfiles[process][1]) << ">" << fread.name << endl; + for (int i = 0; i < fread.scores.size(); i++) { *(tempfiles[process][1]) << fread.scores[i] << " "; } + *(tempfiles[process][1]) << endl; + *(tempfiles[process][3]) << ">" << rread.name << endl; + for (int i = 0; i < rread.scores.size(); i++) { *(tempfiles[process][3]) << rread.scores[i] << " "; } + *(tempfiles[process][3]) << endl; + } + count++; + + //report progress + if((count) % 10000 == 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); } + } + } + } + //report progress + if((count) % 10000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); } + + if (uniques.size() != 0) { + for (itUniques = uniques.begin(); itUniques != uniques.end(); itUniques++) { + m->mothurOut("[WARNING]: did not find paired read for " + itUniques->first + ", ignoring.\n"); + } + m->mothurOutEndLine(); + } + + //close files, delete ofstreams + for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } } + inReverseFasta.close(); + inForwardFasta.close(); + if (fqualfile != "") { inReverseQual.close(); inReverseQual.close(); } + return files; } catch(exception& e) { - m->errorOut(e, "MakeContigsCommand", "readFastqFiles"); + m->errorOut(e, "MakeContigsCommand", "readFastaFiles"); exit(1); } } @@ -1213,19 +1516,19 @@ fastqRead MakeContigsCommand::readFastq(ifstream& in, bool& ignore){ } } //********************************************************************************************************************** -bool MakeContigsCommand::checkReads(fastqRead& forward, fastqRead& reverse){ +bool MakeContigsCommand::checkReads(fastqRead& forward, fastqRead& reverse, string ffile, string rfile){ try { bool good = true; //do sequence lengths match if (forward.sequence.length() != reverse.sequence.length()) { - m->mothurOut("[WARNING]: For sequence " + forward.name + " I read a sequence of length " + toString(forward.sequence.length()) + " from " + ffastqfile + ", but read a sequence of length " + toString(reverse.sequence.length()) + " from " + rfastqfile + ", ignoring.\n"); + m->mothurOut("[WARNING]: For sequence " + forward.name + " I read a sequence of length " + toString(forward.sequence.length()) + " from " + ffile + ", but read a sequence of length " + toString(reverse.sequence.length()) + " from " + rfile + ", ignoring.\n"); good = false; } //do number of qual scores match if (forward.scores.size() != reverse.scores.size()) { - m->mothurOut("[WARNING]: For sequence " + forward.name + " I read " + toString(forward.scores.size()) + " quality scores from " + ffastqfile + ", but read " + toString(reverse.scores.size()) + " quality scores from " + rfastqfile + ", ignoring.\n"); + m->mothurOut("[WARNING]: For sequence " + forward.name + " I read " + toString(forward.scores.size()) + " quality scores from " + ffile + ", but read " + toString(reverse.scores.size()) + " quality scores from " + rfile + ", ignoring.\n"); good = false; } @@ -1237,11 +1540,107 @@ bool MakeContigsCommand::checkReads(fastqRead& forward, fastqRead& reverse){ } } //*************************************************************************************************************** +vector< vector > MakeContigsCommand::readFileNames(string filename){ + try { + vector< vector > files; + string forward, reverse; + + ifstream in; + m->openInputFile(filename, in); + + while(!in.eof()) { + + if (m->control_pressed) { return files; } + + in >> forward; m->gobble(in); + in >> reverse; m->gobble(in); + + //check to make sure both are able to be opened + ifstream in2; + int openForward = m->openInputFile(forward, in2, "noerror"); + + //if you can't open it, try default location + if (openForward == 1) { + if (m->getDefaultPath() != "") { //default path is set + string tryPath = m->getDefaultPath() + m->getSimpleName(forward); + m->mothurOut("Unable to open " + forward + ". Trying default " + tryPath); m->mothurOutEndLine(); + ifstream in3; + openForward = m->openInputFile(tryPath, in3, "noerror"); + in3.close(); + forward = tryPath; + } + } + + //if you can't open it, try output location + if (openForward == 1) { + if (m->getOutputDir() != "") { //default path is set + string tryPath = m->getOutputDir() + m->getSimpleName(forward); + m->mothurOut("Unable to open " + forward + ". Trying output directory " + tryPath); m->mothurOutEndLine(); + ifstream in4; + openForward = m->openInputFile(tryPath, in4, "noerror"); + forward = tryPath; + in4.close(); + } + } + + if (openForward == 1) { //can't find it + m->mothurOut("[WARNING]: can't find " + forward + ", ignoring pair.\n"); + }else{ in2.close(); } + + ifstream in3; + int openReverse = m->openInputFile(reverse, in3, "noerror"); + + //if you can't open it, try default location + if (openReverse == 1) { + if (m->getDefaultPath() != "") { //default path is set + string tryPath = m->getDefaultPath() + m->getSimpleName(reverse); + m->mothurOut("Unable to open " + reverse + ". Trying default " + tryPath); m->mothurOutEndLine(); + ifstream in3; + openReverse = m->openInputFile(tryPath, in3, "noerror"); + in3.close(); + reverse = tryPath; + } + } + + //if you can't open it, try output location + if (openReverse == 1) { + if (m->getOutputDir() != "") { //default path is set + string tryPath = m->getOutputDir() + m->getSimpleName(reverse); + m->mothurOut("Unable to open " + reverse + ". Trying output directory " + tryPath); m->mothurOutEndLine(); + ifstream in4; + openReverse = m->openInputFile(tryPath, in4, "noerror"); + reverse = tryPath; + in4.close(); + } + } + + if (openReverse == 1) { //can't find it + m->mothurOut("[WARNING]: can't find " + reverse + ", ignoring pair.\n"); + }else{ in3.close(); } + + if ((openForward != 1) && (openReverse != 1)) { //good pair + vector pair; + pair.push_back(forward); + pair.push_back(reverse); + files.push_back(pair); + } + + } + in.close(); + + return files; + } + catch(exception& e) { + m->errorOut(e, "MakeContigsCommand", "checkReads"); + exit(1); + } +} +//*************************************************************************************************************** //illumina data requires paired forward and reverse data //BARCODE atgcatgc atgcatgc groupName //PRIMER atgcatgc atgcatgc groupName //PRIMER atgcatgc atgcatgc -bool MakeContigsCommand::getOligos(vector >& fastaFileNames, vector >& qualFileNames){ +bool MakeContigsCommand::getOligos(vector >& fastaFileNames, vector >& qualFileNames, string rootname){ try { ifstream in; m->openInputFile(oligosfile, in); @@ -1403,7 +1802,7 @@ bool MakeContigsCommand::getOligos(vector >& fastaFileNames, vect ofstream temp; - fastaFileName = outputDir + m->getRootName(m->getSimpleName(ffastqfile)) + comboGroupName + ".fasta"; + fastaFileName = rootname + comboGroupName + ".fasta"; if (uniqueNames.count(fastaFileName) == 0) { outputNames.push_back(fastaFileName); outputTypes["fasta"].push_back(fastaFileName); @@ -1413,15 +1812,16 @@ bool MakeContigsCommand::getOligos(vector >& fastaFileNames, vect fastaFileNames[itBar->first][itPrimer->first] = fastaFileName; m->openOutputFile(fastaFileName, temp); temp.close(); - - qualFileName = outputDir + m->getRootName(m->getSimpleName(ffastqfile)) + comboGroupName + ".qual"; - if (uniqueNames.count(qualFileName) == 0) { - outputNames.push_back(qualFileName); - outputTypes["qfile"].push_back(qualFileName); - } + if ((fqualfile != "") || (ffastqfile != "") || (file != "")) { + qualFileName = rootname + ".qual"; + if (uniqueNames.count(qualFileName) == 0) { + outputNames.push_back(qualFileName); + outputTypes["qfile"].push_back(qualFileName); + } - qualFileNames[itBar->first][itPrimer->first] = qualFileName; - m->openOutputFile(qualFileName, temp); temp.close(); + qualFileNames[itBar->first][itPrimer->first] = qualFileName; + m->openOutputFile(qualFileName, temp); temp.close(); + } } } } diff --git a/makecontigscommand.h b/makecontigscommand.h index 3300f69..65b3658 100644 --- a/makecontigscommand.h +++ b/makecontigscommand.h @@ -74,16 +74,16 @@ private: map groupCounts; map groupMap; - //map combos; - //map groupToIndex; - //vector groupVector; fastqRead readFastq(ifstream&, bool&); - vector< vector > readFastqFiles(unsigned long int&); - bool checkReads(fastqRead&, fastqRead&); + vector< vector< vector > > preProcessData(unsigned long int&); + vector< vector > readFileNames(string); + vector< vector > readFastqFiles(unsigned long int&, string, string); + vector< vector > readFastaFiles(unsigned long int&, string, string); + bool checkReads(fastqRead&, fastqRead&, string, string); int createProcesses(vector< vector >, string, string, string, string, string, vector >, vector >); int driver(vector, string, string, string, string, string, vector >, vector >); - bool getOligos(vector >&, vector >&); + bool getOligos(vector >&, vector< vector >&, string); string reverseOligo(string); vector getReads(bool ignoref, bool ignorer, fastqRead forward, fastqRead reverse, map& uniques); }; @@ -173,29 +173,31 @@ static DWORD WINAPI MyContigsThreadFunction(LPVOID lpParam){ if (pDataArray->fastaFileNames[i][j] != "") { ofstream temp; pDataArray->m->openOutputFile(pDataArray->fastaFileNames[i][j], temp); temp.close(); - pDataArray->m->openOutputFile(pDataArray->qualFileNames[i][j], temp); temp.close(); + if (thisfqualfile != "") { pDataArray->m->openOutputFile(pDataArray->qualFileNames[i][j], temp); temp.close(); } } } } } ifstream inFFasta, inRFasta, inFQual, inRQual; + ofstream outFasta, outQual, outMisMatch, outScrapFasta, outScrapQual; pDataArray->m->openInputFile(thisffastafile, inFFasta); - pDataArray->m->openInputFile(thisfqualfile, inFQual); pDataArray->m->openInputFile(thisrfastafile, inRFasta); - pDataArray->m->openInputFile(thisrqualfile, inRQual); - - ofstream outFasta, outQual, outMisMatch, outScrapFasta, outScrapQual; + if (thisfqualfile != "") { + pDataArray->m->openInputFile(thisfqualfile, inFQual); + pDataArray->m->openInputFile(thisrqualfile, inRQual); + pDataArray->m->openOutputFile(pDataArray->outputQual, outQual); + pDataArray->m->openOutputFile(pDataArray->outputScrapQual, outScrapQual); + } pDataArray->m->openOutputFile(pDataArray->outputFasta, outFasta); - pDataArray->m->openOutputFile(pDataArray->outputQual, outQual); pDataArray->m->openOutputFile(pDataArray->outputMisMatches, outMisMatch); pDataArray->m->openOutputFile(pDataArray->outputScrapFasta, outScrapFasta); - pDataArray->m->openOutputFile(pDataArray->outputScrapQual, outScrapQual); + outMisMatch << "Name\tLength\tMisMatches\n"; TrimOligos trimOligos(pDataArray->pdiffs, pDataArray->bdiffs, 0, 0, pDataArray->primers, pDataArray->barcodes); - while ((!inFQual.eof()) && (!inFFasta.eof()) && (!inRFasta.eof()) && (!inRQual.eof())) { + while ((!inFFasta.eof()) && (!inRFasta.eof())) { if (pDataArray->m->control_pressed) { break; } @@ -206,20 +208,31 @@ static DWORD WINAPI MyContigsThreadFunction(LPVOID lpParam){ //read seqs and quality info Sequence fSeq(inFFasta); pDataArray->m->gobble(inFFasta); Sequence rSeq(inRFasta); pDataArray->m->gobble(inRFasta); - QualityScores fQual(inFQual); pDataArray->m->gobble(inFQual); - QualityScores rQual(inRQual); pDataArray->m->gobble(inRQual); + QualityScores* fQual = NULL; QualityScores* rQual = NULL; + if (thisfqualfile != "") { + fQual = new QualityScores(inFQual); pDataArray->m->gobble(inFQual); + rQual = new QualityScores(inRQual); pDataArray->m->gobble(inRQual); + } int barcodeIndex = 0; int primerIndex = 0; if(pDataArray->barcodes.size() != 0){ - success = trimOligos.stripBarcode(fSeq, rSeq, fQual, rQual, barcodeIndex); + if (thisfqualfile != "") { + success = trimOligos.stripBarcode(fSeq, rSeq, *fQual, *rQual, barcodeIndex); + }else { + success = trimOligos.stripBarcode(fSeq, rSeq, barcodeIndex); + } if(success > pDataArray->bdiffs) { trashCode += 'b'; } else{ currentSeqsDiffs += success; } } if(pDataArray->primers.size() != 0){ - success = trimOligos.stripForward(fSeq, rSeq, fQual, rQual, primerIndex); + if (thisfqualfile != "") { + success = trimOligos.stripForward(fSeq, rSeq, *fQual, *rQual, primerIndex); + }else { + success = trimOligos.stripForward(fSeq, rSeq, primerIndex); + } if(success > pDataArray->pdiffs) { trashCode += 'f'; } else{ currentSeqsDiffs += success; } } @@ -228,7 +241,7 @@ static DWORD WINAPI MyContigsThreadFunction(LPVOID lpParam){ //flip the reverse reads rSeq.reverseComplement(); - rQual.flipQScores(); + if (thisfqualfile != "") { rQual->flipQScores(); } //pairwise align alignment->align(fSeq.getUnaligned(), rSeq.getUnaligned()); @@ -244,8 +257,12 @@ static DWORD WINAPI MyContigsThreadFunction(LPVOID lpParam){ int numMismatches = 0; string seq1 = fSeq.getAligned(); string seq2 = rSeq.getAligned(); - vector scores1 = fQual.getQualityScores(); - vector scores2 = rQual.getQualityScores(); + vector scores1, scores2; + if (thisfqualfile != "") { + scores1 = fQual->getQualityScores(); + scores2 = rQual->getQualityScores(); + delete fQual; delete rQual; + } int overlapStart = fSeq.getStartPos(); int seq2Start = rSeq.getStartPos(); @@ -254,12 +271,12 @@ static DWORD WINAPI MyContigsThreadFunction(LPVOID lpParam){ overlapStart = seq2Start; for (int i = 0; i < overlapStart; i++) { contig += seq1[i]; - contigScores.push_back(scores1[ABaseMap[i]]); + if (thisfqualfile != "") { contigScores.push_back(scores1[ABaseMap[i]]); } } }else { //seq1 starts later so take from 0 to overlapStart from seq2 for (int i = 0; i < overlapStart; i++) { contig += seq2[i]; - contigScores.push_back(scores2[BBaseMap[i]]); + if (thisfqualfile != "") { contigScores.push_back(scores2[BBaseMap[i]]); } } } @@ -271,26 +288,34 @@ static DWORD WINAPI MyContigsThreadFunction(LPVOID lpParam){ for (int i = overlapStart; i < overlapEnd; i++) { if (seq1[i] == seq2[i]) { //match, add base and choose highest score contig += seq1[i]; - contigScores.push_back(scores1[ABaseMap[i]]); - if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { contigScores[contigScores.size()-1] = scores2[BBaseMap[i]]; } - }else if (((seq1[i] == '.') || (seq1[i] == '-')) && ((seq2[i] != '-') && (seq2[i] != '.'))) { //seq1 is a gap and seq2 is a base, choose seq2, unless quality score for base is below threshold. In that case eliminate base - if (scores2[BBaseMap[i]] < pDataArray->threshold) { } // - else { - contig += seq2[i]; - contigScores.push_back(scores2[BBaseMap[i]]); - } - }else if (((seq2[i] == '.') || (seq2[i] == '-')) && ((seq1[i] != '-') && (seq1[i] != '.'))) { //seq2 is a gap and seq1 is a base, choose seq1, unless quality score for base is below threshold. In that case eliminate base - if (scores1[ABaseMap[i]] < pDataArray->threshold) { } // - else { - contig += seq1[i]; + if (thisfqualfile != "") { contigScores.push_back(scores1[ABaseMap[i]]); + if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { contigScores[contigScores.size()-1] = scores2[BBaseMap[i]]; } } + }else if (((seq1[i] == '.') || (seq1[i] == '-')) && ((seq2[i] != '-') && (seq2[i] != '.'))) { //seq1 is a gap and seq2 is a base, choose seq2, unless quality score for base is below threshold. In that case eliminate base + if (thisfqualfile != "") { + if (scores2[BBaseMap[i]] < pDataArray->threshold) { } // + else { + contig += seq2[i]; + contigScores.push_back(scores2[BBaseMap[i]]); + } + }else { contig += seq2[i]; } + }else if (((seq2[i] == '.') || (seq2[i] == '-')) && ((seq1[i] != '-') && (seq1[i] != '.'))) { //seq2 is a gap and seq1 is a base, choose seq1, unless quality score for base is below threshold. In that case eliminate base + if (thisfqualfile != "") { + if (scores1[ABaseMap[i]] < pDataArray->threshold) { } // + else { + contig += seq1[i]; + contigScores.push_back(scores1[ABaseMap[i]]); + } + }else { contig += seq1[i]; } }else if (((seq1[i] != '-') && (seq1[i] != '.')) && ((seq2[i] != '-') && (seq2[i] != '.'))) { //both bases choose one with better quality - char c = seq1[i]; - contigScores.push_back(scores1[ABaseMap[i]]); - if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { contigScores[contigScores.size()-1] = scores2[BBaseMap[i]]; c = seq2[i]; } - contig += c; - numMismatches++; + if (thisfqualfile != "") { + char c = seq1[i]; + contigScores.push_back(scores1[ABaseMap[i]]); + if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { contigScores[contigScores.size()-1] = scores2[BBaseMap[i]]; c = seq2[i]; } + contig += c; + numMismatches++; + }else { numMismatches++; } }else { //should never get here pDataArray->m->mothurOut("[ERROR]: case I didn't think of seq1 = " + toString(seq1[i]) + " and seq2 = " + toString(seq2[i]) + "\n"); } @@ -299,12 +324,12 @@ static DWORD WINAPI MyContigsThreadFunction(LPVOID lpParam){ if (seq1End < seq2End) { //seq1 ends before seq2 so take from overlap to length from seq2 for (int i = overlapEnd; i < length; i++) { contig += seq2[i]; - contigScores.push_back(scores2[BBaseMap[i]]); + if (thisfqualfile != "") { contigScores.push_back(scores2[BBaseMap[i]]); } } }else { //seq2 ends before seq1 so take from overlap to length from seq1 for (int i = overlapEnd; i < length; i++) { contig += seq1[i]; - contigScores.push_back(scores1[ABaseMap[i]]); + if (thisfqualfile != "") { contigScores.push_back(scores1[ABaseMap[i]]); } } } @@ -340,25 +365,31 @@ static DWORD WINAPI MyContigsThreadFunction(LPVOID lpParam){ output << ">" << fSeq.getName() << endl << contig << endl; output.close(); - pDataArray->m->openOutputFileAppend(pDataArray->qualFileNames[barcodeIndex][primerIndex], output); - output << ">" << fSeq.getName() << endl; - for (int i = 0; i < contigScores.size(); i++) { output << contigScores[i] << ' '; } - output << endl; - output.close(); + if (thisfqualfile != "") { + pDataArray->m->openOutputFileAppend(pDataArray->qualFileNames[barcodeIndex][primerIndex], output); + output << ">" << fSeq.getName() << endl; + for (int i = 0; i < contigScores.size(); i++) { output << contigScores[i] << ' '; } + output << endl; + output.close(); + } } //output outFasta << ">" << fSeq.getName() << endl << contig << endl; - outQual << ">" << fSeq.getName() << endl; - for (int i = 0; i < contigScores.size(); i++) { outQual << contigScores[i] << ' '; } - outQual << endl; + if (thisfqualfile != "") { + outQual << ">" << fSeq.getName() << endl; + for (int i = 0; i < contigScores.size(); i++) { outQual << contigScores[i] << ' '; } + outQual << endl; + } outMisMatch << fSeq.getName() << '\t' << contig.length() << '\t' << numMismatches << endl; }else { //output outScrapFasta << ">" << fSeq.getName() << " | " << trashCode << endl << contig << endl; - outScrapQual << ">" << fSeq.getName() << " | " << trashCode << endl; - for (int i = 0; i < contigScores.size(); i++) { outScrapQual << contigScores[i] << ' '; } - outScrapQual << endl; + if (thisfqualfile != "") { + outScrapQual << ">" << fSeq.getName() << " | " << trashCode << endl; + for (int i = 0; i < contigScores.size(); i++) { outScrapQual << contigScores[i] << ' '; } + outScrapQual << endl; + } } num++; @@ -370,17 +401,19 @@ static DWORD WINAPI MyContigsThreadFunction(LPVOID lpParam){ if((num) % 1000 != 0){ pDataArray->m->mothurOut(toString(num)); pDataArray->m->mothurOutEndLine(); } inFFasta.close(); - inFQual.close(); inRFasta.close(); - inRQual.close(); outFasta.close(); - outQual.close(); outMisMatch.close(); outScrapFasta.close(); - outScrapQual.close(); + if (thisfqualfile != "") { + inFQual.close(); + inRQual.close(); + outQual.close(); + outScrapQual.close(); + } delete alignment; - if (pDataArray->m->control_pressed) { pDataArray->m->mothurRemove(pDataArray->outputQual); pDataArray->m->mothurRemove(pDataArray->outputFasta); pDataArray->m->mothurRemove(pDataArray->outputMisMatches); pDataArray->m->mothurRemove(pDataArray->outputScrapQual); pDataArray->m->mothurRemove(pDataArray->outputScrapFasta);} + if (pDataArray->m->control_pressed) { pDataArray->m->mothurRemove(pDataArray->outputFasta); pDataArray->m->mothurRemove(pDataArray->outputMisMatches); pDataArray->m->mothurRemove(pDataArray->outputScrapFasta); if (thisfqualfile != "") { pDataArray->m->mothurRemove(pDataArray->outputQual); pDataArray->m->mothurRemove(pDataArray->outputScrapQual); } } return 0; diff --git a/summarysharedcommand.cpp b/summarysharedcommand.cpp index 85eb448..fdec2ec 100644 --- a/summarysharedcommand.cpp +++ b/summarysharedcommand.cpp @@ -190,7 +190,9 @@ SummarySharedCommand::SummarySharedCommand(string option) { temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "1000"; } m->mothurConvert(temp, iters); - output = validParameter.validFile(parameters, "output", false); if(output == "not found"){ output = "lt"; } + output = validParameter.validFile(parameters, "output", false); + if(output == "not found"){ output = "lt"; } + else { createPhylip = true; } if ((output != "lt") && (output != "square")) { m->mothurOut(output + " is not a valid output form. Options are lt and square. I will use lt."); m->mothurOutEndLine(); output = "lt"; } temp = validParameter.validFile(parameters, "subsample", false); if (temp == "not found") { temp = "F"; } @@ -789,6 +791,7 @@ int SummarySharedCommand::process(vector thisLookup, string variables["[calc]"] = sumCalculators[i]->getName(); variables["[distance]"] = thisLookup[0]->getLabel(); variables["[outputtag]"] = output; + variables["[tag2]"] = ""; string distFileName = getOutputFileName("phylip",variables); outputNames.push_back(distFileName); outputTypes["phylip"].push_back(distFileName); ofstream outDist; @@ -804,7 +807,7 @@ int SummarySharedCommand::process(vector thisLookup, string for (int i = 0; i < calcDists.size(); i++) { calcDists[i].clear(); } } - if (iters != 1) { + if (iters != 0) { //we need to find the average distance and standard deviation for each groups distance vector< vector > calcAverages; calcAverages.resize(sumCalculators.size()); diff --git a/trimoligos.cpp b/trimoligos.cpp index c35971a..ecdca9b 100644 --- a/trimoligos.cpp +++ b/trimoligos.cpp @@ -259,7 +259,7 @@ int TrimOligos::stripBarcode(Sequence& seq, QualityScores& qual, int& group){ } } //*******************************************************************/ -int TrimOligos::stripBarcode(Sequence& forwardSeq, Sequence& reverseSeq, QualityScores& forwardQual, QualityScores& reverseQual, int& group){ +int TrimOligos::stripBarcode(Sequence& forwardSeq, Sequence& reverseSeq, int& group){ try { //look for forward barcode string rawFSequence = forwardSeq.getUnaligned(); @@ -284,8 +284,6 @@ int TrimOligos::stripBarcode(Sequence& forwardSeq, Sequence& reverseSeq, Quality group = it->first; forwardSeq.setUnaligned(rawFSequence.substr(foligo.length())); reverseSeq.setUnaligned(rawRSequence.substr(0,(rawRSequence.length()-roligo.length()))); - forwardQual.trimQScores(foligo.length(), -1); - reverseQual.trimQScores(-1, rawRSequence.length()-roligo.length()); success = 0; break; } @@ -464,6 +462,233 @@ int TrimOligos::stripBarcode(Sequence& forwardSeq, Sequence& reverseSeq, Quality rStart = minRPos[0]; } + //we have an acceptable match for the forward and reverse, but do they match? + if (foundMatch) { + forwardSeq.setUnaligned(rawFSequence.substr(fStart)); + reverseSeq.setUnaligned(rawRSequence.substr(rStart)); + success = minDiff; + }else { success = bdiffs + 100; } + } + } + + if (alignment != NULL) { delete alignment; } + } + + return success; + + } + catch(exception& e) { + m->errorOut(e, "TrimOligos", "stripIBarcode"); + exit(1); + } + +} +//*******************************************************************/ +int TrimOligos::stripBarcode(Sequence& forwardSeq, Sequence& reverseSeq, QualityScores& forwardQual, QualityScores& reverseQual, int& group){ + try { + //look for forward barcode + string rawFSequence = forwardSeq.getUnaligned(); + string rawRSequence = reverseSeq.getUnaligned(); + int success = bdiffs + 1; //guilty until proven innocent + + //can you find the forward barcode + for(map::iterator it=ipbarcodes.begin();it!=ipbarcodes.end();it++){ + string foligo = it->second.forward; + string roligo = it->second.reverse; + + if(rawFSequence.length() < foligo.length()){ //let's just assume that the barcodes are the same length + success = bdiffs + 10; //if the sequence is shorter than the barcode then bail out + break; + } + if(rawRSequence.length() < roligo.length()){ //let's just assume that the barcodes are the same length + success = bdiffs + 10; //if the sequence is shorter than the barcode then bail out + break; + } + + if((compareDNASeq(foligo, rawFSequence.substr(0,foligo.length()))) && (compareDNASeq(roligo, rawRSequence.substr((rawRSequence.length()-roligo.length()),roligo.length())))) { + group = it->first; + forwardSeq.setUnaligned(rawFSequence.substr(foligo.length())); + reverseSeq.setUnaligned(rawRSequence.substr(0,(rawRSequence.length()-roligo.length()))); + forwardQual.trimQScores(foligo.length(), -1); + reverseQual.trimQScores(-1, rawRSequence.length()-roligo.length()); + success = 0; + break; + } + } + + //if you found the barcode or if you don't want to allow for diffs + if ((bdiffs == 0) || (success == 0)) { return success; } + else { //try aligning and see if you can find it + Alignment* alignment; + if (ifbarcodes.size() > 0) { alignment = new NeedlemanOverlap(-1.0, 1.0, -1.0, (maxFBarcodeLength+bdiffs+1)); } + else{ alignment = NULL; } + + //can you find the barcode + int minDiff = 1e6; + int minCount = 1; + vector< vector > minFGroup; + vector minFPos; + + //the pair can have duplicates, but we only want to search for the unique forward and reverses, example + /* + 1 Sarah Westcott + 2 John Westcott + 3 Anna Westcott + 4 Sarah Schloss + 5 Pat Schloss + 6 Gail Brown + 7 Pat Moore + + only want to look for forward = Sarah, John, Anna, Pat, Gail + reverse = Westcott, Schloss, Brown, Moore + + but if best match forward = 4, and reverse = 1, we want to count as a valid match because forward 1 and forward 4 are the same. so both barcodes map to same group. + */ + //cout << endl << forwardSeq.getName() << endl; + for(map >::iterator it=ifbarcodes.begin();it!=ifbarcodes.end();it++){ + string oligo = it->first; + + if(rawFSequence.length() < maxFBarcodeLength){ //let's just assume that the barcodes are the same length + success = bdiffs + 10; + break; + } + //cout << "before = " << oligo << '\t' << rawFSequence.substr(0,oligo.length()+bdiffs) << endl; + //use needleman to align first barcode.length()+numdiffs of sequence to each barcode + alignment->align(oligo, rawFSequence.substr(0,oligo.length()+bdiffs)); + oligo = alignment->getSeqAAln(); + string temp = alignment->getSeqBAln(); + + int alnLength = oligo.length(); + + for(int i=oligo.length()-1;i>=0;i--){ if(oligo[i] != '-'){ alnLength = i+1; break; } } + oligo = oligo.substr(0,alnLength); + temp = temp.substr(0,alnLength); + int numDiff = countDiffs(oligo, temp); + + if (alnLength == 0) { numDiff = bdiffs + 100; } + //cout << "after = " << oligo << '\t' << temp << '\t' << numDiff << endl; + + if(numDiff < minDiff){ + minDiff = numDiff; + minCount = 1; + minFGroup.clear(); + minFGroup.push_back(it->second); + int tempminFPos = 0; + minFPos.clear(); + for(int i=0;isecond); + int tempminFPos = 0; + for(int i=0;i bdiffs) { success = minDiff; } //no good matches + else{ + //check for reverse match + if (alignment != NULL) { delete alignment; } + + if (irbarcodes.size() > 0) { alignment = new NeedlemanOverlap(-1.0, 1.0, -1.0, (maxRBarcodeLength+bdiffs+1)); } + else{ alignment = NULL; } + + //can you find the barcode + minDiff = 1e6; + minCount = 1; + vector< vector > minRGroup; + vector minRPos; + + for(map >::iterator it=irbarcodes.begin();it!=irbarcodes.end();it++){ + string oligo = it->first; + //cout << "before = " << oligo << '\t' << rawRSequence.substr(0,oligo.length()+bdiffs) << endl; + if(rawRSequence.length() < maxRBarcodeLength){ //let's just assume that the barcodes are the same length + success = bdiffs + 10; + break; + } + + //use needleman to align first barcode.length()+numdiffs of sequence to each barcode + alignment->align(oligo, rawRSequence.substr(0,oligo.length()+bdiffs)); + oligo = alignment->getSeqAAln(); + string temp = alignment->getSeqBAln(); + + int alnLength = oligo.length(); + for(int i=oligo.length()-1;i>=0;i--){ if(oligo[i] != '-'){ alnLength = i+1; break; } } + oligo = oligo.substr(0,alnLength); + temp = temp.substr(0,alnLength); + int numDiff = countDiffs(oligo, temp); + if (alnLength == 0) { numDiff = bdiffs + 100; } + + //cout << "after = " << oligo << '\t' << temp << '\t' << numDiff << endl; + if(numDiff < minDiff){ + minDiff = numDiff; + minCount = 1; + minRGroup.clear(); + minRGroup.push_back(it->second); + int tempminRPos = 0; + minRPos.clear(); + for(int i=0;isecond); + } + + } + + /*cout << minDiff << '\t' << minCount << '\t' << endl; + for (int i = 0; i < minFGroup.size(); i++) { + cout << i << '\t'; + for (int j = 0; j < minFGroup[i].size(); j++) { cout << minFGroup[i][j] << " "; } + cout << endl; + } + cout << endl; + for (int i = 0; i < minRGroup.size(); i++) { + cout << i << '\t'; + for (int j = 0; j < minRGroup[i].size(); j++) { cout << minRGroup[i][j] << " "; } + cout << endl; + } + cout << endl;*/ + if(minDiff > bdiffs) { success = minDiff; } //no good matches + else { + bool foundMatch = false; + vector matches; + for (int i = 0; i < minFGroup.size(); i++) { + for (int j = 0; j < minFGroup[i].size(); j++) { + for (int k = 0; k < minRGroup.size(); k++) { + if (m->inUsersGroups(minFGroup[i][j], minRGroup[k])) { matches.push_back(minFGroup[i][j]); k+= minRGroup.size(); } + } + } + } + + int fStart = 0; + int rStart = 0; + if (matches.size() == 1) { + foundMatch = true; + group = matches[0]; + fStart = minFPos[0]; + rStart = minRPos[0]; + } + //we have an acceptable match for the forward and reverse, but do they match? if (foundMatch) { forwardSeq.setUnaligned(rawFSequence.substr(fStart)); @@ -715,6 +940,231 @@ int TrimOligos::stripForward(Sequence& forwardSeq, Sequence& reverseSeq, Quality exit(1); } +} +//*******************************************************************/ +int TrimOligos::stripForward(Sequence& forwardSeq, Sequence& reverseSeq, int& group){ + try { + //look for forward barcode + string rawFSequence = forwardSeq.getUnaligned(); + string rawRSequence = reverseSeq.getUnaligned(); + int success = pdiffs + 1; //guilty until proven innocent + + //can you find the forward barcode + for(map::iterator it=ipprimers.begin();it!=ipprimers.end();it++){ + string foligo = it->second.forward; + string roligo = it->second.reverse; + + if(rawFSequence.length() < foligo.length()){ //let's just assume that the barcodes are the same length + success = pdiffs + 10; //if the sequence is shorter than the barcode then bail out + break; + } + if(rawRSequence.length() < roligo.length()){ //let's just assume that the barcodes are the same length + success = pdiffs + 10; //if the sequence is shorter than the barcode then bail out + break; + } + + if((compareDNASeq(foligo, rawFSequence.substr(0,foligo.length()))) && (compareDNASeq(roligo, rawRSequence.substr((rawRSequence.length()-roligo.length()),roligo.length())))) { + group = it->first; + forwardSeq.setUnaligned(rawFSequence.substr(foligo.length())); + reverseSeq.setUnaligned(rawRSequence.substr(0,(rawRSequence.length()-roligo.length()))); + success = 0; + break; + } + } + + //if you found the barcode or if you don't want to allow for diffs + if ((pdiffs == 0) || (success == 0)) { return success; } + else { //try aligning and see if you can find it + Alignment* alignment; + if (ifprimers.size() > 0) { alignment = new NeedlemanOverlap(-1.0, 1.0, -1.0, (maxFPrimerLength+pdiffs+1)); } + else{ alignment = NULL; } + + //can you find the barcode + int minDiff = 1e6; + int minCount = 1; + vector< vector > minFGroup; + vector minFPos; + + //the pair can have duplicates, but we only want to search for the unique forward and reverses, example + /* + 1 Sarah Westcott + 2 John Westcott + 3 Anna Westcott + 4 Sarah Schloss + 5 Pat Schloss + 6 Gail Brown + 7 Pat Moore + + only want to look for forward = Sarah, John, Anna, Pat, Gail + reverse = Westcott, Schloss, Brown, Moore + + but if best match forward = 4, and reverse = 1, we want to count as a valid match because forward 1 and forward 4 are the same. so both barcodes map to same group. + */ + //cout << endl << forwardSeq.getName() << endl; + for(map >::iterator it=ifprimers.begin();it!=ifprimers.end();it++){ + string oligo = it->first; + + if(rawFSequence.length() < maxFPrimerLength){ //let's just assume that the barcodes are the same length + success = pdiffs + 10; + break; + } + //cout << "before = " << oligo << '\t' << rawFSequence.substr(0,oligo.length()+pdiffs) << endl; + //use needleman to align first barcode.length()+numdiffs of sequence to each barcode + alignment->align(oligo, rawFSequence.substr(0,oligo.length()+pdiffs)); + oligo = alignment->getSeqAAln(); + string temp = alignment->getSeqBAln(); + + int alnLength = oligo.length(); + + for(int i=oligo.length()-1;i>=0;i--){ if(oligo[i] != '-'){ alnLength = i+1; break; } } + oligo = oligo.substr(0,alnLength); + temp = temp.substr(0,alnLength); + int numDiff = countDiffs(oligo, temp); + + if (alnLength == 0) { numDiff = pdiffs + 100; } + //cout << "after = " << oligo << '\t' << temp << '\t' << numDiff << endl; + + if(numDiff < minDiff){ + minDiff = numDiff; + minCount = 1; + minFGroup.clear(); + minFGroup.push_back(it->second); + int tempminFPos = 0; + minFPos.clear(); + for(int i=0;isecond); + int tempminFPos = 0; + for(int i=0;i pdiffs) { success = minDiff; } //no good matches + else{ + //check for reverse match + if (alignment != NULL) { delete alignment; } + + if (irbarcodes.size() > 0) { alignment = new NeedlemanOverlap(-1.0, 1.0, -1.0, (maxRPrimerLength+pdiffs+1)); } + else{ alignment = NULL; } + + //can you find the barcode + minDiff = 1e6; + minCount = 1; + vector< vector > minRGroup; + vector minRPos; + + for(map >::iterator it=irprimers.begin();it!=irprimers.end();it++){ + string oligo = it->first; + //cout << "before = " << oligo << '\t' << rawRSequence.substr(0,oligo.length()+pdiffs) << endl; + if(rawRSequence.length() < maxRPrimerLength){ //let's just assume that the barcodes are the same length + success = pdiffs + 10; + break; + } + + //use needleman to align first barcode.length()+numdiffs of sequence to each barcode + alignment->align(oligo, rawRSequence.substr(0,oligo.length()+pdiffs)); + oligo = alignment->getSeqAAln(); + string temp = alignment->getSeqBAln(); + + int alnLength = oligo.length(); + for(int i=oligo.length()-1;i>=0;i--){ if(oligo[i] != '-'){ alnLength = i+1; break; } } + oligo = oligo.substr(0,alnLength); + temp = temp.substr(0,alnLength); + int numDiff = countDiffs(oligo, temp); + if (alnLength == 0) { numDiff = pdiffs + 100; } + + //cout << "after = " << oligo << '\t' << temp << '\t' << numDiff << endl; + if(numDiff < minDiff){ + minDiff = numDiff; + minCount = 1; + minRGroup.clear(); + minRGroup.push_back(it->second); + int tempminRPos = 0; + minRPos.clear(); + for(int i=0;isecond); + } + + } + + /*cout << minDiff << '\t' << minCount << '\t' << endl; + for (int i = 0; i < minFGroup.size(); i++) { + cout << i << '\t'; + for (int j = 0; j < minFGroup[i].size(); j++) { cout << minFGroup[i][j] << " "; } + cout << endl; + } + cout << endl; + for (int i = 0; i < minRGroup.size(); i++) { + cout << i << '\t'; + for (int j = 0; j < minRGroup[i].size(); j++) { cout << minRGroup[i][j] << " "; } + cout << endl; + } + cout << endl;*/ + if(minDiff > pdiffs) { success = minDiff; } //no good matches + else { + bool foundMatch = false; + vector matches; + for (int i = 0; i < minFGroup.size(); i++) { + for (int j = 0; j < minFGroup[i].size(); j++) { + for (int k = 0; k < minRGroup.size(); k++) { + if (m->inUsersGroups(minFGroup[i][j], minRGroup[k])) { matches.push_back(minFGroup[i][j]); k+= minRGroup.size(); } + } + } + } + + int fStart = 0; + int rStart = 0; + if (matches.size() == 1) { + foundMatch = true; + group = matches[0]; + fStart = minFPos[0]; + rStart = minRPos[0]; + } + + //we have an acceptable match for the forward and reverse, but do they match? + if (foundMatch) { + forwardSeq.setUnaligned(rawFSequence.substr(fStart)); + reverseSeq.setUnaligned(rawRSequence.substr(rStart)); + success = minDiff; + }else { success = pdiffs + 100; } + } + } + + if (alignment != NULL) { delete alignment; } + } + + return success; + + } + catch(exception& e) { + m->errorOut(e, "TrimOligos", "stripIForward"); + exit(1); + } + } //*******************************************************************/ int TrimOligos::stripBarcode(Sequence& seq, int& group){ diff --git a/trimoligos.h b/trimoligos.h index 6716deb..8aa44bd 100644 --- a/trimoligos.h +++ b/trimoligos.h @@ -35,10 +35,12 @@ class TrimOligos { int stripBarcode(Sequence&, int&); int stripBarcode(Sequence&, QualityScores&, int&); int stripBarcode(Sequence&, Sequence&, QualityScores&, QualityScores&, int&); + int stripBarcode(Sequence&, Sequence&, int&); int stripForward(Sequence&, int&); int stripForward(Sequence&, QualityScores&, int&, bool); int stripForward(Sequence&, Sequence&, QualityScores&, QualityScores&, int&); + int stripForward(Sequence&, Sequence&, int&); bool stripReverse(Sequence&); bool stripReverse(Sequence&, QualityScores&); diff --git a/trimseqscommand.cpp b/trimseqscommand.cpp index 64d9d9e..eb31e78 100644 --- a/trimseqscommand.cpp +++ b/trimseqscommand.cpp @@ -109,7 +109,7 @@ string TrimSeqsCommand::getOutputPattern(string type) { else if (type == "fasta") { pattern = "[filename],[tag],fasta"; } else if (type == "group") { pattern = "[filename],groups"; } else if (type == "name") { pattern = "[filename],[tag],names"; } - else if (type == "count") { pattern = "[filename],count_table"; } + else if (type == "count") { pattern = "[filename],[tag],count_table"; } else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; } return pattern;