2 * matrixoutputcommand.cpp
5 * Created by Sarah Westcott on 5/20/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "matrixoutputcommand.h"
11 #include "subsample.h"
13 //**********************************************************************************************************************
14 vector<string> MatrixOutputCommand::setParameters(){
16 CommandParameter pshared("shared", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pshared);
17 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
18 CommandParameter psubsample("subsample", "String", "", "", "", "", "",false,false); parameters.push_back(psubsample);
19 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
20 CommandParameter pcalc("calc", "Multiple", "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan-kstest-sharednseqs-ochiai-anderberg-kulczynski-kulczynskicody-lennon-morisitahorn-braycurtis-whittaker-odum-canberra-structeuclidean-structchord-hellinger-manhattan-structpearson-soergel-spearman-structkulczynski-speciesprofile-hamming-structchi2-gower-memchi2-memchord-memeuclidean-mempearson", "jclass-thetayc", "", "", "",true,false); parameters.push_back(pcalc);
21 CommandParameter poutput("output", "Multiple", "lt-square", "lt", "", "", "",false,false); parameters.push_back(poutput);
22 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
23 CommandParameter piters("iters", "Number", "", "1000", "", "", "",false,false); parameters.push_back(piters);
24 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, "MatrixOutputCommand", "setParameters");
36 //**********************************************************************************************************************
37 string MatrixOutputCommand::getHelpString(){
39 string helpString = "";
40 ValidCalculators validCalculator;
41 helpString += "The dist.shared command parameters are shared, groups, calc, output, processors, subsample, iters and label. shared is a required, unless you have a valid current file.\n";
42 helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like included used.\n";
43 helpString += "The group names are separated by dashes. The label parameter allows you to select what distance levels you would like distance matrices created for, and is also separated by dashes.\n";
44 helpString += "The iters parameter allows you to choose the number of times you would like to run the subsample.\n";
45 helpString += "The subsample parameter allows you to enter the size pergroup of the sample or you can set subsample=T and mothur will use the size of your smallest group.\n";
46 helpString += "The dist.shared command should be in the following format: dist.shared(groups=yourGroups, calc=yourCalcs, label=yourLabels).\n";
47 helpString += "The output parameter allows you to specify format of your distance matrix. Options are lt, and square. The default is lt.\n";
48 helpString += "Example dist.shared(groups=A-B-C, calc=jabund-sorabund).\n";
49 helpString += "The default value for groups is all the groups in your groupfile.\n";
50 helpString += "The default value for calc is jclass and thetayc.\n";
51 helpString += validCalculator.printCalc("matrix");
52 helpString += "The dist.shared command outputs a .dist file for each calculator you specify at each distance you choose.\n";
53 helpString += "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n";
57 m->errorOut(e, "MatrixOutputCommand", "getHelpString");
61 //**********************************************************************************************************************
62 MatrixOutputCommand::MatrixOutputCommand(){
64 abort = true; calledHelp = true;
66 vector<string> tempOutNames;
67 outputTypes["phylip"] = tempOutNames;
68 outputTypes["subsample"] = tempOutNames;
71 m->errorOut(e, "MatrixOutputCommand", "MatrixOutputCommand");
75 //**********************************************************************************************************************
77 MatrixOutputCommand::MatrixOutputCommand(string option) {
79 abort = false; calledHelp = false;
82 //allow user to run help
83 if(option == "help") { help(); abort = true; calledHelp = true; }
84 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
87 vector<string> myArray = setParameters();
89 OptionParser parser(option);
90 map<string,string> parameters = parser.getParameters();
91 map<string,string>::iterator it;
93 ValidParameters validParameter;
95 //check to make sure all parameters are valid for command
96 for (it = parameters.begin(); it != parameters.end(); it++) {
97 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
100 //initialize outputTypes
101 vector<string> tempOutNames;
102 outputTypes["phylip"] = tempOutNames;
103 outputTypes["subsample"] = tempOutNames;
105 //if the user changes the input directory command factory will send this info to us in the output parameter
106 string inputDir = validParameter.validFile(parameters, "inputdir", false);
107 if (inputDir == "not found"){ inputDir = ""; }
110 it = parameters.find("shared");
111 //user has given a template file
112 if(it != parameters.end()){
113 path = m->hasPath(it->second);
114 //if the user has not given a path then, add inputdir. else leave path alone.
115 if (path == "") { parameters["shared"] = inputDir + it->second; }
119 sharedfile = validParameter.validFile(parameters, "shared", true);
120 if (sharedfile == "not found") {
121 //if there is a current shared file, use it
122 sharedfile = m->getSharedFile();
123 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
124 else { m->mothurOut("You have no current sharedfile and the shared parameter is required."); m->mothurOutEndLine(); abort = true; }
125 }else if (sharedfile == "not open") { sharedfile = ""; abort = true; }
126 else { m->setSharedFile(sharedfile); }
128 //if the user changes the output directory command factory will send this info to us in the output parameter
129 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
131 outputDir += m->hasPath(sharedfile); //if user entered a file with a path then preserve it
134 //check for optional parameter and set defaults
135 // ...at some point should added some additional type checking...
136 label = validParameter.validFile(parameters, "label", false);
137 if (label == "not found") { label = ""; }
139 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
140 else { allLines = 1; }
143 output = validParameter.validFile(parameters, "output", false); if(output == "not found"){ output = "lt"; }
144 if ((output != "lt") && (output != "square")) { m->mothurOut(output + " is not a valid output form. Options are lt and square. I will use lt."); m->mothurOutEndLine(); output = "lt"; }
146 groups = validParameter.validFile(parameters, "groups", false);
147 if (groups == "not found") { groups = ""; }
149 m->splitAtDash(groups, Groups);
150 m->setGroups(Groups);
153 string temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
154 m->setProcessors(temp);
155 m->mothurConvert(temp, processors);
157 calc = validParameter.validFile(parameters, "calc", false);
158 if (calc == "not found") { calc = "jclass-thetayc"; }
160 if (calc == "default") { calc = "jclass-thetayc"; }
162 m->splitAtDash(calc, Estimators);
163 if (m->inUsersGroups("citation", Estimators)) {
164 ValidCalculators validCalc; validCalc.printCitations(Estimators);
165 //remove citation from list of calcs
166 for (int i = 0; i < Estimators.size(); i++) { if (Estimators[i] == "citation") { Estimators.erase(Estimators.begin()+i); break; } }
169 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "1000"; }
170 m->mothurConvert(temp, iters);
172 temp = validParameter.validFile(parameters, "subsample", false); if (temp == "not found") { temp = "F"; }
173 if (m->isNumeric1(temp)) { m->mothurConvert(temp, subsampleSize); subsample = true; }
175 if (m->isTrue(temp)) { subsample = true; subsampleSize = -1; } //we will set it to smallest group later
176 else { subsample = false; }
179 if (subsample == false) { iters = 1; }
181 if (abort == false) {
183 ValidCalculators validCalculator;
186 for (i=0; i<Estimators.size(); i++) {
187 if (validCalculator.isValidCalculator("matrix", Estimators[i]) == true) {
188 if (Estimators[i] == "sharedsobs") {
189 matrixCalculators.push_back(new SharedSobsCS());
190 }else if (Estimators[i] == "sharedchao") {
191 matrixCalculators.push_back(new SharedChao1());
192 }else if (Estimators[i] == "sharedace") {
193 matrixCalculators.push_back(new SharedAce());
194 }else if (Estimators[i] == "jabund") {
195 matrixCalculators.push_back(new JAbund());
196 }else if (Estimators[i] == "sorabund") {
197 matrixCalculators.push_back(new SorAbund());
198 }else if (Estimators[i] == "jclass") {
199 matrixCalculators.push_back(new Jclass());
200 }else if (Estimators[i] == "sorclass") {
201 matrixCalculators.push_back(new SorClass());
202 }else if (Estimators[i] == "jest") {
203 matrixCalculators.push_back(new Jest());
204 }else if (Estimators[i] == "sorest") {
205 matrixCalculators.push_back(new SorEst());
206 }else if (Estimators[i] == "thetayc") {
207 matrixCalculators.push_back(new ThetaYC());
208 }else if (Estimators[i] == "thetan") {
209 matrixCalculators.push_back(new ThetaN());
210 }else if (Estimators[i] == "kstest") {
211 matrixCalculators.push_back(new KSTest());
212 }else if (Estimators[i] == "sharednseqs") {
213 matrixCalculators.push_back(new SharedNSeqs());
214 }else if (Estimators[i] == "ochiai") {
215 matrixCalculators.push_back(new Ochiai());
216 }else if (Estimators[i] == "anderberg") {
217 matrixCalculators.push_back(new Anderberg());
218 }else if (Estimators[i] == "kulczynski") {
219 matrixCalculators.push_back(new Kulczynski());
220 }else if (Estimators[i] == "kulczynskicody") {
221 matrixCalculators.push_back(new KulczynskiCody());
222 }else if (Estimators[i] == "lennon") {
223 matrixCalculators.push_back(new Lennon());
224 }else if (Estimators[i] == "morisitahorn") {
225 matrixCalculators.push_back(new MorHorn());
226 }else if (Estimators[i] == "braycurtis") {
227 matrixCalculators.push_back(new BrayCurtis());
228 }else if (Estimators[i] == "whittaker") {
229 matrixCalculators.push_back(new Whittaker());
230 }else if (Estimators[i] == "odum") {
231 matrixCalculators.push_back(new Odum());
232 }else if (Estimators[i] == "canberra") {
233 matrixCalculators.push_back(new Canberra());
234 }else if (Estimators[i] == "structeuclidean") {
235 matrixCalculators.push_back(new StructEuclidean());
236 }else if (Estimators[i] == "structchord") {
237 matrixCalculators.push_back(new StructChord());
238 }else if (Estimators[i] == "hellinger") {
239 matrixCalculators.push_back(new Hellinger());
240 }else if (Estimators[i] == "manhattan") {
241 matrixCalculators.push_back(new Manhattan());
242 }else if (Estimators[i] == "structpearson") {
243 matrixCalculators.push_back(new StructPearson());
244 }else if (Estimators[i] == "soergel") {
245 matrixCalculators.push_back(new Soergel());
246 }else if (Estimators[i] == "spearman") {
247 matrixCalculators.push_back(new Spearman());
248 }else if (Estimators[i] == "structkulczynski") {
249 matrixCalculators.push_back(new StructKulczynski());
250 }else if (Estimators[i] == "speciesprofile") {
251 matrixCalculators.push_back(new SpeciesProfile());
252 }else if (Estimators[i] == "hamming") {
253 matrixCalculators.push_back(new Hamming());
254 }else if (Estimators[i] == "structchi2") {
255 matrixCalculators.push_back(new StructChi2());
256 }else if (Estimators[i] == "gower") {
257 matrixCalculators.push_back(new Gower());
258 }else if (Estimators[i] == "memchi2") {
259 matrixCalculators.push_back(new MemChi2());
260 }else if (Estimators[i] == "memchord") {
261 matrixCalculators.push_back(new MemChord());
262 }else if (Estimators[i] == "memeuclidean") {
263 matrixCalculators.push_back(new MemEuclidean());
264 }else if (Estimators[i] == "mempearson") {
265 matrixCalculators.push_back(new MemPearson());
274 catch(exception& e) {
275 m->errorOut(e, "MatrixOutputCommand", "MatrixOutputCommand");
280 //**********************************************************************************************************************
282 MatrixOutputCommand::~MatrixOutputCommand(){}
284 //**********************************************************************************************************************
286 int MatrixOutputCommand::execute(){
289 if (abort == true) { if (calledHelp) { return 0; } return 2; }
291 //if the users entered no valid calculators don't execute command
292 if (matrixCalculators.size() == 0) { m->mothurOut("No valid calculators."); m->mothurOutEndLine(); return 0; }
294 input = new InputData(sharedfile, "sharedfile");
295 lookup = input->getSharedRAbundVectors();
296 string lastLabel = lookup[0]->getLabel();
298 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
299 set<string> processedLabels;
300 set<string> userLabels = labels;
302 if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups. I cannot run the command."); m->mothurOutEndLine(); delete input; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } return 0;}
304 numGroups = lookup.size();
305 lines.resize(processors);
306 for (int i = 0; i < processors; i++) {
307 lines[i].start = int (sqrt(float(i)/float(processors)) * numGroups);
308 lines[i].end = int (sqrt(float(i+1)/float(processors)) * numGroups);
312 if (subsampleSize == -1) { //user has not set size, set size = smallest samples size
313 subsampleSize = lookup[0]->getNumSeqs();
314 for (int i = 1; i < lookup.size(); i++) {
315 int thisSize = lookup[i]->getNumSeqs();
317 if (thisSize < subsampleSize) { subsampleSize = thisSize; }
322 vector<SharedRAbundVector*> temp;
323 for (int i = 0; i < lookup.size(); i++) {
324 if (lookup[i]->getNumSeqs() < subsampleSize) {
325 m->mothurOut(lookup[i]->getGroup() + " contains " + toString(lookup[i]->getNumSeqs()) + ". Eliminating."); m->mothurOutEndLine();
328 Groups.push_back(lookup[i]->getGroup());
329 temp.push_back(lookup[i]);
333 m->setGroups(Groups);
337 if (m->control_pressed) { delete input; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } m->clearGroups(); return 0; }
339 //as long as you are not at the end of the file or done wih the lines you want
340 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
342 if (m->control_pressed) { outputTypes.clear(); delete input; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } m->clearGroups(); return 0; }
344 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
345 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
348 processedLabels.insert(lookup[0]->getLabel());
349 userLabels.erase(lookup[0]->getLabel());
352 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
353 string saveLabel = lookup[0]->getLabel();
355 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
356 lookup = input->getSharedRAbundVectors(lastLabel);
358 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
361 processedLabels.insert(lookup[0]->getLabel());
362 userLabels.erase(lookup[0]->getLabel());
364 //restore real lastlabel to save below
365 lookup[0]->setLabel(saveLabel);
368 lastLabel = lookup[0]->getLabel();
370 //get next line to process
371 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
372 lookup = input->getSharedRAbundVectors();
375 if (m->control_pressed) { outputTypes.clear(); delete input; for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } m->clearGroups(); return 0; }
377 //output error messages about any remaining user labels
378 set<string>::iterator it;
379 bool needToRun = false;
380 for (it = userLabels.begin(); it != userLabels.end(); it++) {
381 m->mothurOut("Your file does not include the label " + *it);
382 if (processedLabels.count(lastLabel) != 1) {
383 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
386 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
390 if (m->control_pressed) { outputTypes.clear(); delete input; for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } m->clearGroups(); return 0; }
392 //run last label if you need to
393 if (needToRun == true) {
394 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
395 lookup = input->getSharedRAbundVectors(lastLabel);
397 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
399 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
402 if (m->control_pressed) { outputTypes.clear(); delete input; for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } m->clearGroups(); return 0; }
404 //reset groups parameter
407 //set phylip file as new current phylipfile
409 itTypes = outputTypes.find("phylip");
410 if (itTypes != outputTypes.end()) {
411 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setPhylipFile(current); }
414 m->mothurOutEndLine();
415 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
416 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
417 m->mothurOutEndLine();
422 catch(exception& e) {
423 m->errorOut(e, "MatrixOutputCommand", "execute");
427 /***********************************************************/
428 void MatrixOutputCommand::printSims(ostream& out, vector< vector<double> >& simMatrix) {
431 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
434 out << simMatrix.size() << endl;
436 if (output == "lt") {
437 for (int m = 0; m < simMatrix.size(); m++) {
438 out << lookup[m]->getGroup() << '\t';
439 for (int n = 0; n < m; n++) {
440 out << simMatrix[m][n] << '\t';
445 for (int m = 0; m < simMatrix.size(); m++) {
446 out << lookup[m]->getGroup() << '\t';
447 for (int n = 0; n < simMatrix[m].size(); n++) {
448 out << simMatrix[m][n] << '\t';
454 catch(exception& e) {
455 m->errorOut(e, "MatrixOutputCommand", "printSims");
459 /***********************************************************/
460 int MatrixOutputCommand::process(vector<SharedRAbundVector*> thisLookup){
463 vector<SharedRAbundVector*> subset;
464 vector< vector< vector<seqDist> > > calcDistsTotals; //each iter, one for each calc, then each groupCombos dists. this will be used to make .dist files
466 vector< vector<seqDist> > calcDists; calcDists.resize(matrixCalculators.size());
468 for (int thisIter = 0; thisIter < iters; thisIter++) {
470 vector<SharedRAbundVector*> thisItersLookup = thisLookup;
474 vector<string> tempLabels; //dont need since we arent printing the sampled sharedRabunds
476 //make copy of lookup so we don't get access violations
477 vector<SharedRAbundVector*> newLookup;
478 for (int k = 0; k < thisItersLookup.size(); k++) {
479 SharedRAbundVector* temp = new SharedRAbundVector();
480 temp->setLabel(thisItersLookup[k]->getLabel());
481 temp->setGroup(thisItersLookup[k]->getGroup());
482 newLookup.push_back(temp);
486 for (int k = 0; k < thisItersLookup[0]->getNumBins(); k++) {
487 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
488 for (int j = 0; j < thisItersLookup.size(); j++) { newLookup[j]->push_back(thisItersLookup[j]->getAbundance(k), thisItersLookup[j]->getGroup()); }
491 tempLabels = sample.getSample(newLookup, subsampleSize);
492 thisItersLookup = newLookup;
496 driver(thisItersLookup, 0, numGroups, calcDists);
499 vector<int> processIDS;
501 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
502 //loop through and create all the processes you want
503 while (process != processors) {
507 processIDS.push_back(pid);
511 driver(thisItersLookup, lines[process].start, lines[process].end, calcDists);
513 string tempdistFileName = m->getRootName(m->getSimpleName(sharedfile)) + toString(getpid()) + ".dist";
515 m->openOutputFile(tempdistFileName, outtemp);
517 for (int i = 0; i < calcDists.size(); i++) {
518 outtemp << calcDists[i].size() << endl;
520 for (int j = 0; j < calcDists[i].size(); j++) {
521 outtemp << calcDists[i][j].seq1 << '\t' << calcDists[i][j].seq2 << '\t' << calcDists[i][j].dist << endl;
528 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
529 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
534 //parent do your part
535 driver(thisItersLookup, lines[0].start, lines[0].end, calcDists);
537 //force parent to wait until all the processes are done
538 for (int i = 0; i < processIDS.size(); i++) {
539 int temp = processIDS[i];
543 for (int i = 0; i < processIDS.size(); i++) {
544 string tempdistFileName = m->getRootName(m->getSimpleName(sharedfile)) + toString(processIDS[i]) + ".dist";
546 m->openInputFile(tempdistFileName, intemp);
548 for (int k = 0; k < calcDists.size(); k++) {
550 intemp >> size; m->gobble(intemp);
552 for (int j = 0; j < size; j++) {
557 intemp >> seq1 >> seq2 >> dist; m->gobble(intemp);
559 seqDist tempDist(seq1, seq2, dist);
560 calcDists[k].push_back(tempDist);
564 m->mothurRemove(tempdistFileName);
567 //////////////////////////////////////////////////////////////////////////////////////////////////////
568 //Windows version shared memory, so be careful when passing variables through the distSharedData struct.
569 //Above fork() will clone, so memory is separate, but that's not the case with windows,
570 //Taking advantage of shared memory to pass results vectors.
571 //////////////////////////////////////////////////////////////////////////////////////////////////////
573 vector<distSharedData*> pDataArray;
574 DWORD dwThreadIdArray[processors-1];
575 HANDLE hThreadArray[processors-1];
577 //Create processor worker threads.
578 for( int i=1; i<processors; i++ ){
580 //make copy of lookup so we don't get access violations
581 vector<SharedRAbundVector*> newLookup;
582 for (int k = 0; k < thisItersLookup.size(); k++) {
583 SharedRAbundVector* temp = new SharedRAbundVector();
584 temp->setLabel(thisItersLookup[k]->getLabel());
585 temp->setGroup(thisItersLookup[k]->getGroup());
586 newLookup.push_back(temp);
590 for (int k = 0; k < thisItersLookup[0]->getNumBins(); k++) {
591 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
592 for (int j = 0; j < thisItersLookup.size(); j++) { newLookup[j]->push_back(thisItersLookup[j]->getAbundance(k), thisItersLookup[j]->getGroup()); }
595 // Allocate memory for thread data.
596 distSharedData* tempSum = new distSharedData(m, lines[i].start, lines[i].end, Estimators, newLookup);
597 pDataArray.push_back(tempSum);
598 processIDS.push_back(i);
600 hThreadArray[i-1] = CreateThread(NULL, 0, MyDistSharedThreadFunction, pDataArray[i-1], 0, &dwThreadIdArray[i-1]);
603 //parent do your part
604 driver(thisItersLookup, lines[0].start, lines[0].end, calcDists);
606 //Wait until all threads have terminated.
607 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
609 //Close all thread handles and free memory allocations.
610 for(int i=0; i < pDataArray.size(); i++){
611 for (int j = 0; j < pDataArray[i]->thisLookup.size(); j++) { delete pDataArray[i]->thisLookup[j]; }
613 for (int k = 0; k < calcDists.size(); k++) {
614 int size = pDataArray[i]->calcDists[k].size();
615 for (int j = 0; j < size; j++) { calcDists[k].push_back(pDataArray[i]->calcDists[k][j]); }
618 CloseHandle(hThreadArray[i]);
619 delete pDataArray[i];
625 calcDistsTotals.push_back(calcDists);
630 for (int i = 0; i < thisItersLookup.size(); i++) { delete thisItersLookup[i]; }
631 thisItersLookup.clear();
632 for (int i = 0; i < calcDists.size(); i++) { calcDists[i].clear(); }
637 //we need to find the average distance and standard deviation for each groups distance
639 vector< vector<seqDist> > calcAverages; calcAverages.resize(matrixCalculators.size());
640 for (int i = 0; i < calcAverages.size(); i++) { //initialize sums to zero.
641 calcAverages[i].resize(calcDistsTotals[0][i].size());
643 for (int j = 0; j < calcAverages[i].size(); j++) {
644 calcAverages[i][j].seq1 = calcDists[i][j].seq1;
645 calcAverages[i][j].seq2 = calcDists[i][j].seq2;
646 calcAverages[i][j].dist = 0.0;
650 for (int thisIter = 0; thisIter < iters; thisIter++) { //sum all groups dists for each calculator
651 for (int i = 0; i < calcAverages.size(); i++) { //initialize sums to zero.
652 for (int j = 0; j < calcAverages[i].size(); j++) {
653 calcAverages[i][j].dist += calcDistsTotals[thisIter][i][j].dist;
658 for (int i = 0; i < calcAverages.size(); i++) { //finds average.
659 for (int j = 0; j < calcAverages[i].size(); j++) {
660 calcAverages[i][j].dist /= (float) iters;
664 //find standard deviation
665 vector< vector<seqDist> > stdDev; stdDev.resize(matrixCalculators.size());
666 for (int i = 0; i < stdDev.size(); i++) { //initialize sums to zero.
667 stdDev[i].resize(calcDistsTotals[0][i].size());
669 for (int j = 0; j < stdDev[i].size(); j++) {
670 stdDev[i][j].seq1 = calcDists[i][j].seq1;
671 stdDev[i][j].seq2 = calcDists[i][j].seq2;
672 stdDev[i][j].dist = 0.0;
676 for (int thisIter = 0; thisIter < iters; thisIter++) { //compute the difference of each dist from the mean, and square the result of each
677 for (int i = 0; i < stdDev.size(); i++) {
678 for (int j = 0; j < stdDev[i].size(); j++) {
679 stdDev[i][j].dist += ((calcDistsTotals[thisIter][i][j].dist - calcAverages[i][j].dist) * (calcDistsTotals[thisIter][i][j].dist - calcAverages[i][j].dist));
684 for (int i = 0; i < stdDev.size(); i++) { //finds average.
685 for (int j = 0; j < stdDev[i].size(); j++) {
686 stdDev[i][j].dist /= (float) iters;
687 stdDev[i][j].dist = sqrt(stdDev[i][j].dist);
692 for (int i = 0; i < calcDists.size(); i++) {
693 vector< vector<double> > matrix; //square matrix to represent the distance
694 matrix.resize(thisLookup.size());
695 for (int k = 0; k < thisLookup.size(); k++) { matrix[k].resize(thisLookup.size(), 0.0); }
697 vector< vector<double> > stdmatrix; //square matrix to represent the stdDev
698 stdmatrix.resize(thisLookup.size());
699 for (int k = 0; k < thisLookup.size(); k++) { stdmatrix[k].resize(thisLookup.size(), 0.0); }
702 for (int j = 0; j < calcAverages[i].size(); j++) {
703 int row = calcAverages[i][j].seq1;
704 int column = calcAverages[i][j].seq2;
705 float dist = calcAverages[i][j].dist;
706 float stdDist = stdDev[i][j].dist;
708 matrix[row][column] = dist;
709 matrix[column][row] = dist;
710 stdmatrix[row][column] = stdDist;
711 stdmatrix[column][row] = stdDist;
714 string distFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + matrixCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + "." + output + ".ave.dist";
715 outputNames.push_back(distFileName); outputTypes["phylip"].push_back(distFileName);
717 m->openOutputFile(distFileName, outAve);
718 outAve.setf(ios::fixed, ios::floatfield); outAve.setf(ios::showpoint);
720 printSims(outAve, matrix);
724 distFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + matrixCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + "." + output + ".std.dist";
725 outputNames.push_back(distFileName); outputTypes["phylip"].push_back(distFileName);
727 m->openOutputFile(distFileName, outSTD);
728 outSTD.setf(ios::fixed, ios::floatfield); outSTD.setf(ios::showpoint);
730 printSims(outSTD, stdmatrix);
737 for (int i = 0; i < calcDists.size(); i++) {
738 if (m->control_pressed) { break; }
741 vector< vector<double> > matrix; //square matrix to represent the distance
742 matrix.resize(thisLookup.size());
743 for (int k = 0; k < thisLookup.size(); k++) { matrix[k].resize(thisLookup.size(), 0.0); }
745 for (int j = 0; j < calcDists[i].size(); j++) {
746 int row = calcDists[i][j].seq1;
747 int column = calcDists[i][j].seq2;
748 double dist = calcDists[i][j].dist;
750 matrix[row][column] = dist;
751 matrix[column][row] = dist;
754 string distFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + matrixCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + "." + output + ".dist";
755 outputNames.push_back(distFileName); outputTypes["phylip"].push_back(distFileName);
757 m->openOutputFile(distFileName, outDist);
758 outDist.setf(ios::fixed, ios::floatfield); outDist.setf(ios::showpoint);
760 printSims(outDist, matrix);
768 catch(exception& e) {
769 m->errorOut(e, "MatrixOutputCommand", "process");
773 /**************************************************************************************************/
774 int MatrixOutputCommand::driver(vector<SharedRAbundVector*> thisLookup, int start, int end, vector< vector<seqDist> >& calcDists) {
776 vector<SharedRAbundVector*> subset;
777 for (int k = start; k < end; k++) { // pass cdd each set of groups to compare
779 for (int l = 0; l < k; l++) {
781 if (k != l) { //we dont need to similiarity of a groups to itself
782 subset.clear(); //clear out old pair of sharedrabunds
783 //add new pair of sharedrabunds
784 subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]);
786 for(int i=0;i<matrixCalculators.size();i++) {
788 //if this calc needs all groups to calculate the pair load all groups
789 if (matrixCalculators[i]->getNeedsAll()) {
790 //load subset with rest of lookup for those calcs that need everyone to calc for a pair
791 for (int w = 0; w < thisLookup.size(); w++) {
792 if ((w != k) && (w != l)) { subset.push_back(thisLookup[w]); }
796 vector<double> tempdata = matrixCalculators[i]->getValues(subset); //saves the calculator outputs
798 if (m->control_pressed) { return 1; }
800 seqDist temp(l, k, tempdata[0]);
801 calcDists[i].push_back(temp);
809 catch(exception& e) {
810 m->errorOut(e, "MatrixOutputCommand", "driver");
814 /***********************************************************/