5 * Created by Pat Schloss on 12/27/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "shhhercommand.h"
12 //**********************************************************************************************************************
13 vector<string> ShhherCommand::setParameters(){
15 CommandParameter pflow("flow", "InputTypes", "", "", "none", "fileflow", "none",false,false); parameters.push_back(pflow);
16 CommandParameter pfile("file", "InputTypes", "", "", "none", "fileflow", "none",false,false); parameters.push_back(pfile);
17 CommandParameter plookup("lookup", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(plookup);
18 CommandParameter pcutoff("cutoff", "Number", "", "0.01", "", "", "",false,false); parameters.push_back(pcutoff);
19 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
20 CommandParameter pmaxiter("maxiter", "Number", "", "1000", "", "", "",false,false); parameters.push_back(pmaxiter);
21 CommandParameter plarge("large", "Number", "", "-1", "", "", "",false,false); parameters.push_back(plarge);
22 CommandParameter psigma("sigma", "Number", "", "60", "", "", "",false,false); parameters.push_back(psigma);
23 CommandParameter pmindelta("mindelta", "Number", "", "0.000001", "", "", "",false,false); parameters.push_back(pmindelta);
24 CommandParameter porder("order", "String", "", "", "", "", "",false,false); parameters.push_back(porder);
25 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
26 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
28 vector<string> myArray;
29 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
33 m->errorOut(e, "ShhherCommand", "setParameters");
37 //**********************************************************************************************************************
38 string ShhherCommand::getHelpString(){
40 string helpString = "";
41 helpString += "The shhh.flows command reads a file containing flowgrams and creates a file of corrected sequences.\n";
45 m->errorOut(e, "ShhherCommand", "getHelpString");
49 //**********************************************************************************************************************
50 string ShhherCommand::getOutputFileNameTag(string type, string inputName=""){
52 string outputFileName = "";
53 map<string, vector<string> >::iterator it;
55 //is this a type this command creates
56 it = outputTypes.find(type);
57 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
59 if (type == "fasta") { outputFileName = "shhh.fasta"; }
60 else if (type == "name") { outputFileName = "shhh.names"; }
61 else if (type == "group") { outputFileName = "shhh.groups"; }
62 else if (type == "counts") { outputFileName = "shhh.counts"; }
63 else if (type == "qfile") { outputFileName = "shhh.qual"; }
64 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
66 return outputFileName;
69 m->errorOut(e, "ShhherCommand", "getOutputFileNameTag");
73 //**********************************************************************************************************************
75 ShhherCommand::ShhherCommand(){
77 abort = true; calledHelp = true;
80 //initialize outputTypes
81 vector<string> tempOutNames;
82 outputTypes["fasta"] = tempOutNames;
83 outputTypes["name"] = tempOutNames;
84 outputTypes["group"] = tempOutNames;
85 outputTypes["counts"] = tempOutNames;
86 outputTypes["qfile"] = tempOutNames;
90 m->errorOut(e, "ShhherCommand", "ShhherCommand");
95 //**********************************************************************************************************************
97 ShhherCommand::ShhherCommand(string option) {
101 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
102 MPI_Comm_size(MPI_COMM_WORLD, &ncpus);
106 abort = false; calledHelp = false;
108 //allow user to run help
109 if(option == "help") { help(); abort = true; calledHelp = true; }
110 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
113 vector<string> myArray = setParameters();
115 OptionParser parser(option);
116 map<string,string> parameters = parser.getParameters();
118 ValidParameters validParameter;
119 map<string,string>::iterator it;
121 //check to make sure all parameters are valid for command
122 for (it = parameters.begin(); it != parameters.end(); it++) {
123 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
126 //initialize outputTypes
127 vector<string> tempOutNames;
128 outputTypes["fasta"] = tempOutNames;
129 outputTypes["name"] = tempOutNames;
130 outputTypes["group"] = tempOutNames;
131 outputTypes["counts"] = tempOutNames;
132 outputTypes["qfile"] = 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; }
165 //if the user changes the output directory command factory will send this info to us in the output parameter
166 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
168 //check for required parameters
169 flowFileName = validParameter.validFile(parameters, "flow", true);
170 flowFilesFileName = validParameter.validFile(parameters, "file", true);
171 if (flowFileName == "not found" && flowFilesFileName == "not found") {
172 m->mothurOut("values for either flow or file must be provided for the shhh.flows command.");
173 m->mothurOutEndLine();
176 else if (flowFileName == "not open" || flowFilesFileName == "not open") { abort = true; }
178 if(flowFileName != "not found"){
179 compositeFASTAFileName = "";
180 compositeNamesFileName = "";
185 string thisoutputDir = outputDir;
186 if (outputDir == "") { thisoutputDir = m->hasPath(flowFilesFileName); } //if user entered a file with a path then preserve it
188 //we want to rip off .files, and also .flow if its there
189 string fileroot = m->getRootName(m->getSimpleName(flowFilesFileName));
190 if (fileroot[fileroot.length()-1] == '.') { fileroot = fileroot.substr(0, fileroot.length()-1); } //rip off dot
191 string extension = m->getExtension(fileroot);
192 if (extension == ".flow") { fileroot = m->getRootName(fileroot); }
193 else { fileroot += "."; } //add back if needed
195 compositeFASTAFileName = thisoutputDir + fileroot + "shhh.fasta";
196 m->openOutputFile(compositeFASTAFileName, temp);
199 compositeNamesFileName = thisoutputDir + fileroot + "shhh.names";
200 m->openOutputFile(compositeNamesFileName, temp);
204 if(flowFilesFileName != "not found"){
207 ifstream flowFilesFile;
208 m->openInputFile(flowFilesFileName, flowFilesFile);
209 while(flowFilesFile){
210 fName = m->getline(flowFilesFile);
212 //test if file is valid
214 int ableToOpen = m->openInputFile(fName, in, "noerror");
216 if (ableToOpen == 1) {
217 if (inputDir != "") { //default path is set
218 string tryPath = inputDir + fName;
219 m->mothurOut("Unable to open " + fName + ". Trying input directory " + tryPath); m->mothurOutEndLine();
221 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
227 if (ableToOpen == 1) {
228 if (m->getDefaultPath() != "") { //default path is set
229 string tryPath = m->getDefaultPath() + m->getSimpleName(fName);
230 m->mothurOut("Unable to open " + fName + ". Trying default " + tryPath); m->mothurOutEndLine();
232 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
238 //if you can't open it its not in current working directory or inputDir, try mothur excutable location
239 if (ableToOpen == 1) {
240 string exepath = m->argv;
241 string tempPath = exepath;
242 for (int i = 0; i < exepath.length(); i++) { tempPath[i] = tolower(exepath[i]); }
243 exepath = exepath.substr(0, (tempPath.find_last_of('m')));
245 string tryPath = m->getFullPathName(exepath) + m->getSimpleName(fName);
246 m->mothurOut("Unable to open " + fName + ". Trying mothur's executable location " + tryPath); m->mothurOutEndLine();
248 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
253 if (ableToOpen == 1) { m->mothurOut("Unable to open " + fName + ". Disregarding. "); m->mothurOutEndLine(); }
254 else { flowFileVector.push_back(fName); }
255 m->gobble(flowFilesFile);
257 flowFilesFile.close();
258 if (flowFileVector.size() == 0) { m->mothurOut("[ERROR]: no valid files."); m->mothurOutEndLine(); abort = true; }
261 if (outputDir == "") { outputDir = m->hasPath(flowFileName); }
262 flowFileVector.push_back(flowFileName);
265 //check for optional parameter and set defaults
266 // ...at some point should added some additional type checking...
268 temp = validParameter.validFile(parameters, "lookup", true);
269 if (temp == "not found") {
270 lookupFileName = "LookUp_Titanium.pat";
274 ableToOpen = m->openInputFile(lookupFileName, in, "noerror");
277 //if you can't open it, try input location
278 if (ableToOpen == 1) {
279 if (inputDir != "") { //default path is set
280 string tryPath = inputDir + lookupFileName;
281 m->mothurOut("Unable to open " + lookupFileName + ". Trying input directory " + tryPath); m->mothurOutEndLine();
283 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
285 lookupFileName = tryPath;
289 //if you can't open it, try default location
290 if (ableToOpen == 1) {
291 if (m->getDefaultPath() != "") { //default path is set
292 string tryPath = m->getDefaultPath() + m->getSimpleName(lookupFileName);
293 m->mothurOut("Unable to open " + lookupFileName + ". Trying default " + tryPath); m->mothurOutEndLine();
295 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
297 lookupFileName = tryPath;
301 //if you can't open it its not in current working directory or inputDir, try mothur excutable location
302 if (ableToOpen == 1) {
303 string exepath = m->argv;
304 string tempPath = exepath;
305 for (int i = 0; i < exepath.length(); i++) { tempPath[i] = tolower(exepath[i]); }
306 exepath = exepath.substr(0, (tempPath.find_last_of('m')));
308 string tryPath = m->getFullPathName(exepath) + m->getSimpleName(lookupFileName);
309 m->mothurOut("Unable to open " + lookupFileName + ". Trying mothur's executable location " + tryPath); m->mothurOutEndLine();
311 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
313 lookupFileName = tryPath;
316 if (ableToOpen == 1) { m->mothurOut("Unable to open " + lookupFileName + "."); m->mothurOutEndLine(); abort=true; }
318 else if(temp == "not open") {
320 lookupFileName = validParameter.validFile(parameters, "lookup", false);
322 //if you can't open it its not inputDir, try mothur excutable location
323 string exepath = m->argv;
324 string tempPath = exepath;
325 for (int i = 0; i < exepath.length(); i++) { tempPath[i] = tolower(exepath[i]); }
326 exepath = exepath.substr(0, (tempPath.find_last_of('m')));
328 string tryPath = m->getFullPathName(exepath) + lookupFileName;
329 m->mothurOut("Unable to open " + lookupFileName + ". Trying mothur's executable location " + tryPath); m->mothurOutEndLine();
331 int ableToOpen = m->openInputFile(tryPath, in2, "noerror");
333 lookupFileName = tryPath;
335 if (ableToOpen == 1) { m->mothurOut("Unable to open " + lookupFileName + "."); m->mothurOutEndLine(); abort=true; }
336 }else { lookupFileName = temp; }
338 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
339 m->setProcessors(temp);
340 m->mothurConvert(temp, processors);
342 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found"){ temp = "0.01"; }
343 m->mothurConvert(temp, cutoff);
345 temp = validParameter.validFile(parameters, "mindelta", false); if (temp == "not found"){ temp = "0.000001"; }
346 m->mothurConvert(temp, minDelta);
348 temp = validParameter.validFile(parameters, "maxiter", false); if (temp == "not found"){ temp = "1000"; }
349 m->mothurConvert(temp, maxIters);
351 temp = validParameter.validFile(parameters, "large", false); if (temp == "not found"){ temp = "0"; }
352 m->mothurConvert(temp, largeSize);
353 if (largeSize != 0) { large = true; }
354 else { large = false; }
355 if (largeSize < 0) { m->mothurOut("The value of the large cannot be negative.\n"); }
358 if (large) { m->mothurOut("The large parameter is not available with the MPI-Enabled version.\n"); large=false; }
362 temp = validParameter.validFile(parameters, "sigma", false);if (temp == "not found") { temp = "60"; }
363 m->mothurConvert(temp, sigma);
365 flowOrder = validParameter.validFile(parameters, "order", false);
366 if (flowOrder == "not found"){ flowOrder = "TACG"; }
367 else if(flowOrder.length() != 4){
368 m->mothurOut("The value of the order option must be four bases long\n");
376 catch(exception& e) {
377 m->errorOut(e, "ShhherCommand", "ShhherCommand");
381 //**********************************************************************************************************************
383 int ShhherCommand::execute(){
385 if (abort == true) { if (calledHelp) { return 0; } return 2; }
392 for(int i=1;i<ncpus;i++){
393 MPI_Send(&abort, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
395 if(abort == 1){ return 0; }
399 m->mothurOut("\nGetting preliminary data...\n");
400 getSingleLookUp(); if (m->control_pressed) { return 0; }
401 getJointLookUp(); if (m->control_pressed) { return 0; }
403 vector<string> flowFileVector;
404 if(flowFilesFileName != "not found"){
407 ifstream flowFilesFile;
408 m->openInputFile(flowFilesFileName, flowFilesFile);
409 while(flowFilesFile){
410 fName = m->getline(flowFilesFile);
411 flowFileVector.push_back(fName);
412 m->gobble(flowFilesFile);
416 flowFileVector.push_back(flowFileName);
419 int numFiles = flowFileVector.size();
421 for(int i=1;i<ncpus;i++){
422 MPI_Send(&numFiles, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
425 for(int i=0;i<numFiles;i++){
427 if (m->control_pressed) { break; }
429 double begClock = clock();
430 unsigned long long begTime = time(NULL);
432 flowFileName = flowFileVector[i];
434 m->mothurOut("\n>>>>>\tProcessing " + flowFileName + " (file " + toString(i+1) + " of " + toString(numFiles) + ")\t<<<<<\n");
435 m->mothurOut("Reading flowgrams...\n");
438 if (m->control_pressed) { break; }
440 m->mothurOut("Identifying unique flowgrams...\n");
443 if (m->control_pressed) { break; }
445 m->mothurOut("Calculating distances between flowgrams...\n");
447 strcpy(fileName, flowFileName.c_str());
449 for(int i=1;i<ncpus;i++){
450 MPI_Send(&fileName[0], 1024, MPI_CHAR, i, tag, MPI_COMM_WORLD);
452 MPI_Send(&numSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
453 MPI_Send(&numUniques, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
454 MPI_Send(&numFlowCells, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
455 MPI_Send(&flowDataIntI[0], numSeqs * numFlowCells, MPI_SHORT, i, tag, MPI_COMM_WORLD);
456 MPI_Send(&flowDataPrI[0], numSeqs * numFlowCells, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
457 MPI_Send(&mapUniqueToSeq[0], numSeqs, MPI_INT, i, tag, MPI_COMM_WORLD);
458 MPI_Send(&mapSeqToUnique[0], numSeqs, MPI_INT, i, tag, MPI_COMM_WORLD);
459 MPI_Send(&lengths[0], numSeqs, MPI_INT, i, tag, MPI_COMM_WORLD);
460 MPI_Send(&jointLookUp[0], NUMBINS * NUMBINS, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
461 MPI_Send(&cutoff, 1, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
464 string distFileName = flowDistMPI(0, int(sqrt(1.0/float(ncpus)) * numUniques));
466 if (m->control_pressed) { break; }
469 for(int i=1;i<ncpus;i++){
470 MPI_Recv(&done, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
472 m->appendFiles((distFileName + ".temp." + toString(i)), distFileName);
473 m->mothurRemove((distFileName + ".temp." + toString(i)));
476 string namesFileName = createNamesFile();
478 if (m->control_pressed) { break; }
480 m->mothurOut("\nClustering flowgrams...\n");
481 string listFileName = cluster(distFileName, namesFileName);
483 if (m->control_pressed) { break; }
487 getOTUData(listFileName);
489 m->mothurRemove(distFileName);
490 m->mothurRemove(namesFileName);
491 m->mothurRemove(listFileName);
493 if (m->control_pressed) { break; }
497 if (m->control_pressed) { break; }
500 for(int i=1;i<ncpus;i++){
501 MPI_Send(&numOTUs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
502 MPI_Send(&singleLookUp[0], singleLookUp.size(), MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
503 MPI_Send(&uniqueFlowgrams[0], numFlowCells * numUniques, MPI_SHORT, i, tag, MPI_COMM_WORLD);
504 MPI_Send(&sigma, 1, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
507 if (m->control_pressed) { break; }
512 int numOTUsOnCPU = numOTUs / ncpus;
513 int numSeqsOnCPU = numSeqs / ncpus;
514 m->mothurOut("\nDenoising flowgrams...\n");
515 m->mothurOut("iter\tmaxDelta\tnLL\t\tcycletime\n");
517 while((maxIters == 0 && maxDelta > minDelta) || iter < MIN_ITER || (maxDelta > minDelta && iter < maxIters)){
519 double cycClock = clock();
520 unsigned long long cycTime = time(NULL);
523 if (m->control_pressed) { break; }
525 int total = singleTau.size();
526 for(int i=1;i<ncpus;i++){
527 MPI_Send(&total, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
528 MPI_Send(&change[0], numOTUs, MPI_SHORT, i, tag, MPI_COMM_WORLD);
529 MPI_Send(¢roids[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
531 MPI_Send(&singleTau[0], total, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
532 MPI_Send(&seqNumber[0], total, MPI_INT, i, tag, MPI_COMM_WORLD);
533 MPI_Send(&seqIndex[0], total, MPI_INT, i, tag, MPI_COMM_WORLD);
534 MPI_Send(&nSeqsPerOTU[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
535 MPI_Send(&cumNumSeqs[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
538 calcCentroidsDriver(0, numOTUsOnCPU);
540 for(int i=1;i<ncpus;i++){
541 int otuStart = i * numOTUs / ncpus;
542 int otuStop = (i + 1) * numOTUs / ncpus;
544 vector<int> tempCentroids(numOTUs, 0);
545 vector<short> tempChange(numOTUs, 0);
547 MPI_Recv(&tempCentroids[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
548 MPI_Recv(&tempChange[0], numOTUs, MPI_SHORT, i, tag, MPI_COMM_WORLD, &status);
550 for(int j=otuStart;j<otuStop;j++){
551 centroids[j] = tempCentroids[j];
552 change[j] = tempChange[j];
556 maxDelta = getNewWeights(); if (m->control_pressed) { break; }
557 double nLL = getLikelihood(); if (m->control_pressed) { break; }
558 checkCentroids(); if (m->control_pressed) { break; }
560 for(int i=1;i<ncpus;i++){
561 MPI_Send(¢roids[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
562 MPI_Send(&weight[0], numOTUs, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
563 MPI_Send(&change[0], numOTUs, MPI_SHORT, i, tag, MPI_COMM_WORLD);
566 calcNewDistancesParent(0, numSeqsOnCPU);
568 total = singleTau.size();
570 for(int i=1;i<ncpus;i++){
572 int seqStart = i * numSeqs / ncpus;
573 int seqStop = (i + 1) * numSeqs / ncpus;
575 MPI_Recv(&childTotal, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
577 vector<int> childSeqIndex(childTotal, 0);
578 vector<double> childSingleTau(childTotal, 0);
579 vector<double> childDist(numSeqs * numOTUs, 0);
580 vector<int> otuIndex(childTotal, 0);
582 MPI_Recv(&childSeqIndex[0], childTotal, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
583 MPI_Recv(&childSingleTau[0], childTotal, MPI_DOUBLE, i, tag, MPI_COMM_WORLD, &status);
584 MPI_Recv(&childDist[0], numOTUs * numSeqs, MPI_DOUBLE, i, tag, MPI_COMM_WORLD, &status);
585 MPI_Recv(&otuIndex[0], childTotal, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
587 int oldTotal = total;
589 singleTau.resize(total, 0);
590 seqIndex.resize(total, 0);
591 seqNumber.resize(total, 0);
595 for(int j=oldTotal;j<total;j++){
596 int otuI = otuIndex[childIndex];
597 int seqI = childSeqIndex[childIndex];
599 singleTau[j] = childSingleTau[childIndex];
601 aaP[otuI][nSeqsPerOTU[otuI]] = j;
602 aaI[otuI][nSeqsPerOTU[otuI]] = seqI;
607 int index = seqStart * numOTUs;
608 for(int j=seqStart;j<seqStop;j++){
609 for(int k=0;k<numOTUs;k++){
610 dist[index] = childDist[index];
618 m->mothurOut(toString(iter) + '\t' + toString(maxDelta) + '\t' + toString(nLL) + '\t' + toString(time(NULL) - cycTime) + '\t' + toString((clock() - cycClock)/(double)CLOCKS_PER_SEC) + '\n');
620 if((maxIters == 0 && maxDelta > minDelta) || iter < MIN_ITER || (maxDelta > minDelta && iter < maxIters)){
622 for(int i=1;i<ncpus;i++){
623 MPI_Send(&live, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
628 for(int i=1;i<ncpus;i++){
629 MPI_Send(&live, 1, MPI_INT, i, tag, MPI_COMM_WORLD); //send kill command
635 if (m->control_pressed) { break; }
637 m->mothurOut("\nFinalizing...\n");
640 if (m->control_pressed) { break; }
644 vector<int> otuCounts(numOTUs, 0);
645 for(int i=0;i<numSeqs;i++) { otuCounts[otuData[i]]++; }
646 calcCentroidsDriver(0, numOTUs);
648 if (m->control_pressed) { break; }
650 writeQualities(otuCounts); if (m->control_pressed) { break; }
651 writeSequences(otuCounts); if (m->control_pressed) { break; }
652 writeNames(otuCounts); if (m->control_pressed) { break; }
653 writeClusters(otuCounts); if (m->control_pressed) { break; }
654 writeGroups(); if (m->control_pressed) { break; }
657 m->mothurOut("Total time to process " + toString(flowFileName) + ":\t" + toString(time(NULL) - begTime) + '\t' + toString((clock() - begClock)/(double)CLOCKS_PER_SEC) + '\n');
663 MPI_Recv(&abort, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
664 if(abort){ return 0; }
667 MPI_Recv(&numFiles, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
669 for(int i=0;i<numFiles;i++){
671 if (m->control_pressed) { break; }
673 //Now into the pyrodist part
677 MPI_Recv(&fileName, 1024, MPI_CHAR, 0, tag, MPI_COMM_WORLD, &status);
678 MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
679 MPI_Recv(&numUniques, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
680 MPI_Recv(&numFlowCells, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
682 flowDataIntI.resize(numSeqs * numFlowCells);
683 flowDataPrI.resize(numSeqs * numFlowCells);
684 mapUniqueToSeq.resize(numSeqs);
685 mapSeqToUnique.resize(numSeqs);
686 lengths.resize(numSeqs);
687 jointLookUp.resize(NUMBINS * NUMBINS);
689 MPI_Recv(&flowDataIntI[0], numSeqs * numFlowCells, MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
690 MPI_Recv(&flowDataPrI[0], numSeqs * numFlowCells, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
691 MPI_Recv(&mapUniqueToSeq[0], numSeqs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
692 MPI_Recv(&mapSeqToUnique[0], numSeqs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
693 MPI_Recv(&lengths[0], numSeqs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
694 MPI_Recv(&jointLookUp[0], NUMBINS * NUMBINS, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
695 MPI_Recv(&cutoff, 1, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
697 flowFileName = string(fileName);
698 int flowDistStart = int(sqrt(float(pid)/float(ncpus)) * numUniques);
699 int flowDistEnd = int(sqrt(float(pid+1)/float(ncpus)) * numUniques);
701 string distanceStringChild = flowDistMPI(flowDistStart, flowDistEnd);
703 if (m->control_pressed) { break; }
706 MPI_Send(&done, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
708 //Now into the pyronoise part
709 MPI_Recv(&numOTUs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
711 singleLookUp.resize(HOMOPS * NUMBINS);
712 uniqueFlowgrams.resize(numUniques * numFlowCells);
713 weight.resize(numOTUs);
714 centroids.resize(numOTUs);
715 change.resize(numOTUs);
716 dist.assign(numOTUs * numSeqs, 0);
717 nSeqsPerOTU.resize(numOTUs);
718 cumNumSeqs.resize(numOTUs);
720 MPI_Recv(&singleLookUp[0], singleLookUp.size(), MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
721 MPI_Recv(&uniqueFlowgrams[0], uniqueFlowgrams.size(), MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
722 MPI_Recv(&sigma, 1, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
724 int startOTU = pid * numOTUs / ncpus;
725 int endOTU = (pid + 1) * numOTUs / ncpus;
727 int startSeq = pid * numSeqs / ncpus;
728 int endSeq = (pid + 1) * numSeqs /ncpus;
734 if (m->control_pressed) { break; }
736 MPI_Recv(&total, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
737 singleTau.assign(total, 0.0000);
738 seqNumber.assign(total, 0);
739 seqIndex.assign(total, 0);
741 MPI_Recv(&change[0], numOTUs, MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
742 MPI_Recv(¢roids[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
743 MPI_Recv(&singleTau[0], total, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
744 MPI_Recv(&seqNumber[0], total, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
745 MPI_Recv(&seqIndex[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
746 MPI_Recv(&nSeqsPerOTU[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
747 MPI_Recv(&cumNumSeqs[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
749 calcCentroidsDriver(startOTU, endOTU);
751 MPI_Send(¢roids[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD);
752 MPI_Send(&change[0], numOTUs, MPI_SHORT, 0, tag, MPI_COMM_WORLD);
754 MPI_Recv(¢roids[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
755 MPI_Recv(&weight[0], numOTUs, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
756 MPI_Recv(&change[0], numOTUs, MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
758 vector<int> otuIndex(total, 0);
759 calcNewDistancesChildMPI(startSeq, endSeq, otuIndex);
760 total = otuIndex.size();
762 MPI_Send(&total, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
763 MPI_Send(&seqIndex[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD);
764 MPI_Send(&singleTau[0], total, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD);
765 MPI_Send(&dist[0], numOTUs * numSeqs, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD);
766 MPI_Send(&otuIndex[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD);
768 MPI_Recv(&live, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
773 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
775 MPI_Barrier(MPI_COMM_WORLD);
778 if(compositeFASTAFileName != ""){
779 outputNames.push_back(compositeFASTAFileName);
780 outputNames.push_back(compositeNamesFileName);
783 m->mothurOutEndLine();
784 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
785 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
786 m->mothurOutEndLine();
791 catch(exception& e) {
792 m->errorOut(e, "ShhherCommand", "execute");
796 /**************************************************************************************************/
797 string ShhherCommand::createNamesFile(){
800 vector<string> duplicateNames(numUniques, "");
801 for(int i=0;i<numSeqs;i++){
802 duplicateNames[mapSeqToUnique[i]] += seqNameVector[i] + ',';
805 string nameFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName) + getOutputFileNameTag("name");
808 m->openOutputFile(nameFileName, nameFile);
810 for(int i=0;i<numUniques;i++){
812 if (m->control_pressed) { break; }
814 // nameFile << seqNameVector[mapUniqueToSeq[i]] << '\t' << duplicateNames[i].substr(0, duplicateNames[i].find_last_of(',')) << endl;
815 nameFile << mapUniqueToSeq[i] << '\t' << duplicateNames[i].substr(0, duplicateNames[i].find_last_of(',')) << endl;
821 catch(exception& e) {
822 m->errorOut(e, "ShhherCommand", "createNamesFile");
826 /**************************************************************************************************/
828 string ShhherCommand::flowDistMPI(int startSeq, int stopSeq){
830 ostringstream outStream;
831 outStream.setf(ios::fixed, ios::floatfield);
832 outStream.setf(ios::dec, ios::basefield);
833 outStream.setf(ios::showpoint);
834 outStream.precision(6);
836 int begTime = time(NULL);
837 double begClock = clock();
839 for(int i=startSeq;i<stopSeq;i++){
841 if (m->control_pressed) { break; }
843 for(int j=0;j<i;j++){
844 float flowDistance = calcPairwiseDist(mapUniqueToSeq[i], mapUniqueToSeq[j]);
846 if(flowDistance < 1e-6){
847 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << 0.000000 << endl;
849 else if(flowDistance <= cutoff){
850 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << flowDistance << endl;
854 m->mothurOut(toString(i) + '\t' + toString(time(NULL) - begTime) + '\t' + toString((clock()-begClock)/CLOCKS_PER_SEC) + '\n');
858 string fDistFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.dist";
859 if(pid != 0){ fDistFileName += ".temp." + toString(pid); }
861 if (m->control_pressed) { return fDistFileName; }
863 m->mothurOut(toString(stopSeq) + '\t' + toString(time(NULL) - begTime) + '\t' + toString((clock()-begClock)/CLOCKS_PER_SEC) + '\n');
865 ofstream distFile(fDistFileName.c_str());
866 distFile << outStream.str();
869 return fDistFileName;
871 catch(exception& e) {
872 m->errorOut(e, "ShhherCommand", "flowDistMPI");
876 /**************************************************************************************************/
878 void ShhherCommand::getOTUData(string listFileName){
882 m->openInputFile(listFileName, listFile);
885 listFile >> label >> numOTUs;
887 otuData.assign(numSeqs, 0);
888 cumNumSeqs.assign(numOTUs, 0);
889 nSeqsPerOTU.assign(numOTUs, 0);
890 aaP.clear();aaP.resize(numOTUs);
896 string singleOTU = "";
898 for(int i=0;i<numOTUs;i++){
900 if (m->control_pressed) { break; }
902 listFile >> singleOTU;
904 istringstream otuString(singleOTU);
910 for(int j=0;j<singleOTU.length();j++){
911 char letter = otuString.get();
917 map<string,int>::iterator nmIt = nameMap.find(seqName);
918 int index = nmIt->second;
924 aaP[i].push_back(index);
929 map<string,int>::iterator nmIt = nameMap.find(seqName);
931 int index = nmIt->second;
936 aaP[i].push_back(index);
941 sort(aaP[i].begin(), aaP[i].end());
942 for(int j=0;j<nSeqsPerOTU[i];j++){
943 seqNumber.push_back(aaP[i][j]);
945 for(int j=nSeqsPerOTU[i];j<numSeqs;j++){
952 for(int i=1;i<numOTUs;i++){
953 cumNumSeqs[i] = cumNumSeqs[i-1] + nSeqsPerOTU[i-1];
956 seqIndex = seqNumber;
961 catch(exception& e) {
962 m->errorOut(e, "ShhherCommand", "getOTUData");
967 /**************************************************************************************************/
969 void ShhherCommand::initPyroCluster(){
971 if (numOTUs < processors) { processors = 1; }
973 if (m->debug) { m->mothurOut("[DEBUG]: numSeqs = " + toString(numSeqs) + " numOTUS = " + toString(numOTUs) + " about to alloc a dist vector with size = " + toString((numSeqs * numOTUs)) + ".\n"); }
975 dist.assign(numSeqs * numOTUs, 0);
976 change.assign(numOTUs, 1);
977 centroids.assign(numOTUs, -1);
978 weight.assign(numOTUs, 0);
979 singleTau.assign(numSeqs, 1.0);
981 nSeqsBreaks.assign(processors+1, 0);
982 nOTUsBreaks.assign(processors+1, 0);
984 if (m->debug) { m->mothurOut("[DEBUG]: made it through the memory allocation.\n"); }
987 for(int i=0;i<processors;i++){
988 nSeqsBreaks[i+1] = nSeqsBreaks[i] + (int)((double) numSeqs / (double) processors);
989 nOTUsBreaks[i+1] = nOTUsBreaks[i] + (int)((double) numOTUs / (double) processors);
991 nSeqsBreaks[processors] = numSeqs;
992 nOTUsBreaks[processors] = numOTUs;
994 catch(exception& e) {
995 m->errorOut(e, "ShhherCommand", "initPyroCluster");
1000 /**************************************************************************************************/
1002 void ShhherCommand::fill(){
1005 for(int i=0;i<numOTUs;i++){
1007 if (m->control_pressed) { break; }
1009 cumNumSeqs[i] = index;
1010 for(int j=0;j<nSeqsPerOTU[i];j++){
1011 seqNumber[index] = aaP[i][j];
1012 seqIndex[index] = aaI[i][j];
1018 catch(exception& e) {
1019 m->errorOut(e, "ShhherCommand", "fill");
1024 /**************************************************************************************************/
1026 void ShhherCommand::getFlowData(){
1029 m->openInputFile(flowFileName, flowFile);
1032 seqNameVector.clear();
1034 flowDataIntI.clear();
1038 int currentNumFlowCells;
1042 flowFile >> numFlowCells;
1043 int index = 0;//pcluster
1044 while(!flowFile.eof()){
1046 if (m->control_pressed) { break; }
1048 flowFile >> seqName >> currentNumFlowCells;
1049 lengths.push_back(currentNumFlowCells);
1051 seqNameVector.push_back(seqName);
1052 nameMap[seqName] = index++;//pcluster
1054 for(int i=0;i<numFlowCells;i++){
1055 flowFile >> intensity;
1056 if(intensity > 9.99) { intensity = 9.99; }
1057 int intI = int(100 * intensity + 0.0001);
1058 flowDataIntI.push_back(intI);
1060 m->gobble(flowFile);
1064 numSeqs = seqNameVector.size();
1066 for(int i=0;i<numSeqs;i++){
1068 if (m->control_pressed) { break; }
1070 int iNumFlowCells = i * numFlowCells;
1071 for(int j=lengths[i];j<numFlowCells;j++){
1072 flowDataIntI[iNumFlowCells + j] = 0;
1077 catch(exception& e) {
1078 m->errorOut(e, "ShhherCommand", "getFlowData");
1082 /**************************************************************************************************/
1083 void ShhherCommand::calcNewDistancesChildMPI(int startSeq, int stopSeq, vector<int>& otuIndex){
1086 vector<double> newTau(numOTUs,0);
1087 vector<double> norms(numSeqs, 0);
1092 for(int i=startSeq;i<stopSeq;i++){
1094 if (m->control_pressed) { break; }
1096 double offset = 1e8;
1097 int indexOffset = i * numOTUs;
1099 for(int j=0;j<numOTUs;j++){
1101 if(weight[j] > MIN_WEIGHT && change[j] == 1){
1102 dist[indexOffset + j] = getDistToCentroid(centroids[j], i, lengths[i]);
1104 if(weight[j] > MIN_WEIGHT && dist[indexOffset + j] < offset){
1105 offset = dist[indexOffset + j];
1109 for(int j=0;j<numOTUs;j++){
1110 if(weight[j] > MIN_WEIGHT){
1111 newTau[j] = exp(sigma * (-dist[indexOffset + j] + offset)) * weight[j];
1112 norms[i] += newTau[j];
1119 for(int j=0;j<numOTUs;j++){
1121 newTau[j] /= norms[i];
1123 if(newTau[j] > MIN_TAU){
1124 otuIndex.push_back(j);
1125 seqIndex.push_back(i);
1126 singleTau.push_back(newTau[j]);
1132 catch(exception& e) {
1133 m->errorOut(e, "ShhherCommand", "calcNewDistancesChildMPI");
1138 /**************************************************************************************************/
1140 void ShhherCommand::calcNewDistancesParent(int startSeq, int stopSeq){
1145 vector<double> newTau(numOTUs,0);
1146 vector<double> norms(numSeqs, 0);
1147 nSeqsPerOTU.assign(numOTUs, 0);
1149 for(int i=startSeq;i<stopSeq;i++){
1151 if (m->control_pressed) { break; }
1153 int indexOffset = i * numOTUs;
1155 double offset = 1e8;
1157 for(int j=0;j<numOTUs;j++){
1159 if(weight[j] > MIN_WEIGHT && change[j] == 1){
1160 dist[indexOffset + j] = getDistToCentroid(centroids[j], i, lengths[i]);
1163 if(weight[j] > MIN_WEIGHT && dist[indexOffset + j] < offset){
1164 offset = dist[indexOffset + j];
1168 for(int j=0;j<numOTUs;j++){
1169 if(weight[j] > MIN_WEIGHT){
1170 newTau[j] = exp(sigma * (-dist[indexOffset + j] + offset)) * weight[j];
1171 norms[i] += newTau[j];
1178 for(int j=0;j<numOTUs;j++){
1179 newTau[j] /= norms[i];
1182 for(int j=0;j<numOTUs;j++){
1183 if(newTau[j] > MIN_TAU){
1185 int oldTotal = total;
1189 singleTau.resize(total, 0);
1190 seqNumber.resize(total, 0);
1191 seqIndex.resize(total, 0);
1193 singleTau[oldTotal] = newTau[j];
1195 aaP[j][nSeqsPerOTU[j]] = oldTotal;
1196 aaI[j][nSeqsPerOTU[j]] = i;
1204 catch(exception& e) {
1205 m->errorOut(e, "ShhherCommand", "calcNewDistancesParent");
1210 /**************************************************************************************************/
1212 void ShhherCommand::setOTUs(){
1215 vector<double> bigTauMatrix(numOTUs * numSeqs, 0.0000);
1217 for(int i=0;i<numOTUs;i++){
1219 if (m->control_pressed) { break; }
1221 for(int j=0;j<nSeqsPerOTU[i];j++){
1222 int index = cumNumSeqs[i] + j;
1223 double tauValue = singleTau[seqNumber[index]];
1224 int sIndex = seqIndex[index];
1225 bigTauMatrix[sIndex * numOTUs + i] = tauValue;
1229 for(int i=0;i<numSeqs;i++){
1230 double maxTau = -1.0000;
1232 for(int j=0;j<numOTUs;j++){
1233 if(bigTauMatrix[i * numOTUs + j] > maxTau){
1234 maxTau = bigTauMatrix[i * numOTUs + j];
1239 otuData[i] = maxOTU;
1242 nSeqsPerOTU.assign(numOTUs, 0);
1244 for(int i=0;i<numSeqs;i++){
1245 int index = otuData[i];
1247 singleTau[i] = 1.0000;
1250 aaP[index][nSeqsPerOTU[index]] = i;
1251 aaI[index][nSeqsPerOTU[index]] = i;
1253 nSeqsPerOTU[index]++;
1257 catch(exception& e) {
1258 m->errorOut(e, "ShhherCommand", "setOTUs");
1263 /**************************************************************************************************/
1265 void ShhherCommand::getUniques(){
1270 uniqueFlowgrams.assign(numFlowCells * numSeqs, -1);
1271 uniqueCount.assign(numSeqs, 0); // anWeights
1272 uniqueLengths.assign(numSeqs, 0);
1273 mapSeqToUnique.assign(numSeqs, -1);
1274 mapUniqueToSeq.assign(numSeqs, -1);
1276 vector<short> uniqueFlowDataIntI(numFlowCells * numSeqs, -1);
1278 for(int i=0;i<numSeqs;i++){
1280 if (m->control_pressed) { break; }
1284 vector<short> current(numFlowCells);
1285 for(int j=0;j<numFlowCells;j++){
1286 current[j] = short(((flowDataIntI[i * numFlowCells + j] + 50.0)/100.0));
1289 for(int j=0;j<numUniques;j++){
1290 int offset = j * numFlowCells;
1294 if(lengths[i] < uniqueLengths[j]) { shorterLength = lengths[i]; }
1295 else { shorterLength = uniqueLengths[j]; }
1297 for(int k=0;k<shorterLength;k++){
1298 if(current[k] != uniqueFlowgrams[offset + k]){
1305 mapSeqToUnique[i] = j;
1308 if(lengths[i] > uniqueLengths[j]) { uniqueLengths[j] = lengths[i]; }
1314 if(index == numUniques){
1315 uniqueLengths[numUniques] = lengths[i];
1316 uniqueCount[numUniques] = 1;
1317 mapSeqToUnique[i] = numUniques;//anMap
1318 mapUniqueToSeq[numUniques] = i;//anF
1320 for(int k=0;k<numFlowCells;k++){
1321 uniqueFlowgrams[numUniques * numFlowCells + k] = current[k];
1322 uniqueFlowDataIntI[numUniques * numFlowCells + k] = flowDataIntI[i * numFlowCells + k];
1328 uniqueFlowDataIntI.resize(numFlowCells * numUniques);
1329 uniqueLengths.resize(numUniques);
1331 flowDataPrI.resize(numSeqs * numFlowCells, 0);
1332 for(int i=0;i<flowDataPrI.size();i++) { if (m->control_pressed) { break; } flowDataPrI[i] = getProbIntensity(flowDataIntI[i]); }
1334 catch(exception& e) {
1335 m->errorOut(e, "ShhherCommand", "getUniques");
1340 /**************************************************************************************************/
1342 float ShhherCommand::calcPairwiseDist(int seqA, int seqB){
1344 int minLength = lengths[mapSeqToUnique[seqA]];
1345 if(lengths[seqB] < minLength){ minLength = lengths[mapSeqToUnique[seqB]]; }
1347 int ANumFlowCells = seqA * numFlowCells;
1348 int BNumFlowCells = seqB * numFlowCells;
1352 for(int i=0;i<minLength;i++){
1354 if (m->control_pressed) { break; }
1356 int flowAIntI = flowDataIntI[ANumFlowCells + i];
1357 float flowAPrI = flowDataPrI[ANumFlowCells + i];
1359 int flowBIntI = flowDataIntI[BNumFlowCells + i];
1360 float flowBPrI = flowDataPrI[BNumFlowCells + i];
1361 dist += jointLookUp[flowAIntI * NUMBINS + flowBIntI] - flowAPrI - flowBPrI;
1364 dist /= (float) minLength;
1367 catch(exception& e) {
1368 m->errorOut(e, "ShhherCommand", "calcPairwiseDist");
1373 //**********************************************************************************************************************/
1375 string ShhherCommand::cluster(string distFileName, string namesFileName){
1378 ReadMatrix* read = new ReadColumnMatrix(distFileName);
1379 read->setCutoff(cutoff);
1381 NameAssignment* clusterNameMap = new NameAssignment(namesFileName);
1382 clusterNameMap->readMap();
1383 read->read(clusterNameMap);
1385 ListVector* list = read->getListVector();
1386 SparseMatrix* matrix = read->getMatrix();
1389 delete clusterNameMap;
1391 RAbundVector* rabund = new RAbundVector(list->getRAbundVector());
1393 Cluster* cluster = new CompleteLinkage(rabund, list, matrix, cutoff, "furthest");
1394 string tag = cluster->getTag();
1396 double clusterCutoff = cutoff;
1397 while (matrix->getSmallDist() <= clusterCutoff && matrix->getNNodes() > 0){
1399 if (m->control_pressed) { break; }
1401 cluster->update(clusterCutoff);
1404 list->setLabel(toString(cutoff));
1406 string listFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.list";
1408 m->openOutputFile(listFileName, listFile);
1409 list->print(listFile);
1412 delete matrix; delete cluster; delete rabund; delete list;
1414 return listFileName;
1416 catch(exception& e) {
1417 m->errorOut(e, "ShhherCommand", "cluster");
1422 /**************************************************************************************************/
1424 void ShhherCommand::calcCentroidsDriver(int start, int finish){
1426 //this function gets the most likely homopolymer length at a flow position for a group of sequences
1431 for(int i=start;i<finish;i++){
1433 if (m->control_pressed) { break; }
1437 int minFlowGram = 100000000;
1438 double minFlowValue = 1e8;
1439 change[i] = 0; //FALSE
1441 for(int j=0;j<nSeqsPerOTU[i];j++){
1442 count += singleTau[seqNumber[cumNumSeqs[i] + j]];
1445 if(nSeqsPerOTU[i] > 0 && count > MIN_COUNT){
1446 vector<double> adF(nSeqsPerOTU[i]);
1447 vector<int> anL(nSeqsPerOTU[i]);
1449 for(int j=0;j<nSeqsPerOTU[i];j++){
1450 int index = cumNumSeqs[i] + j;
1451 int nI = seqIndex[index];
1452 int nIU = mapSeqToUnique[nI];
1455 for(k=0;k<position;k++){
1461 anL[position] = nIU;
1462 adF[position] = 0.0000;
1467 for(int j=0;j<nSeqsPerOTU[i];j++){
1468 int index = cumNumSeqs[i] + j;
1469 int nI = seqIndex[index];
1471 double tauValue = singleTau[seqNumber[index]];
1473 for(int k=0;k<position;k++){
1474 double dist = getDistToCentroid(anL[k], nI, lengths[nI]);
1475 adF[k] += dist * tauValue;
1479 for(int j=0;j<position;j++){
1480 if(adF[j] < minFlowValue){
1482 minFlowValue = adF[j];
1486 if(centroids[i] != anL[minFlowGram]){
1488 centroids[i] = anL[minFlowGram];
1491 else if(centroids[i] != -1){
1497 catch(exception& e) {
1498 m->errorOut(e, "ShhherCommand", "calcCentroidsDriver");
1503 /**************************************************************************************************/
1505 double ShhherCommand::getDistToCentroid(int cent, int flow, int length){
1508 int flowAValue = cent * numFlowCells;
1509 int flowBValue = flow * numFlowCells;
1513 for(int i=0;i<length;i++){
1514 dist += singleLookUp[uniqueFlowgrams[flowAValue] * NUMBINS + flowDataIntI[flowBValue]];
1519 return dist / (double)length;
1521 catch(exception& e) {
1522 m->errorOut(e, "ShhherCommand", "getDistToCentroid");
1527 /**************************************************************************************************/
1529 double ShhherCommand::getNewWeights(){
1532 double maxChange = 0;
1534 for(int i=0;i<numOTUs;i++){
1536 if (m->control_pressed) { break; }
1538 double difference = weight[i];
1541 for(int j=0;j<nSeqsPerOTU[i];j++){
1542 int index = cumNumSeqs[i] + j;
1543 double tauValue = singleTau[seqNumber[index]];
1544 weight[i] += tauValue;
1547 difference = fabs(weight[i] - difference);
1548 if(difference > maxChange){ maxChange = difference; }
1552 catch(exception& e) {
1553 m->errorOut(e, "ShhherCommand", "getNewWeights");
1558 /**************************************************************************************************/
1560 double ShhherCommand::getLikelihood(){
1564 vector<long double> P(numSeqs, 0);
1567 for(int i=0;i<numOTUs;i++){
1568 if(weight[i] > MIN_WEIGHT){
1574 for(int i=0;i<numOTUs;i++){
1576 if (m->control_pressed) { break; }
1578 for(int j=0;j<nSeqsPerOTU[i];j++){
1579 int index = cumNumSeqs[i] + j;
1580 int nI = seqIndex[index];
1581 double singleDist = dist[seqNumber[index]];
1583 P[nI] += weight[i] * exp(-singleDist * sigma);
1587 for(int i=0;i<numSeqs;i++){
1588 if(P[i] == 0){ P[i] = DBL_EPSILON; }
1593 nLL = nLL -(double)numSeqs * log(sigma);
1597 catch(exception& e) {
1598 m->errorOut(e, "ShhherCommand", "getNewWeights");
1603 /**************************************************************************************************/
1605 void ShhherCommand::checkCentroids(){
1607 vector<int> unique(numOTUs, 1);
1609 for(int i=0;i<numOTUs;i++){
1610 if(centroids[i] == -1 || weight[i] < MIN_WEIGHT){
1615 for(int i=0;i<numOTUs;i++){
1617 if (m->control_pressed) { break; }
1620 for(int j=i+1;j<numOTUs;j++){
1623 if(centroids[j] == centroids[i]){
1627 weight[i] += weight[j];
1635 catch(exception& e) {
1636 m->errorOut(e, "ShhherCommand", "checkCentroids");
1640 /**************************************************************************************************/
1644 void ShhherCommand::writeQualities(vector<int> otuCounts){
1647 string thisOutputDir = outputDir;
1648 if (outputDir == "") { thisOutputDir += m->hasPath(flowFileName); }
1649 string qualityFileName = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName)) + getOutputFileNameTag("qfile");
1651 ofstream qualityFile;
1652 m->openOutputFile(qualityFileName, qualityFile);
1654 qualityFile.setf(ios::fixed, ios::floatfield);
1655 qualityFile.setf(ios::showpoint);
1656 qualityFile << setprecision(6);
1658 vector<vector<int> > qualities(numOTUs);
1659 vector<double> pr(HOMOPS, 0);
1662 for(int i=0;i<numOTUs;i++){
1664 if (m->control_pressed) { break; }
1669 if(nSeqsPerOTU[i] > 0){
1670 qualities[i].assign(1024, -1);
1672 while(index < numFlowCells){
1673 double maxPrValue = 1e8;
1674 short maxPrIndex = -1;
1675 double count = 0.0000;
1677 pr.assign(HOMOPS, 0);
1679 for(int j=0;j<nSeqsPerOTU[i];j++){
1680 int lIndex = cumNumSeqs[i] + j;
1681 double tauValue = singleTau[seqNumber[lIndex]];
1682 int sequenceIndex = aaI[i][j];
1683 short intensity = flowDataIntI[sequenceIndex * numFlowCells + index];
1687 for(int s=0;s<HOMOPS;s++){
1688 pr[s] += tauValue * singleLookUp[s * NUMBINS + intensity];
1692 maxPrIndex = uniqueFlowgrams[centroids[i] * numFlowCells + index];
1693 maxPrValue = pr[maxPrIndex];
1695 if(count > MIN_COUNT){
1697 double norm = 0.0000;
1699 for(int s=0;s<HOMOPS;s++){
1700 norm += exp(-(pr[s] - maxPrValue));
1703 for(int s=1;s<=maxPrIndex;s++){
1705 double temp = 0.0000;
1707 U += exp(-(pr[s-1]-maxPrValue))/norm;
1715 temp = floor(-10 * temp);
1716 value = (int)floor(temp);
1717 if(value > 100){ value = 100; }
1719 qualities[i][base] = (int)value;
1729 if(otuCounts[i] > 0){
1730 qualityFile << '>' << seqNameVector[mapUniqueToSeq[i]] << endl;
1732 int j=4; //need to get past the first four bases
1733 while(qualities[i][j] != -1){
1734 qualityFile << qualities[i][j] << ' ';
1737 qualityFile << endl;
1740 qualityFile.close();
1741 outputNames.push_back(qualityFileName);
1744 catch(exception& e) {
1745 m->errorOut(e, "ShhherCommand", "writeQualities");
1750 /**************************************************************************************************/
1752 void ShhherCommand::writeSequences(vector<int> otuCounts){
1754 string thisOutputDir = outputDir;
1755 if (outputDir == "") { thisOutputDir += m->hasPath(flowFileName); }
1756 string fastaFileName = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName)) + getOutputFileNameTag("fasta");
1758 m->openOutputFile(fastaFileName, fastaFile);
1760 vector<string> names(numOTUs, "");
1762 for(int i=0;i<numOTUs;i++){
1764 if (m->control_pressed) { break; }
1766 int index = centroids[i];
1768 if(otuCounts[i] > 0){
1769 fastaFile << '>' << seqNameVector[aaI[i][0]] << endl;
1773 for(int j=0;j<numFlowCells;j++){
1775 char base = flowOrder[j % 4];
1776 for(int k=0;k<uniqueFlowgrams[index * numFlowCells + j];k++){
1781 fastaFile << newSeq.substr(4) << endl;
1786 outputNames.push_back(fastaFileName);
1788 if(compositeFASTAFileName != ""){
1789 m->appendFiles(fastaFileName, compositeFASTAFileName);
1792 catch(exception& e) {
1793 m->errorOut(e, "ShhherCommand", "writeSequences");
1798 /**************************************************************************************************/
1800 void ShhherCommand::writeNames(vector<int> otuCounts){
1802 string thisOutputDir = outputDir;
1803 if (outputDir == "") { thisOutputDir += m->hasPath(flowFileName); }
1804 string nameFileName = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName)) + getOutputFileNameTag("name");
1806 m->openOutputFile(nameFileName, nameFile);
1808 for(int i=0;i<numOTUs;i++){
1810 if (m->control_pressed) { break; }
1812 if(otuCounts[i] > 0){
1813 nameFile << seqNameVector[aaI[i][0]] << '\t' << seqNameVector[aaI[i][0]];
1815 for(int j=1;j<nSeqsPerOTU[i];j++){
1816 nameFile << ',' << seqNameVector[aaI[i][j]];
1823 outputNames.push_back(nameFileName);
1826 if(compositeNamesFileName != ""){
1827 m->appendFiles(nameFileName, compositeNamesFileName);
1830 catch(exception& e) {
1831 m->errorOut(e, "ShhherCommand", "writeNames");
1836 /**************************************************************************************************/
1838 void ShhherCommand::writeGroups(){
1840 string thisOutputDir = outputDir;
1841 if (outputDir == "") { thisOutputDir += m->hasPath(flowFileName); }
1842 string fileRoot = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName));
1843 string groupFileName = fileRoot + getOutputFileNameTag("group");
1845 m->openOutputFile(groupFileName, groupFile);
1847 for(int i=0;i<numSeqs;i++){
1848 if (m->control_pressed) { break; }
1849 groupFile << seqNameVector[i] << '\t' << fileRoot << endl;
1852 outputNames.push_back(groupFileName);
1855 catch(exception& e) {
1856 m->errorOut(e, "ShhherCommand", "writeGroups");
1861 /**************************************************************************************************/
1863 void ShhherCommand::writeClusters(vector<int> otuCounts){
1865 string thisOutputDir = outputDir;
1866 if (outputDir == "") { thisOutputDir += m->hasPath(flowFileName); }
1867 string otuCountsFileName = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName)) +getOutputFileNameTag("counts");
1868 ofstream otuCountsFile;
1869 m->openOutputFile(otuCountsFileName, otuCountsFile);
1871 string bases = flowOrder;
1873 for(int i=0;i<numOTUs;i++){
1875 if (m->control_pressed) {
1878 //output the translated version of the centroid sequence for the otu
1879 if(otuCounts[i] > 0){
1880 int index = centroids[i];
1882 otuCountsFile << "ideal\t";
1883 for(int j=8;j<numFlowCells;j++){
1884 char base = bases[j % 4];
1885 for(int s=0;s<uniqueFlowgrams[index * numFlowCells + j];s++){
1886 otuCountsFile << base;
1889 otuCountsFile << endl;
1891 for(int j=0;j<nSeqsPerOTU[i];j++){
1892 int sequence = aaI[i][j];
1893 otuCountsFile << seqNameVector[sequence] << '\t';
1897 for(int k=0;k<lengths[sequence];k++){
1898 char base = bases[k % 4];
1899 int freq = int(0.01 * (double)flowDataIntI[sequence * numFlowCells + k] + 0.5);
1901 for(int s=0;s<freq;s++){
1903 //otuCountsFile << base;
1906 otuCountsFile << newSeq.substr(4) << endl;
1908 otuCountsFile << endl;
1911 otuCountsFile.close();
1912 outputNames.push_back(otuCountsFileName);
1915 catch(exception& e) {
1916 m->errorOut(e, "ShhherCommand", "writeClusters");
1922 //**********************************************************************************************************************
1924 int ShhherCommand::execute(){
1926 if (abort == true) { return 0; }
1928 getSingleLookUp(); if (m->control_pressed) { return 0; }
1929 getJointLookUp(); if (m->control_pressed) { return 0; }
1931 int numFiles = flowFileVector.size();
1933 if (numFiles < processors) { processors = numFiles; }
1935 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1936 if (processors == 1) { driver(flowFileVector, compositeFASTAFileName, compositeNamesFileName, 0, flowFileVector.size()); }
1937 else { createProcesses(flowFileVector); } //each processor processes one file
1939 driver(flowFileVector, compositeFASTAFileName, compositeNamesFileName, 0, flowFileVector.size());
1942 if(compositeFASTAFileName != ""){
1943 outputNames.push_back(compositeFASTAFileName);
1944 outputNames.push_back(compositeNamesFileName);
1947 m->mothurOutEndLine();
1948 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
1949 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
1950 m->mothurOutEndLine();
1954 catch(exception& e) {
1955 m->errorOut(e, "ShhherCommand", "execute");
1960 /**************************************************************************************************/
1962 int ShhherCommand::createProcesses(vector<string> filenames){
1964 vector<int> processIDS;
1969 if (filenames.size() < processors) { processors = filenames.size(); }
1971 //divide the groups between the processors
1972 vector<linePair> lines;
1973 vector<int> numFilesToComplete;
1974 int numFilesPerProcessor = filenames.size() / processors;
1975 for (int i = 0; i < processors; i++) {
1976 int startIndex = i * numFilesPerProcessor;
1977 int endIndex = (i+1) * numFilesPerProcessor;
1978 if(i == (processors - 1)){ endIndex = filenames.size(); }
1979 lines.push_back(linePair(startIndex, endIndex));
1980 numFilesToComplete.push_back((endIndex-startIndex));
1983 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1985 //loop through and create all the processes you want
1986 while (process != processors) {
1990 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
1992 }else if (pid == 0){
1993 num = driver(filenames, compositeFASTAFileName + toString(getpid()) + ".temp", compositeNamesFileName + toString(getpid()) + ".temp", lines[process].start, lines[process].end);
1995 //pass numSeqs to parent
1997 string tempFile = compositeFASTAFileName + toString(getpid()) + ".num.temp";
1998 m->openOutputFile(tempFile, out);
2004 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
2005 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
2011 driver(filenames, compositeFASTAFileName, compositeNamesFileName, lines[0].start, lines[0].end);
2013 //force parent to wait until all the processes are done
2014 for (int i=0;i<processIDS.size();i++) {
2015 int temp = processIDS[i];
2021 //////////////////////////////////////////////////////////////////////////////////////////////////////
2023 /////////////////////// NOT WORKING, ACCESS VIOLATION ON READ OF FLOWGRAMS IN THREAD /////////////////
2025 //////////////////////////////////////////////////////////////////////////////////////////////////////
2026 //Windows version shared memory, so be careful when passing variables through the shhhFlowsData struct.
2027 //Above fork() will clone, so memory is separate, but that's not the case with windows,
2028 //////////////////////////////////////////////////////////////////////////////////////////////////////
2030 vector<shhhFlowsData*> pDataArray;
2031 DWORD dwThreadIdArray[processors-1];
2032 HANDLE hThreadArray[processors-1];
2034 //Create processor worker threads.
2035 for( int i=0; i<processors-1; i++ ){
2036 // Allocate memory for thread data.
2037 string extension = "";
2038 if (i != 0) { extension = toString(i) + ".temp"; }
2040 shhhFlowsData* tempFlow = new shhhFlowsData(filenames, (compositeFASTAFileName + extension), (compositeNamesFileName + extension), outputDir, flowOrder, jointLookUp, singleLookUp, m, lines[i].start, lines[i].end, cutoff, sigma, minDelta, maxIters, i);
2041 pDataArray.push_back(tempFlow);
2042 processIDS.push_back(i);
2044 hThreadArray[i] = CreateThread(NULL, 0, ShhhFlowsThreadFunction, pDataArray[i], 0, &dwThreadIdArray[i]);
2047 //using the main process as a worker saves time and memory
2049 driver(filenames, compositeFASTAFileName, compositeNamesFileName, lines[processors-1].start, lines[processors-1].end);
2051 //Wait until all threads have terminated.
2052 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
2054 //Close all thread handles and free memory allocations.
2055 for(int i=0; i < pDataArray.size(); i++){
2056 for(int j=0; j < pDataArray[i]->outputNames.size(); j++){ outputNames.push_back(pDataArray[i]->outputNames[j]); }
2057 CloseHandle(hThreadArray[i]);
2058 delete pDataArray[i];
2063 for (int i=0;i<processIDS.size();i++) {
2065 string tempFile = compositeFASTAFileName + toString(processIDS[i]) + ".num.temp";
2066 m->openInputFile(tempFile, in);
2070 if (tempNum != numFilesToComplete[i+1]) {
2071 m->mothurOut("[ERROR]: main process expected " + toString(processIDS[i]) + " to complete " + toString(numFilesToComplete[i+1]) + " files, and it only reported completing " + toString(tempNum) + ". This will cause file mismatches. The flow files may be too large to process with multiple processors. \n");
2074 in.close(); m->mothurRemove(tempFile);
2076 if (compositeFASTAFileName != "") {
2077 m->appendFiles((compositeFASTAFileName + toString(processIDS[i]) + ".temp"), compositeFASTAFileName);
2078 m->appendFiles((compositeNamesFileName + toString(processIDS[i]) + ".temp"), compositeNamesFileName);
2079 m->mothurRemove((compositeFASTAFileName + toString(processIDS[i]) + ".temp"));
2080 m->mothurRemove((compositeNamesFileName + toString(processIDS[i]) + ".temp"));
2087 catch(exception& e) {
2088 m->errorOut(e, "ShhherCommand", "createProcesses");
2092 /**************************************************************************************************/
2094 vector<string> ShhherCommand::parseFlowFiles(string filename){
2096 vector<string> files;
2100 m->openInputFile(filename, in);
2102 int thisNumFLows = 0;
2103 in >> thisNumFLows; m->gobble(in);
2106 if (m->control_pressed) { break; }
2109 string outputFileName = filename + toString(count) + ".temp";
2110 m->openOutputFile(outputFileName, out);
2111 out << thisNumFLows << endl;
2112 files.push_back(outputFileName);
2114 int numLinesWrote = 0;
2115 for (int i = 0; i < largeSize; i++) {
2116 if (in.eof()) { break; }
2117 string line = m->getline(in); m->gobble(in);
2118 out << line << endl;
2123 if (numLinesWrote == 0) { m->mothurRemove(outputFileName); files.pop_back(); }
2128 if (m->control_pressed) { for (int i = 0; i < files.size(); i++) { m->mothurRemove(files[i]); } files.clear(); }
2130 m->mothurOut("\nDivided " + filename + " into " + toString(files.size()) + " files.\n\n");
2134 catch(exception& e) {
2135 m->errorOut(e, "ShhherCommand", "parseFlowFiles");
2139 /**************************************************************************************************/
2141 int ShhherCommand::driver(vector<string> filenames, string thisCompositeFASTAFileName, string thisCompositeNamesFileName, int start, int end){
2144 int numCompleted = 0;
2146 for(int i=start;i<end;i++){
2148 if (m->control_pressed) { break; }
2150 vector<string> theseFlowFileNames; theseFlowFileNames.push_back(filenames[i]);
2151 if (large) { theseFlowFileNames = parseFlowFiles(filenames[i]); }
2153 if (m->control_pressed) { break; }
2155 double begClock = clock();
2156 unsigned long long begTime;
2158 for (int g = 0; g < theseFlowFileNames.size(); g++) {
2160 string flowFileName = theseFlowFileNames[g];
2161 m->mothurOut("\n>>>>>\tProcessing " + flowFileName + " (file " + toString(i+1) + " of " + toString(filenames.size()) + ")\t<<<<<\n");
2162 m->mothurOut("Reading flowgrams...\n");
2164 vector<string> seqNameVector;
2165 vector<int> lengths;
2166 vector<short> flowDataIntI;
2167 vector<double> flowDataPrI;
2168 map<string, int> nameMap;
2169 vector<short> uniqueFlowgrams;
2170 vector<int> uniqueCount;
2171 vector<int> mapSeqToUnique;
2172 vector<int> mapUniqueToSeq;
2173 vector<int> uniqueLengths;
2176 if (m->debug) { m->mothurOut("[DEBUG]: About to read flowgrams.\n"); }
2177 int numSeqs = getFlowData(flowFileName, seqNameVector, lengths, flowDataIntI, nameMap, numFlowCells);
2179 if (m->control_pressed) { break; }
2181 m->mothurOut("Identifying unique flowgrams...\n");
2182 int numUniques = getUniques(numSeqs, numFlowCells, uniqueFlowgrams, uniqueCount, uniqueLengths, mapSeqToUnique, mapUniqueToSeq, lengths, flowDataPrI, flowDataIntI);
2184 if (m->control_pressed) { break; }
2186 m->mothurOut("Calculating distances between flowgrams...\n");
2187 string distFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.dist";
2188 begTime = time(NULL);
2191 flowDistParentFork(numFlowCells, distFileName, numUniques, mapUniqueToSeq, mapSeqToUnique, lengths, flowDataPrI, flowDataIntI);
2193 m->mothurOutEndLine();
2194 m->mothurOut("Total time: " + toString(time(NULL) - begTime) + '\t' + toString((clock() - begClock)/CLOCKS_PER_SEC) + '\n');
2197 string namesFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.names";
2198 createNamesFile(numSeqs, numUniques, namesFileName, seqNameVector, mapSeqToUnique, mapUniqueToSeq);
2200 if (m->control_pressed) { break; }
2202 m->mothurOut("\nClustering flowgrams...\n");
2203 string listFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.list";
2204 cluster(listFileName, distFileName, namesFileName);
2206 if (m->control_pressed) { break; }
2208 vector<int> otuData;
2209 vector<int> cumNumSeqs;
2210 vector<int> nSeqsPerOTU;
2211 vector<vector<int> > aaP; //tMaster->aanP: each row is a different otu / each col contains the sequence indices
2212 vector<vector<int> > aaI; //tMaster->aanI: that are in each otu - can't differentiate between aaP and aaI
2213 vector<int> seqNumber; //tMaster->anP: the sequence id number sorted by OTU
2214 vector<int> seqIndex; //tMaster->anI; the index that corresponds to seqNumber
2217 int numOTUs = getOTUData(numSeqs, listFileName, otuData, cumNumSeqs, nSeqsPerOTU, aaP, aaI, seqNumber, seqIndex, nameMap);
2219 if (m->control_pressed) { break; }
2221 m->mothurRemove(distFileName);
2222 m->mothurRemove(namesFileName);
2223 m->mothurRemove(listFileName);
2225 vector<double> dist; //adDist - distance of sequences to centroids
2226 vector<short> change; //did the centroid sequence change? 0 = no; 1 = yes
2227 vector<int> centroids; //the representative flowgram for each cluster m
2228 vector<double> weight;
2229 vector<double> singleTau; //tMaster->adTau: 1-D Tau vector (1xnumSeqs)
2230 vector<int> nSeqsBreaks;
2231 vector<int> nOTUsBreaks;
2233 if (m->debug) { m->mothurOut("[DEBUG]: numSeqs = " + toString(numSeqs) + " numOTUS = " + toString(numOTUs) + " about to alloc a dist vector with size = " + toString((numSeqs * numOTUs)) + ".\n"); }
2235 dist.assign(numSeqs * numOTUs, 0);
2236 change.assign(numOTUs, 1);
2237 centroids.assign(numOTUs, -1);
2238 weight.assign(numOTUs, 0);
2239 singleTau.assign(numSeqs, 1.0);
2241 nSeqsBreaks.assign(2, 0);
2242 nOTUsBreaks.assign(2, 0);
2245 nSeqsBreaks[1] = numSeqs;
2246 nOTUsBreaks[1] = numOTUs;
2248 if (m->debug) { m->mothurOut("[DEBUG]: done allocating memory, about to denoise.\n"); }
2250 if (m->control_pressed) { break; }
2252 double maxDelta = 0;
2256 begTime = time(NULL);
2258 m->mothurOut("\nDenoising flowgrams...\n");
2259 m->mothurOut("iter\tmaxDelta\tnLL\t\tcycletime\n");
2261 while((maxIters == 0 && maxDelta > minDelta) || iter < MIN_ITER || (maxDelta > minDelta && iter < maxIters)){
2263 if (m->control_pressed) { break; }
2265 double cycClock = clock();
2266 unsigned long long cycTime = time(NULL);
2267 fill(numOTUs, seqNumber, seqIndex, cumNumSeqs, nSeqsPerOTU, aaP, aaI);
2269 if (m->control_pressed) { break; }
2271 calcCentroidsDriver(numOTUs, cumNumSeqs, nSeqsPerOTU, seqIndex, change, centroids, singleTau, mapSeqToUnique, uniqueFlowgrams, flowDataIntI, lengths, numFlowCells, seqNumber);
2273 if (m->control_pressed) { break; }
2275 maxDelta = getNewWeights(numOTUs, cumNumSeqs, nSeqsPerOTU, singleTau, seqNumber, weight);
2277 if (m->control_pressed) { break; }
2279 double nLL = getLikelihood(numSeqs, numOTUs, nSeqsPerOTU, seqNumber, cumNumSeqs, seqIndex, dist, weight);
2281 if (m->control_pressed) { break; }
2283 checkCentroids(numOTUs, centroids, weight);
2285 if (m->control_pressed) { break; }
2287 calcNewDistances(numSeqs, numOTUs, nSeqsPerOTU, dist, weight, change, centroids, aaP, singleTau, aaI, seqNumber, seqIndex, uniqueFlowgrams, flowDataIntI, numFlowCells, lengths);
2289 if (m->control_pressed) { break; }
2293 m->mothurOut(toString(iter) + '\t' + toString(maxDelta) + '\t' + toString(nLL) + '\t' + toString(time(NULL) - cycTime) + '\t' + toString((clock() - cycClock)/(double)CLOCKS_PER_SEC) + '\n');
2297 if (m->control_pressed) { break; }
2299 m->mothurOut("\nFinalizing...\n");
2300 fill(numOTUs, seqNumber, seqIndex, cumNumSeqs, nSeqsPerOTU, aaP, aaI);
2302 if (m->control_pressed) { break; }
2304 setOTUs(numOTUs, numSeqs, seqNumber, seqIndex, cumNumSeqs, nSeqsPerOTU, otuData, singleTau, dist, aaP, aaI);
2306 if (m->control_pressed) { break; }
2308 vector<int> otuCounts(numOTUs, 0);
2309 for(int i=0;i<numSeqs;i++) { otuCounts[otuData[i]]++; }
2311 calcCentroidsDriver(numOTUs, cumNumSeqs, nSeqsPerOTU, seqIndex, change, centroids, singleTau, mapSeqToUnique, uniqueFlowgrams, flowDataIntI, lengths, numFlowCells, seqNumber);
2313 if (m->control_pressed) { break; }
2315 if ((large) && (g == 0)) { flowFileName = filenames[i]; theseFlowFileNames[0] = filenames[i]; }
2316 string thisOutputDir = outputDir;
2317 if (outputDir == "") { thisOutputDir = m->hasPath(flowFileName); }
2318 string qualityFileName = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName)) + getOutputFileNameTag("qfile");
2319 string fastaFileName = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName)) + getOutputFileNameTag("fasta");
2320 string nameFileName = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName)) + getOutputFileNameTag("name");
2321 string otuCountsFileName = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName)) + getOutputFileNameTag("counts");
2322 string fileRoot = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName));
2323 string groupFileName = fileRoot + getOutputFileNameTag("group");
2326 writeQualities(numOTUs, numFlowCells, qualityFileName, otuCounts, nSeqsPerOTU, seqNumber, singleTau, flowDataIntI, uniqueFlowgrams, cumNumSeqs, mapUniqueToSeq, seqNameVector, centroids, aaI); if (m->control_pressed) { break; }
2327 writeSequences(thisCompositeFASTAFileName, numOTUs, numFlowCells, fastaFileName, otuCounts, uniqueFlowgrams, seqNameVector, aaI, centroids);if (m->control_pressed) { break; }
2328 writeNames(thisCompositeNamesFileName, numOTUs, nameFileName, otuCounts, seqNameVector, aaI, nSeqsPerOTU); if (m->control_pressed) { break; }
2329 writeClusters(otuCountsFileName, numOTUs, numFlowCells,otuCounts, centroids, uniqueFlowgrams, seqNameVector, aaI, nSeqsPerOTU, lengths, flowDataIntI); if (m->control_pressed) { break; }
2330 writeGroups(groupFileName, fileRoot, numSeqs, seqNameVector); if (m->control_pressed) { break; }
2334 m->appendFiles(qualityFileName, (thisOutputDir + m->getRootName(m->getSimpleName(theseFlowFileNames[0])) + getOutputFileNameTag("qfile")));
2335 m->mothurRemove(qualityFileName);
2336 m->appendFiles(fastaFileName, (thisOutputDir + m->getRootName(m->getSimpleName(theseFlowFileNames[0])) + getOutputFileNameTag("fasta")));
2337 m->mothurRemove(fastaFileName);
2338 m->appendFiles(nameFileName, (thisOutputDir + m->getRootName(m->getSimpleName(theseFlowFileNames[0])) + getOutputFileNameTag("name")));
2339 m->mothurRemove(nameFileName);
2340 m->appendFiles(otuCountsFileName, (thisOutputDir + m->getRootName(m->getSimpleName(theseFlowFileNames[0])) + getOutputFileNameTag("counts")));
2341 m->mothurRemove(otuCountsFileName);
2342 m->appendFiles(groupFileName, (thisOutputDir + m->getRootName(m->getSimpleName(theseFlowFileNames[0])) + getOutputFileNameTag("group")));
2343 m->mothurRemove(groupFileName);
2345 m->mothurRemove(theseFlowFileNames[g]);
2350 m->mothurOut("Total time to process " + flowFileName + ":\t" + toString(time(NULL) - begTime) + '\t' + toString((clock() - begClock)/(double)CLOCKS_PER_SEC) + '\n');
2353 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
2355 return numCompleted;
2357 }catch(exception& e) {
2358 m->errorOut(e, "ShhherCommand", "driver");
2363 /**************************************************************************************************/
2364 int ShhherCommand::getFlowData(string filename, vector<string>& thisSeqNameVector, vector<int>& thisLengths, vector<short>& thisFlowDataIntI, map<string, int>& thisNameMap, int& numFlowCells){
2369 m->openInputFile(filename, flowFile);
2372 int currentNumFlowCells;
2374 thisSeqNameVector.clear();
2375 thisLengths.clear();
2376 thisFlowDataIntI.clear();
2377 thisNameMap.clear();
2379 flowFile >> numFlowCells;
2380 if (m->debug) { m->mothurOut("[DEBUG]: numFlowCells = " + toString(numFlowCells) + ".\n"); }
2381 int index = 0;//pcluster
2382 while(!flowFile.eof()){
2384 if (m->control_pressed) { break; }
2386 flowFile >> seqName >> currentNumFlowCells;
2388 thisLengths.push_back(currentNumFlowCells);
2390 thisSeqNameVector.push_back(seqName);
2391 thisNameMap[seqName] = index++;//pcluster
2393 if (m->debug) { m->mothurOut("[DEBUG]: seqName = " + seqName + " length = " + toString(currentNumFlowCells) + " index = " + toString(index) + "\n"); }
2395 for(int i=0;i<numFlowCells;i++){
2396 flowFile >> intensity;
2397 if(intensity > 9.99) { intensity = 9.99; }
2398 int intI = int(100 * intensity + 0.0001);
2399 thisFlowDataIntI.push_back(intI);
2401 m->gobble(flowFile);
2405 int numSeqs = thisSeqNameVector.size();
2407 for(int i=0;i<numSeqs;i++){
2409 if (m->control_pressed) { break; }
2411 int iNumFlowCells = i * numFlowCells;
2412 for(int j=thisLengths[i];j<numFlowCells;j++){
2413 thisFlowDataIntI[iNumFlowCells + j] = 0;
2420 catch(exception& e) {
2421 m->errorOut(e, "ShhherCommand", "getFlowData");
2425 /**************************************************************************************************/
2427 int ShhherCommand::flowDistParentFork(int numFlowCells, string distFileName, int stopSeq, vector<int>& mapUniqueToSeq, vector<int>& mapSeqToUnique, vector<int>& lengths, vector<double>& flowDataPrI, vector<short>& flowDataIntI){
2430 ostringstream outStream;
2431 outStream.setf(ios::fixed, ios::floatfield);
2432 outStream.setf(ios::dec, ios::basefield);
2433 outStream.setf(ios::showpoint);
2434 outStream.precision(6);
2436 int begTime = time(NULL);
2437 double begClock = clock();
2439 for(int i=0;i<stopSeq;i++){
2441 if (m->control_pressed) { break; }
2443 for(int j=0;j<i;j++){
2444 float flowDistance = calcPairwiseDist(numFlowCells, mapUniqueToSeq[i], mapUniqueToSeq[j], mapSeqToUnique, lengths, flowDataPrI, flowDataIntI);
2446 if(flowDistance < 1e-6){
2447 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << 0.000000 << endl;
2449 else if(flowDistance <= cutoff){
2450 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << flowDistance << endl;
2454 m->mothurOut(toString(i) + "\t" + toString(time(NULL) - begTime));
2455 m->mothurOut("\t" + toString((clock()-begClock)/CLOCKS_PER_SEC));
2456 m->mothurOutEndLine();
2460 ofstream distFile(distFileName.c_str());
2461 distFile << outStream.str();
2464 if (m->control_pressed) {}
2466 m->mothurOut(toString(stopSeq-1) + "\t" + toString(time(NULL) - begTime));
2467 m->mothurOut("\t" + toString((clock()-begClock)/CLOCKS_PER_SEC));
2468 m->mothurOutEndLine();
2473 catch(exception& e) {
2474 m->errorOut(e, "ShhherCommand", "flowDistParentFork");
2478 /**************************************************************************************************/
2480 float ShhherCommand::calcPairwiseDist(int numFlowCells, int seqA, int seqB, vector<int>& mapSeqToUnique, vector<int>& lengths, vector<double>& flowDataPrI, vector<short>& flowDataIntI){
2482 int minLength = lengths[mapSeqToUnique[seqA]];
2483 if(lengths[seqB] < minLength){ minLength = lengths[mapSeqToUnique[seqB]]; }
2485 int ANumFlowCells = seqA * numFlowCells;
2486 int BNumFlowCells = seqB * numFlowCells;
2490 for(int i=0;i<minLength;i++){
2492 if (m->control_pressed) { break; }
2494 int flowAIntI = flowDataIntI[ANumFlowCells + i];
2495 float flowAPrI = flowDataPrI[ANumFlowCells + i];
2497 int flowBIntI = flowDataIntI[BNumFlowCells + i];
2498 float flowBPrI = flowDataPrI[BNumFlowCells + i];
2499 dist += jointLookUp[flowAIntI * NUMBINS + flowBIntI] - flowAPrI - flowBPrI;
2502 dist /= (float) minLength;
2505 catch(exception& e) {
2506 m->errorOut(e, "ShhherCommand", "calcPairwiseDist");
2511 /**************************************************************************************************/
2513 int ShhherCommand::getUniques(int numSeqs, int numFlowCells, vector<short>& uniqueFlowgrams, vector<int>& uniqueCount, vector<int>& uniqueLengths, vector<int>& mapSeqToUnique, vector<int>& mapUniqueToSeq, vector<int>& lengths, vector<double>& flowDataPrI, vector<short>& flowDataIntI){
2516 uniqueFlowgrams.assign(numFlowCells * numSeqs, -1);
2517 uniqueCount.assign(numSeqs, 0); // anWeights
2518 uniqueLengths.assign(numSeqs, 0);
2519 mapSeqToUnique.assign(numSeqs, -1);
2520 mapUniqueToSeq.assign(numSeqs, -1);
2522 vector<short> uniqueFlowDataIntI(numFlowCells * numSeqs, -1);
2524 for(int i=0;i<numSeqs;i++){
2526 if (m->control_pressed) { break; }
2530 vector<short> current(numFlowCells);
2531 for(int j=0;j<numFlowCells;j++){
2532 current[j] = short(((flowDataIntI[i * numFlowCells + j] + 50.0)/100.0));
2535 for(int j=0;j<numUniques;j++){
2536 int offset = j * numFlowCells;
2540 if(lengths[i] < uniqueLengths[j]) { shorterLength = lengths[i]; }
2541 else { shorterLength = uniqueLengths[j]; }
2543 for(int k=0;k<shorterLength;k++){
2544 if(current[k] != uniqueFlowgrams[offset + k]){
2551 mapSeqToUnique[i] = j;
2554 if(lengths[i] > uniqueLengths[j]) { uniqueLengths[j] = lengths[i]; }
2560 if(index == numUniques){
2561 uniqueLengths[numUniques] = lengths[i];
2562 uniqueCount[numUniques] = 1;
2563 mapSeqToUnique[i] = numUniques;//anMap
2564 mapUniqueToSeq[numUniques] = i;//anF
2566 for(int k=0;k<numFlowCells;k++){
2567 uniqueFlowgrams[numUniques * numFlowCells + k] = current[k];
2568 uniqueFlowDataIntI[numUniques * numFlowCells + k] = flowDataIntI[i * numFlowCells + k];
2574 uniqueFlowDataIntI.resize(numFlowCells * numUniques);
2575 uniqueLengths.resize(numUniques);
2577 flowDataPrI.resize(numSeqs * numFlowCells, 0);
2578 for(int i=0;i<flowDataPrI.size();i++) { if (m->control_pressed) { break; } flowDataPrI[i] = getProbIntensity(flowDataIntI[i]); }
2582 catch(exception& e) {
2583 m->errorOut(e, "ShhherCommand", "getUniques");
2587 /**************************************************************************************************/
2588 int ShhherCommand::createNamesFile(int numSeqs, int numUniques, string filename, vector<string>& seqNameVector, vector<int>& mapSeqToUnique, vector<int>& mapUniqueToSeq){
2591 vector<string> duplicateNames(numUniques, "");
2592 for(int i=0;i<numSeqs;i++){
2593 duplicateNames[mapSeqToUnique[i]] += seqNameVector[i] + ',';
2597 m->openOutputFile(filename, nameFile);
2599 for(int i=0;i<numUniques;i++){
2601 if (m->control_pressed) { break; }
2603 // nameFile << seqNameVector[mapUniqueToSeq[i]] << '\t' << duplicateNames[i].substr(0, duplicateNames[i].find_last_of(',')) << endl;
2604 nameFile << mapUniqueToSeq[i] << '\t' << duplicateNames[i].substr(0, duplicateNames[i].find_last_of(',')) << endl;
2611 catch(exception& e) {
2612 m->errorOut(e, "ShhherCommand", "createNamesFile");
2616 //**********************************************************************************************************************
2618 int ShhherCommand::cluster(string filename, string distFileName, string namesFileName){
2621 ReadMatrix* read = new ReadColumnMatrix(distFileName);
2622 read->setCutoff(cutoff);
2624 NameAssignment* clusterNameMap = new NameAssignment(namesFileName);
2625 clusterNameMap->readMap();
2626 read->read(clusterNameMap);
2628 ListVector* list = read->getListVector();
2629 SparseMatrix* matrix = read->getMatrix();
2632 delete clusterNameMap;
2634 RAbundVector* rabund = new RAbundVector(list->getRAbundVector());
2636 Cluster* cluster = new CompleteLinkage(rabund, list, matrix, cutoff, "furthest");
2637 string tag = cluster->getTag();
2639 double clusterCutoff = cutoff;
2640 while (matrix->getSmallDist() <= clusterCutoff && matrix->getNNodes() > 0){
2642 if (m->control_pressed) { break; }
2644 cluster->update(clusterCutoff);
2647 list->setLabel(toString(cutoff));
2650 m->openOutputFile(filename, listFile);
2651 list->print(listFile);
2654 delete matrix; delete cluster; delete rabund; delete list;
2658 catch(exception& e) {
2659 m->errorOut(e, "ShhherCommand", "cluster");
2663 /**************************************************************************************************/
2665 int ShhherCommand::getOTUData(int numSeqs, string fileName, vector<int>& otuData,
2666 vector<int>& cumNumSeqs,
2667 vector<int>& nSeqsPerOTU,
2668 vector<vector<int> >& aaP, //tMaster->aanP: each row is a different otu / each col contains the sequence indices
2669 vector<vector<int> >& aaI, //tMaster->aanI: that are in each otu - can't differentiate between aaP and aaI
2670 vector<int>& seqNumber, //tMaster->anP: the sequence id number sorted by OTU
2671 vector<int>& seqIndex,
2672 map<string, int>& nameMap){
2676 m->openInputFile(fileName, listFile);
2680 listFile >> label >> numOTUs;
2682 if (m->debug) { m->mothurOut("[DEBUG]: Getting OTU Data...\n"); }
2684 otuData.assign(numSeqs, 0);
2685 cumNumSeqs.assign(numOTUs, 0);
2686 nSeqsPerOTU.assign(numOTUs, 0);
2687 aaP.clear();aaP.resize(numOTUs);
2693 string singleOTU = "";
2695 for(int i=0;i<numOTUs;i++){
2697 if (m->control_pressed) { break; }
2698 if (m->debug) { m->mothurOut("[DEBUG]: processing OTU " + toString(i) + ".\n"); }
2700 listFile >> singleOTU;
2702 istringstream otuString(singleOTU);
2706 string seqName = "";
2708 for(int j=0;j<singleOTU.length();j++){
2709 char letter = otuString.get();
2715 map<string,int>::iterator nmIt = nameMap.find(seqName);
2716 int index = nmIt->second;
2718 nameMap.erase(nmIt);
2722 aaP[i].push_back(index);
2727 map<string,int>::iterator nmIt = nameMap.find(seqName);
2729 int index = nmIt->second;
2730 nameMap.erase(nmIt);
2734 aaP[i].push_back(index);
2739 sort(aaP[i].begin(), aaP[i].end());
2740 for(int j=0;j<nSeqsPerOTU[i];j++){
2741 seqNumber.push_back(aaP[i][j]);
2743 for(int j=nSeqsPerOTU[i];j<numSeqs;j++){
2744 aaP[i].push_back(0);
2750 for(int i=1;i<numOTUs;i++){
2751 cumNumSeqs[i] = cumNumSeqs[i-1] + nSeqsPerOTU[i-1];
2754 seqIndex = seqNumber;
2761 catch(exception& e) {
2762 m->errorOut(e, "ShhherCommand", "getOTUData");
2766 /**************************************************************************************************/
2768 int ShhherCommand::calcCentroidsDriver(int numOTUs,
2769 vector<int>& cumNumSeqs,
2770 vector<int>& nSeqsPerOTU,
2771 vector<int>& seqIndex,
2772 vector<short>& change, //did the centroid sequence change? 0 = no; 1 = yes
2773 vector<int>& centroids, //the representative flowgram for each cluster m
2774 vector<double>& singleTau, //tMaster->adTau: 1-D Tau vector (1xnumSeqs)
2775 vector<int>& mapSeqToUnique,
2776 vector<short>& uniqueFlowgrams,
2777 vector<short>& flowDataIntI,
2778 vector<int>& lengths,
2780 vector<int>& seqNumber){
2782 //this function gets the most likely homopolymer length at a flow position for a group of sequences
2787 for(int i=0;i<numOTUs;i++){
2789 if (m->control_pressed) { break; }
2793 int minFlowGram = 100000000;
2794 double minFlowValue = 1e8;
2795 change[i] = 0; //FALSE
2797 for(int j=0;j<nSeqsPerOTU[i];j++){
2798 count += singleTau[seqNumber[cumNumSeqs[i] + j]];
2801 if(nSeqsPerOTU[i] > 0 && count > MIN_COUNT){
2802 vector<double> adF(nSeqsPerOTU[i]);
2803 vector<int> anL(nSeqsPerOTU[i]);
2805 for(int j=0;j<nSeqsPerOTU[i];j++){
2806 int index = cumNumSeqs[i] + j;
2807 int nI = seqIndex[index];
2808 int nIU = mapSeqToUnique[nI];
2811 for(k=0;k<position;k++){
2817 anL[position] = nIU;
2818 adF[position] = 0.0000;
2823 for(int j=0;j<nSeqsPerOTU[i];j++){
2824 int index = cumNumSeqs[i] + j;
2825 int nI = seqIndex[index];
2827 double tauValue = singleTau[seqNumber[index]];
2829 for(int k=0;k<position;k++){
2830 double dist = getDistToCentroid(anL[k], nI, lengths[nI], uniqueFlowgrams, flowDataIntI, numFlowCells);
2831 adF[k] += dist * tauValue;
2835 for(int j=0;j<position;j++){
2836 if(adF[j] < minFlowValue){
2838 minFlowValue = adF[j];
2842 if(centroids[i] != anL[minFlowGram]){
2844 centroids[i] = anL[minFlowGram];
2847 else if(centroids[i] != -1){
2855 catch(exception& e) {
2856 m->errorOut(e, "ShhherCommand", "calcCentroidsDriver");
2860 /**************************************************************************************************/
2862 double ShhherCommand::getDistToCentroid(int cent, int flow, int length, vector<short>& uniqueFlowgrams,
2863 vector<short>& flowDataIntI, int numFlowCells){
2866 int flowAValue = cent * numFlowCells;
2867 int flowBValue = flow * numFlowCells;
2871 for(int i=0;i<length;i++){
2872 dist += singleLookUp[uniqueFlowgrams[flowAValue] * NUMBINS + flowDataIntI[flowBValue]];
2877 return dist / (double)length;
2879 catch(exception& e) {
2880 m->errorOut(e, "ShhherCommand", "getDistToCentroid");
2884 /**************************************************************************************************/
2886 double ShhherCommand::getNewWeights(int numOTUs, vector<int>& cumNumSeqs, vector<int>& nSeqsPerOTU, vector<double>& singleTau, vector<int>& seqNumber, vector<double>& weight){
2889 double maxChange = 0;
2891 for(int i=0;i<numOTUs;i++){
2893 if (m->control_pressed) { break; }
2895 double difference = weight[i];
2898 for(int j=0;j<nSeqsPerOTU[i];j++){
2899 int index = cumNumSeqs[i] + j;
2900 double tauValue = singleTau[seqNumber[index]];
2901 weight[i] += tauValue;
2904 difference = fabs(weight[i] - difference);
2905 if(difference > maxChange){ maxChange = difference; }
2909 catch(exception& e) {
2910 m->errorOut(e, "ShhherCommand", "getNewWeights");
2915 /**************************************************************************************************/
2917 double ShhherCommand::getLikelihood(int numSeqs, int numOTUs, vector<int>& nSeqsPerOTU, vector<int>& seqNumber, vector<int>& cumNumSeqs, vector<int>& seqIndex, vector<double>& dist, vector<double>& weight){
2921 vector<long double> P(numSeqs, 0);
2924 for(int i=0;i<numOTUs;i++){
2925 if(weight[i] > MIN_WEIGHT){
2931 for(int i=0;i<numOTUs;i++){
2933 if (m->control_pressed) { break; }
2935 for(int j=0;j<nSeqsPerOTU[i];j++){
2936 int index = cumNumSeqs[i] + j;
2937 int nI = seqIndex[index];
2938 double singleDist = dist[seqNumber[index]];
2940 P[nI] += weight[i] * exp(-singleDist * sigma);
2944 for(int i=0;i<numSeqs;i++){
2945 if(P[i] == 0){ P[i] = DBL_EPSILON; }
2950 nLL = nLL -(double)numSeqs * log(sigma);
2954 catch(exception& e) {
2955 m->errorOut(e, "ShhherCommand", "getNewWeights");
2960 /**************************************************************************************************/
2962 int ShhherCommand::checkCentroids(int numOTUs, vector<int>& centroids, vector<double>& weight){
2964 vector<int> unique(numOTUs, 1);
2966 for(int i=0;i<numOTUs;i++){
2967 if(centroids[i] == -1 || weight[i] < MIN_WEIGHT){
2972 for(int i=0;i<numOTUs;i++){
2974 if (m->control_pressed) { break; }
2977 for(int j=i+1;j<numOTUs;j++){
2980 if(centroids[j] == centroids[i]){
2984 weight[i] += weight[j];
2994 catch(exception& e) {
2995 m->errorOut(e, "ShhherCommand", "checkCentroids");
2999 /**************************************************************************************************/
3001 void ShhherCommand::calcNewDistances(int numSeqs, int numOTUs, vector<int>& nSeqsPerOTU, vector<double>& dist,
3002 vector<double>& weight, vector<short>& change, vector<int>& centroids,
3003 vector<vector<int> >& aaP, vector<double>& singleTau, vector<vector<int> >& aaI,
3004 vector<int>& seqNumber, vector<int>& seqIndex,
3005 vector<short>& uniqueFlowgrams,
3006 vector<short>& flowDataIntI, int numFlowCells, vector<int>& lengths){
3011 vector<double> newTau(numOTUs,0);
3012 vector<double> norms(numSeqs, 0);
3013 nSeqsPerOTU.assign(numOTUs, 0);
3015 for(int i=0;i<numSeqs;i++){
3017 if (m->control_pressed) { break; }
3019 int indexOffset = i * numOTUs;
3021 double offset = 1e8;
3023 for(int j=0;j<numOTUs;j++){
3025 if(weight[j] > MIN_WEIGHT && change[j] == 1){
3026 dist[indexOffset + j] = getDistToCentroid(centroids[j], i, lengths[i], uniqueFlowgrams, flowDataIntI, numFlowCells);
3029 if(weight[j] > MIN_WEIGHT && dist[indexOffset + j] < offset){
3030 offset = dist[indexOffset + j];
3034 for(int j=0;j<numOTUs;j++){
3035 if(weight[j] > MIN_WEIGHT){
3036 newTau[j] = exp(sigma * (-dist[indexOffset + j] + offset)) * weight[j];
3037 norms[i] += newTau[j];
3044 for(int j=0;j<numOTUs;j++){
3045 newTau[j] /= norms[i];
3048 for(int j=0;j<numOTUs;j++){
3049 if(newTau[j] > MIN_TAU){
3051 int oldTotal = total;
3055 singleTau.resize(total, 0);
3056 seqNumber.resize(total, 0);
3057 seqIndex.resize(total, 0);
3059 singleTau[oldTotal] = newTau[j];
3061 aaP[j][nSeqsPerOTU[j]] = oldTotal;
3062 aaI[j][nSeqsPerOTU[j]] = i;
3070 catch(exception& e) {
3071 m->errorOut(e, "ShhherCommand", "calcNewDistances");
3075 /**************************************************************************************************/
3077 int ShhherCommand::fill(int numOTUs, vector<int>& seqNumber, vector<int>& seqIndex, vector<int>& cumNumSeqs, vector<int>& nSeqsPerOTU, vector<vector<int> >& aaP, vector<vector<int> >& aaI){
3080 for(int i=0;i<numOTUs;i++){
3082 if (m->control_pressed) { return 0; }
3084 cumNumSeqs[i] = index;
3085 for(int j=0;j<nSeqsPerOTU[i];j++){
3086 seqNumber[index] = aaP[i][j];
3087 seqIndex[index] = aaI[i][j];
3095 catch(exception& e) {
3096 m->errorOut(e, "ShhherCommand", "fill");
3100 /**************************************************************************************************/
3102 void ShhherCommand::setOTUs(int numOTUs, int numSeqs, vector<int>& seqNumber, vector<int>& seqIndex, vector<int>& cumNumSeqs, vector<int>& nSeqsPerOTU,
3103 vector<int>& otuData, vector<double>& singleTau, vector<double>& dist, vector<vector<int> >& aaP, vector<vector<int> >& aaI){
3106 vector<double> bigTauMatrix(numOTUs * numSeqs, 0.0000);
3108 for(int i=0;i<numOTUs;i++){
3110 if (m->control_pressed) { break; }
3112 for(int j=0;j<nSeqsPerOTU[i];j++){
3113 int index = cumNumSeqs[i] + j;
3114 double tauValue = singleTau[seqNumber[index]];
3115 int sIndex = seqIndex[index];
3116 bigTauMatrix[sIndex * numOTUs + i] = tauValue;
3120 for(int i=0;i<numSeqs;i++){
3121 double maxTau = -1.0000;
3123 for(int j=0;j<numOTUs;j++){
3124 if(bigTauMatrix[i * numOTUs + j] > maxTau){
3125 maxTau = bigTauMatrix[i * numOTUs + j];
3130 otuData[i] = maxOTU;
3133 nSeqsPerOTU.assign(numOTUs, 0);
3135 for(int i=0;i<numSeqs;i++){
3136 int index = otuData[i];
3138 singleTau[i] = 1.0000;
3141 aaP[index][nSeqsPerOTU[index]] = i;
3142 aaI[index][nSeqsPerOTU[index]] = i;
3144 nSeqsPerOTU[index]++;
3147 fill(numOTUs, seqNumber, seqIndex, cumNumSeqs, nSeqsPerOTU, aaP, aaI);
3149 catch(exception& e) {
3150 m->errorOut(e, "ShhherCommand", "setOTUs");
3154 /**************************************************************************************************/
3156 void ShhherCommand::writeQualities(int numOTUs, int numFlowCells, string qualityFileName, vector<int> otuCounts, vector<int>& nSeqsPerOTU, vector<int>& seqNumber,
3157 vector<double>& singleTau, vector<short>& flowDataIntI, vector<short>& uniqueFlowgrams, vector<int>& cumNumSeqs,
3158 vector<int>& mapUniqueToSeq, vector<string>& seqNameVector, vector<int>& centroids, vector<vector<int> >& aaI){
3162 ofstream qualityFile;
3163 m->openOutputFile(qualityFileName, qualityFile);
3165 qualityFile.setf(ios::fixed, ios::floatfield);
3166 qualityFile.setf(ios::showpoint);
3167 qualityFile << setprecision(6);
3169 vector<vector<int> > qualities(numOTUs);
3170 vector<double> pr(HOMOPS, 0);
3173 for(int i=0;i<numOTUs;i++){
3175 if (m->control_pressed) { break; }
3180 if(nSeqsPerOTU[i] > 0){
3181 qualities[i].assign(1024, -1);
3183 while(index < numFlowCells){
3184 double maxPrValue = 1e8;
3185 short maxPrIndex = -1;
3186 double count = 0.0000;
3188 pr.assign(HOMOPS, 0);
3190 for(int j=0;j<nSeqsPerOTU[i];j++){
3191 int lIndex = cumNumSeqs[i] + j;
3192 double tauValue = singleTau[seqNumber[lIndex]];
3193 int sequenceIndex = aaI[i][j];
3194 short intensity = flowDataIntI[sequenceIndex * numFlowCells + index];
3198 for(int s=0;s<HOMOPS;s++){
3199 pr[s] += tauValue * singleLookUp[s * NUMBINS + intensity];
3203 maxPrIndex = uniqueFlowgrams[centroids[i] * numFlowCells + index];
3204 maxPrValue = pr[maxPrIndex];
3206 if(count > MIN_COUNT){
3208 double norm = 0.0000;
3210 for(int s=0;s<HOMOPS;s++){
3211 norm += exp(-(pr[s] - maxPrValue));
3214 for(int s=1;s<=maxPrIndex;s++){
3216 double temp = 0.0000;
3218 U += exp(-(pr[s-1]-maxPrValue))/norm;
3226 temp = floor(-10 * temp);
3227 value = (int)floor(temp);
3228 if(value > 100){ value = 100; }
3230 qualities[i][base] = (int)value;
3240 if(otuCounts[i] > 0){
3241 qualityFile << '>' << seqNameVector[mapUniqueToSeq[i]] << endl;
3243 int j=4; //need to get past the first four bases
3244 while(qualities[i][j] != -1){
3245 qualityFile << qualities[i][j] << ' ';
3246 if (j > qualities[i].size()) { break; }
3249 qualityFile << endl;
3252 qualityFile.close();
3253 outputNames.push_back(qualityFileName);
3256 catch(exception& e) {
3257 m->errorOut(e, "ShhherCommand", "writeQualities");
3262 /**************************************************************************************************/
3264 void ShhherCommand::writeSequences(string thisCompositeFASTAFileName, int numOTUs, int numFlowCells, string fastaFileName, vector<int> otuCounts, vector<short>& uniqueFlowgrams, vector<string>& seqNameVector, vector<vector<int> >& aaI, vector<int>& centroids){
3268 m->openOutputFile(fastaFileName, fastaFile);
3270 vector<string> names(numOTUs, "");
3272 for(int i=0;i<numOTUs;i++){
3274 if (m->control_pressed) { break; }
3276 int index = centroids[i];
3278 if(otuCounts[i] > 0){
3279 fastaFile << '>' << seqNameVector[aaI[i][0]] << endl;
3283 for(int j=0;j<numFlowCells;j++){
3285 char base = flowOrder[j % 4];
3286 for(int k=0;k<uniqueFlowgrams[index * numFlowCells + j];k++){
3291 if (newSeq.length() >= 4) { fastaFile << newSeq.substr(4) << endl; }
3292 else { fastaFile << "NNNN" << endl; }
3297 outputNames.push_back(fastaFileName);
3299 if(thisCompositeFASTAFileName != ""){
3300 m->appendFiles(fastaFileName, thisCompositeFASTAFileName);
3303 catch(exception& e) {
3304 m->errorOut(e, "ShhherCommand", "writeSequences");
3309 /**************************************************************************************************/
3311 void ShhherCommand::writeNames(string thisCompositeNamesFileName, int numOTUs, string nameFileName, vector<int> otuCounts, vector<string>& seqNameVector, vector<vector<int> >& aaI, vector<int>& nSeqsPerOTU){
3315 m->openOutputFile(nameFileName, nameFile);
3317 for(int i=0;i<numOTUs;i++){
3319 if (m->control_pressed) { break; }
3321 if(otuCounts[i] > 0){
3322 nameFile << seqNameVector[aaI[i][0]] << '\t' << seqNameVector[aaI[i][0]];
3324 for(int j=1;j<nSeqsPerOTU[i];j++){
3325 nameFile << ',' << seqNameVector[aaI[i][j]];
3332 outputNames.push_back(nameFileName);
3335 if(thisCompositeNamesFileName != ""){
3336 m->appendFiles(nameFileName, thisCompositeNamesFileName);
3339 catch(exception& e) {
3340 m->errorOut(e, "ShhherCommand", "writeNames");
3345 /**************************************************************************************************/
3347 void ShhherCommand::writeGroups(string groupFileName, string fileRoot, int numSeqs, vector<string>& seqNameVector){
3350 m->openOutputFile(groupFileName, groupFile);
3352 for(int i=0;i<numSeqs;i++){
3353 if (m->control_pressed) { break; }
3354 groupFile << seqNameVector[i] << '\t' << fileRoot << endl;
3357 outputNames.push_back(groupFileName);
3360 catch(exception& e) {
3361 m->errorOut(e, "ShhherCommand", "writeGroups");
3366 /**************************************************************************************************/
3368 void ShhherCommand::writeClusters(string otuCountsFileName, int numOTUs, int numFlowCells, vector<int> otuCounts, vector<int>& centroids, vector<short>& uniqueFlowgrams, vector<string>& seqNameVector, vector<vector<int> >& aaI, vector<int>& nSeqsPerOTU, vector<int>& lengths, vector<short>& flowDataIntI){
3370 ofstream otuCountsFile;
3371 m->openOutputFile(otuCountsFileName, otuCountsFile);
3373 string bases = flowOrder;
3375 for(int i=0;i<numOTUs;i++){
3377 if (m->control_pressed) {
3380 //output the translated version of the centroid sequence for the otu
3381 if(otuCounts[i] > 0){
3382 int index = centroids[i];
3384 otuCountsFile << "ideal\t";
3385 for(int j=8;j<numFlowCells;j++){
3386 char base = bases[j % 4];
3387 for(int s=0;s<uniqueFlowgrams[index * numFlowCells + j];s++){
3388 otuCountsFile << base;
3391 otuCountsFile << endl;
3393 for(int j=0;j<nSeqsPerOTU[i];j++){
3394 int sequence = aaI[i][j];
3395 otuCountsFile << seqNameVector[sequence] << '\t';
3399 for(int k=0;k<lengths[sequence];k++){
3400 char base = bases[k % 4];
3401 int freq = int(0.01 * (double)flowDataIntI[sequence * numFlowCells + k] + 0.5);
3403 for(int s=0;s<freq;s++){
3405 //otuCountsFile << base;
3409 if (newSeq.length() >= 4) { otuCountsFile << newSeq.substr(4) << endl; }
3410 else { otuCountsFile << "NNNN" << endl; }
3412 otuCountsFile << endl;
3415 otuCountsFile.close();
3416 outputNames.push_back(otuCountsFileName);
3419 catch(exception& e) {
3420 m->errorOut(e, "ShhherCommand", "writeClusters");
3425 /**************************************************************************************************/
3427 void ShhherCommand::getSingleLookUp(){
3429 // these are the -log probabilities that a signal corresponds to a particular homopolymer length
3430 singleLookUp.assign(HOMOPS * NUMBINS, 0);
3433 ifstream lookUpFile;
3434 m->openInputFile(lookupFileName, lookUpFile);
3436 for(int i=0;i<HOMOPS;i++){
3438 if (m->control_pressed) { break; }
3441 lookUpFile >> logFracFreq;
3443 for(int j=0;j<NUMBINS;j++) {
3444 lookUpFile >> singleLookUp[index];
3450 catch(exception& e) {
3451 m->errorOut(e, "ShhherCommand", "getSingleLookUp");
3456 /**************************************************************************************************/
3458 void ShhherCommand::getJointLookUp(){
3461 // the most likely joint probability (-log) that two intenities have the same polymer length
3462 jointLookUp.resize(NUMBINS * NUMBINS, 0);
3464 for(int i=0;i<NUMBINS;i++){
3466 if (m->control_pressed) { break; }
3468 for(int j=0;j<NUMBINS;j++){
3470 double minSum = 100000000;
3472 for(int k=0;k<HOMOPS;k++){
3473 double sum = singleLookUp[k * NUMBINS + i] + singleLookUp[k * NUMBINS + j];
3475 if(sum < minSum) { minSum = sum; }
3477 jointLookUp[i * NUMBINS + j] = minSum;
3481 catch(exception& e) {
3482 m->errorOut(e, "ShhherCommand", "getJointLookUp");
3487 /**************************************************************************************************/
3489 double ShhherCommand::getProbIntensity(int intIntensity){
3492 double minNegLogProb = 100000000;
3495 for(int i=0;i<HOMOPS;i++){//loop signal strength
3497 if (m->control_pressed) { break; }
3499 float negLogProb = singleLookUp[i * NUMBINS + intIntensity];
3500 if(negLogProb < minNegLogProb) { minNegLogProb = negLogProb; }
3503 return minNegLogProb;
3505 catch(exception& e) {
3506 m->errorOut(e, "ShhherCommand", "getProbIntensity");