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);
256 cout.setf(ios::fixed, ios::floatfield);
257 cout.setf(ios::showpoint);
258 cout << setprecision(2);
263 for(int i=1;i<ncpus;i++){
264 MPI_Send(&abort, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
266 if(abort == 1){ return 0; }
270 cout << "\nGetting preliminary data..." << endl;
274 vector<string> flowFileVector;
275 if(flowFilesFileName != "not found"){
278 ifstream flowFilesFile;
279 m->openInputFile(flowFilesFileName, flowFilesFile);
280 while(flowFilesFile){
281 flowFilesFile >> fName;
282 flowFileVector.push_back(fName);
283 m->gobble(flowFilesFile);
287 flowFileVector.push_back(flowFileName);
289 int numFiles = flowFileVector.size();
291 for(int i=1;i<ncpus;i++){
292 MPI_Send(&numFiles, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
295 for(int i=0;i<numFiles;i++){
296 flowFileName = flowFileVector[i];
298 cout << "\n>>>>>\tProcessing " << flowFileName << " (file " << i+1 << " of " << numFiles << ")\t<<<<<" << endl;
299 cout << "Reading flowgrams..." << endl;
301 cout << "Identifying unique flowgrams..." << endl;
304 cout << "Calculating distances between flowgrams..." << endl;
306 strcpy(fileName, flowFileName.c_str());
308 for(int i=1;i<ncpus;i++){
309 MPI_Send(&fileName[0], 1024, MPI_CHAR, i, tag, MPI_COMM_WORLD);
311 MPI_Send(&numSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
312 MPI_Send(&numUniques, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
313 MPI_Send(&numFlowCells, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
314 MPI_Send(&flowDataIntI[0], numSeqs * numFlowCells, MPI_SHORT, i, tag, MPI_COMM_WORLD);
315 MPI_Send(&flowDataPrI[0], numSeqs * numFlowCells, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
316 MPI_Send(&mapUniqueToSeq[0], numSeqs, MPI_INT, i, tag, MPI_COMM_WORLD);
317 MPI_Send(&mapSeqToUnique[0], numSeqs, MPI_INT, i, tag, MPI_COMM_WORLD);
318 MPI_Send(&lengths[0], numSeqs, MPI_INT, i, tag, MPI_COMM_WORLD);
319 MPI_Send(&jointLookUp[0], NUMBINS * NUMBINS, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
320 MPI_Send(&cutoff, 1, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
323 string distFileName = flowDistMPI(0, int(sqrt(1.0/float(ncpus)) * numUniques));
326 for(int i=1;i<ncpus;i++){
327 MPI_Recv(&done, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
329 m->appendFiles((distFileName + ".temp." + toString(i)), distFileName);
330 remove((distFileName + ".temp." + toString(i)).c_str());
333 string namesFileName = createNamesFile();
335 cout << "\nClustering flowgrams..." << endl;
336 string listFileName = cluster(distFileName, namesFileName);
338 getOTUData(listFileName);
341 for(int i=1;i<ncpus;i++){
342 MPI_Send(&numOTUs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
343 MPI_Send(&singleLookUp[0], singleLookUp.size(), MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
344 MPI_Send(&uniqueFlowgrams[0], numFlowCells * numUniques, MPI_SHORT, i, tag, MPI_COMM_WORLD);
345 MPI_Send(&sigma, 1, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
352 int numOTUsOnCPU = numOTUs / ncpus;
353 int numSeqsOnCPU = numSeqs / ncpus;
355 cout << "\nDenoising flowgrams..." << endl;
356 cout << "iter\tmaxDelta\tnLL\t\tcycletime" << endl;
358 while((maxIters == 0 && maxDelta > minDelta) || iter < MIN_ITER || (maxDelta > minDelta && iter < maxIters)){
360 double cycClock = clock();
361 unsigned long int cycTime = time(NULL);
364 int total = singleTau.size();
365 for(int i=1;i<ncpus;i++){
366 MPI_Send(&total, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
367 MPI_Send(&change[0], numOTUs, MPI_SHORT, i, tag, MPI_COMM_WORLD);
368 MPI_Send(¢roids[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
370 MPI_Send(&singleTau[0], total, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
371 MPI_Send(&seqNumber[0], total, MPI_INT, i, tag, MPI_COMM_WORLD);
372 MPI_Send(&seqIndex[0], total, MPI_INT, i, tag, MPI_COMM_WORLD);
373 MPI_Send(&nSeqsPerOTU[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
374 MPI_Send(&cumNumSeqs[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
377 calcCentroidsDriver(0, numOTUsOnCPU);
379 for(int i=1;i<ncpus;i++){
380 int otuStart = i * numOTUs / ncpus;
381 int otuStop = (i + 1) * numOTUs / ncpus;
383 vector<int> tempCentroids(numOTUs, 0);
384 vector<short> tempChange(numOTUs, 0);
386 MPI_Recv(&tempCentroids[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
387 MPI_Recv(&tempChange[0], numOTUs, MPI_SHORT, i, tag, MPI_COMM_WORLD, &status);
389 for(int j=otuStart;j<otuStop;j++){
390 centroids[j] = tempCentroids[j];
391 change[j] = tempChange[j];
395 maxDelta = getNewWeights();
396 double nLL = getLikelihood();
399 for(int i=1;i<ncpus;i++){
400 MPI_Send(¢roids[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
401 MPI_Send(&weight[0], numOTUs, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
402 MPI_Send(&change[0], numOTUs, MPI_SHORT, i, tag, MPI_COMM_WORLD);
405 calcNewDistancesParent(0, numSeqsOnCPU);
407 total = singleTau.size();
409 for(int i=1;i<ncpus;i++){
411 int seqStart = i * numSeqs / ncpus;
412 int seqStop = (i + 1) * numSeqs / ncpus;
414 MPI_Recv(&childTotal, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
416 vector<int> childSeqIndex(childTotal, 0);
417 vector<double> childSingleTau(childTotal, 0);
418 vector<double> childDist(numSeqs * numOTUs, 0);
419 vector<int> otuIndex(childTotal, 0);
421 MPI_Recv(&childSeqIndex[0], childTotal, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
422 MPI_Recv(&childSingleTau[0], childTotal, MPI_DOUBLE, i, tag, MPI_COMM_WORLD, &status);
423 MPI_Recv(&childDist[0], numOTUs * numSeqs, MPI_DOUBLE, i, tag, MPI_COMM_WORLD, &status);
424 MPI_Recv(&otuIndex[0], childTotal, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
426 int oldTotal = total;
428 singleTau.resize(total, 0);
429 seqIndex.resize(total, 0);
430 seqNumber.resize(total, 0);
434 for(int j=oldTotal;j<total;j++){
435 int otuI = otuIndex[childIndex];
436 int seqI = childSeqIndex[childIndex];
438 singleTau[j] = childSingleTau[childIndex];
440 aaP[otuI][nSeqsPerOTU[otuI]] = j;
441 aaI[otuI][nSeqsPerOTU[otuI]] = seqI;
446 int index = seqStart * numOTUs;
447 for(int j=seqStart;j<seqStop;j++){
448 for(int k=0;k<numOTUs;k++){
449 dist[index] = childDist[index];
457 cout << iter << '\t' << maxDelta << '\t' << setprecision(2) << nLL << '\t' << time(NULL) - cycTime << '\t' << setprecision(6) << (clock() - cycClock)/(double)CLOCKS_PER_SEC << endl;
459 if((maxIters == 0 && maxDelta > minDelta) || iter < MIN_ITER || (maxDelta > minDelta && iter < maxIters)){
461 for(int i=1;i<ncpus;i++){
462 MPI_Send(&live, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
467 for(int i=1;i<ncpus;i++){
468 MPI_Send(&live, 1, MPI_INT, i, tag, MPI_COMM_WORLD); //send kill command
474 cout << "\nFinalizing..." << endl;
477 vector<int> otuCounts(numOTUs, 0);
478 for(int i=0;i<numSeqs;i++) { otuCounts[otuData[i]]++; }
479 calcCentroidsDriver(0, numOTUs);
480 writeQualities(otuCounts);
481 writeSequences(otuCounts);
482 writeNames(otuCounts);
483 writeClusters(otuCounts);
486 remove(distFileName.c_str());
487 remove(namesFileName.c_str());
488 remove(listFileName.c_str());
490 cout << "Total time to process " << flowFileName << ":\t" << time(NULL) - begTime << '\t' << setprecision(6) << (clock() - begClock)/(double)CLOCKS_PER_SEC << endl;
498 MPI_Recv(&abort, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
499 if(abort){ return 0; }
502 MPI_Recv(&numFiles, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
504 for(int i=0;i<numFiles;i++){
505 //Now into the pyrodist part
507 MPI_Recv(&fileName, 1024, MPI_CHAR, 0, tag, MPI_COMM_WORLD, &status);
508 MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
509 MPI_Recv(&numUniques, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
510 MPI_Recv(&numFlowCells, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
512 flowDataIntI.resize(numSeqs * numFlowCells);
513 flowDataPrI.resize(numSeqs * numFlowCells);
514 mapUniqueToSeq.resize(numSeqs);
515 mapSeqToUnique.resize(numSeqs);
516 lengths.resize(numSeqs);
517 jointLookUp.resize(NUMBINS * NUMBINS);
519 MPI_Recv(&flowDataIntI[0], numSeqs * numFlowCells, MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
520 MPI_Recv(&flowDataPrI[0], numSeqs * numFlowCells, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
521 MPI_Recv(&mapUniqueToSeq[0], numSeqs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
522 MPI_Recv(&mapSeqToUnique[0], numSeqs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
523 MPI_Recv(&lengths[0], numSeqs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
524 MPI_Recv(&jointLookUp[0], NUMBINS * NUMBINS, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
525 MPI_Recv(&cutoff, 1, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
527 flowFileName = string(fileName);
528 int flowDistStart = int(sqrt(float(pid)/float(ncpus)) * numUniques);
529 int flowDistEnd = int(sqrt(float(pid+1)/float(ncpus)) * numUniques);
531 string distanceStringChild = flowDistMPI(flowDistStart, flowDistEnd);
534 MPI_Send(&done, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
536 //Now into the pyronoise part
537 MPI_Recv(&numOTUs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
539 singleLookUp.resize(HOMOPS * NUMBINS);
540 uniqueFlowgrams.resize(numUniques * numFlowCells);
541 weight.resize(numOTUs);
542 centroids.resize(numOTUs);
543 change.resize(numOTUs);
544 dist.assign(numOTUs * numSeqs, 0);
545 nSeqsPerOTU.resize(numOTUs);
546 cumNumSeqs.resize(numOTUs);
548 MPI_Recv(&singleLookUp[0], singleLookUp.size(), MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
549 MPI_Recv(&uniqueFlowgrams[0], uniqueFlowgrams.size(), MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
550 MPI_Recv(&sigma, 1, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
552 int startOTU = pid * numOTUs / ncpus;
553 int endOTU = (pid + 1) * numOTUs / ncpus;
555 int startSeq = pid * numSeqs / ncpus;
556 int endSeq = (pid + 1) * numSeqs /ncpus;
562 MPI_Recv(&total, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
563 singleTau.assign(total, 0.0000);
564 seqNumber.assign(total, 0);
565 seqIndex.assign(total, 0);
567 MPI_Recv(&change[0], numOTUs, MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
568 MPI_Recv(¢roids[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
569 MPI_Recv(&singleTau[0], total, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
570 MPI_Recv(&seqNumber[0], total, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
571 MPI_Recv(&seqIndex[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
572 MPI_Recv(&nSeqsPerOTU[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
573 MPI_Recv(&cumNumSeqs[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
575 calcCentroidsDriver(startOTU, endOTU);
577 MPI_Send(¢roids[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD);
578 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);
600 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 cout << i << "\t" << (time(NULL) - begTime) << "\t" << (clock()-begClock)/CLOCKS_PER_SEC << endl;
638 cout << stopSeq << "\t" << (time(NULL) - begTime) << "\t" << (clock()-begClock)/CLOCKS_PER_SEC << endl;
640 string fDistFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.dist";
641 if(pid != 0){ fDistFileName += ".temp." + toString(pid); }
643 ofstream distFile(fDistFileName.c_str());
644 distFile << outStream.str();
647 return fDistFileName;
649 catch(exception& e) {
650 m->errorOut(e, "ShhherCommand", "flowDistParentFork");
656 //**********************************************************************************************************************
658 int ShhherCommand::execute(){
660 if (abort == true) { return 0; }
662 cout.setf(ios::fixed, ios::floatfield);
663 cout.setf(ios::showpoint);
668 vector<string> flowFileVector;
669 if(flowFilesFileName != "not found"){
672 ifstream flowFilesFile;
673 m->openInputFile(flowFilesFileName, flowFilesFile);
674 while(flowFilesFile){
675 flowFilesFile >> fName;
676 flowFileVector.push_back(fName);
677 m->gobble(flowFilesFile);
681 flowFileVector.push_back(flowFileName);
683 int numFiles = flowFileVector.size();
686 for(int i=0;i<numFiles;i++){
687 flowFileName = flowFileVector[i];
689 cout << "\n>>>>>\tProcessing " << flowFileName << " (file " << i+1 << " of " << numFiles << ")\t<<<<<" << endl;
690 cout << "Reading flowgrams..." << endl;
692 cout << "Identifying unique flowgrams..." << endl;
696 cout << "Calculating distances between flowgrams..." << endl;
697 string distFileName = createDistFile(processors);
698 string namesFileName = createNamesFile();
700 cout << "\nClustering flowgrams..." << endl;
701 string listFileName = cluster(distFileName, namesFileName);
702 getOTUData(listFileName);
709 double begClock = clock();
710 unsigned long int begTime = time(NULL);
712 cout << "\nDenoising flowgrams..." << endl;
713 cout << "iter\tmaxDelta\tnLL\t\tcycletime" << endl;
715 while((maxIters == 0 && maxDelta > minDelta) || iter < MIN_ITER || (maxDelta > minDelta && iter < maxIters)){
717 double cycClock = clock();
718 unsigned long int cycTime = time(NULL);
723 maxDelta = getNewWeights();
724 double nLL = getLikelihood();
731 cout << iter << '\t' << maxDelta << '\t' << setprecision(2) << nLL << '\t' << time(NULL) - cycTime << '\t' << setprecision(6) << (clock() - cycClock)/(double)CLOCKS_PER_SEC << endl;
734 cout << "\nFinalizing..." << endl;
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 cout << "Total time to process " << flowFileName << ":\t" << time(NULL) - begTime << '\t' << setprecision(6) << (clock() - begClock)/(double)CLOCKS_PER_SEC << endl;
756 catch(exception& e) {
757 m->errorOut(e, "ShhherCommand", "execute");
762 /**************************************************************************************************/
764 void ShhherCommand::getFlowData(){
767 m->openInputFile(flowFileName, flowFile);
771 int currentNumFlowCells;
775 flowFile >> numFlowCells;
776 int index = 0;//pcluster
777 while(!flowFile.eof()){
778 flowFile >> seqName >> currentNumFlowCells;
779 lengths.push_back(currentNumFlowCells);
781 seqNameVector.push_back(seqName);
782 nameMap[seqName] = index++;//pcluster
784 for(int i=0;i<numFlowCells;i++){
785 flowFile >> intensity;
786 if(intensity > 9.99) { intensity = 9.99; }
787 int intI = int(100 * intensity + 0.0001);
788 flowDataIntI.push_back(intI);
794 numSeqs = seqNameVector.size();
796 for(int i=0;i<numSeqs;i++){
797 int iNumFlowCells = i * numFlowCells;
798 for(int j=lengths[i];j<numFlowCells;j++){
799 flowDataIntI[iNumFlowCells + j] = 0;
804 catch(exception& e) {
805 m->errorOut(e, "ShhherCommand", "getFlowData");
810 /**************************************************************************************************/
812 void ShhherCommand::getSingleLookUp(){
814 // these are the -log probabilities that a signal corresponds to a particular homopolymer length
815 singleLookUp.assign(HOMOPS * NUMBINS, 0);
819 m->openInputFile(lookupFileName, lookUpFile);
821 for(int i=0;i<HOMOPS;i++){
823 lookUpFile >> logFracFreq;
825 for(int j=0;j<NUMBINS;j++) {
826 lookUpFile >> singleLookUp[index];
832 catch(exception& e) {
833 m->errorOut(e, "ShhherCommand", "getSingleLookUp");
838 /**************************************************************************************************/
840 void ShhherCommand::getJointLookUp(){
843 // the most likely joint probability (-log) that two intenities have the same polymer length
844 jointLookUp.resize(NUMBINS * NUMBINS, 0);
846 for(int i=0;i<NUMBINS;i++){
847 for(int j=0;j<NUMBINS;j++){
849 double minSum = 100000000;
851 for(int k=0;k<HOMOPS;k++){
852 double sum = singleLookUp[k * NUMBINS + i] + singleLookUp[k * NUMBINS + j];
854 if(sum < minSum) { minSum = sum; }
856 jointLookUp[i * NUMBINS + j] = minSum;
860 catch(exception& e) {
861 m->errorOut(e, "ShhherCommand", "getJointLookUp");
866 /**************************************************************************************************/
868 double ShhherCommand::getProbIntensity(int intIntensity){
871 double minNegLogProb = 100000000;
874 for(int i=0;i<HOMOPS;i++){//loop signal strength
875 float negLogProb = singleLookUp[i * NUMBINS + intIntensity];
876 if(negLogProb < minNegLogProb) { minNegLogProb = negLogProb; }
879 return minNegLogProb;
881 catch(exception& e) {
882 m->errorOut(e, "ShhherCommand", "getProbIntensity");
887 /**************************************************************************************************/
889 void ShhherCommand::getUniques(){
894 uniqueFlowgrams.assign(numFlowCells * numSeqs, -1);
895 uniqueCount.assign(numSeqs, 0); // anWeights
896 uniqueLengths.assign(numSeqs, 0);
897 mapSeqToUnique.assign(numSeqs, -1);
898 mapUniqueToSeq.assign(numSeqs, -1);
900 vector<short> uniqueFlowDataIntI(numFlowCells * numSeqs, -1);
902 for(int i=0;i<numSeqs;i++){
905 vector<short> current(numFlowCells);
906 for(int j=0;j<numFlowCells;j++){ current[j] = short(((flowDataIntI[i * numFlowCells + j] + 50.0)/100.0)); }
908 for(int j=0;j<numUniques;j++){
909 int offset = j * numFlowCells;
912 for(int k=0;k<numFlowCells;k++){
913 if(current[k] != uniqueFlowgrams[offset + k]){
920 mapSeqToUnique[i] = j;
928 if(index == numUniques){
929 uniqueLengths[numUniques] = lengths[i];
930 uniqueCount[numUniques] = 1;
931 mapSeqToUnique[i] = numUniques;//anMap
932 mapUniqueToSeq[numUniques] = i;//anF
934 for(int k=0;k<numFlowCells;k++){
935 uniqueFlowgrams[numUniques * numFlowCells + k] = current[k];
936 uniqueFlowDataIntI[numUniques * numFlowCells + k] = flowDataIntI[i * numFlowCells + k];
942 uniqueFlowDataIntI.resize(numFlowCells * numUniques);
943 uniqueLengths.resize(numUniques);
945 flowDataPrI.assign(numSeqs * numFlowCells, 0);
946 for(int i=0;i<flowDataPrI.size();i++) { flowDataPrI[i] = getProbIntensity(flowDataIntI[i]); }
948 catch(exception& e) {
949 m->errorOut(e, "ShhherCommand", "getUniques");
954 /**************************************************************************************************/
956 float ShhherCommand::calcPairwiseDist(int seqA, int seqB){
958 int minLength = lengths[mapSeqToUnique[seqA]];
959 if(lengths[seqB] < minLength){ minLength = lengths[mapSeqToUnique[seqB]]; }
961 int ANumFlowCells = seqA * numFlowCells;
962 int BNumFlowCells = seqB * numFlowCells;
966 for(int i=0;i<minLength;i++){
967 int flowAIntI = flowDataIntI[ANumFlowCells + i];
968 float flowAPrI = flowDataPrI[ANumFlowCells + i];
970 int flowBIntI = flowDataIntI[BNumFlowCells + i];
971 float flowBPrI = flowDataPrI[BNumFlowCells + i];
972 dist += jointLookUp[flowAIntI * NUMBINS + flowBIntI] - flowAPrI - flowBPrI;
975 dist /= (float) minLength;
978 catch(exception& e) {
979 m->errorOut(e, "ShhherCommand", "calcPairwiseDist");
984 /**************************************************************************************************/
986 void ShhherCommand::flowDistParentFork(string distFileName, int startSeq, int stopSeq){
989 ostringstream outStream;
990 outStream.setf(ios::fixed, ios::floatfield);
991 outStream.setf(ios::dec, ios::basefield);
992 outStream.setf(ios::showpoint);
993 outStream.precision(6);
995 int begTime = time(NULL);
996 double begClock = clock();
998 for(int i=startSeq;i<stopSeq;i++){
999 for(int j=0;j<i;j++){
1000 float flowDistance = calcPairwiseDist(mapUniqueToSeq[i], mapUniqueToSeq[j]);
1002 if(flowDistance < 1e-6){
1003 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << 0.000000 << endl;
1005 else if(flowDistance <= cutoff){
1006 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << flowDistance << endl;
1010 m->mothurOut(toString(i) + "\t" + toString(time(NULL) - begTime));
1011 m->mothurOut("\t" + toString((clock()-begClock)/CLOCKS_PER_SEC));
1012 m->mothurOutEndLine();
1015 m->mothurOut(toString(stopSeq-1) + "\t" + toString(time(NULL) - begTime));
1016 m->mothurOut("\t" + toString((clock()-begClock)/CLOCKS_PER_SEC));
1017 m->mothurOutEndLine();
1019 ofstream distFile(distFileName.c_str());
1020 distFile << outStream.str();
1023 catch(exception& e) {
1024 m->errorOut(e, "ShhherCommand", "flowDistParentFork");
1029 /**************************************************************************************************/
1031 string ShhherCommand::createDistFile(int processors){
1033 string fDistFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.dist";
1035 unsigned long int begTime = time(NULL);
1036 double begClock = clock();
1041 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
1042 if(processors == 1) { flowDistParentFork(fDistFileName, 0, numUniques); }
1043 else{ //you have multiple processors
1045 if (numSeqs < processors){ processors = 1; }
1047 vector<int> start(processors, 0);
1048 vector<int> end(processors, 0);
1050 for (int i = 0; i < processors; i++) {
1051 start[i] = int(sqrt(float(i)/float(processors)) * numUniques);
1052 end[i] = int(sqrt(float(i+1)/float(processors)) * numUniques);
1056 vector<int> processIDs;
1058 //loop through and create all the processes you want
1059 while (process != processors) {
1063 processIDs.push_back(pid); //create map from line number to pid so you can append files in correct order later
1065 }else if (pid == 0){
1066 flowDistParentFork(fDistFileName + toString(getpid()) + ".temp", start[process], end[process]);
1069 m->mothurOut("[ERROR]: unable to spawn the necessary processes. Error code: " + toString(pid)); m->mothurOutEndLine();
1071 for (int i=0;i<processIDs.size();i++) { int temp = processIDs[i]; kill (temp, SIGINT); }
1076 //parent does its part
1077 flowDistParentFork(fDistFileName, start[0], end[0]);
1079 //force parent to wait until all the processes are done
1080 for (int i=0;i<processIDs.size();i++) {
1081 int temp = processIDs[i];
1085 //append and remove temp files
1086 for (int i=0;i<processIDs.size();i++) {
1087 m->appendFiles((fDistFileName + toString(processIDs[i]) + ".temp"), fDistFileName);
1088 remove((fDistFileName + toString(processIDs[i]) + ".temp").c_str());
1094 flowDistParentFork(fDistFileName, 0, numUniques);
1097 m->mothurOutEndLine();
1099 cout << "Total time: " << (time(NULL) - begTime) << "\t" << (clock() - begClock)/CLOCKS_PER_SEC << endl;;
1101 return fDistFileName;
1103 catch(exception& e) {
1104 m->errorOut(e, "ShhherCommand", "createDistFile");
1110 /**************************************************************************************************/
1112 string ShhherCommand::createNamesFile(){
1115 vector<string> duplicateNames(numUniques, "");
1116 for(int i=0;i<numSeqs;i++){
1117 duplicateNames[mapSeqToUnique[i]] += seqNameVector[i] + ',';
1120 string nameFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.names";
1123 m->openOutputFile(nameFileName, nameFile);
1125 for(int i=0;i<numUniques;i++){
1126 // nameFile << seqNameVector[mapUniqueToSeq[i]] << '\t' << duplicateNames[i].substr(0, duplicateNames[i].find_last_of(',')) << endl;
1127 nameFile << mapUniqueToSeq[i] << '\t' << duplicateNames[i].substr(0, duplicateNames[i].find_last_of(',')) << endl;
1131 return nameFileName;
1133 catch(exception& e) {
1134 m->errorOut(e, "ShhherCommand", "createNamesFile");
1139 //**********************************************************************************************************************
1141 string ShhherCommand::cluster(string distFileName, string namesFileName){
1144 SparseMatrix* matrix;
1146 RAbundVector* rabund;
1148 globaldata->setNameFile(namesFileName);
1149 globaldata->setColumnFile(distFileName);
1150 globaldata->setFormat("column");
1152 ReadMatrix* read = new ReadColumnMatrix(distFileName);
1153 read->setCutoff(cutoff);
1155 NameAssignment* clusterNameMap = new NameAssignment(namesFileName);
1156 clusterNameMap->readMap();
1157 read->read(clusterNameMap);
1159 list = read->getListVector();
1160 matrix = read->getMatrix();
1163 delete clusterNameMap;
1165 rabund = new RAbundVector(list->getRAbundVector());
1167 Cluster* cluster = new CompleteLinkage(rabund, list, matrix, cutoff, "furthest");
1168 string tag = cluster->getTag();
1170 double clusterCutoff = cutoff;
1171 while (matrix->getSmallDist() <= clusterCutoff && matrix->getNNodes() > 0){
1172 cluster->update(clusterCutoff);
1175 list->setLabel(toString(cutoff));
1177 string listFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.list";
1179 m->openOutputFile(listFileName, listFile);
1180 list->print(listFile);
1183 delete matrix; delete cluster; delete rabund; delete list;
1185 return listFileName;
1187 catch(exception& e) {
1188 m->errorOut(e, "ShhherCommand", "cluster");
1193 /**************************************************************************************************/
1195 void ShhherCommand::getOTUData(string listFileName){
1199 m->openInputFile(listFileName, listFile);
1202 listFile >> label >> numOTUs;
1204 otuData.assign(numSeqs, 0);
1205 cumNumSeqs.assign(numOTUs, 0);
1206 nSeqsPerOTU.assign(numOTUs, 0);
1207 aaP.resize(numOTUs);
1209 string singleOTU = "";
1211 for(int i=0;i<numOTUs;i++){
1213 listFile >> singleOTU;
1215 istringstream otuString(singleOTU);
1219 string seqName = "";
1221 for(int j=0;j<singleOTU.length();j++){
1222 char letter = otuString.get();
1228 map<string,int>::iterator nmIt = nameMap.find(seqName);
1229 int index = nmIt->second;
1231 nameMap.erase(nmIt);
1235 aaP[i].push_back(index);
1240 map<string,int>::iterator nmIt = nameMap.find(seqName);
1242 int index = nmIt->second;
1243 nameMap.erase(nmIt);
1247 aaP[i].push_back(index);
1252 sort(aaP[i].begin(), aaP[i].end());
1253 for(int j=0;j<nSeqsPerOTU[i];j++){
1254 seqNumber.push_back(aaP[i][j]);
1256 for(int j=nSeqsPerOTU[i];j<numSeqs;j++){
1257 aaP[i].push_back(0);
1261 for(int i=1;i<numOTUs;i++){
1262 cumNumSeqs[i] = cumNumSeqs[i-1] + nSeqsPerOTU[i-1];
1265 seqIndex = seqNumber;
1269 catch(exception& e) {
1270 m->errorOut(e, "ShhherCommand", "getOTUData");
1275 /**************************************************************************************************/
1277 void ShhherCommand::initPyroCluster(){
1279 dist.assign(numSeqs * numOTUs, 0);
1280 change.assign(numOTUs, 1);
1281 centroids.assign(numOTUs, -1);
1282 weight.assign(numOTUs, 0);
1283 singleTau.assign(numSeqs, 1.0);
1285 nSeqsBreaks.assign(processors+1, 0);
1286 nOTUsBreaks.assign(processors+1, 0);
1289 for(int i=0;i<processors;i++){
1290 nSeqsBreaks[i+1] = nSeqsBreaks[i] + (int)((double) numSeqs / (double) processors);
1291 nOTUsBreaks[i+1] = nOTUsBreaks[i] + (int)((double) numOTUs / (double) processors);
1293 nSeqsBreaks[processors] = numSeqs;
1294 nOTUsBreaks[processors] = numOTUs;
1296 catch(exception& e) {
1297 m->errorOut(e, "ShhherCommand", "initPyroCluster");
1302 /**************************************************************************************************/
1304 void ShhherCommand::fill(){
1307 for(int i=0;i<numOTUs;i++){
1308 cumNumSeqs[i] = index;
1309 for(int j=0;j<nSeqsPerOTU[i];j++){
1310 seqNumber[index] = aaP[i][j];
1311 seqIndex[index] = aaI[i][j];
1317 catch(exception& e) {
1318 m->errorOut(e, "ShhherCommand", "fill");
1323 /**************************************************************************************************/
1325 void ShhherCommand::calcCentroids(){
1328 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
1330 if(processors == 1) {
1331 calcCentroidsDriver(0, numOTUs);
1333 else{ //you have multiple processors
1334 if (numOTUs < processors){ processors = 1; }
1337 vector<int> processIDs;
1339 //loop through and create all the processes you want
1340 while (process != processors) {
1344 processIDs.push_back(pid); //create map from line number to pid so you can append files in correct order later
1346 }else if (pid == 0){
1347 calcCentroidsDriver(nOTUsBreaks[process], nOTUsBreaks[process+1]);
1350 m->mothurOut("[ERROR]: unable to spawn the necessary processes. Error code: " + toString(pid)); m->mothurOutEndLine();
1352 for (int i=0;i<processIDs.size();i++) { int temp = processIDs[i]; kill (temp, SIGINT); }
1357 //parent does its part
1358 calcCentroidsDriver(nOTUsBreaks[0], nOTUsBreaks[1]);
1360 //force parent to wait until all the processes are done
1361 for (int i=0;i<processIDs.size();i++) {
1362 int temp = processIDs[i];
1368 calcCentroidsDriver(0, numOTUs);
1371 catch(exception& e) {
1372 m->errorOut(e, "ShhherCommand", "calcCentroidsDriver");
1377 /**************************************************************************************************/
1379 void ShhherCommand::calcCentroidsDriver(int start, int finish){
1381 //this function gets the most likely homopolymer length at a flow position for a group of sequences
1386 for(int i=start;i<finish;i++){
1390 int minFlowGram = 100000000;
1391 double minFlowValue = 1e8;
1392 change[i] = 0; //FALSE
1394 for(int j=0;j<nSeqsPerOTU[i];j++){
1395 count += singleTau[seqNumber[cumNumSeqs[i] + j]];
1398 if(nSeqsPerOTU[i] > 0 && count > MIN_COUNT){
1399 vector<double> adF(nSeqsPerOTU[i]);
1400 vector<int> anL(nSeqsPerOTU[i]);
1402 for(int j=0;j<nSeqsPerOTU[i];j++){
1403 int index = cumNumSeqs[i] + j;
1404 int nI = seqIndex[index];
1405 int nIU = mapSeqToUnique[nI];
1408 for(k=0;k<position;k++){
1414 anL[position] = nIU;
1415 adF[position] = 0.0000;
1420 for(int j=0;j<nSeqsPerOTU[i];j++){
1421 int index = cumNumSeqs[i] + j;
1422 int nI = seqIndex[index];
1424 double tauValue = singleTau[seqNumber[index]];
1426 for(int k=0;k<position;k++){
1427 double dist = getDistToCentroid(anL[k], nI, lengths[nI]);
1428 adF[k] += dist * tauValue;
1432 for(int j=0;j<position;j++){
1433 if(adF[j] < minFlowValue){
1435 minFlowValue = adF[j];
1439 if(centroids[i] != anL[minFlowGram]){
1441 centroids[i] = anL[minFlowGram];
1444 else if(centroids[i] != -1){
1450 catch(exception& e) {
1451 m->errorOut(e, "ShhherCommand", "calcCentroidsDriver");
1456 /**************************************************************************************************/
1458 double ShhherCommand::getDistToCentroid(int cent, int flow, int length){
1461 int flowAValue = cent * numFlowCells;
1462 int flowBValue = flow * numFlowCells;
1466 for(int i=0;i<length;i++){
1467 dist += singleLookUp[uniqueFlowgrams[flowAValue] * NUMBINS + flowDataIntI[flowBValue]];
1472 return dist / (double)length;
1474 catch(exception& e) {
1475 m->errorOut(e, "ShhherCommand", "getDistToCentroid");
1480 /**************************************************************************************************/
1482 double ShhherCommand::getNewWeights(){
1485 double maxChange = 0;
1487 for(int i=0;i<numOTUs;i++){
1489 double difference = weight[i];
1492 for(int j=0;j<nSeqsPerOTU[i];j++){
1493 int index = cumNumSeqs[i] + j;
1494 double tauValue = singleTau[seqNumber[index]];
1495 weight[i] += tauValue;
1498 difference = fabs(weight[i] - difference);
1499 if(difference > maxChange){ maxChange = difference; }
1503 catch(exception& e) {
1504 m->errorOut(e, "ShhherCommand", "getNewWeights");
1509 /**************************************************************************************************/
1511 double ShhherCommand::getLikelihood(){
1515 vector<long double> P(numSeqs, 0);
1518 for(int i=0;i<numOTUs;i++){
1519 if(weight[i] > MIN_WEIGHT){
1525 for(int i=0;i<numOTUs;i++){
1526 for(int j=0;j<nSeqsPerOTU[i];j++){
1527 int index = cumNumSeqs[i] + j;
1528 int nI = seqIndex[index];
1529 double singleDist = dist[seqNumber[index]];
1531 P[nI] += weight[i] * exp(-singleDist * sigma);
1535 for(int i=0;i<numSeqs;i++){
1536 if(P[i] == 0){ P[i] = DBL_EPSILON; }
1541 nLL = nLL -(double)numSeqs * log(sigma);
1545 catch(exception& e) {
1546 m->errorOut(e, "ShhherCommand", "getNewWeights");
1551 /**************************************************************************************************/
1553 void ShhherCommand::checkCentroids(){
1555 vector<int> unique(numOTUs, 1);
1557 for(int i=0;i<numOTUs;i++){
1558 if(centroids[i] == -1 || weight[i] < MIN_WEIGHT){
1563 for(int i=0;i<numOTUs;i++){
1565 for(int j=i+1;j<numOTUs;j++){
1568 if(centroids[j] == centroids[i]){
1572 weight[i] += weight[j];
1580 catch(exception& e) {
1581 m->errorOut(e, "ShhherCommand", "checkCentroids");
1586 /**************************************************************************************************/
1588 void ShhherCommand::calcNewDistances(){
1591 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
1593 if(processors == 1) {
1594 calcNewDistancesParent(0, numSeqs);
1596 else{ //you have multiple processors
1597 if (numSeqs < processors){ processors = 1; }
1599 vector<vector<int> > child_otuIndex(processors);
1600 vector<vector<int> > child_seqIndex(processors);
1601 vector<vector<double> > child_singleTau(processors);
1602 vector<int> totals(processors);
1605 vector<int> processIDs;
1607 //loop through and create all the processes you want
1608 while (process != processors) {
1612 processIDs.push_back(pid); //create map from line number to pid so you can append files in correct order later
1614 }else if (pid == 0){
1615 calcNewDistancesChild(nSeqsBreaks[process], nSeqsBreaks[process+1], child_otuIndex[process], child_seqIndex[process], child_singleTau[process]);
1616 totals[process] = child_otuIndex[process].size();
1620 m->mothurOut("[ERROR]: unable to spawn the necessary processes. Error code: " + toString(pid)); m->mothurOutEndLine();
1622 for (int i=0;i<processIDs.size();i++) { int temp = processIDs[i]; kill (temp, SIGINT); }
1627 //parent does its part
1628 calcNewDistancesParent(nSeqsBreaks[0], nSeqsBreaks[1]);
1629 int total = seqIndex.size();
1631 //force parent to wait until all the processes are done
1632 for (int i=0;i<processIDs.size();i++) {
1633 int temp = processIDs[i];
1637 for(int i=1;i<processors;i++){
1638 int oldTotal = total;
1641 singleTau.resize(total, 0);
1642 seqIndex.resize(total, 0);
1643 seqNumber.resize(total, 0);
1647 for(int j=oldTotal;j<total;j++){
1648 int otuI = child_otuIndex[i][childIndex];
1649 int seqI = child_seqIndex[i][childIndex];
1651 singleTau[j] = child_singleTau[i][childIndex];
1652 aaP[otuI][nSeqsPerOTU[otuI]] = j;
1653 aaI[otuI][nSeqsPerOTU[otuI]] = seqI;
1654 nSeqsPerOTU[otuI]++;
1661 calcNewDistancesParent(0, numSeqs);
1664 catch(exception& e) {
1665 m->errorOut(e, "ShhherCommand", "calcNewDistances");
1670 /**************************************************************************************************/
1672 void ShhherCommand::calcNewDistancesChildMPI(int startSeq, int stopSeq, vector<int>& otuIndex){
1675 vector<double> newTau(numOTUs,0);
1676 vector<double> norms(numSeqs, 0);
1679 singleTau.resize(0);
1683 for(int i=startSeq;i<stopSeq;i++){
1684 double offset = 1e8;
1685 int indexOffset = i * numOTUs;
1687 for(int j=0;j<numOTUs;j++){
1689 if(weight[j] > MIN_WEIGHT && change[j] == 1){
1690 dist[indexOffset + j] = getDistToCentroid(centroids[j], i, lengths[i]);
1692 if(weight[j] > MIN_WEIGHT && dist[indexOffset + j] < offset){
1693 offset = dist[indexOffset + j];
1697 for(int j=0;j<numOTUs;j++){
1698 if(weight[j] > MIN_WEIGHT){
1699 newTau[j] = exp(sigma * (-dist[indexOffset + j] + offset)) * weight[j];
1700 norms[i] += newTau[j];
1707 for(int j=0;j<numOTUs;j++){
1709 newTau[j] /= norms[i];
1711 if(newTau[j] > MIN_TAU){
1712 otuIndex.push_back(j);
1713 seqIndex.push_back(i);
1714 singleTau.push_back(newTau[j]);
1720 catch(exception& e) {
1721 m->errorOut(e, "ShhherCommand", "calcNewDistancesChildMPI");
1726 /**************************************************************************************************/
1728 void ShhherCommand::calcNewDistancesChild(int startSeq, int stopSeq, vector<int>& child_otuIndex, vector<int>& child_seqIndex, vector<double>& child_singleTau){
1731 vector<double> newTau(numOTUs,0);
1732 vector<double> norms(numSeqs, 0);
1733 child_otuIndex.resize(0);
1734 child_seqIndex.resize(0);
1735 child_singleTau.resize(0);
1737 for(int i=startSeq;i<stopSeq;i++){
1738 double offset = 1e8;
1739 int indexOffset = i * numOTUs;
1742 for(int j=0;j<numOTUs;j++){
1743 if(weight[j] > MIN_WEIGHT && change[j] == 1){
1744 dist[indexOffset + j] = getDistToCentroid(centroids[j], i, lengths[i]);
1747 if(weight[j] > MIN_WEIGHT && dist[indexOffset + j] < offset){
1748 offset = dist[indexOffset + j];
1752 for(int j=0;j<numOTUs;j++){
1753 if(weight[j] > MIN_WEIGHT){
1754 newTau[j] = exp(sigma * (-dist[indexOffset + j] + offset)) * weight[j];
1755 norms[i] += newTau[j];
1762 for(int j=0;j<numOTUs;j++){
1763 newTau[j] /= norms[i];
1765 if(newTau[j] > MIN_TAU){
1766 child_otuIndex.push_back(j);
1767 child_seqIndex.push_back(i);
1768 child_singleTau.push_back(newTau[j]);
1773 catch(exception& e) {
1774 m->errorOut(e, "ShhherCommand", "calcNewDistancesChild");
1779 /**************************************************************************************************/
1781 void ShhherCommand::calcNewDistancesParent(int startSeq, int stopSeq){
1786 vector<double> newTau(numOTUs,0);
1787 vector<double> norms(numSeqs, 0);
1788 nSeqsPerOTU.assign(numOTUs, 0);
1790 for(int i=startSeq;i<stopSeq;i++){
1791 int indexOffset = i * numOTUs;
1793 double offset = 1e8;
1795 for(int j=0;j<numOTUs;j++){
1796 if(weight[j] > MIN_WEIGHT && change[j] == 1){
1797 dist[indexOffset + j] = getDistToCentroid(centroids[j], i, lengths[i]);
1800 if(weight[j] > MIN_WEIGHT && dist[indexOffset + j] < offset){
1801 offset = dist[indexOffset + j];
1805 for(int j=0;j<numOTUs;j++){
1806 if(weight[j] > MIN_WEIGHT){
1807 newTau[j] = exp(sigma * (-dist[indexOffset + j] + offset)) * weight[j];
1808 norms[i] += newTau[j];
1815 for(int j=0;j<numOTUs;j++){
1816 newTau[j] /= norms[i];
1819 for(int j=0;j<numOTUs;j++){
1820 if(newTau[j] > MIN_TAU){
1822 int oldTotal = total;
1826 singleTau.resize(total, 0);
1827 seqNumber.resize(total, 0);
1828 seqIndex.resize(total, 0);
1830 singleTau[oldTotal] = newTau[j];
1832 aaP[j][nSeqsPerOTU[j]] = oldTotal;
1833 aaI[j][nSeqsPerOTU[j]] = i;
1839 catch(exception& e) {
1840 m->errorOut(e, "ShhherCommand", "calcNewDistancesParent");
1845 /**************************************************************************************************/
1847 void ShhherCommand::setOTUs(){
1850 vector<double> bigTauMatrix(numOTUs * numSeqs, 0.0000);
1852 for(int i=0;i<numOTUs;i++){
1853 for(int j=0;j<nSeqsPerOTU[i];j++){
1854 int index = cumNumSeqs[i] + j;
1855 double tauValue = singleTau[seqNumber[index]];
1856 int sIndex = seqIndex[index];
1857 bigTauMatrix[sIndex * numOTUs + i] = tauValue;
1861 for(int i=0;i<numSeqs;i++){
1862 double maxTau = -1.0000;
1864 for(int j=0;j<numOTUs;j++){
1865 if(bigTauMatrix[i * numOTUs + j] > maxTau){
1866 maxTau = bigTauMatrix[i * numOTUs + j];
1871 otuData[i] = maxOTU;
1874 nSeqsPerOTU.assign(numOTUs, 0);
1876 for(int i=0;i<numSeqs;i++){
1877 int index = otuData[i];
1879 singleTau[i] = 1.0000;
1882 aaP[index][nSeqsPerOTU[index]] = i;
1883 aaI[index][nSeqsPerOTU[index]] = i;
1885 nSeqsPerOTU[index]++;
1889 catch(exception& e) {
1890 m->errorOut(e, "ShhherCommand", "calcNewDistances");
1895 /**************************************************************************************************/
1897 void ShhherCommand::writeQualities(vector<int> otuCounts){
1900 string qualityFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.qual";
1902 ofstream qualityFile;
1903 m->openOutputFile(qualityFileName, qualityFile);
1905 qualityFile.setf(ios::fixed, ios::floatfield);
1906 qualityFile.setf(ios::showpoint);
1907 qualityFile << setprecision(6);
1909 vector<vector<int> > qualities(numOTUs);
1910 vector<double> pr(HOMOPS, 0);
1913 for(int i=0;i<numOTUs;i++){
1917 if(nSeqsPerOTU[i] > 0){
1918 qualities[i].assign(1024, -1);
1920 while(index < numFlowCells){
1921 double maxPrValue = 1e8;
1922 short maxPrIndex = -1;
1923 double count = 0.0000;
1925 pr.assign(HOMOPS, 0);
1927 for(int j=0;j<nSeqsPerOTU[i];j++){
1928 int lIndex = cumNumSeqs[i] + j;
1929 double tauValue = singleTau[seqNumber[lIndex]];
1930 int sequenceIndex = aaI[i][j];
1931 short intensity = flowDataIntI[sequenceIndex * numFlowCells + index];
1935 for(int s=0;s<HOMOPS;s++){
1936 pr[s] += tauValue * singleLookUp[s * NUMBINS + intensity];
1940 maxPrIndex = uniqueFlowgrams[centroids[i] * numFlowCells + index];
1941 maxPrValue = pr[maxPrIndex];
1943 if(count > MIN_COUNT){
1945 double norm = 0.0000;
1947 for(int s=0;s<HOMOPS;s++){
1948 norm += exp(-(pr[s] - maxPrValue));
1951 for(int s=1;s<=maxPrIndex;s++){
1953 double temp = 0.0000;
1955 U += exp(-(pr[s-1]-maxPrValue))/norm;
1963 temp = floor(-10 * temp);
1964 value = (int)floor(temp);
1965 if(value > 100){ value = 100; }
1967 qualities[i][base] = (int)value;
1977 if(otuCounts[i] > 0){
1978 qualityFile << '>' << seqNameVector[mapUniqueToSeq[i]] << endl;
1980 int j=4; //need to get past the first four bases
1981 while(qualities[i][j] != -1){
1982 qualityFile << qualities[i][j] << ' ';
1985 qualityFile << endl;
1988 qualityFile.close();
1991 catch(exception& e) {
1992 m->errorOut(e, "ShhherCommand", "writeQualities");
1997 /**************************************************************************************************/
1999 void ShhherCommand::writeSequences(vector<int> otuCounts){
2001 string bases = "TACG";
2003 string fastaFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.fasta";
2005 m->openOutputFile(fastaFileName, fastaFile);
2007 vector<string> names(numOTUs, "");
2009 for(int i=0;i<numOTUs;i++){
2010 int index = centroids[i];
2012 if(otuCounts[i] > 0){
2013 fastaFile << '>' << seqNameVector[aaI[i][0]] << endl;
2015 for(int j=8;j<numFlowCells;j++){
2017 char base = bases[j % 4];
2018 for(int k=0;k<uniqueFlowgrams[index * numFlowCells + j];k++){
2027 if(compositeFASTAFileName != ""){
2028 m->appendFiles(fastaFileName, compositeFASTAFileName);
2031 catch(exception& e) {
2032 m->errorOut(e, "ShhherCommand", "writeSequences");
2037 /**************************************************************************************************/
2039 void ShhherCommand::writeNames(vector<int> otuCounts){
2041 string nameFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.final.names";
2043 m->openOutputFile(nameFileName, nameFile);
2045 for(int i=0;i<numOTUs;i++){
2046 if(otuCounts[i] > 0){
2047 nameFile << seqNameVector[aaI[i][0]] << '\t' << seqNameVector[aaI[i][0]];
2049 for(int j=1;j<nSeqsPerOTU[i];j++){
2050 nameFile << ',' << seqNameVector[aaI[i][j]];
2058 catch(exception& e) {
2059 m->errorOut(e, "ShhherCommand", "writeNames");
2064 /**************************************************************************************************/
2066 void ShhherCommand::writeGroups(){
2068 string fileRoot = flowFileName.substr(0,flowFileName.find_last_of('.'));
2069 string groupFileName = fileRoot + ".pn.groups";
2071 m->openOutputFile(groupFileName, groupFile);
2073 for(int i=0;i<numSeqs;i++){
2074 groupFile << seqNameVector[i] << '\t' << fileRoot << endl;
2078 catch(exception& e) {
2079 m->errorOut(e, "ShhherCommand", "writeGroups");
2084 /**************************************************************************************************/
2086 void ShhherCommand::writeClusters(vector<int> otuCounts){
2088 string otuCountsFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".pn.counts";
2089 ofstream otuCountsFile;
2090 m->openOutputFile(otuCountsFileName, otuCountsFile);
2092 string bases = "TACG";
2094 for(int i=0;i<numOTUs;i++){
2095 //output the translated version of the centroid sequence for the otu
2096 if(otuCounts[i] > 0){
2097 int index = centroids[i];
2099 otuCountsFile << "ideal\t";
2100 for(int j=8;j<numFlowCells;j++){
2101 char base = bases[j % 4];
2102 for(int s=0;s<uniqueFlowgrams[index * numFlowCells + j];s++){
2103 otuCountsFile << base;
2106 otuCountsFile << endl;
2108 for(int j=0;j<nSeqsPerOTU[i];j++){
2109 int sequence = aaI[i][j];
2110 otuCountsFile << seqNameVector[sequence] << '\t';
2112 for(int k=8;k<lengths[sequence];k++){
2113 char base = bases[k % 4];
2114 int freq = int(0.01 * (double)flowDataIntI[sequence * numFlowCells + k] + 0.5);
2116 for(int s=0;s<freq;s++){
2117 otuCountsFile << base;
2120 otuCountsFile << endl;
2122 otuCountsFile << endl;
2125 otuCountsFile.close();
2127 catch(exception& e) {
2128 m->errorOut(e, "ShhherCommand", "writeClusters");
2133 //**********************************************************************************************************************