2 * treegroupscommand.cpp
5 * Created by Sarah Westcott on 4/8/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "treegroupscommand.h"
11 #include "subsample.h"
12 #include "consensus.h"
14 //**********************************************************************************************************************
15 vector<string> TreeGroupCommand::setParameters(){
17 CommandParameter pshared("shared", "InputTypes", "", "", "PhylipColumnShared", "PhylipColumnShared", "none","tree",false,false,true); parameters.push_back(pshared);
18 CommandParameter pphylip("phylip", "InputTypes", "", "", "PhylipColumnShared", "PhylipColumnShared", "none","tree",false,false); parameters.push_back(pphylip);
19 CommandParameter pname("name", "InputTypes", "", "", "NameCount", "none", "ColumnName","",false,false); parameters.push_back(pname);
20 CommandParameter pcount("count", "InputTypes", "", "", "NameCount", "none", "countcolumn","",false,false); parameters.push_back(pcount);
21 CommandParameter pcolumn("column", "InputTypes", "", "", "PhylipColumnShared", "PhylipColumnShared", "ColumnName-countcolumn","tree",false,false); parameters.push_back(pcolumn);
22 CommandParameter piters("iters", "Number", "", "1000", "", "", "","",false,false); parameters.push_back(piters);
23 CommandParameter psubsample("subsample", "String", "", "", "", "", "","",false,false); parameters.push_back(psubsample);
24 CommandParameter pcutoff("cutoff", "Number", "", "10", "", "", "","",false,false); parameters.push_back(pcutoff);
25 CommandParameter pprecision("precision", "Number", "", "100", "", "", "","",false,false); parameters.push_back(pprecision);
26 CommandParameter plabel("label", "String", "", "", "", "", "","",false,false); parameters.push_back(plabel);
27 CommandParameter pgroups("groups", "String", "", "", "", "", "","",false,false); parameters.push_back(pgroups);
28 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,true); parameters.push_back(pcalc);
30 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "","",false,false,true); parameters.push_back(pprocessors);
31 //CommandParameter poutput("output", "Multiple", "lt-square", "lt", "", "", "",false,false); parameters.push_back(poutput);
32 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
33 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
35 vector<string> myArray;
36 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
40 m->errorOut(e, "TreeGroupCommand", "setParameters");
44 //**********************************************************************************************************************
45 string TreeGroupCommand::getHelpString(){
47 string helpString = "";
48 ValidCalculators validCalculator;
49 helpString += "The tree.shared command creates a .tre to represent the similiarity between groups or sequences.\n";
50 helpString += "The tree.shared command parameters are shared, groups, calc, phylip, column, name, cutoff, precision, processors, subsample, iters and label.\n";
51 helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like included used.\n";
52 helpString += "The group names are separated by dashes. The label allow you to select what distance levels you would like trees created for, and are also separated by dashes.\n";
53 helpString += "The phylip or column parameter are required if you do not provide a sharedfile, and only one may be used. If you use a column file the name filename is required. \n";
54 helpString += "If you do not provide a cutoff value 10.00 is assumed. If you do not provide a precision value then 100 is assumed.\n";
55 helpString += "The tree.shared command should be in the following format: tree.shared(groups=yourGroups, calc=yourCalcs, label=yourLabels).\n";
56 helpString += "The iters parameter allows you to choose the number of times you would like to run the subsample.\n";
57 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. The subsample parameter may only be used with a shared file.\n";
58 helpString += "Example tree.shared(groups=A-B-C, calc=jabund-sorabund).\n";
59 helpString += "The default value for groups is all the groups in your groupfile.\n";
60 helpString += "The default value for calc is jclass-thetayc.\n";
61 helpString += "The tree.shared command outputs a .tre file for each calculator you specify at each distance you choose.\n";
62 helpString += validCalculator.printCalc("treegroup");
63 helpString += "Or the tree.shared command can be in the following format: tree.shared(phylip=yourPhylipFile).\n";
64 helpString += "Example tree.shared(phylip=abrecovery.dist).\n";
65 helpString += "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n";
69 m->errorOut(e, "TreeGroupCommand", "getHelpString");
73 //**********************************************************************************************************************
74 string TreeGroupCommand::getOutputPattern(string type) {
78 if (type == "tree") { pattern = "[filename],[calc],[distance],[tag],tre-[filename],tre"; }
79 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
84 m->errorOut(e, "TreeGroupCommand", "getOutputPattern");
88 //**********************************************************************************************************************
89 TreeGroupCommand::TreeGroupCommand(){
91 abort = true; calledHelp = true;
93 //initialize outputTypes
94 vector<string> tempOutNames;
95 outputTypes["tree"] = tempOutNames;
98 m->errorOut(e, "TreeGroupCommand", "TreeGroupCommand");
102 //**********************************************************************************************************************
104 TreeGroupCommand::TreeGroupCommand(string option) {
106 abort = false; calledHelp = false;
109 //allow user to run help
110 if(option == "help") { help(); abort = true; calledHelp = true; }
111 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
114 vector<string> myArray = setParameters();
116 OptionParser parser(option);
117 map<string, string> parameters = parser. getParameters();
119 ValidParameters validParameter;
120 map<string, string>::iterator it;
122 //check to make sure all parameters are valid for command
123 for (it = parameters.begin(); it != parameters.end(); it++) {
124 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
127 //initialize outputTypes
128 vector<string> tempOutNames;
129 outputTypes["tree"] = tempOutNames;
131 //if the user changes the input directory command factory will send this info to us in the output parameter
132 string inputDir = validParameter.validFile(parameters, "inputdir", false);
133 if (inputDir == "not found"){ inputDir = ""; }
136 it = parameters.find("phylip");
137 //user has given a template file
138 if(it != parameters.end()){
139 path = m->hasPath(it->second);
140 //if the user has not given a path then, add inputdir. else leave path alone.
141 if (path == "") { parameters["phylip"] = inputDir + it->second; }
144 it = parameters.find("column");
145 //user has given a template file
146 if(it != parameters.end()){
147 path = m->hasPath(it->second);
148 //if the user has not given a path then, add inputdir. else leave path alone.
149 if (path == "") { parameters["column"] = inputDir + it->second; }
152 it = parameters.find("name");
153 //user has given a template file
154 if(it != parameters.end()){
155 path = m->hasPath(it->second);
156 //if the user has not given a path then, add inputdir. else leave path alone.
157 if (path == "") { parameters["name"] = inputDir + it->second; }
160 it = parameters.find("count");
161 //user has given a template file
162 if(it != parameters.end()){
163 path = m->hasPath(it->second);
164 //if the user has not given a path then, add inputdir. else leave path alone.
165 if (path == "") { parameters["count"] = inputDir + it->second; }
169 //check for required parameters
170 phylipfile = validParameter.validFile(parameters, "phylip", true);
171 if (phylipfile == "not open") { phylipfile = ""; abort = true; }
172 else if (phylipfile == "not found") { phylipfile = ""; }
173 else { inputfile = phylipfile; format = "phylip"; m->setPhylipFile(phylipfile); }
175 columnfile = validParameter.validFile(parameters, "column", true);
176 if (columnfile == "not open") { columnfile = ""; abort = true; }
177 else if (columnfile == "not found") { columnfile = ""; }
178 else { inputfile = columnfile; format = "column"; m->setColumnFile(columnfile); }
180 sharedfile = validParameter.validFile(parameters, "shared", true);
181 if (sharedfile == "not open") { sharedfile = ""; abort = true; }
182 else if (sharedfile == "not found") { sharedfile = ""; }
183 else { inputfile = sharedfile; format = "sharedfile"; m->setSharedFile(sharedfile); }
185 namefile = validParameter.validFile(parameters, "name", true);
186 if (namefile == "not open") { abort = true; }
187 else if (namefile == "not found") { namefile = ""; }
188 else { m->setNameFile(namefile); }
190 countfile = validParameter.validFile(parameters, "count", true);
191 if (countfile == "not open") { abort = true; countfile = ""; }
192 else if (countfile == "not found") { countfile = ""; }
193 else { m->setCountTableFile(countfile); }
195 if ((phylipfile == "") && (columnfile == "") && (sharedfile == "")) {
196 //is there are current file available for either of these?
197 //give priority to shared, then column, then phylip
198 sharedfile = m->getSharedFile();
199 if (sharedfile != "") { inputfile = sharedfile; format = "sharedfile"; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
201 columnfile = m->getColumnFile();
202 if (columnfile != "") { inputfile = columnfile; format = "column"; m->mothurOut("Using " + columnfile + " as input file for the column parameter."); m->mothurOutEndLine(); }
204 phylipfile = m->getPhylipFile();
205 if (phylipfile != "") { inputfile = phylipfile; format = "phylip"; m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
207 m->mothurOut("No valid current files. You must provide a shared, phylip or column file."); m->mothurOutEndLine();
213 else if ((phylipfile != "") && (columnfile != "")) { m->mothurOut("When running the tree.shared command with a distance file you may not use both the column and the phylip parameters."); m->mothurOutEndLine(); abort = true; }
215 if (columnfile != "") {
216 if ((namefile == "") && (countfile == "")){
217 namefile = m->getNameFile();
218 if (namefile != "") { m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
220 countfile = m->getCountTableFile();
221 if (countfile != "") { m->mothurOut("Using " + countfile + " as input file for the count parameter."); m->mothurOutEndLine(); }
223 m->mothurOut("You need to provide a namefile or countfile if you are going to use the column format."); m->mothurOutEndLine();
231 //check for optional parameter and set defaults
232 // ...at some point should added some additional type checking...
233 label = validParameter.validFile(parameters, "label", false);
234 if (label == "not found") { label = ""; }
236 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
237 else { allLines = 1; }
240 groups = validParameter.validFile(parameters, "groups", false);
241 if (groups == "not found") { groups = ""; }
243 m->splitAtDash(groups, Groups);
244 m->setGroups(Groups);
247 calc = validParameter.validFile(parameters, "calc", false);
248 if (calc == "not found") { calc = "jclass-thetayc"; }
250 if (calc == "default") { calc = "jclass-thetayc"; }
252 m->splitAtDash(calc, Estimators);
253 if (m->inUsersGroups("citation", Estimators)) {
254 ValidCalculators validCalc; validCalc.printCitations(Estimators);
255 //remove citation from list of calcs
256 for (int i = 0; i < Estimators.size(); i++) { if (Estimators[i] == "citation") { Estimators.erase(Estimators.begin()+i); break; } }
260 temp = validParameter.validFile(parameters, "precision", false); if (temp == "not found") { temp = "100"; }
261 m->mothurConvert(temp, precision);
263 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found") { temp = "10"; }
264 m->mothurConvert(temp, cutoff);
265 cutoff += (5 / (precision * 10.0));
267 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
268 m->setProcessors(temp);
269 m->mothurConvert(temp, processors);
271 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "1000"; }
272 m->mothurConvert(temp, iters);
274 temp = validParameter.validFile(parameters, "subsample", false); if (temp == "not found") { temp = "F"; }
275 if (m->isNumeric1(temp)) { m->mothurConvert(temp, subsampleSize); subsample = true; }
277 if (m->isTrue(temp)) { subsample = true; subsampleSize = -1; } //we will set it to smallest group later
278 else { subsample = false; }
281 if (subsample == false) { iters = 1; }
283 if (subsample && (format != "sharedfile")) { m->mothurOut("[ERROR]: the subsample parameter can only be used with a shared file.\n"); abort=true; }
285 //if the user changes the output directory command factory will send this info to us in the output parameter
286 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
288 outputDir += m->hasPath(inputfile); //if user entered a file with a path then preserve it
293 catch(exception& e) {
294 m->errorOut(e, "TreeGroupCommand", "TreeGroupCommand");
298 //**********************************************************************************************************************
300 TreeGroupCommand::~TreeGroupCommand(){
301 if (abort == false) {
302 if (format == "sharedfile") { delete input; }
303 else { delete list; }
309 //**********************************************************************************************************************
311 int TreeGroupCommand::execute(){
314 if (abort == true) { if (calledHelp) { return 0; } return 2; }
316 if (format == "sharedfile") {
318 ValidCalculators validCalculator;
320 for (int i=0; i<Estimators.size(); i++) {
321 if (validCalculator.isValidCalculator("treegroup", Estimators[i]) == true) {
322 if (Estimators[i] == "sharedsobs") {
323 treeCalculators.push_back(new SharedSobsCS());
324 }else if (Estimators[i] == "sharedchao") {
325 treeCalculators.push_back(new SharedChao1());
326 }else if (Estimators[i] == "sharedace") {
327 treeCalculators.push_back(new SharedAce());
328 }else if (Estimators[i] == "jabund") {
329 treeCalculators.push_back(new JAbund());
330 }else if (Estimators[i] == "sorabund") {
331 treeCalculators.push_back(new SorAbund());
332 }else if (Estimators[i] == "jclass") {
333 treeCalculators.push_back(new Jclass());
334 }else if (Estimators[i] == "sorclass") {
335 treeCalculators.push_back(new SorClass());
336 }else if (Estimators[i] == "jest") {
337 treeCalculators.push_back(new Jest());
338 }else if (Estimators[i] == "sorest") {
339 treeCalculators.push_back(new SorEst());
340 }else if (Estimators[i] == "thetayc") {
341 treeCalculators.push_back(new ThetaYC());
342 }else if (Estimators[i] == "thetan") {
343 treeCalculators.push_back(new ThetaN());
344 }else if (Estimators[i] == "kstest") {
345 treeCalculators.push_back(new KSTest());
346 }else if (Estimators[i] == "sharednseqs") {
347 treeCalculators.push_back(new SharedNSeqs());
348 }else if (Estimators[i] == "ochiai") {
349 treeCalculators.push_back(new Ochiai());
350 }else if (Estimators[i] == "anderberg") {
351 treeCalculators.push_back(new Anderberg());
352 }else if (Estimators[i] == "kulczynski") {
353 treeCalculators.push_back(new Kulczynski());
354 }else if (Estimators[i] == "kulczynskicody") {
355 treeCalculators.push_back(new KulczynskiCody());
356 }else if (Estimators[i] == "lennon") {
357 treeCalculators.push_back(new Lennon());
358 }else if (Estimators[i] == "morisitahorn") {
359 treeCalculators.push_back(new MorHorn());
360 }else if (Estimators[i] == "braycurtis") {
361 treeCalculators.push_back(new BrayCurtis());
362 }else if (Estimators[i] == "whittaker") {
363 treeCalculators.push_back(new Whittaker());
364 }else if (Estimators[i] == "odum") {
365 treeCalculators.push_back(new Odum());
366 }else if (Estimators[i] == "canberra") {
367 treeCalculators.push_back(new Canberra());
368 }else if (Estimators[i] == "structeuclidean") {
369 treeCalculators.push_back(new StructEuclidean());
370 }else if (Estimators[i] == "structchord") {
371 treeCalculators.push_back(new StructChord());
372 }else if (Estimators[i] == "hellinger") {
373 treeCalculators.push_back(new Hellinger());
374 }else if (Estimators[i] == "manhattan") {
375 treeCalculators.push_back(new Manhattan());
376 }else if (Estimators[i] == "structpearson") {
377 treeCalculators.push_back(new StructPearson());
378 }else if (Estimators[i] == "soergel") {
379 treeCalculators.push_back(new Soergel());
380 }else if (Estimators[i] == "spearman") {
381 treeCalculators.push_back(new Spearman());
382 }else if (Estimators[i] == "structkulczynski") {
383 treeCalculators.push_back(new StructKulczynski());
384 }else if (Estimators[i] == "speciesprofile") {
385 treeCalculators.push_back(new SpeciesProfile());
386 }else if (Estimators[i] == "hamming") {
387 treeCalculators.push_back(new Hamming());
388 }else if (Estimators[i] == "structchi2") {
389 treeCalculators.push_back(new StructChi2());
390 }else if (Estimators[i] == "gower") {
391 treeCalculators.push_back(new Gower());
392 }else if (Estimators[i] == "memchi2") {
393 treeCalculators.push_back(new MemChi2());
394 }else if (Estimators[i] == "memchord") {
395 treeCalculators.push_back(new MemChord());
396 }else if (Estimators[i] == "memeuclidean") {
397 treeCalculators.push_back(new MemEuclidean());
398 }else if (Estimators[i] == "mempearson") {
399 treeCalculators.push_back(new MemPearson());
404 //if the users entered no valid calculators don't execute command
405 if (treeCalculators.size() == 0) { m->mothurOut("You have given no valid calculators."); m->mothurOutEndLine(); return 0; }
407 input = new InputData(sharedfile, "sharedfile");
408 lookup = input->getSharedRAbundVectors();
409 lastLabel = lookup[0]->getLabel();
411 if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups. I cannot run the command."); m->mothurOutEndLine(); return 0; }
413 //used in tree constructor
416 //create treemap class from groupmap for tree class to use
417 ct = new CountTable();
419 map<string, string> groupMap;
421 for (int i = 0; i < m->getAllGroups().size(); i++) {
422 nameMap.insert(m->getAllGroups()[i]);
423 gps.insert(m->getAllGroups()[i]);
424 groupMap[m->getAllGroups()[i]] = m->getAllGroups()[i];
426 ct->createTable(nameMap, groupMap, gps);
428 //clear globaldatas old tree names if any
429 m->Treenames.clear();
431 //fills globaldatas tree names
432 //m->Treenames = m->getGroups();
433 for (int k = 0; k < lookup.size(); k++) {
434 m->Treenames.push_back(lookup[k]->getGroup());
437 if (m->control_pressed) { return 0; }
442 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
445 filename = inputfile;
447 ReadMatrix* readMatrix;
448 if (format == "column") { readMatrix = new ReadColumnMatrix(filename); }
449 else if (format == "phylip") { readMatrix = new ReadPhylipMatrix(filename); }
451 readMatrix->setCutoff(cutoff);
456 nameMap = new NameAssignment(namefile);
458 readMatrix->read(nameMap);
459 }else if (countfile != "") {
460 ct = new CountTable();
461 ct->readTable(countfile, true);
462 readMatrix->read(ct);
464 readMatrix->read(nameMap);
467 list = readMatrix->getListVector();
468 SparseDistanceMatrix* dMatrix = readMatrix->getDMatrix();
470 //clear globaldatas old tree names if any
471 m->Treenames.clear();
474 if (ct != NULL) { delete ct; }
475 ct = new CountTable();
477 map<string, string> groupMap;
479 for (int i = 0; i < list->getNumBins(); i++) {
480 string bin = list->get(i);
484 m->Treenames.push_back(bin);
486 ct->createTable(nameMap, groupMap, gps);
488 vector<string> namesGroups = ct->getNamesOfGroups();
489 m->setGroups(namesGroups);
491 //used in tree constructor
494 if (m->control_pressed) { return 0; }
496 vector< vector<double> > matrix = makeSimsDist(dMatrix);
500 if (m->control_pressed) { return 0; }
502 //create a new filename
503 map<string, string> variables;
504 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inputfile));
505 string outputFile = getOutputFileName("tree",variables);
506 outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile);
508 Tree* newTree = createTree(matrix);
510 if (newTree != NULL) { writeTree(outputFile, newTree); delete newTree; }
512 if (m->control_pressed) { return 0; }
514 m->mothurOut("Tree complete. "); m->mothurOutEndLine();
518 //reset groups parameter
521 //set tree file as new current treefile
523 itTypes = outputTypes.find("tree");
524 if (itTypes != outputTypes.end()) {
525 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTreeFile(current); }
528 m->mothurOutEndLine();
529 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
530 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
531 m->mothurOutEndLine();
535 catch(exception& e) {
536 m->errorOut(e, "TreeGroupCommand", "execute");
540 //**********************************************************************************************************************
542 Tree* TreeGroupCommand::createTree(vector< vector<double> >& simMatrix){
545 t = new Tree(ct, simMatrix);
547 if (m->control_pressed) { delete t; t = NULL; return t; }
554 catch(exception& e) {
555 m->errorOut(e, "TreeGroupCommand", "createTree");
559 /***********************************************************/
560 int TreeGroupCommand::writeTree(string out, Tree* T) {
564 t->createNewickFile(out);
566 if (m->control_pressed) { m->mothurRemove(out); outputNames.pop_back(); return 1; }
571 catch(exception& e) {
572 m->errorOut(e, "TreeGroupCommand", "printSims");
577 /***********************************************************/
578 void TreeGroupCommand::printSims(ostream& out, vector< vector<double> >& simMatrix) {
581 for (int m = 0; m < simMatrix.size(); m++) {
582 //out << lookup[m]->getGroup() << '\t';
583 for (int n = 0; n < simMatrix.size(); n++) {
584 out << simMatrix[m][n] << '\t';
590 catch(exception& e) {
591 m->errorOut(e, "TreeGroupCommand", "printSims");
595 /***********************************************************/
596 vector< vector<double> > TreeGroupCommand::makeSimsDist(SparseDistanceMatrix* matrix) {
598 numGroups = list->size();
600 //initialize simMatrix
601 vector< vector<double> > simMatrix;
602 simMatrix.resize(numGroups);
603 for (int k = 0; k < simMatrix.size(); k++) {
604 for (int j = 0; j < simMatrix.size(); j++) {
605 simMatrix[k].push_back(0.0);
609 //go through sparse matrix and fill sims
610 //go through each cell in the sparsematrix
611 for (int i = 0; i < matrix->seqVec.size(); i++) {
612 for (int j = 0; j < matrix->seqVec[i].size(); j++) {
614 //already checked everyone else in row
615 if (i < matrix->seqVec[i][j].index) {
616 simMatrix[i][matrix->seqVec[i][j].index] = -(matrix->seqVec[i][j].dist -1.0);
617 simMatrix[matrix->seqVec[i][j].index][i] = -(matrix->seqVec[i][j].dist -1.0);
619 if (m->control_pressed) { return simMatrix; }
626 catch(exception& e) {
627 m->errorOut(e, "TreeGroupCommand", "makeSimsDist");
632 /***********************************************************/
633 int TreeGroupCommand::makeSimsShared() {
637 if (subsampleSize == -1) { //user has not set size, set size = smallest samples size
638 subsampleSize = lookup[0]->getNumSeqs();
639 for (int i = 1; i < lookup.size(); i++) {
640 int thisSize = lookup[i]->getNumSeqs();
642 if (thisSize < subsampleSize) { subsampleSize = thisSize; }
647 m->Treenames.clear();
648 vector<SharedRAbundVector*> temp;
649 for (int i = 0; i < lookup.size(); i++) {
650 if (lookup[i]->getNumSeqs() < subsampleSize) {
651 m->mothurOut(lookup[i]->getGroup() + " contains " + toString(lookup[i]->getNumSeqs()) + ". Eliminating."); m->mothurOutEndLine();
654 Groups.push_back(lookup[i]->getGroup());
655 temp.push_back(lookup[i]);
656 m->Treenames.push_back(lookup[i]->getGroup());
660 m->setGroups(Groups);
663 if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups. I cannot run the command."); m->mothurOutEndLine(); m->control_pressed = true; return 0; }
666 numGroups = lookup.size();
667 lines.resize(processors);
668 for (int i = 0; i < processors; i++) {
669 lines[i].start = int (sqrt(float(i)/float(processors)) * numGroups);
670 lines[i].end = int (sqrt(float(i+1)/float(processors)) * numGroups);
673 set<string> processedLabels;
674 set<string> userLabels = labels;
676 //as long as you are not at the end of the file or done wih the lines you want
677 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
678 if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } for(int i = 0 ; i < treeCalculators.size(); i++) { delete treeCalculators[i]; } return 1; }
680 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
681 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
684 processedLabels.insert(lookup[0]->getLabel());
685 userLabels.erase(lookup[0]->getLabel());
688 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
689 string saveLabel = lookup[0]->getLabel();
691 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
692 lookup = input->getSharedRAbundVectors(lastLabel);
694 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
697 processedLabels.insert(lookup[0]->getLabel());
698 userLabels.erase(lookup[0]->getLabel());
700 //restore real lastlabel to save below
701 lookup[0]->setLabel(saveLabel);
704 lastLabel = lookup[0]->getLabel();
706 //get next line to process
707 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
708 lookup = input->getSharedRAbundVectors();
711 if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } for(int i = 0 ; i < treeCalculators.size(); i++) { delete treeCalculators[i]; } return 1; }
713 //output error messages about any remaining user labels
714 set<string>::iterator it;
715 bool needToRun = false;
716 for (it = userLabels.begin(); it != userLabels.end(); it++) {
717 m->mothurOut("Your file does not include the label " + *it);
718 if (processedLabels.count(lastLabel) != 1) {
719 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
722 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
726 //run last label if you need to
727 if (needToRun == true) {
728 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
729 lookup = input->getSharedRAbundVectors(lastLabel);
731 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
733 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
736 for(int i = 0 ; i < treeCalculators.size(); i++) { delete treeCalculators[i]; }
740 catch(exception& e) {
741 m->errorOut(e, "TreeGroupCommand", "makeSimsShared");
746 /***********************************************************/
747 int TreeGroupCommand::process(vector<SharedRAbundVector*> thisLookup) {
749 vector< vector< vector<seqDist> > > calcDistsTotals; //each iter, one for each calc, then each groupCombos dists. this will be used to make .dist files
750 vector< vector<seqDist> > calcDists; calcDists.resize(treeCalculators.size());
752 for (int thisIter = 0; thisIter < iters; thisIter++) {
754 vector<SharedRAbundVector*> thisItersLookup = thisLookup;
758 vector<string> tempLabels; //dont need since we arent printing the sampled sharedRabunds
760 //make copy of lookup so we don't get access violations
761 vector<SharedRAbundVector*> newLookup;
762 for (int k = 0; k < thisItersLookup.size(); k++) {
763 SharedRAbundVector* temp = new SharedRAbundVector();
764 temp->setLabel(thisItersLookup[k]->getLabel());
765 temp->setGroup(thisItersLookup[k]->getGroup());
766 newLookup.push_back(temp);
770 for (int k = 0; k < thisItersLookup[0]->getNumBins(); k++) {
771 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
772 for (int j = 0; j < thisItersLookup.size(); j++) { newLookup[j]->push_back(thisItersLookup[j]->getAbundance(k), thisItersLookup[j]->getGroup()); }
775 tempLabels = sample.getSample(newLookup, subsampleSize);
776 thisItersLookup = newLookup;
780 driver(thisItersLookup, 0, numGroups, calcDists);
783 vector<int> processIDS;
785 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
786 //loop through and create all the processes you want
787 while (process != processors) {
791 processIDS.push_back(pid);
795 driver(thisItersLookup, lines[process].start, lines[process].end, calcDists);
797 string tempdistFileName = m->getRootName(m->getSimpleName(sharedfile)) + toString(getpid()) + ".dist";
799 m->openOutputFile(tempdistFileName, outtemp);
801 for (int i = 0; i < calcDists.size(); i++) {
802 outtemp << calcDists[i].size() << endl;
804 for (int j = 0; j < calcDists[i].size(); j++) {
805 outtemp << calcDists[i][j].seq1 << '\t' << calcDists[i][j].seq2 << '\t' << calcDists[i][j].dist << endl;
812 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
813 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
818 //parent do your part
819 driver(thisItersLookup, lines[0].start, lines[0].end, calcDists);
821 //force parent to wait until all the processes are done
822 for (int i = 0; i < processIDS.size(); i++) {
823 int temp = processIDS[i];
827 for (int i = 0; i < processIDS.size(); i++) {
828 string tempdistFileName = m->getRootName(m->getSimpleName(sharedfile)) + toString(processIDS[i]) + ".dist";
830 m->openInputFile(tempdistFileName, intemp);
832 for (int k = 0; k < calcDists.size(); k++) {
834 intemp >> size; m->gobble(intemp);
836 for (int j = 0; j < size; j++) {
841 intemp >> seq1 >> seq2 >> dist; m->gobble(intemp);
843 seqDist tempDist(seq1, seq2, dist);
844 calcDists[k].push_back(tempDist);
848 m->mothurRemove(tempdistFileName);
851 //////////////////////////////////////////////////////////////////////////////////////////////////////
852 //Windows version shared memory, so be careful when passing variables through the treeSharedData struct.
853 //Above fork() will clone, so memory is separate, but that's not the case with windows,
854 //Taking advantage of shared memory to pass results vectors.
855 //////////////////////////////////////////////////////////////////////////////////////////////////////
857 vector<treeSharedData*> pDataArray;
858 DWORD dwThreadIdArray[processors-1];
859 HANDLE hThreadArray[processors-1];
861 //Create processor worker threads.
862 for( int i=1; i<processors; i++ ){
864 //make copy of lookup so we don't get access violations
865 vector<SharedRAbundVector*> newLookup;
866 for (int k = 0; k < thisItersLookup.size(); k++) {
867 SharedRAbundVector* temp = new SharedRAbundVector();
868 temp->setLabel(thisItersLookup[k]->getLabel());
869 temp->setGroup(thisItersLookup[k]->getGroup());
870 newLookup.push_back(temp);
874 for (int k = 0; k < thisItersLookup[0]->getNumBins(); k++) {
875 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
876 for (int j = 0; j < thisItersLookup.size(); j++) { newLookup[j]->push_back(thisItersLookup[j]->getAbundance(k), thisItersLookup[j]->getGroup()); }
879 // Allocate memory for thread data.
880 treeSharedData* tempSum = new treeSharedData(m, lines[i].start, lines[i].end, Estimators, newLookup);
881 pDataArray.push_back(tempSum);
882 processIDS.push_back(i);
884 hThreadArray[i-1] = CreateThread(NULL, 0, MyTreeSharedThreadFunction, pDataArray[i-1], 0, &dwThreadIdArray[i-1]);
887 //parent do your part
888 driver(thisItersLookup, lines[0].start, lines[0].end, calcDists);
890 //Wait until all threads have terminated.
891 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
893 //Close all thread handles and free memory allocations.
894 for(int i=0; i < pDataArray.size(); i++){
895 if (pDataArray[i]->count != (pDataArray[i]->end-pDataArray[i]->start)) {
896 m->mothurOut("[ERROR]: process " + toString(i) + " only processed " + toString(pDataArray[i]->count) + " of " + toString(pDataArray[i]->end-pDataArray[i]->start) + " groups assigned to it, quitting. \n"); m->control_pressed = true;
898 for (int j = 0; j < pDataArray[i]->thisLookup.size(); j++) { delete pDataArray[i]->thisLookup[j]; }
900 for (int k = 0; k < calcDists.size(); k++) {
901 int size = pDataArray[i]->calcDists[k].size();
902 for (int j = 0; j < size; j++) { calcDists[k].push_back(pDataArray[i]->calcDists[k][j]); }
905 CloseHandle(hThreadArray[i]);
906 delete pDataArray[i];
912 calcDistsTotals.push_back(calcDists);
917 for (int i = 0; i < thisItersLookup.size(); i++) { delete thisItersLookup[i]; }
918 thisItersLookup.clear();
919 for (int i = 0; i < calcDists.size(); i++) { calcDists[i].clear(); }
923 if (m->debug) { m->mothurOut("[DEBUG]: done with iters.\n"); }
926 //we need to find the average distance and standard deviation for each groups distance
927 vector< vector<seqDist> > calcAverages = m->getAverages(calcDistsTotals);
929 if (m->debug) { m->mothurOut("[DEBUG]: found averages.\n"); }
931 //create average tree for each calc
932 for (int i = 0; i < calcDists.size(); i++) {
933 vector< vector<double> > matrix; //square matrix to represent the distance
934 matrix.resize(thisLookup.size());
935 for (int k = 0; k < thisLookup.size(); k++) { matrix[k].resize(thisLookup.size(), 0.0); }
937 for (int j = 0; j < calcAverages[i].size(); j++) {
938 int row = calcAverages[i][j].seq1;
939 int column = calcAverages[i][j].seq2;
940 float dist = calcAverages[i][j].dist;
942 matrix[row][column] = dist;
943 matrix[column][row] = dist;
946 //create a new filename
947 map<string, string> variables;
948 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inputfile));
949 variables["[calc]"] = treeCalculators[i]->getName();
950 variables["[distance]"] = thisLookup[0]->getLabel();
951 variables["[tag]"] = "ave";
952 string outputFile = getOutputFileName("tree",variables);
953 outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile);
955 //creates tree from similarity matrix and write out file
956 Tree* newTree = createTree(matrix);
957 if (newTree != NULL) { writeTree(outputFile, newTree); }
960 if (m->debug) { m->mothurOut("[DEBUG]: done averages trees.\n"); }
962 //create all trees for each calc and find their consensus tree
963 for (int i = 0; i < calcDists.size(); i++) {
964 if (m->control_pressed) { break; }
966 //create a new filename
967 //create a new filename
968 map<string, string> variables;
969 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inputfile));
970 variables["[calc]"] = treeCalculators[i]->getName();
971 variables["[distance]"] = thisLookup[0]->getLabel();
972 variables["[tag]"] = "all";
973 string outputFile = getOutputFileName("tree",variables);
974 outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile);
977 m->openOutputFile(outputFile, outAll);
980 for (int myIter = 0; myIter < iters; myIter++) {
982 if(m->control_pressed) { break; }
985 vector< vector<double> > matrix; //square matrix to represent the distance
986 matrix.resize(thisLookup.size());
987 for (int k = 0; k < thisLookup.size(); k++) { matrix[k].resize(thisLookup.size(), 0.0); }
989 for (int j = 0; j < calcDistsTotals[myIter][i].size(); j++) {
990 int row = calcDistsTotals[myIter][i][j].seq1;
991 int column = calcDistsTotals[myIter][i][j].seq2;
992 double dist = calcDistsTotals[myIter][i][j].dist;
994 matrix[row][column] = dist;
995 matrix[column][row] = dist;
998 //creates tree from similarity matrix and write out file
999 Tree* newTree = createTree(matrix);
1000 if (newTree != NULL) {
1001 newTree->print(outAll);
1002 trees.push_back(newTree);
1006 if (m->control_pressed) { for (int k = 0; k < trees.size(); k++) { delete trees[k]; } }
1008 if (m->debug) { m->mothurOut("[DEBUG]: done all trees.\n"); }
1010 Consensus consensus;
1011 //clear old tree names if any
1012 m->Treenames.clear(); m->Treenames = m->getGroups(); //may have changed if subsample eliminated groups
1013 Tree* conTree = consensus.getTree(trees);
1015 if (m->debug) { m->mothurOut("[DEBUG]: done cons tree.\n"); }
1017 //create a new filename
1018 variables["[tag]"] = "cons";
1019 string conFile = getOutputFileName("tree",variables);
1020 outputNames.push_back(conFile); outputTypes["tree"].push_back(conFile);
1022 m->openOutputFile(conFile, outTree);
1024 if (conTree != NULL) { conTree->print(outTree, "boot"); delete conTree; }
1029 for (int i = 0; i < calcDists.size(); i++) {
1030 if (m->control_pressed) { break; }
1033 vector< vector<double> > matrix; //square matrix to represent the distance
1034 matrix.resize(thisLookup.size());
1035 for (int k = 0; k < thisLookup.size(); k++) { matrix[k].resize(thisLookup.size(), 0.0); }
1037 for (int j = 0; j < calcDists[i].size(); j++) {
1038 int row = calcDists[i][j].seq1;
1039 int column = calcDists[i][j].seq2;
1040 double dist = calcDists[i][j].dist;
1042 matrix[row][column] = dist;
1043 matrix[column][row] = dist;
1046 //create a new filename
1047 map<string, string> variables;
1048 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inputfile));
1049 variables["[calc]"] = treeCalculators[i]->getName();
1050 variables["[distance]"] = thisLookup[0]->getLabel();
1051 variables["[tag]"] = "";
1052 string outputFile = getOutputFileName("tree",variables);
1053 outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile);
1055 //creates tree from similarity matrix and write out file
1056 Tree* newTree = createTree(matrix);
1057 if (newTree != NULL) { writeTree(outputFile, newTree); delete newTree; }
1063 catch(exception& e) {
1064 m->errorOut(e, "TreeGroupCommand", "process");
1068 /**************************************************************************************************/
1069 int TreeGroupCommand::driver(vector<SharedRAbundVector*> thisLookup, int start, int end, vector< vector<seqDist> >& calcDists) {
1071 vector<SharedRAbundVector*> subset;
1072 for (int k = start; k < end; k++) { // pass cdd each set of groups to compare
1074 for (int l = 0; l < k; l++) {
1076 if (k != l) { //we dont need to similiarity of a groups to itself
1077 subset.clear(); //clear out old pair of sharedrabunds
1078 //add new pair of sharedrabunds
1079 subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]);
1081 for(int i=0;i<treeCalculators.size();i++) {
1083 //if this calc needs all groups to calculate the pair load all groups
1084 if (treeCalculators[i]->getNeedsAll()) {
1085 //load subset with rest of lookup for those calcs that need everyone to calc for a pair
1086 for (int w = 0; w < thisLookup.size(); w++) {
1087 if ((w != k) && (w != l)) { subset.push_back(thisLookup[w]); }
1091 vector<double> tempdata = treeCalculators[i]->getValues(subset); //saves the calculator outputs
1093 if (m->control_pressed) { return 1; }
1095 seqDist temp(l, k, -(tempdata[0]-1.0));
1096 calcDists[i].push_back(temp);
1104 catch(exception& e) {
1105 m->errorOut(e, "TreeGroupCommand", "driver");
1109 /***********************************************************/