X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=shhhercommand.cpp;h=115e930969323ba4655e0b5ba5c2a8e1166a1397;hb=1f3d51cfdbe3e28c780501b7ed82d64fe27dba98;hp=d00ead8476ed226043b781edf680ac53a90e9a69;hpb=c9c7a14ad8e6b22fd760741b76f4e687a9ced458;p=mothur.git diff --git a/shhhercommand.cpp b/shhhercommand.cpp index d00ead8..115e930 100644 --- a/shhhercommand.cpp +++ b/shhhercommand.cpp @@ -18,6 +18,7 @@ vector ShhherCommand::setParameters(){ CommandParameter pcutoff("cutoff", "Number", "", "0.01", "", "", "",false,false); parameters.push_back(pcutoff); CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors); CommandParameter pmaxiter("maxiter", "Number", "", "1000", "", "", "",false,false); parameters.push_back(pmaxiter); + CommandParameter plarge("large", "Number", "", "-1", "", "", "",false,false); parameters.push_back(plarge); CommandParameter psigma("sigma", "Number", "", "60", "", "", "",false,false); parameters.push_back(psigma); CommandParameter pmindelta("mindelta", "Number", "", "0.000001", "", "", "",false,false); parameters.push_back(pmindelta); CommandParameter porder("order", "String", "", "", "", "", "",false,false); parameters.push_back(porder); @@ -46,6 +47,30 @@ string ShhherCommand::getHelpString(){ } } //********************************************************************************************************************** +string ShhherCommand::getOutputFileNameTag(string type, string inputName=""){ + try { + string outputFileName = ""; + map >::iterator it; + + //is this a type this command creates + it = outputTypes.find(type); + if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); } + else { + if (type == "fasta") { outputFileName = "shhh.fasta"; } + else if (type == "name") { outputFileName = "shhh.names"; } + else if (type == "group") { outputFileName = "shhh.groups"; } + else if (type == "counts") { outputFileName = "shhh.counts"; } + else if (type == "qfile") { outputFileName = "shhh.qual"; } + else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; } + } + return outputFileName; + } + catch(exception& e) { + m->errorOut(e, "ShhherCommand", "getOutputFileNameTag"); + exit(1); + } +} +//********************************************************************************************************************** ShhherCommand::ShhherCommand(){ try { @@ -53,8 +78,12 @@ ShhherCommand::ShhherCommand(){ setParameters(); //initialize outputTypes -// vector tempOutNames; -// outputTypes["pn.dist"] = tempOutNames; + vector tempOutNames; + outputTypes["fasta"] = tempOutNames; + outputTypes["name"] = tempOutNames; + outputTypes["group"] = tempOutNames; + outputTypes["counts"] = tempOutNames; + outputTypes["qfile"] = tempOutNames; } catch(exception& e) { @@ -95,9 +124,13 @@ ShhherCommand::ShhherCommand(string option) { } //initialize outputTypes - vector tempOutNames; -// outputTypes["pn.dist"] = tempOutNames; - // outputTypes["fasta"] = tempOutNames; + vector tempOutNames; + outputTypes["fasta"] = tempOutNames; + outputTypes["name"] = tempOutNames; + outputTypes["group"] = tempOutNames; + outputTypes["counts"] = tempOutNames; + outputTypes["qfile"] = 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); @@ -128,13 +161,15 @@ ShhherCommand::ShhherCommand(string option) { if (path == "") { parameters["file"] = inputDir + it->second; } } } - - + + //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 = ""; } + //check for required parameters flowFileName = validParameter.validFile(parameters, "flow", true); flowFilesFileName = validParameter.validFile(parameters, "file", true); if (flowFileName == "not found" && flowFilesFileName == "not found") { - m->mothurOut("values for either flow or file must be provided for the shhh.seqs command."); + m->mothurOut("values for either flow or file must be provided for the shhh.flows command."); m->mothurOutEndLine(); abort = true; } @@ -146,13 +181,22 @@ ShhherCommand::ShhherCommand(string option) { } else{ ofstream temp; - - //flow.files = 9 character offset - compositeFASTAFileName = flowFilesFileName.substr(0, flowFilesFileName.length()-10) + "shhh.fasta"; + + string thisoutputDir = outputDir; + if (outputDir == "") { thisoutputDir = m->hasPath(flowFilesFileName); } //if user entered a file with a path then preserve it + + //we want to rip off .files, and also .flow if its there + string fileroot = m->getRootName(m->getSimpleName(flowFilesFileName)); + if (fileroot[fileroot.length()-1] == '.') { fileroot = fileroot.substr(0, fileroot.length()-1); } //rip off dot + string extension = m->getExtension(fileroot); + if (extension == ".flow") { fileroot = m->getRootName(fileroot); } + else { fileroot += "."; } //add back if needed + + compositeFASTAFileName = thisoutputDir + fileroot + "shhh.fasta"; m->openOutputFile(compositeFASTAFileName, temp); temp.close(); - compositeNamesFileName = flowFilesFileName.substr(0, flowFilesFileName.length()-10) + "shhh.names"; + compositeNamesFileName = thisoutputDir + fileroot + "shhh.names"; m->openOutputFile(compositeNamesFileName, temp); temp.close(); } @@ -214,17 +258,10 @@ ShhherCommand::ShhherCommand(string option) { if (flowFileVector.size() == 0) { m->mothurOut("[ERROR]: no valid files."); m->mothurOutEndLine(); abort = true; } } else{ + if (outputDir == "") { outputDir = m->hasPath(flowFileName); } flowFileVector.push_back(flowFileName); } - - - //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 = ""; - outputDir += m->hasPath(flowFileName); //if user entered a file with a path then preserve it - } - - + //check for optional parameter and set defaults // ...at some point should added some additional type checking... string temp; @@ -310,6 +347,17 @@ ShhherCommand::ShhherCommand(string option) { temp = validParameter.validFile(parameters, "maxiter", false); if (temp == "not found"){ temp = "1000"; } m->mothurConvert(temp, maxIters); + + temp = validParameter.validFile(parameters, "large", false); if (temp == "not found"){ temp = "0"; } + m->mothurConvert(temp, largeSize); + if (largeSize != 0) { large = true; } + else { large = false; } + if (largeSize < 0) { m->mothurOut("The value of the large cannot be negative.\n"); } + +#ifdef USE_MPI + if (large) { m->mothurOut("The large parameter is not available with the MPI-Enabled version.\n"); large=false; } +#endif + temp = validParameter.validFile(parameters, "sigma", false);if (temp == "not found") { temp = "60"; } m->mothurConvert(temp, sigma); @@ -754,7 +802,7 @@ string ShhherCommand::createNamesFile(){ duplicateNames[mapSeqToUnique[i]] += seqNameVector[i] + ','; } - string nameFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.names"; + string nameFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName) + getOutputFileNameTag("name"); ofstream nameFile; m->openOutputFile(nameFileName, nameFile); @@ -922,6 +970,8 @@ void ShhherCommand::initPyroCluster(){ try{ if (numOTUs < processors) { processors = 1; } + if (m->debug) { m->mothurOut("[DEBUG]: numSeqs = " + toString(numSeqs) + " numOTUS = " + toString(numOTUs) + " about to alloc a dist vector with size = " + toString((numSeqs * numOTUs)) + ".\n"); } + dist.assign(numSeqs * numOTUs, 0); change.assign(numOTUs, 1); centroids.assign(numOTUs, -1); @@ -931,6 +981,8 @@ void ShhherCommand::initPyroCluster(){ nSeqsBreaks.assign(processors+1, 0); nOTUsBreaks.assign(processors+1, 0); + if (m->debug) { m->mothurOut("[DEBUG]: made it through the memory allocation.\n"); } + nSeqsBreaks[0] = 0; for(int i=0;i otuCounts){ try { string thisOutputDir = outputDir; if (outputDir == "") { thisOutputDir += m->hasPath(flowFileName); } - string qualityFileName = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName)) + "shhh.qual"; + string qualityFileName = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName)) + getOutputFileNameTag("qfile"); ofstream qualityFile; m->openOutputFile(qualityFileName, qualityFile); @@ -1701,7 +1753,7 @@ void ShhherCommand::writeSequences(vector otuCounts){ try { string thisOutputDir = outputDir; if (outputDir == "") { thisOutputDir += m->hasPath(flowFileName); } - string fastaFileName = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName)) + "shhh.fasta"; + string fastaFileName = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName)) + getOutputFileNameTag("fasta"); ofstream fastaFile; m->openOutputFile(fastaFileName, fastaFile); @@ -1734,7 +1786,7 @@ void ShhherCommand::writeSequences(vector otuCounts){ outputNames.push_back(fastaFileName); if(compositeFASTAFileName != ""){ - m->appendFiles(fastaFileName, thisOutputDir + compositeFASTAFileName); + m->appendFiles(fastaFileName, compositeFASTAFileName); } } catch(exception& e) { @@ -1749,7 +1801,7 @@ void ShhherCommand::writeNames(vector otuCounts){ try { string thisOutputDir = outputDir; if (outputDir == "") { thisOutputDir += m->hasPath(flowFileName); } - string nameFileName = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName)) + "shhh.names"; + string nameFileName = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName)) + getOutputFileNameTag("name"); ofstream nameFile; m->openOutputFile(nameFileName, nameFile); @@ -1772,7 +1824,7 @@ void ShhherCommand::writeNames(vector otuCounts){ if(compositeNamesFileName != ""){ - m->appendFiles(nameFileName, thisOutputDir + compositeNamesFileName); + m->appendFiles(nameFileName, compositeNamesFileName); } } catch(exception& e) { @@ -1787,14 +1839,17 @@ void ShhherCommand::writeGroups(){ try { string thisOutputDir = outputDir; if (outputDir == "") { thisOutputDir += m->hasPath(flowFileName); } - string fileRoot = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName)); - string groupFileName = fileRoot + "shhh.groups"; + string fileRoot = m->getRootName(m->getSimpleName(flowFileName)); + int pos = fileRoot.find_first_of('.'); + string fileGroup = fileRoot; + if (pos != string::npos) { fileGroup = fileRoot.substr(pos+1, (fileRoot.length()-1-(pos+1))); } + string groupFileName = thisOutputDir + fileRoot + getOutputFileNameTag("group"); ofstream groupFile; m->openOutputFile(groupFileName, groupFile); for(int i=0;icontrol_pressed) { break; } - groupFile << seqNameVector[i] << '\t' << fileRoot << endl; + groupFile << seqNameVector[i] << '\t' << fileGroup << endl; } groupFile.close(); outputNames.push_back(groupFileName); @@ -1812,7 +1867,7 @@ void ShhherCommand::writeClusters(vector otuCounts){ try { string thisOutputDir = outputDir; if (outputDir == "") { thisOutputDir += m->hasPath(flowFileName); } - string otuCountsFileName = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName)) + "shhh.counts"; + string otuCountsFileName = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName)) +getOutputFileNameTag("counts"); ofstream otuCountsFile; m->openOutputFile(otuCountsFileName, otuCountsFile); @@ -1918,12 +1973,14 @@ int ShhherCommand::createProcesses(vector filenames){ //divide the groups between the processors vector lines; + vector numFilesToComplete; int numFilesPerProcessor = filenames.size() / processors; for (int i = 0; i < processors; i++) { int startIndex = i * numFilesPerProcessor; int endIndex = (i+1) * numFilesPerProcessor; if(i == (processors - 1)){ endIndex = filenames.size(); } lines.push_back(linePair(startIndex, endIndex)); + numFilesToComplete.push_back((endIndex-startIndex)); } #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) @@ -1937,6 +1994,14 @@ int ShhherCommand::createProcesses(vector filenames){ process++; }else if (pid == 0){ num = driver(filenames, compositeFASTAFileName + toString(getpid()) + ".temp", compositeNamesFileName + toString(getpid()) + ".temp", lines[process].start, lines[process].end); + + //pass numSeqs to parent + ofstream out; + string tempFile = compositeFASTAFileName + toString(getpid()) + ".num.temp"; + m->openOutputFile(tempFile, out); + out << num << endl; + out.close(); + exit(0); }else { m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); @@ -1999,6 +2064,18 @@ int ShhherCommand::createProcesses(vector filenames){ #endif for (int i=0;iopenInputFile(tempFile, in); + if (!in.eof()) { + int tempNum = 0; + in >> tempNum; + if (tempNum != numFilesToComplete[i+1]) { + m->mothurOut("[ERROR]: main process expected " + toString(processIDS[i]) + " to complete " + toString(numFilesToComplete[i+1]) + " files, and it only reported completing " + toString(tempNum) + ". This will cause file mismatches. The flow files may be too large to process with multiple processors. \n"); + } + } + in.close(); m->mothurRemove(tempFile); + if (compositeFASTAFileName != "") { m->appendFiles((compositeFASTAFileName + toString(processIDS[i]) + ".temp"), compositeFASTAFileName); m->appendFiles((compositeNamesFileName + toString(processIDS[i]) + ".temp"), compositeNamesFileName); @@ -2017,176 +2094,271 @@ int ShhherCommand::createProcesses(vector filenames){ } /**************************************************************************************************/ -int ShhherCommand::driver(vector filenames, string thisCompositeFASTAFileName, string thisCompositeNamesFileName, int start, int end){ +vector ShhherCommand::parseFlowFiles(string filename){ try { + vector files; + int count = 0; - for(int i=start;icontrol_pressed) { break; } - - string flowFileName = filenames[i]; - - m->mothurOut("\n>>>>>\tProcessing " + flowFileName + " (file " + toString(i+1) + " of " + toString(filenames.size()) + ")\t<<<<<\n"); - m->mothurOut("Reading flowgrams...\n"); - - vector seqNameVector; - vector lengths; - vector flowDataIntI; - vector flowDataPrI; - map nameMap; - vector uniqueFlowgrams; - vector uniqueCount; - vector mapSeqToUnique; - vector mapUniqueToSeq; - vector uniqueLengths; - int numFlowCells; - - int numSeqs = getFlowData(flowFileName, seqNameVector, lengths, flowDataIntI, nameMap, numFlowCells); - - if (m->control_pressed) { break; } - - m->mothurOut("Identifying unique flowgrams...\n"); - int numUniques = getUniques(numSeqs, numFlowCells, uniqueFlowgrams, uniqueCount, uniqueLengths, mapSeqToUnique, mapUniqueToSeq, lengths, flowDataPrI, flowDataIntI); - - if (m->control_pressed) { break; } - - m->mothurOut("Calculating distances between flowgrams...\n"); - string distFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.dist"; - unsigned long long begTime = time(NULL); - double begClock = clock(); + ifstream in; + m->openInputFile(filename, in); - flowDistParentFork(numFlowCells, distFileName, numUniques, mapUniqueToSeq, mapSeqToUnique, lengths, flowDataPrI, flowDataIntI); - - m->mothurOutEndLine(); - m->mothurOut("Total time: " + toString(time(NULL) - begTime) + '\t' + toString((clock() - begClock)/CLOCKS_PER_SEC) + '\n'); - + int thisNumFLows = 0; + in >> thisNumFLows; m->gobble(in); + + while (!in.eof()) { + if (m->control_pressed) { break; } - string namesFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.names"; - createNamesFile(numSeqs, numUniques, namesFileName, seqNameVector, mapSeqToUnique, mapUniqueToSeq); - - if (m->control_pressed) { break; } - - m->mothurOut("\nClustering flowgrams...\n"); - string listFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.list"; - cluster(listFileName, distFileName, namesFileName); - - if (m->control_pressed) { break; } + ofstream out; + string outputFileName = filename + toString(count) + ".temp"; + m->openOutputFile(outputFileName, out); + out << thisNumFLows << endl; + files.push_back(outputFileName); + + int numLinesWrote = 0; + for (int i = 0; i < largeSize; i++) { + if (in.eof()) { break; } + string line = m->getline(in); m->gobble(in); + out << line << endl; + numLinesWrote++; + } + out.close(); - vector otuData; - vector cumNumSeqs; - vector nSeqsPerOTU; - vector > aaP; //tMaster->aanP: each row is a different otu / each col contains the sequence indices - vector > aaI; //tMaster->aanI: that are in each otu - can't differentiate between aaP and aaI - vector seqNumber; //tMaster->anP: the sequence id number sorted by OTU - vector seqIndex; //tMaster->anI; the index that corresponds to seqNumber + if (numLinesWrote == 0) { m->mothurRemove(outputFileName); files.pop_back(); } + count++; + } + in.close(); + + if (m->control_pressed) { for (int i = 0; i < files.size(); i++) { m->mothurRemove(files[i]); } files.clear(); } + + m->mothurOut("\nDivided " + filename + " into " + toString(files.size()) + " files.\n\n"); + + return files; + } + catch(exception& e) { + m->errorOut(e, "ShhherCommand", "parseFlowFiles"); + exit(1); + } +} +/**************************************************************************************************/ - - int numOTUs = getOTUData(numSeqs, listFileName, otuData, cumNumSeqs, nSeqsPerOTU, aaP, aaI, seqNumber, seqIndex, nameMap); +int ShhherCommand::driver(vector filenames, string thisCompositeFASTAFileName, string thisCompositeNamesFileName, int start, int end){ + try { + + int numCompleted = 0; + + for(int i=start;icontrol_pressed) { break; } - m->mothurRemove(distFileName); - m->mothurRemove(namesFileName); - m->mothurRemove(listFileName); - - vector dist; //adDist - distance of sequences to centroids - vector change; //did the centroid sequence change? 0 = no; 1 = yes - vector centroids; //the representative flowgram for each cluster m - vector weight; - vector singleTau; //tMaster->adTau: 1-D Tau vector (1xnumSeqs) - vector nSeqsBreaks; - vector nOTUsBreaks; - - dist.assign(numSeqs * numOTUs, 0); - change.assign(numOTUs, 1); - centroids.assign(numOTUs, -1); - weight.assign(numOTUs, 0); - singleTau.assign(numSeqs, 1.0); + vector theseFlowFileNames; theseFlowFileNames.push_back(filenames[i]); + if (large) { theseFlowFileNames = parseFlowFiles(filenames[i]); } - nSeqsBreaks.assign(2, 0); - nOTUsBreaks.assign(2, 0); + if (m->control_pressed) { break; } - nSeqsBreaks[0] = 0; - nSeqsBreaks[1] = numSeqs; - nOTUsBreaks[1] = numOTUs; - - if (m->control_pressed) { break; } - - double maxDelta = 0; - int iter = 0; - - begClock = clock(); - begTime = time(NULL); + double begClock = clock(); + unsigned long long begTime; - m->mothurOut("\nDenoising flowgrams...\n"); - m->mothurOut("iter\tmaxDelta\tnLL\t\tcycletime\n"); - - while((maxIters == 0 && maxDelta > minDelta) || iter < MIN_ITER || (maxDelta > minDelta && iter < maxIters)){ - - if (m->control_pressed) { break; } - - double cycClock = clock(); - unsigned long long cycTime = time(NULL); - fill(numOTUs, seqNumber, seqIndex, cumNumSeqs, nSeqsPerOTU, aaP, aaI); - - if (m->control_pressed) { break; } + for (int g = 0; g < theseFlowFileNames.size(); g++) { - calcCentroidsDriver(numOTUs, cumNumSeqs, nSeqsPerOTU, seqIndex, change, centroids, singleTau, mapSeqToUnique, uniqueFlowgrams, flowDataIntI, lengths, numFlowCells, seqNumber); - - if (m->control_pressed) { break; } + string flowFileName = theseFlowFileNames[g]; + m->mothurOut("\n>>>>>\tProcessing " + flowFileName + " (file " + toString(i+1) + " of " + toString(filenames.size()) + ")\t<<<<<\n"); + m->mothurOut("Reading flowgrams...\n"); + + vector seqNameVector; + vector lengths; + vector flowDataIntI; + vector flowDataPrI; + map nameMap; + vector uniqueFlowgrams; + vector uniqueCount; + vector mapSeqToUnique; + vector mapUniqueToSeq; + vector uniqueLengths; + int numFlowCells; - maxDelta = getNewWeights(numOTUs, cumNumSeqs, nSeqsPerOTU, singleTau, seqNumber, weight); + if (m->debug) { m->mothurOut("[DEBUG]: About to read flowgrams.\n"); } + int numSeqs = getFlowData(flowFileName, seqNameVector, lengths, flowDataIntI, nameMap, numFlowCells); if (m->control_pressed) { break; } - double nLL = getLikelihood(numSeqs, numOTUs, nSeqsPerOTU, seqNumber, cumNumSeqs, seqIndex, dist, weight); + m->mothurOut("Identifying unique flowgrams...\n"); + int numUniques = getUniques(numSeqs, numFlowCells, uniqueFlowgrams, uniqueCount, uniqueLengths, mapSeqToUnique, mapUniqueToSeq, lengths, flowDataPrI, flowDataIntI); if (m->control_pressed) { break; } - checkCentroids(numOTUs, centroids, weight); - - if (m->control_pressed) { break; } - - calcNewDistances(numSeqs, numOTUs, nSeqsPerOTU, dist, weight, change, centroids, aaP, singleTau, aaI, seqNumber, seqIndex, uniqueFlowgrams, flowDataIntI, numFlowCells, lengths); - - if (m->control_pressed) { break; } - - iter++; - - m->mothurOut(toString(iter) + '\t' + toString(maxDelta) + '\t' + toString(nLL) + '\t' + toString(time(NULL) - cycTime) + '\t' + toString((clock() - cycClock)/(double)CLOCKS_PER_SEC) + '\n'); + m->mothurOut("Calculating distances between flowgrams...\n"); + string distFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.dist"; + begTime = time(NULL); + - } - - if (m->control_pressed) { break; } - - m->mothurOut("\nFinalizing...\n"); - fill(numOTUs, seqNumber, seqIndex, cumNumSeqs, nSeqsPerOTU, aaP, aaI); - - if (m->control_pressed) { break; } - - setOTUs(numOTUs, numSeqs, seqNumber, seqIndex, cumNumSeqs, nSeqsPerOTU, otuData, singleTau, dist, aaP, aaI); - - if (m->control_pressed) { break; } - - vector otuCounts(numOTUs, 0); - for(int i=0;icontrol_pressed) { break; } + flowDistParentFork(numFlowCells, distFileName, numUniques, mapUniqueToSeq, mapSeqToUnique, lengths, flowDataPrI, flowDataIntI); + + m->mothurOutEndLine(); + m->mothurOut("Total time: " + toString(time(NULL) - begTime) + '\t' + toString((clock() - begClock)/CLOCKS_PER_SEC) + '\n'); + + + string namesFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.names"; + createNamesFile(numSeqs, numUniques, namesFileName, seqNameVector, mapSeqToUnique, mapUniqueToSeq); + + if (m->control_pressed) { break; } + + m->mothurOut("\nClustering flowgrams...\n"); + string listFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.list"; + cluster(listFileName, distFileName, namesFileName); + + if (m->control_pressed) { break; } + + vector otuData; + vector cumNumSeqs; + vector nSeqsPerOTU; + vector > aaP; //tMaster->aanP: each row is a different otu / each col contains the sequence indices + vector > aaI; //tMaster->aanI: that are in each otu - can't differentiate between aaP and aaI + vector seqNumber; //tMaster->anP: the sequence id number sorted by OTU + vector seqIndex; //tMaster->anI; the index that corresponds to seqNumber + + + int numOTUs = getOTUData(numSeqs, listFileName, otuData, cumNumSeqs, nSeqsPerOTU, aaP, aaI, seqNumber, seqIndex, nameMap); + + if (m->control_pressed) { break; } + + m->mothurRemove(distFileName); + m->mothurRemove(namesFileName); + m->mothurRemove(listFileName); + + vector dist; //adDist - distance of sequences to centroids + vector change; //did the centroid sequence change? 0 = no; 1 = yes + vector centroids; //the representative flowgram for each cluster m + vector weight; + vector singleTau; //tMaster->adTau: 1-D Tau vector (1xnumSeqs) + vector nSeqsBreaks; + vector nOTUsBreaks; + + if (m->debug) { m->mothurOut("[DEBUG]: numSeqs = " + toString(numSeqs) + " numOTUS = " + toString(numOTUs) + " about to alloc a dist vector with size = " + toString((numSeqs * numOTUs)) + ".\n"); } + + dist.assign(numSeqs * numOTUs, 0); + change.assign(numOTUs, 1); + centroids.assign(numOTUs, -1); + weight.assign(numOTUs, 0); + singleTau.assign(numSeqs, 1.0); + + nSeqsBreaks.assign(2, 0); + nOTUsBreaks.assign(2, 0); + + nSeqsBreaks[0] = 0; + nSeqsBreaks[1] = numSeqs; + nOTUsBreaks[1] = numOTUs; + + if (m->debug) { m->mothurOut("[DEBUG]: done allocating memory, about to denoise.\n"); } + + if (m->control_pressed) { break; } + + double maxDelta = 0; + int iter = 0; + + begClock = clock(); + begTime = time(NULL); + + m->mothurOut("\nDenoising flowgrams...\n"); + m->mothurOut("iter\tmaxDelta\tnLL\t\tcycletime\n"); + + while((maxIters == 0 && maxDelta > minDelta) || iter < MIN_ITER || (maxDelta > minDelta && iter < maxIters)){ + + if (m->control_pressed) { break; } + + double cycClock = clock(); + unsigned long long cycTime = time(NULL); + fill(numOTUs, seqNumber, seqIndex, cumNumSeqs, nSeqsPerOTU, aaP, aaI); + + if (m->control_pressed) { break; } + + calcCentroidsDriver(numOTUs, cumNumSeqs, nSeqsPerOTU, seqIndex, change, centroids, singleTau, mapSeqToUnique, uniqueFlowgrams, flowDataIntI, lengths, numFlowCells, seqNumber); + + if (m->control_pressed) { break; } + + maxDelta = getNewWeights(numOTUs, cumNumSeqs, nSeqsPerOTU, singleTau, seqNumber, weight); + + if (m->control_pressed) { break; } + + double nLL = getLikelihood(numSeqs, numOTUs, nSeqsPerOTU, seqNumber, cumNumSeqs, seqIndex, dist, weight); + + if (m->control_pressed) { break; } + + checkCentroids(numOTUs, centroids, weight); + + if (m->control_pressed) { break; } + + calcNewDistances(numSeqs, numOTUs, nSeqsPerOTU, dist, weight, change, centroids, aaP, singleTau, aaI, seqNumber, seqIndex, uniqueFlowgrams, flowDataIntI, numFlowCells, lengths); + + if (m->control_pressed) { break; } + + iter++; + + m->mothurOut(toString(iter) + '\t' + toString(maxDelta) + '\t' + toString(nLL) + '\t' + toString(time(NULL) - cycTime) + '\t' + toString((clock() - cycClock)/(double)CLOCKS_PER_SEC) + '\n'); + + } + + if (m->control_pressed) { break; } + + m->mothurOut("\nFinalizing...\n"); + fill(numOTUs, seqNumber, seqIndex, cumNumSeqs, nSeqsPerOTU, aaP, aaI); + + if (m->control_pressed) { break; } + + setOTUs(numOTUs, numSeqs, seqNumber, seqIndex, cumNumSeqs, nSeqsPerOTU, otuData, singleTau, dist, aaP, aaI); + + if (m->control_pressed) { break; } + + vector otuCounts(numOTUs, 0); + for(int i=0;icontrol_pressed) { break; } + + if ((large) && (g == 0)) { flowFileName = filenames[i]; theseFlowFileNames[0] = filenames[i]; } + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir = m->hasPath(flowFileName); } + string qualityFileName = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName)) + getOutputFileNameTag("qfile"); + string fastaFileName = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName)) + getOutputFileNameTag("fasta"); + string nameFileName = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName)) + getOutputFileNameTag("name"); + string otuCountsFileName = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName)) + getOutputFileNameTag("counts"); + string fileRoot = m->getRootName(m->getSimpleName(flowFileName)); + int pos = fileRoot.find_first_of('.'); + string fileGroup = fileRoot; + if (pos != string::npos) { fileGroup = fileRoot.substr(pos+1, (fileRoot.length()-1-(pos+1))); } + string groupFileName = thisOutputDir + fileRoot + getOutputFileNameTag("group"); + + + writeQualities(numOTUs, numFlowCells, qualityFileName, otuCounts, nSeqsPerOTU, seqNumber, singleTau, flowDataIntI, uniqueFlowgrams, cumNumSeqs, mapUniqueToSeq, seqNameVector, centroids, aaI); if (m->control_pressed) { break; } + writeSequences(thisCompositeFASTAFileName, numOTUs, numFlowCells, fastaFileName, otuCounts, uniqueFlowgrams, seqNameVector, aaI, centroids);if (m->control_pressed) { break; } + writeNames(thisCompositeNamesFileName, numOTUs, nameFileName, otuCounts, seqNameVector, aaI, nSeqsPerOTU); if (m->control_pressed) { break; } + writeClusters(otuCountsFileName, numOTUs, numFlowCells,otuCounts, centroids, uniqueFlowgrams, seqNameVector, aaI, nSeqsPerOTU, lengths, flowDataIntI); if (m->control_pressed) { break; } + writeGroups(groupFileName, fileGroup, numSeqs, seqNameVector); if (m->control_pressed) { break; } + + if (large) { + if (g > 0) { + m->appendFiles(qualityFileName, (thisOutputDir + m->getRootName(m->getSimpleName(theseFlowFileNames[0])) + getOutputFileNameTag("qfile"))); + m->mothurRemove(qualityFileName); + m->appendFiles(fastaFileName, (thisOutputDir + m->getRootName(m->getSimpleName(theseFlowFileNames[0])) + getOutputFileNameTag("fasta"))); + m->mothurRemove(fastaFileName); + m->appendFiles(nameFileName, (thisOutputDir + m->getRootName(m->getSimpleName(theseFlowFileNames[0])) + getOutputFileNameTag("name"))); + m->mothurRemove(nameFileName); + m->appendFiles(otuCountsFileName, (thisOutputDir + m->getRootName(m->getSimpleName(theseFlowFileNames[0])) + getOutputFileNameTag("counts"))); + m->mothurRemove(otuCountsFileName); + m->appendFiles(groupFileName, (thisOutputDir + m->getRootName(m->getSimpleName(theseFlowFileNames[0])) + getOutputFileNameTag("group"))); + m->mothurRemove(groupFileName); + } + m->mothurRemove(theseFlowFileNames[g]); + } + } - writeQualities(numOTUs, numFlowCells, flowFileName, otuCounts, nSeqsPerOTU, seqNumber, singleTau, flowDataIntI, uniqueFlowgrams, cumNumSeqs, mapUniqueToSeq, seqNameVector, centroids, aaI); if (m->control_pressed) { break; } - writeSequences(thisCompositeFASTAFileName, numOTUs, numFlowCells, flowFileName, otuCounts, uniqueFlowgrams, seqNameVector, aaI, centroids);if (m->control_pressed) { break; } - writeNames(thisCompositeNamesFileName, numOTUs, flowFileName, otuCounts, seqNameVector, aaI, nSeqsPerOTU); if (m->control_pressed) { break; } - writeClusters(flowFileName, numOTUs, numFlowCells,otuCounts, centroids, uniqueFlowgrams, seqNameVector, aaI, nSeqsPerOTU, lengths, flowDataIntI); if (m->control_pressed) { break; } - writeGroups(flowFileName, numSeqs, seqNameVector); if (m->control_pressed) { break; } - + numCompleted++; m->mothurOut("Total time to process " + flowFileName + ":\t" + toString(time(NULL) - begTime) + '\t' + toString((clock() - begClock)/(double)CLOCKS_PER_SEC) + '\n'); } if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; } - return 0; + return numCompleted; }catch(exception& e) { m->errorOut(e, "ShhherCommand", "driver"); @@ -2211,17 +2383,21 @@ int ShhherCommand::getFlowData(string filename, vector& thisSeqNameVecto thisNameMap.clear(); flowFile >> numFlowCells; + if (m->debug) { m->mothurOut("[DEBUG]: numFlowCells = " + toString(numFlowCells) + ".\n"); } int index = 0;//pcluster while(!flowFile.eof()){ if (m->control_pressed) { break; } flowFile >> seqName >> currentNumFlowCells; + thisLengths.push_back(currentNumFlowCells); thisSeqNameVector.push_back(seqName); thisNameMap[seqName] = index++;//pcluster - + + if (m->debug) { m->mothurOut("[DEBUG]: seqName = " + seqName + " length = " + toString(currentNumFlowCells) + " index = " + toString(index) + "\n"); } + for(int i=0;i> intensity; if(intensity > 9.99) { intensity = 9.99; } @@ -2403,8 +2579,7 @@ int ShhherCommand::getUniques(int numSeqs, int numFlowCells, vector& uniq } uniqueFlowDataIntI.resize(numFlowCells * numUniques); uniqueLengths.resize(numUniques); - uniqueFlowgrams.resize(numFlowCells * numUniques); - + flowDataPrI.resize(numSeqs * numFlowCells, 0); for(int i=0;icontrol_pressed) { break; } flowDataPrI[i] = getProbIntensity(flowDataIntI[i]); } @@ -2510,6 +2685,8 @@ int ShhherCommand::getOTUData(int numSeqs, string fileName, vector& otuDat listFile >> label >> numOTUs; + if (m->debug) { m->mothurOut("[DEBUG]: Getting OTU Data...\n"); } + otuData.assign(numSeqs, 0); cumNumSeqs.assign(numOTUs, 0); nSeqsPerOTU.assign(numOTUs, 0); @@ -2524,6 +2701,7 @@ int ShhherCommand::getOTUData(int numSeqs, string fileName, vector& otuDat for(int i=0;icontrol_pressed) { break; } + if (m->debug) { m->mothurOut("[DEBUG]: processing OTU " + toString(i) + ".\n"); } listFile >> singleOTU; @@ -2981,14 +3159,11 @@ void ShhherCommand::setOTUs(int numOTUs, int numSeqs, vector& seqNumber, ve } /**************************************************************************************************/ -void ShhherCommand::writeQualities(int numOTUs, int numFlowCells, string filename, vector otuCounts, vector& nSeqsPerOTU, vector& seqNumber, +void ShhherCommand::writeQualities(int numOTUs, int numFlowCells, string qualityFileName, vector otuCounts, vector& nSeqsPerOTU, vector& seqNumber, vector& singleTau, vector& flowDataIntI, vector& uniqueFlowgrams, vector& cumNumSeqs, vector& mapUniqueToSeq, vector& seqNameVector, vector& centroids, vector >& aaI){ try { - string thisOutputDir = outputDir; - if (outputDir == "") { thisOutputDir += m->hasPath(filename); } - string qualityFileName = thisOutputDir + m->getRootName(m->getSimpleName(filename)) + "shhh.qual"; ofstream qualityFile; m->openOutputFile(qualityFileName, qualityFile); @@ -3092,11 +3267,9 @@ void ShhherCommand::writeQualities(int numOTUs, int numFlowCells, string filenam /**************************************************************************************************/ -void ShhherCommand::writeSequences(string thisCompositeFASTAFileName, int numOTUs, int numFlowCells, string filename, vector otuCounts, vector& uniqueFlowgrams, vector& seqNameVector, vector >& aaI, vector& centroids){ +void ShhherCommand::writeSequences(string thisCompositeFASTAFileName, int numOTUs, int numFlowCells, string fastaFileName, vector otuCounts, vector& uniqueFlowgrams, vector& seqNameVector, vector >& aaI, vector& centroids){ try { - string thisOutputDir = outputDir; - if (outputDir == "") { thisOutputDir += m->hasPath(filename); } - string fastaFileName = thisOutputDir + m->getRootName(m->getSimpleName(filename)) + "shhh.fasta"; + ofstream fastaFile; m->openOutputFile(fastaFileName, fastaFile); @@ -3121,7 +3294,8 @@ void ShhherCommand::writeSequences(string thisCompositeFASTAFileName, int numOTU } } - fastaFile << newSeq.substr(4) << endl; + if (newSeq.length() >= 4) { fastaFile << newSeq.substr(4) << endl; } + else { fastaFile << "NNNN" << endl; } } } fastaFile.close(); @@ -3140,11 +3314,9 @@ void ShhherCommand::writeSequences(string thisCompositeFASTAFileName, int numOTU /**************************************************************************************************/ -void ShhherCommand::writeNames(string thisCompositeNamesFileName, int numOTUs, string filename, vector otuCounts, vector& seqNameVector, vector >& aaI, vector& nSeqsPerOTU){ +void ShhherCommand::writeNames(string thisCompositeNamesFileName, int numOTUs, string nameFileName, vector otuCounts, vector& seqNameVector, vector >& aaI, vector& nSeqsPerOTU){ try { - string thisOutputDir = outputDir; - if (outputDir == "") { thisOutputDir += m->hasPath(filename); } - string nameFileName = thisOutputDir + m->getRootName(m->getSimpleName(filename)) + "shhh.names"; + ofstream nameFile; m->openOutputFile(nameFileName, nameFile); @@ -3178,13 +3350,9 @@ void ShhherCommand::writeNames(string thisCompositeNamesFileName, int numOTUs, s /**************************************************************************************************/ -void ShhherCommand::writeGroups(string filename, int numSeqs, vector& seqNameVector){ +void ShhherCommand::writeGroups(string groupFileName, string fileRoot, int numSeqs, vector& seqNameVector){ try { - string thisOutputDir = outputDir; - if (outputDir == "") { thisOutputDir += m->hasPath(filename); } - string fileRoot = thisOutputDir + m->getRootName(m->getSimpleName(filename)); - string groupFileName = fileRoot + "shhh.groups"; - ofstream groupFile; + ofstream groupFile; m->openOutputFile(groupFileName, groupFile); for(int i=0;i& se /**************************************************************************************************/ -void ShhherCommand::writeClusters(string filename, int numOTUs, int numFlowCells, vector otuCounts, vector& centroids, vector& uniqueFlowgrams, vector& seqNameVector, vector >& aaI, vector& nSeqsPerOTU, vector& lengths, vector& flowDataIntI){ +void ShhherCommand::writeClusters(string otuCountsFileName, int numOTUs, int numFlowCells, vector otuCounts, vector& centroids, vector& uniqueFlowgrams, vector& seqNameVector, vector >& aaI, vector& nSeqsPerOTU, vector& lengths, vector& flowDataIntI){ try { - string thisOutputDir = outputDir; - if (outputDir == "") { thisOutputDir += m->hasPath(filename); } - string otuCountsFileName = thisOutputDir + m->getRootName(m->getSimpleName(filename)) + "shhh.counts"; ofstream otuCountsFile; m->openOutputFile(otuCountsFileName, otuCountsFile); @@ -3246,7 +3411,9 @@ void ShhherCommand::writeClusters(string filename, int numOTUs, int numFlowCells //otuCountsFile << base; } } - otuCountsFile << newSeq.substr(4) << endl; + + if (newSeq.length() >= 4) { otuCountsFile << newSeq.substr(4) << endl; } + else { otuCountsFile << "NNNN" << endl; } } otuCountsFile << endl; }