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"
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"
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 globaldata = GlobalData::getInstance();
222 catch(exception& e) {
223 m->errorOut(e, "ShhherCommand", "ShhherCommand");
228 //**********************************************************************************************************************
230 ShhherCommand::~ShhherCommand(){}
232 //**********************************************************************************************************************
234 void ShhherCommand::help(){
236 m->mothurOut("The shhher command reads a file containing flowgrams and creates a file of corrected sequences.\n");
238 catch(exception& e) {
239 m->errorOut(e, "ShhherCommand", "help");
244 //**********************************************************************************************************************
246 int ShhherCommand::execute(){
248 if (abort == true) { if (calledHelp) { return 0; } return 2; }
253 double begClock = clock();
254 unsigned long int begTime = time(NULL);
258 for(int i=1;i<ncpus;i++){
259 MPI_Send(&abort, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
261 if(abort == 1){ return 0; }
265 m->mothurOut("\nGetting preliminary data...\n");
269 vector<string> flowFileVector;
270 if(flowFilesFileName != "not found"){
273 ifstream flowFilesFile;
274 m->openInputFile(flowFilesFileName, flowFilesFile);
275 while(flowFilesFile){
276 flowFilesFile >> fName;
277 flowFileVector.push_back(fName);
278 m->gobble(flowFilesFile);
282 flowFileVector.push_back(flowFileName);
284 int numFiles = flowFileVector.size();
286 for(int i=1;i<ncpus;i++){
287 MPI_Send(&numFiles, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
290 for(int i=0;i<numFiles;i++){
291 flowFileName = flowFileVector[i];
293 m->mothurOut("\n>>>>>\tProcessing " + flowFileName + " (file " + toString(i+1) + " of " + toString(numFiles) + ")\t<<<<<\n");
294 m->mothurOut("Reading flowgrams...\n");
297 m->mothurOut("Identifying unique flowgrams...\n");
300 m->mothurOut("Calculating distances between flowgrams...\n");
302 strcpy(fileName, flowFileName.c_str());
304 for(int i=1;i<ncpus;i++){
305 MPI_Send(&fileName[0], 1024, MPI_CHAR, i, tag, MPI_COMM_WORLD);
307 MPI_Send(&numSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
308 MPI_Send(&numUniques, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
309 MPI_Send(&numFlowCells, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
310 MPI_Send(&flowDataIntI[0], numSeqs * numFlowCells, MPI_SHORT, i, tag, MPI_COMM_WORLD);
311 MPI_Send(&flowDataPrI[0], numSeqs * numFlowCells, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
312 MPI_Send(&mapUniqueToSeq[0], numSeqs, MPI_INT, i, tag, MPI_COMM_WORLD);
313 MPI_Send(&mapSeqToUnique[0], numSeqs, MPI_INT, i, tag, MPI_COMM_WORLD);
314 MPI_Send(&lengths[0], numSeqs, MPI_INT, i, tag, MPI_COMM_WORLD);
315 MPI_Send(&jointLookUp[0], NUMBINS * NUMBINS, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
316 MPI_Send(&cutoff, 1, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
319 string distFileName = flowDistMPI(0, int(sqrt(1.0/float(ncpus)) * numUniques));
322 for(int i=1;i<ncpus;i++){
323 MPI_Recv(&done, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
325 m->appendFiles((distFileName + ".temp." + toString(i)), distFileName);
326 remove((distFileName + ".temp." + toString(i)).c_str());
329 string namesFileName = createNamesFile();
331 m->mothurOut("\nClustering flowgrams...\n");
332 string listFileName = cluster(distFileName, namesFileName);
334 getOTUData(listFileName);
337 for(int i=1;i<ncpus;i++){
338 MPI_Send(&numOTUs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
339 MPI_Send(&singleLookUp[0], singleLookUp.size(), MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
340 MPI_Send(&uniqueFlowgrams[0], numFlowCells * numUniques, MPI_SHORT, i, tag, MPI_COMM_WORLD);
341 MPI_Send(&sigma, 1, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
348 int numOTUsOnCPU = numOTUs / ncpus;
349 int numSeqsOnCPU = numSeqs / ncpus;
350 m->mothurOut("\nDenoising flowgrams...\n");
351 m->mothurOut("iter\tmaxDelta\tnLL\t\tcycletime\n");
353 while((maxIters == 0 && maxDelta > minDelta) || iter < MIN_ITER || (maxDelta > minDelta && iter < maxIters)){
355 double cycClock = clock();
356 unsigned long int cycTime = time(NULL);
359 int total = singleTau.size();
360 for(int i=1;i<ncpus;i++){
361 MPI_Send(&total, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
362 MPI_Send(&change[0], numOTUs, MPI_SHORT, i, tag, MPI_COMM_WORLD);
363 MPI_Send(¢roids[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
365 MPI_Send(&singleTau[0], total, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
366 MPI_Send(&seqNumber[0], total, MPI_INT, i, tag, MPI_COMM_WORLD);
367 MPI_Send(&seqIndex[0], total, MPI_INT, i, tag, MPI_COMM_WORLD);
368 MPI_Send(&nSeqsPerOTU[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
369 MPI_Send(&cumNumSeqs[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
372 calcCentroidsDriver(0, numOTUsOnCPU);
374 for(int i=1;i<ncpus;i++){
375 int otuStart = i * numOTUs / ncpus;
376 int otuStop = (i + 1) * numOTUs / ncpus;
378 vector<int> tempCentroids(numOTUs, 0);
379 vector<short> tempChange(numOTUs, 0);
381 MPI_Recv(&tempCentroids[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
382 MPI_Recv(&tempChange[0], numOTUs, MPI_SHORT, i, tag, MPI_COMM_WORLD, &status);
384 for(int j=otuStart;j<otuStop;j++){
385 centroids[j] = tempCentroids[j];
386 change[j] = tempChange[j];
390 maxDelta = getNewWeights();
391 double nLL = getLikelihood();
394 for(int i=1;i<ncpus;i++){
395 MPI_Send(¢roids[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
396 MPI_Send(&weight[0], numOTUs, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
397 MPI_Send(&change[0], numOTUs, MPI_SHORT, i, tag, MPI_COMM_WORLD);
400 calcNewDistancesParent(0, numSeqsOnCPU);
402 total = singleTau.size();
404 for(int i=1;i<ncpus;i++){
406 int seqStart = i * numSeqs / ncpus;
407 int seqStop = (i + 1) * numSeqs / ncpus;
409 MPI_Recv(&childTotal, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
411 vector<int> childSeqIndex(childTotal, 0);
412 vector<double> childSingleTau(childTotal, 0);
413 vector<double> childDist(numSeqs * numOTUs, 0);
414 vector<int> otuIndex(childTotal, 0);
416 MPI_Recv(&childSeqIndex[0], childTotal, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
417 MPI_Recv(&childSingleTau[0], childTotal, MPI_DOUBLE, i, tag, MPI_COMM_WORLD, &status);
418 MPI_Recv(&childDist[0], numOTUs * numSeqs, MPI_DOUBLE, i, tag, MPI_COMM_WORLD, &status);
419 MPI_Recv(&otuIndex[0], childTotal, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
421 int oldTotal = total;
423 singleTau.resize(total, 0);
424 seqIndex.resize(total, 0);
425 seqNumber.resize(total, 0);
429 for(int j=oldTotal;j<total;j++){
430 int otuI = otuIndex[childIndex];
431 int seqI = childSeqIndex[childIndex];
433 singleTau[j] = childSingleTau[childIndex];
435 aaP[otuI][nSeqsPerOTU[otuI]] = j;
436 aaI[otuI][nSeqsPerOTU[otuI]] = seqI;
441 int index = seqStart * numOTUs;
442 for(int j=seqStart;j<seqStop;j++){
443 for(int k=0;k<numOTUs;k++){
444 dist[index] = childDist[index];
452 m->mothurOut(toString(iter) + '\t' + toString(maxDelta) + '\t' + toString(nLL) + '\t' + toString(time(NULL) - cycTime) + '\t' + toString((clock() - cycClock)/(double)CLOCKS_PER_SEC) + '\n');
454 if((maxIters == 0 && maxDelta > minDelta) || iter < MIN_ITER || (maxDelta > minDelta && iter < maxIters)){
456 for(int i=1;i<ncpus;i++){
457 MPI_Send(&live, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
462 for(int i=1;i<ncpus;i++){
463 MPI_Send(&live, 1, MPI_INT, i, tag, MPI_COMM_WORLD); //send kill command
469 m->mothurOut("\nFinalizing...\n");
472 vector<int> otuCounts(numOTUs, 0);
473 for(int i=0;i<numSeqs;i++) { otuCounts[otuData[i]]++; }
474 calcCentroidsDriver(0, numOTUs);
475 writeQualities(otuCounts);
476 writeSequences(otuCounts);
477 writeNames(otuCounts);
478 writeClusters(otuCounts);
481 remove(distFileName.c_str());
482 remove(namesFileName.c_str());
483 remove(listFileName.c_str());
485 m->mothurOut("Total time to process " + toString(flowFileName) + ":\t" + toString(time(NULL) - begTime) + '\t' + toString((clock() - begClock)/(double)CLOCKS_PER_SEC) + '\n');
492 MPI_Recv(&abort, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
493 if(abort){ return 0; }
496 MPI_Recv(&numFiles, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
498 for(int i=0;i<numFiles;i++){
499 //Now into the pyrodist part
501 MPI_Recv(&fileName, 1024, MPI_CHAR, 0, tag, MPI_COMM_WORLD, &status);
502 MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
503 MPI_Recv(&numUniques, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
504 MPI_Recv(&numFlowCells, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
506 flowDataIntI.resize(numSeqs * numFlowCells);
507 flowDataPrI.resize(numSeqs * numFlowCells);
508 mapUniqueToSeq.resize(numSeqs);
509 mapSeqToUnique.resize(numSeqs);
510 lengths.resize(numSeqs);
511 jointLookUp.resize(NUMBINS * NUMBINS);
513 MPI_Recv(&flowDataIntI[0], numSeqs * numFlowCells, MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
514 MPI_Recv(&flowDataPrI[0], numSeqs * numFlowCells, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
515 MPI_Recv(&mapUniqueToSeq[0], numSeqs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
516 MPI_Recv(&mapSeqToUnique[0], numSeqs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
517 MPI_Recv(&lengths[0], numSeqs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
518 MPI_Recv(&jointLookUp[0], NUMBINS * NUMBINS, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
519 MPI_Recv(&cutoff, 1, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
521 flowFileName = string(fileName);
522 int flowDistStart = int(sqrt(float(pid)/float(ncpus)) * numUniques);
523 int flowDistEnd = int(sqrt(float(pid+1)/float(ncpus)) * numUniques);
525 string distanceStringChild = flowDistMPI(flowDistStart, flowDistEnd);
528 MPI_Send(&done, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
530 //Now into the pyronoise part
531 MPI_Recv(&numOTUs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
533 singleLookUp.resize(HOMOPS * NUMBINS);
534 uniqueFlowgrams.resize(numUniques * numFlowCells);
535 weight.resize(numOTUs);
536 centroids.resize(numOTUs);
537 change.resize(numOTUs);
538 dist.assign(numOTUs * numSeqs, 0);
539 nSeqsPerOTU.resize(numOTUs);
540 cumNumSeqs.resize(numOTUs);
542 MPI_Recv(&singleLookUp[0], singleLookUp.size(), MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
543 MPI_Recv(&uniqueFlowgrams[0], uniqueFlowgrams.size(), MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
544 MPI_Recv(&sigma, 1, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
546 int startOTU = pid * numOTUs / ncpus;
547 int endOTU = (pid + 1) * numOTUs / ncpus;
549 int startSeq = pid * numSeqs / ncpus;
550 int endSeq = (pid + 1) * numSeqs /ncpus;
556 MPI_Recv(&total, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
557 singleTau.assign(total, 0.0000);
558 seqNumber.assign(total, 0);
559 seqIndex.assign(total, 0);
561 MPI_Recv(&change[0], numOTUs, MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
562 MPI_Recv(¢roids[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
563 MPI_Recv(&singleTau[0], total, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
564 MPI_Recv(&seqNumber[0], total, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
565 MPI_Recv(&seqIndex[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
566 MPI_Recv(&nSeqsPerOTU[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
567 MPI_Recv(&cumNumSeqs[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
569 calcCentroidsDriver(startOTU, endOTU);
571 MPI_Send(¢roids[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD);
572 MPI_Send(&change[0], numOTUs, MPI_SHORT, 0, tag, MPI_COMM_WORLD);
575 MPI_Recv(¢roids[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
576 MPI_Recv(&weight[0], numOTUs, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
577 MPI_Recv(&change[0], numOTUs, MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
579 vector<int> otuIndex(total, 0);
580 calcNewDistancesChildMPI(startSeq, endSeq, otuIndex);
581 total = otuIndex.size();
583 MPI_Send(&total, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
584 MPI_Send(&seqIndex[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD);
585 MPI_Send(&singleTau[0], total, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD);
586 MPI_Send(&dist[0], numOTUs * numSeqs, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD);
587 MPI_Send(&otuIndex[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD);
589 MPI_Recv(&live, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
594 MPI_Barrier(MPI_COMM_WORLD);
598 catch(exception& e) {
599 m->errorOut(e, "ShhherCommand", "execute");
604 /**************************************************************************************************/
606 string ShhherCommand::flowDistMPI(int startSeq, int stopSeq){
608 ostringstream outStream;
609 outStream.setf(ios::fixed, ios::floatfield);
610 outStream.setf(ios::dec, ios::basefield);
611 outStream.setf(ios::showpoint);
612 outStream.precision(6);
614 int begTime = time(NULL);
615 double begClock = clock();
617 for(int i=startSeq;i<stopSeq;i++){
618 for(int j=0;j<i;j++){
619 float flowDistance = calcPairwiseDist(mapUniqueToSeq[i], mapUniqueToSeq[j]);
621 if(flowDistance < 1e-6){
622 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << 0.000000 << endl;
624 else if(flowDistance <= cutoff){
625 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << flowDistance << endl;
629 m->mothurOut(toString(i) + '\t' + toString(time(NULL) - begTime) + '\t' + toString((clock()-begClock)/CLOCKS_PER_SEC) + '\n');
633 m->mothurOut(toString(stopSeq) + '\t' + toString(time(NULL) - begTime) + '\t' + toString((clock()-begClock)/CLOCKS_PER_SEC) + '\n');
635 string fDistFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.dist";
636 if(pid != 0){ fDistFileName += ".temp." + toString(pid); }
638 ofstream distFile(fDistFileName.c_str());
639 distFile << outStream.str();
642 return fDistFileName;
644 catch(exception& e) {
645 m->errorOut(e, "ShhherCommand", "flowDistParentFork");
651 //**********************************************************************************************************************
653 int ShhherCommand::execute(){
655 if (abort == true) { return 0; }
660 vector<string> flowFileVector;
661 if(flowFilesFileName != "not found"){
664 ifstream flowFilesFile;
665 m->openInputFile(flowFilesFileName, flowFilesFile);
666 while(flowFilesFile){
667 flowFilesFile >> fName;
668 flowFileVector.push_back(fName);
669 m->gobble(flowFilesFile);
673 flowFileVector.push_back(flowFileName);
675 int numFiles = flowFileVector.size();
678 for(int i=0;i<numFiles;i++){
679 flowFileName = flowFileVector[i];
681 m->mothurOut("\n>>>>>\tProcessing " + flowFileName + " (file " + toString(i+1) + " of " + toString(numFiles) + ")\t<<<<<\n");
682 m->mothurOut("Reading flowgrams...\n");
685 m->mothurOut("Identifying unique flowgrams...\n");
689 m->mothurOut("Calculating distances between flowgrams...\n");
690 string distFileName = createDistFile(processors);
691 string namesFileName = createNamesFile();
693 m->mothurOut("\nClustering flowgrams...\n");
694 string listFileName = cluster(distFileName, namesFileName);
695 getOTUData(listFileName);
702 double begClock = clock();
703 unsigned long int begTime = time(NULL);
705 m->mothurOut("\nDenoising flowgrams...\n");
706 m->mothurOut("iter\tmaxDelta\tnLL\t\tcycletime\n");
708 while((maxIters == 0 && maxDelta > minDelta) || iter < MIN_ITER || (maxDelta > minDelta && iter < maxIters)){
710 double cycClock = clock();
711 unsigned long int cycTime = time(NULL);
716 maxDelta = getNewWeights();
717 double nLL = getLikelihood();
724 m->mothurOut(toString(iter) + '\t' + toString(maxDelta) + '\t' + toString(nLL) + '\t' + toString(time(NULL) - cycTime) + '\t' + toString((clock() - cycClock)/(double)CLOCKS_PER_SEC) + '\n');
728 m->mothurOut("\nFinalizing...\n");
732 vector<int> otuCounts(numOTUs, 0);
733 for(int i=0;i<numSeqs;i++) { otuCounts[otuData[i]]++; }
735 calcCentroidsDriver(0, numOTUs);
736 writeQualities(otuCounts);
737 writeSequences(otuCounts);
738 writeNames(otuCounts);
739 writeClusters(otuCounts);
742 remove(distFileName.c_str());
743 remove(namesFileName.c_str());
744 remove(listFileName.c_str());
746 m->mothurOut("Total time to process " + flowFileName + ":\t" + toString(time(NULL) - begTime) + '\t' + toString((clock() - begClock)/(double)CLOCKS_PER_SEC) + '\n');
750 catch(exception& e) {
751 m->errorOut(e, "ShhherCommand", "execute");
756 /**************************************************************************************************/
758 void ShhherCommand::getFlowData(){
761 m->openInputFile(flowFileName, flowFile);
765 int currentNumFlowCells;
769 flowFile >> numFlowCells;
770 int index = 0;//pcluster
771 while(!flowFile.eof()){
772 flowFile >> seqName >> currentNumFlowCells;
773 lengths.push_back(currentNumFlowCells);
775 seqNameVector.push_back(seqName);
776 nameMap[seqName] = index++;//pcluster
778 for(int i=0;i<numFlowCells;i++){
779 flowFile >> intensity;
780 if(intensity > 9.99) { intensity = 9.99; }
781 int intI = int(100 * intensity + 0.0001);
782 flowDataIntI.push_back(intI);
788 numSeqs = seqNameVector.size();
790 for(int i=0;i<numSeqs;i++){
791 int iNumFlowCells = i * numFlowCells;
792 for(int j=lengths[i];j<numFlowCells;j++){
793 flowDataIntI[iNumFlowCells + j] = 0;
798 catch(exception& e) {
799 m->errorOut(e, "ShhherCommand", "getFlowData");
804 /**************************************************************************************************/
806 void ShhherCommand::getSingleLookUp(){
808 // these are the -log probabilities that a signal corresponds to a particular homopolymer length
809 singleLookUp.assign(HOMOPS * NUMBINS, 0);
813 m->openInputFile(lookupFileName, lookUpFile);
815 for(int i=0;i<HOMOPS;i++){
817 lookUpFile >> logFracFreq;
819 for(int j=0;j<NUMBINS;j++) {
820 lookUpFile >> singleLookUp[index];
826 catch(exception& e) {
827 m->errorOut(e, "ShhherCommand", "getSingleLookUp");
832 /**************************************************************************************************/
834 void ShhherCommand::getJointLookUp(){
837 // the most likely joint probability (-log) that two intenities have the same polymer length
838 jointLookUp.resize(NUMBINS * NUMBINS, 0);
840 for(int i=0;i<NUMBINS;i++){
841 for(int j=0;j<NUMBINS;j++){
843 double minSum = 100000000;
845 for(int k=0;k<HOMOPS;k++){
846 double sum = singleLookUp[k * NUMBINS + i] + singleLookUp[k * NUMBINS + j];
848 if(sum < minSum) { minSum = sum; }
850 jointLookUp[i * NUMBINS + j] = minSum;
854 catch(exception& e) {
855 m->errorOut(e, "ShhherCommand", "getJointLookUp");
860 /**************************************************************************************************/
862 double ShhherCommand::getProbIntensity(int intIntensity){
865 double minNegLogProb = 100000000;
868 for(int i=0;i<HOMOPS;i++){//loop signal strength
869 float negLogProb = singleLookUp[i * NUMBINS + intIntensity];
870 if(negLogProb < minNegLogProb) { minNegLogProb = negLogProb; }
873 return minNegLogProb;
875 catch(exception& e) {
876 m->errorOut(e, "ShhherCommand", "getProbIntensity");
881 /**************************************************************************************************/
883 void ShhherCommand::getUniques(){
888 uniqueFlowgrams.assign(numFlowCells * numSeqs, -1);
889 uniqueCount.assign(numSeqs, 0); // anWeights
890 uniqueLengths.assign(numSeqs, 0);
891 mapSeqToUnique.assign(numSeqs, -1);
892 mapUniqueToSeq.assign(numSeqs, -1);
894 vector<short> uniqueFlowDataIntI(numFlowCells * numSeqs, -1);
896 for(int i=0;i<numSeqs;i++){
899 vector<short> current(numFlowCells);
900 for(int j=0;j<numFlowCells;j++){
901 current[j] = short(((flowDataIntI[i * numFlowCells + j] + 50.0)/100.0));
904 for(int j=0;j<numUniques;j++){
905 int offset = j * numFlowCells;
909 if(lengths[i] < uniqueLengths[j]) { shorterLength = lengths[i]; }
910 else { shorterLength = uniqueLengths[j]; }
912 for(int k=0;k<shorterLength;k++){
913 if(current[k] != uniqueFlowgrams[offset + k]){
920 mapSeqToUnique[i] = j;
923 if(lengths[i] > uniqueLengths[j]) { uniqueLengths[j] = lengths[i]; }
929 if(index == numUniques){
930 uniqueLengths[numUniques] = lengths[i];
931 uniqueCount[numUniques] = 1;
932 mapSeqToUnique[i] = numUniques;//anMap
933 mapUniqueToSeq[numUniques] = i;//anF
935 for(int k=0;k<numFlowCells;k++){
936 uniqueFlowgrams[numUniques * numFlowCells + k] = current[k];
937 uniqueFlowDataIntI[numUniques * numFlowCells + k] = flowDataIntI[i * numFlowCells + k];
943 uniqueFlowDataIntI.resize(numFlowCells * numUniques);
944 uniqueLengths.resize(numUniques);
946 flowDataPrI.assign(numSeqs * numFlowCells, 0);
947 for(int i=0;i<flowDataPrI.size();i++) { flowDataPrI[i] = getProbIntensity(flowDataIntI[i]); }
949 catch(exception& e) {
950 m->errorOut(e, "ShhherCommand", "getUniques");
955 /**************************************************************************************************/
957 float ShhherCommand::calcPairwiseDist(int seqA, int seqB){
959 int minLength = lengths[mapSeqToUnique[seqA]];
960 if(lengths[seqB] < minLength){ minLength = lengths[mapSeqToUnique[seqB]]; }
962 int ANumFlowCells = seqA * numFlowCells;
963 int BNumFlowCells = seqB * numFlowCells;
967 for(int i=0;i<minLength;i++){
968 int flowAIntI = flowDataIntI[ANumFlowCells + i];
969 float flowAPrI = flowDataPrI[ANumFlowCells + i];
971 int flowBIntI = flowDataIntI[BNumFlowCells + i];
972 float flowBPrI = flowDataPrI[BNumFlowCells + i];
973 dist += jointLookUp[flowAIntI * NUMBINS + flowBIntI] - flowAPrI - flowBPrI;
976 dist /= (float) minLength;
979 catch(exception& e) {
980 m->errorOut(e, "ShhherCommand", "calcPairwiseDist");
985 /**************************************************************************************************/
987 void ShhherCommand::flowDistParentFork(string distFileName, int startSeq, int stopSeq){
990 ostringstream outStream;
991 outStream.setf(ios::fixed, ios::floatfield);
992 outStream.setf(ios::dec, ios::basefield);
993 outStream.setf(ios::showpoint);
994 outStream.precision(6);
996 int begTime = time(NULL);
997 double begClock = clock();
999 for(int i=startSeq;i<stopSeq;i++){
1000 for(int j=0;j<i;j++){
1001 float flowDistance = calcPairwiseDist(mapUniqueToSeq[i], mapUniqueToSeq[j]);
1003 if(flowDistance < 1e-6){
1004 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << 0.000000 << endl;
1006 else if(flowDistance <= cutoff){
1007 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << flowDistance << endl;
1011 m->mothurOut(toString(i) + "\t" + toString(time(NULL) - begTime));
1012 m->mothurOut("\t" + toString((clock()-begClock)/CLOCKS_PER_SEC));
1013 m->mothurOutEndLine();
1016 m->mothurOut(toString(stopSeq-1) + "\t" + toString(time(NULL) - begTime));
1017 m->mothurOut("\t" + toString((clock()-begClock)/CLOCKS_PER_SEC));
1018 m->mothurOutEndLine();
1020 ofstream distFile(distFileName.c_str());
1021 distFile << outStream.str();
1024 catch(exception& e) {
1025 m->errorOut(e, "ShhherCommand", "flowDistParentFork");
1030 /**************************************************************************************************/
1032 string ShhherCommand::createDistFile(int processors){
1034 string fDistFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.dist";
1036 unsigned long int begTime = time(NULL);
1037 double begClock = clock();
1042 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
1043 if(processors == 1) { flowDistParentFork(fDistFileName, 0, numUniques); }
1044 else{ //you have multiple processors
1046 if (numSeqs < processors){ processors = 1; }
1048 vector<int> start(processors, 0);
1049 vector<int> end(processors, 0);
1051 for (int i = 0; i < processors; i++) {
1052 start[i] = int(sqrt(float(i)/float(processors)) * numUniques);
1053 end[i] = int(sqrt(float(i+1)/float(processors)) * numUniques);
1057 vector<int> processIDs;
1059 //loop through and create all the processes you want
1060 while (process != processors) {
1064 processIDs.push_back(pid); //create map from line number to pid so you can append files in correct order later
1066 }else if (pid == 0){
1067 flowDistParentFork(fDistFileName + toString(getpid()) + ".temp", start[process], end[process]);
1070 m->mothurOut("[ERROR]: unable to spawn the necessary processes. Error code: " + toString(pid)); m->mothurOutEndLine();
1072 for (int i=0;i<processIDs.size();i++) { int temp = processIDs[i]; kill (temp, SIGINT); }
1077 //parent does its part
1078 flowDistParentFork(fDistFileName, start[0], end[0]);
1080 //force parent to wait until all the processes are done
1081 for (int i=0;i<processIDs.size();i++) {
1082 int temp = processIDs[i];
1086 //append and remove temp files
1087 for (int i=0;i<processIDs.size();i++) {
1088 m->appendFiles((fDistFileName + toString(processIDs[i]) + ".temp"), fDistFileName);
1089 remove((fDistFileName + toString(processIDs[i]) + ".temp").c_str());
1095 flowDistParentFork(fDistFileName, 0, numUniques);
1098 m->mothurOutEndLine();
1100 m-mothurOut("Total time: " + toString(time(NULL) - begTime) + '\t' + toString((clock() - begClock)/CLOCKS_PER_SEC) + '\n');
1103 return fDistFileName;
1105 catch(exception& e) {
1106 m->errorOut(e, "ShhherCommand", "createDistFile");
1112 /**************************************************************************************************/
1114 string ShhherCommand::createNamesFile(){
1117 vector<string> duplicateNames(numUniques, "");
1118 for(int i=0;i<numSeqs;i++){
1119 duplicateNames[mapSeqToUnique[i]] += seqNameVector[i] + ',';
1122 string nameFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.names";
1125 m->openOutputFile(nameFileName, nameFile);
1127 for(int i=0;i<numUniques;i++){
1128 // nameFile << seqNameVector[mapUniqueToSeq[i]] << '\t' << duplicateNames[i].substr(0, duplicateNames[i].find_last_of(',')) << endl;
1129 nameFile << mapUniqueToSeq[i] << '\t' << duplicateNames[i].substr(0, duplicateNames[i].find_last_of(',')) << endl;
1133 return nameFileName;
1135 catch(exception& e) {
1136 m->errorOut(e, "ShhherCommand", "createNamesFile");
1141 //**********************************************************************************************************************
1143 string ShhherCommand::cluster(string distFileName, string namesFileName){
1147 globaldata->setNameFile(namesFileName);
1148 globaldata->setColumnFile(distFileName);
1149 globaldata->setFormat("column");
1151 ReadMatrix* read = new ReadColumnMatrix(distFileName);
1152 read->setCutoff(cutoff);
1154 NameAssignment* clusterNameMap = new NameAssignment(namesFileName);
1155 clusterNameMap->readMap();
1156 read->read(clusterNameMap);
1158 ListVector* list = read->getListVector();
1159 SparseMatrix* matrix = read->getMatrix();
1162 delete clusterNameMap;
1164 RAbundVector* rabund = new RAbundVector(list->getRAbundVector());
1166 Cluster* cluster = new CompleteLinkage(rabund, list, matrix, cutoff, "furthest");
1167 string tag = cluster->getTag();
1169 double clusterCutoff = cutoff;
1170 while (matrix->getSmallDist() <= clusterCutoff && matrix->getNNodes() > 0){
1171 cluster->update(clusterCutoff);
1174 list->setLabel(toString(cutoff));
1176 string listFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.list";
1178 m->openOutputFile(listFileName, listFile);
1179 list->print(listFile);
1182 delete matrix; delete cluster; delete rabund; delete list;
1184 return listFileName;
1186 catch(exception& e) {
1187 m->errorOut(e, "ShhherCommand", "cluster");
1192 /**************************************************************************************************/
1194 void ShhherCommand::getOTUData(string listFileName){
1198 m->openInputFile(listFileName, listFile);
1201 listFile >> label >> numOTUs;
1203 otuData.assign(numSeqs, 0);
1204 cumNumSeqs.assign(numOTUs, 0);
1205 nSeqsPerOTU.assign(numOTUs, 0);
1206 aaP.resize(numOTUs);
1208 string singleOTU = "";
1210 for(int i=0;i<numOTUs;i++){
1212 listFile >> singleOTU;
1214 istringstream otuString(singleOTU);
1218 string seqName = "";
1220 for(int j=0;j<singleOTU.length();j++){
1221 char letter = otuString.get();
1227 map<string,int>::iterator nmIt = nameMap.find(seqName);
1228 int index = nmIt->second;
1230 nameMap.erase(nmIt);
1234 aaP[i].push_back(index);
1239 map<string,int>::iterator nmIt = nameMap.find(seqName);
1241 int index = nmIt->second;
1242 nameMap.erase(nmIt);
1246 aaP[i].push_back(index);
1251 sort(aaP[i].begin(), aaP[i].end());
1252 for(int j=0;j<nSeqsPerOTU[i];j++){
1253 seqNumber.push_back(aaP[i][j]);
1255 for(int j=nSeqsPerOTU[i];j<numSeqs;j++){
1256 aaP[i].push_back(0);
1260 for(int i=1;i<numOTUs;i++){
1261 cumNumSeqs[i] = cumNumSeqs[i-1] + nSeqsPerOTU[i-1];
1264 seqIndex = seqNumber;
1268 catch(exception& e) {
1269 m->errorOut(e, "ShhherCommand", "getOTUData");
1274 /**************************************************************************************************/
1276 void ShhherCommand::initPyroCluster(){
1278 dist.assign(numSeqs * numOTUs, 0);
1279 change.assign(numOTUs, 1);
1280 centroids.assign(numOTUs, -1);
1281 weight.assign(numOTUs, 0);
1282 singleTau.assign(numSeqs, 1.0);
1284 nSeqsBreaks.assign(processors+1, 0);
1285 nOTUsBreaks.assign(processors+1, 0);
1288 for(int i=0;i<processors;i++){
1289 nSeqsBreaks[i+1] = nSeqsBreaks[i] + (int)((double) numSeqs / (double) processors);
1290 nOTUsBreaks[i+1] = nOTUsBreaks[i] + (int)((double) numOTUs / (double) processors);
1292 nSeqsBreaks[processors] = numSeqs;
1293 nOTUsBreaks[processors] = numOTUs;
1295 catch(exception& e) {
1296 m->errorOut(e, "ShhherCommand", "initPyroCluster");
1301 /**************************************************************************************************/
1303 void ShhherCommand::fill(){
1306 for(int i=0;i<numOTUs;i++){
1307 cumNumSeqs[i] = index;
1308 for(int j=0;j<nSeqsPerOTU[i];j++){
1309 seqNumber[index] = aaP[i][j];
1310 seqIndex[index] = aaI[i][j];
1316 catch(exception& e) {
1317 m->errorOut(e, "ShhherCommand", "fill");
1322 /**************************************************************************************************/
1324 void ShhherCommand::calcCentroids(){
1327 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
1329 if(processors == 1) {
1330 calcCentroidsDriver(0, numOTUs);
1332 else{ //you have multiple processors
1333 if (numOTUs < processors){ processors = 1; }
1336 vector<int> processIDs;
1338 //loop through and create all the processes you want
1339 while (process != processors) {
1343 processIDs.push_back(pid); //create map from line number to pid so you can append files in correct order later
1345 }else if (pid == 0){
1346 calcCentroidsDriver(nOTUsBreaks[process], nOTUsBreaks[process+1]);
1349 m->mothurOut("[ERROR]: unable to spawn the necessary processes. Error code: " + toString(pid)); m->mothurOutEndLine();
1351 for (int i=0;i<processIDs.size();i++) { int temp = processIDs[i]; kill (temp, SIGINT); }
1356 //parent does its part
1357 calcCentroidsDriver(nOTUsBreaks[0], nOTUsBreaks[1]);
1359 //force parent to wait until all the processes are done
1360 for (int i=0;i<processIDs.size();i++) {
1361 int temp = processIDs[i];
1367 calcCentroidsDriver(0, numOTUs);
1370 catch(exception& e) {
1371 m->errorOut(e, "ShhherCommand", "calcCentroidsDriver");
1376 /**************************************************************************************************/
1378 void ShhherCommand::calcCentroidsDriver(int start, int finish){
1380 //this function gets the most likely homopolymer length at a flow position for a group of sequences
1385 for(int i=start;i<finish;i++){
1389 int minFlowGram = 100000000;
1390 double minFlowValue = 1e8;
1391 change[i] = 0; //FALSE
1393 for(int j=0;j<nSeqsPerOTU[i];j++){
1394 count += singleTau[seqNumber[cumNumSeqs[i] + j]];
1397 if(nSeqsPerOTU[i] > 0 && count > MIN_COUNT){
1398 vector<double> adF(nSeqsPerOTU[i]);
1399 vector<int> anL(nSeqsPerOTU[i]);
1401 for(int j=0;j<nSeqsPerOTU[i];j++){
1402 int index = cumNumSeqs[i] + j;
1403 int nI = seqIndex[index];
1404 int nIU = mapSeqToUnique[nI];
1407 for(k=0;k<position;k++){
1413 anL[position] = nIU;
1414 adF[position] = 0.0000;
1419 for(int j=0;j<nSeqsPerOTU[i];j++){
1420 int index = cumNumSeqs[i] + j;
1421 int nI = seqIndex[index];
1423 double tauValue = singleTau[seqNumber[index]];
1425 for(int k=0;k<position;k++){
1426 double dist = getDistToCentroid(anL[k], nI, lengths[nI]);
1427 adF[k] += dist * tauValue;
1431 for(int j=0;j<position;j++){
1432 if(adF[j] < minFlowValue){
1434 minFlowValue = adF[j];
1438 if(centroids[i] != anL[minFlowGram]){
1440 centroids[i] = anL[minFlowGram];
1443 else if(centroids[i] != -1){
1449 catch(exception& e) {
1450 m->errorOut(e, "ShhherCommand", "calcCentroidsDriver");
1455 /**************************************************************************************************/
1457 double ShhherCommand::getDistToCentroid(int cent, int flow, int length){
1460 int flowAValue = cent * numFlowCells;
1461 int flowBValue = flow * numFlowCells;
1465 for(int i=0;i<length;i++){
1466 dist += singleLookUp[uniqueFlowgrams[flowAValue] * NUMBINS + flowDataIntI[flowBValue]];
1471 return dist / (double)length;
1473 catch(exception& e) {
1474 m->errorOut(e, "ShhherCommand", "getDistToCentroid");
1479 /**************************************************************************************************/
1481 double ShhherCommand::getNewWeights(){
1484 double maxChange = 0;
1486 for(int i=0;i<numOTUs;i++){
1488 double difference = weight[i];
1491 for(int j=0;j<nSeqsPerOTU[i];j++){
1492 int index = cumNumSeqs[i] + j;
1493 double tauValue = singleTau[seqNumber[index]];
1494 weight[i] += tauValue;
1497 difference = fabs(weight[i] - difference);
1498 if(difference > maxChange){ maxChange = difference; }
1502 catch(exception& e) {
1503 m->errorOut(e, "ShhherCommand", "getNewWeights");
1508 /**************************************************************************************************/
1510 double ShhherCommand::getLikelihood(){
1514 vector<long double> P(numSeqs, 0);
1517 for(int i=0;i<numOTUs;i++){
1518 if(weight[i] > MIN_WEIGHT){
1524 for(int i=0;i<numOTUs;i++){
1525 for(int j=0;j<nSeqsPerOTU[i];j++){
1526 int index = cumNumSeqs[i] + j;
1527 int nI = seqIndex[index];
1528 double singleDist = dist[seqNumber[index]];
1530 P[nI] += weight[i] * exp(-singleDist * sigma);
1534 for(int i=0;i<numSeqs;i++){
1535 if(P[i] == 0){ P[i] = DBL_EPSILON; }
1540 nLL = nLL -(double)numSeqs * log(sigma);
1544 catch(exception& e) {
1545 m->errorOut(e, "ShhherCommand", "getNewWeights");
1550 /**************************************************************************************************/
1552 void ShhherCommand::checkCentroids(){
1554 vector<int> unique(numOTUs, 1);
1556 for(int i=0;i<numOTUs;i++){
1557 if(centroids[i] == -1 || weight[i] < MIN_WEIGHT){
1562 for(int i=0;i<numOTUs;i++){
1564 for(int j=i+1;j<numOTUs;j++){
1567 if(centroids[j] == centroids[i]){
1571 weight[i] += weight[j];
1579 catch(exception& e) {
1580 m->errorOut(e, "ShhherCommand", "checkCentroids");
1585 /**************************************************************************************************/
1587 void ShhherCommand::calcNewDistances(){
1590 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
1592 if(processors == 1) {
1593 calcNewDistancesParent(0, numSeqs);
1595 else{ //you have multiple processors
1596 if (numSeqs < processors){ processors = 1; }
1598 vector<vector<int> > child_otuIndex(processors);
1599 vector<vector<int> > child_seqIndex(processors);
1600 vector<vector<double> > child_singleTau(processors);
1601 vector<int> totals(processors);
1604 vector<int> processIDs;
1606 //loop through and create all the processes you want
1607 while (process != processors) {
1611 processIDs.push_back(pid); //create map from line number to pid so you can append files in correct order later
1613 }else if (pid == 0){
1614 calcNewDistancesChild(nSeqsBreaks[process], nSeqsBreaks[process+1], child_otuIndex[process], child_seqIndex[process], child_singleTau[process]);
1615 totals[process] = child_otuIndex[process].size();
1619 m->mothurOut("[ERROR]: unable to spawn the necessary processes. Error code: " + toString(pid)); m->mothurOutEndLine();
1621 for (int i=0;i<processIDs.size();i++) { int temp = processIDs[i]; kill (temp, SIGINT); }
1626 //parent does its part
1627 calcNewDistancesParent(nSeqsBreaks[0], nSeqsBreaks[1]);
1628 int total = seqIndex.size();
1630 //force parent to wait until all the processes are done
1631 for (int i=0;i<processIDs.size();i++) {
1632 int temp = processIDs[i];
1636 for(int i=1;i<processors;i++){
1637 int oldTotal = total;
1640 singleTau.resize(total, 0);
1641 seqIndex.resize(total, 0);
1642 seqNumber.resize(total, 0);
1646 for(int j=oldTotal;j<total;j++){
1647 int otuI = child_otuIndex[i][childIndex];
1648 int seqI = child_seqIndex[i][childIndex];
1650 singleTau[j] = child_singleTau[i][childIndex];
1651 aaP[otuI][nSeqsPerOTU[otuI]] = j;
1652 aaI[otuI][nSeqsPerOTU[otuI]] = seqI;
1653 nSeqsPerOTU[otuI]++;
1660 calcNewDistancesParent(0, numSeqs);
1663 catch(exception& e) {
1664 m->errorOut(e, "ShhherCommand", "calcNewDistances");
1669 /**************************************************************************************************/
1671 void ShhherCommand::calcNewDistancesChildMPI(int startSeq, int stopSeq, vector<int>& otuIndex){
1674 vector<double> newTau(numOTUs,0);
1675 vector<double> norms(numSeqs, 0);
1678 singleTau.resize(0);
1682 for(int i=startSeq;i<stopSeq;i++){
1683 double offset = 1e8;
1684 int indexOffset = i * numOTUs;
1686 for(int j=0;j<numOTUs;j++){
1688 if(weight[j] > MIN_WEIGHT && change[j] == 1){
1689 dist[indexOffset + j] = getDistToCentroid(centroids[j], i, lengths[i]);
1691 if(weight[j] > MIN_WEIGHT && dist[indexOffset + j] < offset){
1692 offset = dist[indexOffset + j];
1696 for(int j=0;j<numOTUs;j++){
1697 if(weight[j] > MIN_WEIGHT){
1698 newTau[j] = exp(sigma * (-dist[indexOffset + j] + offset)) * weight[j];
1699 norms[i] += newTau[j];
1706 for(int j=0;j<numOTUs;j++){
1708 newTau[j] /= norms[i];
1710 if(newTau[j] > MIN_TAU){
1711 otuIndex.push_back(j);
1712 seqIndex.push_back(i);
1713 singleTau.push_back(newTau[j]);
1719 catch(exception& e) {
1720 m->errorOut(e, "ShhherCommand", "calcNewDistancesChildMPI");
1725 /**************************************************************************************************/
1727 void ShhherCommand::calcNewDistancesChild(int startSeq, int stopSeq, vector<int>& child_otuIndex, vector<int>& child_seqIndex, vector<double>& child_singleTau){
1730 vector<double> newTau(numOTUs,0);
1731 vector<double> norms(numSeqs, 0);
1732 child_otuIndex.resize(0);
1733 child_seqIndex.resize(0);
1734 child_singleTau.resize(0);
1736 for(int i=startSeq;i<stopSeq;i++){
1737 double offset = 1e8;
1738 int indexOffset = i * numOTUs;
1741 for(int j=0;j<numOTUs;j++){
1742 if(weight[j] > MIN_WEIGHT && change[j] == 1){
1743 dist[indexOffset + j] = getDistToCentroid(centroids[j], i, lengths[i]);
1746 if(weight[j] > MIN_WEIGHT && dist[indexOffset + j] < offset){
1747 offset = dist[indexOffset + j];
1751 for(int j=0;j<numOTUs;j++){
1752 if(weight[j] > MIN_WEIGHT){
1753 newTau[j] = exp(sigma * (-dist[indexOffset + j] + offset)) * weight[j];
1754 norms[i] += newTau[j];
1761 for(int j=0;j<numOTUs;j++){
1762 newTau[j] /= norms[i];
1764 if(newTau[j] > MIN_TAU){
1765 child_otuIndex.push_back(j);
1766 child_seqIndex.push_back(i);
1767 child_singleTau.push_back(newTau[j]);
1772 catch(exception& e) {
1773 m->errorOut(e, "ShhherCommand", "calcNewDistancesChild");
1778 /**************************************************************************************************/
1780 void ShhherCommand::calcNewDistancesParent(int startSeq, int stopSeq){
1785 vector<double> newTau(numOTUs,0);
1786 vector<double> norms(numSeqs, 0);
1787 nSeqsPerOTU.assign(numOTUs, 0);
1789 for(int i=startSeq;i<stopSeq;i++){
1790 int indexOffset = i * numOTUs;
1792 double offset = 1e8;
1794 for(int j=0;j<numOTUs;j++){
1795 if(weight[j] > MIN_WEIGHT && change[j] == 1){
1796 dist[indexOffset + j] = getDistToCentroid(centroids[j], i, lengths[i]);
1799 if(weight[j] > MIN_WEIGHT && dist[indexOffset + j] < offset){
1800 offset = dist[indexOffset + j];
1804 for(int j=0;j<numOTUs;j++){
1805 if(weight[j] > MIN_WEIGHT){
1806 newTau[j] = exp(sigma * (-dist[indexOffset + j] + offset)) * weight[j];
1807 norms[i] += newTau[j];
1814 for(int j=0;j<numOTUs;j++){
1815 newTau[j] /= norms[i];
1818 for(int j=0;j<numOTUs;j++){
1819 if(newTau[j] > MIN_TAU){
1821 int oldTotal = total;
1825 singleTau.resize(total, 0);
1826 seqNumber.resize(total, 0);
1827 seqIndex.resize(total, 0);
1829 singleTau[oldTotal] = newTau[j];
1831 aaP[j][nSeqsPerOTU[j]] = oldTotal;
1832 aaI[j][nSeqsPerOTU[j]] = i;
1838 catch(exception& e) {
1839 m->errorOut(e, "ShhherCommand", "calcNewDistancesParent");
1844 /**************************************************************************************************/
1846 void ShhherCommand::setOTUs(){
1849 vector<double> bigTauMatrix(numOTUs * numSeqs, 0.0000);
1851 for(int i=0;i<numOTUs;i++){
1852 for(int j=0;j<nSeqsPerOTU[i];j++){
1853 int index = cumNumSeqs[i] + j;
1854 double tauValue = singleTau[seqNumber[index]];
1855 int sIndex = seqIndex[index];
1856 bigTauMatrix[sIndex * numOTUs + i] = tauValue;
1860 for(int i=0;i<numSeqs;i++){
1861 double maxTau = -1.0000;
1863 for(int j=0;j<numOTUs;j++){
1864 if(bigTauMatrix[i * numOTUs + j] > maxTau){
1865 maxTau = bigTauMatrix[i * numOTUs + j];
1870 otuData[i] = maxOTU;
1873 nSeqsPerOTU.assign(numOTUs, 0);
1875 for(int i=0;i<numSeqs;i++){
1876 int index = otuData[i];
1878 singleTau[i] = 1.0000;
1881 aaP[index][nSeqsPerOTU[index]] = i;
1882 aaI[index][nSeqsPerOTU[index]] = i;
1884 nSeqsPerOTU[index]++;
1888 catch(exception& e) {
1889 m->errorOut(e, "ShhherCommand", "calcNewDistances");
1894 /**************************************************************************************************/
1896 void ShhherCommand::writeQualities(vector<int> otuCounts){
1899 string qualityFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.qual";
1901 ofstream qualityFile;
1902 m->openOutputFile(qualityFileName, qualityFile);
1904 qualityFile.setf(ios::fixed, ios::floatfield);
1905 qualityFile.setf(ios::showpoint);
1906 qualityFile << setprecision(6);
1908 vector<vector<int> > qualities(numOTUs);
1909 vector<double> pr(HOMOPS, 0);
1912 for(int i=0;i<numOTUs;i++){
1916 if(nSeqsPerOTU[i] > 0){
1917 qualities[i].assign(1024, -1);
1919 while(index < numFlowCells){
1920 double maxPrValue = 1e8;
1921 short maxPrIndex = -1;
1922 double count = 0.0000;
1924 pr.assign(HOMOPS, 0);
1926 for(int j=0;j<nSeqsPerOTU[i];j++){
1927 int lIndex = cumNumSeqs[i] + j;
1928 double tauValue = singleTau[seqNumber[lIndex]];
1929 int sequenceIndex = aaI[i][j];
1930 short intensity = flowDataIntI[sequenceIndex * numFlowCells + index];
1934 for(int s=0;s<HOMOPS;s++){
1935 pr[s] += tauValue * singleLookUp[s * NUMBINS + intensity];
1939 maxPrIndex = uniqueFlowgrams[centroids[i] * numFlowCells + index];
1940 maxPrValue = pr[maxPrIndex];
1942 if(count > MIN_COUNT){
1944 double norm = 0.0000;
1946 for(int s=0;s<HOMOPS;s++){
1947 norm += exp(-(pr[s] - maxPrValue));
1950 for(int s=1;s<=maxPrIndex;s++){
1952 double temp = 0.0000;
1954 U += exp(-(pr[s-1]-maxPrValue))/norm;
1962 temp = floor(-10 * temp);
1963 value = (int)floor(temp);
1964 if(value > 100){ value = 100; }
1966 qualities[i][base] = (int)value;
1976 if(otuCounts[i] > 0){
1977 qualityFile << '>' << seqNameVector[mapUniqueToSeq[i]] << endl;
1979 int j=4; //need to get past the first four bases
1980 while(qualities[i][j] != -1){
1981 qualityFile << qualities[i][j] << ' ';
1984 qualityFile << endl;
1987 qualityFile.close();
1990 catch(exception& e) {
1991 m->errorOut(e, "ShhherCommand", "writeQualities");
1996 /**************************************************************************************************/
1998 void ShhherCommand::writeSequences(vector<int> otuCounts){
2000 string bases = "TACG";
2002 string fastaFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.fasta";
2004 m->openOutputFile(fastaFileName, fastaFile);
2006 vector<string> names(numOTUs, "");
2008 for(int i=0;i<numOTUs;i++){
2009 int index = centroids[i];
2011 if(otuCounts[i] > 0){
2012 fastaFile << '>' << seqNameVector[aaI[i][0]] << endl;
2014 for(int j=8;j<numFlowCells;j++){
2016 char base = bases[j % 4];
2017 for(int k=0;k<uniqueFlowgrams[index * numFlowCells + j];k++){
2026 if(compositeFASTAFileName != ""){
2027 m->appendFiles(fastaFileName, compositeFASTAFileName);
2030 catch(exception& e) {
2031 m->errorOut(e, "ShhherCommand", "writeSequences");
2036 /**************************************************************************************************/
2038 void ShhherCommand::writeNames(vector<int> otuCounts){
2040 string nameFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.final.names";
2042 m->openOutputFile(nameFileName, nameFile);
2044 for(int i=0;i<numOTUs;i++){
2045 if(otuCounts[i] > 0){
2046 nameFile << seqNameVector[aaI[i][0]] << '\t' << seqNameVector[aaI[i][0]];
2048 for(int j=1;j<nSeqsPerOTU[i];j++){
2049 nameFile << ',' << seqNameVector[aaI[i][j]];
2057 catch(exception& e) {
2058 m->errorOut(e, "ShhherCommand", "writeNames");
2063 /**************************************************************************************************/
2065 void ShhherCommand::writeGroups(){
2067 string fileRoot = flowFileName.substr(0,flowFileName.find_last_of('.'));
2068 string groupFileName = fileRoot + ".pn.groups";
2070 m->openOutputFile(groupFileName, groupFile);
2072 for(int i=0;i<numSeqs;i++){
2073 groupFile << seqNameVector[i] << '\t' << fileRoot << endl;
2077 catch(exception& e) {
2078 m->errorOut(e, "ShhherCommand", "writeGroups");
2083 /**************************************************************************************************/
2085 void ShhherCommand::writeClusters(vector<int> otuCounts){
2087 string otuCountsFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.counts";
2088 ofstream otuCountsFile;
2089 m->openOutputFile(otuCountsFileName, otuCountsFile);
2091 string bases = "TACG";
2093 for(int i=0;i<numOTUs;i++){
2094 //output the translated version of the centroid sequence for the otu
2095 if(otuCounts[i] > 0){
2096 int index = centroids[i];
2098 otuCountsFile << "ideal\t";
2099 for(int j=8;j<numFlowCells;j++){
2100 char base = bases[j % 4];
2101 for(int s=0;s<uniqueFlowgrams[index * numFlowCells + j];s++){
2102 otuCountsFile << base;
2105 otuCountsFile << endl;
2107 for(int j=0;j<nSeqsPerOTU[i];j++){
2108 int sequence = aaI[i][j];
2109 otuCountsFile << seqNameVector[sequence] << '\t';
2111 for(int k=8;k<lengths[sequence];k++){
2112 char base = bases[k % 4];
2113 int freq = int(0.01 * (double)flowDataIntI[sequence * numFlowCells + k] + 0.5);
2115 for(int s=0;s<freq;s++){
2116 otuCountsFile << base;
2119 otuCountsFile << endl;
2121 otuCountsFile << endl;
2124 otuCountsFile.close();
2126 catch(exception& e) {
2127 m->errorOut(e, "ShhherCommand", "writeClusters");
2132 //**********************************************************************************************************************