5 * Created by Pat Schloss on 12/27/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "shhhercommand.h"
12 #include "readcolumn.h"
13 #include "readmatrix.hpp"
14 #include "rabundvector.hpp"
15 #include "sabundvector.hpp"
16 #include "listvector.hpp"
17 #include "cluster.hpp"
18 #include "sparsematrix.hpp"
21 //**********************************************************************************************************************
26 #define MIN_WEIGHT 0.1
27 #define MIN_TAU 0.0001
29 //**********************************************************************************************************************
30 vector<string> ShhherCommand::setParameters(){
32 CommandParameter pflow("flow", "InputTypes", "", "", "none", "fileflow", "none",false,false); parameters.push_back(pflow);
33 CommandParameter pfile("file", "InputTypes", "", "", "none", "fileflow", "none",false,false); parameters.push_back(pfile);
34 CommandParameter plookup("lookup", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(plookup);
35 CommandParameter pcutoff("cutoff", "Number", "", "0.01", "", "", "",false,false); parameters.push_back(pcutoff);
36 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
37 CommandParameter pmaxiter("maxiter", "Number", "", "1000", "", "", "",false,false); parameters.push_back(pmaxiter);
38 CommandParameter psigma("sigma", "Number", "", "60", "", "", "",false,false); parameters.push_back(psigma);
39 CommandParameter pmindelta("mindelta", "Number", "", "0.000001", "", "", "",false,false); parameters.push_back(pmindelta);
40 CommandParameter porder("order", "String", "", "", "", "", "",false,false); parameters.push_back(porder);
41 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
42 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
44 vector<string> myArray;
45 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
49 m->errorOut(e, "ShhherCommand", "setParameters");
53 //**********************************************************************************************************************
54 string ShhherCommand::getHelpString(){
56 string helpString = "";
57 helpString += "The shhh.seqs command reads a file containing flowgrams and creates a file of corrected sequences.\n";
61 m->errorOut(e, "ShhherCommand", "getHelpString");
65 //**********************************************************************************************************************
67 ShhherCommand::ShhherCommand(){
69 abort = true; calledHelp = true;
72 //initialize outputTypes
73 // vector<string> tempOutNames;
74 // outputTypes["pn.dist"] = tempOutNames;
78 m->errorOut(e, "ShhherCommand", "ShhherCommand");
83 //**********************************************************************************************************************
85 ShhherCommand::ShhherCommand(string option) {
89 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
90 MPI_Comm_size(MPI_COMM_WORLD, &ncpus);
96 abort = false; calledHelp = false;
99 //allow user to run help
100 if(option == "help") { help(); abort = true; calledHelp = true; }
101 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
104 vector<string> myArray = setParameters();
106 OptionParser parser(option);
107 map<string,string> parameters = parser.getParameters();
109 ValidParameters validParameter;
110 map<string,string>::iterator it;
112 //check to make sure all parameters are valid for command
113 for (it = parameters.begin(); it != parameters.end(); it++) {
114 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
117 //initialize outputTypes
118 vector<string> tempOutNames;
119 // outputTypes["pn.dist"] = tempOutNames;
120 // outputTypes["fasta"] = tempOutNames;
122 //if the user changes the input directory command factory will send this info to us in the output parameter
123 string inputDir = validParameter.validFile(parameters, "inputdir", false);
124 if (inputDir == "not found"){ inputDir = ""; }
127 it = parameters.find("flow");
128 //user has given a template file
129 if(it != parameters.end()){
130 path = m->hasPath(it->second);
131 //if the user has not given a path then, add inputdir. else leave path alone.
132 if (path == "") { parameters["flow"] = inputDir + it->second; }
135 it = parameters.find("lookup");
136 //user has given a template file
137 if(it != parameters.end()){
138 path = m->hasPath(it->second);
139 //if the user has not given a path then, add inputdir. else leave path alone.
140 if (path == "") { parameters["lookup"] = inputDir + it->second; }
143 it = parameters.find("file");
144 //user has given a template file
145 if(it != parameters.end()){
146 path = m->hasPath(it->second);
147 //if the user has not given a path then, add inputdir. else leave path alone.
148 if (path == "") { parameters["file"] = inputDir + it->second; }
153 //check for required parameters
154 flowFileName = validParameter.validFile(parameters, "flow", true);
155 flowFilesFileName = validParameter.validFile(parameters, "file", true);
156 if (flowFileName == "not found" && flowFilesFileName == "not found") {
157 m->mothurOut("values for either flow or file must be provided for the shhh.seqs command.");
158 m->mothurOutEndLine();
161 else if (flowFileName == "not open" || flowFilesFileName == "not open") { abort = true; }
163 if(flowFileName != "not found"){
164 compositeFASTAFileName = "";
165 compositeNamesFileName = "";
170 compositeFASTAFileName = flowFilesFileName.substr(0, flowFilesFileName.length()-10) + "shhh.fasta";
171 m->openOutputFile(compositeFASTAFileName, temp);
174 compositeNamesFileName = flowFilesFileName.substr(0, flowFilesFileName.length()-10) + "shhh.names";
175 m->openOutputFile(compositeNamesFileName, temp);
179 //if the user changes the output directory command factory will send this info to us in the output parameter
180 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
182 outputDir += m->hasPath(flowFileName); //if user entered a file with a path then preserve it
186 //check for optional parameter and set defaults
187 // ...at some point should added some additional type checking...
189 temp = validParameter.validFile(parameters, "lookup", true);
190 if (temp == "not found") { lookupFileName = "LookUp_Titanium.pat"; }
191 else if(temp == "not open") { abort = true; }
192 else { lookupFileName = temp; }
194 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
195 m->setProcessors(temp);
196 convert(temp, processors);
198 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found"){ temp = "0.01"; }
199 convert(temp, cutoff);
201 temp = validParameter.validFile(parameters, "mindelta", false); if (temp == "not found"){ temp = "0.000001"; }
202 convert(temp, minDelta);
204 temp = validParameter.validFile(parameters, "maxiter", false); if (temp == "not found"){ temp = "1000"; }
205 convert(temp, maxIters);
207 temp = validParameter.validFile(parameters, "sigma", false);if (temp == "not found") { temp = "60"; }
208 convert(temp, sigma);
210 flowOrder = validParameter.validFile(parameters, "order", false);
211 if (flowOrder == "not found"){ flowOrder = "TACG"; }
212 else if(flowOrder.length() != 4){
213 m->mothurOut("The value of the order option must be four bases long\n");
223 catch(exception& e) {
224 m->errorOut(e, "ShhherCommand", "ShhherCommand");
228 //**********************************************************************************************************************
230 int ShhherCommand::execute(){
232 if (abort == true) { if (calledHelp) { return 0; } return 2; }
239 for(int i=1;i<ncpus;i++){
240 MPI_Send(&abort, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
242 if(abort == 1){ return 0; }
246 m->mothurOut("\nGetting preliminary data...\n");
250 vector<string> flowFileVector;
251 if(flowFilesFileName != "not found"){
254 ifstream flowFilesFile;
255 m->openInputFile(flowFilesFileName, flowFilesFile);
256 while(flowFilesFile){
257 flowFilesFile >> fName;
258 flowFileVector.push_back(fName);
259 m->gobble(flowFilesFile);
263 flowFileVector.push_back(flowFileName);
265 int numFiles = flowFileVector.size();
267 for(int i=1;i<ncpus;i++){
268 MPI_Send(&numFiles, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
271 for(int i=0;i<numFiles;i++){
272 double begClock = clock();
273 unsigned long int begTime = time(NULL);
275 flowFileName = flowFileVector[i];
277 m->mothurOut("\n>>>>>\tProcessing " + flowFileName + " (file " + toString(i+1) + " of " + toString(numFiles) + ")\t<<<<<\n");
278 m->mothurOut("Reading flowgrams...\n");
281 m->mothurOut("Identifying unique flowgrams...\n");
284 m->mothurOut("Calculating distances between flowgrams...\n");
286 strcpy(fileName, flowFileName.c_str());
288 for(int i=1;i<ncpus;i++){
289 MPI_Send(&fileName[0], 1024, MPI_CHAR, i, tag, MPI_COMM_WORLD);
291 MPI_Send(&numSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
292 MPI_Send(&numUniques, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
293 MPI_Send(&numFlowCells, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
294 MPI_Send(&flowDataIntI[0], numSeqs * numFlowCells, MPI_SHORT, i, tag, MPI_COMM_WORLD);
295 MPI_Send(&flowDataPrI[0], numSeqs * numFlowCells, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
296 MPI_Send(&mapUniqueToSeq[0], numSeqs, MPI_INT, i, tag, MPI_COMM_WORLD);
297 MPI_Send(&mapSeqToUnique[0], numSeqs, MPI_INT, i, tag, MPI_COMM_WORLD);
298 MPI_Send(&lengths[0], numSeqs, MPI_INT, i, tag, MPI_COMM_WORLD);
299 MPI_Send(&jointLookUp[0], NUMBINS * NUMBINS, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
300 MPI_Send(&cutoff, 1, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
303 string distFileName = flowDistMPI(0, int(sqrt(1.0/float(ncpus)) * numUniques));
306 for(int i=1;i<ncpus;i++){
307 MPI_Recv(&done, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
309 m->appendFiles((distFileName + ".temp." + toString(i)), distFileName);
310 remove((distFileName + ".temp." + toString(i)).c_str());
313 string namesFileName = createNamesFile();
315 m->mothurOut("\nClustering flowgrams...\n");
316 string listFileName = cluster(distFileName, namesFileName);
318 getOTUData(listFileName);
320 remove(distFileName.c_str());
321 remove(namesFileName.c_str());
322 remove(listFileName.c_str());
326 for(int i=1;i<ncpus;i++){
327 MPI_Send(&numOTUs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
328 MPI_Send(&singleLookUp[0], singleLookUp.size(), MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
329 MPI_Send(&uniqueFlowgrams[0], numFlowCells * numUniques, MPI_SHORT, i, tag, MPI_COMM_WORLD);
330 MPI_Send(&sigma, 1, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
337 int numOTUsOnCPU = numOTUs / ncpus;
338 int numSeqsOnCPU = numSeqs / ncpus;
339 m->mothurOut("\nDenoising flowgrams...\n");
340 m->mothurOut("iter\tmaxDelta\tnLL\t\tcycletime\n");
342 while((maxIters == 0 && maxDelta > minDelta) || iter < MIN_ITER || (maxDelta > minDelta && iter < maxIters)){
344 double cycClock = clock();
345 unsigned long int cycTime = time(NULL);
348 int total = singleTau.size();
349 for(int i=1;i<ncpus;i++){
350 MPI_Send(&total, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
351 MPI_Send(&change[0], numOTUs, MPI_SHORT, i, tag, MPI_COMM_WORLD);
352 MPI_Send(¢roids[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
354 MPI_Send(&singleTau[0], total, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
355 MPI_Send(&seqNumber[0], total, MPI_INT, i, tag, MPI_COMM_WORLD);
356 MPI_Send(&seqIndex[0], total, MPI_INT, i, tag, MPI_COMM_WORLD);
357 MPI_Send(&nSeqsPerOTU[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
358 MPI_Send(&cumNumSeqs[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
361 calcCentroidsDriver(0, numOTUsOnCPU);
363 for(int i=1;i<ncpus;i++){
364 int otuStart = i * numOTUs / ncpus;
365 int otuStop = (i + 1) * numOTUs / ncpus;
367 vector<int> tempCentroids(numOTUs, 0);
368 vector<short> tempChange(numOTUs, 0);
370 MPI_Recv(&tempCentroids[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
371 MPI_Recv(&tempChange[0], numOTUs, MPI_SHORT, i, tag, MPI_COMM_WORLD, &status);
373 for(int j=otuStart;j<otuStop;j++){
374 centroids[j] = tempCentroids[j];
375 change[j] = tempChange[j];
379 maxDelta = getNewWeights();
380 double nLL = getLikelihood();
383 for(int i=1;i<ncpus;i++){
384 MPI_Send(¢roids[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
385 MPI_Send(&weight[0], numOTUs, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
386 MPI_Send(&change[0], numOTUs, MPI_SHORT, i, tag, MPI_COMM_WORLD);
389 calcNewDistancesParent(0, numSeqsOnCPU);
391 total = singleTau.size();
393 for(int i=1;i<ncpus;i++){
395 int seqStart = i * numSeqs / ncpus;
396 int seqStop = (i + 1) * numSeqs / ncpus;
398 MPI_Recv(&childTotal, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
400 vector<int> childSeqIndex(childTotal, 0);
401 vector<double> childSingleTau(childTotal, 0);
402 vector<double> childDist(numSeqs * numOTUs, 0);
403 vector<int> otuIndex(childTotal, 0);
405 MPI_Recv(&childSeqIndex[0], childTotal, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
406 MPI_Recv(&childSingleTau[0], childTotal, MPI_DOUBLE, i, tag, MPI_COMM_WORLD, &status);
407 MPI_Recv(&childDist[0], numOTUs * numSeqs, MPI_DOUBLE, i, tag, MPI_COMM_WORLD, &status);
408 MPI_Recv(&otuIndex[0], childTotal, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
410 int oldTotal = total;
412 singleTau.resize(total, 0);
413 seqIndex.resize(total, 0);
414 seqNumber.resize(total, 0);
418 for(int j=oldTotal;j<total;j++){
419 int otuI = otuIndex[childIndex];
420 int seqI = childSeqIndex[childIndex];
422 singleTau[j] = childSingleTau[childIndex];
424 aaP[otuI][nSeqsPerOTU[otuI]] = j;
425 aaI[otuI][nSeqsPerOTU[otuI]] = seqI;
430 int index = seqStart * numOTUs;
431 for(int j=seqStart;j<seqStop;j++){
432 for(int k=0;k<numOTUs;k++){
433 dist[index] = childDist[index];
441 m->mothurOut(toString(iter) + '\t' + toString(maxDelta) + '\t' + toString(nLL) + '\t' + toString(time(NULL) - cycTime) + '\t' + toString((clock() - cycClock)/(double)CLOCKS_PER_SEC) + '\n');
443 if((maxIters == 0 && maxDelta > minDelta) || iter < MIN_ITER || (maxDelta > minDelta && iter < maxIters)){
445 for(int i=1;i<ncpus;i++){
446 MPI_Send(&live, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
451 for(int i=1;i<ncpus;i++){
452 MPI_Send(&live, 1, MPI_INT, i, tag, MPI_COMM_WORLD); //send kill command
458 m->mothurOut("\nFinalizing...\n");
462 vector<int> otuCounts(numOTUs, 0);
463 for(int i=0;i<numSeqs;i++) { otuCounts[otuData[i]]++; }
464 calcCentroidsDriver(0, numOTUs);
466 writeQualities(otuCounts);
467 writeSequences(otuCounts);
468 writeNames(otuCounts);
469 writeClusters(otuCounts);
473 m->mothurOut("Total time to process " + toString(flowFileName) + ":\t" + toString(time(NULL) - begTime) + '\t' + toString((clock() - begClock)/(double)CLOCKS_PER_SEC) + '\n');
479 MPI_Recv(&abort, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
480 if(abort){ return 0; }
483 MPI_Recv(&numFiles, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
485 for(int i=0;i<numFiles;i++){
486 //Now into the pyrodist part
490 MPI_Recv(&fileName, 1024, MPI_CHAR, 0, tag, MPI_COMM_WORLD, &status);
491 MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
492 MPI_Recv(&numUniques, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
493 MPI_Recv(&numFlowCells, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
495 flowDataIntI.resize(numSeqs * numFlowCells);
496 flowDataPrI.resize(numSeqs * numFlowCells);
497 mapUniqueToSeq.resize(numSeqs);
498 mapSeqToUnique.resize(numSeqs);
499 lengths.resize(numSeqs);
500 jointLookUp.resize(NUMBINS * NUMBINS);
502 MPI_Recv(&flowDataIntI[0], numSeqs * numFlowCells, MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
503 MPI_Recv(&flowDataPrI[0], numSeqs * numFlowCells, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
504 MPI_Recv(&mapUniqueToSeq[0], numSeqs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
505 MPI_Recv(&mapSeqToUnique[0], numSeqs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
506 MPI_Recv(&lengths[0], numSeqs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
507 MPI_Recv(&jointLookUp[0], NUMBINS * NUMBINS, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
508 MPI_Recv(&cutoff, 1, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
510 flowFileName = string(fileName);
511 int flowDistStart = int(sqrt(float(pid)/float(ncpus)) * numUniques);
512 int flowDistEnd = int(sqrt(float(pid+1)/float(ncpus)) * numUniques);
514 string distanceStringChild = flowDistMPI(flowDistStart, flowDistEnd);
517 MPI_Send(&done, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
519 //Now into the pyronoise part
520 MPI_Recv(&numOTUs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
522 singleLookUp.resize(HOMOPS * NUMBINS);
523 uniqueFlowgrams.resize(numUniques * numFlowCells);
524 weight.resize(numOTUs);
525 centroids.resize(numOTUs);
526 change.resize(numOTUs);
527 dist.assign(numOTUs * numSeqs, 0);
528 nSeqsPerOTU.resize(numOTUs);
529 cumNumSeqs.resize(numOTUs);
531 MPI_Recv(&singleLookUp[0], singleLookUp.size(), MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
532 MPI_Recv(&uniqueFlowgrams[0], uniqueFlowgrams.size(), MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
533 MPI_Recv(&sigma, 1, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
535 int startOTU = pid * numOTUs / ncpus;
536 int endOTU = (pid + 1) * numOTUs / ncpus;
538 int startSeq = pid * numSeqs / ncpus;
539 int endSeq = (pid + 1) * numSeqs /ncpus;
545 MPI_Recv(&total, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
546 singleTau.assign(total, 0.0000);
547 seqNumber.assign(total, 0);
548 seqIndex.assign(total, 0);
550 MPI_Recv(&change[0], numOTUs, MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
551 MPI_Recv(¢roids[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
552 MPI_Recv(&singleTau[0], total, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
553 MPI_Recv(&seqNumber[0], total, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
554 MPI_Recv(&seqIndex[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
555 MPI_Recv(&nSeqsPerOTU[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
556 MPI_Recv(&cumNumSeqs[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
558 calcCentroidsDriver(startOTU, endOTU);
560 MPI_Send(¢roids[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD);
561 MPI_Send(&change[0], numOTUs, MPI_SHORT, 0, tag, MPI_COMM_WORLD);
563 MPI_Recv(¢roids[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
564 MPI_Recv(&weight[0], numOTUs, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
565 MPI_Recv(&change[0], numOTUs, MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
567 vector<int> otuIndex(total, 0);
568 calcNewDistancesChildMPI(startSeq, endSeq, otuIndex);
569 total = otuIndex.size();
571 MPI_Send(&total, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
572 MPI_Send(&seqIndex[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD);
573 MPI_Send(&singleTau[0], total, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD);
574 MPI_Send(&dist[0], numOTUs * numSeqs, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD);
575 MPI_Send(&otuIndex[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD);
577 MPI_Recv(&live, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
581 MPI_Barrier(MPI_COMM_WORLD);
584 if(compositeFASTAFileName != ""){
585 outputNames.push_back(compositeFASTAFileName);
586 outputNames.push_back(compositeNamesFileName);
589 m->mothurOutEndLine();
590 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
591 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
592 m->mothurOutEndLine();
597 catch(exception& e) {
598 m->errorOut(e, "ShhherCommand", "execute");
603 /**************************************************************************************************/
605 string ShhherCommand::flowDistMPI(int startSeq, int stopSeq){
607 ostringstream outStream;
608 outStream.setf(ios::fixed, ios::floatfield);
609 outStream.setf(ios::dec, ios::basefield);
610 outStream.setf(ios::showpoint);
611 outStream.precision(6);
613 int begTime = time(NULL);
614 double begClock = clock();
616 for(int i=startSeq;i<stopSeq;i++){
617 for(int j=0;j<i;j++){
618 float flowDistance = calcPairwiseDist(mapUniqueToSeq[i], mapUniqueToSeq[j]);
620 if(flowDistance < 1e-6){
621 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << 0.000000 << endl;
623 else if(flowDistance <= cutoff){
624 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << flowDistance << endl;
628 m->mothurOut(toString(i) + '\t' + toString(time(NULL) - begTime) + '\t' + toString((clock()-begClock)/CLOCKS_PER_SEC) + '\n');
632 m->mothurOut(toString(stopSeq) + '\t' + toString(time(NULL) - begTime) + '\t' + toString((clock()-begClock)/CLOCKS_PER_SEC) + '\n');
634 string fDistFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.dist";
635 if(pid != 0){ fDistFileName += ".temp." + toString(pid); }
637 ofstream distFile(fDistFileName.c_str());
638 distFile << outStream.str();
641 return fDistFileName;
643 catch(exception& e) {
644 m->errorOut(e, "ShhherCommand", "flowDistParentFork");
650 //**********************************************************************************************************************
652 int ShhherCommand::execute(){
654 if (abort == true) { return 0; }
659 vector<string> flowFileVector;
660 if(flowFilesFileName != "not found"){
663 ifstream flowFilesFile;
664 m->openInputFile(flowFilesFileName, flowFilesFile);
665 while(flowFilesFile){
666 flowFilesFile >> fName;
667 flowFileVector.push_back(fName);
668 m->gobble(flowFilesFile);
672 flowFileVector.push_back(flowFileName);
674 int numFiles = flowFileVector.size();
677 for(int i=0;i<numFiles;i++){
678 flowFileName = flowFileVector[i];
680 m->mothurOut("\n>>>>>\tProcessing " + flowFileName + " (file " + toString(i+1) + " of " + toString(numFiles) + ")\t<<<<<\n");
681 m->mothurOut("Reading flowgrams...\n");
684 m->mothurOut("Identifying unique flowgrams...\n");
688 m->mothurOut("Calculating distances between flowgrams...\n");
689 string distFileName = createDistFile(processors);
690 string namesFileName = createNamesFile();
692 m->mothurOut("\nClustering flowgrams...\n");
693 string listFileName = cluster(distFileName, namesFileName);
694 getOTUData(listFileName);
696 remove(distFileName.c_str());
697 remove(namesFileName.c_str());
698 remove(listFileName.c_str());
705 double begClock = clock();
706 unsigned long int begTime = time(NULL);
708 m->mothurOut("\nDenoising flowgrams...\n");
709 m->mothurOut("iter\tmaxDelta\tnLL\t\tcycletime\n");
711 while((maxIters == 0 && maxDelta > minDelta) || iter < MIN_ITER || (maxDelta > minDelta && iter < maxIters)){
713 double cycClock = clock();
714 unsigned long int cycTime = time(NULL);
719 maxDelta = getNewWeights();
720 double nLL = getLikelihood();
727 m->mothurOut(toString(iter) + '\t' + toString(maxDelta) + '\t' + toString(nLL) + '\t' + toString(time(NULL) - cycTime) + '\t' + toString((clock() - cycClock)/(double)CLOCKS_PER_SEC) + '\n');
731 m->mothurOut("\nFinalizing...\n");
735 vector<int> otuCounts(numOTUs, 0);
736 for(int i=0;i<numSeqs;i++) { otuCounts[otuData[i]]++; }
738 calcCentroidsDriver(0, numOTUs);
739 writeQualities(otuCounts);
740 writeSequences(otuCounts);
741 writeNames(otuCounts);
742 writeClusters(otuCounts);
745 m->mothurOut("Total time to process " + flowFileName + ":\t" + toString(time(NULL) - begTime) + '\t' + toString((clock() - begClock)/(double)CLOCKS_PER_SEC) + '\n');
748 if(compositeFASTAFileName != ""){
749 outputNames.push_back(compositeFASTAFileName);
750 outputNames.push_back(compositeNamesFileName);
753 m->mothurOutEndLine();
754 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
755 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
756 m->mothurOutEndLine();
760 catch(exception& e) {
761 m->errorOut(e, "ShhherCommand", "execute");
766 /**************************************************************************************************/
768 void ShhherCommand::getFlowData(){
771 m->openInputFile(flowFileName, flowFile);
774 seqNameVector.clear();
776 flowDataIntI.clear();
780 int currentNumFlowCells;
784 flowFile >> numFlowCells;
785 int index = 0;//pcluster
786 while(!flowFile.eof()){
787 flowFile >> seqName >> currentNumFlowCells;
788 lengths.push_back(currentNumFlowCells);
790 seqNameVector.push_back(seqName);
791 nameMap[seqName] = index++;//pcluster
793 for(int i=0;i<numFlowCells;i++){
794 flowFile >> intensity;
795 if(intensity > 9.99) { intensity = 9.99; }
796 int intI = int(100 * intensity + 0.0001);
797 flowDataIntI.push_back(intI);
803 numSeqs = seqNameVector.size();
805 for(int i=0;i<numSeqs;i++){
806 int iNumFlowCells = i * numFlowCells;
807 for(int j=lengths[i];j<numFlowCells;j++){
808 flowDataIntI[iNumFlowCells + j] = 0;
813 catch(exception& e) {
814 m->errorOut(e, "ShhherCommand", "getFlowData");
819 /**************************************************************************************************/
821 void ShhherCommand::getSingleLookUp(){
823 // these are the -log probabilities that a signal corresponds to a particular homopolymer length
824 singleLookUp.assign(HOMOPS * NUMBINS, 0);
828 m->openInputFile(lookupFileName, lookUpFile);
830 for(int i=0;i<HOMOPS;i++){
832 lookUpFile >> logFracFreq;
834 for(int j=0;j<NUMBINS;j++) {
835 lookUpFile >> singleLookUp[index];
841 catch(exception& e) {
842 m->errorOut(e, "ShhherCommand", "getSingleLookUp");
847 /**************************************************************************************************/
849 void ShhherCommand::getJointLookUp(){
852 // the most likely joint probability (-log) that two intenities have the same polymer length
853 jointLookUp.resize(NUMBINS * NUMBINS, 0);
855 for(int i=0;i<NUMBINS;i++){
856 for(int j=0;j<NUMBINS;j++){
858 double minSum = 100000000;
860 for(int k=0;k<HOMOPS;k++){
861 double sum = singleLookUp[k * NUMBINS + i] + singleLookUp[k * NUMBINS + j];
863 if(sum < minSum) { minSum = sum; }
865 jointLookUp[i * NUMBINS + j] = minSum;
869 catch(exception& e) {
870 m->errorOut(e, "ShhherCommand", "getJointLookUp");
875 /**************************************************************************************************/
877 double ShhherCommand::getProbIntensity(int intIntensity){
880 double minNegLogProb = 100000000;
883 for(int i=0;i<HOMOPS;i++){//loop signal strength
884 float negLogProb = singleLookUp[i * NUMBINS + intIntensity];
885 if(negLogProb < minNegLogProb) { minNegLogProb = negLogProb; }
888 return minNegLogProb;
890 catch(exception& e) {
891 m->errorOut(e, "ShhherCommand", "getProbIntensity");
896 /**************************************************************************************************/
898 void ShhherCommand::getUniques(){
903 uniqueFlowgrams.assign(numFlowCells * numSeqs, -1);
904 uniqueCount.assign(numSeqs, 0); // anWeights
905 uniqueLengths.assign(numSeqs, 0);
906 mapSeqToUnique.assign(numSeqs, -1);
907 mapUniqueToSeq.assign(numSeqs, -1);
909 vector<short> uniqueFlowDataIntI(numFlowCells * numSeqs, -1);
911 for(int i=0;i<numSeqs;i++){
914 vector<short> current(numFlowCells);
915 for(int j=0;j<numFlowCells;j++){
916 current[j] = short(((flowDataIntI[i * numFlowCells + j] + 50.0)/100.0));
919 for(int j=0;j<numUniques;j++){
920 int offset = j * numFlowCells;
924 if(lengths[i] < uniqueLengths[j]) { shorterLength = lengths[i]; }
925 else { shorterLength = uniqueLengths[j]; }
927 for(int k=0;k<shorterLength;k++){
928 if(current[k] != uniqueFlowgrams[offset + k]){
935 mapSeqToUnique[i] = j;
938 if(lengths[i] > uniqueLengths[j]) { uniqueLengths[j] = lengths[i]; }
944 if(index == numUniques){
945 uniqueLengths[numUniques] = lengths[i];
946 uniqueCount[numUniques] = 1;
947 mapSeqToUnique[i] = numUniques;//anMap
948 mapUniqueToSeq[numUniques] = i;//anF
950 for(int k=0;k<numFlowCells;k++){
951 uniqueFlowgrams[numUniques * numFlowCells + k] = current[k];
952 uniqueFlowDataIntI[numUniques * numFlowCells + k] = flowDataIntI[i * numFlowCells + k];
958 uniqueFlowDataIntI.resize(numFlowCells * numUniques);
959 uniqueLengths.resize(numUniques);
961 flowDataPrI.resize(numSeqs * numFlowCells, 0);
962 for(int i=0;i<flowDataPrI.size();i++) { flowDataPrI[i] = getProbIntensity(flowDataIntI[i]); }
964 catch(exception& e) {
965 m->errorOut(e, "ShhherCommand", "getUniques");
970 /**************************************************************************************************/
972 float ShhherCommand::calcPairwiseDist(int seqA, int seqB){
974 int minLength = lengths[mapSeqToUnique[seqA]];
975 if(lengths[seqB] < minLength){ minLength = lengths[mapSeqToUnique[seqB]]; }
977 int ANumFlowCells = seqA * numFlowCells;
978 int BNumFlowCells = seqB * numFlowCells;
982 for(int i=0;i<minLength;i++){
983 int flowAIntI = flowDataIntI[ANumFlowCells + i];
984 float flowAPrI = flowDataPrI[ANumFlowCells + i];
986 int flowBIntI = flowDataIntI[BNumFlowCells + i];
987 float flowBPrI = flowDataPrI[BNumFlowCells + i];
988 dist += jointLookUp[flowAIntI * NUMBINS + flowBIntI] - flowAPrI - flowBPrI;
991 dist /= (float) minLength;
994 catch(exception& e) {
995 m->errorOut(e, "ShhherCommand", "calcPairwiseDist");
1000 /**************************************************************************************************/
1002 void ShhherCommand::flowDistParentFork(string distFileName, int startSeq, int stopSeq){
1005 ostringstream outStream;
1006 outStream.setf(ios::fixed, ios::floatfield);
1007 outStream.setf(ios::dec, ios::basefield);
1008 outStream.setf(ios::showpoint);
1009 outStream.precision(6);
1011 int begTime = time(NULL);
1012 double begClock = clock();
1014 for(int i=startSeq;i<stopSeq;i++){
1015 for(int j=0;j<i;j++){
1016 float flowDistance = calcPairwiseDist(mapUniqueToSeq[i], mapUniqueToSeq[j]);
1018 if(flowDistance < 1e-6){
1019 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << 0.000000 << endl;
1021 else if(flowDistance <= cutoff){
1022 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << flowDistance << endl;
1026 m->mothurOut(toString(i) + "\t" + toString(time(NULL) - begTime));
1027 m->mothurOut("\t" + toString((clock()-begClock)/CLOCKS_PER_SEC));
1028 m->mothurOutEndLine();
1031 m->mothurOut(toString(stopSeq-1) + "\t" + toString(time(NULL) - begTime));
1032 m->mothurOut("\t" + toString((clock()-begClock)/CLOCKS_PER_SEC));
1033 m->mothurOutEndLine();
1035 ofstream distFile(distFileName.c_str());
1036 distFile << outStream.str();
1039 catch(exception& e) {
1040 m->errorOut(e, "ShhherCommand", "flowDistParentFork");
1045 /**************************************************************************************************/
1047 string ShhherCommand::createDistFile(int processors){
1049 string fDistFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.dist";
1051 unsigned long int begTime = time(NULL);
1052 double begClock = clock();
1057 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
1058 if(processors == 1) { flowDistParentFork(fDistFileName, 0, numUniques); }
1059 else{ //you have multiple processors
1061 if (numSeqs < processors){ processors = 1; }
1063 vector<int> start(processors, 0);
1064 vector<int> end(processors, 0);
1066 for (int i = 0; i < processors; i++) {
1067 start[i] = int(sqrt(float(i)/float(processors)) * numUniques);
1068 end[i] = int(sqrt(float(i+1)/float(processors)) * numUniques);
1072 vector<int> processIDs;
1074 //loop through and create all the processes you want
1075 while (process != processors) {
1079 processIDs.push_back(pid); //create map from line number to pid so you can append files in correct order later
1081 }else if (pid == 0){
1082 flowDistParentFork(fDistFileName + toString(getpid()) + ".temp", start[process], end[process]);
1085 m->mothurOut("[ERROR]: unable to spawn the necessary processes. Error code: " + toString(pid)); m->mothurOutEndLine();
1087 for (int i=0;i<processIDs.size();i++) { int temp = processIDs[i]; kill (temp, SIGINT); }
1092 //parent does its part
1093 flowDistParentFork(fDistFileName, start[0], end[0]);
1095 //force parent to wait until all the processes are done
1096 for (int i=0;i<processIDs.size();i++) {
1097 int temp = processIDs[i];
1101 //append and remove temp files
1102 for (int i=0;i<processIDs.size();i++) {
1103 m->appendFiles((fDistFileName + toString(processIDs[i]) + ".temp"), fDistFileName);
1104 remove((fDistFileName + toString(processIDs[i]) + ".temp").c_str());
1110 flowDistParentFork(fDistFileName, 0, numUniques);
1113 m->mothurOutEndLine();
1115 m->mothurOut("Total time: " + toString(time(NULL) - begTime) + '\t' + toString((clock() - begClock)/CLOCKS_PER_SEC) + '\n');
1118 return fDistFileName;
1120 catch(exception& e) {
1121 m->errorOut(e, "ShhherCommand", "createDistFile");
1127 /**************************************************************************************************/
1129 string ShhherCommand::createNamesFile(){
1132 vector<string> duplicateNames(numUniques, "");
1133 for(int i=0;i<numSeqs;i++){
1134 duplicateNames[mapSeqToUnique[i]] += seqNameVector[i] + ',';
1137 string nameFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.names";
1140 m->openOutputFile(nameFileName, nameFile);
1142 for(int i=0;i<numUniques;i++){
1143 // nameFile << seqNameVector[mapUniqueToSeq[i]] << '\t' << duplicateNames[i].substr(0, duplicateNames[i].find_last_of(',')) << endl;
1144 nameFile << mapUniqueToSeq[i] << '\t' << duplicateNames[i].substr(0, duplicateNames[i].find_last_of(',')) << endl;
1148 return nameFileName;
1150 catch(exception& e) {
1151 m->errorOut(e, "ShhherCommand", "createNamesFile");
1156 //**********************************************************************************************************************
1158 string ShhherCommand::cluster(string distFileName, string namesFileName){
1161 ReadMatrix* read = new ReadColumnMatrix(distFileName);
1162 read->setCutoff(cutoff);
1164 NameAssignment* clusterNameMap = new NameAssignment(namesFileName);
1165 clusterNameMap->readMap();
1166 read->read(clusterNameMap);
1168 ListVector* list = read->getListVector();
1169 SparseMatrix* matrix = read->getMatrix();
1172 delete clusterNameMap;
1174 RAbundVector* rabund = new RAbundVector(list->getRAbundVector());
1176 Cluster* cluster = new CompleteLinkage(rabund, list, matrix, cutoff, "furthest");
1177 string tag = cluster->getTag();
1179 double clusterCutoff = cutoff;
1180 while (matrix->getSmallDist() <= clusterCutoff && matrix->getNNodes() > 0){
1181 cluster->update(clusterCutoff);
1184 list->setLabel(toString(cutoff));
1186 string listFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.list";
1188 m->openOutputFile(listFileName, listFile);
1189 list->print(listFile);
1192 delete matrix; delete cluster; delete rabund; delete list;
1194 return listFileName;
1196 catch(exception& e) {
1197 m->errorOut(e, "ShhherCommand", "cluster");
1202 /**************************************************************************************************/
1204 void ShhherCommand::getOTUData(string listFileName){
1208 m->openInputFile(listFileName, listFile);
1211 listFile >> label >> numOTUs;
1213 otuData.assign(numSeqs, 0);
1214 cumNumSeqs.assign(numOTUs, 0);
1215 nSeqsPerOTU.assign(numOTUs, 0);
1216 aaP.clear();aaP.resize(numOTUs);
1222 string singleOTU = "";
1224 for(int i=0;i<numOTUs;i++){
1226 listFile >> singleOTU;
1228 istringstream otuString(singleOTU);
1232 string seqName = "";
1234 for(int j=0;j<singleOTU.length();j++){
1235 char letter = otuString.get();
1241 map<string,int>::iterator nmIt = nameMap.find(seqName);
1242 int index = nmIt->second;
1244 nameMap.erase(nmIt);
1248 aaP[i].push_back(index);
1253 map<string,int>::iterator nmIt = nameMap.find(seqName);
1255 int index = nmIt->second;
1256 nameMap.erase(nmIt);
1260 aaP[i].push_back(index);
1265 sort(aaP[i].begin(), aaP[i].end());
1266 for(int j=0;j<nSeqsPerOTU[i];j++){
1267 seqNumber.push_back(aaP[i][j]);
1269 for(int j=nSeqsPerOTU[i];j<numSeqs;j++){
1270 aaP[i].push_back(0);
1276 for(int i=1;i<numOTUs;i++){
1277 cumNumSeqs[i] = cumNumSeqs[i-1] + nSeqsPerOTU[i-1];
1280 seqIndex = seqNumber;
1285 catch(exception& e) {
1286 m->errorOut(e, "ShhherCommand", "getOTUData");
1291 /**************************************************************************************************/
1293 void ShhherCommand::initPyroCluster(){
1295 dist.assign(numSeqs * numOTUs, 0);
1296 change.assign(numOTUs, 1);
1297 centroids.assign(numOTUs, -1);
1298 weight.assign(numOTUs, 0);
1299 singleTau.assign(numSeqs, 1.0);
1301 nSeqsBreaks.assign(processors+1, 0);
1302 nOTUsBreaks.assign(processors+1, 0);
1305 for(int i=0;i<processors;i++){
1306 nSeqsBreaks[i+1] = nSeqsBreaks[i] + (int)((double) numSeqs / (double) processors);
1307 nOTUsBreaks[i+1] = nOTUsBreaks[i] + (int)((double) numOTUs / (double) processors);
1309 nSeqsBreaks[processors] = numSeqs;
1310 nOTUsBreaks[processors] = numOTUs;
1312 catch(exception& e) {
1313 m->errorOut(e, "ShhherCommand", "initPyroCluster");
1318 /**************************************************************************************************/
1320 void ShhherCommand::fill(){
1323 for(int i=0;i<numOTUs;i++){
1324 cumNumSeqs[i] = index;
1325 for(int j=0;j<nSeqsPerOTU[i];j++){
1326 seqNumber[index] = aaP[i][j];
1327 seqIndex[index] = aaI[i][j];
1333 catch(exception& e) {
1334 m->errorOut(e, "ShhherCommand", "fill");
1339 /**************************************************************************************************/
1341 void ShhherCommand::calcCentroids(){
1344 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
1346 if(processors == 1) {
1347 calcCentroidsDriver(0, numOTUs);
1349 else{ //you have multiple processors
1350 if (numOTUs < processors){ processors = 1; }
1353 vector<int> processIDs;
1355 //loop through and create all the processes you want
1356 while (process != processors) {
1360 processIDs.push_back(pid); //create map from line number to pid so you can append files in correct order later
1362 }else if (pid == 0){
1363 calcCentroidsDriver(nOTUsBreaks[process], nOTUsBreaks[process+1]);
1366 m->mothurOut("[ERROR]: unable to spawn the necessary processes. Error code: " + toString(pid)); m->mothurOutEndLine();
1368 for (int i=0;i<processIDs.size();i++) { int temp = processIDs[i]; kill (temp, SIGINT); }
1373 //parent does its part
1374 calcCentroidsDriver(nOTUsBreaks[0], nOTUsBreaks[1]);
1376 //force parent to wait until all the processes are done
1377 for (int i=0;i<processIDs.size();i++) {
1378 int temp = processIDs[i];
1384 calcCentroidsDriver(0, numOTUs);
1387 catch(exception& e) {
1388 m->errorOut(e, "ShhherCommand", "calcCentroidsDriver");
1393 /**************************************************************************************************/
1395 void ShhherCommand::calcCentroidsDriver(int start, int finish){
1397 //this function gets the most likely homopolymer length at a flow position for a group of sequences
1403 for(int i=start;i<finish;i++){
1407 int minFlowGram = 100000000;
1408 double minFlowValue = 1e8;
1409 change[i] = 0; //FALSE
1411 for(int j=0;j<nSeqsPerOTU[i];j++){
1412 count += singleTau[seqNumber[cumNumSeqs[i] + j]];
1415 if(nSeqsPerOTU[i] > 0 && count > MIN_COUNT){
1416 vector<double> adF(nSeqsPerOTU[i]);
1417 vector<int> anL(nSeqsPerOTU[i]);
1419 for(int j=0;j<nSeqsPerOTU[i];j++){
1420 int index = cumNumSeqs[i] + j;
1421 int nI = seqIndex[index];
1422 int nIU = mapSeqToUnique[nI];
1425 for(k=0;k<position;k++){
1431 anL[position] = nIU;
1432 adF[position] = 0.0000;
1437 for(int j=0;j<nSeqsPerOTU[i];j++){
1438 int index = cumNumSeqs[i] + j;
1439 int nI = seqIndex[index];
1441 double tauValue = singleTau[seqNumber[index]];
1443 for(int k=0;k<position;k++){
1444 double dist = getDistToCentroid(anL[k], nI, lengths[nI]);
1445 adF[k] += dist * tauValue;
1449 for(int j=0;j<position;j++){
1450 if(adF[j] < minFlowValue){
1452 minFlowValue = adF[j];
1456 if(centroids[i] != anL[minFlowGram]){
1458 centroids[i] = anL[minFlowGram];
1461 else if(centroids[i] != -1){
1467 catch(exception& e) {
1468 m->errorOut(e, "ShhherCommand", "calcCentroidsDriver");
1473 /**************************************************************************************************/
1475 double ShhherCommand::getDistToCentroid(int cent, int flow, int length){
1478 int flowAValue = cent * numFlowCells;
1479 int flowBValue = flow * numFlowCells;
1483 for(int i=0;i<length;i++){
1484 dist += singleLookUp[uniqueFlowgrams[flowAValue] * NUMBINS + flowDataIntI[flowBValue]];
1489 return dist / (double)length;
1491 catch(exception& e) {
1492 m->errorOut(e, "ShhherCommand", "getDistToCentroid");
1497 /**************************************************************************************************/
1499 double ShhherCommand::getNewWeights(){
1502 double maxChange = 0;
1504 for(int i=0;i<numOTUs;i++){
1506 double difference = weight[i];
1509 for(int j=0;j<nSeqsPerOTU[i];j++){
1510 int index = cumNumSeqs[i] + j;
1511 double tauValue = singleTau[seqNumber[index]];
1512 weight[i] += tauValue;
1515 difference = fabs(weight[i] - difference);
1516 if(difference > maxChange){ maxChange = difference; }
1520 catch(exception& e) {
1521 m->errorOut(e, "ShhherCommand", "getNewWeights");
1526 /**************************************************************************************************/
1528 double ShhherCommand::getLikelihood(){
1532 vector<long double> P(numSeqs, 0);
1535 for(int i=0;i<numOTUs;i++){
1536 if(weight[i] > MIN_WEIGHT){
1542 for(int i=0;i<numOTUs;i++){
1543 for(int j=0;j<nSeqsPerOTU[i];j++){
1544 int index = cumNumSeqs[i] + j;
1545 int nI = seqIndex[index];
1546 double singleDist = dist[seqNumber[index]];
1548 P[nI] += weight[i] * exp(-singleDist * sigma);
1552 for(int i=0;i<numSeqs;i++){
1553 if(P[i] == 0){ P[i] = DBL_EPSILON; }
1558 nLL = nLL -(double)numSeqs * log(sigma);
1562 catch(exception& e) {
1563 m->errorOut(e, "ShhherCommand", "getNewWeights");
1568 /**************************************************************************************************/
1570 void ShhherCommand::checkCentroids(){
1572 vector<int> unique(numOTUs, 1);
1574 for(int i=0;i<numOTUs;i++){
1575 if(centroids[i] == -1 || weight[i] < MIN_WEIGHT){
1580 for(int i=0;i<numOTUs;i++){
1582 for(int j=i+1;j<numOTUs;j++){
1585 if(centroids[j] == centroids[i]){
1589 weight[i] += weight[j];
1597 catch(exception& e) {
1598 m->errorOut(e, "ShhherCommand", "checkCentroids");
1603 /**************************************************************************************************/
1605 void ShhherCommand::calcNewDistances(){
1608 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
1610 if(processors == 1) {
1611 calcNewDistancesParent(0, numSeqs);
1613 else{ //you have multiple processors
1614 if (numSeqs < processors){ processors = 1; }
1616 vector<vector<int> > child_otuIndex(processors);
1617 vector<vector<int> > child_seqIndex(processors);
1618 vector<vector<double> > child_singleTau(processors);
1619 vector<int> totals(processors);
1622 vector<int> processIDs;
1624 //loop through and create all the processes you want
1625 while (process != processors) {
1629 processIDs.push_back(pid); //create map from line number to pid so you can append files in correct order later
1631 }else if (pid == 0){
1632 calcNewDistancesChild(nSeqsBreaks[process], nSeqsBreaks[process+1], child_otuIndex[process], child_seqIndex[process], child_singleTau[process]);
1633 totals[process] = child_otuIndex[process].size();
1637 m->mothurOut("[ERROR]: unable to spawn the necessary processes. Error code: " + toString(pid)); m->mothurOutEndLine();
1639 for (int i=0;i<processIDs.size();i++) { int temp = processIDs[i]; kill (temp, SIGINT); }
1644 //parent does its part
1645 calcNewDistancesParent(nSeqsBreaks[0], nSeqsBreaks[1]);
1646 int total = seqIndex.size();
1648 //force parent to wait until all the processes are done
1649 for (int i=0;i<processIDs.size();i++) {
1650 int temp = processIDs[i];
1654 for(int i=1;i<processors;i++){
1655 int oldTotal = total;
1658 singleTau.resize(total, 0);
1659 seqIndex.resize(total, 0);
1660 seqNumber.resize(total, 0);
1664 for(int j=oldTotal;j<total;j++){
1665 int otuI = child_otuIndex[i][childIndex];
1666 int seqI = child_seqIndex[i][childIndex];
1668 singleTau[j] = child_singleTau[i][childIndex];
1669 aaP[otuI][nSeqsPerOTU[otuI]] = j;
1670 aaI[otuI][nSeqsPerOTU[otuI]] = seqI;
1671 nSeqsPerOTU[otuI]++;
1678 calcNewDistancesParent(0, numSeqs);
1681 catch(exception& e) {
1682 m->errorOut(e, "ShhherCommand", "calcNewDistances");
1687 /**************************************************************************************************/
1689 void ShhherCommand::calcNewDistancesChildMPI(int startSeq, int stopSeq, vector<int>& otuIndex){
1692 vector<double> newTau(numOTUs,0);
1693 vector<double> norms(numSeqs, 0);
1700 for(int i=startSeq;i<stopSeq;i++){
1701 double offset = 1e8;
1702 int indexOffset = i * numOTUs;
1704 for(int j=0;j<numOTUs;j++){
1706 if(weight[j] > MIN_WEIGHT && change[j] == 1){
1707 dist[indexOffset + j] = getDistToCentroid(centroids[j], i, lengths[i]);
1709 if(weight[j] > MIN_WEIGHT && dist[indexOffset + j] < offset){
1710 offset = dist[indexOffset + j];
1714 for(int j=0;j<numOTUs;j++){
1715 if(weight[j] > MIN_WEIGHT){
1716 newTau[j] = exp(sigma * (-dist[indexOffset + j] + offset)) * weight[j];
1717 norms[i] += newTau[j];
1724 for(int j=0;j<numOTUs;j++){
1726 newTau[j] /= norms[i];
1728 if(newTau[j] > MIN_TAU){
1729 otuIndex.push_back(j);
1730 seqIndex.push_back(i);
1731 singleTau.push_back(newTau[j]);
1737 catch(exception& e) {
1738 m->errorOut(e, "ShhherCommand", "calcNewDistancesChildMPI");
1743 /**************************************************************************************************/
1745 void ShhherCommand::calcNewDistancesChild(int startSeq, int stopSeq, vector<int>& child_otuIndex, vector<int>& child_seqIndex, vector<double>& child_singleTau){
1748 vector<double> newTau(numOTUs,0);
1749 vector<double> norms(numSeqs, 0);
1750 child_otuIndex.resize(0);
1751 child_seqIndex.resize(0);
1752 child_singleTau.resize(0);
1754 for(int i=startSeq;i<stopSeq;i++){
1755 double offset = 1e8;
1756 int indexOffset = i * numOTUs;
1759 for(int j=0;j<numOTUs;j++){
1760 if(weight[j] > MIN_WEIGHT && change[j] == 1){
1761 dist[indexOffset + j] = getDistToCentroid(centroids[j], i, lengths[i]);
1764 if(weight[j] > MIN_WEIGHT && dist[indexOffset + j] < offset){
1765 offset = dist[indexOffset + j];
1769 for(int j=0;j<numOTUs;j++){
1770 if(weight[j] > MIN_WEIGHT){
1771 newTau[j] = exp(sigma * (-dist[indexOffset + j] + offset)) * weight[j];
1772 norms[i] += newTau[j];
1779 for(int j=0;j<numOTUs;j++){
1780 newTau[j] /= norms[i];
1782 if(newTau[j] > MIN_TAU){
1783 child_otuIndex.push_back(j);
1784 child_seqIndex.push_back(i);
1785 child_singleTau.push_back(newTau[j]);
1790 catch(exception& e) {
1791 m->errorOut(e, "ShhherCommand", "calcNewDistancesChild");
1796 /**************************************************************************************************/
1798 void ShhherCommand::calcNewDistancesParent(int startSeq, int stopSeq){
1803 vector<double> newTau(numOTUs,0);
1804 vector<double> norms(numSeqs, 0);
1805 nSeqsPerOTU.assign(numOTUs, 0);
1807 for(int i=startSeq;i<stopSeq;i++){
1808 int indexOffset = i * numOTUs;
1810 double offset = 1e8;
1812 for(int j=0;j<numOTUs;j++){
1813 if(weight[j] > MIN_WEIGHT && change[j] == 1){
1814 dist[indexOffset + j] = getDistToCentroid(centroids[j], i, lengths[i]);
1817 if(weight[j] > MIN_WEIGHT && dist[indexOffset + j] < offset){
1818 offset = dist[indexOffset + j];
1822 for(int j=0;j<numOTUs;j++){
1823 if(weight[j] > MIN_WEIGHT){
1824 newTau[j] = exp(sigma * (-dist[indexOffset + j] + offset)) * weight[j];
1825 norms[i] += newTau[j];
1832 for(int j=0;j<numOTUs;j++){
1833 newTau[j] /= norms[i];
1836 for(int j=0;j<numOTUs;j++){
1837 if(newTau[j] > MIN_TAU){
1839 int oldTotal = total;
1843 singleTau.resize(total, 0);
1844 seqNumber.resize(total, 0);
1845 seqIndex.resize(total, 0);
1847 singleTau[oldTotal] = newTau[j];
1849 aaP[j][nSeqsPerOTU[j]] = oldTotal;
1850 aaI[j][nSeqsPerOTU[j]] = i;
1856 catch(exception& e) {
1857 m->errorOut(e, "ShhherCommand", "calcNewDistancesParent");
1862 /**************************************************************************************************/
1864 void ShhherCommand::setOTUs(){
1867 vector<double> bigTauMatrix(numOTUs * numSeqs, 0.0000);
1869 for(int i=0;i<numOTUs;i++){
1870 for(int j=0;j<nSeqsPerOTU[i];j++){
1871 int index = cumNumSeqs[i] + j;
1872 double tauValue = singleTau[seqNumber[index]];
1873 int sIndex = seqIndex[index];
1874 bigTauMatrix[sIndex * numOTUs + i] = tauValue;
1878 for(int i=0;i<numSeqs;i++){
1879 double maxTau = -1.0000;
1881 for(int j=0;j<numOTUs;j++){
1882 if(bigTauMatrix[i * numOTUs + j] > maxTau){
1883 maxTau = bigTauMatrix[i * numOTUs + j];
1888 otuData[i] = maxOTU;
1891 nSeqsPerOTU.assign(numOTUs, 0);
1893 for(int i=0;i<numSeqs;i++){
1894 int index = otuData[i];
1896 singleTau[i] = 1.0000;
1899 aaP[index][nSeqsPerOTU[index]] = i;
1900 aaI[index][nSeqsPerOTU[index]] = i;
1902 nSeqsPerOTU[index]++;
1906 catch(exception& e) {
1907 m->errorOut(e, "ShhherCommand", "calcNewDistances");
1912 /**************************************************************************************************/
1914 void ShhherCommand::writeQualities(vector<int> otuCounts){
1917 string qualityFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.qual";
1919 ofstream qualityFile;
1920 m->openOutputFile(qualityFileName, qualityFile);
1922 qualityFile.setf(ios::fixed, ios::floatfield);
1923 qualityFile.setf(ios::showpoint);
1924 qualityFile << setprecision(6);
1926 vector<vector<int> > qualities(numOTUs);
1927 vector<double> pr(HOMOPS, 0);
1930 for(int i=0;i<numOTUs;i++){
1934 if(nSeqsPerOTU[i] > 0){
1935 qualities[i].assign(1024, -1);
1937 while(index < numFlowCells){
1938 double maxPrValue = 1e8;
1939 short maxPrIndex = -1;
1940 double count = 0.0000;
1942 pr.assign(HOMOPS, 0);
1944 for(int j=0;j<nSeqsPerOTU[i];j++){
1945 int lIndex = cumNumSeqs[i] + j;
1946 double tauValue = singleTau[seqNumber[lIndex]];
1947 int sequenceIndex = aaI[i][j];
1948 short intensity = flowDataIntI[sequenceIndex * numFlowCells + index];
1952 for(int s=0;s<HOMOPS;s++){
1953 pr[s] += tauValue * singleLookUp[s * NUMBINS + intensity];
1957 maxPrIndex = uniqueFlowgrams[centroids[i] * numFlowCells + index];
1958 maxPrValue = pr[maxPrIndex];
1960 if(count > MIN_COUNT){
1962 double norm = 0.0000;
1964 for(int s=0;s<HOMOPS;s++){
1965 norm += exp(-(pr[s] - maxPrValue));
1968 for(int s=1;s<=maxPrIndex;s++){
1970 double temp = 0.0000;
1972 U += exp(-(pr[s-1]-maxPrValue))/norm;
1980 temp = floor(-10 * temp);
1981 value = (int)floor(temp);
1982 if(value > 100){ value = 100; }
1984 qualities[i][base] = (int)value;
1994 if(otuCounts[i] > 0){
1995 qualityFile << '>' << seqNameVector[mapUniqueToSeq[i]] << endl;
1997 int j=4; //need to get past the first four bases
1998 while(qualities[i][j] != -1){
1999 qualityFile << qualities[i][j] << ' ';
2002 qualityFile << endl;
2005 qualityFile.close();
2006 outputNames.push_back(qualityFileName);
2009 catch(exception& e) {
2010 m->errorOut(e, "ShhherCommand", "writeQualities");
2015 /**************************************************************************************************/
2017 void ShhherCommand::writeSequences(vector<int> otuCounts){
2020 string fastaFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.fasta";
2022 m->openOutputFile(fastaFileName, fastaFile);
2024 vector<string> names(numOTUs, "");
2026 for(int i=0;i<numOTUs;i++){
2027 int index = centroids[i];
2029 if(otuCounts[i] > 0){
2030 fastaFile << '>' << seqNameVector[aaI[i][0]] << endl;
2034 for(int j=0;j<numFlowCells;j++){
2036 char base = flowOrder[j % 4];
2037 for(int k=0;k<uniqueFlowgrams[index * numFlowCells + j];k++){
2042 fastaFile << newSeq.substr(4) << endl;
2047 outputNames.push_back(fastaFileName);
2049 if(compositeFASTAFileName != ""){
2050 m->appendFiles(fastaFileName, compositeFASTAFileName);
2053 catch(exception& e) {
2054 m->errorOut(e, "ShhherCommand", "writeSequences");
2059 /**************************************************************************************************/
2061 void ShhherCommand::writeNames(vector<int> otuCounts){
2063 string nameFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.names";
2065 m->openOutputFile(nameFileName, nameFile);
2067 for(int i=0;i<numOTUs;i++){
2068 if(otuCounts[i] > 0){
2069 nameFile << seqNameVector[aaI[i][0]] << '\t' << seqNameVector[aaI[i][0]];
2071 for(int j=1;j<nSeqsPerOTU[i];j++){
2072 nameFile << ',' << seqNameVector[aaI[i][j]];
2079 outputNames.push_back(nameFileName);
2082 if(compositeNamesFileName != ""){
2083 m->appendFiles(nameFileName, compositeNamesFileName);
2086 catch(exception& e) {
2087 m->errorOut(e, "ShhherCommand", "writeNames");
2092 /**************************************************************************************************/
2094 void ShhherCommand::writeGroups(){
2096 string fileRoot = flowFileName.substr(0,flowFileName.find_last_of('.'));
2097 string groupFileName = fileRoot + ".shhh.groups";
2099 m->openOutputFile(groupFileName, groupFile);
2101 for(int i=0;i<numSeqs;i++){
2102 groupFile << seqNameVector[i] << '\t' << fileRoot << endl;
2105 outputNames.push_back(groupFileName);
2108 catch(exception& e) {
2109 m->errorOut(e, "ShhherCommand", "writeGroups");
2114 /**************************************************************************************************/
2116 void ShhherCommand::writeClusters(vector<int> otuCounts){
2118 string otuCountsFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.counts";
2119 ofstream otuCountsFile;
2120 m->openOutputFile(otuCountsFileName, otuCountsFile);
2122 string bases = flowOrder;
2124 for(int i=0;i<numOTUs;i++){
2125 //output the translated version of the centroid sequence for the otu
2126 if(otuCounts[i] > 0){
2127 int index = centroids[i];
2129 otuCountsFile << "ideal\t";
2130 for(int j=8;j<numFlowCells;j++){
2131 char base = bases[j % 4];
2132 for(int s=0;s<uniqueFlowgrams[index * numFlowCells + j];s++){
2133 otuCountsFile << base;
2136 otuCountsFile << endl;
2138 for(int j=0;j<nSeqsPerOTU[i];j++){
2139 int sequence = aaI[i][j];
2140 otuCountsFile << seqNameVector[sequence] << '\t';
2144 for(int k=0;k<lengths[sequence];k++){
2145 char base = bases[k % 4];
2146 int freq = int(0.01 * (double)flowDataIntI[sequence * numFlowCells + k] + 0.5);
2148 for(int s=0;s<freq;s++){
2150 //otuCountsFile << base;
2153 otuCountsFile << newSeq.substr(4) << endl;
2155 otuCountsFile << endl;
2158 otuCountsFile.close();
2159 outputNames.push_back(otuCountsFileName);
2162 catch(exception& e) {
2163 m->errorOut(e, "ShhherCommand", "writeClusters");
2168 //**********************************************************************************************************************