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","fasta-name-group-counts-qfile",false,false,true); parameters.push_back(pflow);
16 CommandParameter pfile("file", "InputTypes", "", "", "none", "fileflow", "none","fasta-name-group-counts-qfile",false,false,true); parameters.push_back(pfile);
17 CommandParameter plookup("lookup", "InputTypes", "", "", "none", "none", "none","",false,false,true); 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,true); 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", "Multiple", "A-B-I", "A", "", "", "","",false,false, true); parameters.push_back(porder); CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
25 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
27 vector<string> myArray;
28 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
32 m->errorOut(e, "ShhherCommand", "setParameters");
36 //**********************************************************************************************************************
37 string ShhherCommand::getHelpString(){
39 string helpString = "";
40 helpString += "The shhh.flows command reads a file containing flowgrams and creates a file of corrected sequences.\n";
41 helpString += "The shhh.flows command parameters are flow, file, lookup, cutoff, processors, large, maxiter, sigma, mindelta and order.\n";
42 helpString += "The flow parameter is used to input your flow file.\n";
43 helpString += "The file parameter is used to input the *flow.files file created by trim.flows.\n";
44 helpString += "The lookup parameter is used specify the lookup file you would like to use. http://www.mothur.org/wiki/Lookup_files.\n";
45 helpString += "The order parameter options are A, B or I. Default=A. A = TACG and B = TACGTACGTACGATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGC and I = TACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGC.\n";
49 m->errorOut(e, "ShhherCommand", "getHelpString");
53 //**********************************************************************************************************************
54 string ShhherCommand::getOutputPattern(string type) {
58 if (type == "fasta") { pattern = "[filename],shhh.fasta"; }
59 else if (type == "name") { pattern = "[filename],shhh.names"; }
60 else if (type == "group") { pattern = "[filename],shhh.groups"; }
61 else if (type == "counts") { pattern = "[filename],shhh.counts"; }
62 else if (type == "qfile") { pattern = "[filename],shhh.qual"; }
63 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
68 m->errorOut(e, "ShhherCommand", "getOutputPattern");
72 //**********************************************************************************************************************
74 ShhherCommand::ShhherCommand(){
76 abort = true; calledHelp = true;
79 //initialize outputTypes
80 vector<string> tempOutNames;
81 outputTypes["fasta"] = tempOutNames;
82 outputTypes["name"] = tempOutNames;
83 outputTypes["group"] = tempOutNames;
84 outputTypes["counts"] = tempOutNames;
85 outputTypes["qfile"] = tempOutNames;
89 m->errorOut(e, "ShhherCommand", "ShhherCommand");
94 //**********************************************************************************************************************
96 ShhherCommand::ShhherCommand(string option) {
100 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
101 MPI_Comm_size(MPI_COMM_WORLD, &ncpus);
105 abort = false; calledHelp = false;
107 //allow user to run help
108 if(option == "help") { help(); abort = true; calledHelp = true; }
109 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
112 vector<string> myArray = setParameters();
114 OptionParser parser(option);
115 map<string,string> parameters = parser.getParameters();
117 ValidParameters validParameter;
118 map<string,string>::iterator it;
120 //check to make sure all parameters are valid for command
121 for (it = parameters.begin(); it != parameters.end(); it++) {
122 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
125 //initialize outputTypes
126 vector<string> tempOutNames;
127 outputTypes["fasta"] = tempOutNames;
128 outputTypes["name"] = tempOutNames;
129 outputTypes["group"] = tempOutNames;
130 outputTypes["counts"] = tempOutNames;
131 outputTypes["qfile"] = tempOutNames;
134 //if the user changes the input directory command factory will send this info to us in the output parameter
135 string inputDir = validParameter.validFile(parameters, "inputdir", false);
136 if (inputDir == "not found"){ inputDir = ""; }
139 it = parameters.find("flow");
140 //user has given a template file
141 if(it != parameters.end()){
142 path = m->hasPath(it->second);
143 //if the user has not given a path then, add inputdir. else leave path alone.
144 if (path == "") { parameters["flow"] = inputDir + it->second; }
147 it = parameters.find("lookup");
148 //user has given a template file
149 if(it != parameters.end()){
150 path = m->hasPath(it->second);
151 //if the user has not given a path then, add inputdir. else leave path alone.
152 if (path == "") { parameters["lookup"] = inputDir + it->second; }
155 it = parameters.find("file");
156 //user has given a template file
157 if(it != parameters.end()){
158 path = m->hasPath(it->second);
159 //if the user has not given a path then, add inputdir. else leave path alone.
160 if (path == "") { parameters["file"] = inputDir + it->second; }
164 //if the user changes the output directory command factory will send this info to us in the output parameter
165 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
167 //check for required parameters
168 flowFileName = validParameter.validFile(parameters, "flow", true);
169 flowFilesFileName = validParameter.validFile(parameters, "file", true);
170 if (flowFileName == "not found" && flowFilesFileName == "not found") {
171 m->mothurOut("values for either flow or file must be provided for the shhh.flows command.");
172 m->mothurOutEndLine();
175 else if (flowFileName == "not open" || flowFilesFileName == "not open") { abort = true; }
177 if(flowFileName != "not found"){
178 compositeFASTAFileName = "";
179 compositeNamesFileName = "";
184 string thisoutputDir = outputDir;
185 if (outputDir == "") { thisoutputDir = m->hasPath(flowFilesFileName); } //if user entered a file with a path then preserve it
187 //we want to rip off .files, and also .flow if its there
188 string fileroot = m->getRootName(m->getSimpleName(flowFilesFileName));
189 if (fileroot[fileroot.length()-1] == '.') { fileroot = fileroot.substr(0, fileroot.length()-1); } //rip off dot
190 string extension = m->getExtension(fileroot);
191 if (extension == ".flow") { fileroot = m->getRootName(fileroot); }
192 else { fileroot += "."; } //add back if needed
194 compositeFASTAFileName = thisoutputDir + fileroot + "shhh.fasta";
195 m->openOutputFile(compositeFASTAFileName, temp);
198 compositeNamesFileName = thisoutputDir + fileroot + "shhh.names";
199 m->openOutputFile(compositeNamesFileName, temp);
203 if(flowFilesFileName != "not found"){
206 ifstream flowFilesFile;
207 m->openInputFile(flowFilesFileName, flowFilesFile);
208 while(flowFilesFile){
209 fName = m->getline(flowFilesFile);
211 //test if file is valid
213 int ableToOpen = m->openInputFile(fName, in, "noerror");
215 if (ableToOpen == 1) {
216 if (inputDir != "") { //default path is set
217 string tryPath = inputDir + fName;
218 m->mothurOut("Unable to open " + fName + ". Trying input directory " + tryPath); m->mothurOutEndLine();
220 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
226 if (ableToOpen == 1) {
227 if (m->getDefaultPath() != "") { //default path is set
228 string tryPath = m->getDefaultPath() + m->getSimpleName(fName);
229 m->mothurOut("Unable to open " + fName + ". Trying default " + tryPath); m->mothurOutEndLine();
231 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
237 //if you can't open it its not in current working directory or inputDir, try mothur excutable location
238 if (ableToOpen == 1) {
239 string exepath = m->argv;
240 string tempPath = exepath;
241 for (int i = 0; i < exepath.length(); i++) { tempPath[i] = tolower(exepath[i]); }
242 exepath = exepath.substr(0, (tempPath.find_last_of('m')));
244 string tryPath = m->getFullPathName(exepath) + m->getSimpleName(fName);
245 m->mothurOut("Unable to open " + fName + ". Trying mothur's executable location " + tryPath); m->mothurOutEndLine();
247 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
252 if (ableToOpen == 1) { m->mothurOut("Unable to open " + fName + ". Disregarding. "); m->mothurOutEndLine(); }
253 else { flowFileVector.push_back(fName); }
254 m->gobble(flowFilesFile);
256 flowFilesFile.close();
257 if (flowFileVector.size() == 0) { m->mothurOut("[ERROR]: no valid files."); m->mothurOutEndLine(); abort = true; }
260 if (outputDir == "") { outputDir = m->hasPath(flowFileName); }
261 flowFileVector.push_back(flowFileName);
264 //check for optional parameter and set defaults
265 // ...at some point should added some additional type checking...
267 temp = validParameter.validFile(parameters, "lookup", true);
268 if (temp == "not found") {
269 string path = m->argv;
270 string tempPath = path;
271 for (int i = 0; i < path.length(); i++) { tempPath[i] = tolower(path[i]); }
272 path = path.substr(0, (tempPath.find_last_of('m')));
274 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
275 path += "lookupFiles/";
277 path += "lookupFiles\\";
279 lookupFileName = m->getFullPathName(path) + "LookUp_Titanium.pat";
283 ableToOpen = m->openInputFile(lookupFileName, in, "noerror");
286 //if you can't open it, try input location
287 if (ableToOpen == 1) {
288 if (inputDir != "") { //default path is set
289 string tryPath = inputDir + m->getSimpleName(lookupFileName);
290 m->mothurOut("Unable to open " + lookupFileName + ". Trying input directory " + tryPath); m->mothurOutEndLine();
292 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
294 lookupFileName = tryPath;
298 //if you can't open it, try default location
299 if (ableToOpen == 1) {
300 if (m->getDefaultPath() != "") { //default path is set
301 string tryPath = m->getDefaultPath() + m->getSimpleName(lookupFileName);
302 m->mothurOut("Unable to open " + lookupFileName + ". Trying default " + tryPath); m->mothurOutEndLine();
304 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
306 lookupFileName = tryPath;
310 //if you can't open it its not in current working directory or inputDir, try mothur excutable location
311 if (ableToOpen == 1) {
312 string exepath = m->argv;
313 string tempPath = exepath;
314 for (int i = 0; i < exepath.length(); i++) { tempPath[i] = tolower(exepath[i]); }
315 exepath = exepath.substr(0, (tempPath.find_last_of('m')));
317 string tryPath = m->getFullPathName(exepath) + m->getSimpleName(lookupFileName);
318 m->mothurOut("Unable to open " + lookupFileName + ". Trying mothur's executable location " + tryPath); m->mothurOutEndLine();
320 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
322 lookupFileName = tryPath;
325 if (ableToOpen == 1) { m->mothurOut("Unable to open " + lookupFileName + "."); m->mothurOutEndLine(); abort=true; }
327 else if(temp == "not open") {
329 lookupFileName = validParameter.validFile(parameters, "lookup", false);
331 //if you can't open it its not inputDir, try mothur excutable location
332 string exepath = m->argv;
333 string tempPath = exepath;
334 for (int i = 0; i < exepath.length(); i++) { tempPath[i] = tolower(exepath[i]); }
335 exepath = exepath.substr(0, (tempPath.find_last_of('m')));
337 string tryPath = m->getFullPathName(exepath) + m->getSimpleName(lookupFileName);
338 m->mothurOut("Unable to open " + lookupFileName + ". Trying mothur's executable location " + tryPath); m->mothurOutEndLine();
340 int ableToOpen = m->openInputFile(tryPath, in2, "noerror");
342 lookupFileName = tryPath;
344 if (ableToOpen == 1) { m->mothurOut("Unable to open " + lookupFileName + "."); m->mothurOutEndLine(); abort=true; }
345 }else { lookupFileName = temp; }
347 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
348 m->setProcessors(temp);
349 m->mothurConvert(temp, processors);
351 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found"){ temp = "0.01"; }
352 m->mothurConvert(temp, cutoff);
354 temp = validParameter.validFile(parameters, "mindelta", false); if (temp == "not found"){ temp = "0.000001"; }
355 m->mothurConvert(temp, minDelta);
357 temp = validParameter.validFile(parameters, "maxiter", false); if (temp == "not found"){ temp = "1000"; }
358 m->mothurConvert(temp, maxIters);
360 temp = validParameter.validFile(parameters, "large", false); if (temp == "not found"){ temp = "0"; }
361 m->mothurConvert(temp, largeSize);
362 if (largeSize != 0) { large = true; }
363 else { large = false; }
364 if (largeSize < 0) { m->mothurOut("The value of the large cannot be negative.\n"); }
367 if (large) { m->mothurOut("The large parameter is not available with the MPI-Enabled version.\n"); large=false; }
371 temp = validParameter.validFile(parameters, "sigma", false);if (temp == "not found") { temp = "60"; }
372 m->mothurConvert(temp, sigma);
374 temp = validParameter.validFile(parameters, "order", false); if (temp == "not found"){ temp = "A"; }
375 if (temp.length() > 1) { m->mothurOut("[ERROR]: " + temp + " is not a valid option for order. order options are A, B, or I. A = TACG, B = TACGTACGTACGATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGC, and I = TACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGC.\n"); abort=true;
378 if (toupper(temp[0]) == 'A') { flowOrder = "TACG"; }
379 else if(toupper(temp[0]) == 'B'){
380 flowOrder = "TACGTACGTACGATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGC"; }
381 else if(toupper(temp[0]) == 'I'){
382 flowOrder = "TACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGC"; }
384 m->mothurOut("[ERROR]: " + temp + " is not a valid option for order. order options are A, B, or I. A = TACG, B = TACGTACGTACGATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATAGATCGCATGACGATCGCATATCGTCAGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGTAGTCGAGCATCATCTGACGCAGTACGTGCATGATCTCAGTCAGCAGCTATGTCAGTGCATGCATAGATCGCATGACGATCGCATATCGTCAGTGCAGTGACTGATCGTCATCAGCTAGCATCGACTGCATGATCTCAGTCAGCAGC, and I = TACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGCTACGTACGTCTGAGCATCGATCGATGTACAGC.\n"); abort=true;
394 catch(exception& e) {
395 m->errorOut(e, "ShhherCommand", "ShhherCommand");
399 //**********************************************************************************************************************
401 int ShhherCommand::execute(){
403 if (abort == true) { if (calledHelp) { return 0; } return 2; }
410 for(int i=1;i<ncpus;i++){
411 MPI_Send(&abort, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
413 if(abort == 1){ return 0; }
417 m->mothurOut("\nGetting preliminary data...\n");
418 getSingleLookUp(); if (m->control_pressed) { return 0; }
419 getJointLookUp(); if (m->control_pressed) { return 0; }
421 vector<string> flowFileVector;
422 if(flowFilesFileName != "not found"){
425 ifstream flowFilesFile;
426 m->openInputFile(flowFilesFileName, flowFilesFile);
427 while(flowFilesFile){
428 fName = m->getline(flowFilesFile);
429 flowFileVector.push_back(fName);
430 m->gobble(flowFilesFile);
434 flowFileVector.push_back(flowFileName);
437 int numFiles = flowFileVector.size();
439 for(int i=1;i<ncpus;i++){
440 MPI_Send(&numFiles, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
443 for(int i=0;i<numFiles;i++){
445 if (m->control_pressed) { break; }
447 double begClock = clock();
448 unsigned long long begTime = time(NULL);
450 flowFileName = flowFileVector[i];
452 m->mothurOut("\n>>>>>\tProcessing " + flowFileName + " (file " + toString(i+1) + " of " + toString(numFiles) + ")\t<<<<<\n");
453 m->mothurOut("Reading flowgrams...\n");
456 if (m->control_pressed) { break; }
458 m->mothurOut("Identifying unique flowgrams...\n");
461 if (m->control_pressed) { break; }
463 m->mothurOut("Calculating distances between flowgrams...\n");
465 strcpy(fileName, flowFileName.c_str());
467 for(int i=1;i<ncpus;i++){
468 MPI_Send(&fileName[0], 1024, MPI_CHAR, i, tag, MPI_COMM_WORLD);
470 MPI_Send(&numSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
471 MPI_Send(&numUniques, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
472 MPI_Send(&numFlowCells, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
473 MPI_Send(&flowDataIntI[0], numSeqs * numFlowCells, MPI_SHORT, i, tag, MPI_COMM_WORLD);
474 MPI_Send(&flowDataPrI[0], numSeqs * numFlowCells, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
475 MPI_Send(&mapUniqueToSeq[0], numSeqs, MPI_INT, i, tag, MPI_COMM_WORLD);
476 MPI_Send(&mapSeqToUnique[0], numSeqs, MPI_INT, i, tag, MPI_COMM_WORLD);
477 MPI_Send(&lengths[0], numSeqs, MPI_INT, i, tag, MPI_COMM_WORLD);
478 MPI_Send(&jointLookUp[0], NUMBINS * NUMBINS, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
479 MPI_Send(&cutoff, 1, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
482 string distFileName = flowDistMPI(0, int(sqrt(1.0/float(ncpus)) * numUniques));
484 if (m->control_pressed) { break; }
487 for(int i=1;i<ncpus;i++){
488 MPI_Recv(&done, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
490 m->appendFiles((distFileName + ".temp." + toString(i)), distFileName);
491 m->mothurRemove((distFileName + ".temp." + toString(i)));
494 string namesFileName = createNamesFile();
496 if (m->control_pressed) { break; }
498 m->mothurOut("\nClustering flowgrams...\n");
499 string listFileName = cluster(distFileName, namesFileName);
501 if (m->control_pressed) { break; }
505 getOTUData(listFileName);
507 m->mothurRemove(distFileName);
508 m->mothurRemove(namesFileName);
509 m->mothurRemove(listFileName);
511 if (m->control_pressed) { break; }
515 if (m->control_pressed) { break; }
518 for(int i=1;i<ncpus;i++){
519 MPI_Send(&numOTUs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
520 MPI_Send(&singleLookUp[0], singleLookUp.size(), MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
521 MPI_Send(&uniqueFlowgrams[0], numFlowCells * numUniques, MPI_SHORT, i, tag, MPI_COMM_WORLD);
522 MPI_Send(&sigma, 1, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
525 if (m->control_pressed) { break; }
530 int numOTUsOnCPU = numOTUs / ncpus;
531 int numSeqsOnCPU = numSeqs / ncpus;
532 m->mothurOut("\nDenoising flowgrams...\n");
533 m->mothurOut("iter\tmaxDelta\tnLL\t\tcycletime\n");
535 while((maxIters == 0 && maxDelta > minDelta) || iter < MIN_ITER || (maxDelta > minDelta && iter < maxIters)){
537 double cycClock = clock();
538 unsigned long long cycTime = time(NULL);
541 if (m->control_pressed) { break; }
543 int total = singleTau.size();
544 for(int i=1;i<ncpus;i++){
545 MPI_Send(&total, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
546 MPI_Send(&change[0], numOTUs, MPI_SHORT, i, tag, MPI_COMM_WORLD);
547 MPI_Send(¢roids[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
549 MPI_Send(&singleTau[0], total, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
550 MPI_Send(&seqNumber[0], total, MPI_INT, i, tag, MPI_COMM_WORLD);
551 MPI_Send(&seqIndex[0], total, MPI_INT, i, tag, MPI_COMM_WORLD);
552 MPI_Send(&nSeqsPerOTU[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
553 MPI_Send(&cumNumSeqs[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
556 calcCentroidsDriver(0, numOTUsOnCPU);
558 for(int i=1;i<ncpus;i++){
559 int otuStart = i * numOTUs / ncpus;
560 int otuStop = (i + 1) * numOTUs / ncpus;
562 vector<int> tempCentroids(numOTUs, 0);
563 vector<short> tempChange(numOTUs, 0);
565 MPI_Recv(&tempCentroids[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
566 MPI_Recv(&tempChange[0], numOTUs, MPI_SHORT, i, tag, MPI_COMM_WORLD, &status);
568 for(int j=otuStart;j<otuStop;j++){
569 centroids[j] = tempCentroids[j];
570 change[j] = tempChange[j];
574 maxDelta = getNewWeights(); if (m->control_pressed) { break; }
575 double nLL = getLikelihood(); if (m->control_pressed) { break; }
576 checkCentroids(); if (m->control_pressed) { break; }
578 for(int i=1;i<ncpus;i++){
579 MPI_Send(¢roids[0], numOTUs, MPI_INT, i, tag, MPI_COMM_WORLD);
580 MPI_Send(&weight[0], numOTUs, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
581 MPI_Send(&change[0], numOTUs, MPI_SHORT, i, tag, MPI_COMM_WORLD);
584 calcNewDistancesParent(0, numSeqsOnCPU);
586 total = singleTau.size();
588 for(int i=1;i<ncpus;i++){
590 int seqStart = i * numSeqs / ncpus;
591 int seqStop = (i + 1) * numSeqs / ncpus;
593 MPI_Recv(&childTotal, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
595 vector<int> childSeqIndex(childTotal, 0);
596 vector<double> childSingleTau(childTotal, 0);
597 vector<double> childDist(numSeqs * numOTUs, 0);
598 vector<int> otuIndex(childTotal, 0);
600 MPI_Recv(&childSeqIndex[0], childTotal, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
601 MPI_Recv(&childSingleTau[0], childTotal, MPI_DOUBLE, i, tag, MPI_COMM_WORLD, &status);
602 MPI_Recv(&childDist[0], numOTUs * numSeqs, MPI_DOUBLE, i, tag, MPI_COMM_WORLD, &status);
603 MPI_Recv(&otuIndex[0], childTotal, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
605 int oldTotal = total;
607 singleTau.resize(total, 0);
608 seqIndex.resize(total, 0);
609 seqNumber.resize(total, 0);
613 for(int j=oldTotal;j<total;j++){
614 int otuI = otuIndex[childIndex];
615 int seqI = childSeqIndex[childIndex];
617 singleTau[j] = childSingleTau[childIndex];
619 aaP[otuI][nSeqsPerOTU[otuI]] = j;
620 aaI[otuI][nSeqsPerOTU[otuI]] = seqI;
625 int index = seqStart * numOTUs;
626 for(int j=seqStart;j<seqStop;j++){
627 for(int k=0;k<numOTUs;k++){
628 dist[index] = childDist[index];
636 m->mothurOut(toString(iter) + '\t' + toString(maxDelta) + '\t' + toString(nLL) + '\t' + toString(time(NULL) - cycTime) + '\t' + toString((clock() - cycClock)/(double)CLOCKS_PER_SEC) + '\n');
638 if((maxIters == 0 && maxDelta > minDelta) || iter < MIN_ITER || (maxDelta > minDelta && iter < maxIters)){
640 for(int i=1;i<ncpus;i++){
641 MPI_Send(&live, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
646 for(int i=1;i<ncpus;i++){
647 MPI_Send(&live, 1, MPI_INT, i, tag, MPI_COMM_WORLD); //send kill command
653 if (m->control_pressed) { break; }
655 m->mothurOut("\nFinalizing...\n");
658 if (m->control_pressed) { break; }
662 vector<int> otuCounts(numOTUs, 0);
663 for(int i=0;i<numSeqs;i++) { otuCounts[otuData[i]]++; }
664 calcCentroidsDriver(0, numOTUs);
666 if (m->control_pressed) { break; }
668 writeQualities(otuCounts); if (m->control_pressed) { break; }
669 writeSequences(otuCounts); if (m->control_pressed) { break; }
670 writeNames(otuCounts); if (m->control_pressed) { break; }
671 writeClusters(otuCounts); if (m->control_pressed) { break; }
672 writeGroups(); if (m->control_pressed) { break; }
675 m->mothurOut("Total time to process " + toString(flowFileName) + ":\t" + toString(time(NULL) - begTime) + '\t' + toString((clock() - begClock)/(double)CLOCKS_PER_SEC) + '\n');
681 MPI_Recv(&abort, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
682 if(abort){ return 0; }
685 MPI_Recv(&numFiles, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
687 for(int i=0;i<numFiles;i++){
689 if (m->control_pressed) { break; }
691 //Now into the pyrodist part
695 MPI_Recv(&fileName, 1024, MPI_CHAR, 0, tag, MPI_COMM_WORLD, &status);
696 MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
697 MPI_Recv(&numUniques, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
698 MPI_Recv(&numFlowCells, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
700 flowDataIntI.resize(numSeqs * numFlowCells);
701 flowDataPrI.resize(numSeqs * numFlowCells);
702 mapUniqueToSeq.resize(numSeqs);
703 mapSeqToUnique.resize(numSeqs);
704 lengths.resize(numSeqs);
705 jointLookUp.resize(NUMBINS * NUMBINS);
707 MPI_Recv(&flowDataIntI[0], numSeqs * numFlowCells, MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
708 MPI_Recv(&flowDataPrI[0], numSeqs * numFlowCells, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
709 MPI_Recv(&mapUniqueToSeq[0], numSeqs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
710 MPI_Recv(&mapSeqToUnique[0], numSeqs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
711 MPI_Recv(&lengths[0], numSeqs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
712 MPI_Recv(&jointLookUp[0], NUMBINS * NUMBINS, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
713 MPI_Recv(&cutoff, 1, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
715 flowFileName = string(fileName);
716 int flowDistStart = int(sqrt(float(pid)/float(ncpus)) * numUniques);
717 int flowDistEnd = int(sqrt(float(pid+1)/float(ncpus)) * numUniques);
719 string distanceStringChild = flowDistMPI(flowDistStart, flowDistEnd);
721 if (m->control_pressed) { break; }
724 MPI_Send(&done, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
726 //Now into the pyronoise part
727 MPI_Recv(&numOTUs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
729 singleLookUp.resize(HOMOPS * NUMBINS);
730 uniqueFlowgrams.resize(numUniques * numFlowCells);
731 weight.resize(numOTUs);
732 centroids.resize(numOTUs);
733 change.resize(numOTUs);
734 dist.assign(numOTUs * numSeqs, 0);
735 nSeqsPerOTU.resize(numOTUs);
736 cumNumSeqs.resize(numOTUs);
738 MPI_Recv(&singleLookUp[0], singleLookUp.size(), MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
739 MPI_Recv(&uniqueFlowgrams[0], uniqueFlowgrams.size(), MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
740 MPI_Recv(&sigma, 1, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
742 int startOTU = pid * numOTUs / ncpus;
743 int endOTU = (pid + 1) * numOTUs / ncpus;
745 int startSeq = pid * numSeqs / ncpus;
746 int endSeq = (pid + 1) * numSeqs /ncpus;
752 if (m->control_pressed) { break; }
754 MPI_Recv(&total, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
755 singleTau.assign(total, 0.0000);
756 seqNumber.assign(total, 0);
757 seqIndex.assign(total, 0);
759 MPI_Recv(&change[0], numOTUs, MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
760 MPI_Recv(¢roids[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
761 MPI_Recv(&singleTau[0], total, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
762 MPI_Recv(&seqNumber[0], total, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
763 MPI_Recv(&seqIndex[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
764 MPI_Recv(&nSeqsPerOTU[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
765 MPI_Recv(&cumNumSeqs[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
767 calcCentroidsDriver(startOTU, endOTU);
769 MPI_Send(¢roids[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD);
770 MPI_Send(&change[0], numOTUs, MPI_SHORT, 0, tag, MPI_COMM_WORLD);
772 MPI_Recv(¢roids[0], numOTUs, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
773 MPI_Recv(&weight[0], numOTUs, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
774 MPI_Recv(&change[0], numOTUs, MPI_SHORT, 0, tag, MPI_COMM_WORLD, &status);
776 vector<int> otuIndex(total, 0);
777 calcNewDistancesChildMPI(startSeq, endSeq, otuIndex);
778 total = otuIndex.size();
780 MPI_Send(&total, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
781 MPI_Send(&seqIndex[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD);
782 MPI_Send(&singleTau[0], total, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD);
783 MPI_Send(&dist[0], numOTUs * numSeqs, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD);
784 MPI_Send(&otuIndex[0], total, MPI_INT, 0, tag, MPI_COMM_WORLD);
786 MPI_Recv(&live, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
791 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
793 MPI_Barrier(MPI_COMM_WORLD);
796 if(compositeFASTAFileName != ""){
797 outputNames.push_back(compositeFASTAFileName); outputTypes["fasta"].push_back(compositeFASTAFileName);
798 outputNames.push_back(compositeNamesFileName); outputTypes["name"].push_back(compositeNamesFileName);
801 m->mothurOutEndLine();
802 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
803 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
804 m->mothurOutEndLine();
809 catch(exception& e) {
810 m->errorOut(e, "ShhherCommand", "execute");
814 /**************************************************************************************************/
815 string ShhherCommand::createNamesFile(){
818 vector<string> duplicateNames(numUniques, "");
819 for(int i=0;i<numSeqs;i++){
820 duplicateNames[mapSeqToUnique[i]] += seqNameVector[i] + ',';
822 map<string, string> variables;
823 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(flowFileName));
824 string nameFileName = getOutputFileName("name",variables);
827 m->openOutputFile(nameFileName, nameFile);
829 for(int i=0;i<numUniques;i++){
831 if (m->control_pressed) { break; }
833 // nameFile << seqNameVector[mapUniqueToSeq[i]] << '\t' << duplicateNames[i].substr(0, duplicateNames[i].find_last_of(',')) << endl;
834 nameFile << mapUniqueToSeq[i] << '\t' << duplicateNames[i].substr(0, duplicateNames[i].find_last_of(',')) << endl;
840 catch(exception& e) {
841 m->errorOut(e, "ShhherCommand", "createNamesFile");
845 /**************************************************************************************************/
847 string ShhherCommand::flowDistMPI(int startSeq, int stopSeq){
849 ostringstream outStream;
850 outStream.setf(ios::fixed, ios::floatfield);
851 outStream.setf(ios::dec, ios::basefield);
852 outStream.setf(ios::showpoint);
853 outStream.precision(6);
855 int begTime = time(NULL);
856 double begClock = clock();
858 for(int i=startSeq;i<stopSeq;i++){
860 if (m->control_pressed) { break; }
862 for(int j=0;j<i;j++){
863 float flowDistance = calcPairwiseDist(mapUniqueToSeq[i], mapUniqueToSeq[j]);
865 if(flowDistance < 1e-6){
866 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << 0.000000 << endl;
868 else if(flowDistance <= cutoff){
869 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << flowDistance << endl;
873 m->mothurOutJustToScreen(toString(i) + '\t' + toString(time(NULL) - begTime) + '\t' + toString((clock()-begClock)/CLOCKS_PER_SEC) + '\n');
877 string fDistFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.dist";
878 if(pid != 0){ fDistFileName += ".temp." + toString(pid); }
880 if (m->control_pressed) { return fDistFileName; }
882 m->mothurOutJustToScreen(toString(stopSeq) + '\t' + toString(time(NULL) - begTime) + '\t' + toString((clock()-begClock)/CLOCKS_PER_SEC) + '\n');
884 ofstream distFile(fDistFileName.c_str());
885 distFile << outStream.str();
888 return fDistFileName;
890 catch(exception& e) {
891 m->errorOut(e, "ShhherCommand", "flowDistMPI");
895 /**************************************************************************************************/
897 void ShhherCommand::getOTUData(string listFileName){
901 m->openInputFile(listFileName, listFile);
904 listFile >> label >> numOTUs;
906 otuData.assign(numSeqs, 0);
907 cumNumSeqs.assign(numOTUs, 0);
908 nSeqsPerOTU.assign(numOTUs, 0);
909 aaP.clear();aaP.resize(numOTUs);
915 string singleOTU = "";
917 for(int i=0;i<numOTUs;i++){
919 if (m->control_pressed) { break; }
921 listFile >> singleOTU;
923 istringstream otuString(singleOTU);
929 for(int j=0;j<singleOTU.length();j++){
930 char letter = otuString.get();
936 map<string,int>::iterator nmIt = nameMap.find(seqName);
937 int index = nmIt->second;
943 aaP[i].push_back(index);
948 map<string,int>::iterator nmIt = nameMap.find(seqName);
950 int index = nmIt->second;
955 aaP[i].push_back(index);
960 sort(aaP[i].begin(), aaP[i].end());
961 for(int j=0;j<nSeqsPerOTU[i];j++){
962 seqNumber.push_back(aaP[i][j]);
964 for(int j=nSeqsPerOTU[i];j<numSeqs;j++){
971 for(int i=1;i<numOTUs;i++){
972 cumNumSeqs[i] = cumNumSeqs[i-1] + nSeqsPerOTU[i-1];
975 seqIndex = seqNumber;
980 catch(exception& e) {
981 m->errorOut(e, "ShhherCommand", "getOTUData");
986 /**************************************************************************************************/
988 void ShhherCommand::initPyroCluster(){
990 if (numOTUs < processors) { processors = 1; }
992 if (m->debug) { m->mothurOut("[DEBUG]: numSeqs = " + toString(numSeqs) + " numOTUS = " + toString(numOTUs) + " about to alloc a dist vector with size = " + toString((numSeqs * numOTUs)) + ".\n"); }
994 dist.assign(numSeqs * numOTUs, 0);
995 change.assign(numOTUs, 1);
996 centroids.assign(numOTUs, -1);
997 weight.assign(numOTUs, 0);
998 singleTau.assign(numSeqs, 1.0);
1000 nSeqsBreaks.assign(processors+1, 0);
1001 nOTUsBreaks.assign(processors+1, 0);
1003 if (m->debug) { m->mothurOut("[DEBUG]: made it through the memory allocation.\n"); }
1006 for(int i=0;i<processors;i++){
1007 nSeqsBreaks[i+1] = nSeqsBreaks[i] + (int)((double) numSeqs / (double) processors);
1008 nOTUsBreaks[i+1] = nOTUsBreaks[i] + (int)((double) numOTUs / (double) processors);
1010 nSeqsBreaks[processors] = numSeqs;
1011 nOTUsBreaks[processors] = numOTUs;
1013 catch(exception& e) {
1014 m->errorOut(e, "ShhherCommand", "initPyroCluster");
1019 /**************************************************************************************************/
1021 void ShhherCommand::fill(){
1024 for(int i=0;i<numOTUs;i++){
1026 if (m->control_pressed) { break; }
1028 cumNumSeqs[i] = index;
1029 for(int j=0;j<nSeqsPerOTU[i];j++){
1030 seqNumber[index] = aaP[i][j];
1031 seqIndex[index] = aaI[i][j];
1037 catch(exception& e) {
1038 m->errorOut(e, "ShhherCommand", "fill");
1043 /**************************************************************************************************/
1045 void ShhherCommand::getFlowData(){
1048 m->openInputFile(flowFileName, flowFile);
1051 seqNameVector.clear();
1053 flowDataIntI.clear();
1057 int currentNumFlowCells;
1062 flowFile >> numFlowTest;
1064 if (!m->isContainingOnlyDigits(numFlowTest)) { m->mothurOut("[ERROR]: expected a number and got " + numFlowTest + ", quitting. Did you use the flow parameter instead of the file parameter?"); m->mothurOutEndLine(); exit(1); }
1065 else { convert(numFlowTest, numFlowCells); }
1067 int index = 0;//pcluster
1068 while(!flowFile.eof()){
1070 if (m->control_pressed) { break; }
1072 flowFile >> seqName >> currentNumFlowCells;
1073 lengths.push_back(currentNumFlowCells);
1075 seqNameVector.push_back(seqName);
1076 nameMap[seqName] = index++;//pcluster
1078 for(int i=0;i<numFlowCells;i++){
1079 flowFile >> intensity;
1080 if(intensity > 9.99) { intensity = 9.99; }
1081 int intI = int(100 * intensity + 0.0001);
1082 flowDataIntI.push_back(intI);
1084 m->gobble(flowFile);
1088 numSeqs = seqNameVector.size();
1090 for(int i=0;i<numSeqs;i++){
1092 if (m->control_pressed) { break; }
1094 int iNumFlowCells = i * numFlowCells;
1095 for(int j=lengths[i];j<numFlowCells;j++){
1096 flowDataIntI[iNumFlowCells + j] = 0;
1101 catch(exception& e) {
1102 m->errorOut(e, "ShhherCommand", "getFlowData");
1106 /**************************************************************************************************/
1107 void ShhherCommand::calcNewDistancesChildMPI(int startSeq, int stopSeq, vector<int>& otuIndex){
1110 vector<double> newTau(numOTUs,0);
1111 vector<double> norms(numSeqs, 0);
1116 for(int i=startSeq;i<stopSeq;i++){
1118 if (m->control_pressed) { break; }
1120 double offset = 1e8;
1121 int indexOffset = i * numOTUs;
1123 for(int j=0;j<numOTUs;j++){
1125 if(weight[j] > MIN_WEIGHT && change[j] == 1){
1126 dist[indexOffset + j] = getDistToCentroid(centroids[j], i, lengths[i]);
1128 if(weight[j] > MIN_WEIGHT && dist[indexOffset + j] < offset){
1129 offset = dist[indexOffset + j];
1133 for(int j=0;j<numOTUs;j++){
1134 if(weight[j] > MIN_WEIGHT){
1135 newTau[j] = exp(sigma * (-dist[indexOffset + j] + offset)) * weight[j];
1136 norms[i] += newTau[j];
1143 for(int j=0;j<numOTUs;j++){
1145 newTau[j] /= norms[i];
1147 if(newTau[j] > MIN_TAU){
1148 otuIndex.push_back(j);
1149 seqIndex.push_back(i);
1150 singleTau.push_back(newTau[j]);
1156 catch(exception& e) {
1157 m->errorOut(e, "ShhherCommand", "calcNewDistancesChildMPI");
1162 /**************************************************************************************************/
1164 void ShhherCommand::calcNewDistancesParent(int startSeq, int stopSeq){
1169 vector<double> newTau(numOTUs,0);
1170 vector<double> norms(numSeqs, 0);
1171 nSeqsPerOTU.assign(numOTUs, 0);
1173 for(int i=startSeq;i<stopSeq;i++){
1175 if (m->control_pressed) { break; }
1177 int indexOffset = i * numOTUs;
1179 double offset = 1e8;
1181 for(int j=0;j<numOTUs;j++){
1183 if(weight[j] > MIN_WEIGHT && change[j] == 1){
1184 dist[indexOffset + j] = getDistToCentroid(centroids[j], i, lengths[i]);
1187 if(weight[j] > MIN_WEIGHT && dist[indexOffset + j] < offset){
1188 offset = dist[indexOffset + j];
1192 for(int j=0;j<numOTUs;j++){
1193 if(weight[j] > MIN_WEIGHT){
1194 newTau[j] = exp(sigma * (-dist[indexOffset + j] + offset)) * weight[j];
1195 norms[i] += newTau[j];
1202 for(int j=0;j<numOTUs;j++){
1203 newTau[j] /= norms[i];
1206 for(int j=0;j<numOTUs;j++){
1207 if(newTau[j] > MIN_TAU){
1209 int oldTotal = total;
1213 singleTau.resize(total, 0);
1214 seqNumber.resize(total, 0);
1215 seqIndex.resize(total, 0);
1217 singleTau[oldTotal] = newTau[j];
1219 aaP[j][nSeqsPerOTU[j]] = oldTotal;
1220 aaI[j][nSeqsPerOTU[j]] = i;
1228 catch(exception& e) {
1229 m->errorOut(e, "ShhherCommand", "calcNewDistancesParent");
1234 /**************************************************************************************************/
1236 void ShhherCommand::setOTUs(){
1239 vector<double> bigTauMatrix(numOTUs * numSeqs, 0.0000);
1241 for(int i=0;i<numOTUs;i++){
1243 if (m->control_pressed) { break; }
1245 for(int j=0;j<nSeqsPerOTU[i];j++){
1246 int index = cumNumSeqs[i] + j;
1247 double tauValue = singleTau[seqNumber[index]];
1248 int sIndex = seqIndex[index];
1249 bigTauMatrix[sIndex * numOTUs + i] = tauValue;
1253 for(int i=0;i<numSeqs;i++){
1254 double maxTau = -1.0000;
1256 for(int j=0;j<numOTUs;j++){
1257 if(bigTauMatrix[i * numOTUs + j] > maxTau){
1258 maxTau = bigTauMatrix[i * numOTUs + j];
1263 otuData[i] = maxOTU;
1266 nSeqsPerOTU.assign(numOTUs, 0);
1268 for(int i=0;i<numSeqs;i++){
1269 int index = otuData[i];
1271 singleTau[i] = 1.0000;
1274 aaP[index][nSeqsPerOTU[index]] = i;
1275 aaI[index][nSeqsPerOTU[index]] = i;
1277 nSeqsPerOTU[index]++;
1281 catch(exception& e) {
1282 m->errorOut(e, "ShhherCommand", "setOTUs");
1287 /**************************************************************************************************/
1289 void ShhherCommand::getUniques(){
1294 uniqueFlowgrams.assign(numFlowCells * numSeqs, -1);
1295 uniqueCount.assign(numSeqs, 0); // anWeights
1296 uniqueLengths.assign(numSeqs, 0);
1297 mapSeqToUnique.assign(numSeqs, -1);
1298 mapUniqueToSeq.assign(numSeqs, -1);
1300 vector<short> uniqueFlowDataIntI(numFlowCells * numSeqs, -1);
1302 for(int i=0;i<numSeqs;i++){
1304 if (m->control_pressed) { break; }
1308 vector<short> current(numFlowCells);
1309 for(int j=0;j<numFlowCells;j++){
1310 current[j] = short(((flowDataIntI[i * numFlowCells + j] + 50.0)/100.0));
1313 for(int j=0;j<numUniques;j++){
1314 int offset = j * numFlowCells;
1318 if(lengths[i] < uniqueLengths[j]) { shorterLength = lengths[i]; }
1319 else { shorterLength = uniqueLengths[j]; }
1321 for(int k=0;k<shorterLength;k++){
1322 if(current[k] != uniqueFlowgrams[offset + k]){
1329 mapSeqToUnique[i] = j;
1332 if(lengths[i] > uniqueLengths[j]) { uniqueLengths[j] = lengths[i]; }
1338 if(index == numUniques){
1339 uniqueLengths[numUniques] = lengths[i];
1340 uniqueCount[numUniques] = 1;
1341 mapSeqToUnique[i] = numUniques;//anMap
1342 mapUniqueToSeq[numUniques] = i;//anF
1344 for(int k=0;k<numFlowCells;k++){
1345 uniqueFlowgrams[numUniques * numFlowCells + k] = current[k];
1346 uniqueFlowDataIntI[numUniques * numFlowCells + k] = flowDataIntI[i * numFlowCells + k];
1352 uniqueFlowDataIntI.resize(numFlowCells * numUniques);
1353 uniqueLengths.resize(numUniques);
1355 flowDataPrI.resize(numSeqs * numFlowCells, 0);
1356 for(int i=0;i<flowDataPrI.size();i++) { if (m->control_pressed) { break; } flowDataPrI[i] = getProbIntensity(flowDataIntI[i]); }
1358 catch(exception& e) {
1359 m->errorOut(e, "ShhherCommand", "getUniques");
1364 /**************************************************************************************************/
1366 float ShhherCommand::calcPairwiseDist(int seqA, int seqB){
1368 int minLength = lengths[mapSeqToUnique[seqA]];
1369 if(lengths[seqB] < minLength){ minLength = lengths[mapSeqToUnique[seqB]]; }
1371 int ANumFlowCells = seqA * numFlowCells;
1372 int BNumFlowCells = seqB * numFlowCells;
1376 for(int i=0;i<minLength;i++){
1378 if (m->control_pressed) { break; }
1380 int flowAIntI = flowDataIntI[ANumFlowCells + i];
1381 float flowAPrI = flowDataPrI[ANumFlowCells + i];
1383 int flowBIntI = flowDataIntI[BNumFlowCells + i];
1384 float flowBPrI = flowDataPrI[BNumFlowCells + i];
1385 dist += jointLookUp[flowAIntI * NUMBINS + flowBIntI] - flowAPrI - flowBPrI;
1388 dist /= (float) minLength;
1391 catch(exception& e) {
1392 m->errorOut(e, "ShhherCommand", "calcPairwiseDist");
1397 //**********************************************************************************************************************/
1399 string ShhherCommand::cluster(string distFileName, string namesFileName){
1402 ReadMatrix* read = new ReadColumnMatrix(distFileName);
1403 read->setCutoff(cutoff);
1405 NameAssignment* clusterNameMap = new NameAssignment(namesFileName);
1406 clusterNameMap->readMap();
1407 read->read(clusterNameMap);
1409 ListVector* list = read->getListVector();
1410 SparseDistanceMatrix* matrix = read->getDMatrix();
1413 delete clusterNameMap;
1415 RAbundVector* rabund = new RAbundVector(list->getRAbundVector());
1417 Cluster* cluster = new CompleteLinkage(rabund, list, matrix, cutoff, "furthest");
1418 string tag = cluster->getTag();
1420 double clusterCutoff = cutoff;
1421 while (matrix->getSmallDist() <= clusterCutoff && matrix->getNNodes() > 0){
1423 if (m->control_pressed) { break; }
1425 cluster->update(clusterCutoff);
1428 list->setLabel(toString(cutoff));
1430 string listFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.list";
1432 m->openOutputFile(listFileName, listFile);
1433 list->print(listFile);
1436 delete matrix; delete cluster; delete rabund; delete list;
1438 return listFileName;
1440 catch(exception& e) {
1441 m->errorOut(e, "ShhherCommand", "cluster");
1446 /**************************************************************************************************/
1448 void ShhherCommand::calcCentroidsDriver(int start, int finish){
1450 //this function gets the most likely homopolymer length at a flow position for a group of sequences
1455 for(int i=start;i<finish;i++){
1457 if (m->control_pressed) { break; }
1461 int minFlowGram = 100000000;
1462 double minFlowValue = 1e8;
1463 change[i] = 0; //FALSE
1465 for(int j=0;j<nSeqsPerOTU[i];j++){
1466 count += singleTau[seqNumber[cumNumSeqs[i] + j]];
1469 if(nSeqsPerOTU[i] > 0 && count > MIN_COUNT){
1470 vector<double> adF(nSeqsPerOTU[i]);
1471 vector<int> anL(nSeqsPerOTU[i]);
1473 for(int j=0;j<nSeqsPerOTU[i];j++){
1474 int index = cumNumSeqs[i] + j;
1475 int nI = seqIndex[index];
1476 int nIU = mapSeqToUnique[nI];
1479 for(k=0;k<position;k++){
1485 anL[position] = nIU;
1486 adF[position] = 0.0000;
1491 for(int j=0;j<nSeqsPerOTU[i];j++){
1492 int index = cumNumSeqs[i] + j;
1493 int nI = seqIndex[index];
1495 double tauValue = singleTau[seqNumber[index]];
1497 for(int k=0;k<position;k++){
1498 double dist = getDistToCentroid(anL[k], nI, lengths[nI]);
1499 adF[k] += dist * tauValue;
1503 for(int j=0;j<position;j++){
1504 if(adF[j] < minFlowValue){
1506 minFlowValue = adF[j];
1510 if(centroids[i] != anL[minFlowGram]){
1512 centroids[i] = anL[minFlowGram];
1515 else if(centroids[i] != -1){
1521 catch(exception& e) {
1522 m->errorOut(e, "ShhherCommand", "calcCentroidsDriver");
1527 /**************************************************************************************************/
1529 double ShhherCommand::getDistToCentroid(int cent, int flow, int length){
1532 int flowAValue = cent * numFlowCells;
1533 int flowBValue = flow * numFlowCells;
1537 for(int i=0;i<length;i++){
1538 dist += singleLookUp[uniqueFlowgrams[flowAValue] * NUMBINS + flowDataIntI[flowBValue]];
1543 return dist / (double)length;
1545 catch(exception& e) {
1546 m->errorOut(e, "ShhherCommand", "getDistToCentroid");
1551 /**************************************************************************************************/
1553 double ShhherCommand::getNewWeights(){
1556 double maxChange = 0;
1558 for(int i=0;i<numOTUs;i++){
1560 if (m->control_pressed) { break; }
1562 double difference = weight[i];
1565 for(int j=0;j<nSeqsPerOTU[i];j++){
1566 int index = cumNumSeqs[i] + j;
1567 double tauValue = singleTau[seqNumber[index]];
1568 weight[i] += tauValue;
1571 difference = fabs(weight[i] - difference);
1572 if(difference > maxChange){ maxChange = difference; }
1576 catch(exception& e) {
1577 m->errorOut(e, "ShhherCommand", "getNewWeights");
1582 /**************************************************************************************************/
1584 double ShhherCommand::getLikelihood(){
1588 vector<long double> P(numSeqs, 0);
1591 for(int i=0;i<numOTUs;i++){
1592 if(weight[i] > MIN_WEIGHT){
1598 for(int i=0;i<numOTUs;i++){
1600 if (m->control_pressed) { break; }
1602 for(int j=0;j<nSeqsPerOTU[i];j++){
1603 int index = cumNumSeqs[i] + j;
1604 int nI = seqIndex[index];
1605 double singleDist = dist[seqNumber[index]];
1607 P[nI] += weight[i] * exp(-singleDist * sigma);
1611 for(int i=0;i<numSeqs;i++){
1612 if(P[i] == 0){ P[i] = DBL_EPSILON; }
1617 nLL = nLL -(double)numSeqs * log(sigma);
1621 catch(exception& e) {
1622 m->errorOut(e, "ShhherCommand", "getNewWeights");
1627 /**************************************************************************************************/
1629 void ShhherCommand::checkCentroids(){
1631 vector<int> unique(numOTUs, 1);
1633 for(int i=0;i<numOTUs;i++){
1634 if(centroids[i] == -1 || weight[i] < MIN_WEIGHT){
1639 for(int i=0;i<numOTUs;i++){
1641 if (m->control_pressed) { break; }
1644 for(int j=i+1;j<numOTUs;j++){
1647 if(centroids[j] == centroids[i]){
1651 weight[i] += weight[j];
1659 catch(exception& e) {
1660 m->errorOut(e, "ShhherCommand", "checkCentroids");
1664 /**************************************************************************************************/
1668 void ShhherCommand::writeQualities(vector<int> otuCounts){
1671 string thisOutputDir = outputDir;
1672 if (outputDir == "") { thisOutputDir += m->hasPath(flowFileName); }
1673 map<string, string> variables;
1674 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(flowFileName));
1675 string qualityFileName = getOutputFileName("qfile",variables);
1677 ofstream qualityFile;
1678 m->openOutputFile(qualityFileName, qualityFile);
1680 qualityFile.setf(ios::fixed, ios::floatfield);
1681 qualityFile.setf(ios::showpoint);
1682 qualityFile << setprecision(6);
1684 vector<vector<int> > qualities(numOTUs);
1685 vector<double> pr(HOMOPS, 0);
1688 for(int i=0;i<numOTUs;i++){
1690 if (m->control_pressed) { break; }
1695 if(nSeqsPerOTU[i] > 0){
1696 qualities[i].assign(1024, -1);
1698 while(index < numFlowCells){
1699 double maxPrValue = 1e8;
1700 short maxPrIndex = -1;
1701 double count = 0.0000;
1703 pr.assign(HOMOPS, 0);
1705 for(int j=0;j<nSeqsPerOTU[i];j++){
1706 int lIndex = cumNumSeqs[i] + j;
1707 double tauValue = singleTau[seqNumber[lIndex]];
1708 int sequenceIndex = aaI[i][j];
1709 short intensity = flowDataIntI[sequenceIndex * numFlowCells + index];
1713 for(int s=0;s<HOMOPS;s++){
1714 pr[s] += tauValue * singleLookUp[s * NUMBINS + intensity];
1718 maxPrIndex = uniqueFlowgrams[centroids[i] * numFlowCells + index];
1719 maxPrValue = pr[maxPrIndex];
1721 if(count > MIN_COUNT){
1723 double norm = 0.0000;
1725 for(int s=0;s<HOMOPS;s++){
1726 norm += exp(-(pr[s] - maxPrValue));
1729 for(int s=1;s<=maxPrIndex;s++){
1731 double temp = 0.0000;
1733 U += exp(-(pr[s-1]-maxPrValue))/norm;
1741 temp = floor(-10 * temp);
1742 value = (int)floor(temp);
1743 if(value > 100){ value = 100; }
1745 qualities[i][base] = (int)value;
1755 if(otuCounts[i] > 0){
1756 qualityFile << '>' << seqNameVector[mapUniqueToSeq[i]] << endl;
1758 int j=4; //need to get past the first four bases
1759 while(qualities[i][j] != -1){
1760 qualityFile << qualities[i][j] << ' ';
1763 qualityFile << endl;
1766 qualityFile.close();
1767 outputNames.push_back(qualityFileName); outputTypes["qfile"].push_back(qualityFileName);
1770 catch(exception& e) {
1771 m->errorOut(e, "ShhherCommand", "writeQualities");
1776 /**************************************************************************************************/
1778 void ShhherCommand::writeSequences(vector<int> otuCounts){
1780 string thisOutputDir = outputDir;
1781 if (outputDir == "") { thisOutputDir += m->hasPath(flowFileName); }
1782 map<string, string> variables;
1783 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName));
1784 string fastaFileName = getOutputFileName("fasta",variables);
1786 m->openOutputFile(fastaFileName, fastaFile);
1788 vector<string> names(numOTUs, "");
1790 for(int i=0;i<numOTUs;i++){
1792 if (m->control_pressed) { break; }
1794 int index = centroids[i];
1796 if(otuCounts[i] > 0){
1797 fastaFile << '>' << seqNameVector[aaI[i][0]] << endl;
1801 for(int j=0;j<numFlowCells;j++){
1803 char base = flowOrder[j % flowOrder.length()];
1804 for(int k=0;k<uniqueFlowgrams[index * numFlowCells + j];k++){
1809 fastaFile << newSeq.substr(4) << endl;
1814 outputNames.push_back(fastaFileName); outputTypes["fasta"].push_back(fastaFileName);
1816 if(compositeFASTAFileName != ""){
1817 m->appendFiles(fastaFileName, compositeFASTAFileName);
1820 catch(exception& e) {
1821 m->errorOut(e, "ShhherCommand", "writeSequences");
1826 /**************************************************************************************************/
1828 void ShhherCommand::writeNames(vector<int> otuCounts){
1830 string thisOutputDir = outputDir;
1831 if (outputDir == "") { thisOutputDir += m->hasPath(flowFileName); }
1832 map<string, string> variables;
1833 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName));
1834 string nameFileName = getOutputFileName("name",variables);
1836 m->openOutputFile(nameFileName, nameFile);
1838 for(int i=0;i<numOTUs;i++){
1840 if (m->control_pressed) { break; }
1842 if(otuCounts[i] > 0){
1843 nameFile << seqNameVector[aaI[i][0]] << '\t' << seqNameVector[aaI[i][0]];
1845 for(int j=1;j<nSeqsPerOTU[i];j++){
1846 nameFile << ',' << seqNameVector[aaI[i][j]];
1853 outputNames.push_back(nameFileName); outputTypes["name"].push_back(nameFileName);
1856 if(compositeNamesFileName != ""){
1857 m->appendFiles(nameFileName, compositeNamesFileName);
1860 catch(exception& e) {
1861 m->errorOut(e, "ShhherCommand", "writeNames");
1866 /**************************************************************************************************/
1868 void ShhherCommand::writeGroups(){
1870 string thisOutputDir = outputDir;
1871 if (outputDir == "") { thisOutputDir += m->hasPath(flowFileName); }
1872 string fileRoot = m->getRootName(m->getSimpleName(flowFileName));
1873 int pos = fileRoot.find_first_of('.');
1874 string fileGroup = fileRoot;
1875 if (pos != string::npos) { fileGroup = fileRoot.substr(pos+1, (fileRoot.length()-1-(pos+1))); }
1876 map<string, string> variables;
1877 variables["[filename]"] = thisOutputDir + fileRoot;
1878 string groupFileName = getOutputFileName("group",variables);
1880 m->openOutputFile(groupFileName, groupFile);
1882 for(int i=0;i<numSeqs;i++){
1883 if (m->control_pressed) { break; }
1884 groupFile << seqNameVector[i] << '\t' << fileGroup << endl;
1887 outputNames.push_back(groupFileName); outputTypes["group"].push_back(groupFileName);
1890 catch(exception& e) {
1891 m->errorOut(e, "ShhherCommand", "writeGroups");
1896 /**************************************************************************************************/
1898 void ShhherCommand::writeClusters(vector<int> otuCounts){
1900 string thisOutputDir = outputDir;
1901 if (outputDir == "") { thisOutputDir += m->hasPath(flowFileName); }
1902 map<string, string> variables;
1903 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName));
1904 string otuCountsFileName = getOutputFileName("counts",variables);
1905 ofstream otuCountsFile;
1906 m->openOutputFile(otuCountsFileName, otuCountsFile);
1908 string bases = flowOrder;
1910 for(int i=0;i<numOTUs;i++){
1912 if (m->control_pressed) {
1915 //output the translated version of the centroid sequence for the otu
1916 if(otuCounts[i] > 0){
1917 int index = centroids[i];
1919 otuCountsFile << "ideal\t";
1920 for(int j=8;j<numFlowCells;j++){
1921 char base = bases[j % bases.length()];
1922 for(int s=0;s<uniqueFlowgrams[index * numFlowCells + j];s++){
1923 otuCountsFile << base;
1926 otuCountsFile << endl;
1928 for(int j=0;j<nSeqsPerOTU[i];j++){
1929 int sequence = aaI[i][j];
1930 otuCountsFile << seqNameVector[sequence] << '\t';
1934 for(int k=0;k<lengths[sequence];k++){
1935 char base = bases[k % bases.length()];
1936 int freq = int(0.01 * (double)flowDataIntI[sequence * numFlowCells + k] + 0.5);
1938 for(int s=0;s<freq;s++){
1940 //otuCountsFile << base;
1943 otuCountsFile << newSeq.substr(4) << endl;
1945 otuCountsFile << endl;
1948 otuCountsFile.close();
1949 outputNames.push_back(otuCountsFileName); outputTypes["counts"].push_back(otuCountsFileName);
1952 catch(exception& e) {
1953 m->errorOut(e, "ShhherCommand", "writeClusters");
1959 //**********************************************************************************************************************
1961 int ShhherCommand::execute(){
1963 if (abort == true) { if (calledHelp) { return 0; } return 2; }
1965 getSingleLookUp(); if (m->control_pressed) { return 0; }
1966 getJointLookUp(); if (m->control_pressed) { return 0; }
1968 int numFiles = flowFileVector.size();
1970 if (numFiles < processors) { processors = numFiles; }
1972 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1973 if (processors == 1) { driver(flowFileVector, compositeFASTAFileName, compositeNamesFileName); }
1974 else { createProcesses(flowFileVector); } //each processor processes one file
1976 driver(flowFileVector, compositeFASTAFileName, compositeNamesFileName);
1979 if(compositeFASTAFileName != ""){
1980 outputNames.push_back(compositeFASTAFileName); outputTypes["fasta"].push_back(compositeFASTAFileName);
1981 outputNames.push_back(compositeNamesFileName); outputTypes["name"].push_back(compositeNamesFileName);
1984 m->mothurOutEndLine();
1985 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
1986 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
1987 m->mothurOutEndLine();
1991 catch(exception& e) {
1992 m->errorOut(e, "ShhherCommand", "execute");
1997 //********************************************************************************************************************
1998 //sorts biggest to smallest
1999 inline bool compareFileSizes(string left, string right){
2004 //get num bytes in file
2005 string filename = left;
2006 pFile = fopen (filename.c_str(),"rb");
2007 string error = "Error opening " + filename;
2008 if (pFile==NULL) perror (error.c_str());
2010 fseek (pFile, 0, SEEK_END);
2011 leftsize=ftell (pFile);
2018 //get num bytes in file
2020 pFile2 = fopen (filename.c_str(),"rb");
2021 error = "Error opening " + filename;
2022 if (pFile2==NULL) perror (error.c_str());
2024 fseek (pFile2, 0, SEEK_END);
2025 rightsize=ftell (pFile2);
2029 return (leftsize > rightsize);
2031 /**************************************************************************************************/
2033 int ShhherCommand::createProcesses(vector<string> filenames){
2035 vector<int> processIDS;
2040 if (filenames.size() < processors) { processors = filenames.size(); }
2042 //sort file names by size to divide load better
2043 sort(filenames.begin(), filenames.end(), compareFileSizes);
2045 vector < vector <string> > dividedFiles; //dividedFiles[1] = vector of filenames for process 1...
2046 dividedFiles.resize(processors);
2048 //for each file, figure out which process will complete it
2049 //want to divide the load intelligently so the big files are spread between processes
2050 for (int i = 0; i < filenames.size(); i++) {
2051 int processToAssign = (i+1) % processors;
2052 if (processToAssign == 0) { processToAssign = processors; }
2054 dividedFiles[(processToAssign-1)].push_back(filenames[i]);
2057 //now lets reverse the order of ever other process, so we balance big files running with little ones
2058 for (int i = 0; i < processors; i++) {
2059 int remainder = ((i+1) % processors);
2060 if (remainder) { reverse(dividedFiles[i].begin(), dividedFiles[i].end()); }
2064 //divide the groups between the processors
2065 /*vector<linePair> lines;
2066 vector<int> numFilesToComplete;
2067 int numFilesPerProcessor = filenames.size() / processors;
2068 for (int i = 0; i < processors; i++) {
2069 int startIndex = i * numFilesPerProcessor;
2070 int endIndex = (i+1) * numFilesPerProcessor;
2071 if(i == (processors - 1)){ endIndex = filenames.size(); }
2072 lines.push_back(linePair(startIndex, endIndex));
2073 numFilesToComplete.push_back((endIndex-startIndex));
2076 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
2078 //loop through and create all the processes you want
2079 while (process != processors) {
2083 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
2085 }else if (pid == 0){
2086 num = driver(dividedFiles[process], compositeFASTAFileName + toString(getpid()) + ".temp", compositeNamesFileName + toString(getpid()) + ".temp");
2088 //pass numSeqs to parent
2090 string tempFile = compositeFASTAFileName + toString(getpid()) + ".num.temp";
2091 m->openOutputFile(tempFile, out);
2097 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
2098 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
2104 driver(dividedFiles[0], compositeFASTAFileName, compositeNamesFileName);
2106 //force parent to wait until all the processes are done
2107 for (int i=0;i<processIDS.size();i++) {
2108 int temp = processIDS[i];
2114 //////////////////////////////////////////////////////////////////////////////////////////////////////
2116 /////////////////////// NOT WORKING, ACCESS VIOLATION ON READ OF FLOWGRAMS IN THREAD /////////////////
2118 //////////////////////////////////////////////////////////////////////////////////////////////////////
2119 //Windows version shared memory, so be careful when passing variables through the shhhFlowsData struct.
2120 //Above fork() will clone, so memory is separate, but that's not the case with windows,
2121 //////////////////////////////////////////////////////////////////////////////////////////////////////
2123 vector<shhhFlowsData*> pDataArray;
2124 DWORD dwThreadIdArray[processors-1];
2125 HANDLE hThreadArray[processors-1];
2127 //Create processor worker threads.
2128 for( int i=0; i<processors-1; i++ ){
2129 // Allocate memory for thread data.
2130 string extension = "";
2131 if (i != 0) { extension = toString(i) + ".temp"; }
2133 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);
2134 pDataArray.push_back(tempFlow);
2135 processIDS.push_back(i);
2137 hThreadArray[i] = CreateThread(NULL, 0, ShhhFlowsThreadFunction, pDataArray[i], 0, &dwThreadIdArray[i]);
2140 //using the main process as a worker saves time and memory
2142 driver(filenames, compositeFASTAFileName, compositeNamesFileName, lines[processors-1].start, lines[processors-1].end);
2144 //Wait until all threads have terminated.
2145 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
2147 //Close all thread handles and free memory allocations.
2148 for(int i=0; i < pDataArray.size(); i++){
2149 for(int j=0; j < pDataArray[i]->outputNames.size(); j++){ outputNames.push_back(pDataArray[i]->outputNames[j]); }
2150 CloseHandle(hThreadArray[i]);
2151 delete pDataArray[i];
2156 for (int i=0;i<processIDS.size();i++) {
2158 string tempFile = compositeFASTAFileName + toString(processIDS[i]) + ".num.temp";
2159 m->openInputFile(tempFile, in);
2163 if (tempNum != dividedFiles[i+1].size()) {
2164 m->mothurOut("[ERROR]: main process expected " + toString(processIDS[i]) + " to complete " + toString(dividedFiles[i+1].size()) + " 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");
2167 in.close(); m->mothurRemove(tempFile);
2169 if (compositeFASTAFileName != "") {
2170 m->appendFiles((compositeFASTAFileName + toString(processIDS[i]) + ".temp"), compositeFASTAFileName);
2171 m->appendFiles((compositeNamesFileName + toString(processIDS[i]) + ".temp"), compositeNamesFileName);
2172 m->mothurRemove((compositeFASTAFileName + toString(processIDS[i]) + ".temp"));
2173 m->mothurRemove((compositeNamesFileName + toString(processIDS[i]) + ".temp"));
2180 catch(exception& e) {
2181 m->errorOut(e, "ShhherCommand", "createProcesses");
2185 /**************************************************************************************************/
2187 vector<string> ShhherCommand::parseFlowFiles(string filename){
2189 vector<string> files;
2193 m->openInputFile(filename, in);
2195 int thisNumFLows = 0;
2196 in >> thisNumFLows; m->gobble(in);
2199 if (m->control_pressed) { break; }
2202 string outputFileName = filename + toString(count) + ".temp";
2203 m->openOutputFile(outputFileName, out);
2204 out << thisNumFLows << endl;
2205 files.push_back(outputFileName);
2207 int numLinesWrote = 0;
2208 for (int i = 0; i < largeSize; i++) {
2209 if (in.eof()) { break; }
2210 string line = m->getline(in); m->gobble(in);
2211 out << line << endl;
2216 if (numLinesWrote == 0) { m->mothurRemove(outputFileName); files.pop_back(); }
2221 if (m->control_pressed) { for (int i = 0; i < files.size(); i++) { m->mothurRemove(files[i]); } files.clear(); }
2223 m->mothurOut("\nDivided " + filename + " into " + toString(files.size()) + " files.\n\n");
2227 catch(exception& e) {
2228 m->errorOut(e, "ShhherCommand", "parseFlowFiles");
2232 /**************************************************************************************************/
2234 int ShhherCommand::driver(vector<string> filenames, string thisCompositeFASTAFileName, string thisCompositeNamesFileName){
2237 int numCompleted = 0;
2239 for(int i=0;i<filenames.size();i++){
2241 if (m->control_pressed) { break; }
2243 vector<string> theseFlowFileNames; theseFlowFileNames.push_back(filenames[i]);
2244 if (large) { theseFlowFileNames = parseFlowFiles(filenames[i]); }
2246 if (m->control_pressed) { break; }
2248 double begClock = clock();
2249 unsigned long long begTime;
2251 string fileNameForOutput = filenames[i];
2253 for (int g = 0; g < theseFlowFileNames.size(); g++) {
2255 string flowFileName = theseFlowFileNames[g];
2256 m->mothurOut("\n>>>>>\tProcessing " + flowFileName + " (file " + toString(i+1) + " of " + toString(filenames.size()) + ")\t<<<<<\n");
2257 m->mothurOut("Reading flowgrams...\n");
2259 vector<string> seqNameVector;
2260 vector<int> lengths;
2261 vector<short> flowDataIntI;
2262 vector<double> flowDataPrI;
2263 map<string, int> nameMap;
2264 vector<short> uniqueFlowgrams;
2265 vector<int> uniqueCount;
2266 vector<int> mapSeqToUnique;
2267 vector<int> mapUniqueToSeq;
2268 vector<int> uniqueLengths;
2271 if (m->debug) { m->mothurOut("[DEBUG]: About to read flowgrams.\n"); }
2272 int numSeqs = getFlowData(flowFileName, seqNameVector, lengths, flowDataIntI, nameMap, numFlowCells);
2274 if (m->control_pressed) { break; }
2276 m->mothurOut("Identifying unique flowgrams...\n");
2277 int numUniques = getUniques(numSeqs, numFlowCells, uniqueFlowgrams, uniqueCount, uniqueLengths, mapSeqToUnique, mapUniqueToSeq, lengths, flowDataPrI, flowDataIntI);
2279 if (m->control_pressed) { break; }
2281 m->mothurOut("Calculating distances between flowgrams...\n");
2282 string distFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.dist";
2283 begTime = time(NULL);
2286 flowDistParentFork(numFlowCells, distFileName, numUniques, mapUniqueToSeq, mapSeqToUnique, lengths, flowDataPrI, flowDataIntI);
2288 m->mothurOutEndLine();
2289 m->mothurOut("Total time: " + toString(time(NULL) - begTime) + '\t' + toString((clock() - begClock)/CLOCKS_PER_SEC) + '\n');
2292 string namesFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.names";
2293 createNamesFile(numSeqs, numUniques, namesFileName, seqNameVector, mapSeqToUnique, mapUniqueToSeq);
2295 if (m->control_pressed) { break; }
2297 m->mothurOut("\nClustering flowgrams...\n");
2298 string listFileName = flowFileName.substr(0,flowFileName.find_last_of('.')) + ".shhh.list";
2299 cluster(listFileName, distFileName, namesFileName);
2301 if (m->control_pressed) { break; }
2303 vector<int> otuData;
2304 vector<int> cumNumSeqs;
2305 vector<int> nSeqsPerOTU;
2306 vector<vector<int> > aaP; //tMaster->aanP: each row is a different otu / each col contains the sequence indices
2307 vector<vector<int> > aaI; //tMaster->aanI: that are in each otu - can't differentiate between aaP and aaI
2308 vector<int> seqNumber; //tMaster->anP: the sequence id number sorted by OTU
2309 vector<int> seqIndex; //tMaster->anI; the index that corresponds to seqNumber
2312 int numOTUs = getOTUData(numSeqs, listFileName, otuData, cumNumSeqs, nSeqsPerOTU, aaP, aaI, seqNumber, seqIndex, nameMap);
2314 if (m->control_pressed) { break; }
2316 m->mothurRemove(distFileName);
2317 m->mothurRemove(namesFileName);
2318 m->mothurRemove(listFileName);
2320 vector<double> dist; //adDist - distance of sequences to centroids
2321 vector<short> change; //did the centroid sequence change? 0 = no; 1 = yes
2322 vector<int> centroids; //the representative flowgram for each cluster m
2323 vector<double> weight;
2324 vector<double> singleTau; //tMaster->adTau: 1-D Tau vector (1xnumSeqs)
2325 vector<int> nSeqsBreaks;
2326 vector<int> nOTUsBreaks;
2328 if (m->debug) { m->mothurOut("[DEBUG]: numSeqs = " + toString(numSeqs) + " numOTUS = " + toString(numOTUs) + " about to alloc a dist vector with size = " + toString((numSeqs * numOTUs)) + ".\n"); }
2330 dist.assign(numSeqs * numOTUs, 0);
2331 change.assign(numOTUs, 1);
2332 centroids.assign(numOTUs, -1);
2333 weight.assign(numOTUs, 0);
2334 singleTau.assign(numSeqs, 1.0);
2336 nSeqsBreaks.assign(2, 0);
2337 nOTUsBreaks.assign(2, 0);
2340 nSeqsBreaks[1] = numSeqs;
2341 nOTUsBreaks[1] = numOTUs;
2343 if (m->debug) { m->mothurOut("[DEBUG]: done allocating memory, about to denoise.\n"); }
2345 if (m->control_pressed) { break; }
2347 double maxDelta = 0;
2351 begTime = time(NULL);
2353 m->mothurOut("\nDenoising flowgrams...\n");
2354 m->mothurOut("iter\tmaxDelta\tnLL\t\tcycletime\n");
2356 while((maxIters == 0 && maxDelta > minDelta) || iter < MIN_ITER || (maxDelta > minDelta && iter < maxIters)){
2358 if (m->control_pressed) { break; }
2360 double cycClock = clock();
2361 unsigned long long cycTime = time(NULL);
2362 fill(numOTUs, seqNumber, seqIndex, cumNumSeqs, nSeqsPerOTU, aaP, aaI);
2364 if (m->control_pressed) { break; }
2366 calcCentroidsDriver(numOTUs, cumNumSeqs, nSeqsPerOTU, seqIndex, change, centroids, singleTau, mapSeqToUnique, uniqueFlowgrams, flowDataIntI, lengths, numFlowCells, seqNumber);
2368 if (m->control_pressed) { break; }
2370 maxDelta = getNewWeights(numOTUs, cumNumSeqs, nSeqsPerOTU, singleTau, seqNumber, weight);
2372 if (m->control_pressed) { break; }
2374 double nLL = getLikelihood(numSeqs, numOTUs, nSeqsPerOTU, seqNumber, cumNumSeqs, seqIndex, dist, weight);
2376 if (m->control_pressed) { break; }
2378 checkCentroids(numOTUs, centroids, weight);
2380 if (m->control_pressed) { break; }
2382 calcNewDistances(numSeqs, numOTUs, nSeqsPerOTU, dist, weight, change, centroids, aaP, singleTau, aaI, seqNumber, seqIndex, uniqueFlowgrams, flowDataIntI, numFlowCells, lengths);
2384 if (m->control_pressed) { break; }
2388 m->mothurOut(toString(iter) + '\t' + toString(maxDelta) + '\t' + toString(nLL) + '\t' + toString(time(NULL) - cycTime) + '\t' + toString((clock() - cycClock)/(double)CLOCKS_PER_SEC) + '\n');
2392 if (m->control_pressed) { break; }
2394 m->mothurOut("\nFinalizing...\n");
2395 fill(numOTUs, seqNumber, seqIndex, cumNumSeqs, nSeqsPerOTU, aaP, aaI);
2397 if (m->control_pressed) { break; }
2399 setOTUs(numOTUs, numSeqs, seqNumber, seqIndex, cumNumSeqs, nSeqsPerOTU, otuData, singleTau, dist, aaP, aaI);
2401 if (m->control_pressed) { break; }
2403 vector<int> otuCounts(numOTUs, 0);
2404 for(int j=0;j<numSeqs;j++) { otuCounts[otuData[j]]++; }
2406 calcCentroidsDriver(numOTUs, cumNumSeqs, nSeqsPerOTU, seqIndex, change, centroids, singleTau, mapSeqToUnique, uniqueFlowgrams, flowDataIntI, lengths, numFlowCells, seqNumber);
2408 if (m->control_pressed) { break; }
2410 if ((large) && (g == 0)) { flowFileName = filenames[i]; theseFlowFileNames[0] = filenames[i]; }
2411 string thisOutputDir = outputDir;
2412 if (outputDir == "") { thisOutputDir = m->hasPath(flowFileName); }
2413 map<string, string> variables;
2414 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(flowFileName));
2415 string qualityFileName = getOutputFileName("qfile",variables);
2416 string fastaFileName = getOutputFileName("fasta",variables);
2417 string nameFileName = getOutputFileName("name",variables);
2418 string otuCountsFileName = getOutputFileName("counts",variables);
2419 string fileRoot = m->getRootName(m->getSimpleName(flowFileName));
2420 int pos = fileRoot.find_first_of('.');
2421 string fileGroup = fileRoot;
2422 if (pos != string::npos) { fileGroup = fileRoot.substr(pos+1, (fileRoot.length()-1-(pos+1))); }
2423 string groupFileName = getOutputFileName("group",variables);
2426 writeQualities(numOTUs, numFlowCells, qualityFileName, otuCounts, nSeqsPerOTU, seqNumber, singleTau, flowDataIntI, uniqueFlowgrams, cumNumSeqs, mapUniqueToSeq, seqNameVector, centroids, aaI); if (m->control_pressed) { break; }
2427 writeSequences(thisCompositeFASTAFileName, numOTUs, numFlowCells, fastaFileName, otuCounts, uniqueFlowgrams, seqNameVector, aaI, centroids);if (m->control_pressed) { break; }
2428 writeNames(thisCompositeNamesFileName, numOTUs, nameFileName, otuCounts, seqNameVector, aaI, nSeqsPerOTU); if (m->control_pressed) { break; }
2429 writeClusters(otuCountsFileName, numOTUs, numFlowCells,otuCounts, centroids, uniqueFlowgrams, seqNameVector, aaI, nSeqsPerOTU, lengths, flowDataIntI); if (m->control_pressed) { break; }
2430 writeGroups(groupFileName, fileGroup, numSeqs, seqNameVector); if (m->control_pressed) { break; }
2434 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(theseFlowFileNames[0]));
2435 m->appendFiles(qualityFileName, getOutputFileName("qfile",variables));
2436 m->mothurRemove(qualityFileName);
2437 m->appendFiles(fastaFileName, getOutputFileName("fasta",variables));
2438 m->mothurRemove(fastaFileName);
2439 m->appendFiles(nameFileName, getOutputFileName("name",variables));
2440 m->mothurRemove(nameFileName);
2441 m->appendFiles(otuCountsFileName, getOutputFileName("counts",variables));
2442 m->mothurRemove(otuCountsFileName);
2443 m->appendFiles(groupFileName, getOutputFileName("group",variables));
2444 m->mothurRemove(groupFileName);
2446 m->mothurRemove(theseFlowFileNames[g]);
2451 m->mothurOut("Total time to process " + fileNameForOutput + ":\t" + toString(time(NULL) - begTime) + '\t' + toString((clock() - begClock)/(double)CLOCKS_PER_SEC) + '\n');
2454 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
2456 return numCompleted;
2458 }catch(exception& e) {
2459 m->errorOut(e, "ShhherCommand", "driver");
2464 /**************************************************************************************************/
2465 int ShhherCommand::getFlowData(string filename, vector<string>& thisSeqNameVector, vector<int>& thisLengths, vector<short>& thisFlowDataIntI, map<string, int>& thisNameMap, int& numFlowCells){
2470 m->openInputFile(filename, flowFile);
2473 int currentNumFlowCells;
2475 thisSeqNameVector.clear();
2476 thisLengths.clear();
2477 thisFlowDataIntI.clear();
2478 thisNameMap.clear();
2481 flowFile >> numFlowTest;
2483 if (!m->isContainingOnlyDigits(numFlowTest)) { m->mothurOut("[ERROR]: expected a number and got " + numFlowTest + ", quitting. Did you use the flow parameter instead of the file parameter?"); m->mothurOutEndLine(); exit(1); }
2484 else { convert(numFlowTest, numFlowCells); }
2486 if (m->debug) { m->mothurOut("[DEBUG]: numFlowCells = " + toString(numFlowCells) + ".\n"); }
2487 int index = 0;//pcluster
2488 while(!flowFile.eof()){
2490 if (m->control_pressed) { break; }
2492 flowFile >> seqName >> currentNumFlowCells;
2494 thisLengths.push_back(currentNumFlowCells);
2496 thisSeqNameVector.push_back(seqName);
2497 thisNameMap[seqName] = index++;//pcluster
2499 if (m->debug) { m->mothurOut("[DEBUG]: seqName = " + seqName + " length = " + toString(currentNumFlowCells) + " index = " + toString(index) + "\n"); }
2501 for(int i=0;i<numFlowCells;i++){
2502 flowFile >> intensity;
2503 if(intensity > 9.99) { intensity = 9.99; }
2504 int intI = int(100 * intensity + 0.0001);
2505 thisFlowDataIntI.push_back(intI);
2507 m->gobble(flowFile);
2511 int numSeqs = thisSeqNameVector.size();
2513 for(int i=0;i<numSeqs;i++){
2515 if (m->control_pressed) { break; }
2517 int iNumFlowCells = i * numFlowCells;
2518 for(int j=thisLengths[i];j<numFlowCells;j++){
2519 thisFlowDataIntI[iNumFlowCells + j] = 0;
2526 catch(exception& e) {
2527 m->errorOut(e, "ShhherCommand", "getFlowData");
2531 /**************************************************************************************************/
2533 int ShhherCommand::flowDistParentFork(int numFlowCells, string distFileName, int stopSeq, vector<int>& mapUniqueToSeq, vector<int>& mapSeqToUnique, vector<int>& lengths, vector<double>& flowDataPrI, vector<short>& flowDataIntI){
2536 ostringstream outStream;
2537 outStream.setf(ios::fixed, ios::floatfield);
2538 outStream.setf(ios::dec, ios::basefield);
2539 outStream.setf(ios::showpoint);
2540 outStream.precision(6);
2542 int begTime = time(NULL);
2543 double begClock = clock();
2545 for(int i=0;i<stopSeq;i++){
2547 if (m->control_pressed) { break; }
2549 for(int j=0;j<i;j++){
2550 float flowDistance = calcPairwiseDist(numFlowCells, mapUniqueToSeq[i], mapUniqueToSeq[j], mapSeqToUnique, lengths, flowDataPrI, flowDataIntI);
2552 if(flowDistance < 1e-6){
2553 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << 0.000000 << endl;
2555 else if(flowDistance <= cutoff){
2556 outStream << mapUniqueToSeq[i] << '\t' << mapUniqueToSeq[j] << '\t' << flowDistance << endl;
2560 m->mothurOutJustToScreen(toString(i) + "\t" + toString(time(NULL) - begTime));
2561 m->mothurOutJustToScreen("\t" + toString((clock()-begClock)/CLOCKS_PER_SEC)+"\n");
2565 ofstream distFile(distFileName.c_str());
2566 distFile << outStream.str();
2569 if (m->control_pressed) {}
2571 m->mothurOutJustToScreen(toString(stopSeq-1) + "\t" + toString(time(NULL) - begTime));
2572 m->mothurOutJustToScreen("\t" + toString((clock()-begClock)/CLOCKS_PER_SEC)+"\n");
2577 catch(exception& e) {
2578 m->errorOut(e, "ShhherCommand", "flowDistParentFork");
2582 /**************************************************************************************************/
2584 float ShhherCommand::calcPairwiseDist(int numFlowCells, int seqA, int seqB, vector<int>& mapSeqToUnique, vector<int>& lengths, vector<double>& flowDataPrI, vector<short>& flowDataIntI){
2586 int minLength = lengths[mapSeqToUnique[seqA]];
2587 if(lengths[seqB] < minLength){ minLength = lengths[mapSeqToUnique[seqB]]; }
2589 int ANumFlowCells = seqA * numFlowCells;
2590 int BNumFlowCells = seqB * numFlowCells;
2594 for(int i=0;i<minLength;i++){
2596 if (m->control_pressed) { break; }
2598 int flowAIntI = flowDataIntI[ANumFlowCells + i];
2599 float flowAPrI = flowDataPrI[ANumFlowCells + i];
2601 int flowBIntI = flowDataIntI[BNumFlowCells + i];
2602 float flowBPrI = flowDataPrI[BNumFlowCells + i];
2603 dist += jointLookUp[flowAIntI * NUMBINS + flowBIntI] - flowAPrI - flowBPrI;
2606 dist /= (float) minLength;
2609 catch(exception& e) {
2610 m->errorOut(e, "ShhherCommand", "calcPairwiseDist");
2615 /**************************************************************************************************/
2617 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){
2620 uniqueFlowgrams.assign(numFlowCells * numSeqs, -1);
2621 uniqueCount.assign(numSeqs, 0); // anWeights
2622 uniqueLengths.assign(numSeqs, 0);
2623 mapSeqToUnique.assign(numSeqs, -1);
2624 mapUniqueToSeq.assign(numSeqs, -1);
2626 vector<short> uniqueFlowDataIntI(numFlowCells * numSeqs, -1);
2628 for(int i=0;i<numSeqs;i++){
2630 if (m->control_pressed) { break; }
2634 vector<short> current(numFlowCells);
2635 for(int j=0;j<numFlowCells;j++){
2636 current[j] = short(((flowDataIntI[i * numFlowCells + j] + 50.0)/100.0));
2639 for(int j=0;j<numUniques;j++){
2640 int offset = j * numFlowCells;
2644 if(lengths[i] < uniqueLengths[j]) { shorterLength = lengths[i]; }
2645 else { shorterLength = uniqueLengths[j]; }
2647 for(int k=0;k<shorterLength;k++){
2648 if(current[k] != uniqueFlowgrams[offset + k]){
2655 mapSeqToUnique[i] = j;
2658 if(lengths[i] > uniqueLengths[j]) { uniqueLengths[j] = lengths[i]; }
2664 if(index == numUniques){
2665 uniqueLengths[numUniques] = lengths[i];
2666 uniqueCount[numUniques] = 1;
2667 mapSeqToUnique[i] = numUniques;//anMap
2668 mapUniqueToSeq[numUniques] = i;//anF
2670 for(int k=0;k<numFlowCells;k++){
2671 uniqueFlowgrams[numUniques * numFlowCells + k] = current[k];
2672 uniqueFlowDataIntI[numUniques * numFlowCells + k] = flowDataIntI[i * numFlowCells + k];
2678 uniqueFlowDataIntI.resize(numFlowCells * numUniques);
2679 uniqueLengths.resize(numUniques);
2681 flowDataPrI.resize(numSeqs * numFlowCells, 0);
2682 for(int i=0;i<flowDataPrI.size();i++) { if (m->control_pressed) { break; } flowDataPrI[i] = getProbIntensity(flowDataIntI[i]); }
2686 catch(exception& e) {
2687 m->errorOut(e, "ShhherCommand", "getUniques");
2691 /**************************************************************************************************/
2692 int ShhherCommand::createNamesFile(int numSeqs, int numUniques, string filename, vector<string>& seqNameVector, vector<int>& mapSeqToUnique, vector<int>& mapUniqueToSeq){
2695 vector<string> duplicateNames(numUniques, "");
2696 for(int i=0;i<numSeqs;i++){
2697 duplicateNames[mapSeqToUnique[i]] += seqNameVector[i] + ',';
2701 m->openOutputFile(filename, nameFile);
2703 for(int i=0;i<numUniques;i++){
2705 if (m->control_pressed) { break; }
2707 // nameFile << seqNameVector[mapUniqueToSeq[i]] << '\t' << duplicateNames[i].substr(0, duplicateNames[i].find_last_of(',')) << endl;
2708 nameFile << mapUniqueToSeq[i] << '\t' << duplicateNames[i].substr(0, duplicateNames[i].find_last_of(',')) << endl;
2715 catch(exception& e) {
2716 m->errorOut(e, "ShhherCommand", "createNamesFile");
2720 //**********************************************************************************************************************
2722 int ShhherCommand::cluster(string filename, string distFileName, string namesFileName){
2725 ReadMatrix* read = new ReadColumnMatrix(distFileName);
2726 read->setCutoff(cutoff);
2728 NameAssignment* clusterNameMap = new NameAssignment(namesFileName);
2729 clusterNameMap->readMap();
2730 read->read(clusterNameMap);
2732 ListVector* list = read->getListVector();
2733 SparseDistanceMatrix* matrix = read->getDMatrix();
2736 delete clusterNameMap;
2738 RAbundVector* rabund = new RAbundVector(list->getRAbundVector());
2740 float adjust = -1.0;
2741 Cluster* cluster = new CompleteLinkage(rabund, list, matrix, cutoff, "furthest", adjust);
2742 string tag = cluster->getTag();
2744 double clusterCutoff = cutoff;
2745 while (matrix->getSmallDist() <= clusterCutoff && matrix->getNNodes() > 0){
2747 if (m->control_pressed) { break; }
2749 cluster->update(clusterCutoff);
2752 list->setLabel(toString(cutoff));
2755 m->openOutputFile(filename, listFile);
2756 list->print(listFile);
2759 delete matrix; delete cluster; delete rabund; delete list;
2763 catch(exception& e) {
2764 m->errorOut(e, "ShhherCommand", "cluster");
2768 /**************************************************************************************************/
2770 int ShhherCommand::getOTUData(int numSeqs, string fileName, vector<int>& otuData,
2771 vector<int>& cumNumSeqs,
2772 vector<int>& nSeqsPerOTU,
2773 vector<vector<int> >& aaP, //tMaster->aanP: each row is a different otu / each col contains the sequence indices
2774 vector<vector<int> >& aaI, //tMaster->aanI: that are in each otu - can't differentiate between aaP and aaI
2775 vector<int>& seqNumber, //tMaster->anP: the sequence id number sorted by OTU
2776 vector<int>& seqIndex,
2777 map<string, int>& nameMap){
2781 m->openInputFile(fileName, listFile);
2785 listFile >> label >> numOTUs;
2787 if (m->debug) { m->mothurOut("[DEBUG]: Getting OTU Data...\n"); }
2789 otuData.assign(numSeqs, 0);
2790 cumNumSeqs.assign(numOTUs, 0);
2791 nSeqsPerOTU.assign(numOTUs, 0);
2792 aaP.clear();aaP.resize(numOTUs);
2798 string singleOTU = "";
2800 for(int i=0;i<numOTUs;i++){
2802 if (m->control_pressed) { break; }
2803 if (m->debug) { m->mothurOut("[DEBUG]: processing OTU " + toString(i) + ".\n"); }
2805 listFile >> singleOTU;
2807 istringstream otuString(singleOTU);
2811 string seqName = "";
2813 for(int j=0;j<singleOTU.length();j++){
2814 char letter = otuString.get();
2820 map<string,int>::iterator nmIt = nameMap.find(seqName);
2821 int index = nmIt->second;
2823 nameMap.erase(nmIt);
2827 aaP[i].push_back(index);
2832 map<string,int>::iterator nmIt = nameMap.find(seqName);
2834 int index = nmIt->second;
2835 nameMap.erase(nmIt);
2839 aaP[i].push_back(index);
2844 sort(aaP[i].begin(), aaP[i].end());
2845 for(int j=0;j<nSeqsPerOTU[i];j++){
2846 seqNumber.push_back(aaP[i][j]);
2848 for(int j=nSeqsPerOTU[i];j<numSeqs;j++){
2849 aaP[i].push_back(0);
2855 for(int i=1;i<numOTUs;i++){
2856 cumNumSeqs[i] = cumNumSeqs[i-1] + nSeqsPerOTU[i-1];
2859 seqIndex = seqNumber;
2866 catch(exception& e) {
2867 m->errorOut(e, "ShhherCommand", "getOTUData");
2871 /**************************************************************************************************/
2873 int ShhherCommand::calcCentroidsDriver(int numOTUs,
2874 vector<int>& cumNumSeqs,
2875 vector<int>& nSeqsPerOTU,
2876 vector<int>& seqIndex,
2877 vector<short>& change, //did the centroid sequence change? 0 = no; 1 = yes
2878 vector<int>& centroids, //the representative flowgram for each cluster m
2879 vector<double>& singleTau, //tMaster->adTau: 1-D Tau vector (1xnumSeqs)
2880 vector<int>& mapSeqToUnique,
2881 vector<short>& uniqueFlowgrams,
2882 vector<short>& flowDataIntI,
2883 vector<int>& lengths,
2885 vector<int>& seqNumber){
2887 //this function gets the most likely homopolymer length at a flow position for a group of sequences
2892 for(int i=0;i<numOTUs;i++){
2894 if (m->control_pressed) { break; }
2898 int minFlowGram = 100000000;
2899 double minFlowValue = 1e8;
2900 change[i] = 0; //FALSE
2902 for(int j=0;j<nSeqsPerOTU[i];j++){
2903 count += singleTau[seqNumber[cumNumSeqs[i] + j]];
2906 if(nSeqsPerOTU[i] > 0 && count > MIN_COUNT){
2907 vector<double> adF(nSeqsPerOTU[i]);
2908 vector<int> anL(nSeqsPerOTU[i]);
2910 for(int j=0;j<nSeqsPerOTU[i];j++){
2911 int index = cumNumSeqs[i] + j;
2912 int nI = seqIndex[index];
2913 int nIU = mapSeqToUnique[nI];
2916 for(k=0;k<position;k++){
2922 anL[position] = nIU;
2923 adF[position] = 0.0000;
2928 for(int j=0;j<nSeqsPerOTU[i];j++){
2929 int index = cumNumSeqs[i] + j;
2930 int nI = seqIndex[index];
2932 double tauValue = singleTau[seqNumber[index]];
2934 for(int k=0;k<position;k++){
2935 double dist = getDistToCentroid(anL[k], nI, lengths[nI], uniqueFlowgrams, flowDataIntI, numFlowCells);
2936 adF[k] += dist * tauValue;
2940 for(int j=0;j<position;j++){
2941 if(adF[j] < minFlowValue){
2943 minFlowValue = adF[j];
2947 if(centroids[i] != anL[minFlowGram]){
2949 centroids[i] = anL[minFlowGram];
2952 else if(centroids[i] != -1){
2960 catch(exception& e) {
2961 m->errorOut(e, "ShhherCommand", "calcCentroidsDriver");
2965 /**************************************************************************************************/
2967 double ShhherCommand::getDistToCentroid(int cent, int flow, int length, vector<short>& uniqueFlowgrams,
2968 vector<short>& flowDataIntI, int numFlowCells){
2971 int flowAValue = cent * numFlowCells;
2972 int flowBValue = flow * numFlowCells;
2976 for(int i=0;i<length;i++){
2977 dist += singleLookUp[uniqueFlowgrams[flowAValue] * NUMBINS + flowDataIntI[flowBValue]];
2982 return dist / (double)length;
2984 catch(exception& e) {
2985 m->errorOut(e, "ShhherCommand", "getDistToCentroid");
2989 /**************************************************************************************************/
2991 double ShhherCommand::getNewWeights(int numOTUs, vector<int>& cumNumSeqs, vector<int>& nSeqsPerOTU, vector<double>& singleTau, vector<int>& seqNumber, vector<double>& weight){
2994 double maxChange = 0;
2996 for(int i=0;i<numOTUs;i++){
2998 if (m->control_pressed) { break; }
3000 double difference = weight[i];
3003 for(int j=0;j<nSeqsPerOTU[i];j++){
3004 int index = cumNumSeqs[i] + j;
3005 double tauValue = singleTau[seqNumber[index]];
3006 weight[i] += tauValue;
3009 difference = fabs(weight[i] - difference);
3010 if(difference > maxChange){ maxChange = difference; }
3014 catch(exception& e) {
3015 m->errorOut(e, "ShhherCommand", "getNewWeights");
3020 /**************************************************************************************************/
3022 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){
3026 vector<long double> P(numSeqs, 0);
3029 for(int i=0;i<numOTUs;i++){
3030 if(weight[i] > MIN_WEIGHT){
3036 for(int i=0;i<numOTUs;i++){
3038 if (m->control_pressed) { break; }
3040 for(int j=0;j<nSeqsPerOTU[i];j++){
3041 int index = cumNumSeqs[i] + j;
3042 int nI = seqIndex[index];
3043 double singleDist = dist[seqNumber[index]];
3045 P[nI] += weight[i] * exp(-singleDist * sigma);
3049 for(int i=0;i<numSeqs;i++){
3050 if(P[i] == 0){ P[i] = DBL_EPSILON; }
3055 nLL = nLL -(double)numSeqs * log(sigma);
3059 catch(exception& e) {
3060 m->errorOut(e, "ShhherCommand", "getNewWeights");
3065 /**************************************************************************************************/
3067 int ShhherCommand::checkCentroids(int numOTUs, vector<int>& centroids, vector<double>& weight){
3069 vector<int> unique(numOTUs, 1);
3071 for(int i=0;i<numOTUs;i++){
3072 if(centroids[i] == -1 || weight[i] < MIN_WEIGHT){
3077 for(int i=0;i<numOTUs;i++){
3079 if (m->control_pressed) { break; }
3082 for(int j=i+1;j<numOTUs;j++){
3085 if(centroids[j] == centroids[i]){
3089 weight[i] += weight[j];
3099 catch(exception& e) {
3100 m->errorOut(e, "ShhherCommand", "checkCentroids");
3104 /**************************************************************************************************/
3106 void ShhherCommand::calcNewDistances(int numSeqs, int numOTUs, vector<int>& nSeqsPerOTU, vector<double>& dist,
3107 vector<double>& weight, vector<short>& change, vector<int>& centroids,
3108 vector<vector<int> >& aaP, vector<double>& singleTau, vector<vector<int> >& aaI,
3109 vector<int>& seqNumber, vector<int>& seqIndex,
3110 vector<short>& uniqueFlowgrams,
3111 vector<short>& flowDataIntI, int numFlowCells, vector<int>& lengths){
3116 vector<double> newTau(numOTUs,0);
3117 vector<double> norms(numSeqs, 0);
3118 nSeqsPerOTU.assign(numOTUs, 0);
3120 for(int i=0;i<numSeqs;i++){
3122 if (m->control_pressed) { break; }
3124 int indexOffset = i * numOTUs;
3126 double offset = 1e8;
3128 for(int j=0;j<numOTUs;j++){
3130 if(weight[j] > MIN_WEIGHT && change[j] == 1){
3131 dist[indexOffset + j] = getDistToCentroid(centroids[j], i, lengths[i], uniqueFlowgrams, flowDataIntI, numFlowCells);
3134 if(weight[j] > MIN_WEIGHT && dist[indexOffset + j] < offset){
3135 offset = dist[indexOffset + j];
3139 for(int j=0;j<numOTUs;j++){
3140 if(weight[j] > MIN_WEIGHT){
3141 newTau[j] = exp(sigma * (-dist[indexOffset + j] + offset)) * weight[j];
3142 norms[i] += newTau[j];
3149 for(int j=0;j<numOTUs;j++){
3150 newTau[j] /= norms[i];
3153 for(int j=0;j<numOTUs;j++){
3154 if(newTau[j] > MIN_TAU){
3156 int oldTotal = total;
3160 singleTau.resize(total, 0);
3161 seqNumber.resize(total, 0);
3162 seqIndex.resize(total, 0);
3164 singleTau[oldTotal] = newTau[j];
3166 aaP[j][nSeqsPerOTU[j]] = oldTotal;
3167 aaI[j][nSeqsPerOTU[j]] = i;
3175 catch(exception& e) {
3176 m->errorOut(e, "ShhherCommand", "calcNewDistances");
3180 /**************************************************************************************************/
3182 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){
3185 for(int i=0;i<numOTUs;i++){
3187 if (m->control_pressed) { return 0; }
3189 cumNumSeqs[i] = index;
3190 for(int j=0;j<nSeqsPerOTU[i];j++){
3191 seqNumber[index] = aaP[i][j];
3192 seqIndex[index] = aaI[i][j];
3200 catch(exception& e) {
3201 m->errorOut(e, "ShhherCommand", "fill");
3205 /**************************************************************************************************/
3207 void ShhherCommand::setOTUs(int numOTUs, int numSeqs, vector<int>& seqNumber, vector<int>& seqIndex, vector<int>& cumNumSeqs, vector<int>& nSeqsPerOTU,
3208 vector<int>& otuData, vector<double>& singleTau, vector<double>& dist, vector<vector<int> >& aaP, vector<vector<int> >& aaI){
3211 vector<double> bigTauMatrix(numOTUs * numSeqs, 0.0000);
3213 for(int i=0;i<numOTUs;i++){
3215 if (m->control_pressed) { break; }
3217 for(int j=0;j<nSeqsPerOTU[i];j++){
3218 int index = cumNumSeqs[i] + j;
3219 double tauValue = singleTau[seqNumber[index]];
3220 int sIndex = seqIndex[index];
3221 bigTauMatrix[sIndex * numOTUs + i] = tauValue;
3225 for(int i=0;i<numSeqs;i++){
3226 double maxTau = -1.0000;
3228 for(int j=0;j<numOTUs;j++){
3229 if(bigTauMatrix[i * numOTUs + j] > maxTau){
3230 maxTau = bigTauMatrix[i * numOTUs + j];
3235 otuData[i] = maxOTU;
3238 nSeqsPerOTU.assign(numOTUs, 0);
3240 for(int i=0;i<numSeqs;i++){
3241 int index = otuData[i];
3243 singleTau[i] = 1.0000;
3246 aaP[index][nSeqsPerOTU[index]] = i;
3247 aaI[index][nSeqsPerOTU[index]] = i;
3249 nSeqsPerOTU[index]++;
3252 fill(numOTUs, seqNumber, seqIndex, cumNumSeqs, nSeqsPerOTU, aaP, aaI);
3254 catch(exception& e) {
3255 m->errorOut(e, "ShhherCommand", "setOTUs");
3259 /**************************************************************************************************/
3261 void ShhherCommand::writeQualities(int numOTUs, int numFlowCells, string qualityFileName, vector<int> otuCounts, vector<int>& nSeqsPerOTU, vector<int>& seqNumber,
3262 vector<double>& singleTau, vector<short>& flowDataIntI, vector<short>& uniqueFlowgrams, vector<int>& cumNumSeqs,
3263 vector<int>& mapUniqueToSeq, vector<string>& seqNameVector, vector<int>& centroids, vector<vector<int> >& aaI){
3267 ofstream qualityFile;
3268 m->openOutputFile(qualityFileName, qualityFile);
3270 qualityFile.setf(ios::fixed, ios::floatfield);
3271 qualityFile.setf(ios::showpoint);
3272 qualityFile << setprecision(6);
3274 vector<vector<int> > qualities(numOTUs);
3275 vector<double> pr(HOMOPS, 0);
3278 for(int i=0;i<numOTUs;i++){
3280 if (m->control_pressed) { break; }
3285 if(nSeqsPerOTU[i] > 0){
3286 qualities[i].assign(1024, -1);
3288 while(index < numFlowCells){
3289 double maxPrValue = 1e8;
3290 short maxPrIndex = -1;
3291 double count = 0.0000;
3293 pr.assign(HOMOPS, 0);
3295 for(int j=0;j<nSeqsPerOTU[i];j++){
3296 int lIndex = cumNumSeqs[i] + j;
3297 double tauValue = singleTau[seqNumber[lIndex]];
3298 int sequenceIndex = aaI[i][j];
3299 short intensity = flowDataIntI[sequenceIndex * numFlowCells + index];
3303 for(int s=0;s<HOMOPS;s++){
3304 pr[s] += tauValue * singleLookUp[s * NUMBINS + intensity];
3308 maxPrIndex = uniqueFlowgrams[centroids[i] * numFlowCells + index];
3309 maxPrValue = pr[maxPrIndex];
3311 if(count > MIN_COUNT){
3313 double norm = 0.0000;
3315 for(int s=0;s<HOMOPS;s++){
3316 norm += exp(-(pr[s] - maxPrValue));
3319 for(int s=1;s<=maxPrIndex;s++){
3321 double temp = 0.0000;
3323 U += exp(-(pr[s-1]-maxPrValue))/norm;
3331 temp = floor(-10 * temp);
3332 value = (int)floor(temp);
3333 if(value > 100){ value = 100; }
3335 qualities[i][base] = (int)value;
3345 if(otuCounts[i] > 0){
3346 qualityFile << '>' << seqNameVector[mapUniqueToSeq[i]] << endl;
3348 int j=4; //need to get past the first four bases
3349 while(qualities[i][j] != -1){
3350 qualityFile << qualities[i][j] << ' ';
3351 if (j > qualities[i].size()) { break; }
3354 qualityFile << endl;
3357 qualityFile.close();
3358 outputNames.push_back(qualityFileName); outputTypes["qfile"].push_back(qualityFileName);
3361 catch(exception& e) {
3362 m->errorOut(e, "ShhherCommand", "writeQualities");
3367 /**************************************************************************************************/
3369 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){
3373 m->openOutputFile(fastaFileName, fastaFile);
3375 vector<string> names(numOTUs, "");
3377 for(int i=0;i<numOTUs;i++){
3379 if (m->control_pressed) { break; }
3381 int index = centroids[i];
3383 if(otuCounts[i] > 0){
3384 fastaFile << '>' << seqNameVector[aaI[i][0]] << endl;
3388 for(int j=0;j<numFlowCells;j++){
3390 char base = flowOrder[j % flowOrder.length()];
3391 for(int k=0;k<uniqueFlowgrams[index * numFlowCells + j];k++){
3396 if (newSeq.length() >= 4) { fastaFile << newSeq.substr(4) << endl; }
3397 else { fastaFile << "NNNN" << endl; }
3402 outputNames.push_back(fastaFileName); outputTypes["fasta"].push_back(fastaFileName);
3404 if(thisCompositeFASTAFileName != ""){
3405 m->appendFiles(fastaFileName, thisCompositeFASTAFileName);
3408 catch(exception& e) {
3409 m->errorOut(e, "ShhherCommand", "writeSequences");
3414 /**************************************************************************************************/
3416 void ShhherCommand::writeNames(string thisCompositeNamesFileName, int numOTUs, string nameFileName, vector<int> otuCounts, vector<string>& seqNameVector, vector<vector<int> >& aaI, vector<int>& nSeqsPerOTU){
3420 m->openOutputFile(nameFileName, nameFile);
3422 for(int i=0;i<numOTUs;i++){
3424 if (m->control_pressed) { break; }
3426 if(otuCounts[i] > 0){
3427 nameFile << seqNameVector[aaI[i][0]] << '\t' << seqNameVector[aaI[i][0]];
3429 for(int j=1;j<nSeqsPerOTU[i];j++){
3430 nameFile << ',' << seqNameVector[aaI[i][j]];
3437 outputNames.push_back(nameFileName); outputTypes["name"].push_back(nameFileName);
3440 if(thisCompositeNamesFileName != ""){
3441 m->appendFiles(nameFileName, thisCompositeNamesFileName);
3444 catch(exception& e) {
3445 m->errorOut(e, "ShhherCommand", "writeNames");
3450 /**************************************************************************************************/
3452 void ShhherCommand::writeGroups(string groupFileName, string fileRoot, int numSeqs, vector<string>& seqNameVector){
3455 m->openOutputFile(groupFileName, groupFile);
3457 for(int i=0;i<numSeqs;i++){
3458 if (m->control_pressed) { break; }
3459 groupFile << seqNameVector[i] << '\t' << fileRoot << endl;
3462 outputNames.push_back(groupFileName); outputTypes["group"].push_back(groupFileName);
3465 catch(exception& e) {
3466 m->errorOut(e, "ShhherCommand", "writeGroups");
3471 /**************************************************************************************************/
3473 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){
3475 ofstream otuCountsFile;
3476 m->openOutputFile(otuCountsFileName, otuCountsFile);
3478 string bases = flowOrder;
3480 for(int i=0;i<numOTUs;i++){
3482 if (m->control_pressed) {
3485 //output the translated version of the centroid sequence for the otu
3486 if(otuCounts[i] > 0){
3487 int index = centroids[i];
3489 otuCountsFile << "ideal\t";
3490 for(int j=8;j<numFlowCells;j++){
3491 char base = bases[j % bases.length()];
3492 for(int s=0;s<uniqueFlowgrams[index * numFlowCells + j];s++){
3493 otuCountsFile << base;
3496 otuCountsFile << endl;
3498 for(int j=0;j<nSeqsPerOTU[i];j++){
3499 int sequence = aaI[i][j];
3500 otuCountsFile << seqNameVector[sequence] << '\t';
3504 for(int k=0;k<lengths[sequence];k++){
3505 char base = bases[k % bases.length()];
3506 int freq = int(0.01 * (double)flowDataIntI[sequence * numFlowCells + k] + 0.5);
3508 for(int s=0;s<freq;s++){
3510 //otuCountsFile << base;
3514 if (newSeq.length() >= 4) { otuCountsFile << newSeq.substr(4) << endl; }
3515 else { otuCountsFile << "NNNN" << endl; }
3517 otuCountsFile << endl;
3520 otuCountsFile.close();
3521 outputNames.push_back(otuCountsFileName); outputTypes["counts"].push_back(otuCountsFileName);
3524 catch(exception& e) {
3525 m->errorOut(e, "ShhherCommand", "writeClusters");
3530 /**************************************************************************************************/
3532 void ShhherCommand::getSingleLookUp(){
3534 // these are the -log probabilities that a signal corresponds to a particular homopolymer length
3535 singleLookUp.assign(HOMOPS * NUMBINS, 0);
3538 ifstream lookUpFile;
3539 m->openInputFile(lookupFileName, lookUpFile);
3541 for(int i=0;i<HOMOPS;i++){
3543 if (m->control_pressed) { break; }
3546 lookUpFile >> logFracFreq;
3548 for(int j=0;j<NUMBINS;j++) {
3549 lookUpFile >> singleLookUp[index];
3555 catch(exception& e) {
3556 m->errorOut(e, "ShhherCommand", "getSingleLookUp");
3561 /**************************************************************************************************/
3563 void ShhherCommand::getJointLookUp(){
3566 // the most likely joint probability (-log) that two intenities have the same polymer length
3567 jointLookUp.resize(NUMBINS * NUMBINS, 0);
3569 for(int i=0;i<NUMBINS;i++){
3571 if (m->control_pressed) { break; }
3573 for(int j=0;j<NUMBINS;j++){
3575 double minSum = 100000000;
3577 for(int k=0;k<HOMOPS;k++){
3578 double sum = singleLookUp[k * NUMBINS + i] + singleLookUp[k * NUMBINS + j];
3580 if(sum < minSum) { minSum = sum; }
3582 jointLookUp[i * NUMBINS + j] = minSum;
3586 catch(exception& e) {
3587 m->errorOut(e, "ShhherCommand", "getJointLookUp");
3592 /**************************************************************************************************/
3594 double ShhherCommand::getProbIntensity(int intIntensity){
3597 double minNegLogProb = 100000000;
3600 for(int i=0;i<HOMOPS;i++){//loop signal strength
3602 if (m->control_pressed) { break; }
3604 float negLogProb = singleLookUp[i * NUMBINS + intIntensity];
3605 if(negLogProb < minNegLogProb) { minNegLogProb = negLogProb; }
3608 return minNegLogProb;
3610 catch(exception& e) {
3611 m->errorOut(e, "ShhherCommand", "getProbIntensity");