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
30 //**********************************************************************************************************************
32 vector<string> ShhherCommand::getValidParameters(){
35 "file", "flow", "lookup", "cutoff", "sigma", "outputdir","inputdir", "processors", "maxiter", "mindelta", "order"
38 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
42 m->errorOut(e, "ShhherCommand", "getValidParameters");
47 //**********************************************************************************************************************
49 ShhherCommand::ShhherCommand(){
51 abort = true; calledHelp = true;
53 //initialize outputTypes
54 vector<string> tempOutNames;
55 outputTypes["pn.dist"] = tempOutNames;
59 m->errorOut(e, "ShhherCommand", "ShhherCommand");
64 //**********************************************************************************************************************
66 vector<string> ShhherCommand::getRequiredParameters(){
68 string Array[] = {"flow"};
69 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
73 m->errorOut(e, "ShhherCommand", "getRequiredParameters");
78 //**********************************************************************************************************************
80 vector<string> ShhherCommand::getRequiredFiles(){
82 vector<string> myArray;
86 m->errorOut(e, "ShhherCommand", "getRequiredFiles");
91 //**********************************************************************************************************************
93 ShhherCommand::ShhherCommand(string option) {
97 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
98 MPI_Comm_size(MPI_COMM_WORLD, &ncpus);
104 abort = false; calledHelp = false;
107 //allow user to run help
108 if(option == "help") { help(); abort = true; calledHelp = true; }
112 //valid paramters for this command
113 string AlignArray[] = {
114 "file", "flow", "lookup", "cutoff", "sigma", "outputdir","inputdir", "processors", "maxiter", "mindelta", "order"
117 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
119 OptionParser parser(option);
120 map<string,string> parameters = parser.getParameters();
122 ValidParameters validParameter;
123 map<string,string>::iterator it;
125 //check to make sure all parameters are valid for command
126 for (it = parameters.begin(); it != parameters.end(); it++) {
127 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
130 //initialize outputTypes
131 vector<string> tempOutNames;
132 outputTypes["pn.dist"] = tempOutNames;
133 // outputTypes["fasta"] = tempOutNames;
135 //if the user changes the input directory command factory will send this info to us in the output parameter
136 string inputDir = validParameter.validFile(parameters, "inputdir", false);
137 if (inputDir == "not found"){ inputDir = ""; }
140 it = parameters.find("flow");
141 //user has given a template file
142 if(it != parameters.end()){
143 path = m->hasPath(it->second);
144 //if the user has not given a path then, add inputdir. else leave path alone.
145 if (path == "") { parameters["flow"] = inputDir + it->second; }
148 it = parameters.find("lookup");
149 //user has given a template file
150 if(it != parameters.end()){
151 path = m->hasPath(it->second);
152 //if the user has not given a path then, add inputdir. else leave path alone.
153 if (path == "") { parameters["lookup"] = inputDir + it->second; }
156 it = parameters.find("file");
157 //user has given a template file
158 if(it != parameters.end()){
159 path = m->hasPath(it->second);
160 //if the user has not given a path then, add inputdir. else leave path alone.
161 if (path == "") { parameters["file"] = inputDir + it->second; }
166 //check for required parameters
167 flowFileName = validParameter.validFile(parameters, "flow", true);
168 flowFilesFileName = validParameter.validFile(parameters, "file", true);
169 if (flowFileName == "not found" && flowFilesFileName == "not found") {
170 m->mothurOut("values for either flow or file must be provided for the shhh.seqs command.");
171 m->mothurOutEndLine();
174 else if (flowFileName == "not open" || flowFilesFileName == "not open") { abort = true; }
176 if(flowFileName != "not found"){ compositeFASTAFileName = ""; }
178 compositeFASTAFileName = flowFilesFileName.substr(0, flowFilesFileName.length()-10) + "pn.fasta";
180 m->openOutputFile(compositeFASTAFileName, temp);
184 //if the user changes the output directory command factory will send this info to us in the output parameter
185 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
187 outputDir += m->hasPath(flowFileName); //if user entered a file with a path then preserve it
191 //check for optional parameter and set defaults
192 // ...at some point should added some additional type checking...
194 temp = validParameter.validFile(parameters, "lookup", true);
195 if (temp == "not found") { lookupFileName = "LookUp_Titanium.pat"; }
196 else if(temp == "not open") { abort = true; }
197 else { lookupFileName = temp; }
199 temp = validParameter.validFile(parameters, "processors", false);if (temp == "not found"){ temp = "1"; }
200 convert(temp, processors);
202 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found"){ temp = "0.01"; }
203 convert(temp, cutoff);
205 temp = validParameter.validFile(parameters, "mindelta", false); if (temp == "not found"){ temp = "0.000001"; }
206 convert(temp, minDelta);
208 temp = validParameter.validFile(parameters, "maxiter", false); if (temp == "not found"){ temp = "1000"; }
209 convert(temp, maxIters);
211 temp = validParameter.validFile(parameters, "sigma", false);if (temp == "not found") { temp = "60"; }
212 convert(temp, sigma);
214 flowOrder = validParameter.validFile(parameters, "order", false);
215 if (flowOrder == "not found"){ flowOrder = "TACG"; }
216 else if(flowOrder.length() != 4){
217 m->mothurOut("The value of the order option must be four bases long\n");
220 globaldata = GlobalData::getInstance();
228 catch(exception& e) {
229 m->errorOut(e, "ShhherCommand", "ShhherCommand");
234 //**********************************************************************************************************************
236 ShhherCommand::~ShhherCommand(){}
238 //**********************************************************************************************************************
240 void ShhherCommand::help(){
242 m->mothurOut("The shhher command reads a file containing flowgrams and creates a file of corrected sequences.\n");
244 catch(exception& e) {
245 m->errorOut(e, "ShhherCommand", "help");
250 //**********************************************************************************************************************
252 int ShhherCommand::execute(){
254 if (abort == true) { if (calledHelp) { return 0; } return 2; }
261 for(int i=1;i<ncpus;i++){
262 MPI_Send(&abort, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
264 if(abort == 1){ return 0; }
268 m->mothurOut("\nGetting preliminary data...\n");
272 vector<string> flowFileVector;
273 if(flowFilesFileName != "not found"){
276 ifstream flowFilesFile;
277 m->openInputFile(flowFilesFileName, flowFilesFile);
278 while(flowFilesFile){
279 flowFilesFile >> fName;
280 flowFileVector.push_back(fName);
281 m->gobble(flowFilesFile);
285 flowFileVector.push_back(flowFileName);
287 int numFiles = flowFileVector.size();
289 for(int i=1;i<ncpus;i++){
290 MPI_Send(&numFiles, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
293 for(int i=0;i<numFiles;i++){
294 double begClock = clock();
295 unsigned long int begTime = time(NULL);
297 flowFileName = flowFileVector[i];
299 m->mothurOut("\n>>>>>\tProcessing " + flowFileName + " (file " + toString(i+1) + " of " + toString(numFiles) + ")\t<<<<<\n");
300 m->mothurOut("Reading flowgrams...\n");
303 m->mothurOut("Identifying unique flowgrams...\n");
306 m->mothurOut("Calculating distances between flowgrams...\n");
308 strcpy(fileName, flowFileName.c_str());
310 for(int i=1;i<ncpus;i++){
311 MPI_Send(&fileName[0], 1024, MPI_CHAR, i, tag, MPI_COMM_WORLD);
313 MPI_Send(&numSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
314 MPI_Send(&numUniques, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
315 MPI_Send(&numFlowCells, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
316 MPI_Send(&flowDataIntI[0], numSeqs * numFlowCells, MPI_SHORT, i, tag, MPI_COMM_WORLD);
317 MPI_Send(&flowDataPrI[0], numSeqs * numFlowCells, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
318 MPI_Send(&mapUniqueToSeq[0], numSeqs, MPI_INT, i, tag, MPI_COMM_WORLD);
319 MPI_Send(&mapSeqToUnique[0], numSeqs, MPI_INT, i, tag, MPI_COMM_WORLD);
320 MPI_Send(&lengths[0], numSeqs, MPI_INT, i, tag, MPI_COMM_WORLD);
321 MPI_Send(&jointLookUp[0], NUMBINS * NUMBINS, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
322 MPI_Send(&cutoff, 1, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
325 string distFileName = flowDistMPI(0, int(sqrt(1.0/float(ncpus)) * numUniques));
328 for(int i=1;i<ncpus;i++){
329 MPI_Recv(&done, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
331 m->appendFiles((distFileName + ".temp." + toString(i)), distFileName);
332 remove((distFileName + ".temp." + toString(i)).c_str());
335 string namesFileName = createNamesFile();
337 m->mothurOut("\nClustering flowgrams...\n");
338 string listFileName = cluster(distFileName, namesFileName);
340 getOTUData(listFileName);
343 for(int i=1;i<ncpus;i++){
344 MPI_Send(&numOTUs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
345 MPI_Send(&singleLookUp[0], singleLookUp.size(), MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
346 MPI_Send(&uniqueFlowgrams[0], numFlowCells * numUniques, MPI_SHORT, i, tag, MPI_COMM_WORLD);
347 MPI_Send(&sigma, 1, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
354 int numOTUsOnCPU = numOTUs / ncpus;
355 int numSeqsOnCPU = numSeqs / ncpus;
356 m->mothurOut("\nDenoising flowgrams...\n");
357 m->mothurOut("iter\tmaxDelta\tnLL\t\tcycletime\n");
359 while((maxIters == 0 && maxDelta > minDelta) || iter < MIN_ITER || (maxDelta > minDelta && iter < maxIters)){
361 double cycClock = clock();
362 unsigned long int cycTime = time(NULL);
365 int total = singleTau.size();
366 for(int i=1;i<ncpus;i++){
367 MPI_Send(&total, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
368 MPI_Send(&change[0], numOTUs, MPI_SHORT, i, tag, MPI_COMM_WORLD);
369 MPI_Send(¢roids[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
371 MPI_Send(&singleTau[0], total, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
372 MPI_Send(&seqNumber[0], total, MPI_INT, i, tag, MPI_COMM_WORLD);
373 MPI_Send(&seqIndex[0], total, MPI_INT, i, tag, MPI_COMM_WORLD);
374 MPI_Send(&nSeqsPerOTU[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
375 MPI_Send(&cumNumSeqs[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
378 calcCentroidsDriver(0, numOTUsOnCPU);
380 for(int i=1;i<ncpus;i++){
381 int otuStart = i * numOTUs / ncpus;
382 int otuStop = (i + 1) * numOTUs / ncpus;
384 vector<int> tempCentroids(numOTUs, 0);
385 vector<short> tempChange(numOTUs, 0);
387 MPI_Recv(&tempCentroids[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
388 MPI_Recv(&tempChange[0], numOTUs, MPI_SHORT, i, tag, MPI_COMM_WORLD, &status);
390 for(int j=otuStart;j<otuStop;j++){
391 centroids[j] = tempCentroids[j];
392 change[j] = tempChange[j];
396 maxDelta = getNewWeights();
397 double nLL = getLikelihood();
400 for(int i=1;i<ncpus;i++){
401 MPI_Send(¢roids[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
402 MPI_Send(&weight[0], numOTUs, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
403 MPI_Send(&change[0], numOTUs, MPI_SHORT, i, tag, MPI_COMM_WORLD);
406 calcNewDistancesParent(0, numSeqsOnCPU);
408 total = singleTau.size();
410 for(int i=1;i<ncpus;i++){
412 int seqStart = i * numSeqs / ncpus;
413 int seqStop = (i + 1) * numSeqs / ncpus;
415 MPI_Recv(&childTotal, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
417 vector<int> childSeqIndex(childTotal, 0);
418 vector<double> childSingleTau(childTotal, 0);
419 vector<double> childDist(numSeqs * numOTUs, 0);
420 vector<int> otuIndex(childTotal, 0);
422 MPI_Recv(&childSeqIndex[0], childTotal, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
423 MPI_Recv(&childSingleTau[0], childTotal, MPI_DOUBLE, i, tag, MPI_COMM_WORLD, &status);
424 MPI_Recv(&childDist[0], numOTUs * numSeqs, MPI_DOUBLE, i, tag, MPI_COMM_WORLD, &status);
425 MPI_Recv(&otuIndex[0], childTotal, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
427 int oldTotal = total;
429 singleTau.resize(total, 0);
430 seqIndex.resize(total, 0);
431 seqNumber.resize(total, 0);
435 for(int j=oldTotal;j<total;j++){
436 int otuI = otuIndex[childIndex];
437 int seqI = childSeqIndex[childIndex];
439 singleTau[j] = childSingleTau[childIndex];
441 aaP[otuI][nSeqsPerOTU[otuI]] = j;
442 aaI[otuI][nSeqsPerOTU[otuI]] = seqI;
447 int index = seqStart * numOTUs;
448 for(int j=seqStart;j<seqStop;j++){
449 for(int k=0;k<numOTUs;k++){
450 dist[index] = childDist[index];
458 m->mothurOut(toString(iter) + '\t' + toString(maxDelta) + '\t' + toString(nLL) + '\t' + toString(time(NULL) - cycTime) + '\t' + toString((clock() - cycClock)/(double)CLOCKS_PER_SEC) + '\n');
460 if((maxIters == 0 && maxDelta > minDelta) || iter < MIN_ITER || (maxDelta > minDelta && iter < maxIters)){
462 for(int i=1;i<ncpus;i++){
463 MPI_Send(&live, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
468 for(int i=1;i<ncpus;i++){
469 MPI_Send(&live, 1, MPI_INT, i, tag, MPI_COMM_WORLD); //send kill command
475 m->mothurOut("\nFinalizing...\n");
478 vector<int> otuCounts(numOTUs, 0);
479 for(int i=0;i<numSeqs;i++) { otuCounts[otuData[i]]++; }
480 calcCentroidsDriver(0, numOTUs);
481 writeQualities(otuCounts);
482 writeSequences(otuCounts);
483 writeNames(otuCounts);
484 writeClusters(otuCounts);
487 remove(distFileName.c_str());
488 remove(namesFileName.c_str());
489 remove(listFileName.c_str());
491 m->mothurOut("Total time to process " + toString(flowFileName) + ":\t" + toString(time(NULL) - begTime) + '\t' + toString((clock() - begClock)/(double)CLOCKS_PER_SEC) + '\n');
497 MPI_Recv(&abort, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
498 if(abort){ return 0; }
501 MPI_Recv(&numFiles, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
503 for(int i=0;i<numFiles;i++){
504 //Now into the pyrodist part
508 MPI_Recv(&fileName, 1024, MPI_CHAR, 0, tag, MPI_COMM_WORLD, &status);
509 MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
510 MPI_Recv(&numUniques, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
511 MPI_Recv(&numFlowCells, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
513 flowDataIntI.resize(numSeqs * numFlowCells);
514 flowDataPrI.resize(numSeqs * numFlowCells);
515 mapUniqueToSeq.resize(numSeqs);
516 mapSeqToUnique.resize(numSeqs);
517 lengths.resize(numSeqs);
518 jointLookUp.resize(NUMBINS * NUMBINS);
520 MPI_Recv(&flowDataIntI[0], numSeqs * numFlowCells, MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
521 MPI_Recv(&flowDataPrI[0], numSeqs * numFlowCells, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
522 MPI_Recv(&mapUniqueToSeq[0], numSeqs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
523 MPI_Recv(&mapSeqToUnique[0], numSeqs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
524 MPI_Recv(&lengths[0], numSeqs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
525 MPI_Recv(&jointLookUp[0], NUMBINS * NUMBINS, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
526 MPI_Recv(&cutoff, 1, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
528 flowFileName = string(fileName);
529 int flowDistStart = int(sqrt(float(pid)/float(ncpus)) * numUniques);
530 int flowDistEnd = int(sqrt(float(pid+1)/float(ncpus)) * numUniques);
532 string distanceStringChild = flowDistMPI(flowDistStart, flowDistEnd);
535 MPI_Send(&done, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
537 //Now into the pyronoise part
538 MPI_Recv(&numOTUs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
540 singleLookUp.resize(HOMOPS * NUMBINS);
541 uniqueFlowgrams.resize(numUniques * numFlowCells);
542 weight.resize(numOTUs);
543 centroids.resize(numOTUs);
544 change.resize(numOTUs);
545 dist.assign(numOTUs * numSeqs, 0);
546 nSeqsPerOTU.resize(numOTUs);
547 cumNumSeqs.resize(numOTUs);
549 MPI_Recv(&singleLookUp[0], singleLookUp.size(), MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
550 MPI_Recv(&uniqueFlowgrams[0], uniqueFlowgrams.size(), MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
551 MPI_Recv(&sigma, 1, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
553 int startOTU = pid * numOTUs / ncpus;
554 int endOTU = (pid + 1) * numOTUs / ncpus;
556 int startSeq = pid * numSeqs / ncpus;
557 int endSeq = (pid + 1) * numSeqs /ncpus;
563 MPI_Recv(&total, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
564 singleTau.assign(total, 0.0000);
565 seqNumber.assign(total, 0);
566 seqIndex.assign(total, 0);
568 MPI_Recv(&change[0], numOTUs, MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
569 MPI_Recv(¢roids[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
570 MPI_Recv(&singleTau[0], total, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
571 MPI_Recv(&seqNumber[0], total, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
572 MPI_Recv(&seqIndex[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
573 MPI_Recv(&nSeqsPerOTU[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
574 MPI_Recv(&cumNumSeqs[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
576 calcCentroidsDriver(startOTU, endOTU);
578 MPI_Send(¢roids[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD);
579 MPI_Send(&change[0], numOTUs, MPI_SHORT, 0, tag, MPI_COMM_WORLD);
581 MPI_Recv(¢roids[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
582 MPI_Recv(&weight[0], numOTUs, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
583 MPI_Recv(&change[0], numOTUs, MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
585 vector<int> otuIndex(total, 0);
586 calcNewDistancesChildMPI(startSeq, endSeq, otuIndex);
587 total = otuIndex.size();
589 MPI_Send(&total, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
590 MPI_Send(&seqIndex[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD);
591 MPI_Send(&singleTau[0], total, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD);
592 MPI_Send(&dist[0], numOTUs * numSeqs, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD);
593 MPI_Send(&otuIndex[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD);
595 MPI_Recv(&live, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
599 MPI_Barrier(MPI_COMM_WORLD);
604 catch(exception& e) {
605 m->errorOut(e, "ShhherCommand", "execute");
610 /**************************************************************************************************/
612 string ShhherCommand::flowDistMPI(int startSeq, int stopSeq){
614 ostringstream outStream;
615 outStream.setf(ios::fixed, ios::floatfield);
616 outStream.setf(ios::dec, ios::basefield);
617 outStream.setf(ios::showpoint);
618 outStream.precision(6);
620 int begTime = time(NULL);
621 double begClock = clock();
623 for(int i=startSeq;i<stopSeq;i++){
624 for(int j=0;j<i;j++){
625 float flowDistance = calcPairwiseDist(mapUniqueToSeq[i], mapUniqueToSeq[j]);
627 if(flowDistance < 1e-6){
628 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << 0.000000 << endl;
630 else if(flowDistance <= cutoff){
631 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << flowDistance << endl;
635 m->mothurOut(toString(i) + '\t' + toString(time(NULL) - begTime) + '\t' + toString((clock()-begClock)/CLOCKS_PER_SEC) + '\n');
639 m->mothurOut(toString(stopSeq) + '\t' + toString(time(NULL) - begTime) + '\t' + toString((clock()-begClock)/CLOCKS_PER_SEC) + '\n');
641 string fDistFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.dist";
642 if(pid != 0){ fDistFileName += ".temp." + toString(pid); }
644 ofstream distFile(fDistFileName.c_str());
645 distFile << outStream.str();
648 return fDistFileName;
650 catch(exception& e) {
651 m->errorOut(e, "ShhherCommand", "flowDistParentFork");
657 //**********************************************************************************************************************
659 int ShhherCommand::execute(){
661 if (abort == true) { return 0; }
666 vector<string> flowFileVector;
667 if(flowFilesFileName != "not found"){
670 ifstream flowFilesFile;
671 m->openInputFile(flowFilesFileName, flowFilesFile);
672 while(flowFilesFile){
673 flowFilesFile >> fName;
674 flowFileVector.push_back(fName);
675 m->gobble(flowFilesFile);
679 flowFileVector.push_back(flowFileName);
681 int numFiles = flowFileVector.size();
684 for(int i=0;i<numFiles;i++){
685 flowFileName = flowFileVector[i];
687 m->mothurOut("\n>>>>>\tProcessing " + flowFileName + " (file " + toString(i+1) + " of " + toString(numFiles) + ")\t<<<<<\n");
688 m->mothurOut("Reading flowgrams...\n");
691 m->mothurOut("Identifying unique flowgrams...\n");
695 m->mothurOut("Calculating distances between flowgrams...\n");
696 string distFileName = createDistFile(processors);
697 string namesFileName = createNamesFile();
699 m->mothurOut("\nClustering flowgrams...\n");
700 string listFileName = cluster(distFileName, namesFileName);
701 getOTUData(listFileName);
708 double begClock = clock();
709 unsigned long int begTime = time(NULL);
711 m->mothurOut("\nDenoising flowgrams...\n");
712 m->mothurOut("iter\tmaxDelta\tnLL\t\tcycletime\n");
714 while((maxIters == 0 && maxDelta > minDelta) || iter < MIN_ITER || (maxDelta > minDelta && iter < maxIters)){
716 double cycClock = clock();
717 unsigned long int cycTime = time(NULL);
722 maxDelta = getNewWeights();
723 double nLL = getLikelihood();
730 m->mothurOut(toString(iter) + '\t' + toString(maxDelta) + '\t' + toString(nLL) + '\t' + toString(time(NULL) - cycTime) + '\t' + toString((clock() - cycClock)/(double)CLOCKS_PER_SEC) + '\n');
734 m->mothurOut("\nFinalizing...\n");
738 vector<int> otuCounts(numOTUs, 0);
739 for(int i=0;i<numSeqs;i++) { otuCounts[otuData[i]]++; }
741 calcCentroidsDriver(0, numOTUs);
742 writeQualities(otuCounts);
743 writeSequences(otuCounts);
744 writeNames(otuCounts);
745 writeClusters(otuCounts);
748 remove(distFileName.c_str());
749 remove(namesFileName.c_str());
750 remove(listFileName.c_str());
752 m->mothurOut("Total time to process " + flowFileName + ":\t" + toString(time(NULL) - begTime) + '\t' + toString((clock() - begClock)/(double)CLOCKS_PER_SEC) + '\n');
756 catch(exception& e) {
757 m->errorOut(e, "ShhherCommand", "execute");
762 /**************************************************************************************************/
764 void ShhherCommand::getFlowData(){
767 m->openInputFile(flowFileName, flowFile);
770 seqNameVector.clear();
772 flowDataIntI.clear();
776 int currentNumFlowCells;
780 flowFile >> numFlowCells;
781 int index = 0;//pcluster
782 while(!flowFile.eof()){
783 flowFile >> seqName >> currentNumFlowCells;
784 lengths.push_back(currentNumFlowCells);
786 seqNameVector.push_back(seqName);
787 nameMap[seqName] = index++;//pcluster
789 for(int i=0;i<numFlowCells;i++){
790 flowFile >> intensity;
791 if(intensity > 9.99) { intensity = 9.99; }
792 int intI = int(100 * intensity + 0.0001);
793 flowDataIntI.push_back(intI);
799 numSeqs = seqNameVector.size();
801 for(int i=0;i<numSeqs;i++){
802 int iNumFlowCells = i * numFlowCells;
803 for(int j=lengths[i];j<numFlowCells;j++){
804 flowDataIntI[iNumFlowCells + j] = 0;
809 catch(exception& e) {
810 m->errorOut(e, "ShhherCommand", "getFlowData");
815 /**************************************************************************************************/
817 void ShhherCommand::getSingleLookUp(){
819 // these are the -log probabilities that a signal corresponds to a particular homopolymer length
820 singleLookUp.assign(HOMOPS * NUMBINS, 0);
824 m->openInputFile(lookupFileName, lookUpFile);
826 for(int i=0;i<HOMOPS;i++){
828 lookUpFile >> logFracFreq;
830 for(int j=0;j<NUMBINS;j++) {
831 lookUpFile >> singleLookUp[index];
837 catch(exception& e) {
838 m->errorOut(e, "ShhherCommand", "getSingleLookUp");
843 /**************************************************************************************************/
845 void ShhherCommand::getJointLookUp(){
848 // the most likely joint probability (-log) that two intenities have the same polymer length
849 jointLookUp.resize(NUMBINS * NUMBINS, 0);
851 for(int i=0;i<NUMBINS;i++){
852 for(int j=0;j<NUMBINS;j++){
854 double minSum = 100000000;
856 for(int k=0;k<HOMOPS;k++){
857 double sum = singleLookUp[k * NUMBINS + i] + singleLookUp[k * NUMBINS + j];
859 if(sum < minSum) { minSum = sum; }
861 jointLookUp[i * NUMBINS + j] = minSum;
865 catch(exception& e) {
866 m->errorOut(e, "ShhherCommand", "getJointLookUp");
871 /**************************************************************************************************/
873 double ShhherCommand::getProbIntensity(int intIntensity){
876 double minNegLogProb = 100000000;
879 for(int i=0;i<HOMOPS;i++){//loop signal strength
880 float negLogProb = singleLookUp[i * NUMBINS + intIntensity];
881 if(negLogProb < minNegLogProb) { minNegLogProb = negLogProb; }
884 return minNegLogProb;
886 catch(exception& e) {
887 m->errorOut(e, "ShhherCommand", "getProbIntensity");
892 /**************************************************************************************************/
894 void ShhherCommand::getUniques(){
899 uniqueFlowgrams.assign(numFlowCells * numSeqs, -1);
900 uniqueCount.assign(numSeqs, 0); // anWeights
901 uniqueLengths.assign(numSeqs, 0);
902 mapSeqToUnique.assign(numSeqs, -1);
903 mapUniqueToSeq.assign(numSeqs, -1);
905 vector<short> uniqueFlowDataIntI(numFlowCells * numSeqs, -1);
907 for(int i=0;i<numSeqs;i++){
910 vector<short> current(numFlowCells);
911 for(int j=0;j<numFlowCells;j++){
912 current[j] = short(((flowDataIntI[i * numFlowCells + j] + 50.0)/100.0));
915 for(int j=0;j<numUniques;j++){
916 int offset = j * numFlowCells;
920 if(lengths[i] < uniqueLengths[j]) { shorterLength = lengths[i]; }
921 else { shorterLength = uniqueLengths[j]; }
923 for(int k=0;k<shorterLength;k++){
924 if(current[k] != uniqueFlowgrams[offset + k]){
931 mapSeqToUnique[i] = j;
934 if(lengths[i] > uniqueLengths[j]) { uniqueLengths[j] = lengths[i]; }
940 if(index == numUniques){
941 uniqueLengths[numUniques] = lengths[i];
942 uniqueCount[numUniques] = 1;
943 mapSeqToUnique[i] = numUniques;//anMap
944 mapUniqueToSeq[numUniques] = i;//anF
946 for(int k=0;k<numFlowCells;k++){
947 uniqueFlowgrams[numUniques * numFlowCells + k] = current[k];
948 uniqueFlowDataIntI[numUniques * numFlowCells + k] = flowDataIntI[i * numFlowCells + k];
954 uniqueFlowDataIntI.resize(numFlowCells * numUniques);
955 uniqueLengths.resize(numUniques);
957 flowDataPrI.resize(numSeqs * numFlowCells, 0);
958 for(int i=0;i<flowDataPrI.size();i++) { flowDataPrI[i] = getProbIntensity(flowDataIntI[i]); }
960 catch(exception& e) {
961 m->errorOut(e, "ShhherCommand", "getUniques");
966 /**************************************************************************************************/
968 float ShhherCommand::calcPairwiseDist(int seqA, int seqB){
970 int minLength = lengths[mapSeqToUnique[seqA]];
971 if(lengths[seqB] < minLength){ minLength = lengths[mapSeqToUnique[seqB]]; }
973 int ANumFlowCells = seqA * numFlowCells;
974 int BNumFlowCells = seqB * numFlowCells;
978 for(int i=0;i<minLength;i++){
979 int flowAIntI = flowDataIntI[ANumFlowCells + i];
980 float flowAPrI = flowDataPrI[ANumFlowCells + i];
982 int flowBIntI = flowDataIntI[BNumFlowCells + i];
983 float flowBPrI = flowDataPrI[BNumFlowCells + i];
984 dist += jointLookUp[flowAIntI * NUMBINS + flowBIntI] - flowAPrI - flowBPrI;
987 dist /= (float) minLength;
990 catch(exception& e) {
991 m->errorOut(e, "ShhherCommand", "calcPairwiseDist");
996 /**************************************************************************************************/
998 void ShhherCommand::flowDistParentFork(string distFileName, int startSeq, int stopSeq){
1001 ostringstream outStream;
1002 outStream.setf(ios::fixed, ios::floatfield);
1003 outStream.setf(ios::dec, ios::basefield);
1004 outStream.setf(ios::showpoint);
1005 outStream.precision(6);
1007 int begTime = time(NULL);
1008 double begClock = clock();
1010 for(int i=startSeq;i<stopSeq;i++){
1011 for(int j=0;j<i;j++){
1012 float flowDistance = calcPairwiseDist(mapUniqueToSeq[i], mapUniqueToSeq[j]);
1014 if(flowDistance < 1e-6){
1015 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << 0.000000 << endl;
1017 else if(flowDistance <= cutoff){
1018 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << flowDistance << endl;
1022 m->mothurOut(toString(i) + "\t" + toString(time(NULL) - begTime));
1023 m->mothurOut("\t" + toString((clock()-begClock)/CLOCKS_PER_SEC));
1024 m->mothurOutEndLine();
1027 m->mothurOut(toString(stopSeq-1) + "\t" + toString(time(NULL) - begTime));
1028 m->mothurOut("\t" + toString((clock()-begClock)/CLOCKS_PER_SEC));
1029 m->mothurOutEndLine();
1031 ofstream distFile(distFileName.c_str());
1032 distFile << outStream.str();
1035 catch(exception& e) {
1036 m->errorOut(e, "ShhherCommand", "flowDistParentFork");
1041 /**************************************************************************************************/
1043 string ShhherCommand::createDistFile(int processors){
1045 string fDistFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.dist";
1047 unsigned long int begTime = time(NULL);
1048 double begClock = clock();
1053 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
1054 if(processors == 1) { flowDistParentFork(fDistFileName, 0, numUniques); }
1055 else{ //you have multiple processors
1057 if (numSeqs < processors){ processors = 1; }
1059 vector<int> start(processors, 0);
1060 vector<int> end(processors, 0);
1062 for (int i = 0; i < processors; i++) {
1063 start[i] = int(sqrt(float(i)/float(processors)) * numUniques);
1064 end[i] = int(sqrt(float(i+1)/float(processors)) * numUniques);
1068 vector<int> processIDs;
1070 //loop through and create all the processes you want
1071 while (process != processors) {
1075 processIDs.push_back(pid); //create map from line number to pid so you can append files in correct order later
1077 }else if (pid == 0){
1078 flowDistParentFork(fDistFileName + toString(getpid()) + ".temp", start[process], end[process]);
1081 m->mothurOut("[ERROR]: unable to spawn the necessary processes. Error code: " + toString(pid)); m->mothurOutEndLine();
1083 for (int i=0;i<processIDs.size();i++) { int temp = processIDs[i]; kill (temp, SIGINT); }
1088 //parent does its part
1089 flowDistParentFork(fDistFileName, start[0], end[0]);
1091 //force parent to wait until all the processes are done
1092 for (int i=0;i<processIDs.size();i++) {
1093 int temp = processIDs[i];
1097 //append and remove temp files
1098 for (int i=0;i<processIDs.size();i++) {
1099 m->appendFiles((fDistFileName + toString(processIDs[i]) + ".temp"), fDistFileName);
1100 remove((fDistFileName + toString(processIDs[i]) + ".temp").c_str());
1106 flowDistParentFork(fDistFileName, 0, numUniques);
1109 m->mothurOutEndLine();
1111 m->mothurOut("Total time: " + toString(time(NULL) - begTime) + '\t' + toString((clock() - begClock)/CLOCKS_PER_SEC) + '\n');
1114 return fDistFileName;
1116 catch(exception& e) {
1117 m->errorOut(e, "ShhherCommand", "createDistFile");
1123 /**************************************************************************************************/
1125 string ShhherCommand::createNamesFile(){
1128 vector<string> duplicateNames(numUniques, "");
1129 for(int i=0;i<numSeqs;i++){
1130 duplicateNames[mapSeqToUnique[i]] += seqNameVector[i] + ',';
1133 string nameFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.names";
1136 m->openOutputFile(nameFileName, nameFile);
1138 for(int i=0;i<numUniques;i++){
1139 // nameFile << seqNameVector[mapUniqueToSeq[i]] << '\t' << duplicateNames[i].substr(0, duplicateNames[i].find_last_of(',')) << endl;
1140 nameFile << mapUniqueToSeq[i] << '\t' << duplicateNames[i].substr(0, duplicateNames[i].find_last_of(',')) << endl;
1144 return nameFileName;
1146 catch(exception& e) {
1147 m->errorOut(e, "ShhherCommand", "createNamesFile");
1152 //**********************************************************************************************************************
1154 string ShhherCommand::cluster(string distFileName, string namesFileName){
1158 globaldata->setNameFile(namesFileName);
1159 globaldata->setColumnFile(distFileName);
1160 globaldata->setFormat("column");
1162 ReadMatrix* read = new ReadColumnMatrix(distFileName);
1163 read->setCutoff(cutoff);
1165 NameAssignment* clusterNameMap = new NameAssignment(namesFileName);
1166 clusterNameMap->readMap();
1167 read->read(clusterNameMap);
1169 ListVector* list = read->getListVector();
1170 SparseMatrix* matrix = read->getMatrix();
1173 delete clusterNameMap;
1175 RAbundVector* rabund = new RAbundVector(list->getRAbundVector());
1177 Cluster* cluster = new CompleteLinkage(rabund, list, matrix, cutoff, "furthest");
1178 string tag = cluster->getTag();
1180 double clusterCutoff = cutoff;
1181 while (matrix->getSmallDist() <= clusterCutoff && matrix->getNNodes() > 0){
1182 cluster->update(clusterCutoff);
1185 list->setLabel(toString(cutoff));
1187 string listFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.list";
1189 m->openOutputFile(listFileName, listFile);
1190 list->print(listFile);
1193 delete matrix; delete cluster; delete rabund; delete list;
1195 return listFileName;
1197 catch(exception& e) {
1198 m->errorOut(e, "ShhherCommand", "cluster");
1203 /**************************************************************************************************/
1205 void ShhherCommand::getOTUData(string listFileName){
1209 m->openInputFile(listFileName, listFile);
1212 listFile >> label >> numOTUs;
1214 otuData.assign(numSeqs, 0);
1215 cumNumSeqs.assign(numOTUs, 0);
1216 nSeqsPerOTU.assign(numOTUs, 0);
1217 aaP.clear();aaP.resize(numOTUs);
1223 string singleOTU = "";
1225 for(int i=0;i<numOTUs;i++){
1227 listFile >> singleOTU;
1229 istringstream otuString(singleOTU);
1233 string seqName = "";
1235 for(int j=0;j<singleOTU.length();j++){
1236 char letter = otuString.get();
1242 map<string,int>::iterator nmIt = nameMap.find(seqName);
1243 int index = nmIt->second;
1245 nameMap.erase(nmIt);
1249 aaP[i].push_back(index);
1254 map<string,int>::iterator nmIt = nameMap.find(seqName);
1256 int index = nmIt->second;
1257 nameMap.erase(nmIt);
1261 aaP[i].push_back(index);
1266 sort(aaP[i].begin(), aaP[i].end());
1267 for(int j=0;j<nSeqsPerOTU[i];j++){
1268 seqNumber.push_back(aaP[i][j]);
1270 for(int j=nSeqsPerOTU[i];j<numSeqs;j++){
1271 aaP[i].push_back(0);
1277 for(int i=1;i<numOTUs;i++){
1278 cumNumSeqs[i] = cumNumSeqs[i-1] + nSeqsPerOTU[i-1];
1281 seqIndex = seqNumber;
1286 catch(exception& e) {
1287 m->errorOut(e, "ShhherCommand", "getOTUData");
1292 /**************************************************************************************************/
1294 void ShhherCommand::initPyroCluster(){
1296 dist.assign(numSeqs * numOTUs, 0);
1297 change.assign(numOTUs, 1);
1298 centroids.assign(numOTUs, -1);
1299 weight.assign(numOTUs, 0);
1300 singleTau.assign(numSeqs, 1.0);
1302 nSeqsBreaks.assign(processors+1, 0);
1303 nOTUsBreaks.assign(processors+1, 0);
1306 for(int i=0;i<processors;i++){
1307 nSeqsBreaks[i+1] = nSeqsBreaks[i] + (int)((double) numSeqs / (double) processors);
1308 nOTUsBreaks[i+1] = nOTUsBreaks[i] + (int)((double) numOTUs / (double) processors);
1310 nSeqsBreaks[processors] = numSeqs;
1311 nOTUsBreaks[processors] = numOTUs;
1313 catch(exception& e) {
1314 m->errorOut(e, "ShhherCommand", "initPyroCluster");
1319 /**************************************************************************************************/
1321 void ShhherCommand::fill(){
1324 for(int i=0;i<numOTUs;i++){
1325 cumNumSeqs[i] = index;
1326 for(int j=0;j<nSeqsPerOTU[i];j++){
1327 seqNumber[index] = aaP[i][j];
1328 seqIndex[index] = aaI[i][j];
1334 catch(exception& e) {
1335 m->errorOut(e, "ShhherCommand", "fill");
1340 /**************************************************************************************************/
1342 void ShhherCommand::calcCentroids(){
1345 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
1347 if(processors == 1) {
1348 calcCentroidsDriver(0, numOTUs);
1350 else{ //you have multiple processors
1351 if (numOTUs < processors){ processors = 1; }
1354 vector<int> processIDs;
1356 //loop through and create all the processes you want
1357 while (process != processors) {
1361 processIDs.push_back(pid); //create map from line number to pid so you can append files in correct order later
1363 }else if (pid == 0){
1364 calcCentroidsDriver(nOTUsBreaks[process], nOTUsBreaks[process+1]);
1367 m->mothurOut("[ERROR]: unable to spawn the necessary processes. Error code: " + toString(pid)); m->mothurOutEndLine();
1369 for (int i=0;i<processIDs.size();i++) { int temp = processIDs[i]; kill (temp, SIGINT); }
1374 //parent does its part
1375 calcCentroidsDriver(nOTUsBreaks[0], nOTUsBreaks[1]);
1377 //force parent to wait until all the processes are done
1378 for (int i=0;i<processIDs.size();i++) {
1379 int temp = processIDs[i];
1385 calcCentroidsDriver(0, numOTUs);
1388 catch(exception& e) {
1389 m->errorOut(e, "ShhherCommand", "calcCentroidsDriver");
1394 /**************************************************************************************************/
1396 void ShhherCommand::calcCentroidsDriver(int start, int finish){
1398 //this function gets the most likely homopolymer length at a flow position for a group of sequences
1404 for(int i=start;i<finish;i++){
1408 int minFlowGram = 100000000;
1409 double minFlowValue = 1e8;
1410 change[i] = 0; //FALSE
1412 for(int j=0;j<nSeqsPerOTU[i];j++){
1413 count += singleTau[seqNumber[cumNumSeqs[i] + j]];
1416 if(nSeqsPerOTU[i] > 0 && count > MIN_COUNT){
1417 vector<double> adF(nSeqsPerOTU[i]);
1418 vector<int> anL(nSeqsPerOTU[i]);
1420 for(int j=0;j<nSeqsPerOTU[i];j++){
1421 int index = cumNumSeqs[i] + j;
1422 int nI = seqIndex[index];
1423 int nIU = mapSeqToUnique[nI];
1426 for(k=0;k<position;k++){
1432 anL[position] = nIU;
1433 adF[position] = 0.0000;
1438 for(int j=0;j<nSeqsPerOTU[i];j++){
1439 int index = cumNumSeqs[i] + j;
1440 int nI = seqIndex[index];
1442 double tauValue = singleTau[seqNumber[index]];
1444 for(int k=0;k<position;k++){
1445 double dist = getDistToCentroid(anL[k], nI, lengths[nI]);
1446 adF[k] += dist * tauValue;
1450 for(int j=0;j<position;j++){
1451 if(adF[j] < minFlowValue){
1453 minFlowValue = adF[j];
1457 if(centroids[i] != anL[minFlowGram]){
1459 centroids[i] = anL[minFlowGram];
1462 else if(centroids[i] != -1){
1468 catch(exception& e) {
1469 m->errorOut(e, "ShhherCommand", "calcCentroidsDriver");
1474 /**************************************************************************************************/
1476 double ShhherCommand::getDistToCentroid(int cent, int flow, int length){
1479 int flowAValue = cent * numFlowCells;
1480 int flowBValue = flow * numFlowCells;
1484 for(int i=0;i<length;i++){
1485 dist += singleLookUp[uniqueFlowgrams[flowAValue] * NUMBINS + flowDataIntI[flowBValue]];
1490 return dist / (double)length;
1492 catch(exception& e) {
1493 m->errorOut(e, "ShhherCommand", "getDistToCentroid");
1498 /**************************************************************************************************/
1500 double ShhherCommand::getNewWeights(){
1503 double maxChange = 0;
1505 for(int i=0;i<numOTUs;i++){
1507 double difference = weight[i];
1510 for(int j=0;j<nSeqsPerOTU[i];j++){
1511 int index = cumNumSeqs[i] + j;
1512 double tauValue = singleTau[seqNumber[index]];
1513 weight[i] += tauValue;
1516 difference = fabs(weight[i] - difference);
1517 if(difference > maxChange){ maxChange = difference; }
1521 catch(exception& e) {
1522 m->errorOut(e, "ShhherCommand", "getNewWeights");
1527 /**************************************************************************************************/
1529 double ShhherCommand::getLikelihood(){
1533 vector<long double> P(numSeqs, 0);
1536 for(int i=0;i<numOTUs;i++){
1537 if(weight[i] > MIN_WEIGHT){
1543 for(int i=0;i<numOTUs;i++){
1544 for(int j=0;j<nSeqsPerOTU[i];j++){
1545 int index = cumNumSeqs[i] + j;
1546 int nI = seqIndex[index];
1547 double singleDist = dist[seqNumber[index]];
1549 P[nI] += weight[i] * exp(-singleDist * sigma);
1553 for(int i=0;i<numSeqs;i++){
1554 if(P[i] == 0){ P[i] = DBL_EPSILON; }
1559 nLL = nLL -(double)numSeqs * log(sigma);
1563 catch(exception& e) {
1564 m->errorOut(e, "ShhherCommand", "getNewWeights");
1569 /**************************************************************************************************/
1571 void ShhherCommand::checkCentroids(){
1573 vector<int> unique(numOTUs, 1);
1575 for(int i=0;i<numOTUs;i++){
1576 if(centroids[i] == -1 || weight[i] < MIN_WEIGHT){
1581 for(int i=0;i<numOTUs;i++){
1583 for(int j=i+1;j<numOTUs;j++){
1586 if(centroids[j] == centroids[i]){
1590 weight[i] += weight[j];
1598 catch(exception& e) {
1599 m->errorOut(e, "ShhherCommand", "checkCentroids");
1604 /**************************************************************************************************/
1606 void ShhherCommand::calcNewDistances(){
1609 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
1611 if(processors == 1) {
1612 calcNewDistancesParent(0, numSeqs);
1614 else{ //you have multiple processors
1615 if (numSeqs < processors){ processors = 1; }
1617 vector<vector<int> > child_otuIndex(processors);
1618 vector<vector<int> > child_seqIndex(processors);
1619 vector<vector<double> > child_singleTau(processors);
1620 vector<int> totals(processors);
1623 vector<int> processIDs;
1625 //loop through and create all the processes you want
1626 while (process != processors) {
1630 processIDs.push_back(pid); //create map from line number to pid so you can append files in correct order later
1632 }else if (pid == 0){
1633 calcNewDistancesChild(nSeqsBreaks[process], nSeqsBreaks[process+1], child_otuIndex[process], child_seqIndex[process], child_singleTau[process]);
1634 totals[process] = child_otuIndex[process].size();
1638 m->mothurOut("[ERROR]: unable to spawn the necessary processes. Error code: " + toString(pid)); m->mothurOutEndLine();
1640 for (int i=0;i<processIDs.size();i++) { int temp = processIDs[i]; kill (temp, SIGINT); }
1645 //parent does its part
1646 calcNewDistancesParent(nSeqsBreaks[0], nSeqsBreaks[1]);
1647 int total = seqIndex.size();
1649 //force parent to wait until all the processes are done
1650 for (int i=0;i<processIDs.size();i++) {
1651 int temp = processIDs[i];
1655 for(int i=1;i<processors;i++){
1656 int oldTotal = total;
1659 singleTau.resize(total, 0);
1660 seqIndex.resize(total, 0);
1661 seqNumber.resize(total, 0);
1665 for(int j=oldTotal;j<total;j++){
1666 int otuI = child_otuIndex[i][childIndex];
1667 int seqI = child_seqIndex[i][childIndex];
1669 singleTau[j] = child_singleTau[i][childIndex];
1670 aaP[otuI][nSeqsPerOTU[otuI]] = j;
1671 aaI[otuI][nSeqsPerOTU[otuI]] = seqI;
1672 nSeqsPerOTU[otuI]++;
1679 calcNewDistancesParent(0, numSeqs);
1682 catch(exception& e) {
1683 m->errorOut(e, "ShhherCommand", "calcNewDistances");
1688 /**************************************************************************************************/
1690 void ShhherCommand::calcNewDistancesChildMPI(int startSeq, int stopSeq, vector<int>& otuIndex){
1693 vector<double> newTau(numOTUs,0);
1694 vector<double> norms(numSeqs, 0);
1701 for(int i=startSeq;i<stopSeq;i++){
1702 double offset = 1e8;
1703 int indexOffset = i * numOTUs;
1705 for(int j=0;j<numOTUs;j++){
1707 if(weight[j] > MIN_WEIGHT && change[j] == 1){
1708 dist[indexOffset + j] = getDistToCentroid(centroids[j], i, lengths[i]);
1710 if(weight[j] > MIN_WEIGHT && dist[indexOffset + j] < offset){
1711 offset = dist[indexOffset + j];
1715 for(int j=0;j<numOTUs;j++){
1716 if(weight[j] > MIN_WEIGHT){
1717 newTau[j] = exp(sigma * (-dist[indexOffset + j] + offset)) * weight[j];
1718 norms[i] += newTau[j];
1725 for(int j=0;j<numOTUs;j++){
1727 newTau[j] /= norms[i];
1729 if(newTau[j] > MIN_TAU){
1730 otuIndex.push_back(j);
1731 seqIndex.push_back(i);
1732 singleTau.push_back(newTau[j]);
1738 catch(exception& e) {
1739 m->errorOut(e, "ShhherCommand", "calcNewDistancesChildMPI");
1744 /**************************************************************************************************/
1746 void ShhherCommand::calcNewDistancesChild(int startSeq, int stopSeq, vector<int>& child_otuIndex, vector<int>& child_seqIndex, vector<double>& child_singleTau){
1749 vector<double> newTau(numOTUs,0);
1750 vector<double> norms(numSeqs, 0);
1751 child_otuIndex.resize(0);
1752 child_seqIndex.resize(0);
1753 child_singleTau.resize(0);
1755 for(int i=startSeq;i<stopSeq;i++){
1756 double offset = 1e8;
1757 int indexOffset = i * numOTUs;
1760 for(int j=0;j<numOTUs;j++){
1761 if(weight[j] > MIN_WEIGHT && change[j] == 1){
1762 dist[indexOffset + j] = getDistToCentroid(centroids[j], i, lengths[i]);
1765 if(weight[j] > MIN_WEIGHT && dist[indexOffset + j] < offset){
1766 offset = dist[indexOffset + j];
1770 for(int j=0;j<numOTUs;j++){
1771 if(weight[j] > MIN_WEIGHT){
1772 newTau[j] = exp(sigma * (-dist[indexOffset + j] + offset)) * weight[j];
1773 norms[i] += newTau[j];
1780 for(int j=0;j<numOTUs;j++){
1781 newTau[j] /= norms[i];
1783 if(newTau[j] > MIN_TAU){
1784 child_otuIndex.push_back(j);
1785 child_seqIndex.push_back(i);
1786 child_singleTau.push_back(newTau[j]);
1791 catch(exception& e) {
1792 m->errorOut(e, "ShhherCommand", "calcNewDistancesChild");
1797 /**************************************************************************************************/
1799 void ShhherCommand::calcNewDistancesParent(int startSeq, int stopSeq){
1804 vector<double> newTau(numOTUs,0);
1805 vector<double> norms(numSeqs, 0);
1806 nSeqsPerOTU.assign(numOTUs, 0);
1808 for(int i=startSeq;i<stopSeq;i++){
1809 int indexOffset = i * numOTUs;
1811 double offset = 1e8;
1813 for(int j=0;j<numOTUs;j++){
1814 if(weight[j] > MIN_WEIGHT && change[j] == 1){
1815 dist[indexOffset + j] = getDistToCentroid(centroids[j], i, lengths[i]);
1818 if(weight[j] > MIN_WEIGHT && dist[indexOffset + j] < offset){
1819 offset = dist[indexOffset + j];
1823 for(int j=0;j<numOTUs;j++){
1824 if(weight[j] > MIN_WEIGHT){
1825 newTau[j] = exp(sigma * (-dist[indexOffset + j] + offset)) * weight[j];
1826 norms[i] += newTau[j];
1833 for(int j=0;j<numOTUs;j++){
1834 newTau[j] /= norms[i];
1837 for(int j=0;j<numOTUs;j++){
1838 if(newTau[j] > MIN_TAU){
1840 int oldTotal = total;
1844 singleTau.resize(total, 0);
1845 seqNumber.resize(total, 0);
1846 seqIndex.resize(total, 0);
1848 singleTau[oldTotal] = newTau[j];
1850 aaP[j][nSeqsPerOTU[j]] = oldTotal;
1851 aaI[j][nSeqsPerOTU[j]] = i;
1857 catch(exception& e) {
1858 m->errorOut(e, "ShhherCommand", "calcNewDistancesParent");
1863 /**************************************************************************************************/
1865 void ShhherCommand::setOTUs(){
1868 vector<double> bigTauMatrix(numOTUs * numSeqs, 0.0000);
1870 for(int i=0;i<numOTUs;i++){
1871 for(int j=0;j<nSeqsPerOTU[i];j++){
1872 int index = cumNumSeqs[i] + j;
1873 double tauValue = singleTau[seqNumber[index]];
1874 int sIndex = seqIndex[index];
1875 bigTauMatrix[sIndex * numOTUs + i] = tauValue;
1879 for(int i=0;i<numSeqs;i++){
1880 double maxTau = -1.0000;
1882 for(int j=0;j<numOTUs;j++){
1883 if(bigTauMatrix[i * numOTUs + j] > maxTau){
1884 maxTau = bigTauMatrix[i * numOTUs + j];
1889 otuData[i] = maxOTU;
1892 nSeqsPerOTU.assign(numOTUs, 0);
1894 for(int i=0;i<numSeqs;i++){
1895 int index = otuData[i];
1897 singleTau[i] = 1.0000;
1900 aaP[index][nSeqsPerOTU[index]] = i;
1901 aaI[index][nSeqsPerOTU[index]] = i;
1903 nSeqsPerOTU[index]++;
1907 catch(exception& e) {
1908 m->errorOut(e, "ShhherCommand", "calcNewDistances");
1913 /**************************************************************************************************/
1915 void ShhherCommand::writeQualities(vector<int> otuCounts){
1918 string qualityFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.qual";
1920 ofstream qualityFile;
1921 m->openOutputFile(qualityFileName, qualityFile);
1923 qualityFile.setf(ios::fixed, ios::floatfield);
1924 qualityFile.setf(ios::showpoint);
1925 qualityFile << setprecision(6);
1927 vector<vector<int> > qualities(numOTUs);
1928 vector<double> pr(HOMOPS, 0);
1931 for(int i=0;i<numOTUs;i++){
1935 if(nSeqsPerOTU[i] > 0){
1936 qualities[i].assign(1024, -1);
1938 while(index < numFlowCells){
1939 double maxPrValue = 1e8;
1940 short maxPrIndex = -1;
1941 double count = 0.0000;
1943 pr.assign(HOMOPS, 0);
1945 for(int j=0;j<nSeqsPerOTU[i];j++){
1946 int lIndex = cumNumSeqs[i] + j;
1947 double tauValue = singleTau[seqNumber[lIndex]];
1948 int sequenceIndex = aaI[i][j];
1949 short intensity = flowDataIntI[sequenceIndex * numFlowCells + index];
1953 for(int s=0;s<HOMOPS;s++){
1954 pr[s] += tauValue * singleLookUp[s * NUMBINS + intensity];
1958 maxPrIndex = uniqueFlowgrams[centroids[i] * numFlowCells + index];
1959 maxPrValue = pr[maxPrIndex];
1961 if(count > MIN_COUNT){
1963 double norm = 0.0000;
1965 for(int s=0;s<HOMOPS;s++){
1966 norm += exp(-(pr[s] - maxPrValue));
1969 for(int s=1;s<=maxPrIndex;s++){
1971 double temp = 0.0000;
1973 U += exp(-(pr[s-1]-maxPrValue))/norm;
1981 temp = floor(-10 * temp);
1982 value = (int)floor(temp);
1983 if(value > 100){ value = 100; }
1985 qualities[i][base] = (int)value;
1995 if(otuCounts[i] > 0){
1996 qualityFile << '>' << seqNameVector[mapUniqueToSeq[i]] << endl;
1998 int j=4; //need to get past the first four bases
1999 while(qualities[i][j] != -1){
2000 qualityFile << qualities[i][j] << ' ';
2003 qualityFile << endl;
2006 qualityFile.close();
2009 catch(exception& e) {
2010 m->errorOut(e, "ShhherCommand", "writeQualities");
2015 /**************************************************************************************************/
2017 void ShhherCommand::writeSequences(vector<int> otuCounts){
2021 string fastaFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.fasta";
2023 m->openOutputFile(fastaFileName, fastaFile);
2025 vector<string> names(numOTUs, "");
2027 for(int i=0;i<numOTUs;i++){
2028 int index = centroids[i];
2030 if(otuCounts[i] > 0){
2031 fastaFile << '>' << seqNameVector[aaI[i][0]] << endl;
2033 for(int j=8;j<numFlowCells;j++){
2035 char base = flowOrder[j % 4];
2036 for(int k=0;k<uniqueFlowgrams[index * numFlowCells + j];k++){
2045 if(compositeFASTAFileName != ""){
2046 m->appendFiles(fastaFileName, compositeFASTAFileName);
2049 catch(exception& e) {
2050 m->errorOut(e, "ShhherCommand", "writeSequences");
2055 /**************************************************************************************************/
2057 void ShhherCommand::writeNames(vector<int> otuCounts){
2059 string nameFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.final.names";
2061 m->openOutputFile(nameFileName, nameFile);
2063 for(int i=0;i<numOTUs;i++){
2064 if(otuCounts[i] > 0){
2065 nameFile << seqNameVector[aaI[i][0]] << '\t' << seqNameVector[aaI[i][0]];
2067 for(int j=1;j<nSeqsPerOTU[i];j++){
2068 nameFile << ',' << seqNameVector[aaI[i][j]];
2076 catch(exception& e) {
2077 m->errorOut(e, "ShhherCommand", "writeNames");
2082 /**************************************************************************************************/
2084 void ShhherCommand::writeGroups(){
2086 string fileRoot = flowFileName.substr(0,flowFileName.find_last_of('.'));
2087 string groupFileName = fileRoot + ".pn.groups";
2089 m->openOutputFile(groupFileName, groupFile);
2091 for(int i=0;i<numSeqs;i++){
2092 groupFile << seqNameVector[i] << '\t' << fileRoot << endl;
2096 catch(exception& e) {
2097 m->errorOut(e, "ShhherCommand", "writeGroups");
2102 /**************************************************************************************************/
2104 void ShhherCommand::writeClusters(vector<int> otuCounts){
2106 string otuCountsFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.counts";
2107 ofstream otuCountsFile;
2108 m->openOutputFile(otuCountsFileName, otuCountsFile);
2110 string bases = "TACG";
2112 for(int i=0;i<numOTUs;i++){
2113 //output the translated version of the centroid sequence for the otu
2114 if(otuCounts[i] > 0){
2115 int index = centroids[i];
2117 otuCountsFile << "ideal\t";
2118 for(int j=8;j<numFlowCells;j++){
2119 char base = bases[j % 4];
2120 for(int s=0;s<uniqueFlowgrams[index * numFlowCells + j];s++){
2121 otuCountsFile << base;
2124 otuCountsFile << endl;
2126 for(int j=0;j<nSeqsPerOTU[i];j++){
2127 int sequence = aaI[i][j];
2128 otuCountsFile << seqNameVector[sequence] << '\t';
2130 for(int k=8;k<lengths[sequence];k++){
2131 char base = bases[k % 4];
2132 int freq = int(0.01 * (double)flowDataIntI[sequence * numFlowCells + k] + 0.5);
2134 for(int s=0;s<freq;s++){
2135 otuCountsFile << base;
2138 otuCountsFile << endl;
2140 otuCountsFile << endl;
2143 otuCountsFile.close();
2145 catch(exception& e) {
2146 m->errorOut(e, "ShhherCommand", "writeClusters");
2151 //**********************************************************************************************************************