]> git.donarmstrong.com Git - mothur.git/blob - shhhseqscommand.cpp
1.23.0
[mothur.git] / shhhseqscommand.cpp
1 /*
2  *  shhhseqscommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 11/8/11.
6  *  Copyright 2011 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "shhhseqscommand.h"
11
12
13
14 //**********************************************************************************************************************
15 vector<string> ShhhSeqsCommand::setParameters(){        
16         try {
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);
24                 
25                 vector<string> myArray;
26                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
27                 return myArray;
28         }
29         catch(exception& e) {
30                 m->errorOut(e, "ShhhSeqsCommand", "setParameters");
31                 exit(1);
32         }
33 }
34 //**********************************************************************************************************************
35 string ShhhSeqsCommand::getHelpString(){        
36         try {
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";       
49                 return helpString;
50                 
51         }
52         catch(exception& e) {
53                 m->errorOut(e, "ShhhSeqsCommand", "getHelpString");
54                 exit(1);
55         }
56 }
57 //**********************************************************************************************************************
58
59 ShhhSeqsCommand::ShhhSeqsCommand(){     
60         try {
61                 abort = true; calledHelp = true;
62                 setParameters();
63                 vector<string> tempOutNames;
64                 outputTypes["fasta"] = tempOutNames;
65                 outputTypes["name"] = tempOutNames;
66                 outputTypes["map"] = tempOutNames;
67         }
68         catch(exception& e) {
69                 m->errorOut(e, "ShhhSeqsCommand", "ShhhSeqsCommand");
70                 exit(1);
71         }
72 }
73
74 //**********************************************************************************************************************
75 ShhhSeqsCommand::ShhhSeqsCommand(string option) {
76         try {
77                 abort = false; calledHelp = false;   
78                 
79                 //allow user to run help
80                 if(option == "help") { help(); abort = true; calledHelp = true; }
81                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
82                 
83                 else {
84                         vector<string> myArray = setParameters();
85                         
86                         OptionParser parser(option);
87                         map<string, string> parameters = parser.getParameters();
88                         
89                         ValidParameters validParameter;
90                         map<string, string>::iterator it;
91                         
92                         //check to make sure all parameters are valid for command
93                         for (map<string, string>::iterator it2 = parameters.begin(); it2 != parameters.end(); it2++) { 
94                                 if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) {  abort = true;  }
95                         }
96                         
97                         //initialize outputTypes
98                         vector<string> tempOutNames;
99                         outputTypes["fasta"] = tempOutNames;
100                         outputTypes["name"] = tempOutNames;
101                         outputTypes["map"] = tempOutNames;
102                         
103                         //if the user changes the input directory command factory will send this info to us in the output parameter 
104                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
105                         if (inputDir == "not found"){   inputDir = "";          }
106                         else {
107                                 string path;
108                                 it = parameters.find("fasta");
109                                 //user has given a template file
110                                 if(it != parameters.end()){ 
111                                         path = m->hasPath(it->second);
112                                         //if the user has not given a path then, add inputdir. else leave path alone.
113                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
114                                 }
115                                 
116                                 it = parameters.find("name");
117                                 //user has given a template file
118                                 if(it != parameters.end()){ 
119                                         path = m->hasPath(it->second);
120                                         //if the user has not given a path then, add inputdir. else leave path alone.
121                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
122                                 }
123                                 
124                                 it = parameters.find("group");
125                                 //user has given a template file
126                                 if(it != parameters.end()){ 
127                                         path = m->hasPath(it->second);
128                                         //if the user has not given a path then, add inputdir. else leave path alone.
129                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
130                                 }
131                         }
132                         
133                         //check for required parameters
134                         fastafile = validParameter.validFile(parameters, "fasta", true);
135                         if (fastafile == "not found") {                                 
136                                 fastafile = m->getFastaFile(); 
137                                 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
138                                 else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
139                         }
140                         else if (fastafile == "not open") { abort = true; }     
141                         else { m->setFastaFile(fastafile); }
142                         
143                         //if the user changes the output directory command factory will send this info to us in the output parameter 
144                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = ""; }
145                         
146                         //check for optional parameter and set defaults
147                         // ...at some point should added some additional type checking...
148                         namefile = validParameter.validFile(parameters, "name", true);
149                         if (namefile == "not found") {                  
150                                 namefile = m->getNameFile(); 
151                                 if (namefile != "") { m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
152                                 else {  m->mothurOut("You have no current namefile and the name parameter is required."); m->mothurOutEndLine(); abort = true; }
153                         }
154                         else if (namefile == "not open") { namefile =  ""; abort = true; }      
155                         else {  m->setNameFile(namefile); }
156                         
157                         groupfile = validParameter.validFile(parameters, "group", true);
158                         if (groupfile == "not found") { groupfile =  "";   }
159                         else if (groupfile == "not open") { abort = true; groupfile =  ""; }    
160                         else {   m->setGroupFile(groupfile);  }
161                         
162                         string temp     = validParameter.validFile(parameters, "sigma", false);         if(temp == "not found"){        temp = "0.01"; }
163                         m->mothurConvert(temp, sigma); 
164                         
165                         temp = validParameter.validFile(parameters, "processors", false);       if (temp == "not found"){       temp = m->getProcessors();      }
166                         m->setProcessors(temp);
167                         m->mothurConvert(temp, processors);
168                 }
169         }
170         catch(exception& e) {
171                 m->errorOut(e, "ShhhSeqsCommand", "ShhhSeqsCommand");
172                 exit(1);
173         }
174 }
175 //**********************************************************************************************************************
176 int ShhhSeqsCommand::execute() {
177         try {
178                 
179                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
180                 
181                 if (outputDir == "") { outputDir = m->hasPath(fastafile);  }//if user entered a file with a path then preserve it                               
182                 string outputFileName = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "shhh.fasta";
183                 string nameFileName = outputDir + m->getRootName(m->getSimpleName(fastafile))  + "shhh.names";
184                 string mapFileName = outputDir + m->getRootName(m->getSimpleName(fastafile))  + "shhh.map";
185                 
186                 if (groupfile != "") {
187                         //Parse sequences by group
188                         SequenceParser parser(groupfile, fastafile, namefile);
189                         vector<string> groups = parser.getNamesOfGroups();
190                         
191                         if (m->control_pressed) {  return 0; }
192                         
193                         //clears files
194                         ofstream out, out1, out2;
195                         m->openOutputFile(outputFileName, out); out.close(); 
196                         m->openOutputFile(nameFileName, out1); out1.close();
197                         mapFileName = outputDir + m->getRootName(m->getSimpleName(fastafile))  + "shhh.";
198                         
199                         vector<string> mapFileNames;
200                         if(processors == 1)     {       mapFileNames = driverGroups(parser, outputFileName, nameFileName, mapFileName, 0, groups.size(), groups);       }
201                         else                            {       mapFileNames = createProcessesGroups(parser, outputFileName, nameFileName, mapFileName, groups);                        }
202                         
203                         if (m->control_pressed) {    return 0;  }       
204                         
205                         for (int j = 0; j < mapFileNames.size(); j++) { outputNames.push_back(mapFileNames[j]); outputTypes["map"].push_back(mapFileNames[j]); }
206                         
207                         //deconvolute results by running unique.seqs
208                         deconvoluteResults(outputFileName, nameFileName);
209                         
210                         if (m->control_pressed) {   return 0;   }                               
211                         
212                 }else{  
213                         vector<string> sequences;
214                         vector<string> uniqueNames;
215                         vector<string> redundantNames;
216                         vector<int> seqFreq;
217                         
218                         seqNoise noise;
219                         correctDist* correct = new correctDist(processors);
220                         
221                         //reads fasta and name file and loads them in order
222                         readData(correct, noise, sequences, uniqueNames, redundantNames, seqFreq);
223                         if (m->control_pressed) { return 0; }
224                         
225                         //calc distances for cluster
226                         string distFileName = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "shhh.dist";
227                         correct->execute(distFileName);
228                         delete correct;
229                         
230                         if (m->control_pressed) { m->mothurRemove(distFileName); return 0; }
231                         
232                         driver(noise, sequences, uniqueNames, redundantNames, seqFreq, distFileName, outputFileName, nameFileName, mapFileName); 
233                         outputNames.push_back(mapFileName); outputTypes["map"].push_back(mapFileName);
234                 }
235                 
236                 if (m->control_pressed) { for (int j = 0; j < outputNames.size(); j++) {        m->mothurRemove(outputNames[j]);        } return 0; }
237                 
238                 outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
239                 outputNames.push_back(nameFileName); outputTypes["name"].push_back(nameFileName);
240                 
241                 m->mothurOutEndLine();
242                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
243                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }       
244                 m->mothurOutEndLine();
245                 
246                 //set accnos file as new current accnosfile
247                 string current = "";
248                 itTypes = outputTypes.find("fasta");
249                 if (itTypes != outputTypes.end()) {
250                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
251                 }
252                 
253                 itTypes = outputTypes.find("name");
254                 if (itTypes != outputTypes.end()) {
255                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
256                 }
257                 
258                 
259                 return 0;
260         }
261         catch(exception& e) {
262                 m->errorOut(e, "ShhhSeqsCommand", "execute");
263                 exit(1);
264         }
265 }
266 //**********************************************************************************************************************
267 int ShhhSeqsCommand::readData(correctDist* correct, seqNoise& noise, vector<string>& seqs, vector<string>& uNames, vector<string>& rNames, vector<int>& freq) {
268         try {
269                 map<string, string> nameMap; 
270                 map<string, string>::iterator it;
271                 m->readNames(namefile, nameMap);
272                 bool error = false;
273                 
274                 ifstream in;
275                 m->openInputFile(fastafile, in);
276                 
277                 while (!in.eof()) {
278                         
279                         if (m->control_pressed) { in.close(); return 0; }
280                         
281                         Sequence seq(in); m->gobble(in);
282                         
283                         if (seq.getName() != "") {
284                                 correct->addSeq(seq.getName(), seq.getAligned());
285                                 
286                                 it = nameMap.find(seq.getName());
287                                 if (it != nameMap.end()) {
288                                         noise.addSeq(seq.getAligned(), seqs);
289                                         noise.addRedundantName(it->first, it->second, uNames, rNames, freq);
290                                 }else {
291                                         m->mothurOut("[ERROR]: " + seq.getName() + " is in your fasta file and not in your namefile, please correct.");
292                                         error = true;
293                                 }
294                         }
295                 }
296                 in.close();
297                 
298                 if (error) { m->control_pressed = true; }
299                 
300                 return seqs.size();
301                 
302         }catch(exception& e) {
303                 m->errorOut(e, "ShhhSeqsCommand", "readData");
304                 exit(1);
305         }
306 }
307 //**********************************************************************************************************************
308 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) {
309         try {
310                 bool error = false;
311                 map<string, string>::iterator it;
312                 
313                 for (int i = 0; i < sequences.size(); i++) {
314                         
315                         if (m->control_pressed) { return 0; }
316                         
317                         if (sequences[i].getName() != "") {
318                                 correct->addSeq(sequences[i].getName(), sequences[i].getAligned());
319                                 
320                                 it = nameMap.find(sequences[i].getName());
321                                 if (it != nameMap.end()) {
322                                         noise.addSeq(sequences[i].getAligned(), seqs);
323                                         noise.addRedundantName(it->first, it->second, uNames, rNames, freq);
324                                 }else {
325                                         m->mothurOut("[ERROR]: " + sequences[i].getName() + " is in your fasta file and not in your namefile, please correct.");
326                                         error = true;
327                                 }
328                         }
329                 }
330                                 
331                 if (error) { m->control_pressed = true; }
332                 
333                 return seqs.size();
334                 
335         }catch(exception& e) {
336                 m->errorOut(e, "ShhhSeqsCommand", "loadData");
337                 exit(1);
338         }
339 }
340 /**************************************************************************************************/
341 vector<string> ShhhSeqsCommand::createProcessesGroups(SequenceParser& parser, string newFName, string newNName, string newMName, vector<string> groups) {
342         try {
343                 
344                 vector<int> processIDS;
345                 int process = 1;
346                 vector<string> mapfileNames;
347                 
348                 //sanity check
349                 if (groups.size() < processors) { processors = groups.size(); }
350                 
351                 //divide the groups between the processors
352                 vector<linePair> lines;
353                 int numGroupsPerProcessor = groups.size() / processors;
354                 for (int i = 0; i < processors; i++) {
355                         int startIndex =  i * numGroupsPerProcessor;
356                         int endIndex = (i+1) * numGroupsPerProcessor;
357                         if(i == (processors - 1)){      endIndex = groups.size();       }
358                         lines.push_back(linePair(startIndex, endIndex));
359                 }
360                 
361 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)           
362                 
363                 //loop through and create all the processes you want
364                 while (process != processors) {
365                         int pid = fork();
366                         
367                         if (pid > 0) {
368                                 processIDS.push_back(pid);  //create map from line number to pid so you can append files in correct order later
369                                 process++;
370                         }else if (pid == 0){
371                                 mapfileNames = driverGroups(parser, newFName + toString(getpid()) + ".temp", newNName + toString(getpid()) + ".temp", newMName, lines[process].start, lines[process].end, groups);
372                                 
373                                 //pass filenames to parent
374                                 ofstream out;
375                                 string tempFile = newMName + toString(getpid()) + ".temp";
376                                 m->openOutputFile(tempFile, out);
377                                 out << mapfileNames.size() << endl;
378                                 for (int i = 0; i < mapfileNames.size(); i++) {
379                                         out << mapfileNames[i] << endl;
380                                 }
381                                 out.close();
382                                 
383                                 exit(0);
384                         }else { 
385                                 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
386                                 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
387                                 exit(0);
388                         }
389                 }
390                 
391                 //do my part
392                 mapfileNames = driverGroups(parser, newFName, newNName, newMName, lines[0].start, lines[0].end, groups);
393                 
394                 //force parent to wait until all the processes are done
395                 for (int i=0;i<processIDS.size();i++) { 
396                         int temp = processIDS[i];
397                         wait(&temp);
398                 }
399                 
400                 //append output files
401                 for(int i=0;i<processIDS.size();i++){
402                         ifstream in;
403                         string tempFile =  newMName + toString(processIDS[i]) + ".temp";
404                         m->openInputFile(tempFile, in);
405                         if (!in.eof()) { 
406                                 int tempNum = 0; in >> tempNum;  m->gobble(in);
407                                 for (int j = 0; j < tempNum; j++) {
408                                         string filename;
409                                         in >> filename; m->gobble(in);
410                                         mapfileNames.push_back(filename);
411                                 }
412                         }
413                         in.close(); m->mothurRemove(tempFile);
414                         
415                 }
416 #else
417                 
418                 //////////////////////////////////////////////////////////////////////////////////////////////////////
419                 //Windows version shared memory, so be careful when passing variables through the shhhseqsData struct. 
420                 //Above fork() will clone, so memory is separate, but that's not the case with windows, 
421                 //////////////////////////////////////////////////////////////////////////////////////////////////////
422                 
423                 vector<shhhseqsData*> pDataArray; 
424                 DWORD   dwThreadIdArray[processors-1];
425                 HANDLE  hThreadArray[processors-1]; 
426                 
427                 //Create processor worker threads.
428                 for( int i=1; i<processors; i++ ){
429                         // Allocate memory for thread data.
430                         string extension = toString(i) + ".temp";
431
432                         shhhseqsData* tempShhhseqs = new shhhseqsData(fastafile, namefile, groupfile, (newFName+extension), (newNName+extension), newMName, groups, m, lines[i].start, lines[i].end, sigma, i);
433                         pDataArray.push_back(tempShhhseqs);
434                         processIDS.push_back(i);
435                         
436                         //MySeqSumThreadFunction is in header. It must be global or static to work with the threads.
437                         //default security attributes, thread function name, argument to thread function, use default creation flags, returns the thread identifier
438                         hThreadArray[i-1] = CreateThread(NULL, 0, MyShhhSeqsThreadFunction, pDataArray[i-1], 0, &dwThreadIdArray[i-1]);   
439                 }
440                 
441                 
442                 //using the main process as a worker saves time and memory
443                 mapfileNames = driverGroups(parser, newFName, newNName, newMName, lines[0].start, lines[0].end, groups);
444                 
445                 //Wait until all threads have terminated.
446                 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
447                 
448                 //Close all thread handles and free memory allocations.
449                 for(int i=0; i < pDataArray.size(); i++){
450                         for (int j = 0; j < pDataArray[i]->mapfileNames.size(); j++) {
451                                 mapfileNames.push_back(pDataArray[i]->mapfileNames[j]);
452                         }
453                         CloseHandle(hThreadArray[i]);
454                         delete pDataArray[i];
455                 }
456                 
457 #endif          
458                 
459                 //append output files
460                 for(int i=0;i<processIDS.size();i++){
461                         m->appendFiles((newFName + toString(processIDS[i]) + ".temp"), newFName);
462                         m->mothurRemove((newFName + toString(processIDS[i]) + ".temp"));
463                         
464                         m->appendFiles((newNName + toString(processIDS[i]) + ".temp"), newNName);
465                         m->mothurRemove((newNName + toString(processIDS[i]) + ".temp"));
466                 }
467                 
468                 return mapfileNames;    
469                 
470         }
471         catch(exception& e) {
472                 m->errorOut(e, "ShhhSeqsCommand", "createProcessesGroups");
473                 exit(1);
474         }
475 }
476 /**************************************************************************************************/
477 vector<string> ShhhSeqsCommand::driverGroups(SequenceParser& parser, string newFFile, string newNFile, string newMFile, int start, int end, vector<string> groups){
478         try {
479                 
480                 vector<string> mapFileNames;
481                 
482                 for (int i = start; i < end; i++) {
483                         
484                         start = time(NULL);
485                         
486                         if (m->control_pressed) {  return mapFileNames; }
487                         
488                         m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[i] + ":"); m->mothurOutEndLine();
489                         
490                         map<string, string> thisNameMap;
491                         thisNameMap = parser.getNameMap(groups[i]); 
492                         vector<Sequence> thisSeqs = parser.getSeqs(groups[i]);
493                         
494                         vector<string> sequences;
495                         vector<string> uniqueNames;
496                         vector<string> redundantNames;
497                         vector<int> seqFreq;
498                         
499                         seqNoise noise;
500                         correctDist* correct = new correctDist(1); //we use one processor since we already split up the work load.
501                         
502                         //load this groups info in order
503                         loadData(correct, noise, sequences, uniqueNames, redundantNames, seqFreq, thisNameMap, thisSeqs);
504                         if (m->control_pressed) { return mapFileNames; }
505                         
506                         //calc distances for cluster
507                         string distFileName = outputDir + m->getRootName(m->getSimpleName(fastafile)) + groups[i] + ".shhh.dist";
508                         correct->execute(distFileName);
509                         delete correct;
510                         
511                         if (m->control_pressed) { m->mothurRemove(distFileName); return mapFileNames; }
512                         
513                         driver(noise, sequences, uniqueNames, redundantNames, seqFreq, distFileName, newFFile+groups[i], newNFile+groups[i], newMFile+groups[i]+".map"); 
514                         
515                         if (m->control_pressed) { return mapFileNames; }
516                         
517                         m->appendFiles(newFFile+groups[i], newFFile); m->mothurRemove(newFFile+groups[i]);
518                         m->appendFiles(newNFile+groups[i], newNFile); m->mothurRemove(newNFile+groups[i]);
519                         mapFileNames.push_back(newMFile+groups[i]+".map");
520                         
521                         m->mothurOut("It took " + toString(time(NULL) - start) + " secs to process group " + groups[i] + "."); m->mothurOutEndLine(); 
522                 }
523                 
524                 return mapFileNames;
525         }
526         catch(exception& e) {
527                 m->errorOut(e, "ShhhSeqsCommand", "driverGroups");
528                 exit(1);
529         }
530 }
531 //**********************************************************************************************************************
532 int ShhhSeqsCommand::driver(seqNoise& noise, 
533                                                         vector<string>& sequences, 
534                                                         vector<string>& uniqueNames, 
535                                                         vector<string>& redundantNames, 
536                                                         vector<int>& seqFreq, 
537                                                         string distFileName, string outputFileName, string nameFileName, string mapFileName) {
538         try {
539                 double cutOff = 0.08;
540                 int minIter = 10;
541                 int maxIter = 1000;
542                 double minDelta = 1e-6;
543                 int numIters = 0;
544                 double maxDelta = 1e6;
545                 int numSeqs = sequences.size();
546                                 
547                 //run cluster command
548                 string inputString = "phylip=" + distFileName + ", method=furthest, cutoff=0.08";
549                 m->mothurOut("/******************************************/"); m->mothurOutEndLine(); 
550                 m->mothurOut("Running command: cluster(" + inputString + ")"); m->mothurOutEndLine(); 
551                 
552                 Command* clusterCommand = new ClusterCommand(inputString);
553                 clusterCommand->execute();
554                 
555                 map<string, vector<string> > filenames = clusterCommand->getOutputFiles();
556                 string listFileName = filenames["list"][0];
557                 string rabundFileName = filenames["rabund"][0]; m->mothurRemove(rabundFileName);
558                 string sabundFileName = filenames["sabund"][0]; m->mothurRemove(sabundFileName);
559         
560                 delete clusterCommand;
561                 m->mothurOut("/******************************************/"); m->mothurOutEndLine(); 
562                 
563                 if (m->control_pressed) { m->mothurRemove(distFileName); m->mothurRemove(listFileName); return 0; }
564                 
565                 vector<double> distances(numSeqs * numSeqs);
566                 noise.getDistanceData(distFileName, distances);
567                 m->mothurRemove(distFileName); 
568                 if (m->control_pressed) { m->mothurRemove(listFileName); return 0; }
569                 
570                 vector<int> otuData(numSeqs);
571                 vector<int> otuFreq;
572                 vector<vector<int> > otuBySeqLookUp;
573                 noise.getListData(listFileName, cutOff, otuData, otuFreq, otuBySeqLookUp);
574                 m->mothurRemove(listFileName);
575                 if (m->control_pressed) { return 0; }
576                 
577                 int numOTUs = otuFreq.size();
578                 
579                 vector<double> weights(numOTUs, 0);
580                 vector<int> change(numOTUs, 1);
581                 vector<int> centroids(numOTUs, -1);
582                 vector<int> cumCount(numOTUs, 0);
583                 
584                 vector<double> tau(numSeqs, 1);
585                 vector<int> anP(numSeqs, 0);
586                 vector<int> anI(numSeqs, 0);
587                 vector<int> anN(numSeqs, 0);
588                 vector<vector<int> > aanI = otuBySeqLookUp;
589                 
590                 while(numIters < minIter || ((maxDelta > minDelta) && (numIters < maxIter))){
591                         
592                         if (m->control_pressed) { return 0; }
593                         
594                         noise.updateOTUCountData(otuFreq, otuBySeqLookUp, aanI, anP, anI, cumCount); if (m->control_pressed) { return 0; }
595                         maxDelta = noise.calcNewWeights(weights, seqFreq, anI, cumCount, anP, otuFreq, tau);  if (m->control_pressed) { return 0; }
596                         
597                         noise.calcCentroids(anI, anP, change, centroids, cumCount, distances, seqFreq, otuFreq, tau); if (m->control_pressed) { return 0; }
598                         noise.checkCentroids(weights, centroids); if (m->control_pressed) { return 0; }
599                          
600                         otuFreq.assign(numOTUs, 0);
601                         
602                         int total = 0;
603                         
604                         for(int i=0;i<numSeqs;i++){
605                                 if (m->control_pressed) { return 0; }
606                                 
607                                 double offset = 1e6;
608                                 double norm = 0.0000;
609                                 double minWeight = 0.1;
610                                 vector<double> currentTau(numOTUs);
611                                 
612                                 for(int j=0;j<numOTUs;j++){
613                                         if (m->control_pressed) { return 0; }
614                                         if(weights[j] > minWeight && distances[i * numSeqs+centroids[j]] < offset){
615                                                 offset = distances[i * numSeqs+centroids[j]];
616                                         }
617                                 }
618                                 
619                                 for(int j=0;j<numOTUs;j++){
620                                         if (m->control_pressed) { return 0; }
621                                         if(weights[j] > minWeight){
622                                                 currentTau[j] = exp(sigma * (-distances[(i * numSeqs + centroids[j])] + offset)) * weights[j];
623                                                 norm += currentTau[j];
624                                         }
625                                         else{
626                                                 currentTau[j] = 0.0000;
627                                         }
628                                 }                       
629                                 
630                                 for(int j=0;j<numOTUs;j++){
631                                         if (m->control_pressed) { return 0; }
632                                         currentTau[j] /= norm;
633                                 }
634                                 
635                                 for(int j=0;j<numOTUs;j++){
636                                         if (m->control_pressed) { return 0; }
637                                         
638                                         if(currentTau[j] > 1.0e-4){
639                                                 int oldTotal = total;
640                                                 total++;
641                                                 
642                                                 tau.resize(oldTotal+1);
643                                                 tau[oldTotal] = currentTau[j];
644                                                 otuBySeqLookUp[j][otuFreq[j]] = oldTotal;
645                                                 aanI[j][otuFreq[j]] = i;
646                                                 otuFreq[j]++;
647                                                 
648                                         }
649                                 }
650                                 
651                                 anP.resize(total);
652                                 anI.resize(total);
653                         }
654                         
655                         numIters++;
656                 }
657                 
658                 noise.updateOTUCountData(otuFreq, otuBySeqLookUp, aanI, anP, anI, cumCount);  if (m->control_pressed) { return 0; }
659                 
660                 vector<double> percentage(numSeqs);
661                 noise.setUpOTUData(otuData, percentage, cumCount, tau, otuFreq, anP, anI);  if (m->control_pressed) { return 0; }
662                 noise.finishOTUData(otuData, otuFreq, anP, anI, cumCount, otuBySeqLookUp, aanI, tau);  if (m->control_pressed) { return 0; }
663                 
664                 change.assign(numOTUs, 1);
665                 noise.calcCentroids(anI, anP, change, centroids, cumCount, distances, seqFreq, otuFreq, tau); if (m->control_pressed) { return 0; }
666                 
667                 
668                 vector<int> finalTau(numOTUs, 0);
669                 for(int i=0;i<numSeqs;i++){
670                         if (m->control_pressed) { return 0; }
671                         finalTau[otuData[i]] += int(seqFreq[i]);
672                 }
673                 
674                 noise.writeOutput(outputFileName, nameFileName, mapFileName, finalTau, centroids, otuData, sequences, uniqueNames, redundantNames, seqFreq, distances);
675                 
676                 return 0;
677                 
678         }catch(exception& e) {
679                 m->errorOut(e, "ShhhSeqsCommand", "driver");
680                 exit(1);
681         }
682 }       
683 //**********************************************************************************************************************
684 int ShhhSeqsCommand::deconvoluteResults(string fastaFile, string nameFile){
685         try {
686                 m->mothurOutEndLine(); m->mothurOut("Deconvoluting results:"); m->mothurOutEndLine(); m->mothurOutEndLine();
687                 
688                 //use unique.seqs to create new name and fastafile
689                 string inputString = "fasta=" + fastaFile + ", name=" + nameFile;
690                 m->mothurOut("/******************************************/"); m->mothurOutEndLine(); 
691                 m->mothurOut("Running command: unique.seqs(" + inputString + ")"); m->mothurOutEndLine(); 
692                 
693                 Command* uniqueCommand = new DeconvoluteCommand(inputString);
694                 uniqueCommand->execute();
695                 
696                 map<string, vector<string> > filenames = uniqueCommand->getOutputFiles();
697                 
698                 delete uniqueCommand;
699                 
700                 m->mothurOut("/******************************************/"); m->mothurOutEndLine(); 
701                 
702                 string newnameFile = filenames["name"][0];
703                 string newfastaFile = filenames["fasta"][0];
704                 
705                 m->mothurRemove(fastaFile); rename(newfastaFile.c_str(), fastaFile.c_str()); 
706                 if (nameFile != newnameFile) { m->mothurRemove(nameFile); rename(newnameFile.c_str(), nameFile.c_str()); }
707                 
708                 return 0;
709         }
710         catch(exception& e) {
711                 m->errorOut(e, "ShhhSeqsCommand", "deconvoluteResults");
712                 exit(1);
713         }
714 }
715 //**********************************************************************************************************************
716
717
718