5 * Created by westcott on 11/8/11.
6 * Copyright 2011 Schloss Lab. All rights reserved.
10 #include "shhhseqscommand.h"
14 //**********************************************************************************************************************
15 vector<string> ShhhSeqsCommand::setParameters(){
17 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none","fasta-map",false,true,true); parameters.push_back(pfasta);
18 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none","name",false,true,true); parameters.push_back(pname);
19 CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none","",false,false); parameters.push_back(pgroup);
20 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "","",false,false,true); parameters.push_back(pprocessors);
21 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
22 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
23 CommandParameter psigma("sigma", "Number", "", "0.01", "", "", "","",false,false); parameters.push_back(psigma);
25 vector<string> myArray;
26 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
30 m->errorOut(e, "ShhhSeqsCommand", "setParameters");
34 //**********************************************************************************************************************
35 string ShhhSeqsCommand::getHelpString(){
37 string helpString = "";
38 helpString += "The shhh.seqs command reads a fasta and name file and ....\n";
39 helpString += "The shhh.seqs command parameters are fasta, name, group, sigma and processors.\n";
40 helpString += "The fasta parameter allows you to enter the fasta file containing your sequences, and is required, unless you have a valid current fasta file. \n";
41 helpString += "The name parameter allows you to provide a name file associated with your fasta file. It is required. \n";
42 helpString += "The group parameter allows you to provide a group file. When checking sequences, only sequences from the same group as the query sequence will be used as the reference. \n";
43 helpString += "The processors parameter allows you to specify how many processors you would like to use. The default is 1. \n";
44 helpString += "The sigma parameter .... The default is 0.01. \n";
45 helpString += "The shhh.seqs command should be in the following format: \n";
46 helpString += "shhh.seqs(fasta=yourFastaFile, name=yourNameFile) \n";
47 helpString += "Example: shhh.seqs(fasta=AD.align, name=AD.names) \n";
48 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n";
53 m->errorOut(e, "ShhhSeqsCommand", "getHelpString");
57 //**********************************************************************************************************************
58 string ShhhSeqsCommand::getOutputPattern(string type) {
62 if (type == "fasta") { pattern = "[filename],shhh_seqs.fasta"; }
63 else if (type == "name") { pattern = "[filename],shhh_seqs.names"; }
64 else if (type == "map") { pattern = "[filename],shhh_seqs.map"; }
65 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
70 m->errorOut(e, "ShhhSeqsCommand", "getOutputPattern");
75 //**********************************************************************************************************************
77 ShhhSeqsCommand::ShhhSeqsCommand(){
79 abort = true; calledHelp = true;
81 vector<string> tempOutNames;
82 outputTypes["fasta"] = tempOutNames;
83 outputTypes["name"] = tempOutNames;
84 outputTypes["map"] = tempOutNames;
87 m->errorOut(e, "ShhhSeqsCommand", "ShhhSeqsCommand");
92 //**********************************************************************************************************************
93 ShhhSeqsCommand::ShhhSeqsCommand(string option) {
95 abort = false; calledHelp = false;
97 //allow user to run help
98 if(option == "help") { help(); abort = true; calledHelp = true; }
99 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
102 vector<string> myArray = setParameters();
104 OptionParser parser(option);
105 map<string, string> parameters = parser.getParameters();
107 ValidParameters validParameter;
108 map<string, string>::iterator it;
110 //check to make sure all parameters are valid for command
111 for (map<string, string>::iterator it2 = parameters.begin(); it2 != parameters.end(); it2++) {
112 if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) { abort = true; }
115 //initialize outputTypes
116 vector<string> tempOutNames;
117 outputTypes["fasta"] = tempOutNames;
118 outputTypes["name"] = tempOutNames;
119 outputTypes["map"] = tempOutNames;
121 //if the user changes the input directory command factory will send this info to us in the output parameter
122 string inputDir = validParameter.validFile(parameters, "inputdir", false);
123 if (inputDir == "not found"){ inputDir = ""; }
126 it = parameters.find("fasta");
127 //user has given a template file
128 if(it != parameters.end()){
129 path = m->hasPath(it->second);
130 //if the user has not given a path then, add inputdir. else leave path alone.
131 if (path == "") { parameters["fasta"] = inputDir + it->second; }
134 it = parameters.find("name");
135 //user has given a template file
136 if(it != parameters.end()){
137 path = m->hasPath(it->second);
138 //if the user has not given a path then, add inputdir. else leave path alone.
139 if (path == "") { parameters["name"] = inputDir + it->second; }
142 it = parameters.find("group");
143 //user has given a template file
144 if(it != parameters.end()){
145 path = m->hasPath(it->second);
146 //if the user has not given a path then, add inputdir. else leave path alone.
147 if (path == "") { parameters["group"] = inputDir + it->second; }
151 //check for required parameters
152 fastafile = validParameter.validFile(parameters, "fasta", true);
153 if (fastafile == "not found") {
154 fastafile = m->getFastaFile();
155 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
156 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
158 else if (fastafile == "not open") { abort = true; }
159 else { m->setFastaFile(fastafile); }
161 //if the user changes the output directory command factory will send this info to us in the output parameter
162 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
164 //check for optional parameter and set defaults
165 // ...at some point should added some additional type checking...
166 namefile = validParameter.validFile(parameters, "name", true);
167 if (namefile == "not found") {
168 namefile = m->getNameFile();
169 if (namefile != "") { m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
170 else { m->mothurOut("You have no current namefile and the name parameter is required."); m->mothurOutEndLine(); abort = true; }
172 else if (namefile == "not open") { namefile = ""; abort = true; }
173 else { m->setNameFile(namefile); }
175 groupfile = validParameter.validFile(parameters, "group", true);
176 if (groupfile == "not found") { groupfile = ""; }
177 else if (groupfile == "not open") { abort = true; groupfile = ""; }
178 else { m->setGroupFile(groupfile); }
180 string temp = validParameter.validFile(parameters, "sigma", false); if(temp == "not found"){ temp = "0.01"; }
181 m->mothurConvert(temp, sigma);
184 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
185 m->setProcessors(temp);
186 m->mothurConvert(temp, processors);
188 if (namefile == "") {
189 vector<string> files; files.push_back(fastafile);
190 parser.getNameFile(files);
194 catch(exception& e) {
195 m->errorOut(e, "ShhhSeqsCommand", "ShhhSeqsCommand");
199 //**********************************************************************************************************************
200 int ShhhSeqsCommand::execute() {
203 if (abort == true) { if (calledHelp) { return 0; } return 2; }
205 if (outputDir == "") { outputDir = m->hasPath(fastafile); }//if user entered a file with a path then preserve it
207 map<string, string> variables;
208 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(fastafile));
209 string outputFileName = getOutputFileName("fasta",variables);
210 string nameFileName = getOutputFileName("name",variables);
211 string mapFileName = getOutputFileName("map",variables);
213 if (groupfile != "") {
214 //Parse sequences by group
215 SequenceParser parser(groupfile, fastafile, namefile);
216 vector<string> groups = parser.getNamesOfGroups();
218 if (m->control_pressed) { return 0; }
221 ofstream out, out1, out2;
222 m->openOutputFile(outputFileName, out); out.close();
223 m->openOutputFile(nameFileName, out1); out1.close();
224 mapFileName = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "shhh.";
226 vector<string> mapFileNames;
227 if(processors == 1) { mapFileNames = driverGroups(parser, outputFileName, nameFileName, mapFileName, 0, groups.size(), groups); }
228 else { mapFileNames = createProcessesGroups(parser, outputFileName, nameFileName, mapFileName, groups); }
230 if (m->control_pressed) { return 0; }
232 for (int j = 0; j < mapFileNames.size(); j++) { outputNames.push_back(mapFileNames[j]); outputTypes["map"].push_back(mapFileNames[j]); }
234 //deconvolute results by running unique.seqs
235 deconvoluteResults(outputFileName, nameFileName);
237 if (m->control_pressed) { return 0; }
240 vector<string> sequences;
241 vector<string> uniqueNames;
242 vector<string> redundantNames;
246 correctDist* correct = new correctDist(processors);
248 //reads fasta and name file and loads them in order
249 readData(correct, noise, sequences, uniqueNames, redundantNames, seqFreq);
250 if (m->control_pressed) { return 0; }
252 //calc distances for cluster
253 string distFileName = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "shhh.dist";
254 correct->execute(distFileName);
257 if (m->control_pressed) { m->mothurRemove(distFileName); return 0; }
259 driver(noise, sequences, uniqueNames, redundantNames, seqFreq, distFileName, outputFileName, nameFileName, mapFileName);
260 outputNames.push_back(mapFileName); outputTypes["map"].push_back(mapFileName);
263 if (m->control_pressed) { for (int j = 0; j < outputNames.size(); j++) { m->mothurRemove(outputNames[j]); } return 0; }
265 outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
266 outputNames.push_back(nameFileName); outputTypes["name"].push_back(nameFileName);
268 m->mothurOutEndLine();
269 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
270 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
271 m->mothurOutEndLine();
273 //set accnos file as new current accnosfile
275 itTypes = outputTypes.find("fasta");
276 if (itTypes != outputTypes.end()) {
277 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
280 itTypes = outputTypes.find("name");
281 if (itTypes != outputTypes.end()) {
282 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
288 catch(exception& e) {
289 m->errorOut(e, "ShhhSeqsCommand", "execute");
293 //**********************************************************************************************************************
294 int ShhhSeqsCommand::readData(correctDist* correct, seqNoise& noise, vector<string>& seqs, vector<string>& uNames, vector<string>& rNames, vector<int>& freq) {
296 map<string, string> nameMap;
297 map<string, string>::iterator it;
298 m->readNames(namefile, nameMap);
302 m->openInputFile(fastafile, in);
306 if (m->control_pressed) { in.close(); return 0; }
308 Sequence seq(in); m->gobble(in);
310 if (seq.getName() != "") {
311 correct->addSeq(seq.getName(), seq.getAligned());
313 it = nameMap.find(seq.getName());
314 if (it != nameMap.end()) {
315 noise.addSeq(seq.getAligned(), seqs);
316 noise.addRedundantName(it->first, it->second, uNames, rNames, freq);
318 m->mothurOut("[ERROR]: " + seq.getName() + " is in your fasta file and not in your namefile, please correct.");
325 if (error) { m->control_pressed = true; }
329 }catch(exception& e) {
330 m->errorOut(e, "ShhhSeqsCommand", "readData");
334 //**********************************************************************************************************************
335 int ShhhSeqsCommand::loadData(correctDist* correct, seqNoise& noise, vector<string>& seqs, vector<string>& uNames, vector<string>& rNames, vector<int>& freq, map<string, string>& nameMap, vector<Sequence>& sequences) {
338 map<string, string>::iterator it;
340 for (int i = 0; i < sequences.size(); i++) {
342 if (m->control_pressed) { return 0; }
344 if (sequences[i].getName() != "") {
345 correct->addSeq(sequences[i].getName(), sequences[i].getAligned());
347 it = nameMap.find(sequences[i].getName());
348 if (it != nameMap.end()) {
349 noise.addSeq(sequences[i].getAligned(), seqs);
350 noise.addRedundantName(it->first, it->second, uNames, rNames, freq);
352 m->mothurOut("[ERROR]: " + sequences[i].getName() + " is in your fasta file and not in your namefile, please correct.");
358 if (error) { m->control_pressed = true; }
362 }catch(exception& e) {
363 m->errorOut(e, "ShhhSeqsCommand", "loadData");
367 /**************************************************************************************************/
368 vector<string> ShhhSeqsCommand::createProcessesGroups(SequenceParser& parser, string newFName, string newNName, string newMName, vector<string> groups) {
371 vector<int> processIDS;
373 vector<string> mapfileNames;
376 if (groups.size() < processors) { processors = groups.size(); }
378 //divide the groups between the processors
379 vector<linePair> lines;
380 int remainingPairs = groups.size();
382 for (int remainingProcessors = processors; remainingProcessors > 0; remainingProcessors--) {
383 int numPairs = remainingPairs; //case for last processor
384 if (remainingProcessors != 1) { numPairs = ceil(remainingPairs / remainingProcessors); }
385 lines.push_back(linePair(startIndex, (startIndex+numPairs))); //startIndex, endIndex
386 startIndex = startIndex + numPairs;
387 remainingPairs = remainingPairs - numPairs;
391 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
393 //loop through and create all the processes you want
394 while (process != processors) {
398 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
401 mapfileNames = driverGroups(parser, newFName + m->mothurGetpid(process) + ".temp", newNName + m->mothurGetpid(process) + ".temp", newMName, lines[process].start, lines[process].end, groups);
403 //pass filenames to parent
405 string tempFile = newMName + m->mothurGetpid(process) + ".temp";
406 m->openOutputFile(tempFile, out);
407 out << mapfileNames.size() << endl;
408 for (int i = 0; i < mapfileNames.size(); i++) {
409 out << mapfileNames[i] << endl;
415 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
416 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
422 mapfileNames = driverGroups(parser, newFName, newNName, newMName, lines[0].start, lines[0].end, groups);
424 //force parent to wait until all the processes are done
425 for (int i=0;i<processIDS.size();i++) {
426 int temp = processIDS[i];
430 //append output files
431 for(int i=0;i<processIDS.size();i++){
433 string tempFile = newMName + toString(processIDS[i]) + ".temp";
434 m->openInputFile(tempFile, in);
436 int tempNum = 0; in >> tempNum; m->gobble(in);
437 for (int j = 0; j < tempNum; j++) {
439 in >> filename; m->gobble(in);
440 mapfileNames.push_back(filename);
443 in.close(); m->mothurRemove(tempFile);
448 //////////////////////////////////////////////////////////////////////////////////////////////////////
449 //Windows version shared memory, so be careful when passing variables through the shhhseqsData struct.
450 //Above fork() will clone, so memory is separate, but that's not the case with windows,
451 //////////////////////////////////////////////////////////////////////////////////////////////////////
453 vector<shhhseqsData*> pDataArray;
454 DWORD dwThreadIdArray[processors-1];
455 HANDLE hThreadArray[processors-1];
457 //Create processor worker threads.
458 for( int i=1; i<processors; i++ ){
459 // Allocate memory for thread data.
460 string extension = toString(i) + ".temp";
462 shhhseqsData* tempShhhseqs = new shhhseqsData(fastafile, namefile, groupfile, (newFName+extension), (newNName+extension), newMName, groups, m, lines[i].start, lines[i].end, sigma, i);
463 pDataArray.push_back(tempShhhseqs);
464 processIDS.push_back(i);
466 //MySeqSumThreadFunction is in header. It must be global or static to work with the threads.
467 //default security attributes, thread function name, argument to thread function, use default creation flags, returns the thread identifier
468 hThreadArray[i-1] = CreateThread(NULL, 0, MyShhhSeqsThreadFunction, pDataArray[i-1], 0, &dwThreadIdArray[i-1]);
472 //using the main process as a worker saves time and memory
473 mapfileNames = driverGroups(parser, newFName, newNName, newMName, lines[0].start, lines[0].end, groups);
475 //Wait until all threads have terminated.
476 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
478 //Close all thread handles and free memory allocations.
479 for(int i=0; i < pDataArray.size(); i++){
480 if (pDataArray[i]->count != (pDataArray[i]->end-pDataArray[i]->start)) {
481 m->mothurOut("[ERROR]: process " + toString(i) + " only processed " + toString(pDataArray[i]->count) + " of " + toString(pDataArray[i]->end-pDataArray[i]->start) + " groups assigned to it, quitting. \n"); m->control_pressed = true;
483 for (int j = 0; j < pDataArray[i]->mapfileNames.size(); j++) {
484 mapfileNames.push_back(pDataArray[i]->mapfileNames[j]);
486 CloseHandle(hThreadArray[i]);
487 delete pDataArray[i];
492 //append output files
493 for(int i=0;i<processIDS.size();i++){
494 m->appendFiles((newFName + toString(processIDS[i]) + ".temp"), newFName);
495 m->mothurRemove((newFName + toString(processIDS[i]) + ".temp"));
497 m->appendFiles((newNName + toString(processIDS[i]) + ".temp"), newNName);
498 m->mothurRemove((newNName + toString(processIDS[i]) + ".temp"));
504 catch(exception& e) {
505 m->errorOut(e, "ShhhSeqsCommand", "createProcessesGroups");
509 /**************************************************************************************************/
510 vector<string> ShhhSeqsCommand::driverGroups(SequenceParser& parser, string newFFile, string newNFile, string newMFile, int start, int end, vector<string> groups){
513 vector<string> mapFileNames;
515 for (int i = start; i < end; i++) {
519 if (m->control_pressed) { return mapFileNames; }
521 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[i] + ":"); m->mothurOutEndLine();
523 map<string, string> thisNameMap;
524 thisNameMap = parser.getNameMap(groups[i]);
525 vector<Sequence> thisSeqs = parser.getSeqs(groups[i]);
527 vector<string> sequences;
528 vector<string> uniqueNames;
529 vector<string> redundantNames;
533 correctDist* correct = new correctDist(1); //we use one processor since we already split up the work load.
535 //load this groups info in order
536 loadData(correct, noise, sequences, uniqueNames, redundantNames, seqFreq, thisNameMap, thisSeqs);
537 if (m->control_pressed) { return mapFileNames; }
539 //calc distances for cluster
540 string distFileName = outputDir + m->getRootName(m->getSimpleName(fastafile)) + groups[i] + ".shhh.dist";
541 correct->execute(distFileName);
544 if (m->control_pressed) { m->mothurRemove(distFileName); return mapFileNames; }
546 driver(noise, sequences, uniqueNames, redundantNames, seqFreq, distFileName, newFFile+groups[i], newNFile+groups[i], newMFile+groups[i]+".map");
548 if (m->control_pressed) { return mapFileNames; }
550 m->appendFiles(newFFile+groups[i], newFFile); m->mothurRemove(newFFile+groups[i]);
551 m->appendFiles(newNFile+groups[i], newNFile); m->mothurRemove(newNFile+groups[i]);
552 mapFileNames.push_back(newMFile+groups[i]+".map");
554 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to process group " + groups[i] + "."); m->mothurOutEndLine();
559 catch(exception& e) {
560 m->errorOut(e, "ShhhSeqsCommand", "driverGroups");
564 //**********************************************************************************************************************
565 int ShhhSeqsCommand::driver(seqNoise& noise,
566 vector<string>& sequences,
567 vector<string>& uniqueNames,
568 vector<string>& redundantNames,
569 vector<int>& seqFreq,
570 string distFileName, string outputFileName, string nameFileName, string mapFileName) {
572 double cutOff = 0.08;
575 double minDelta = 1e-6;
577 double maxDelta = 1e6;
578 int numSeqs = sequences.size();
580 //run cluster command
581 string inputString = "phylip=" + distFileName + ", method=furthest, cutoff=0.08";
582 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
583 m->mothurOut("Running command: cluster(" + inputString + ")"); m->mothurOutEndLine();
585 Command* clusterCommand = new ClusterCommand(inputString);
586 clusterCommand->execute();
588 map<string, vector<string> > filenames = clusterCommand->getOutputFiles();
589 string listFileName = filenames["list"][0];
590 string rabundFileName = filenames["rabund"][0]; m->mothurRemove(rabundFileName);
591 string sabundFileName = filenames["sabund"][0]; m->mothurRemove(sabundFileName);
593 delete clusterCommand;
594 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
596 if (m->control_pressed) { m->mothurRemove(distFileName); m->mothurRemove(listFileName); return 0; }
598 vector<double> distances(numSeqs * numSeqs);
599 noise.getDistanceData(distFileName, distances);
600 m->mothurRemove(distFileName);
601 if (m->control_pressed) { m->mothurRemove(listFileName); return 0; }
603 vector<int> otuData(numSeqs);
605 vector<vector<int> > otuBySeqLookUp;
606 noise.getListData(listFileName, cutOff, otuData, otuFreq, otuBySeqLookUp);
607 m->mothurRemove(listFileName);
608 if (m->control_pressed) { return 0; }
610 int numOTUs = otuFreq.size();
612 vector<double> weights(numOTUs, 0);
613 vector<int> change(numOTUs, 1);
614 vector<int> centroids(numOTUs, -1);
615 vector<int> cumCount(numOTUs, 0);
617 vector<double> tau(numSeqs, 1);
618 vector<int> anP(numSeqs, 0);
619 vector<int> anI(numSeqs, 0);
620 vector<int> anN(numSeqs, 0);
621 vector<vector<int> > aanI = otuBySeqLookUp;
623 while(numIters < minIter || ((maxDelta > minDelta) && (numIters < maxIter))){
625 if (m->control_pressed) { return 0; }
627 noise.updateOTUCountData(otuFreq, otuBySeqLookUp, aanI, anP, anI, cumCount); if (m->control_pressed) { return 0; }
628 maxDelta = noise.calcNewWeights(weights, seqFreq, anI, cumCount, anP, otuFreq, tau); if (m->control_pressed) { return 0; }
630 noise.calcCentroids(anI, anP, change, centroids, cumCount, distances, seqFreq, otuFreq, tau); if (m->control_pressed) { return 0; }
631 noise.checkCentroids(weights, centroids); if (m->control_pressed) { return 0; }
633 otuFreq.assign(numOTUs, 0);
637 for(int i=0;i<numSeqs;i++){
638 if (m->control_pressed) { return 0; }
641 double norm = 0.0000;
642 double minWeight = 0.1;
643 vector<double> currentTau(numOTUs);
645 for(int j=0;j<numOTUs;j++){
646 if (m->control_pressed) { return 0; }
647 if(weights[j] > minWeight && distances[i * numSeqs+centroids[j]] < offset){
648 offset = distances[i * numSeqs+centroids[j]];
652 for(int j=0;j<numOTUs;j++){
653 if (m->control_pressed) { return 0; }
654 if(weights[j] > minWeight){
655 currentTau[j] = exp(sigma * (-distances[(i * numSeqs + centroids[j])] + offset)) * weights[j];
656 norm += currentTau[j];
659 currentTau[j] = 0.0000;
663 for(int j=0;j<numOTUs;j++){
664 if (m->control_pressed) { return 0; }
665 currentTau[j] /= norm;
668 for(int j=0;j<numOTUs;j++){
669 if (m->control_pressed) { return 0; }
671 if(currentTau[j] > 1.0e-4){
672 int oldTotal = total;
675 tau.resize(oldTotal+1);
676 tau[oldTotal] = currentTau[j];
677 otuBySeqLookUp[j][otuFreq[j]] = oldTotal;
678 aanI[j][otuFreq[j]] = i;
691 noise.updateOTUCountData(otuFreq, otuBySeqLookUp, aanI, anP, anI, cumCount); if (m->control_pressed) { return 0; }
693 vector<double> percentage(numSeqs);
694 noise.setUpOTUData(otuData, percentage, cumCount, tau, otuFreq, anP, anI); if (m->control_pressed) { return 0; }
695 noise.finishOTUData(otuData, otuFreq, anP, anI, cumCount, otuBySeqLookUp, aanI, tau); if (m->control_pressed) { return 0; }
697 change.assign(numOTUs, 1);
698 noise.calcCentroids(anI, anP, change, centroids, cumCount, distances, seqFreq, otuFreq, tau); if (m->control_pressed) { return 0; }
701 vector<int> finalTau(numOTUs, 0);
702 for(int i=0;i<numSeqs;i++){
703 if (m->control_pressed) { return 0; }
704 finalTau[otuData[i]] += int(seqFreq[i]);
707 noise.writeOutput(outputFileName, nameFileName, mapFileName, finalTau, centroids, otuData, sequences, uniqueNames, redundantNames, seqFreq, distances);
711 }catch(exception& e) {
712 m->errorOut(e, "ShhhSeqsCommand", "driver");
716 //**********************************************************************************************************************
717 int ShhhSeqsCommand::deconvoluteResults(string fastaFile, string nameFile){
719 m->mothurOutEndLine(); m->mothurOut("Deconvoluting results:"); m->mothurOutEndLine(); m->mothurOutEndLine();
721 //use unique.seqs to create new name and fastafile
722 string inputString = "fasta=" + fastaFile + ", name=" + nameFile;
723 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
724 m->mothurOut("Running command: unique.seqs(" + inputString + ")"); m->mothurOutEndLine();
725 m->mothurCalling = true;
727 Command* uniqueCommand = new DeconvoluteCommand(inputString);
728 uniqueCommand->execute();
730 map<string, vector<string> > filenames = uniqueCommand->getOutputFiles();
732 delete uniqueCommand;
733 m->mothurCalling = false;
734 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
736 string newnameFile = filenames["name"][0];
737 string newfastaFile = filenames["fasta"][0];
739 m->mothurRemove(fastaFile); rename(newfastaFile.c_str(), fastaFile.c_str());
740 if (nameFile != newnameFile) { m->mothurRemove(nameFile); rename(newnameFile.c_str(), nameFile.c_str()); }
744 catch(exception& e) {
745 m->errorOut(e, "ShhhSeqsCommand", "deconvoluteResults");
749 //**********************************************************************************************************************