5 * Created by Pat Schloss on 12/27/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "shhhercommand.h"
12 #include "readcolumn.h"
13 #include "readmatrix.hpp"
14 #include "rabundvector.hpp"
15 #include "sabundvector.hpp"
16 #include "listvector.hpp"
17 #include "cluster.hpp"
18 #include "sparsematrix.hpp"
21 //**********************************************************************************************************************
26 #define MIN_WEIGHT 0.1
27 #define MIN_TAU 0.0001
29 //**********************************************************************************************************************
30 vector<string> ShhherCommand::setParameters(){
32 CommandParameter pflow("flow", "InputTypes", "", "", "none", "fileflow", "none",false,false); parameters.push_back(pflow);
33 CommandParameter pfile("file", "InputTypes", "", "", "none", "fileflow", "none",false,false); parameters.push_back(pfile);
34 CommandParameter plookup("lookup", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(plookup);
35 CommandParameter pcutoff("cutoff", "Number", "", "0.01", "", "", "",false,false); parameters.push_back(pcutoff);
36 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
37 CommandParameter pmaxiter("maxiter", "Number", "", "1000", "", "", "",false,false); parameters.push_back(pmaxiter);
38 CommandParameter psigma("sigma", "Number", "", "60", "", "", "",false,false); parameters.push_back(psigma);
39 CommandParameter pmindelta("mindelta", "Number", "", "0.000001", "", "", "",false,false); parameters.push_back(pmindelta);
40 CommandParameter porder("order", "String", "", "", "", "", "",false,false); parameters.push_back(porder);
41 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
42 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
44 vector<string> myArray;
45 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
49 m->errorOut(e, "ShhherCommand", "setParameters");
53 //**********************************************************************************************************************
54 string ShhherCommand::getHelpString(){
56 string helpString = "";
57 helpString += "The shhh.seqs command reads a file containing flowgrams and creates a file of corrected sequences.\n";
61 m->errorOut(e, "ShhherCommand", "getHelpString");
65 //**********************************************************************************************************************
67 ShhherCommand::ShhherCommand(){
69 abort = true; calledHelp = true;
72 //initialize outputTypes
73 vector<string> tempOutNames;
74 outputTypes["pn.dist"] = tempOutNames;
78 m->errorOut(e, "ShhherCommand", "ShhherCommand");
83 //**********************************************************************************************************************
85 ShhherCommand::ShhherCommand(string option) {
89 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
90 MPI_Comm_size(MPI_COMM_WORLD, &ncpus);
96 abort = false; calledHelp = false;
99 //allow user to run help
100 if(option == "help") { help(); abort = true; calledHelp = true; }
101 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
104 vector<string> myArray = setParameters();
106 OptionParser parser(option);
107 map<string,string> parameters = parser.getParameters();
109 ValidParameters validParameter;
110 map<string,string>::iterator it;
112 //check to make sure all parameters are valid for command
113 for (it = parameters.begin(); it != parameters.end(); it++) {
114 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
117 //initialize outputTypes
118 vector<string> tempOutNames;
119 outputTypes["pn.dist"] = tempOutNames;
120 // outputTypes["fasta"] = tempOutNames;
122 //if the user changes the input directory command factory will send this info to us in the output parameter
123 string inputDir = validParameter.validFile(parameters, "inputdir", false);
124 if (inputDir == "not found"){ inputDir = ""; }
127 it = parameters.find("flow");
128 //user has given a template file
129 if(it != parameters.end()){
130 path = m->hasPath(it->second);
131 //if the user has not given a path then, add inputdir. else leave path alone.
132 if (path == "") { parameters["flow"] = inputDir + it->second; }
135 it = parameters.find("lookup");
136 //user has given a template file
137 if(it != parameters.end()){
138 path = m->hasPath(it->second);
139 //if the user has not given a path then, add inputdir. else leave path alone.
140 if (path == "") { parameters["lookup"] = inputDir + it->second; }
143 it = parameters.find("file");
144 //user has given a template file
145 if(it != parameters.end()){
146 path = m->hasPath(it->second);
147 //if the user has not given a path then, add inputdir. else leave path alone.
148 if (path == "") { parameters["file"] = inputDir + it->second; }
153 //check for required parameters
154 flowFileName = validParameter.validFile(parameters, "flow", true);
155 flowFilesFileName = validParameter.validFile(parameters, "file", true);
156 if (flowFileName == "not found" && flowFilesFileName == "not found") {
157 m->mothurOut("values for either flow or file must be provided for the shhh.seqs command.");
158 m->mothurOutEndLine();
161 else if (flowFileName == "not open" || flowFilesFileName == "not open") { abort = true; }
163 if(flowFileName != "not found"){ compositeFASTAFileName = ""; }
165 compositeFASTAFileName = flowFilesFileName.substr(0, flowFilesFileName.length()-10) + "pn.fasta";
167 m->openOutputFile(compositeFASTAFileName, temp);
171 //if the user changes the output directory command factory will send this info to us in the output parameter
172 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
174 outputDir += m->hasPath(flowFileName); //if user entered a file with a path then preserve it
178 //check for optional parameter and set defaults
179 // ...at some point should added some additional type checking...
181 temp = validParameter.validFile(parameters, "lookup", true);
182 if (temp == "not found") { lookupFileName = "LookUp_Titanium.pat"; }
183 else if(temp == "not open") { abort = true; }
184 else { lookupFileName = temp; }
186 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
187 m->setProcessors(temp);
188 convert(temp, processors);
190 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found"){ temp = "0.01"; }
191 convert(temp, cutoff);
193 temp = validParameter.validFile(parameters, "mindelta", false); if (temp == "not found"){ temp = "0.000001"; }
194 convert(temp, minDelta);
196 temp = validParameter.validFile(parameters, "maxiter", false); if (temp == "not found"){ temp = "1000"; }
197 convert(temp, maxIters);
199 temp = validParameter.validFile(parameters, "sigma", false);if (temp == "not found") { temp = "60"; }
200 convert(temp, sigma);
202 flowOrder = validParameter.validFile(parameters, "order", false);
203 if (flowOrder == "not found"){ flowOrder = "TACG"; }
204 else if(flowOrder.length() != 4){
205 m->mothurOut("The value of the order option must be four bases long\n");
215 catch(exception& e) {
216 m->errorOut(e, "ShhherCommand", "ShhherCommand");
220 //**********************************************************************************************************************
222 int ShhherCommand::execute(){
224 if (abort == true) { if (calledHelp) { return 0; } return 2; }
231 for(int i=1;i<ncpus;i++){
232 MPI_Send(&abort, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
234 if(abort == 1){ return 0; }
238 m->mothurOut("\nGetting preliminary data...\n");
242 vector<string> flowFileVector;
243 if(flowFilesFileName != "not found"){
246 ifstream flowFilesFile;
247 m->openInputFile(flowFilesFileName, flowFilesFile);
248 while(flowFilesFile){
249 flowFilesFile >> fName;
250 flowFileVector.push_back(fName);
251 m->gobble(flowFilesFile);
255 flowFileVector.push_back(flowFileName);
257 int numFiles = flowFileVector.size();
259 for(int i=1;i<ncpus;i++){
260 MPI_Send(&numFiles, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
263 for(int i=0;i<numFiles;i++){
264 double begClock = clock();
265 unsigned long int begTime = time(NULL);
267 flowFileName = flowFileVector[i];
269 m->mothurOut("\n>>>>>\tProcessing " + flowFileName + " (file " + toString(i+1) + " of " + toString(numFiles) + ")\t<<<<<\n");
270 m->mothurOut("Reading flowgrams...\n");
273 m->mothurOut("Identifying unique flowgrams...\n");
276 m->mothurOut("Calculating distances between flowgrams...\n");
278 strcpy(fileName, flowFileName.c_str());
280 for(int i=1;i<ncpus;i++){
281 MPI_Send(&fileName[0], 1024, MPI_CHAR, i, tag, MPI_COMM_WORLD);
283 MPI_Send(&numSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
284 MPI_Send(&numUniques, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
285 MPI_Send(&numFlowCells, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
286 MPI_Send(&flowDataIntI[0], numSeqs * numFlowCells, MPI_SHORT, i, tag, MPI_COMM_WORLD);
287 MPI_Send(&flowDataPrI[0], numSeqs * numFlowCells, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
288 MPI_Send(&mapUniqueToSeq[0], numSeqs, MPI_INT, i, tag, MPI_COMM_WORLD);
289 MPI_Send(&mapSeqToUnique[0], numSeqs, MPI_INT, i, tag, MPI_COMM_WORLD);
290 MPI_Send(&lengths[0], numSeqs, MPI_INT, i, tag, MPI_COMM_WORLD);
291 MPI_Send(&jointLookUp[0], NUMBINS * NUMBINS, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
292 MPI_Send(&cutoff, 1, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
295 string distFileName = flowDistMPI(0, int(sqrt(1.0/float(ncpus)) * numUniques));
298 for(int i=1;i<ncpus;i++){
299 MPI_Recv(&done, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
301 m->appendFiles((distFileName + ".temp." + toString(i)), distFileName);
302 remove((distFileName + ".temp." + toString(i)).c_str());
305 string namesFileName = createNamesFile();
307 m->mothurOut("\nClustering flowgrams...\n");
308 string listFileName = cluster(distFileName, namesFileName);
310 getOTUData(listFileName);
313 for(int i=1;i<ncpus;i++){
314 MPI_Send(&numOTUs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
315 MPI_Send(&singleLookUp[0], singleLookUp.size(), MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
316 MPI_Send(&uniqueFlowgrams[0], numFlowCells * numUniques, MPI_SHORT, i, tag, MPI_COMM_WORLD);
317 MPI_Send(&sigma, 1, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
324 int numOTUsOnCPU = numOTUs / ncpus;
325 int numSeqsOnCPU = numSeqs / ncpus;
326 m->mothurOut("\nDenoising flowgrams...\n");
327 m->mothurOut("iter\tmaxDelta\tnLL\t\tcycletime\n");
329 while((maxIters == 0 && maxDelta > minDelta) || iter < MIN_ITER || (maxDelta > minDelta && iter < maxIters)){
331 double cycClock = clock();
332 unsigned long int cycTime = time(NULL);
335 int total = singleTau.size();
336 for(int i=1;i<ncpus;i++){
337 MPI_Send(&total, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
338 MPI_Send(&change[0], numOTUs, MPI_SHORT, i, tag, MPI_COMM_WORLD);
339 MPI_Send(¢roids[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
341 MPI_Send(&singleTau[0], total, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
342 MPI_Send(&seqNumber[0], total, MPI_INT, i, tag, MPI_COMM_WORLD);
343 MPI_Send(&seqIndex[0], total, MPI_INT, i, tag, MPI_COMM_WORLD);
344 MPI_Send(&nSeqsPerOTU[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
345 MPI_Send(&cumNumSeqs[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
348 calcCentroidsDriver(0, numOTUsOnCPU);
350 for(int i=1;i<ncpus;i++){
351 int otuStart = i * numOTUs / ncpus;
352 int otuStop = (i + 1) * numOTUs / ncpus;
354 vector<int> tempCentroids(numOTUs, 0);
355 vector<short> tempChange(numOTUs, 0);
357 MPI_Recv(&tempCentroids[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
358 MPI_Recv(&tempChange[0], numOTUs, MPI_SHORT, i, tag, MPI_COMM_WORLD, &status);
360 for(int j=otuStart;j<otuStop;j++){
361 centroids[j] = tempCentroids[j];
362 change[j] = tempChange[j];
366 maxDelta = getNewWeights();
367 double nLL = getLikelihood();
370 for(int i=1;i<ncpus;i++){
371 MPI_Send(¢roids[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
372 MPI_Send(&weight[0], numOTUs, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
373 MPI_Send(&change[0], numOTUs, MPI_SHORT, i, tag, MPI_COMM_WORLD);
376 calcNewDistancesParent(0, numSeqsOnCPU);
378 total = singleTau.size();
380 for(int i=1;i<ncpus;i++){
382 int seqStart = i * numSeqs / ncpus;
383 int seqStop = (i + 1) * numSeqs / ncpus;
385 MPI_Recv(&childTotal, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
387 vector<int> childSeqIndex(childTotal, 0);
388 vector<double> childSingleTau(childTotal, 0);
389 vector<double> childDist(numSeqs * numOTUs, 0);
390 vector<int> otuIndex(childTotal, 0);
392 MPI_Recv(&childSeqIndex[0], childTotal, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
393 MPI_Recv(&childSingleTau[0], childTotal, MPI_DOUBLE, i, tag, MPI_COMM_WORLD, &status);
394 MPI_Recv(&childDist[0], numOTUs * numSeqs, MPI_DOUBLE, i, tag, MPI_COMM_WORLD, &status);
395 MPI_Recv(&otuIndex[0], childTotal, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
397 int oldTotal = total;
399 singleTau.resize(total, 0);
400 seqIndex.resize(total, 0);
401 seqNumber.resize(total, 0);
405 for(int j=oldTotal;j<total;j++){
406 int otuI = otuIndex[childIndex];
407 int seqI = childSeqIndex[childIndex];
409 singleTau[j] = childSingleTau[childIndex];
411 aaP[otuI][nSeqsPerOTU[otuI]] = j;
412 aaI[otuI][nSeqsPerOTU[otuI]] = seqI;
417 int index = seqStart * numOTUs;
418 for(int j=seqStart;j<seqStop;j++){
419 for(int k=0;k<numOTUs;k++){
420 dist[index] = childDist[index];
428 m->mothurOut(toString(iter) + '\t' + toString(maxDelta) + '\t' + toString(nLL) + '\t' + toString(time(NULL) - cycTime) + '\t' + toString((clock() - cycClock)/(double)CLOCKS_PER_SEC) + '\n');
430 if((maxIters == 0 && maxDelta > minDelta) || iter < MIN_ITER || (maxDelta > minDelta && iter < maxIters)){
432 for(int i=1;i<ncpus;i++){
433 MPI_Send(&live, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
438 for(int i=1;i<ncpus;i++){
439 MPI_Send(&live, 1, MPI_INT, i, tag, MPI_COMM_WORLD); //send kill command
445 m->mothurOut("\nFinalizing...\n");
448 vector<int> otuCounts(numOTUs, 0);
449 for(int i=0;i<numSeqs;i++) { otuCounts[otuData[i]]++; }
450 calcCentroidsDriver(0, numOTUs);
451 writeQualities(otuCounts);
452 writeSequences(otuCounts);
453 writeNames(otuCounts);
454 writeClusters(otuCounts);
457 remove(distFileName.c_str());
458 remove(namesFileName.c_str());
459 remove(listFileName.c_str());
461 m->mothurOut("Total time to process " + toString(flowFileName) + ":\t" + toString(time(NULL) - begTime) + '\t' + toString((clock() - begClock)/(double)CLOCKS_PER_SEC) + '\n');
467 MPI_Recv(&abort, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
468 if(abort){ return 0; }
471 MPI_Recv(&numFiles, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
473 for(int i=0;i<numFiles;i++){
474 //Now into the pyrodist part
478 MPI_Recv(&fileName, 1024, MPI_CHAR, 0, tag, MPI_COMM_WORLD, &status);
479 MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
480 MPI_Recv(&numUniques, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
481 MPI_Recv(&numFlowCells, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
483 flowDataIntI.resize(numSeqs * numFlowCells);
484 flowDataPrI.resize(numSeqs * numFlowCells);
485 mapUniqueToSeq.resize(numSeqs);
486 mapSeqToUnique.resize(numSeqs);
487 lengths.resize(numSeqs);
488 jointLookUp.resize(NUMBINS * NUMBINS);
490 MPI_Recv(&flowDataIntI[0], numSeqs * numFlowCells, MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
491 MPI_Recv(&flowDataPrI[0], numSeqs * numFlowCells, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
492 MPI_Recv(&mapUniqueToSeq[0], numSeqs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
493 MPI_Recv(&mapSeqToUnique[0], numSeqs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
494 MPI_Recv(&lengths[0], numSeqs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
495 MPI_Recv(&jointLookUp[0], NUMBINS * NUMBINS, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
496 MPI_Recv(&cutoff, 1, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
498 flowFileName = string(fileName);
499 int flowDistStart = int(sqrt(float(pid)/float(ncpus)) * numUniques);
500 int flowDistEnd = int(sqrt(float(pid+1)/float(ncpus)) * numUniques);
502 string distanceStringChild = flowDistMPI(flowDistStart, flowDistEnd);
505 MPI_Send(&done, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
507 //Now into the pyronoise part
508 MPI_Recv(&numOTUs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
510 singleLookUp.resize(HOMOPS * NUMBINS);
511 uniqueFlowgrams.resize(numUniques * numFlowCells);
512 weight.resize(numOTUs);
513 centroids.resize(numOTUs);
514 change.resize(numOTUs);
515 dist.assign(numOTUs * numSeqs, 0);
516 nSeqsPerOTU.resize(numOTUs);
517 cumNumSeqs.resize(numOTUs);
519 MPI_Recv(&singleLookUp[0], singleLookUp.size(), MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
520 MPI_Recv(&uniqueFlowgrams[0], uniqueFlowgrams.size(), MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
521 MPI_Recv(&sigma, 1, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
523 int startOTU = pid * numOTUs / ncpus;
524 int endOTU = (pid + 1) * numOTUs / ncpus;
526 int startSeq = pid * numSeqs / ncpus;
527 int endSeq = (pid + 1) * numSeqs /ncpus;
533 MPI_Recv(&total, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
534 singleTau.assign(total, 0.0000);
535 seqNumber.assign(total, 0);
536 seqIndex.assign(total, 0);
538 MPI_Recv(&change[0], numOTUs, MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
539 MPI_Recv(¢roids[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
540 MPI_Recv(&singleTau[0], total, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
541 MPI_Recv(&seqNumber[0], total, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
542 MPI_Recv(&seqIndex[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
543 MPI_Recv(&nSeqsPerOTU[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
544 MPI_Recv(&cumNumSeqs[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
546 calcCentroidsDriver(startOTU, endOTU);
548 MPI_Send(¢roids[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD);
549 MPI_Send(&change[0], numOTUs, MPI_SHORT, 0, tag, MPI_COMM_WORLD);
551 MPI_Recv(¢roids[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
552 MPI_Recv(&weight[0], numOTUs, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
553 MPI_Recv(&change[0], numOTUs, MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
555 vector<int> otuIndex(total, 0);
556 calcNewDistancesChildMPI(startSeq, endSeq, otuIndex);
557 total = otuIndex.size();
559 MPI_Send(&total, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
560 MPI_Send(&seqIndex[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD);
561 MPI_Send(&singleTau[0], total, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD);
562 MPI_Send(&dist[0], numOTUs * numSeqs, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD);
563 MPI_Send(&otuIndex[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD);
565 MPI_Recv(&live, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
569 MPI_Barrier(MPI_COMM_WORLD);
574 catch(exception& e) {
575 m->errorOut(e, "ShhherCommand", "execute");
580 /**************************************************************************************************/
582 string ShhherCommand::flowDistMPI(int startSeq, int stopSeq){
584 ostringstream outStream;
585 outStream.setf(ios::fixed, ios::floatfield);
586 outStream.setf(ios::dec, ios::basefield);
587 outStream.setf(ios::showpoint);
588 outStream.precision(6);
590 int begTime = time(NULL);
591 double begClock = clock();
593 for(int i=startSeq;i<stopSeq;i++){
594 for(int j=0;j<i;j++){
595 float flowDistance = calcPairwiseDist(mapUniqueToSeq[i], mapUniqueToSeq[j]);
597 if(flowDistance < 1e-6){
598 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << 0.000000 << endl;
600 else if(flowDistance <= cutoff){
601 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << flowDistance << endl;
605 m->mothurOut(toString(i) + '\t' + toString(time(NULL) - begTime) + '\t' + toString((clock()-begClock)/CLOCKS_PER_SEC) + '\n');
609 m->mothurOut(toString(stopSeq) + '\t' + toString(time(NULL) - begTime) + '\t' + toString((clock()-begClock)/CLOCKS_PER_SEC) + '\n');
611 string fDistFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.dist";
612 if(pid != 0){ fDistFileName += ".temp." + toString(pid); }
614 ofstream distFile(fDistFileName.c_str());
615 distFile << outStream.str();
618 return fDistFileName;
620 catch(exception& e) {
621 m->errorOut(e, "ShhherCommand", "flowDistParentFork");
627 //**********************************************************************************************************************
629 int ShhherCommand::execute(){
631 if (abort == true) { return 0; }
636 vector<string> flowFileVector;
637 if(flowFilesFileName != "not found"){
640 ifstream flowFilesFile;
641 m->openInputFile(flowFilesFileName, flowFilesFile);
642 while(flowFilesFile){
643 flowFilesFile >> fName;
644 flowFileVector.push_back(fName);
645 m->gobble(flowFilesFile);
649 flowFileVector.push_back(flowFileName);
651 int numFiles = flowFileVector.size();
654 for(int i=0;i<numFiles;i++){
655 flowFileName = flowFileVector[i];
657 m->mothurOut("\n>>>>>\tProcessing " + flowFileName + " (file " + toString(i+1) + " of " + toString(numFiles) + ")\t<<<<<\n");
658 m->mothurOut("Reading flowgrams...\n");
661 m->mothurOut("Identifying unique flowgrams...\n");
665 m->mothurOut("Calculating distances between flowgrams...\n");
666 string distFileName = createDistFile(processors);
667 string namesFileName = createNamesFile();
669 m->mothurOut("\nClustering flowgrams...\n");
670 string listFileName = cluster(distFileName, namesFileName);
671 getOTUData(listFileName);
678 double begClock = clock();
679 unsigned long int begTime = time(NULL);
681 m->mothurOut("\nDenoising flowgrams...\n");
682 m->mothurOut("iter\tmaxDelta\tnLL\t\tcycletime\n");
684 while((maxIters == 0 && maxDelta > minDelta) || iter < MIN_ITER || (maxDelta > minDelta && iter < maxIters)){
686 double cycClock = clock();
687 unsigned long int cycTime = time(NULL);
692 maxDelta = getNewWeights();
693 double nLL = getLikelihood();
700 m->mothurOut(toString(iter) + '\t' + toString(maxDelta) + '\t' + toString(nLL) + '\t' + toString(time(NULL) - cycTime) + '\t' + toString((clock() - cycClock)/(double)CLOCKS_PER_SEC) + '\n');
704 m->mothurOut("\nFinalizing...\n");
708 vector<int> otuCounts(numOTUs, 0);
709 for(int i=0;i<numSeqs;i++) { otuCounts[otuData[i]]++; }
711 calcCentroidsDriver(0, numOTUs);
712 writeQualities(otuCounts);
713 writeSequences(otuCounts);
714 writeNames(otuCounts);
715 writeClusters(otuCounts);
718 remove(distFileName.c_str());
719 remove(namesFileName.c_str());
720 remove(listFileName.c_str());
722 m->mothurOut("Total time to process " + flowFileName + ":\t" + toString(time(NULL) - begTime) + '\t' + toString((clock() - begClock)/(double)CLOCKS_PER_SEC) + '\n');
726 catch(exception& e) {
727 m->errorOut(e, "ShhherCommand", "execute");
732 /**************************************************************************************************/
734 void ShhherCommand::getFlowData(){
737 m->openInputFile(flowFileName, flowFile);
740 seqNameVector.clear();
742 flowDataIntI.clear();
746 int currentNumFlowCells;
750 flowFile >> numFlowCells;
751 int index = 0;//pcluster
752 while(!flowFile.eof()){
753 flowFile >> seqName >> currentNumFlowCells;
754 lengths.push_back(currentNumFlowCells);
756 seqNameVector.push_back(seqName);
757 nameMap[seqName] = index++;//pcluster
759 for(int i=0;i<numFlowCells;i++){
760 flowFile >> intensity;
761 if(intensity > 9.99) { intensity = 9.99; }
762 int intI = int(100 * intensity + 0.0001);
763 flowDataIntI.push_back(intI);
769 numSeqs = seqNameVector.size();
771 for(int i=0;i<numSeqs;i++){
772 int iNumFlowCells = i * numFlowCells;
773 for(int j=lengths[i];j<numFlowCells;j++){
774 flowDataIntI[iNumFlowCells + j] = 0;
779 catch(exception& e) {
780 m->errorOut(e, "ShhherCommand", "getFlowData");
785 /**************************************************************************************************/
787 void ShhherCommand::getSingleLookUp(){
789 // these are the -log probabilities that a signal corresponds to a particular homopolymer length
790 singleLookUp.assign(HOMOPS * NUMBINS, 0);
794 m->openInputFile(lookupFileName, lookUpFile);
796 for(int i=0;i<HOMOPS;i++){
798 lookUpFile >> logFracFreq;
800 for(int j=0;j<NUMBINS;j++) {
801 lookUpFile >> singleLookUp[index];
807 catch(exception& e) {
808 m->errorOut(e, "ShhherCommand", "getSingleLookUp");
813 /**************************************************************************************************/
815 void ShhherCommand::getJointLookUp(){
818 // the most likely joint probability (-log) that two intenities have the same polymer length
819 jointLookUp.resize(NUMBINS * NUMBINS, 0);
821 for(int i=0;i<NUMBINS;i++){
822 for(int j=0;j<NUMBINS;j++){
824 double minSum = 100000000;
826 for(int k=0;k<HOMOPS;k++){
827 double sum = singleLookUp[k * NUMBINS + i] + singleLookUp[k * NUMBINS + j];
829 if(sum < minSum) { minSum = sum; }
831 jointLookUp[i * NUMBINS + j] = minSum;
835 catch(exception& e) {
836 m->errorOut(e, "ShhherCommand", "getJointLookUp");
841 /**************************************************************************************************/
843 double ShhherCommand::getProbIntensity(int intIntensity){
846 double minNegLogProb = 100000000;
849 for(int i=0;i<HOMOPS;i++){//loop signal strength
850 float negLogProb = singleLookUp[i * NUMBINS + intIntensity];
851 if(negLogProb < minNegLogProb) { minNegLogProb = negLogProb; }
854 return minNegLogProb;
856 catch(exception& e) {
857 m->errorOut(e, "ShhherCommand", "getProbIntensity");
862 /**************************************************************************************************/
864 void ShhherCommand::getUniques(){
869 uniqueFlowgrams.assign(numFlowCells * numSeqs, -1);
870 uniqueCount.assign(numSeqs, 0); // anWeights
871 uniqueLengths.assign(numSeqs, 0);
872 mapSeqToUnique.assign(numSeqs, -1);
873 mapUniqueToSeq.assign(numSeqs, -1);
875 vector<short> uniqueFlowDataIntI(numFlowCells * numSeqs, -1);
877 for(int i=0;i<numSeqs;i++){
880 vector<short> current(numFlowCells);
881 for(int j=0;j<numFlowCells;j++){
882 current[j] = short(((flowDataIntI[i * numFlowCells + j] + 50.0)/100.0));
885 for(int j=0;j<numUniques;j++){
886 int offset = j * numFlowCells;
890 if(lengths[i] < uniqueLengths[j]) { shorterLength = lengths[i]; }
891 else { shorterLength = uniqueLengths[j]; }
893 for(int k=0;k<shorterLength;k++){
894 if(current[k] != uniqueFlowgrams[offset + k]){
901 mapSeqToUnique[i] = j;
904 if(lengths[i] > uniqueLengths[j]) { uniqueLengths[j] = lengths[i]; }
910 if(index == numUniques){
911 uniqueLengths[numUniques] = lengths[i];
912 uniqueCount[numUniques] = 1;
913 mapSeqToUnique[i] = numUniques;//anMap
914 mapUniqueToSeq[numUniques] = i;//anF
916 for(int k=0;k<numFlowCells;k++){
917 uniqueFlowgrams[numUniques * numFlowCells + k] = current[k];
918 uniqueFlowDataIntI[numUniques * numFlowCells + k] = flowDataIntI[i * numFlowCells + k];
924 uniqueFlowDataIntI.resize(numFlowCells * numUniques);
925 uniqueLengths.resize(numUniques);
927 flowDataPrI.resize(numSeqs * numFlowCells, 0);
928 for(int i=0;i<flowDataPrI.size();i++) { flowDataPrI[i] = getProbIntensity(flowDataIntI[i]); }
930 catch(exception& e) {
931 m->errorOut(e, "ShhherCommand", "getUniques");
936 /**************************************************************************************************/
938 float ShhherCommand::calcPairwiseDist(int seqA, int seqB){
940 int minLength = lengths[mapSeqToUnique[seqA]];
941 if(lengths[seqB] < minLength){ minLength = lengths[mapSeqToUnique[seqB]]; }
943 int ANumFlowCells = seqA * numFlowCells;
944 int BNumFlowCells = seqB * numFlowCells;
948 for(int i=0;i<minLength;i++){
949 int flowAIntI = flowDataIntI[ANumFlowCells + i];
950 float flowAPrI = flowDataPrI[ANumFlowCells + i];
952 int flowBIntI = flowDataIntI[BNumFlowCells + i];
953 float flowBPrI = flowDataPrI[BNumFlowCells + i];
954 dist += jointLookUp[flowAIntI * NUMBINS + flowBIntI] - flowAPrI - flowBPrI;
957 dist /= (float) minLength;
960 catch(exception& e) {
961 m->errorOut(e, "ShhherCommand", "calcPairwiseDist");
966 /**************************************************************************************************/
968 void ShhherCommand::flowDistParentFork(string distFileName, int startSeq, int stopSeq){
971 ostringstream outStream;
972 outStream.setf(ios::fixed, ios::floatfield);
973 outStream.setf(ios::dec, ios::basefield);
974 outStream.setf(ios::showpoint);
975 outStream.precision(6);
977 int begTime = time(NULL);
978 double begClock = clock();
980 for(int i=startSeq;i<stopSeq;i++){
981 for(int j=0;j<i;j++){
982 float flowDistance = calcPairwiseDist(mapUniqueToSeq[i], mapUniqueToSeq[j]);
984 if(flowDistance < 1e-6){
985 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << 0.000000 << endl;
987 else if(flowDistance <= cutoff){
988 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << flowDistance << endl;
992 m->mothurOut(toString(i) + "\t" + toString(time(NULL) - begTime));
993 m->mothurOut("\t" + toString((clock()-begClock)/CLOCKS_PER_SEC));
994 m->mothurOutEndLine();
997 m->mothurOut(toString(stopSeq-1) + "\t" + toString(time(NULL) - begTime));
998 m->mothurOut("\t" + toString((clock()-begClock)/CLOCKS_PER_SEC));
999 m->mothurOutEndLine();
1001 ofstream distFile(distFileName.c_str());
1002 distFile << outStream.str();
1005 catch(exception& e) {
1006 m->errorOut(e, "ShhherCommand", "flowDistParentFork");
1011 /**************************************************************************************************/
1013 string ShhherCommand::createDistFile(int processors){
1015 string fDistFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.dist";
1017 unsigned long int begTime = time(NULL);
1018 double begClock = clock();
1023 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
1024 if(processors == 1) { flowDistParentFork(fDistFileName, 0, numUniques); }
1025 else{ //you have multiple processors
1027 if (numSeqs < processors){ processors = 1; }
1029 vector<int> start(processors, 0);
1030 vector<int> end(processors, 0);
1032 for (int i = 0; i < processors; i++) {
1033 start[i] = int(sqrt(float(i)/float(processors)) * numUniques);
1034 end[i] = int(sqrt(float(i+1)/float(processors)) * numUniques);
1038 vector<int> processIDs;
1040 //loop through and create all the processes you want
1041 while (process != processors) {
1045 processIDs.push_back(pid); //create map from line number to pid so you can append files in correct order later
1047 }else if (pid == 0){
1048 flowDistParentFork(fDistFileName + toString(getpid()) + ".temp", start[process], end[process]);
1051 m->mothurOut("[ERROR]: unable to spawn the necessary processes. Error code: " + toString(pid)); m->mothurOutEndLine();
1053 for (int i=0;i<processIDs.size();i++) { int temp = processIDs[i]; kill (temp, SIGINT); }
1058 //parent does its part
1059 flowDistParentFork(fDistFileName, start[0], end[0]);
1061 //force parent to wait until all the processes are done
1062 for (int i=0;i<processIDs.size();i++) {
1063 int temp = processIDs[i];
1067 //append and remove temp files
1068 for (int i=0;i<processIDs.size();i++) {
1069 m->appendFiles((fDistFileName + toString(processIDs[i]) + ".temp"), fDistFileName);
1070 remove((fDistFileName + toString(processIDs[i]) + ".temp").c_str());
1076 flowDistParentFork(fDistFileName, 0, numUniques);
1079 m->mothurOutEndLine();
1081 m->mothurOut("Total time: " + toString(time(NULL) - begTime) + '\t' + toString((clock() - begClock)/CLOCKS_PER_SEC) + '\n');
1084 return fDistFileName;
1086 catch(exception& e) {
1087 m->errorOut(e, "ShhherCommand", "createDistFile");
1093 /**************************************************************************************************/
1095 string ShhherCommand::createNamesFile(){
1098 vector<string> duplicateNames(numUniques, "");
1099 for(int i=0;i<numSeqs;i++){
1100 duplicateNames[mapSeqToUnique[i]] += seqNameVector[i] + ',';
1103 string nameFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.names";
1106 m->openOutputFile(nameFileName, nameFile);
1108 for(int i=0;i<numUniques;i++){
1109 // nameFile << seqNameVector[mapUniqueToSeq[i]] << '\t' << duplicateNames[i].substr(0, duplicateNames[i].find_last_of(',')) << endl;
1110 nameFile << mapUniqueToSeq[i] << '\t' << duplicateNames[i].substr(0, duplicateNames[i].find_last_of(',')) << endl;
1114 return nameFileName;
1116 catch(exception& e) {
1117 m->errorOut(e, "ShhherCommand", "createNamesFile");
1122 //**********************************************************************************************************************
1124 string ShhherCommand::cluster(string distFileName, string namesFileName){
1127 ReadMatrix* read = new ReadColumnMatrix(distFileName);
1128 read->setCutoff(cutoff);
1130 NameAssignment* clusterNameMap = new NameAssignment(namesFileName);
1131 clusterNameMap->readMap();
1132 read->read(clusterNameMap);
1134 ListVector* list = read->getListVector();
1135 SparseMatrix* matrix = read->getMatrix();
1138 delete clusterNameMap;
1140 RAbundVector* rabund = new RAbundVector(list->getRAbundVector());
1142 Cluster* cluster = new CompleteLinkage(rabund, list, matrix, cutoff, "furthest");
1143 string tag = cluster->getTag();
1145 double clusterCutoff = cutoff;
1146 while (matrix->getSmallDist() <= clusterCutoff && matrix->getNNodes() > 0){
1147 cluster->update(clusterCutoff);
1150 list->setLabel(toString(cutoff));
1152 string listFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.list";
1154 m->openOutputFile(listFileName, listFile);
1155 list->print(listFile);
1158 delete matrix; delete cluster; delete rabund; delete list;
1160 return listFileName;
1162 catch(exception& e) {
1163 m->errorOut(e, "ShhherCommand", "cluster");
1168 /**************************************************************************************************/
1170 void ShhherCommand::getOTUData(string listFileName){
1174 m->openInputFile(listFileName, listFile);
1177 listFile >> label >> numOTUs;
1179 otuData.assign(numSeqs, 0);
1180 cumNumSeqs.assign(numOTUs, 0);
1181 nSeqsPerOTU.assign(numOTUs, 0);
1182 aaP.clear();aaP.resize(numOTUs);
1188 string singleOTU = "";
1190 for(int i=0;i<numOTUs;i++){
1192 listFile >> singleOTU;
1194 istringstream otuString(singleOTU);
1198 string seqName = "";
1200 for(int j=0;j<singleOTU.length();j++){
1201 char letter = otuString.get();
1207 map<string,int>::iterator nmIt = nameMap.find(seqName);
1208 int index = nmIt->second;
1210 nameMap.erase(nmIt);
1214 aaP[i].push_back(index);
1219 map<string,int>::iterator nmIt = nameMap.find(seqName);
1221 int index = nmIt->second;
1222 nameMap.erase(nmIt);
1226 aaP[i].push_back(index);
1231 sort(aaP[i].begin(), aaP[i].end());
1232 for(int j=0;j<nSeqsPerOTU[i];j++){
1233 seqNumber.push_back(aaP[i][j]);
1235 for(int j=nSeqsPerOTU[i];j<numSeqs;j++){
1236 aaP[i].push_back(0);
1242 for(int i=1;i<numOTUs;i++){
1243 cumNumSeqs[i] = cumNumSeqs[i-1] + nSeqsPerOTU[i-1];
1246 seqIndex = seqNumber;
1251 catch(exception& e) {
1252 m->errorOut(e, "ShhherCommand", "getOTUData");
1257 /**************************************************************************************************/
1259 void ShhherCommand::initPyroCluster(){
1261 dist.assign(numSeqs * numOTUs, 0);
1262 change.assign(numOTUs, 1);
1263 centroids.assign(numOTUs, -1);
1264 weight.assign(numOTUs, 0);
1265 singleTau.assign(numSeqs, 1.0);
1267 nSeqsBreaks.assign(processors+1, 0);
1268 nOTUsBreaks.assign(processors+1, 0);
1271 for(int i=0;i<processors;i++){
1272 nSeqsBreaks[i+1] = nSeqsBreaks[i] + (int)((double) numSeqs / (double) processors);
1273 nOTUsBreaks[i+1] = nOTUsBreaks[i] + (int)((double) numOTUs / (double) processors);
1275 nSeqsBreaks[processors] = numSeqs;
1276 nOTUsBreaks[processors] = numOTUs;
1278 catch(exception& e) {
1279 m->errorOut(e, "ShhherCommand", "initPyroCluster");
1284 /**************************************************************************************************/
1286 void ShhherCommand::fill(){
1289 for(int i=0;i<numOTUs;i++){
1290 cumNumSeqs[i] = index;
1291 for(int j=0;j<nSeqsPerOTU[i];j++){
1292 seqNumber[index] = aaP[i][j];
1293 seqIndex[index] = aaI[i][j];
1299 catch(exception& e) {
1300 m->errorOut(e, "ShhherCommand", "fill");
1305 /**************************************************************************************************/
1307 void ShhherCommand::calcCentroids(){
1310 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
1312 if(processors == 1) {
1313 calcCentroidsDriver(0, numOTUs);
1315 else{ //you have multiple processors
1316 if (numOTUs < processors){ processors = 1; }
1319 vector<int> processIDs;
1321 //loop through and create all the processes you want
1322 while (process != processors) {
1326 processIDs.push_back(pid); //create map from line number to pid so you can append files in correct order later
1328 }else if (pid == 0){
1329 calcCentroidsDriver(nOTUsBreaks[process], nOTUsBreaks[process+1]);
1332 m->mothurOut("[ERROR]: unable to spawn the necessary processes. Error code: " + toString(pid)); m->mothurOutEndLine();
1334 for (int i=0;i<processIDs.size();i++) { int temp = processIDs[i]; kill (temp, SIGINT); }
1339 //parent does its part
1340 calcCentroidsDriver(nOTUsBreaks[0], nOTUsBreaks[1]);
1342 //force parent to wait until all the processes are done
1343 for (int i=0;i<processIDs.size();i++) {
1344 int temp = processIDs[i];
1350 calcCentroidsDriver(0, numOTUs);
1353 catch(exception& e) {
1354 m->errorOut(e, "ShhherCommand", "calcCentroidsDriver");
1359 /**************************************************************************************************/
1361 void ShhherCommand::calcCentroidsDriver(int start, int finish){
1363 //this function gets the most likely homopolymer length at a flow position for a group of sequences
1369 for(int i=start;i<finish;i++){
1373 int minFlowGram = 100000000;
1374 double minFlowValue = 1e8;
1375 change[i] = 0; //FALSE
1377 for(int j=0;j<nSeqsPerOTU[i];j++){
1378 count += singleTau[seqNumber[cumNumSeqs[i] + j]];
1381 if(nSeqsPerOTU[i] > 0 && count > MIN_COUNT){
1382 vector<double> adF(nSeqsPerOTU[i]);
1383 vector<int> anL(nSeqsPerOTU[i]);
1385 for(int j=0;j<nSeqsPerOTU[i];j++){
1386 int index = cumNumSeqs[i] + j;
1387 int nI = seqIndex[index];
1388 int nIU = mapSeqToUnique[nI];
1391 for(k=0;k<position;k++){
1397 anL[position] = nIU;
1398 adF[position] = 0.0000;
1403 for(int j=0;j<nSeqsPerOTU[i];j++){
1404 int index = cumNumSeqs[i] + j;
1405 int nI = seqIndex[index];
1407 double tauValue = singleTau[seqNumber[index]];
1409 for(int k=0;k<position;k++){
1410 double dist = getDistToCentroid(anL[k], nI, lengths[nI]);
1411 adF[k] += dist * tauValue;
1415 for(int j=0;j<position;j++){
1416 if(adF[j] < minFlowValue){
1418 minFlowValue = adF[j];
1422 if(centroids[i] != anL[minFlowGram]){
1424 centroids[i] = anL[minFlowGram];
1427 else if(centroids[i] != -1){
1433 catch(exception& e) {
1434 m->errorOut(e, "ShhherCommand", "calcCentroidsDriver");
1439 /**************************************************************************************************/
1441 double ShhherCommand::getDistToCentroid(int cent, int flow, int length){
1444 int flowAValue = cent * numFlowCells;
1445 int flowBValue = flow * numFlowCells;
1449 for(int i=0;i<length;i++){
1450 dist += singleLookUp[uniqueFlowgrams[flowAValue] * NUMBINS + flowDataIntI[flowBValue]];
1455 return dist / (double)length;
1457 catch(exception& e) {
1458 m->errorOut(e, "ShhherCommand", "getDistToCentroid");
1463 /**************************************************************************************************/
1465 double ShhherCommand::getNewWeights(){
1468 double maxChange = 0;
1470 for(int i=0;i<numOTUs;i++){
1472 double difference = weight[i];
1475 for(int j=0;j<nSeqsPerOTU[i];j++){
1476 int index = cumNumSeqs[i] + j;
1477 double tauValue = singleTau[seqNumber[index]];
1478 weight[i] += tauValue;
1481 difference = fabs(weight[i] - difference);
1482 if(difference > maxChange){ maxChange = difference; }
1486 catch(exception& e) {
1487 m->errorOut(e, "ShhherCommand", "getNewWeights");
1492 /**************************************************************************************************/
1494 double ShhherCommand::getLikelihood(){
1498 vector<long double> P(numSeqs, 0);
1501 for(int i=0;i<numOTUs;i++){
1502 if(weight[i] > MIN_WEIGHT){
1508 for(int i=0;i<numOTUs;i++){
1509 for(int j=0;j<nSeqsPerOTU[i];j++){
1510 int index = cumNumSeqs[i] + j;
1511 int nI = seqIndex[index];
1512 double singleDist = dist[seqNumber[index]];
1514 P[nI] += weight[i] * exp(-singleDist * sigma);
1518 for(int i=0;i<numSeqs;i++){
1519 if(P[i] == 0){ P[i] = DBL_EPSILON; }
1524 nLL = nLL -(double)numSeqs * log(sigma);
1528 catch(exception& e) {
1529 m->errorOut(e, "ShhherCommand", "getNewWeights");
1534 /**************************************************************************************************/
1536 void ShhherCommand::checkCentroids(){
1538 vector<int> unique(numOTUs, 1);
1540 for(int i=0;i<numOTUs;i++){
1541 if(centroids[i] == -1 || weight[i] < MIN_WEIGHT){
1546 for(int i=0;i<numOTUs;i++){
1548 for(int j=i+1;j<numOTUs;j++){
1551 if(centroids[j] == centroids[i]){
1555 weight[i] += weight[j];
1563 catch(exception& e) {
1564 m->errorOut(e, "ShhherCommand", "checkCentroids");
1569 /**************************************************************************************************/
1571 void ShhherCommand::calcNewDistances(){
1574 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
1576 if(processors == 1) {
1577 calcNewDistancesParent(0, numSeqs);
1579 else{ //you have multiple processors
1580 if (numSeqs < processors){ processors = 1; }
1582 vector<vector<int> > child_otuIndex(processors);
1583 vector<vector<int> > child_seqIndex(processors);
1584 vector<vector<double> > child_singleTau(processors);
1585 vector<int> totals(processors);
1588 vector<int> processIDs;
1590 //loop through and create all the processes you want
1591 while (process != processors) {
1595 processIDs.push_back(pid); //create map from line number to pid so you can append files in correct order later
1597 }else if (pid == 0){
1598 calcNewDistancesChild(nSeqsBreaks[process], nSeqsBreaks[process+1], child_otuIndex[process], child_seqIndex[process], child_singleTau[process]);
1599 totals[process] = child_otuIndex[process].size();
1603 m->mothurOut("[ERROR]: unable to spawn the necessary processes. Error code: " + toString(pid)); m->mothurOutEndLine();
1605 for (int i=0;i<processIDs.size();i++) { int temp = processIDs[i]; kill (temp, SIGINT); }
1610 //parent does its part
1611 calcNewDistancesParent(nSeqsBreaks[0], nSeqsBreaks[1]);
1612 int total = seqIndex.size();
1614 //force parent to wait until all the processes are done
1615 for (int i=0;i<processIDs.size();i++) {
1616 int temp = processIDs[i];
1620 for(int i=1;i<processors;i++){
1621 int oldTotal = total;
1624 singleTau.resize(total, 0);
1625 seqIndex.resize(total, 0);
1626 seqNumber.resize(total, 0);
1630 for(int j=oldTotal;j<total;j++){
1631 int otuI = child_otuIndex[i][childIndex];
1632 int seqI = child_seqIndex[i][childIndex];
1634 singleTau[j] = child_singleTau[i][childIndex];
1635 aaP[otuI][nSeqsPerOTU[otuI]] = j;
1636 aaI[otuI][nSeqsPerOTU[otuI]] = seqI;
1637 nSeqsPerOTU[otuI]++;
1644 calcNewDistancesParent(0, numSeqs);
1647 catch(exception& e) {
1648 m->errorOut(e, "ShhherCommand", "calcNewDistances");
1653 /**************************************************************************************************/
1655 void ShhherCommand::calcNewDistancesChildMPI(int startSeq, int stopSeq, vector<int>& otuIndex){
1658 vector<double> newTau(numOTUs,0);
1659 vector<double> norms(numSeqs, 0);
1666 for(int i=startSeq;i<stopSeq;i++){
1667 double offset = 1e8;
1668 int indexOffset = i * numOTUs;
1670 for(int j=0;j<numOTUs;j++){
1672 if(weight[j] > MIN_WEIGHT && change[j] == 1){
1673 dist[indexOffset + j] = getDistToCentroid(centroids[j], i, lengths[i]);
1675 if(weight[j] > MIN_WEIGHT && dist[indexOffset + j] < offset){
1676 offset = dist[indexOffset + j];
1680 for(int j=0;j<numOTUs;j++){
1681 if(weight[j] > MIN_WEIGHT){
1682 newTau[j] = exp(sigma * (-dist[indexOffset + j] + offset)) * weight[j];
1683 norms[i] += newTau[j];
1690 for(int j=0;j<numOTUs;j++){
1692 newTau[j] /= norms[i];
1694 if(newTau[j] > MIN_TAU){
1695 otuIndex.push_back(j);
1696 seqIndex.push_back(i);
1697 singleTau.push_back(newTau[j]);
1703 catch(exception& e) {
1704 m->errorOut(e, "ShhherCommand", "calcNewDistancesChildMPI");
1709 /**************************************************************************************************/
1711 void ShhherCommand::calcNewDistancesChild(int startSeq, int stopSeq, vector<int>& child_otuIndex, vector<int>& child_seqIndex, vector<double>& child_singleTau){
1714 vector<double> newTau(numOTUs,0);
1715 vector<double> norms(numSeqs, 0);
1716 child_otuIndex.resize(0);
1717 child_seqIndex.resize(0);
1718 child_singleTau.resize(0);
1720 for(int i=startSeq;i<stopSeq;i++){
1721 double offset = 1e8;
1722 int indexOffset = i * numOTUs;
1725 for(int j=0;j<numOTUs;j++){
1726 if(weight[j] > MIN_WEIGHT && change[j] == 1){
1727 dist[indexOffset + j] = getDistToCentroid(centroids[j], i, lengths[i]);
1730 if(weight[j] > MIN_WEIGHT && dist[indexOffset + j] < offset){
1731 offset = dist[indexOffset + j];
1735 for(int j=0;j<numOTUs;j++){
1736 if(weight[j] > MIN_WEIGHT){
1737 newTau[j] = exp(sigma * (-dist[indexOffset + j] + offset)) * weight[j];
1738 norms[i] += newTau[j];
1745 for(int j=0;j<numOTUs;j++){
1746 newTau[j] /= norms[i];
1748 if(newTau[j] > MIN_TAU){
1749 child_otuIndex.push_back(j);
1750 child_seqIndex.push_back(i);
1751 child_singleTau.push_back(newTau[j]);
1756 catch(exception& e) {
1757 m->errorOut(e, "ShhherCommand", "calcNewDistancesChild");
1762 /**************************************************************************************************/
1764 void ShhherCommand::calcNewDistancesParent(int startSeq, int stopSeq){
1769 vector<double> newTau(numOTUs,0);
1770 vector<double> norms(numSeqs, 0);
1771 nSeqsPerOTU.assign(numOTUs, 0);
1773 for(int i=startSeq;i<stopSeq;i++){
1774 int indexOffset = i * numOTUs;
1776 double offset = 1e8;
1778 for(int j=0;j<numOTUs;j++){
1779 if(weight[j] > MIN_WEIGHT && change[j] == 1){
1780 dist[indexOffset + j] = getDistToCentroid(centroids[j], i, lengths[i]);
1783 if(weight[j] > MIN_WEIGHT && dist[indexOffset + j] < offset){
1784 offset = dist[indexOffset + j];
1788 for(int j=0;j<numOTUs;j++){
1789 if(weight[j] > MIN_WEIGHT){
1790 newTau[j] = exp(sigma * (-dist[indexOffset + j] + offset)) * weight[j];
1791 norms[i] += newTau[j];
1798 for(int j=0;j<numOTUs;j++){
1799 newTau[j] /= norms[i];
1802 for(int j=0;j<numOTUs;j++){
1803 if(newTau[j] > MIN_TAU){
1805 int oldTotal = total;
1809 singleTau.resize(total, 0);
1810 seqNumber.resize(total, 0);
1811 seqIndex.resize(total, 0);
1813 singleTau[oldTotal] = newTau[j];
1815 aaP[j][nSeqsPerOTU[j]] = oldTotal;
1816 aaI[j][nSeqsPerOTU[j]] = i;
1822 catch(exception& e) {
1823 m->errorOut(e, "ShhherCommand", "calcNewDistancesParent");
1828 /**************************************************************************************************/
1830 void ShhherCommand::setOTUs(){
1833 vector<double> bigTauMatrix(numOTUs * numSeqs, 0.0000);
1835 for(int i=0;i<numOTUs;i++){
1836 for(int j=0;j<nSeqsPerOTU[i];j++){
1837 int index = cumNumSeqs[i] + j;
1838 double tauValue = singleTau[seqNumber[index]];
1839 int sIndex = seqIndex[index];
1840 bigTauMatrix[sIndex * numOTUs + i] = tauValue;
1844 for(int i=0;i<numSeqs;i++){
1845 double maxTau = -1.0000;
1847 for(int j=0;j<numOTUs;j++){
1848 if(bigTauMatrix[i * numOTUs + j] > maxTau){
1849 maxTau = bigTauMatrix[i * numOTUs + j];
1854 otuData[i] = maxOTU;
1857 nSeqsPerOTU.assign(numOTUs, 0);
1859 for(int i=0;i<numSeqs;i++){
1860 int index = otuData[i];
1862 singleTau[i] = 1.0000;
1865 aaP[index][nSeqsPerOTU[index]] = i;
1866 aaI[index][nSeqsPerOTU[index]] = i;
1868 nSeqsPerOTU[index]++;
1872 catch(exception& e) {
1873 m->errorOut(e, "ShhherCommand", "calcNewDistances");
1878 /**************************************************************************************************/
1880 void ShhherCommand::writeQualities(vector<int> otuCounts){
1883 string qualityFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.qual";
1885 ofstream qualityFile;
1886 m->openOutputFile(qualityFileName, qualityFile);
1888 qualityFile.setf(ios::fixed, ios::floatfield);
1889 qualityFile.setf(ios::showpoint);
1890 qualityFile << setprecision(6);
1892 vector<vector<int> > qualities(numOTUs);
1893 vector<double> pr(HOMOPS, 0);
1896 for(int i=0;i<numOTUs;i++){
1900 if(nSeqsPerOTU[i] > 0){
1901 qualities[i].assign(1024, -1);
1903 while(index < numFlowCells){
1904 double maxPrValue = 1e8;
1905 short maxPrIndex = -1;
1906 double count = 0.0000;
1908 pr.assign(HOMOPS, 0);
1910 for(int j=0;j<nSeqsPerOTU[i];j++){
1911 int lIndex = cumNumSeqs[i] + j;
1912 double tauValue = singleTau[seqNumber[lIndex]];
1913 int sequenceIndex = aaI[i][j];
1914 short intensity = flowDataIntI[sequenceIndex * numFlowCells + index];
1918 for(int s=0;s<HOMOPS;s++){
1919 pr[s] += tauValue * singleLookUp[s * NUMBINS + intensity];
1923 maxPrIndex = uniqueFlowgrams[centroids[i] * numFlowCells + index];
1924 maxPrValue = pr[maxPrIndex];
1926 if(count > MIN_COUNT){
1928 double norm = 0.0000;
1930 for(int s=0;s<HOMOPS;s++){
1931 norm += exp(-(pr[s] - maxPrValue));
1934 for(int s=1;s<=maxPrIndex;s++){
1936 double temp = 0.0000;
1938 U += exp(-(pr[s-1]-maxPrValue))/norm;
1946 temp = floor(-10 * temp);
1947 value = (int)floor(temp);
1948 if(value > 100){ value = 100; }
1950 qualities[i][base] = (int)value;
1960 if(otuCounts[i] > 0){
1961 qualityFile << '>' << seqNameVector[mapUniqueToSeq[i]] << endl;
1963 int j=4; //need to get past the first four bases
1964 while(qualities[i][j] != -1){
1965 qualityFile << qualities[i][j] << ' ';
1968 qualityFile << endl;
1971 qualityFile.close();
1974 catch(exception& e) {
1975 m->errorOut(e, "ShhherCommand", "writeQualities");
1980 /**************************************************************************************************/
1982 void ShhherCommand::writeSequences(vector<int> otuCounts){
1985 string fastaFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.fasta";
1987 m->openOutputFile(fastaFileName, fastaFile);
1989 vector<string> names(numOTUs, "");
1991 for(int i=0;i<numOTUs;i++){
1992 int index = centroids[i];
1994 if(otuCounts[i] > 0){
1995 fastaFile << '>' << seqNameVector[aaI[i][0]] << endl;
1999 for(int j=0;j<numFlowCells;j++){
2001 char base = flowOrder[j % 4];
2002 for(int k=0;k<uniqueFlowgrams[index * numFlowCells + j];k++){
2007 fastaFile << newSeq.substr(4) << endl;
2012 if(compositeFASTAFileName != ""){
2013 m->appendFiles(fastaFileName, compositeFASTAFileName);
2016 catch(exception& e) {
2017 m->errorOut(e, "ShhherCommand", "writeSequences");
2022 /**************************************************************************************************/
2024 void ShhherCommand::writeNames(vector<int> otuCounts){
2026 string nameFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.final.names";
2028 m->openOutputFile(nameFileName, nameFile);
2030 for(int i=0;i<numOTUs;i++){
2031 if(otuCounts[i] > 0){
2032 nameFile << seqNameVector[aaI[i][0]] << '\t' << seqNameVector[aaI[i][0]];
2034 for(int j=1;j<nSeqsPerOTU[i];j++){
2035 nameFile << ',' << seqNameVector[aaI[i][j]];
2043 catch(exception& e) {
2044 m->errorOut(e, "ShhherCommand", "writeNames");
2049 /**************************************************************************************************/
2051 void ShhherCommand::writeGroups(){
2053 string fileRoot = flowFileName.substr(0,flowFileName.find_last_of('.'));
2054 string groupFileName = fileRoot + ".pn.groups";
2056 m->openOutputFile(groupFileName, groupFile);
2058 for(int i=0;i<numSeqs;i++){
2059 groupFile << seqNameVector[i] << '\t' << fileRoot << endl;
2063 catch(exception& e) {
2064 m->errorOut(e, "ShhherCommand", "writeGroups");
2069 /**************************************************************************************************/
2071 void ShhherCommand::writeClusters(vector<int> otuCounts){
2073 string otuCountsFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.counts";
2074 ofstream otuCountsFile;
2075 m->openOutputFile(otuCountsFileName, otuCountsFile);
2077 string bases = flowOrder;
2079 for(int i=0;i<numOTUs;i++){
2080 //output the translated version of the centroid sequence for the otu
2081 if(otuCounts[i] > 0){
2082 int index = centroids[i];
2084 otuCountsFile << "ideal\t";
2085 for(int j=8;j<numFlowCells;j++){
2086 char base = bases[j % 4];
2087 for(int s=0;s<uniqueFlowgrams[index * numFlowCells + j];s++){
2088 otuCountsFile << base;
2091 otuCountsFile << endl;
2093 for(int j=0;j<nSeqsPerOTU[i];j++){
2094 int sequence = aaI[i][j];
2095 otuCountsFile << seqNameVector[sequence] << '\t';
2099 for(int k=0;k<lengths[sequence];k++){
2100 char base = bases[k % 4];
2101 int freq = int(0.01 * (double)flowDataIntI[sequence * numFlowCells + k] + 0.5);
2103 for(int s=0;s<freq;s++){
2105 //otuCountsFile << base;
2108 otuCountsFile << newSeq.substr(4) << endl;
2110 otuCountsFile << endl;
2113 otuCountsFile.close();
2115 catch(exception& e) {
2116 m->errorOut(e, "ShhherCommand", "writeClusters");
2121 //**********************************************************************************************************************