2 // getmetacommunitycommand.cpp
5 // Created by SarahsWork on 4/9/13.
6 // Copyright (c) 2013 Schloss Lab. All rights reserved.
9 #include "getmetacommunitycommand.h"
12 //**********************************************************************************************************************
13 vector<string> GetMetaCommunityCommand::setParameters(){
15 CommandParameter pshared("shared", "InputTypes", "", "", "none", "none", "none","outputType",false,true); parameters.push_back(pshared);
16 CommandParameter pgroups("groups", "String", "", "", "", "", "","",false,false); parameters.push_back(pgroups);
17 CommandParameter plabel("label", "String", "", "", "", "", "","",false,false); parameters.push_back(plabel);
18 CommandParameter pminpartitions("minpartitions", "Number", "", "5", "", "", "","",false,false,true); parameters.push_back(pminpartitions);
19 CommandParameter pmaxpartitions("maxpartitions", "Number", "", "100", "", "", "","",false,false,true); parameters.push_back(pmaxpartitions);
20 CommandParameter poptimizegap("optimizegap", "Number", "", "3", "", "", "","",false,false,true); parameters.push_back(poptimizegap);
21 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "","",false,false,true); parameters.push_back(pprocessors);
22 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
23 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
25 vector<string> myArray;
26 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
30 m->errorOut(e, "NewCommand", "setParameters");
34 //**********************************************************************************************************************
35 string GetMetaCommunityCommand::getHelpString(){
37 string helpString = "";
38 helpString += "The get.metacommunity command parameters are shared, label, groups, minpartitions, maxpartitions, optimizegap and processors. The shared file is required. \n";
39 helpString += "The label parameter is used to analyze specific labels in your input. labels are separated by dashes.\n";
40 helpString += "The groups parameter allows you to specify which of the groups in your shared file you would like analyzed. Group names are separated by dashes.\n";
41 helpString += "The minpartitions parameter is used to .... Default=5.\n";
42 helpString += "The maxpartitions parameter is used to .... Default=10.\n";
43 helpString += "The optimizegap parameter is used to .... Default=3.\n";
44 helpString += "The processors parameter allows you to specify number of processors to use. The default is 1.\n";
45 helpString += "The get.metacommunity command should be in the following format: get.metacommunity(shared=yourSharedFile).\n";
49 m->errorOut(e, "GetMetaCommunityCommand", "getHelpString");
53 //**********************************************************************************************************************
54 string GetMetaCommunityCommand::getOutputPattern(string type) {
58 if (type == "fit") { pattern = "[filename],[distance],mix.fit"; }
59 else if (type == "relabund") { pattern = "[filename],[distance],[tag],mix.relabund"; }
60 else if (type == "design") { pattern = "[filename],[distance],mix.design"; }
61 else if (type == "matrix") { pattern = "[filename],[distance],[tag],mix.posterior"; }
62 else if (type == "parameters") { pattern = "[filename],[distance],mix.parameters"; }
63 else if (type == "summary") { pattern = "[filename],[distance],mix.summary"; }
64 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
69 m->errorOut(e, "GetMetaCommunityCommand", "getOutputPattern");
73 //**********************************************************************************************************************
74 GetMetaCommunityCommand::GetMetaCommunityCommand(){
76 abort = true; calledHelp = true;
78 vector<string> tempOutNames;
79 outputTypes["fit"] = tempOutNames;
80 outputTypes["relabund"] = tempOutNames;
81 outputTypes["matrix"] = tempOutNames;
82 outputTypes["design"] = tempOutNames;
83 outputTypes["parameters"] = tempOutNames;
84 outputTypes["summary"] = tempOutNames;
87 m->errorOut(e, "GetMetaCommunityCommand", "GetMetaCommunityCommand");
91 //**********************************************************************************************************************
92 GetMetaCommunityCommand::GetMetaCommunityCommand(string option) {
94 abort = false; calledHelp = false;
97 //allow user to run help
98 if(option == "help") { help(); abort = true; calledHelp = true; }
99 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
102 //valid paramters for this command
103 vector<string> myArray = setParameters();
105 OptionParser parser(option);
106 map<string,string> parameters = parser.getParameters();
108 ValidParameters validParameter;
109 map<string,string>::iterator it;
110 //check to make sure all parameters are valid for command
111 for (it = parameters.begin(); it != parameters.end(); it++) {
112 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
115 vector<string> tempOutNames;
116 outputTypes["fit"] = tempOutNames;
117 outputTypes["relabund"] = tempOutNames;
118 outputTypes["matrix"] = tempOutNames;
119 outputTypes["design"] = tempOutNames;
120 outputTypes["parameters"] = tempOutNames;
121 outputTypes["summary"] = tempOutNames;
123 //if the user changes the input directory command factory will send this info to us in the output parameter
124 string inputDir = validParameter.validFile(parameters, "inputdir", false);
125 if (inputDir == "not found"){ inputDir = ""; }
128 it = parameters.find("shared");
129 if(it != parameters.end()){
130 path = m->hasPath(it->second);
131 if (path == "") { parameters["shared"] = inputDir + it->second; }
135 //get shared file, it is required
136 sharedfile = validParameter.validFile(parameters, "shared", true);
137 if (sharedfile == "not open") { sharedfile = ""; abort = true; }
138 else if (sharedfile == "not found") {
139 //if there is a current shared file, use it
140 sharedfile = m->getSharedFile();
141 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
142 else { m->mothurOut("You have no current sharedfile and the shared parameter is required."); m->mothurOutEndLine(); abort = true; }
143 }else { m->setSharedFile(sharedfile); }
145 //if the user changes the output directory command factory will send this info to us in the output parameter
146 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
147 outputDir = m->hasPath(sharedfile); //if user entered a file with a path then preserve it
150 string temp = validParameter.validFile(parameters, "minpartitions", false); if (temp == "not found"){ temp = "5"; }
151 m->mothurConvert(temp, minpartitions);
153 temp = validParameter.validFile(parameters, "maxpartitions", false); if (temp == "not found"){ temp = "10"; }
154 m->mothurConvert(temp, maxpartitions);
156 temp = validParameter.validFile(parameters, "optimizegap", false); if (temp == "not found"){ temp = "3"; }
157 m->mothurConvert(temp, optimizegap);
159 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
160 m->setProcessors(temp);
161 m->mothurConvert(temp, processors);
163 string groups = validParameter.validFile(parameters, "groups", false);
164 if (groups == "not found") { groups = ""; }
165 else { m->splitAtDash(groups, Groups); }
166 m->setGroups(Groups);
168 string label = validParameter.validFile(parameters, "label", false);
169 if (label == "not found") { label = ""; }
171 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
172 else { allLines = 1; }
177 catch(exception& e) {
178 m->errorOut(e, "GetMetaCommunityCommand", "GetMetaCommunityCommand");
182 //**********************************************************************************************************************
184 int GetMetaCommunityCommand::execute(){
187 if (abort == true) { if (calledHelp) { return 0; } return 2; }
189 InputData input(sharedfile, "sharedfile");
190 vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
191 string lastLabel = lookup[0]->getLabel();
193 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
194 set<string> processedLabels;
195 set<string> userLabels = labels;
197 //as long as you are not at the end of the file or done wih the lines you want
198 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
200 if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } return 0; }
202 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
204 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
206 createProcesses(lookup);
208 processedLabels.insert(lookup[0]->getLabel());
209 userLabels.erase(lookup[0]->getLabel());
212 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
213 string saveLabel = lookup[0]->getLabel();
215 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
216 lookup = input.getSharedRAbundVectors(lastLabel);
217 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
219 createProcesses(lookup);
221 processedLabels.insert(lookup[0]->getLabel());
222 userLabels.erase(lookup[0]->getLabel());
224 //restore real lastlabel to save below
225 lookup[0]->setLabel(saveLabel);
228 lastLabel = lookup[0]->getLabel();
229 //prevent memory leak
230 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
232 if (m->control_pressed) { return 0; }
234 //get next line to process
235 lookup = input.getSharedRAbundVectors();
238 if (m->control_pressed) { return 0; }
240 //output error messages about any remaining user labels
241 set<string>::iterator it;
242 bool needToRun = false;
243 for (it = userLabels.begin(); it != userLabels.end(); it++) {
244 m->mothurOut("Your file does not include the label " + *it);
245 if (processedLabels.count(lastLabel) != 1) {
246 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
249 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
253 //run last label if you need to
254 if (needToRun == true) {
255 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
256 lookup = input.getSharedRAbundVectors(lastLabel);
258 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
260 createProcesses(lookup);
262 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
265 //output files created by command
266 m->mothurOutEndLine();
267 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
268 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
269 m->mothurOutEndLine();
273 catch(exception& e) {
274 m->errorOut(e, "GetMetaCommunityCommand", "execute");
278 //**********************************************************************************************************************
279 int GetMetaCommunityCommand::createProcesses(vector<SharedRAbundVector*>& thislookup){
282 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
284 processors=1; //qFinderDMM not thread safe
287 vector<int> processIDS;
290 int minPartition = 0;
293 if (maxpartitions < processors) { processors = maxpartitions; }
295 map<string, string> variables;
296 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(sharedfile));
297 variables["[distance]"] = thislookup[0]->getLabel();
298 string outputFileName = getOutputFileName("fit", variables);
299 outputNames.push_back(outputFileName); outputTypes["fit"].push_back(outputFileName);
301 //divide the partitions between the processors
302 vector< vector<int> > dividedPartitions;
303 vector< vector<string> > rels, matrix;
304 vector<string> doneFlags;
305 dividedPartitions.resize(processors);
306 rels.resize(processors);
307 matrix.resize(processors);
309 //for each file group figure out which process will complete it
310 //want to divide the load intelligently so the big files are spread between processes
311 for (int i=1; i<=maxpartitions; i++) {
313 int processToAssign = (i+1) % processors;
314 if (processToAssign == 0) { processToAssign = processors; }
316 if (m->debug) { m->mothurOut("[DEBUG]: assigning " + toString(i) + " to process " + toString(processToAssign-1) + "\n"); }
317 dividedPartitions[(processToAssign-1)].push_back(i);
319 variables["[tag]"] = toString(i);
320 string relName = getOutputFileName("relabund", variables);
321 string mName = getOutputFileName("matrix", variables);
322 rels[(processToAssign-1)].push_back(relName);
323 matrix[(processToAssign-1)].push_back(mName);
326 for (int i = 0; i < processors; i++) { //read from everyone elses, just write to yours
327 string tempDoneFile = toString(i) + ".done.temp";
328 doneFlags.push_back(tempDoneFile);
330 m->openOutputFile(tempDoneFile, out); //clear out
335 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
337 //loop through and create all the processes you want
338 while (process != processors) {
342 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
346 num = processDriver(thislookup, dividedPartitions[process], (outputFileName + toString(getpid())), rels[process], matrix[process], doneFlags, process);
348 //pass numSeqs to parent
350 string tempFile = toString(getpid()) + ".outputNames.temp";
351 m->openOutputFile(tempFile, out);
353 out << outputNames.size() << endl;
354 for (int i = 0; i < outputNames.size(); i++) { out << outputNames[i] << endl; }
359 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
360 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
366 minPartition = processDriver(thislookup, dividedPartitions[0], outputFileName, rels[0], matrix[0], doneFlags, 0);
368 //force parent to wait until all the processes are done
369 for (int i=0;i<processIDS.size();i++) {
370 int temp = processIDS[i];
374 vector<string> tempOutputNames = outputNames;
375 for (int i=0;i<processIDS.size();i++) {
377 string tempFile = toString(processIDS[i]) + ".outputNames.temp";
378 m->openInputFile(tempFile, in);
381 in >> tempNum; m->gobble(in);
382 if (tempNum < minPartition) { minPartition = tempNum; }
383 in >> tempNum; m->gobble(in);
384 for (int i = 0; i < tempNum; i++) {
385 string tempName = "";
386 in >> tempName; m->gobble(in);
387 tempOutputNames.push_back(tempName);
390 in.close(); m->mothurRemove(tempFile);
392 m->appendFilesWithoutHeaders(outputFileName + toString(processIDS[i]), outputFileName);
393 m->mothurRemove(outputFileName + toString(processIDS[i]));
396 if (processors > 1) {
398 for (int i = 0; i < tempOutputNames.size(); i++) { //remove files if needed
399 string name = tempOutputNames[i];
400 vector<string> parts;
401 m->splitAtChar(name, parts, '.');
403 if (((parts[parts.size()-1] == "relabund") || (parts[parts.size()-1] == "posterior")) && (parts[parts.size()-2] == "mix")) {
404 string tempNum = parts[parts.size()-3];
405 int num; m->mothurConvert(tempNum, num);
406 //if (num > minPartition) {
407 // m->mothurRemove(tempOutputNames[i]);
408 // keep = false; if (m->debug) { m->mothurOut("[DEBUG]: removing " + tempOutputNames[i] + ".\n"); }
411 if (keep) { outputNames.push_back(tempOutputNames[i]); }
416 m->openInputFile(outputFileName, in);
417 string headers = m->getline(in); m->gobble(in);
419 map<int, string> file;
421 string numString, line;
423 in >> numString; line = m->getline(in); m->gobble(in);
424 m->mothurConvert(numString, num);
429 m->openOutputFile(outputFileName, out);
430 out << headers << endl;
431 for (map<int, string>::iterator it = file.begin(); it != file.end(); it++) {
432 out << it->first << '\t' << it->second << endl;
433 if (m->debug) { m->mothurOut("[DEBUG]: printing: " + toString(it->first) + '\t' + it->second + ".\n"); }
440 vector<metaCommunityData*> pDataArray;
441 DWORD dwThreadIdArray[processors-1];
442 HANDLE hThreadArray[processors-1];
444 //Create processor worker threads.
445 for( int i=1; i<processors; i++ ){
447 //make copy of lookup so we don't get access violations
448 vector<SharedRAbundVector*> newLookup;
449 for (int k = 0; k < thislookup.size(); k++) {
450 SharedRAbundVector* temp = new SharedRAbundVector();
451 temp->setLabel(thislookup[k]->getLabel());
452 temp->setGroup(thislookup[k]->getGroup());
453 newLookup.push_back(temp);
457 for (int k = 0; k < thislookup[0]->getNumBins(); k++) {
458 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
459 for (int j = 0; j < thislookup.size(); j++) { newLookup[j]->push_back(thislookup[j]->getAbundance(k), thislookup[j]->getGroup()); }
462 processIDS.push_back(i);
464 // Allocate memory for thread data.
465 metaCommunityData* tempMeta = new metaCommunityData(newLookup, m, dividedPartitions[i], outputFileName + toString(i), rels[i], matrix[i], minpartitions, maxpartitions, optimizegap);
466 pDataArray.push_back(tempMeta);
468 hThreadArray[i] = CreateThread(NULL, 0, MyMetaCommunityThreadFunction, pDataArray[i], 0, &dwThreadIdArray[i]);
472 minPartition = processDriver(thislookup, dividedPartitions[0], outputFileName, rels[0], matrix[0]);
474 //Wait until all threads have terminated.
475 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
477 //Close all thread handles and free memory allocations.
478 for(int i=0; i < pDataArray.size(); i++){
479 if (pDataArray[i]->minPartition < minPartition) { minPartition = pDataArray[i]->minPartition; }
480 for (int j = 0; j < pDataArray[i]->outputNames.size(); j++) {
481 outputNames.push_back(pDataArray[i]->outputNames[j]);
483 m->appendFilesWithoutHeaders(outputFileName + toString(processIDS[i]), outputFileName);
484 m->mothurRemove(outputFileName + toString(processIDS[i]));
485 CloseHandle(hThreadArray[i]);
486 delete pDataArray[i];
489 minPartition = processDriver(thislookup, dividedPartitions[0], outputFileName, rels[0], matrix[0], doneFlags, 0);
491 for (int i = 0; i < processors; i++) { //read from everyone elses, just write to yours
492 string tempDoneFile = toString(i) + ".done.temp";
493 m->mothurRemove(tempDoneFile);
496 if (m->control_pressed) { return 0; }
498 if (m->debug) { m->mothurOut("[DEBUG]: minPartition = " + toString(minPartition) + "\n"); }
500 //run generate Summary function for smallest minPartition
501 variables["[tag]"] = toString(minPartition);
502 generateSummaryFile(minPartition, variables);
507 catch(exception& e) {
508 m->errorOut(e, "GetMetaCommunityCommand", "createProcesses");
512 //**********************************************************************************************************************
513 int GetMetaCommunityCommand::processDriver(vector<SharedRAbundVector*>& thislookup, vector<int>& parts, string outputFileName, vector<string> relabunds, vector<string> matrix, vector<string> doneFlags, int processID){
516 double minLaplace = 1e10;
517 int minPartition = 0;
520 m->openOutputFile(outputFileName, fitData);
521 fitData.setf(ios::fixed, ios::floatfield);
522 fitData.setf(ios::showpoint);
523 cout.setf(ios::fixed, ios::floatfield);
524 cout.setf(ios::showpoint);
526 vector< vector<int> > sharedMatrix;
527 for (int i = 0; i < thislookup.size(); i++) { sharedMatrix.push_back(thislookup[i]->getAbundances()); }
529 m->mothurOut("K\tNLE\t\tlogDet\tBIC\t\tAIC\t\tLaplace\n");
530 fitData << "K\tNLE\tlogDet\tBIC\tAIC\tLaplace" << endl;
532 for(int i=0;i<parts.size();i++){
534 int numPartitions = parts[i];
536 if (m->debug) { m->mothurOut("[DEBUG]: running partition " + toString(numPartitions) + "\n"); }
538 if (m->control_pressed) { break; }
540 //check to see if anyone else is done
541 for (int j = 0; j < doneFlags.size(); j++) {
542 if (!m->isBlank(doneFlags[j])) { //another process has finished
543 //are they done at a lower partition?
545 m->openInputFile(doneFlags[j], in);
547 in >> tempNum; in.close();
548 if (tempNum < numPartitions) { break; } //quit, because someone else has finished
552 qFinderDMM findQ(sharedMatrix, numPartitions);
554 double laplace = findQ.getLaplace();
555 m->mothurOut(toString(numPartitions) + '\t');
556 cout << setprecision (2) << findQ.getNLL() << '\t' << findQ.getLogDet() << '\t';
557 m->mothurOutJustToLog(toString(findQ.getNLL()) + '\t' + toString(findQ.getLogDet()) + '\t');
558 cout << findQ.getBIC() << '\t' << findQ.getAIC() << '\t' << laplace;
559 m->mothurOutJustToLog(toString(findQ.getBIC()) + '\t' + toString(findQ.getAIC()) + '\t' + toString(laplace));
561 fitData << numPartitions << '\t';
562 fitData << setprecision (2) << findQ.getNLL() << '\t' << findQ.getLogDet() << '\t';
563 fitData << findQ.getBIC() << '\t' << findQ.getAIC() << '\t' << laplace << endl;
565 if(laplace < minLaplace){
566 minPartition = numPartitions;
567 minLaplace = laplace;
570 m->mothurOutEndLine();
572 string relabund = relabunds[i];
573 outputNames.push_back(relabund); outputTypes["relabund"].push_back(relabund);
574 string matrixName = matrix[i];
575 outputNames.push_back(matrixName); outputTypes["matrix"].push_back(matrixName);
577 findQ.printZMatrix(matrixName, m->getGroups());
578 findQ.printRelAbund(relabund, m->currentBinLabels);
580 if(optimizegap != -1 && (numPartitions - minPartition) >= optimizegap && numPartitions >= minpartitions){
581 string tempDoneFile = toString(processID) + ".done.temp";
583 m->openOutputFile(tempDoneFile, outDone);
584 outDone << minPartition << endl;
593 if (m->control_pressed) { return 0; }
597 catch(exception& e) {
598 m->errorOut(e, "GetMetaCommunityCommand", "processDriver");
602 /**************************************************************************************************/
604 vector<double> GetMetaCommunityCommand::generateDesignFile(int numPartitions, map<string,string> variables){
606 vector<double> piValues(numPartitions, 0);
609 variables["[tag]"] = toString(numPartitions);
610 string input = getOutputFileName("matrix", variables);
611 m->openInputFile(input, postFile);//((fileRoot + toString(numPartitions) + "mix.posterior").c_str()); //matrix file
612 variables.erase("[tag]");
613 string outputFileName = getOutputFileName("design", variables);
615 m->openOutputFile(outputFileName, designFile);
616 outputNames.push_back(outputFileName); outputTypes["design"].push_back(outputFileName);
619 vector<string> titles(numPartitions);
621 for(int i=0;i<numPartitions;i++){ postFile >> titles[i]; }
629 if (m->control_pressed) { break; }
631 double maxPosterior = 0.0000;
632 int maxPartition = -1;
634 postFile >> sampleName;
636 for(int i=0;i<numPartitions;i++){
638 postFile >> posterior;
639 if(posterior > maxPosterior){
640 maxPosterior = posterior;
643 piValues[i] += posterior;
647 designFile << sampleName << '\t' << titles[maxPartition] << endl;
652 for(int i=0;i<numPartitions;i++){
653 piValues[i] /= (double)numSamples;
662 catch(exception& e) {
663 m->errorOut(e, "GetMetaCommunityCommand", "generateDesignFile");
668 /**************************************************************************************************/
670 inline bool summaryFunction(summaryData i, summaryData j){ return i.difference > j.difference; }
672 /**************************************************************************************************/
673 int GetMetaCommunityCommand::generateSummaryFile(int numPartitions, map<string,string> v){
675 vector<summaryData> summary;
677 vector<double> pMean(numPartitions, 0);
678 vector<double> pLCI(numPartitions, 0);
679 vector<double> pUCI(numPartitions, 0);
682 double mean, lci, uci;
685 vector<double> piValues = generateDesignFile(numPartitions, v);
687 ifstream referenceFile;
688 map<string, string> variables;
689 variables["[filename]"] = v["[filename]"];
690 variables["[distance]"] = v["[distance]"];
691 variables["[tag]"] = "1";
692 string reference = getOutputFileName("relabund", variables);
693 m->openInputFile(reference, referenceFile); //((fileRoot + label + ".1mix.relabund").c_str());
694 variables["[tag]"] = toString(numPartitions);
695 string partFile = getOutputFileName("relabund", variables);
696 ifstream partitionFile;
697 m->openInputFile(partFile, partitionFile); //((fileRoot + toString(numPartitions) + "mix.relabund").c_str());
699 header = m->getline(referenceFile);
700 header = m->getline(partitionFile);
701 stringstream head(header);
704 vector<string> thetaValues(numPartitions, "");
705 for(int i=0;i<numPartitions;i++){
706 head >> label >> dummy >> dummy;
707 thetaValues[i] = label.substr(label.find_last_of('_')+1);
711 vector<double> partitionDiff(numPartitions, 0.0000);
713 while(referenceFile){
715 if (m->control_pressed) { break; }
717 referenceFile >> name >> mean >> lci >> uci;
719 summaryData tempData;
720 tempData.name = name;
721 tempData.refMean = mean;
723 double difference = 0.0000;
725 partitionFile >> name;
726 for(int j=0;j<numPartitions;j++){
727 partitionFile >> pMean[j] >> pLCI[j] >> pUCI[j];
728 difference += abs(mean - pMean[j]);
729 partitionDiff[j] += abs(mean - pMean[j]);;
732 tempData.partMean = pMean;
733 tempData.partLCI = pLCI;
734 tempData.partUCI = pUCI;
735 tempData.difference = difference;
736 summary.push_back(tempData);
738 m->gobble(referenceFile);
739 m->gobble(partitionFile);
741 referenceFile.close();
742 partitionFile.close();
744 if (m->control_pressed) { return 0; }
746 int numOTUs = (int)summary.size();
748 sort(summary.begin(), summary.end(), summaryFunction);
750 variables.erase("[tag]");
751 string outputFileName = getOutputFileName("parameters", variables);
752 outputNames.push_back(outputFileName); outputTypes["parameters"].push_back(outputFileName);
754 ofstream parameterFile;
755 m->openOutputFile(outputFileName, parameterFile); //((fileRoot + "mix.parameters").c_str());
756 parameterFile.setf(ios::fixed, ios::floatfield);
757 parameterFile.setf(ios::showpoint);
759 double totalDifference = 0.0000;
760 parameterFile << "Part\tDif2Ref_i\ttheta_i\tpi_i\n";
761 for(int i=0;i<numPartitions;i++){
762 if (m->control_pressed) { break; }
763 parameterFile << i+1 << '\t' << setprecision(2) << partitionDiff[i] << '\t' << thetaValues[i] << '\t' << piValues[i] << endl;
764 totalDifference += partitionDiff[i];
766 parameterFile.close();
768 if (m->control_pressed) { return 0; }
770 string summaryFileName = getOutputFileName("summary", variables);
771 outputNames.push_back(summaryFileName); outputTypes["summary"].push_back(summaryFileName);
773 ofstream summaryFile;
774 m->openOutputFile(summaryFileName, summaryFile); //((fileRoot + "mix.summary").c_str());
775 summaryFile.setf(ios::fixed, ios::floatfield);
776 summaryFile.setf(ios::showpoint);
779 summaryFile << "OTU\tP0.mean";
780 for(int i=0;i<numPartitions;i++){
781 summaryFile << "\tP" << i+1 << ".mean\tP" << i+1 << ".lci\tP" << i+1 << ".uci";
783 summaryFile << "\tDifference\tCumFraction" << endl;
785 double cumDiff = 0.0000;
787 for(int i=0;i<numOTUs;i++){
788 if (m->control_pressed) { break; }
789 summaryFile << summary[i].name << setprecision(2) << '\t' << summary[i].refMean;
790 for(int j=0;j<numPartitions;j++){
791 summaryFile << '\t' << summary[i].partMean[j] << '\t' << summary[i].partLCI[j] << '\t' << summary[i].partUCI[j];
794 cumDiff += summary[i].difference/totalDifference;
795 summaryFile << '\t' << summary[i].difference << '\t' << cumDiff << endl;
802 catch(exception& e) {
803 m->errorOut(e, "GetMetaCommunityCommand", "generateSummaryFile");
808 //**********************************************************************************************************************