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 "sharedsobscollectsummary.h"
12 #include "sharedchao1.h"
13 #include "sharedace.h"
14 #include "sharednseqs.h"
15 #include "sharedjabund.h"
16 #include "sharedsorabund.h"
17 #include "sharedjclass.h"
18 #include "sharedsorclass.h"
19 #include "sharedjest.h"
20 #include "sharedsorest.h"
21 #include "sharedthetayc.h"
22 #include "sharedthetan.h"
23 #include "sharedkstest.h"
24 #include "whittaker.h"
25 #include "sharedochiai.h"
26 #include "sharedanderbergs.h"
27 #include "sharedkulczynski.h"
28 #include "sharedkulczynskicody.h"
29 #include "sharedlennon.h"
30 #include "sharedmorisitahorn.h"
31 #include "sharedbraycurtis.h"
32 #include "sharedjackknife.h"
33 #include "whittaker.h"
36 #include "structeuclidean.h"
37 #include "structchord.h"
38 #include "hellinger.h"
39 #include "manhattan.h"
40 #include "structpearson.h"
43 #include "structkulczynski.h"
44 #include "structchi2.h"
45 #include "speciesprofile.h"
50 #include "memeuclidean.h"
51 #include "mempearson.h"
53 //**********************************************************************************************************************
54 vector<string> TreeGroupCommand::setParameters(){
56 CommandParameter pshared("shared", "InputTypes", "", "", "PhylipColumnShared", "PhylipColumnShared", "none",false,false); parameters.push_back(pshared);
57 CommandParameter pphylip("phylip", "InputTypes", "", "", "PhylipColumnShared", "PhylipColumnShared", "none",false,false); parameters.push_back(pphylip);
58 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "ColumnName",false,false); parameters.push_back(pname);
59 CommandParameter pcolumn("column", "InputTypes", "", "", "PhylipColumnShared", "PhylipColumnShared", "ColumnName",false,false); parameters.push_back(pcolumn);
60 CommandParameter pcutoff("cutoff", "Number", "", "10", "", "", "",false,false); parameters.push_back(pcutoff);
61 CommandParameter pprecision("precision", "Number", "", "100", "", "", "",false,false); parameters.push_back(pprecision);
62 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
63 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
64 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);
65 CommandParameter poutput("output", "Multiple", "lt-square", "lt", "", "", "",false,false); parameters.push_back(poutput);
66 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
67 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
69 vector<string> myArray;
70 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
74 m->errorOut(e, "TreeGroupCommand", "setParameters");
78 //**********************************************************************************************************************
79 string TreeGroupCommand::getHelpString(){
81 string helpString = "";
82 ValidCalculators validCalculator;
83 helpString += "The tree.shared command creates a .tre to represent the similiarity between groups or sequences.\n";
84 helpString += "The tree.shared command parameters are shared, groups, calc, phylip, column, name, cutoff, precision and label.\n";
85 helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like included used.\n";
86 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";
87 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";
88 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";
89 helpString += "The tree.shared command should be in the following format: tree.shared(groups=yourGroups, calc=yourCalcs, label=yourLabels).\n";
90 helpString += "Example tree.shared(groups=A-B-C, calc=jabund-sorabund).\n";
91 helpString += "The default value for groups is all the groups in your groupfile.\n";
92 helpString += "The default value for calc is jclass-thetayc.\n";
93 helpString += "The tree.shared command outputs a .tre file for each calculator you specify at each distance you choose.\n";
94 helpString += validCalculator.printCalc("treegroup");
95 helpString += "Or the tree.shared command can be in the following format: tree.shared(phylip=yourPhylipFile).\n";
96 helpString += "Example tree.shared(phylip=abrecovery.dist).\n";
97 helpString += "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n";
100 catch(exception& e) {
101 m->errorOut(e, "TreeGroupCommand", "getHelpString");
105 //**********************************************************************************************************************
106 TreeGroupCommand::TreeGroupCommand(){
108 abort = true; calledHelp = true;
110 //initialize outputTypes
111 vector<string> tempOutNames;
112 outputTypes["tree"] = tempOutNames;
114 catch(exception& e) {
115 m->errorOut(e, "TreeGroupCommand", "TreeGroupCommand");
119 //**********************************************************************************************************************
121 TreeGroupCommand::TreeGroupCommand(string option) {
123 abort = false; calledHelp = false;
126 //allow user to run help
127 if(option == "help") { help(); abort = true; calledHelp = true; }
128 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
131 vector<string> myArray = setParameters();
133 OptionParser parser(option);
134 map<string, string> parameters = parser. getParameters();
136 ValidParameters validParameter;
137 map<string, string>::iterator it;
139 //check to make sure all parameters are valid for command
140 for (it = parameters.begin(); it != parameters.end(); it++) {
141 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
144 //initialize outputTypes
145 vector<string> tempOutNames;
146 outputTypes["tree"] = tempOutNames;
148 //if the user changes the input directory command factory will send this info to us in the output parameter
149 string inputDir = validParameter.validFile(parameters, "inputdir", false);
150 if (inputDir == "not found"){ inputDir = ""; }
153 it = parameters.find("phylip");
154 //user has given a template file
155 if(it != parameters.end()){
156 path = m->hasPath(it->second);
157 //if the user has not given a path then, add inputdir. else leave path alone.
158 if (path == "") { parameters["phylip"] = inputDir + it->second; }
161 it = parameters.find("column");
162 //user has given a template file
163 if(it != parameters.end()){
164 path = m->hasPath(it->second);
165 //if the user has not given a path then, add inputdir. else leave path alone.
166 if (path == "") { parameters["column"] = inputDir + it->second; }
169 it = parameters.find("name");
170 //user has given a template file
171 if(it != parameters.end()){
172 path = m->hasPath(it->second);
173 //if the user has not given a path then, add inputdir. else leave path alone.
174 if (path == "") { parameters["name"] = inputDir + it->second; }
178 //check for required parameters
179 phylipfile = validParameter.validFile(parameters, "phylip", true);
180 if (phylipfile == "not open") { phylipfile = ""; abort = true; }
181 else if (phylipfile == "not found") { phylipfile = ""; }
182 else { inputfile = phylipfile; format = "phylip"; m->setPhylipFile(phylipfile); }
184 columnfile = validParameter.validFile(parameters, "column", true);
185 if (columnfile == "not open") { columnfile = ""; abort = true; }
186 else if (columnfile == "not found") { columnfile = ""; }
187 else { inputfile = columnfile; format = "column"; m->setColumnFile(columnfile); }
189 sharedfile = validParameter.validFile(parameters, "shared", true);
190 if (sharedfile == "not open") { sharedfile = ""; abort = true; }
191 else if (sharedfile == "not found") { sharedfile = ""; }
192 else { inputfile = sharedfile; format = "sharedfile"; m->setSharedFile(sharedfile); }
194 namefile = validParameter.validFile(parameters, "name", true);
195 if (namefile == "not open") { abort = true; }
196 else if (namefile == "not found") { namefile = ""; }
197 else { m->setNameFile(namefile); }
199 if ((phylipfile == "") && (columnfile == "") && (sharedfile == "")) {
200 //is there are current file available for either of these?
201 //give priority to shared, then column, then phylip
202 sharedfile = m->getSharedFile();
203 if (sharedfile != "") { inputfile = sharedfile; format = "sharedfile"; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
205 columnfile = m->getColumnFile();
206 if (columnfile != "") { inputfile = columnfile; format = "column"; m->mothurOut("Using " + columnfile + " as input file for the column parameter."); m->mothurOutEndLine(); }
208 phylipfile = m->getPhylipFile();
209 if (phylipfile != "") { inputfile = phylipfile; format = "phylip"; m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
211 m->mothurOut("No valid current files. You must provide a shared, phylip or column file."); m->mothurOutEndLine();
217 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; }
219 if (columnfile != "") {
220 if (namefile == "") {
221 namefile = m->getNameFile();
222 if (namefile != "") { m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
224 m->mothurOut("You need to provide a namefile if you are going to use the column format."); m->mothurOutEndLine();
230 //check for optional parameter and set defaults
231 // ...at some point should added some additional type checking...
232 label = validParameter.validFile(parameters, "label", false);
233 if (label == "not found") { label = ""; }
235 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
236 else { allLines = 1; }
239 groups = validParameter.validFile(parameters, "groups", false);
240 if (groups == "not found") { groups = ""; }
242 m->splitAtDash(groups, Groups);
243 m->setGroups(Groups);
246 calc = validParameter.validFile(parameters, "calc", false);
247 if (calc == "not found") { calc = "jclass-thetayc"; }
249 if (calc == "default") { calc = "jclass-thetayc"; }
251 m->splitAtDash(calc, Estimators);
252 if (m->inUsersGroups("citation", Estimators)) {
253 ValidCalculators validCalc; validCalc.printCitations(Estimators);
254 //remove citation from list of calcs
255 for (int i = 0; i < Estimators.size(); i++) { if (Estimators[i] == "citation") { Estimators.erase(Estimators.begin()+i); break; } }
259 temp = validParameter.validFile(parameters, "precision", false); if (temp == "not found") { temp = "100"; }
260 m->mothurConvert(temp, precision);
262 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found") { temp = "10"; }
263 m->mothurConvert(temp, cutoff);
264 cutoff += (5 / (precision * 10.0));
266 //if the user changes the output directory command factory will send this info to us in the output parameter
267 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
269 outputDir += m->hasPath(inputfile); //if user entered a file with a path then preserve it
274 catch(exception& e) {
275 m->errorOut(e, "TreeGroupCommand", "TreeGroupCommand");
279 //**********************************************************************************************************************
281 TreeGroupCommand::~TreeGroupCommand(){
282 if (abort == false) {
283 if (format == "sharedfile") { delete input; }
284 else { delete readMatrix; delete matrix; delete list; }
290 //**********************************************************************************************************************
292 int TreeGroupCommand::execute(){
295 if (abort == true) { if (calledHelp) { return 0; } return 2; }
297 if (format == "sharedfile") {
299 ValidCalculators validCalculator;
301 for (int i=0; i<Estimators.size(); i++) {
302 if (validCalculator.isValidCalculator("treegroup", Estimators[i]) == true) {
303 if (Estimators[i] == "sharedsobs") {
304 treeCalculators.push_back(new SharedSobsCS());
305 }else if (Estimators[i] == "sharedchao") {
306 treeCalculators.push_back(new SharedChao1());
307 }else if (Estimators[i] == "sharedace") {
308 treeCalculators.push_back(new SharedAce());
309 }else if (Estimators[i] == "jabund") {
310 treeCalculators.push_back(new JAbund());
311 }else if (Estimators[i] == "sorabund") {
312 treeCalculators.push_back(new SorAbund());
313 }else if (Estimators[i] == "jclass") {
314 treeCalculators.push_back(new Jclass());
315 }else if (Estimators[i] == "sorclass") {
316 treeCalculators.push_back(new SorClass());
317 }else if (Estimators[i] == "jest") {
318 treeCalculators.push_back(new Jest());
319 }else if (Estimators[i] == "sorest") {
320 treeCalculators.push_back(new SorEst());
321 }else if (Estimators[i] == "thetayc") {
322 treeCalculators.push_back(new ThetaYC());
323 }else if (Estimators[i] == "thetan") {
324 treeCalculators.push_back(new ThetaN());
325 }else if (Estimators[i] == "kstest") {
326 treeCalculators.push_back(new KSTest());
327 }else if (Estimators[i] == "sharednseqs") {
328 treeCalculators.push_back(new SharedNSeqs());
329 }else if (Estimators[i] == "ochiai") {
330 treeCalculators.push_back(new Ochiai());
331 }else if (Estimators[i] == "anderberg") {
332 treeCalculators.push_back(new Anderberg());
333 }else if (Estimators[i] == "kulczynski") {
334 treeCalculators.push_back(new Kulczynski());
335 }else if (Estimators[i] == "kulczynskicody") {
336 treeCalculators.push_back(new KulczynskiCody());
337 }else if (Estimators[i] == "lennon") {
338 treeCalculators.push_back(new Lennon());
339 }else if (Estimators[i] == "morisitahorn") {
340 treeCalculators.push_back(new MorHorn());
341 }else if (Estimators[i] == "braycurtis") {
342 treeCalculators.push_back(new BrayCurtis());
343 }else if (Estimators[i] == "whittaker") {
344 treeCalculators.push_back(new Whittaker());
345 }else if (Estimators[i] == "odum") {
346 treeCalculators.push_back(new Odum());
347 }else if (Estimators[i] == "canberra") {
348 treeCalculators.push_back(new Canberra());
349 }else if (Estimators[i] == "structeuclidean") {
350 treeCalculators.push_back(new StructEuclidean());
351 }else if (Estimators[i] == "structchord") {
352 treeCalculators.push_back(new StructChord());
353 }else if (Estimators[i] == "hellinger") {
354 treeCalculators.push_back(new Hellinger());
355 }else if (Estimators[i] == "manhattan") {
356 treeCalculators.push_back(new Manhattan());
357 }else if (Estimators[i] == "structpearson") {
358 treeCalculators.push_back(new StructPearson());
359 }else if (Estimators[i] == "soergel") {
360 treeCalculators.push_back(new Soergel());
361 }else if (Estimators[i] == "spearman") {
362 treeCalculators.push_back(new Spearman());
363 }else if (Estimators[i] == "structkulczynski") {
364 treeCalculators.push_back(new StructKulczynski());
365 }else if (Estimators[i] == "speciesprofile") {
366 treeCalculators.push_back(new SpeciesProfile());
367 }else if (Estimators[i] == "hamming") {
368 treeCalculators.push_back(new Hamming());
369 }else if (Estimators[i] == "structchi2") {
370 treeCalculators.push_back(new StructChi2());
371 }else if (Estimators[i] == "gower") {
372 treeCalculators.push_back(new Gower());
373 }else if (Estimators[i] == "memchi2") {
374 treeCalculators.push_back(new MemChi2());
375 }else if (Estimators[i] == "memchord") {
376 treeCalculators.push_back(new MemChord());
377 }else if (Estimators[i] == "memeuclidean") {
378 treeCalculators.push_back(new MemEuclidean());
379 }else if (Estimators[i] == "mempearson") {
380 treeCalculators.push_back(new MemPearson());
385 //if the users entered no valid calculators don't execute command
386 if (treeCalculators.size() == 0) { m->mothurOut("You have given no valid calculators."); m->mothurOutEndLine(); return 0; }
388 input = new InputData(sharedfile, "sharedfile");
389 lookup = input->getSharedRAbundVectors();
390 lastLabel = lookup[0]->getLabel();
392 if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups. I cannot run the command."); m->mothurOutEndLine(); return 0; }
394 //used in tree constructor
397 //create treemap class from groupmap for tree class to use
398 tmap = new TreeMap();
399 tmap->makeSim(m->getAllGroups());
401 //clear globaldatas old tree names if any
402 m->Treenames.clear();
404 //fills globaldatas tree names
405 m->Treenames = m->getGroups();
407 if (m->control_pressed) { return 0; }
412 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
415 filename = inputfile;
417 if (format == "column") { readMatrix = new ReadColumnMatrix(filename); }
418 else if (format == "phylip") { readMatrix = new ReadPhylipMatrix(filename); }
420 readMatrix->setCutoff(cutoff);
423 nameMap = new NameAssignment(namefile);
430 readMatrix->read(nameMap);
431 list = readMatrix->getListVector();
432 matrix = readMatrix->getMatrix();
435 tmap = new TreeMap();
437 if (m->control_pressed) { return 0; }
441 vector<string> namesGroups = tmap->getNamesOfGroups();
442 m->setGroups(namesGroups);
444 //clear globaldatas old tree names if any
445 m->Treenames.clear();
447 //fills globaldatas tree names
448 m->Treenames = m->getGroups();
450 //used in tree constructor
453 if (m->control_pressed) { return 0; }
457 if (m->control_pressed) { return 0; }
459 //create a new filename
460 outputFile = outputDir + m->getRootName(m->getSimpleName(inputfile)) + "tre";
461 outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile);
465 if (m->control_pressed) { return 0; }
467 m->mothurOut("Tree complete. "); m->mothurOutEndLine();
471 //reset groups parameter
474 //set tree file as new current treefile
476 itTypes = outputTypes.find("tree");
477 if (itTypes != outputTypes.end()) {
478 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTreeFile(current); }
481 m->mothurOutEndLine();
482 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
483 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
484 m->mothurOutEndLine();
488 catch(exception& e) {
489 m->errorOut(e, "TreeGroupCommand", "execute");
493 //**********************************************************************************************************************
495 int TreeGroupCommand::createTree(){
500 //do merges and create tree structure by setting parents and children
501 //there are numGroups - 1 merges to do
502 for (int i = 0; i < (numGroups - 1); i++) {
503 float largest = -1000.0;
505 if (m->control_pressed) { delete t; return 1; }
508 //find largest value in sims matrix by searching lower triangle
509 for (int j = 1; j < simMatrix.size(); j++) {
510 for (int k = 0; k < j; k++) {
511 if (simMatrix[j][k] > largest) { largest = simMatrix[j][k]; row = j; column = k; }
515 //set non-leaf node info and update leaves to know their parents
517 t->tree[numGroups + i].setChildren(index[row], index[column]);
520 t->tree[index[row]].setParent(numGroups + i);
521 t->tree[index[column]].setParent(numGroups + i);
523 //blength = distance / 2;
524 float blength = ((1.0 - largest) / 2);
527 t->tree[index[row]].setBranchLength(blength - t->tree[index[row]].getLengthToLeaves());
528 t->tree[index[column]].setBranchLength(blength - t->tree[index[column]].getLengthToLeaves());
530 //set your length to leaves to your childs length plus branchlength
531 t->tree[numGroups + i].setLengthToLeaves(t->tree[index[row]].getLengthToLeaves() + t->tree[index[row]].getBranchLength());
535 index[row] = numGroups+i;
536 index[column] = numGroups+i;
538 //remove highest value that caused the merge.
539 simMatrix[row][column] = -1000.0;
540 simMatrix[column][row] = -1000.0;
542 //merge values in simsMatrix
543 for (int n = 0; n < simMatrix.size(); n++) {
544 //row becomes merge of 2 groups
545 simMatrix[row][n] = (simMatrix[row][n] + simMatrix[column][n]) / 2;
546 simMatrix[n][row] = simMatrix[row][n];
548 simMatrix[column][n] = -1000.0;
549 simMatrix[n][column] = -1000.0;
553 //adjust tree to make sure root to tip length is .5
554 int root = t->findRoot();
555 t->tree[root].setBranchLength((0.5 - t->tree[root].getLengthToLeaves()));
560 if (m->control_pressed) { delete t; return 1; }
563 t->createNewickFile(outputFile);
568 if (m->control_pressed) { m->mothurRemove(outputFile); outputNames.pop_back(); return 1; }
573 catch(exception& e) {
574 m->errorOut(e, "TreeGroupCommand", "createTree");
578 /***********************************************************/
579 void TreeGroupCommand::printSims(ostream& out) {
582 //output column headers
584 //for (int i = 0; i < lookup.size(); i++) { out << lookup[i]->getGroup() << '\t'; }
588 for (int m = 0; m < simMatrix.size(); m++) {
589 //out << lookup[m]->getGroup() << '\t';
590 for (int n = 0; n < simMatrix.size(); n++) {
591 out << simMatrix[m][n] << '\t';
597 catch(exception& e) {
598 m->errorOut(e, "TreeGroupCommand", "printSims");
602 /***********************************************************/
603 int TreeGroupCommand::makeSimsDist() {
605 numGroups = list->size();
609 for (int g = 0; g < numGroups; g++) { index[g] = g; }
611 //initialize simMatrix
613 simMatrix.resize(numGroups);
614 for (int k = 0; k < simMatrix.size(); k++) {
615 for (int j = 0; j < simMatrix.size(); j++) {
616 simMatrix[k].push_back(0.0);
620 //go through sparse matrix and fill sims
621 //go through each cell in the sparsematrix
622 for(MatData currentCell = matrix->begin(); currentCell != matrix->end(); currentCell++){
623 //similairity = -(distance-1)
624 simMatrix[currentCell->row][currentCell->column] = -(currentCell->dist -1.0);
625 simMatrix[currentCell->column][currentCell->row] = -(currentCell->dist -1.0);
627 if (m->control_pressed) { return 1; }
633 catch(exception& e) {
634 m->errorOut(e, "TreeGroupCommand", "makeSimsDist");
639 /***********************************************************/
640 int TreeGroupCommand::makeSimsShared() {
642 set<string> processedLabels;
643 set<string> userLabels = labels;
645 //as long as you are not at the end of the file or done wih the lines you want
646 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
647 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; }
649 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
650 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
653 processedLabels.insert(lookup[0]->getLabel());
654 userLabels.erase(lookup[0]->getLabel());
657 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
658 string saveLabel = lookup[0]->getLabel();
660 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
661 lookup = input->getSharedRAbundVectors(lastLabel);
663 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
666 processedLabels.insert(lookup[0]->getLabel());
667 userLabels.erase(lookup[0]->getLabel());
669 //restore real lastlabel to save below
670 lookup[0]->setLabel(saveLabel);
673 lastLabel = lookup[0]->getLabel();
675 //get next line to process
676 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
677 lookup = input->getSharedRAbundVectors();
680 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; }
682 //output error messages about any remaining user labels
683 set<string>::iterator it;
684 bool needToRun = false;
685 for (it = userLabels.begin(); it != userLabels.end(); it++) {
686 m->mothurOut("Your file does not include the label " + *it);
687 if (processedLabels.count(lastLabel) != 1) {
688 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
691 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
695 //run last label if you need to
696 if (needToRun == true) {
697 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
698 lookup = input->getSharedRAbundVectors(lastLabel);
700 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
702 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
705 for(int i = 0 ; i < treeCalculators.size(); i++) { delete treeCalculators[i]; }
709 catch(exception& e) {
710 m->errorOut(e, "TreeGroupCommand", "makeSimsShared");
715 /***********************************************************/
716 int TreeGroupCommand::process(vector<SharedRAbundVector*> thisLookup) {
719 vector<SharedRAbundVector*> subset;
720 numGroups = thisLookup.size();
722 //for each calculator
723 for(int i = 0 ; i < treeCalculators.size(); i++) {
724 //initialize simMatrix
726 simMatrix.resize(numGroups);
727 for (int k = 0; k < simMatrix.size(); k++) {
728 for (int j = 0; j < simMatrix.size(); j++) {
729 simMatrix[k].push_back(0.0);
735 for (int g = 0; g < numGroups; g++) { index[g] = g; }
737 //create a new filename
738 outputFile = outputDir + m->getRootName(m->getSimpleName(inputfile)) + treeCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + ".tre";
739 outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile);
741 for (int k = 0; k < thisLookup.size(); k++) {
742 for (int l = k; l < thisLookup.size(); l++) {
743 if (k != l) { //we dont need to similiarity of a groups to itself
744 //get estimated similarity between 2 groups
746 subset.clear(); //clear out old pair of sharedrabunds
747 //add new pair of sharedrabunds
748 subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]);
750 //if this calc needs all groups to calculate the pair load all groups
751 if (treeCalculators[i]->getNeedsAll()) {
752 //load subset with rest of lookup for those calcs that need everyone to calc for a pair
753 for (int w = 0; w < thisLookup.size(); w++) {
754 if ((w != k) && (w != l)) { subset.push_back(thisLookup[w]); }
758 data = treeCalculators[i]->getValues(subset); //saves the calculator outputs
759 //cout << thisLookup[k]->getGroup() << '\t' << thisLookup[l]->getGroup() << '\t' << (1.0 - data[0]) << endl;
760 if (m->control_pressed) { return 1; }
762 //save values in similarity matrix
763 simMatrix[k][l] = -(data[0]-1.0);
764 simMatrix[l][k] = -(data[0]-1.0);
769 //createdistance file from simMatrix
770 /*string o = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + treeCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + ".dist";
772 m->openOutputFile(o, outDist);
773 outDist << simMatrix.size() << endl;
774 for (int k = 0; k < simMatrix.size(); k++) {
775 outDist << thisLookup[k]->getGroup() << '\t';
776 for (int l = 0; l < k; l++) {
777 outDist << (1.0-simMatrix[k][l]) << '\t';
784 if (m->control_pressed) { return 1; }
785 //creates tree from similarity matrix and write out file
788 if (m->control_pressed) { return 1; }
794 catch(exception& e) {
795 m->errorOut(e, "TreeGroupCommand", "process");
799 /***********************************************************/