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",false,true); parameters.push_back(pfasta);
18 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,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); 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::getOutputFileNameTag(string type, string inputName=""){
60 string outputFileName = "";
61 map<string, vector<string> >::iterator it;
63 //is this a type this command creates
64 it = outputTypes.find(type);
65 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
67 if (type == "fasta") { outputFileName = "shhh_seqs.fasta"; }
68 else if (type == "name") { outputFileName = "shhh_seqs.names"; }
69 else if (type == "map") { outputFileName = "shhh_seqs.map"; }
70 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
72 return outputFileName;
75 m->errorOut(e, "ShhhSeqsCommand", "getOutputFileNameTag");
80 //**********************************************************************************************************************
82 ShhhSeqsCommand::ShhhSeqsCommand(){
84 abort = true; calledHelp = true;
86 vector<string> tempOutNames;
87 outputTypes["fasta"] = tempOutNames;
88 outputTypes["name"] = tempOutNames;
89 outputTypes["map"] = tempOutNames;
92 m->errorOut(e, "ShhhSeqsCommand", "ShhhSeqsCommand");
97 //**********************************************************************************************************************
98 ShhhSeqsCommand::ShhhSeqsCommand(string option) {
100 abort = false; calledHelp = false;
102 //allow user to run help
103 if(option == "help") { help(); abort = true; calledHelp = true; }
104 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
107 vector<string> myArray = setParameters();
109 OptionParser parser(option);
110 map<string, string> parameters = parser.getParameters();
112 ValidParameters validParameter;
113 map<string, string>::iterator it;
115 //check to make sure all parameters are valid for command
116 for (map<string, string>::iterator it2 = parameters.begin(); it2 != parameters.end(); it2++) {
117 if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) { abort = true; }
120 //initialize outputTypes
121 vector<string> tempOutNames;
122 outputTypes["fasta"] = tempOutNames;
123 outputTypes["name"] = tempOutNames;
124 outputTypes["map"] = tempOutNames;
126 //if the user changes the input directory command factory will send this info to us in the output parameter
127 string inputDir = validParameter.validFile(parameters, "inputdir", false);
128 if (inputDir == "not found"){ inputDir = ""; }
131 it = parameters.find("fasta");
132 //user has given a template file
133 if(it != parameters.end()){
134 path = m->hasPath(it->second);
135 //if the user has not given a path then, add inputdir. else leave path alone.
136 if (path == "") { parameters["fasta"] = inputDir + it->second; }
139 it = parameters.find("name");
140 //user has given a template file
141 if(it != parameters.end()){
142 path = m->hasPath(it->second);
143 //if the user has not given a path then, add inputdir. else leave path alone.
144 if (path == "") { parameters["name"] = inputDir + it->second; }
147 it = parameters.find("group");
148 //user has given a template file
149 if(it != parameters.end()){
150 path = m->hasPath(it->second);
151 //if the user has not given a path then, add inputdir. else leave path alone.
152 if (path == "") { parameters["group"] = inputDir + it->second; }
156 //check for required parameters
157 fastafile = validParameter.validFile(parameters, "fasta", true);
158 if (fastafile == "not found") {
159 fastafile = m->getFastaFile();
160 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
161 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
163 else if (fastafile == "not open") { abort = true; }
164 else { m->setFastaFile(fastafile); }
166 //if the user changes the output directory command factory will send this info to us in the output parameter
167 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
169 //check for optional parameter and set defaults
170 // ...at some point should added some additional type checking...
171 namefile = validParameter.validFile(parameters, "name", true);
172 if (namefile == "not found") {
173 namefile = m->getNameFile();
174 if (namefile != "") { m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
175 else { m->mothurOut("You have no current namefile and the name parameter is required."); m->mothurOutEndLine(); abort = true; }
177 else if (namefile == "not open") { namefile = ""; abort = true; }
178 else { m->setNameFile(namefile); }
180 groupfile = validParameter.validFile(parameters, "group", true);
181 if (groupfile == "not found") { groupfile = ""; }
182 else if (groupfile == "not open") { abort = true; groupfile = ""; }
183 else { m->setGroupFile(groupfile); }
185 string temp = validParameter.validFile(parameters, "sigma", false); if(temp == "not found"){ temp = "0.01"; }
186 m->mothurConvert(temp, sigma);
189 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
190 m->setProcessors(temp);
191 m->mothurConvert(temp, processors);
193 if (namefile == "") {
194 vector<string> files; files.push_back(fastafile);
195 parser.getNameFile(files);
199 catch(exception& e) {
200 m->errorOut(e, "ShhhSeqsCommand", "ShhhSeqsCommand");
204 //**********************************************************************************************************************
205 int ShhhSeqsCommand::execute() {
208 if (abort == true) { if (calledHelp) { return 0; } return 2; }
210 if (outputDir == "") { outputDir = m->hasPath(fastafile); }//if user entered a file with a path then preserve it
211 string outputFileName = outputDir + m->getRootName(m->getSimpleName(fastafile)) + getOutputFileNameTag("fasta");
212 string nameFileName = outputDir + m->getRootName(m->getSimpleName(fastafile)) + getOutputFileNameTag("name");
213 string mapFileName = outputDir + m->getRootName(m->getSimpleName(fastafile)) + getOutputFileNameTag("map");
215 if (groupfile != "") {
216 //Parse sequences by group
217 SequenceParser parser(groupfile, fastafile, namefile);
218 vector<string> groups = parser.getNamesOfGroups();
220 if (m->control_pressed) { return 0; }
223 ofstream out, out1, out2;
224 m->openOutputFile(outputFileName, out); out.close();
225 m->openOutputFile(nameFileName, out1); out1.close();
226 mapFileName = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "shhh.";
228 vector<string> mapFileNames;
229 if(processors == 1) { mapFileNames = driverGroups(parser, outputFileName, nameFileName, mapFileName, 0, groups.size(), groups); }
230 else { mapFileNames = createProcessesGroups(parser, outputFileName, nameFileName, mapFileName, groups); }
232 if (m->control_pressed) { return 0; }
234 for (int j = 0; j < mapFileNames.size(); j++) { outputNames.push_back(mapFileNames[j]); outputTypes["map"].push_back(mapFileNames[j]); }
236 //deconvolute results by running unique.seqs
237 deconvoluteResults(outputFileName, nameFileName);
239 if (m->control_pressed) { return 0; }
242 vector<string> sequences;
243 vector<string> uniqueNames;
244 vector<string> redundantNames;
248 correctDist* correct = new correctDist(processors);
250 //reads fasta and name file and loads them in order
251 readData(correct, noise, sequences, uniqueNames, redundantNames, seqFreq);
252 if (m->control_pressed) { return 0; }
254 //calc distances for cluster
255 string distFileName = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "shhh.dist";
256 correct->execute(distFileName);
259 if (m->control_pressed) { m->mothurRemove(distFileName); return 0; }
261 driver(noise, sequences, uniqueNames, redundantNames, seqFreq, distFileName, outputFileName, nameFileName, mapFileName);
262 outputNames.push_back(mapFileName); outputTypes["map"].push_back(mapFileName);
265 if (m->control_pressed) { for (int j = 0; j < outputNames.size(); j++) { m->mothurRemove(outputNames[j]); } return 0; }
267 outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
268 outputNames.push_back(nameFileName); outputTypes["name"].push_back(nameFileName);
270 m->mothurOutEndLine();
271 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
272 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
273 m->mothurOutEndLine();
275 //set accnos file as new current accnosfile
277 itTypes = outputTypes.find("fasta");
278 if (itTypes != outputTypes.end()) {
279 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
282 itTypes = outputTypes.find("name");
283 if (itTypes != outputTypes.end()) {
284 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
290 catch(exception& e) {
291 m->errorOut(e, "ShhhSeqsCommand", "execute");
295 //**********************************************************************************************************************
296 int ShhhSeqsCommand::readData(correctDist* correct, seqNoise& noise, vector<string>& seqs, vector<string>& uNames, vector<string>& rNames, vector<int>& freq) {
298 map<string, string> nameMap;
299 map<string, string>::iterator it;
300 m->readNames(namefile, nameMap);
304 m->openInputFile(fastafile, in);
308 if (m->control_pressed) { in.close(); return 0; }
310 Sequence seq(in); m->gobble(in);
312 if (seq.getName() != "") {
313 correct->addSeq(seq.getName(), seq.getAligned());
315 it = nameMap.find(seq.getName());
316 if (it != nameMap.end()) {
317 noise.addSeq(seq.getAligned(), seqs);
318 noise.addRedundantName(it->first, it->second, uNames, rNames, freq);
320 m->mothurOut("[ERROR]: " + seq.getName() + " is in your fasta file and not in your namefile, please correct.");
327 if (error) { m->control_pressed = true; }
331 }catch(exception& e) {
332 m->errorOut(e, "ShhhSeqsCommand", "readData");
336 //**********************************************************************************************************************
337 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) {
340 map<string, string>::iterator it;
342 for (int i = 0; i < sequences.size(); i++) {
344 if (m->control_pressed) { return 0; }
346 if (sequences[i].getName() != "") {
347 correct->addSeq(sequences[i].getName(), sequences[i].getAligned());
349 it = nameMap.find(sequences[i].getName());
350 if (it != nameMap.end()) {
351 noise.addSeq(sequences[i].getAligned(), seqs);
352 noise.addRedundantName(it->first, it->second, uNames, rNames, freq);
354 m->mothurOut("[ERROR]: " + sequences[i].getName() + " is in your fasta file and not in your namefile, please correct.");
360 if (error) { m->control_pressed = true; }
364 }catch(exception& e) {
365 m->errorOut(e, "ShhhSeqsCommand", "loadData");
369 /**************************************************************************************************/
370 vector<string> ShhhSeqsCommand::createProcessesGroups(SequenceParser& parser, string newFName, string newNName, string newMName, vector<string> groups) {
373 vector<int> processIDS;
375 vector<string> mapfileNames;
378 if (groups.size() < processors) { processors = groups.size(); }
380 //divide the groups between the processors
381 vector<linePair> lines;
382 int numGroupsPerProcessor = groups.size() / processors;
383 for (int i = 0; i < processors; i++) {
384 int startIndex = i * numGroupsPerProcessor;
385 int endIndex = (i+1) * numGroupsPerProcessor;
386 if(i == (processors - 1)){ endIndex = groups.size(); }
387 lines.push_back(linePair(startIndex, endIndex));
390 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
392 //loop through and create all the processes you want
393 while (process != processors) {
397 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
400 mapfileNames = driverGroups(parser, newFName + toString(getpid()) + ".temp", newNName + toString(getpid()) + ".temp", newMName, lines[process].start, lines[process].end, groups);
402 //pass filenames to parent
404 string tempFile = newMName + toString(getpid()) + ".temp";
405 m->openOutputFile(tempFile, out);
406 out << mapfileNames.size() << endl;
407 for (int i = 0; i < mapfileNames.size(); i++) {
408 out << mapfileNames[i] << endl;
414 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
415 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
421 mapfileNames = driverGroups(parser, newFName, newNName, newMName, lines[0].start, lines[0].end, groups);
423 //force parent to wait until all the processes are done
424 for (int i=0;i<processIDS.size();i++) {
425 int temp = processIDS[i];
429 //append output files
430 for(int i=0;i<processIDS.size();i++){
432 string tempFile = newMName + toString(processIDS[i]) + ".temp";
433 m->openInputFile(tempFile, in);
435 int tempNum = 0; in >> tempNum; m->gobble(in);
436 for (int j = 0; j < tempNum; j++) {
438 in >> filename; m->gobble(in);
439 mapfileNames.push_back(filename);
442 in.close(); m->mothurRemove(tempFile);
447 //////////////////////////////////////////////////////////////////////////////////////////////////////
448 //Windows version shared memory, so be careful when passing variables through the shhhseqsData struct.
449 //Above fork() will clone, so memory is separate, but that's not the case with windows,
450 //////////////////////////////////////////////////////////////////////////////////////////////////////
452 vector<shhhseqsData*> pDataArray;
453 DWORD dwThreadIdArray[processors-1];
454 HANDLE hThreadArray[processors-1];
456 //Create processor worker threads.
457 for( int i=1; i<processors; i++ ){
458 // Allocate memory for thread data.
459 string extension = toString(i) + ".temp";
461 shhhseqsData* tempShhhseqs = new shhhseqsData(fastafile, namefile, groupfile, (newFName+extension), (newNName+extension), newMName, groups, m, lines[i].start, lines[i].end, sigma, i);
462 pDataArray.push_back(tempShhhseqs);
463 processIDS.push_back(i);
465 //MySeqSumThreadFunction is in header. It must be global or static to work with the threads.
466 //default security attributes, thread function name, argument to thread function, use default creation flags, returns the thread identifier
467 hThreadArray[i-1] = CreateThread(NULL, 0, MyShhhSeqsThreadFunction, pDataArray[i-1], 0, &dwThreadIdArray[i-1]);
471 //using the main process as a worker saves time and memory
472 mapfileNames = driverGroups(parser, newFName, newNName, newMName, lines[0].start, lines[0].end, groups);
474 //Wait until all threads have terminated.
475 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
477 //Close all thread handles and free memory allocations.
478 for(int i=0; i < pDataArray.size(); i++){
479 for (int j = 0; j < pDataArray[i]->mapfileNames.size(); j++) {
480 mapfileNames.push_back(pDataArray[i]->mapfileNames[j]);
482 CloseHandle(hThreadArray[i]);
483 delete pDataArray[i];
488 //append output files
489 for(int i=0;i<processIDS.size();i++){
490 m->appendFiles((newFName + toString(processIDS[i]) + ".temp"), newFName);
491 m->mothurRemove((newFName + toString(processIDS[i]) + ".temp"));
493 m->appendFiles((newNName + toString(processIDS[i]) + ".temp"), newNName);
494 m->mothurRemove((newNName + toString(processIDS[i]) + ".temp"));
500 catch(exception& e) {
501 m->errorOut(e, "ShhhSeqsCommand", "createProcessesGroups");
505 /**************************************************************************************************/
506 vector<string> ShhhSeqsCommand::driverGroups(SequenceParser& parser, string newFFile, string newNFile, string newMFile, int start, int end, vector<string> groups){
509 vector<string> mapFileNames;
511 for (int i = start; i < end; i++) {
515 if (m->control_pressed) { return mapFileNames; }
517 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[i] + ":"); m->mothurOutEndLine();
519 map<string, string> thisNameMap;
520 thisNameMap = parser.getNameMap(groups[i]);
521 vector<Sequence> thisSeqs = parser.getSeqs(groups[i]);
523 vector<string> sequences;
524 vector<string> uniqueNames;
525 vector<string> redundantNames;
529 correctDist* correct = new correctDist(1); //we use one processor since we already split up the work load.
531 //load this groups info in order
532 loadData(correct, noise, sequences, uniqueNames, redundantNames, seqFreq, thisNameMap, thisSeqs);
533 if (m->control_pressed) { return mapFileNames; }
535 //calc distances for cluster
536 string distFileName = outputDir + m->getRootName(m->getSimpleName(fastafile)) + groups[i] + ".shhh.dist";
537 correct->execute(distFileName);
540 if (m->control_pressed) { m->mothurRemove(distFileName); return mapFileNames; }
542 driver(noise, sequences, uniqueNames, redundantNames, seqFreq, distFileName, newFFile+groups[i], newNFile+groups[i], newMFile+groups[i]+".map");
544 if (m->control_pressed) { return mapFileNames; }
546 m->appendFiles(newFFile+groups[i], newFFile); m->mothurRemove(newFFile+groups[i]);
547 m->appendFiles(newNFile+groups[i], newNFile); m->mothurRemove(newNFile+groups[i]);
548 mapFileNames.push_back(newMFile+groups[i]+".map");
550 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to process group " + groups[i] + "."); m->mothurOutEndLine();
555 catch(exception& e) {
556 m->errorOut(e, "ShhhSeqsCommand", "driverGroups");
560 //**********************************************************************************************************************
561 int ShhhSeqsCommand::driver(seqNoise& noise,
562 vector<string>& sequences,
563 vector<string>& uniqueNames,
564 vector<string>& redundantNames,
565 vector<int>& seqFreq,
566 string distFileName, string outputFileName, string nameFileName, string mapFileName) {
568 double cutOff = 0.08;
571 double minDelta = 1e-6;
573 double maxDelta = 1e6;
574 int numSeqs = sequences.size();
576 //run cluster command
577 string inputString = "phylip=" + distFileName + ", method=furthest, cutoff=0.08";
578 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
579 m->mothurOut("Running command: cluster(" + inputString + ")"); m->mothurOutEndLine();
581 Command* clusterCommand = new ClusterCommand(inputString);
582 clusterCommand->execute();
584 map<string, vector<string> > filenames = clusterCommand->getOutputFiles();
585 string listFileName = filenames["list"][0];
586 string rabundFileName = filenames["rabund"][0]; m->mothurRemove(rabundFileName);
587 string sabundFileName = filenames["sabund"][0]; m->mothurRemove(sabundFileName);
589 delete clusterCommand;
590 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
592 if (m->control_pressed) { m->mothurRemove(distFileName); m->mothurRemove(listFileName); return 0; }
594 vector<double> distances(numSeqs * numSeqs);
595 noise.getDistanceData(distFileName, distances);
596 m->mothurRemove(distFileName);
597 if (m->control_pressed) { m->mothurRemove(listFileName); return 0; }
599 vector<int> otuData(numSeqs);
601 vector<vector<int> > otuBySeqLookUp;
602 noise.getListData(listFileName, cutOff, otuData, otuFreq, otuBySeqLookUp);
603 m->mothurRemove(listFileName);
604 if (m->control_pressed) { return 0; }
606 int numOTUs = otuFreq.size();
608 vector<double> weights(numOTUs, 0);
609 vector<int> change(numOTUs, 1);
610 vector<int> centroids(numOTUs, -1);
611 vector<int> cumCount(numOTUs, 0);
613 vector<double> tau(numSeqs, 1);
614 vector<int> anP(numSeqs, 0);
615 vector<int> anI(numSeqs, 0);
616 vector<int> anN(numSeqs, 0);
617 vector<vector<int> > aanI = otuBySeqLookUp;
619 while(numIters < minIter || ((maxDelta > minDelta) && (numIters < maxIter))){
621 if (m->control_pressed) { return 0; }
623 noise.updateOTUCountData(otuFreq, otuBySeqLookUp, aanI, anP, anI, cumCount); if (m->control_pressed) { return 0; }
624 maxDelta = noise.calcNewWeights(weights, seqFreq, anI, cumCount, anP, otuFreq, tau); if (m->control_pressed) { return 0; }
626 noise.calcCentroids(anI, anP, change, centroids, cumCount, distances, seqFreq, otuFreq, tau); if (m->control_pressed) { return 0; }
627 noise.checkCentroids(weights, centroids); if (m->control_pressed) { return 0; }
629 otuFreq.assign(numOTUs, 0);
633 for(int i=0;i<numSeqs;i++){
634 if (m->control_pressed) { return 0; }
637 double norm = 0.0000;
638 double minWeight = 0.1;
639 vector<double> currentTau(numOTUs);
641 for(int j=0;j<numOTUs;j++){
642 if (m->control_pressed) { return 0; }
643 if(weights[j] > minWeight && distances[i * numSeqs+centroids[j]] < offset){
644 offset = distances[i * numSeqs+centroids[j]];
648 for(int j=0;j<numOTUs;j++){
649 if (m->control_pressed) { return 0; }
650 if(weights[j] > minWeight){
651 currentTau[j] = exp(sigma * (-distances[(i * numSeqs + centroids[j])] + offset)) * weights[j];
652 norm += currentTau[j];
655 currentTau[j] = 0.0000;
659 for(int j=0;j<numOTUs;j++){
660 if (m->control_pressed) { return 0; }
661 currentTau[j] /= norm;
664 for(int j=0;j<numOTUs;j++){
665 if (m->control_pressed) { return 0; }
667 if(currentTau[j] > 1.0e-4){
668 int oldTotal = total;
671 tau.resize(oldTotal+1);
672 tau[oldTotal] = currentTau[j];
673 otuBySeqLookUp[j][otuFreq[j]] = oldTotal;
674 aanI[j][otuFreq[j]] = i;
687 noise.updateOTUCountData(otuFreq, otuBySeqLookUp, aanI, anP, anI, cumCount); if (m->control_pressed) { return 0; }
689 vector<double> percentage(numSeqs);
690 noise.setUpOTUData(otuData, percentage, cumCount, tau, otuFreq, anP, anI); if (m->control_pressed) { return 0; }
691 noise.finishOTUData(otuData, otuFreq, anP, anI, cumCount, otuBySeqLookUp, aanI, tau); if (m->control_pressed) { return 0; }
693 change.assign(numOTUs, 1);
694 noise.calcCentroids(anI, anP, change, centroids, cumCount, distances, seqFreq, otuFreq, tau); if (m->control_pressed) { return 0; }
697 vector<int> finalTau(numOTUs, 0);
698 for(int i=0;i<numSeqs;i++){
699 if (m->control_pressed) { return 0; }
700 finalTau[otuData[i]] += int(seqFreq[i]);
703 noise.writeOutput(outputFileName, nameFileName, mapFileName, finalTau, centroids, otuData, sequences, uniqueNames, redundantNames, seqFreq, distances);
707 }catch(exception& e) {
708 m->errorOut(e, "ShhhSeqsCommand", "driver");
712 //**********************************************************************************************************************
713 int ShhhSeqsCommand::deconvoluteResults(string fastaFile, string nameFile){
715 m->mothurOutEndLine(); m->mothurOut("Deconvoluting results:"); m->mothurOutEndLine(); m->mothurOutEndLine();
717 //use unique.seqs to create new name and fastafile
718 string inputString = "fasta=" + fastaFile + ", name=" + nameFile;
719 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
720 m->mothurOut("Running command: unique.seqs(" + inputString + ")"); m->mothurOutEndLine();
721 m->mothurCalling = true;
723 Command* uniqueCommand = new DeconvoluteCommand(inputString);
724 uniqueCommand->execute();
726 map<string, vector<string> > filenames = uniqueCommand->getOutputFiles();
728 delete uniqueCommand;
729 m->mothurCalling = false;
730 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
732 string newnameFile = filenames["name"][0];
733 string newfastaFile = filenames["fasta"][0];
735 m->mothurRemove(fastaFile); rename(newfastaFile.c_str(), fastaFile.c_str());
736 if (nameFile != newnameFile) { m->mothurRemove(nameFile); rename(newnameFile.c_str(), nameFile.c_str()); }
740 catch(exception& e) {
741 m->errorOut(e, "ShhhSeqsCommand", "deconvoluteResults");
745 //**********************************************************************************************************************