forked from mothur/mothur
/
getmetacommunitycommand.cpp
811 lines (654 loc) · 34.1 KB
/
getmetacommunitycommand.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
//
// getmetacommunitycommand.cpp
// Mothur
//
// Created by SarahsWork on 4/9/13.
// Copyright (c) 2013 Schloss Lab. All rights reserved.
//
#include "getmetacommunitycommand.h"
//**********************************************************************************************************************
vector<string> GetMetaCommunityCommand::setParameters(){
try {
CommandParameter pshared("shared", "InputTypes", "", "", "none", "none", "none","outputType",false,true); parameters.push_back(pshared);
CommandParameter pgroups("groups", "String", "", "", "", "", "","",false,false); parameters.push_back(pgroups);
CommandParameter plabel("label", "String", "", "", "", "", "","",false,false); parameters.push_back(plabel);
CommandParameter pminpartitions("minpartitions", "Number", "", "5", "", "", "","",false,false,true); parameters.push_back(pminpartitions);
CommandParameter pmaxpartitions("maxpartitions", "Number", "", "100", "", "", "","",false,false,true); parameters.push_back(pmaxpartitions);
CommandParameter poptimizegap("optimizegap", "Number", "", "3", "", "", "","",false,false,true); parameters.push_back(poptimizegap);
CommandParameter pprocessors("processors", "Number", "", "1", "", "", "","",false,false,true); parameters.push_back(pprocessors);
CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
vector<string> myArray;
for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
return myArray;
}
catch(exception& e) {
m->errorOut(e, "NewCommand", "setParameters");
exit(1);
}
}
//**********************************************************************************************************************
string GetMetaCommunityCommand::getHelpString(){
try {
string helpString = "";
helpString += "The get.communitytype command parameters are shared, label, groups, minpartitions, maxpartitions, optimizegap and processors. The shared file is required. \n";
helpString += "The label parameter is used to analyze specific labels in your input. labels are separated by dashes.\n";
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";
helpString += "The minpartitions parameter is used to .... Default=5.\n";
helpString += "The maxpartitions parameter is used to .... Default=10.\n";
helpString += "The optimizegap parameter is used to .... Default=3.\n";
helpString += "The processors parameter allows you to specify number of processors to use. The default is 1.\n";
helpString += "The get.communitytype command should be in the following format: get.communitytype(shared=yourSharedFile).\n";
return helpString;
}
catch(exception& e) {
m->errorOut(e, "GetMetaCommunityCommand", "getHelpString");
exit(1);
}
}
//**********************************************************************************************************************
string GetMetaCommunityCommand::getOutputPattern(string type) {
try {
string pattern = "";
if (type == "fit") { pattern = "[filename],[distance],mix.fit"; }
else if (type == "relabund") { pattern = "[filename],[distance],[tag],mix.relabund"; }
else if (type == "design") { pattern = "[filename],[distance],mix.design"; }
else if (type == "matrix") { pattern = "[filename],[distance],[tag],mix.posterior"; }
else if (type == "parameters") { pattern = "[filename],[distance],mix.parameters"; }
else if (type == "summary") { pattern = "[filename],[distance],mix.summary"; }
else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
return pattern;
}
catch(exception& e) {
m->errorOut(e, "GetMetaCommunityCommand", "getOutputPattern");
exit(1);
}
}
//**********************************************************************************************************************
GetMetaCommunityCommand::GetMetaCommunityCommand(){
try {
abort = true; calledHelp = true;
setParameters();
vector<string> tempOutNames;
outputTypes["fit"] = tempOutNames;
outputTypes["relabund"] = tempOutNames;
outputTypes["matrix"] = tempOutNames;
outputTypes["design"] = tempOutNames;
outputTypes["parameters"] = tempOutNames;
outputTypes["summary"] = tempOutNames;
}
catch(exception& e) {
m->errorOut(e, "GetMetaCommunityCommand", "GetMetaCommunityCommand");
exit(1);
}
}
//**********************************************************************************************************************
GetMetaCommunityCommand::GetMetaCommunityCommand(string option) {
try {
abort = false; calledHelp = false;
allLines=true;
//allow user to run help
if(option == "help") { help(); abort = true; calledHelp = true; }
else if(option == "citation") { citation(); abort = true; calledHelp = true;}
else {
//valid paramters for this command
vector<string> myArray = setParameters();
OptionParser parser(option);
map<string,string> parameters = parser.getParameters();
ValidParameters validParameter;
map<string,string>::iterator it;
//check to make sure all parameters are valid for command
for (it = parameters.begin(); it != parameters.end(); it++) {
if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
}
vector<string> tempOutNames;
outputTypes["fit"] = tempOutNames;
outputTypes["relabund"] = tempOutNames;
outputTypes["matrix"] = tempOutNames;
outputTypes["design"] = tempOutNames;
outputTypes["parameters"] = tempOutNames;
outputTypes["summary"] = tempOutNames;
//if the user changes the input directory command factory will send this info to us in the output parameter
string inputDir = validParameter.validFile(parameters, "inputdir", false);
if (inputDir == "not found"){ inputDir = ""; }
else {
string path;
it = parameters.find("shared");
if(it != parameters.end()){
path = m->hasPath(it->second);
if (path == "") { parameters["shared"] = inputDir + it->second; }
}
}
//get shared file, it is required
sharedfile = validParameter.validFile(parameters, "shared", true);
if (sharedfile == "not open") { sharedfile = ""; abort = true; }
else if (sharedfile == "not found") {
//if there is a current shared file, use it
sharedfile = m->getSharedFile();
if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
else { m->mothurOut("You have no current sharedfile and the shared parameter is required."); m->mothurOutEndLine(); abort = true; }
}else { m->setSharedFile(sharedfile); }
//if the user changes the output directory command factory will send this info to us in the output parameter
outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
outputDir = m->hasPath(sharedfile); //if user entered a file with a path then preserve it
}
string temp = validParameter.validFile(parameters, "minpartitions", false); if (temp == "not found"){ temp = "5"; }
m->mothurConvert(temp, minpartitions);
temp = validParameter.validFile(parameters, "maxpartitions", false); if (temp == "not found"){ temp = "10"; }
m->mothurConvert(temp, maxpartitions);
temp = validParameter.validFile(parameters, "optimizegap", false); if (temp == "not found"){ temp = "3"; }
m->mothurConvert(temp, optimizegap);
temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
m->setProcessors(temp);
m->mothurConvert(temp, processors);
string groups = validParameter.validFile(parameters, "groups", false);
if (groups == "not found") { groups = ""; }
else { m->splitAtDash(groups, Groups); }
m->setGroups(Groups);
string label = validParameter.validFile(parameters, "label", false);
if (label == "not found") { label = ""; }
else {
if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
else { allLines = 1; }
}
}
}
catch(exception& e) {
m->errorOut(e, "GetMetaCommunityCommand", "GetMetaCommunityCommand");
exit(1);
}
}
//**********************************************************************************************************************
int GetMetaCommunityCommand::execute(){
try {
if (abort == true) { if (calledHelp) { return 0; } return 2; }
InputData input(sharedfile, "sharedfile");
vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
string lastLabel = lookup[0]->getLabel();
//if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
set<string> processedLabels;
set<string> userLabels = labels;
//as long as you are not at the end of the file or done wih the lines you want
while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } return 0; }
if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
createProcesses(lookup);
processedLabels.insert(lookup[0]->getLabel());
userLabels.erase(lookup[0]->getLabel());
}
if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
string saveLabel = lookup[0]->getLabel();
for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
lookup = input.getSharedRAbundVectors(lastLabel);
m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
createProcesses(lookup);
processedLabels.insert(lookup[0]->getLabel());
userLabels.erase(lookup[0]->getLabel());
//restore real lastlabel to save below
lookup[0]->setLabel(saveLabel);
}
lastLabel = lookup[0]->getLabel();
//prevent memory leak
for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
if (m->control_pressed) { return 0; }
//get next line to process
lookup = input.getSharedRAbundVectors();
}
if (m->control_pressed) { return 0; }
//output error messages about any remaining user labels
set<string>::iterator it;
bool needToRun = false;
for (it = userLabels.begin(); it != userLabels.end(); it++) {
m->mothurOut("Your file does not include the label " + *it);
if (processedLabels.count(lastLabel) != 1) {
m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
needToRun = true;
}else {
m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
}
}
//run last label if you need to
if (needToRun == true) {
for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
lookup = input.getSharedRAbundVectors(lastLabel);
m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
createProcesses(lookup);
for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
}
//output files created by command
m->mothurOutEndLine();
m->mothurOut("Output File Names: "); m->mothurOutEndLine();
for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
m->mothurOutEndLine();
return 0;
}
catch(exception& e) {
m->errorOut(e, "GetMetaCommunityCommand", "execute");
exit(1);
}
}
//**********************************************************************************************************************
int GetMetaCommunityCommand::createProcesses(vector<SharedRAbundVector*>& thislookup){
try {
#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
#else
processors=1; //qFinderDMM not thread safe
#endif
vector<int> processIDS;
int process = 1;
int num = 0;
int minPartition = 0;
//sanity check
if (maxpartitions < processors) { processors = maxpartitions; }
map<string, string> variables;
variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(sharedfile));
variables["[distance]"] = thislookup[0]->getLabel();
string outputFileName = getOutputFileName("fit", variables);
outputNames.push_back(outputFileName); outputTypes["fit"].push_back(outputFileName);
//divide the partitions between the processors
vector< vector<int> > dividedPartitions;
vector< vector<string> > rels, matrix;
vector<string> doneFlags;
dividedPartitions.resize(processors);
rels.resize(processors);
matrix.resize(processors);
//for each file group figure out which process will complete it
//want to divide the load intelligently so the big files are spread between processes
for (int i=1; i<=maxpartitions; i++) {
//cout << i << endl;
int processToAssign = (i+1) % processors;
if (processToAssign == 0) { processToAssign = processors; }
if (m->debug) { m->mothurOut("[DEBUG]: assigning " + toString(i) + " to process " + toString(processToAssign-1) + "\n"); }
dividedPartitions[(processToAssign-1)].push_back(i);
variables["[tag]"] = toString(i);
string relName = getOutputFileName("relabund", variables);
string mName = getOutputFileName("matrix", variables);
rels[(processToAssign-1)].push_back(relName);
matrix[(processToAssign-1)].push_back(mName);
}
for (int i = 0; i < processors; i++) { //read from everyone elses, just write to yours
string tempDoneFile = m->getRootName(m->getSimpleName(sharedfile)) + toString(i) + ".done.temp";
doneFlags.push_back(tempDoneFile);
ofstream out;
m->openOutputFile(tempDoneFile, out); //clear out
out.close();
}
#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
//loop through and create all the processes you want
while (process != processors) {
int pid = fork();
if (pid > 0) {
processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
process++;
}else if (pid == 0){
outputNames.clear();
num = processDriver(thislookup, dividedPartitions[process], (outputFileName + toString(getpid())), rels[process], matrix[process], doneFlags, process);
//pass numSeqs to parent
ofstream out;
string tempFile = toString(getpid()) + ".outputNames.temp";
m->openOutputFile(tempFile, out);
out << num << endl;
out << outputNames.size() << endl;
for (int i = 0; i < outputNames.size(); i++) { out << outputNames[i] << endl; }
out.close();
exit(0);
}else {
m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
exit(0);
}
}
//do my part
m->mothurOut("K\tNLE\t\tlogDet\tBIC\t\tAIC\t\tLaplace\n");
minPartition = processDriver(thislookup, dividedPartitions[0], outputFileName, rels[0], matrix[0], doneFlags, 0);
//force parent to wait until all the processes are done
for (int i=0;i<processIDS.size();i++) {
int temp = processIDS[i];
wait(&temp);
}
vector<string> tempOutputNames = outputNames;
for (int i=0;i<processIDS.size();i++) {
ifstream in;
string tempFile = toString(processIDS[i]) + ".outputNames.temp";
m->openInputFile(tempFile, in);
if (!in.eof()) {
int tempNum = 0;
in >> tempNum; m->gobble(in);
if (tempNum < minPartition) { minPartition = tempNum; }
in >> tempNum; m->gobble(in);
for (int i = 0; i < tempNum; i++) {
string tempName = "";
in >> tempName; m->gobble(in);
tempOutputNames.push_back(tempName);
}
}
in.close(); m->mothurRemove(tempFile);
m->appendFilesWithoutHeaders(outputFileName + toString(processIDS[i]), outputFileName);
m->mothurRemove(outputFileName + toString(processIDS[i]));
}
if (processors > 1) {
outputNames.clear();
for (int i = 0; i < tempOutputNames.size(); i++) { //remove files if needed
string name = tempOutputNames[i];
vector<string> parts;
m->splitAtChar(name, parts, '.');
bool keep = true;
if (((parts[parts.size()-1] == "relabund") || (parts[parts.size()-1] == "posterior")) && (parts[parts.size()-2] == "mix")) {
string tempNum = parts[parts.size()-3];
int num; m->mothurConvert(tempNum, num);
//if (num > minPartition) {
// m->mothurRemove(tempOutputNames[i]);
// keep = false; if (m->debug) { m->mothurOut("[DEBUG]: removing " + tempOutputNames[i] + ".\n"); }
//}
}
if (keep) { outputNames.push_back(tempOutputNames[i]); }
}
//reorder fit file
ifstream in;
m->openInputFile(outputFileName, in);
string headers = m->getline(in); m->gobble(in);
map<int, string> file;
while (!in.eof()) {
string numString, line;
int num;
in >> numString; line = m->getline(in); m->gobble(in);
m->mothurConvert(numString, num);
file[num] = line;
}
in.close();
ofstream out;
m->openOutputFile(outputFileName, out);
out << headers << endl;
for (map<int, string>::iterator it = file.begin(); it != file.end(); it++) {
out << it->first << '\t' << it->second << endl;
if (m->debug) { m->mothurOut("[DEBUG]: printing: " + toString(it->first) + '\t' + it->second + ".\n"); }
}
out.close();
}
#else
/*
vector<metaCommunityData*> pDataArray;
DWORD dwThreadIdArray[processors-1];
HANDLE hThreadArray[processors-1];
//Create processor worker threads.
for( int i=1; i<processors; i++ ){
//copy lookup
//make copy of lookup so we don't get access violations
vector<SharedRAbundVector*> newLookup;
for (int k = 0; k < thislookup.size(); k++) {
SharedRAbundVector* temp = new SharedRAbundVector();
temp->setLabel(thislookup[k]->getLabel());
temp->setGroup(thislookup[k]->getGroup());
newLookup.push_back(temp);
}
//for each bin
for (int k = 0; k < thislookup[0]->getNumBins(); k++) {
if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
for (int j = 0; j < thislookup.size(); j++) { newLookup[j]->push_back(thislookup[j]->getAbundance(k), thislookup[j]->getGroup()); }
}
processIDS.push_back(i);
// Allocate memory for thread data.
metaCommunityData* tempMeta = new metaCommunityData(newLookup, m, dividedPartitions[i], outputFileName + toString(i), rels[i], matrix[i], minpartitions, maxpartitions, optimizegap);
pDataArray.push_back(tempMeta);
hThreadArray[i] = CreateThread(NULL, 0, MyMetaCommunityThreadFunction, pDataArray[i], 0, &dwThreadIdArray[i]);
}
//do my part
minPartition = processDriver(thislookup, dividedPartitions[0], outputFileName, rels[0], matrix[0]);
//Wait until all threads have terminated.
WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
//Close all thread handles and free memory allocations.
for(int i=0; i < pDataArray.size(); i++){
if (pDataArray[i]->minPartition < minPartition) { minPartition = pDataArray[i]->minPartition; }
for (int j = 0; j < pDataArray[i]->outputNames.size(); j++) {
outputNames.push_back(pDataArray[i]->outputNames[j]);
}
m->appendFilesWithoutHeaders(outputFileName + toString(processIDS[i]), outputFileName);
m->mothurRemove(outputFileName + toString(processIDS[i]));
CloseHandle(hThreadArray[i]);
delete pDataArray[i];
} */
//do my part
m->mothurOut("K\tNLE\t\tlogDet\tBIC\t\tAIC\t\tLaplace\n");
minPartition = processDriver(thislookup, dividedPartitions[0], outputFileName, rels[0], matrix[0], doneFlags, 0);
#endif
for (int i = 0; i < processors; i++) { //read from everyone elses, just write to yours
string tempDoneFile = m->getRootName(m->getSimpleName(sharedfile)) + toString(i) + ".done.temp";
m->mothurRemove(tempDoneFile);
}
if (m->control_pressed) { return 0; }
if (m->debug) { m->mothurOut("[DEBUG]: minPartition = " + toString(minPartition) + "\n"); }
//run generate Summary function for smallest minPartition
variables["[tag]"] = toString(minPartition);
generateSummaryFile(minPartition, variables);
return 0;
}
catch(exception& e) {
m->errorOut(e, "GetMetaCommunityCommand", "createProcesses");
exit(1);
}
}
//**********************************************************************************************************************
int GetMetaCommunityCommand::processDriver(vector<SharedRAbundVector*>& thislookup, vector<int>& parts, string outputFileName, vector<string> relabunds, vector<string> matrix, vector<string> doneFlags, int processID){
try {
double minLaplace = 1e10;
int minPartition = 0;
ofstream fitData;
m->openOutputFile(outputFileName, fitData);
fitData.setf(ios::fixed, ios::floatfield);
fitData.setf(ios::showpoint);
cout.setf(ios::fixed, ios::floatfield);
cout.setf(ios::showpoint);
vector< vector<int> > sharedMatrix;
vector<string> thisGroups;
for (int i = 0; i < thislookup.size(); i++) { sharedMatrix.push_back(thislookup[i]->getAbundances()); thisGroups.push_back(thislookup[i]->getGroup()); }
fitData << "K\tNLE\tlogDet\tBIC\tAIC\tLaplace" << endl;
for(int i=0;i<parts.size();i++){
int numPartitions = parts[i];
if (m->debug) { m->mothurOut("[DEBUG]: running partition " + toString(numPartitions) + "\n"); }
if (m->control_pressed) { break; }
//check to see if anyone else is done
for (int j = 0; j < doneFlags.size(); j++) {
if (!m->isBlank(doneFlags[j])) { //another process has finished
//are they done at a lower partition?
ifstream in;
m->openInputFile(doneFlags[j], in);
int tempNum;
in >> tempNum; in.close();
if (tempNum < numPartitions) { break; } //quit, because someone else has finished
}
}
qFinderDMM findQ(sharedMatrix, numPartitions);
double laplace = findQ.getLaplace();
m->mothurOut(toString(numPartitions) + '\t');
cout << setprecision (2) << findQ.getNLL() << '\t' << findQ.getLogDet() << '\t';
m->mothurOutJustToLog(toString(findQ.getNLL()) + '\t' + toString(findQ.getLogDet()) + '\t');
cout << findQ.getBIC() << '\t' << findQ.getAIC() << '\t' << laplace;
m->mothurOutJustToLog(toString(findQ.getBIC()) + '\t' + toString(findQ.getAIC()) + '\t' + toString(laplace));
fitData << numPartitions << '\t';
fitData << setprecision (2) << findQ.getNLL() << '\t' << findQ.getLogDet() << '\t';
fitData << findQ.getBIC() << '\t' << findQ.getAIC() << '\t' << laplace << endl;
if(laplace < minLaplace){
minPartition = numPartitions;
minLaplace = laplace;
m->mothurOut("***");
}
m->mothurOutEndLine();
string relabund = relabunds[i];
outputNames.push_back(relabund); outputTypes["relabund"].push_back(relabund);
string matrixName = matrix[i];
outputNames.push_back(matrixName); outputTypes["matrix"].push_back(matrixName);
findQ.printZMatrix(matrixName, thisGroups);
findQ.printRelAbund(relabund, m->currentSharedBinLabels);
if(optimizegap != -1 && (numPartitions - minPartition) >= optimizegap && numPartitions >= minpartitions){
string tempDoneFile = m->getRootName(m->getSimpleName(sharedfile)) + toString(processID) + ".done.temp";
ofstream outDone;
m->openOutputFile(tempDoneFile, outDone);
outDone << minPartition << endl;
outDone.close();
break;
}
}
fitData.close();
//minPartition = 4;
if (m->control_pressed) { return 0; }
return minPartition;
}
catch(exception& e) {
m->errorOut(e, "GetMetaCommunityCommand", "processDriver");
exit(1);
}
}
/**************************************************************************************************/
vector<double> GetMetaCommunityCommand::generateDesignFile(int numPartitions, map<string,string> variables){
try {
vector<double> piValues(numPartitions, 0);
ifstream postFile;
variables["[tag]"] = toString(numPartitions);
string input = getOutputFileName("matrix", variables);
m->openInputFile(input, postFile);//((fileRoot + toString(numPartitions) + "mix.posterior").c_str()); //matrix file
variables.erase("[tag]");
string outputFileName = getOutputFileName("design", variables);
ofstream designFile;
m->openOutputFile(outputFileName, designFile);
outputNames.push_back(outputFileName); outputTypes["design"].push_back(outputFileName);
vector<string> titles(numPartitions);
for(int i=0;i<numPartitions;i++){ postFile >> titles[i]; }
double posterior;
string sampleName;
int numSamples = 0;
while(postFile){
if (m->control_pressed) { break; }
double maxPosterior = 0.0000;
int maxPartition = -1;
postFile >> sampleName;
for(int i=0;i<numPartitions;i++){
postFile >> posterior;
if(posterior > maxPosterior){
maxPosterior = posterior;
maxPartition = i;
}
piValues[i] += posterior;
}
designFile << sampleName << '\t' << titles[maxPartition] << endl;
numSamples++;
m->gobble(postFile);
}
for(int i=0;i<numPartitions;i++){
piValues[i] /= (double)numSamples;
}
postFile.close();
designFile.close();
return piValues;
}
catch(exception& e) {
m->errorOut(e, "GetMetaCommunityCommand", "generateDesignFile");
exit(1);
}
}
/**************************************************************************************************/
inline bool summaryFunction(summaryData i, summaryData j){ return i.difference > j.difference; }
/**************************************************************************************************/
int GetMetaCommunityCommand::generateSummaryFile(int numPartitions, map<string,string> v){
try {
vector<summaryData> summary;
vector<double> pMean(numPartitions, 0);
vector<double> pLCI(numPartitions, 0);
vector<double> pUCI(numPartitions, 0);
string name, header;
double mean, lci, uci;
vector<double> piValues = generateDesignFile(numPartitions, v);
ifstream referenceFile;
map<string, string> variables;
variables["[filename]"] = v["[filename]"];
variables["[distance]"] = v["[distance]"];
variables["[tag]"] = "1";
string reference = getOutputFileName("relabund", variables);
m->openInputFile(reference, referenceFile); //((fileRoot + label + ".1mix.relabund").c_str());
variables["[tag]"] = toString(numPartitions);
string partFile = getOutputFileName("relabund", variables);
ifstream partitionFile;
m->openInputFile(partFile, partitionFile); //((fileRoot + toString(numPartitions) + "mix.relabund").c_str());
header = m->getline(referenceFile);
header = m->getline(partitionFile);
stringstream head(header);
string dummy, label;
head >> dummy;
vector<string> thetaValues(numPartitions, "");
for(int i=0;i<numPartitions;i++){
head >> label >> dummy >> dummy;
thetaValues[i] = label.substr(label.find_last_of('_')+1);
}
vector<double> partitionDiff(numPartitions, 0.0000);
while(referenceFile){
if (m->control_pressed) { break; }
referenceFile >> name >> mean >> lci >> uci;
summaryData tempData;
tempData.name = name;
tempData.refMean = mean;
double difference = 0.0000;
partitionFile >> name;
for(int j=0;j<numPartitions;j++){
partitionFile >> pMean[j] >> pLCI[j] >> pUCI[j];
difference += abs(mean - pMean[j]);
partitionDiff[j] += abs(mean - pMean[j]);;
}
tempData.partMean = pMean;
tempData.partLCI = pLCI;
tempData.partUCI = pUCI;
tempData.difference = difference;
summary.push_back(tempData);
m->gobble(referenceFile);
m->gobble(partitionFile);
}
referenceFile.close();
partitionFile.close();
if (m->control_pressed) { return 0; }
int numOTUs = (int)summary.size();
sort(summary.begin(), summary.end(), summaryFunction);
variables.erase("[tag]");
string outputFileName = getOutputFileName("parameters", variables);
outputNames.push_back(outputFileName); outputTypes["parameters"].push_back(outputFileName);
ofstream parameterFile;
m->openOutputFile(outputFileName, parameterFile); //((fileRoot + "mix.parameters").c_str());
parameterFile.setf(ios::fixed, ios::floatfield);
parameterFile.setf(ios::showpoint);
double totalDifference = 0.0000;
parameterFile << "Part\tDif2Ref_i\ttheta_i\tpi_i\n";
for(int i=0;i<numPartitions;i++){
if (m->control_pressed) { break; }
parameterFile << i+1 << '\t' << setprecision(2) << partitionDiff[i] << '\t' << thetaValues[i] << '\t' << piValues[i] << endl;
totalDifference += partitionDiff[i];
}
parameterFile.close();
if (m->control_pressed) { return 0; }
string summaryFileName = getOutputFileName("summary", variables);
outputNames.push_back(summaryFileName); outputTypes["summary"].push_back(summaryFileName);
ofstream summaryFile;
m->openOutputFile(summaryFileName, summaryFile); //((fileRoot + "mix.summary").c_str());
summaryFile.setf(ios::fixed, ios::floatfield);
summaryFile.setf(ios::showpoint);
summaryFile << "OTU\tP0.mean";
for(int i=0;i<numPartitions;i++){
summaryFile << "\tP" << i+1 << ".mean\tP" << i+1 << ".lci\tP" << i+1 << ".uci";
}
summaryFile << "\tDifference\tCumFraction" << endl;
double cumDiff = 0.0000;
for(int i=0;i<numOTUs;i++){
if (m->control_pressed) { break; }
summaryFile << summary[i].name << setprecision(2) << '\t' << summary[i].refMean;
for(int j=0;j<numPartitions;j++){
summaryFile << '\t' << summary[i].partMean[j] << '\t' << summary[i].partLCI[j] << '\t' << summary[i].partUCI[j];
}
cumDiff += summary[i].difference/totalDifference;
summaryFile << '\t' << summary[i].difference << '\t' << cumDiff << endl;
}
summaryFile.close();
return 0;
}
catch(exception& e) {
m->errorOut(e, "GetMetaCommunityCommand", "generateSummaryFile");
exit(1);
}
}
//**********************************************************************************************************************