X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=treegroupscommand.cpp;h=cb797d8d229fd0a2dfaff82d431b084bd0a227a5;hb=510b1cfc25cd79391d6973ca20c5ec25fb1bb3b2;hp=a6e1918ffdefcf34d6052730f5161d011d644567;hpb=c537597ec5ebd47b4898da87ae03b0c2aeced354;p=mothur.git diff --git a/treegroupscommand.cpp b/treegroupscommand.cpp index a6e1918..cb797d8 100644 --- a/treegroupscommand.cpp +++ b/treegroupscommand.cpp @@ -17,177 +17,275 @@ #include "sharedthetayc.h" #include "sharedthetan.h" #include "sharedmorisitahorn.h" +#include "sharedbraycurtis.h" //********************************************************************************************************************** -TreeGroupCommand::TreeGroupCommand(){ +TreeGroupCommand::TreeGroupCommand(string option){ try { globaldata = GlobalData::getInstance(); - format = globaldata->getFormat(); - validCalculator = new ValidCalculators(); - util = new SharedUtil(); + abort = false; + allLines = 1; + lines.clear(); + labels.clear(); + Groups.clear(); + Estimators.clear(); - int i; - for (i=0; iEstimators.size(); i++) { - if (validCalculator->isValidCalculator("treegroup", globaldata->Estimators[i]) == true) { - if (globaldata->Estimators[i] == "jabund") { - treeCalculators.push_back(new JAbund()); - }else if (globaldata->Estimators[i] == "sorensonabund") { - treeCalculators.push_back(new SorAbund()); - }else if (globaldata->Estimators[i] == "jclass") { - treeCalculators.push_back(new Jclass()); - }else if (globaldata->Estimators[i] == "sorclass") { - treeCalculators.push_back(new SorClass()); - }else if (globaldata->Estimators[i] == "jest") { - treeCalculators.push_back(new Jest()); - }else if (globaldata->Estimators[i] == "sorest") { - treeCalculators.push_back(new SorEst()); - }else if (globaldata->Estimators[i] == "thetayc") { - treeCalculators.push_back(new ThetaYC()); - }else if (globaldata->Estimators[i] == "thetan") { - treeCalculators.push_back(new ThetaN()); - }else if (globaldata->Estimators[i] == "morisitahorn") { - treeCalculators.push_back(new MorHorn()); - } + //allow user to run help + if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; } + + else { + //valid paramters for this command + string Array[] = {"line","label","calc","groups", "phylip", "column", "name", "precision","cutoff"}; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + + OptionParser parser(option); + map parameters = parser. getParameters(); + + ValidParameters validParameter; + + //check to make sure all parameters are valid for command + for (map::iterator it = parameters.begin(); it != parameters.end(); it++) { + if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; } + } + + //required parameters + phylipfile = validParameter.validFile(parameters, "phylip", true); + if (phylipfile == "not open") { abort = true; } + else if (phylipfile == "not found") { phylipfile = ""; } + else { format = "phylip"; } + + columnfile = validParameter.validFile(parameters, "column", true); + if (columnfile == "not open") { abort = true; } + else if (columnfile == "not found") { columnfile = ""; } + else { format = "column"; } + + namefile = validParameter.validFile(parameters, "name", true); + if (namefile == "not open") { abort = true; } + else if (namefile == "not found") { namefile = ""; } + else { globaldata->setNameFile(namefile); } + + format = globaldata->getFormat(); + + //error checking on files + if ((globaldata->getSharedFile() == "") && ((phylipfile == "") && (columnfile == ""))) { mothurOut("You must run the read.otu command or provide a distance file before running the tree.shared command."); mothurOutEndLine(); abort = true; } + else if ((phylipfile != "") && (columnfile != "")) { mothurOut("When running the tree.shared command with a distance file you may not use both the column and the phylip parameters."); mothurOutEndLine(); abort = true; } + + if (columnfile != "") { + if (namefile == "") { mothurOut("You need to provide a namefile if you are going to use the column format."); mothurOutEndLine(); abort = true; } + } + + //check for optional parameter and set defaults + // ...at some point should added some additional type checking... + line = validParameter.validFile(parameters, "line", false); + if (line == "not found") { line = ""; } + else { + if(line != "all") { splitAtDash(line, lines); allLines = 0; } + else { allLines = 1; } } + + label = validParameter.validFile(parameters, "label", false); + if (label == "not found") { label = ""; } + else { + if(label != "all") { splitAtDash(label, labels); allLines = 0; } + else { allLines = 1; } + } + + //make sure user did not use both the line and label parameters + if ((line != "") && (label != "")) { mothurOut("You cannot use both the line and label parameters at the same time. "); mothurOutEndLine(); abort = true; } + //if the user has not specified any line or labels use the ones from read.otu + else if((line == "") && (label == "")) { + allLines = globaldata->allLines; + labels = globaldata->labels; + lines = globaldata->lines; + } + + groups = validParameter.validFile(parameters, "groups", false); + if (groups == "not found") { groups = ""; } + else { + splitAtDash(groups, Groups); + globaldata->Groups = Groups; + } + + calc = validParameter.validFile(parameters, "calc", false); + if (calc == "not found") { calc = "jclass-thetayc"; } + else { + if (calc == "default") { calc = "jclass-thetayc"; } + } + splitAtDash(calc, Estimators); + + string temp; + temp = validParameter.validFile(parameters, "precision", false); if (temp == "not found") { temp = "100"; } + convert(temp, precision); + + temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found") { temp = "10"; } + convert(temp, cutoff); + cutoff += (5 / (precision * 10.0)); + + + if (abort == false) { + + validCalculator = new ValidCalculators(); + + if (format == "sharedfile") { + int i; + for (i=0; iisValidCalculator("treegroup", Estimators[i]) == true) { + if (Estimators[i] == "jabund") { + treeCalculators.push_back(new JAbund()); + }else if (Estimators[i] == "sorabund") { + treeCalculators.push_back(new SorAbund()); + }else if (Estimators[i] == "jclass") { + treeCalculators.push_back(new Jclass()); + }else if (Estimators[i] == "sorclass") { + treeCalculators.push_back(new SorClass()); + }else if (Estimators[i] == "jest") { + treeCalculators.push_back(new Jest()); + }else if (Estimators[i] == "sorest") { + treeCalculators.push_back(new SorEst()); + }else if (Estimators[i] == "thetayc") { + treeCalculators.push_back(new ThetaYC()); + }else if (Estimators[i] == "thetan") { + treeCalculators.push_back(new ThetaN()); + }else if (Estimators[i] == "morisitahorn") { + treeCalculators.push_back(new MorHorn()); + }else if (Estimators[i] == "braycurtis") { + treeCalculators.push_back(new BrayCurtis()); + } + } + } + } + } } - - //reset calc for next command - globaldata->setCalc(""); } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function TreeGroupCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + errorOut(e, "TreeGroupCommand", "TreeGroupCommand"); exit(1); } - catch(...) { - cout << "An unknown error has occurred in the TreeGroupCommand class function TreeGroupCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; +} + +//********************************************************************************************************************** + +void TreeGroupCommand::help(){ + try { + mothurOut("The tree.shared command creates a .tre to represent the similiarity between groups or sequences.\n"); + mothurOut("The tree.shared command can only be executed after a successful read.otu command or by providing a distance file.\n"); + mothurOut("The tree.shared command parameters are groups, calc, phylip, column, name, cutoff, precision, line and label. You may not use line and label at the same time.\n"); + mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included used.\n"); + mothurOut("The group names are separated by dashes. The line and label allow you to select what distance levels you would like trees created for, and are also separated by dashes.\n"); + mothurOut("The phylip or column parameter are required if you do not run the read.otu command first, and only one may be used. If you use a column file the name filename is required. \n"); + mothurOut("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"); + mothurOut("The tree.shared command should be in the following format: tree.shared(groups=yourGroups, calc=yourCalcs, line=yourLines, label=yourLabels).\n"); + mothurOut("Example tree.shared(groups=A-B-C, line=1-3-5, calc=jabund-sorabund).\n"); + mothurOut("The default value for groups is all the groups in your groupfile.\n"); + mothurOut("The default value for calc is jclass-thetayc.\n"); + mothurOut("The tree.shared command outputs a .tre file for each calculator you specify at each distance you choose.\n"); + validCalculator->printCalc("treegroup", cout); + mothurOut("Or the tree.shared command can be in the following format: tree.shared(phylip=yourPhylipFile).\n"); + mothurOut("Example tree.shared(phylip=abrecovery.dist).\n"); + mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n"); + } + catch(exception& e) { + errorOut(e, "TreeGroupCommand", "help"); exit(1); - } + } } + + //********************************************************************************************************************** TreeGroupCommand::~TreeGroupCommand(){ - delete input; - delete read; - delete util; + if (abort == false) { + + if (format == "sharedfile") { delete read; delete input; globaldata->ginput = NULL;} + else { delete readMatrix; delete matrix; delete list; } + delete tmap; + delete validCalculator; + } + } //********************************************************************************************************************** int TreeGroupCommand::execute(){ try { - int count = 1; - EstOutput data; - //if the users entered no valid calculators don't execute command - if (treeCalculators.size() == 0) { return 0; } - + if (abort == true) { return 0; } + if (format == "sharedfile") { - read = new ReadPhilFile(globaldata->inputFileName); + //if the users entered no valid calculators don't execute command + if (treeCalculators.size() == 0) { mothurOut("You have given no valid calculators."); mothurOutEndLine(); return 0; } + + //you have groups + read = new ReadOTUFile(globaldata->inputFileName); read->read(&*globaldata); input = globaldata->ginput; - order = input->getSharedOrderVector(); - }else { - //you are using a list and a groupfile - read = new ReadPhilFile(globaldata->inputFileName); - read->read(&*globaldata); - - input = globaldata->ginput; - SharedList = globaldata->gSharedList; - order = SharedList->getSharedOrderVector(); - } - - //set users groups - util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups, "treegroup"); - numGroups = globaldata->Groups.size(); - groupNames = ""; - for (int i = 0; i < numGroups; i++) { groupNames += globaldata->Groups[i]; } - - //clear globaldatas old tree names if any - globaldata->Treenames.clear(); - - //fills globaldatas tree names - globaldata->Treenames = globaldata->Groups; + lookup = input->getSharedRAbundVectors(); + lastLabel = lookup[0]->getLabel(); + + if (lookup.size() < 2) { mothurOut("You have not provided enough valid groups. I cannot run the command."); mothurOutEndLine(); return 0; } - //create treemap class from groupmap for tree class to use - tmap = new TreeMap(); - tmap->makeSim(globaldata->gGroupmap); - globaldata->gTreemap = tmap; + globaldata->runParse = false; - while(order != NULL){ + //create tree file + makeSimsShared(); + }else{ + //read in dist file + filename = globaldata->inputFileName; - if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){ + if (format == "column") { readMatrix = new ReadColumnMatrix(filename); } + else if (format == "phylip") { readMatrix = new ReadPhylipMatrix(filename); } - cout << order->getLabel() << '\t' << count << endl; - util->getSharedVectors(globaldata->Groups, lookup, order); //fills group vectors from order vector. - - //for each calculator - for(int i = 0 ; i < treeCalculators.size(); i++) { - - //initialize simMatrix - simMatrix.clear(); - simMatrix.resize(numGroups); - for (int m = 0; m < simMatrix.size(); m++) { - for (int j = 0; j < simMatrix.size(); j++) { - simMatrix[m].push_back(0.0); - } - } - - //initialize index - index.clear(); - for (int g = 0; g < numGroups; g++) { index[g] = g; } - - //create a new filename - outputFile = getRootName(globaldata->inputFileName) + treeCalculators[i]->getName() + "." + order->getLabel() + groupNames + ".tre"; - - for (int k = 0; k < lookup.size(); k++) { // pass cdd each set of groups to commpare - for (int l = k; l < lookup.size(); l++) { - if (k != l) { //we dont need to similiarity of a groups to itself - //get estimated similarity between 2 groups - data = treeCalculators[i]->getValues(lookup[k], lookup[l]); //saves the calculator outputs - //save values in similarity matrix - simMatrix[k][l] = data[0]; - simMatrix[l][k] = data[0]; - } - } - } - - //creates tree from similarity matrix and write out file - createTree(); - } + readMatrix->setCutoff(cutoff); + + if(namefile != ""){ + nameMap = new NameAssignment(namefile); + nameMap->readMap(); } - - //get next line to process - if (format == "sharedfile") { - order = input->getSharedOrderVector(); - }else { - //you are using a list and a groupfile - SharedList = input->getSharedListVector(); //get new list vector to process - if (SharedList != NULL) { - order = SharedList->getSharedOrderVector(); //gets new order vector with group info. - }else { - break; - } + else{ + nameMap = NULL; } - count++; - } + + readMatrix->read(nameMap); + list = readMatrix->getListVector(); + matrix = readMatrix->getMatrix(); + + //make treemap + tmap = new TreeMap(); + tmap->makeSim(list); + globaldata->gTreemap = tmap; + + globaldata->Groups = tmap->namesOfGroups; + //clear globaldatas old tree names if any + globaldata->Treenames.clear(); + + //fills globaldatas tree names + globaldata->Treenames = globaldata->Groups; + + globaldata->runParse = false; + + makeSimsDist(); + + //create a new filename + outputFile = getRootName(globaldata->inputFileName) + "tre"; + + createTree(); + mothurOut("Tree complete. "); mothurOutEndLine(); + } + //reset groups parameter - globaldata->Groups.clear(); globaldata->setGroups(""); + globaldata->Groups.clear(); return 0; } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + errorOut(e, "TreeGroupCommand", "execute"); exit(1); } - catch(...) { - cout << "An unknown error has occurred in the TreeGroupCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } } //********************************************************************************************************************** @@ -199,8 +297,8 @@ void TreeGroupCommand::createTree(){ //do merges and create tree structure by setting parents and children //there are numGroups - 1 merges to do for (int i = 0; i < (numGroups - 1); i++) { - - float largest = 0.0; + float largest = -1000.0; + int row, column; //find largest value in sims matrix by searching lower triangle for (int j = 1; j < simMatrix.size(); j++) { @@ -232,9 +330,9 @@ void TreeGroupCommand::createTree(){ index[row] = numGroups+i; index[column] = numGroups+i; - //zero out highest value that caused the merge. - simMatrix[row][column] = 0.0; - simMatrix[column][row] = 0.0; + //remove highest value that caused the merge. + simMatrix[row][column] = -1000.0; + simMatrix[column][row] = -1000.0; //merge values in simsMatrix for (int n = 0; n < simMatrix.size(); n++) { @@ -242,11 +340,15 @@ void TreeGroupCommand::createTree(){ simMatrix[row][n] = (simMatrix[row][n] + simMatrix[column][n]) / 2; simMatrix[n][row] = simMatrix[row][n]; //delete column - simMatrix[column][n] = 0.0; - simMatrix[n][column] = 0.0; + simMatrix[column][n] = -1000.0; + simMatrix[n][column] = -1000.0; } } - + + //adjust tree to make sure root to tip length is .5 + int root = t->findRoot(); + t->tree[root].setBranchLength((0.5 - t->tree[root].getLengthToLeaves())); + //assemble tree t->assembleTree(); @@ -258,35 +360,203 @@ void TreeGroupCommand::createTree(){ } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function createTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + errorOut(e, "TreeGroupCommand", "createTree"); exit(1); } - catch(...) { - cout << "An unknown error has occurred in the TreeGroupCommand class function createTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; +} +/***********************************************************/ +void TreeGroupCommand::printSims(ostream& out) { + try { + + //output column headers + //out << '\t'; + //for (int i = 0; i < lookup.size(); i++) { out << lookup[i]->getGroup() << '\t'; } + //out << endl; + + + for (int m = 0; m < simMatrix.size(); m++) { + //out << lookup[m]->getGroup() << '\t'; + for (int n = 0; n < simMatrix.size(); n++) { + out << simMatrix[m][n] << '\t'; + } + out << endl; + } + + } + catch(exception& e) { + errorOut(e, "TreeGroupCommand", "printSims"); exit(1); } } /***********************************************************/ -void TreeGroupCommand::printSims() { +void TreeGroupCommand::makeSimsDist() { try { - cout << "simsMatrix" << endl; + numGroups = list->size(); + + //initialize index + index.clear(); + for (int g = 0; g < numGroups; g++) { index[g] = g; } + + //initialize simMatrix + simMatrix.clear(); + simMatrix.resize(numGroups); for (int m = 0; m < simMatrix.size(); m++) { - for (int n = 0; n < simMatrix.size(); n++) { - cout << simMatrix[m][n] << '\t'; + for (int j = 0; j < simMatrix.size(); j++) { + simMatrix[m].push_back(0.0); + } + } + + //go through sparse matrix and fill sims + //go through each cell in the sparsematrix + for(MatData currentCell = matrix->begin(); currentCell != matrix->end(); currentCell++){ + //similairity = -(distance-1) + simMatrix[currentCell->row][currentCell->column] = -(currentCell->dist -1.0); + simMatrix[currentCell->column][currentCell->row] = -(currentCell->dist -1.0); + } + + + } + catch(exception& e) { + errorOut(e, "TreeGroupCommand", "makeSimsDist"); + exit(1); + } +} + +/***********************************************************/ +void TreeGroupCommand::makeSimsShared() { + try { + int count = 1; + + //clear globaldatas old tree names if any + globaldata->Treenames.clear(); + + //fills globaldatas tree names + globaldata->Treenames = globaldata->Groups; + + //create treemap class from groupmap for tree class to use + tmap = new TreeMap(); + tmap->makeSim(globaldata->gGroupmap); + globaldata->gTreemap = tmap; + + set processedLabels; + set userLabels = labels; + set userLines = lines; + + //as long as you are not at the end of the file or done wih the lines you want + while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) { + + if(allLines == 1 || lines.count(count) == 1 || labels.count(lookup[0]->getLabel()) == 1){ + mothurOut(lookup[0]->getLabel() + "\t" + toString(count)); mothurOutEndLine(); + process(lookup); + + processedLabels.insert(lookup[0]->getLabel()); + userLabels.erase(lookup[0]->getLabel()); + userLines.erase(count); + } + + if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) { + for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } + lookup = input->getSharedRAbundVectors(lastLabel); + + mothurOut(lookup[0]->getLabel() + "\t" + toString(count)); mothurOutEndLine(); + process(lookup); + + processedLabels.insert(lookup[0]->getLabel()); + userLabels.erase(lookup[0]->getLabel()); + } + + lastLabel = lookup[0]->getLabel(); + + //get next line to process + for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } + lookup = input->getSharedRAbundVectors(); + count++; + } + + //output error messages about any remaining user labels + set::iterator it; + bool needToRun = false; + for (it = userLabels.begin(); it != userLabels.end(); it++) { + mothurOut("Your file does not include the label " + *it); + if (processedLabels.count(lastLabel) != 1) { + mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine(); + needToRun = true; + }else { + mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine(); } - cout << endl; } + + //run last line if you need to + if (needToRun == true) { + for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } + lookup = input->getSharedRAbundVectors(lastLabel); + mothurOut(lookup[0]->getLabel() + "\t" + toString(count)); mothurOutEndLine(); + process(lookup); + for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } + } + + for(int i = 0 ; i < treeCalculators.size(); i++) { delete treeCalculators[i]; } } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + errorOut(e, "TreeGroupCommand", "makeSimsShared"); exit(1); } - catch(...) { - cout << "An unknown error has occurred in the TreeGroupCommand class function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; +} + +/***********************************************************/ +void TreeGroupCommand::process(vector thisLookup) { + try{ + EstOutput data; + vector subset; + numGroups = thisLookup.size(); + + //for each calculator + for(int i = 0 ; i < treeCalculators.size(); i++) { + //initialize simMatrix + simMatrix.clear(); + simMatrix.resize(numGroups); + for (int m = 0; m < simMatrix.size(); m++) { + for (int j = 0; j < simMatrix.size(); j++) { + simMatrix[m].push_back(0.0); + } + } + + //initialize index + index.clear(); + for (int g = 0; g < numGroups; g++) { index[g] = g; } + + //create a new filename + outputFile = getRootName(globaldata->inputFileName) + treeCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + ".tre"; + + for (int k = 0; k < thisLookup.size(); k++) { + for (int l = k; l < thisLookup.size(); l++) { + if (k != l) { //we dont need to similiarity of a groups to itself + //get estimated similarity between 2 groups + + subset.clear(); //clear out old pair of sharedrabunds + //add new pair of sharedrabunds + subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]); + + data = treeCalculators[i]->getValues(subset); //saves the calculator outputs + //save values in similarity matrix + simMatrix[k][l] = data[0]; + simMatrix[l][k] = data[0]; + } + } + } + + //creates tree from similarity matrix and write out file + createTree(); + } + + } + catch(exception& e) { + errorOut(e, "TreeGroupCommand", "process"); exit(1); - } + } } /***********************************************************/ +