X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=deuniquetreecommand.cpp;h=d334f8f4ef107d12a57eaa4aff4d71b84808edb9;hb=96dbe925073caefaed6e6db85659c144a806aeb1;hp=dca81e5ebb6359752731b7a003173b2da0306b0e;hpb=19fcbbdba99658f5eca244803280f9ee7f9f6607;p=mothur.git diff --git a/deuniquetreecommand.cpp b/deuniquetreecommand.cpp index dca81e5..d334f8f 100644 --- a/deuniquetreecommand.cpp +++ b/deuniquetreecommand.cpp @@ -8,6 +8,7 @@ */ #include "deuniquetreecommand.h" +#include "treereader.h" //********************************************************************************************************************** vector DeuniqueTreeCommand::setParameters(){ @@ -42,6 +43,26 @@ string DeuniqueTreeCommand::getHelpString(){ } } //********************************************************************************************************************** +string DeuniqueTreeCommand::getOutputFileNameTag(string type, string inputName=""){ + try { + string outputFileName = ""; + map >::iterator it; + + //is this a type this command creates + it = outputTypes.find(type); + if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); } + else { + if (type == "tree") { outputFileName = "deunique.tre"; } + else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; } + } + return outputFileName; + } + catch(exception& e) { + m->errorOut(e, "DeuniqueTreeCommand", "getOutputFileNameTag"); + exit(1); + } +} +//********************************************************************************************************************** DeuniqueTreeCommand::DeuniqueTreeCommand(){ try { abort = true; calledHelp = true; @@ -103,13 +124,7 @@ DeuniqueTreeCommand::DeuniqueTreeCommand(string option) { } } - m->runParse = true; - m->Groups.clear(); - m->namesOfGroups.clear(); - m->Treenames.clear(); - m->names.clear(); - - //check for required parameters + //check for required parameters treefile = validParameter.validFile(parameters, "tree", true); if (treefile == "not open") { abort = true; } else if (treefile == "not found") { //if there is a current design file, use it @@ -144,72 +159,22 @@ int DeuniqueTreeCommand::execute() { m->setTreeFile(treefile); - //extracts names from tree to make faked out groupmap - Tree* tree = new Tree(treefile); delete tree; - tmap = new TreeMap(); - for (int i = 0; i < m->Treenames.size(); i++) { tmap->addSeq(m->Treenames[i], "Group1"); } - - if (m->control_pressed) { delete tmap; return 0; } - - readNamesFile(); - - if (m->control_pressed) { delete tmap; return 0; } - - ReadTree* read = new ReadNewickTree(treefile); - int readOk = read->read(tmap); - if (readOk != 0) { m->mothurOut("Read Terminated."); m->mothurOutEndLine(); delete tmap; delete read; return 0; } - - read->AssembleTrees(); - vector T = read->getTrees(); - delete read; - - //make sure all files match - //if you provide a namefile we will use the numNames in the namefile as long as the number of unique match the tree names size. - int numNamesInTree; - if (numUniquesInName == m->Treenames.size()) { numNamesInTree = nameMap.size(); } - else { numNamesInTree = m->Treenames.size(); } - - //output any names that are in group file but not in tree - if (numNamesInTree < tmap->getNumSeqs()) { - for (int i = 0; i < tmap->namesOfSeqs.size(); i++) { - //is that name in the tree? - int count = 0; - for (int j = 0; j < m->Treenames.size(); j++) { - if (tmap->namesOfSeqs[i] == m->Treenames[j]) { break; } //found it - count++; - } - - if (m->control_pressed) { - delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; } - for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear(); - m->Groups.clear(); - return 0; - } - - //then you did not find it so report it - if (count == m->Treenames.size()) { - //if it is in your namefile then don't remove - map::iterator it = nameMap.find(tmap->namesOfSeqs[i]); - - if (it == nameMap.end()) { - m->mothurOut(tmap->namesOfSeqs[i] + " is in your groupfile and not in your tree. It will be disregarded."); m->mothurOutEndLine(); - tmap->removeSeq(tmap->namesOfSeqs[i]); - i--; //need this because removeSeq removes name from namesOfSeqs - } - } - } - } - + TreeReader* reader = new TreeReader(treefile, "", namefile); + vector T = reader->getTrees(); + map nameMap; + m->readNames(namefile, nameMap); + delete reader; //print new Tree - string outputFile = outputDir + m->getRootName(m->getSimpleName(treefile)) + "deunique.tre"; + string outputFile = outputDir + m->getRootName(m->getSimpleName(treefile)) + getOutputFileNameTag("tree"); outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile); ofstream out; m->openOutputFile(outputFile, out); - T[0]->print(out, "deunique"); + T[0]->print(out, nameMap); out.close(); - delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; } + delete (T[0]->getCountTable()); + for (int i = 0; i < T.size(); i++) { delete T[i]; } //set phylip file as new current phylipfile string current = ""; @@ -231,46 +196,6 @@ int DeuniqueTreeCommand::execute() { exit(1); } } -/*****************************************************************/ -int DeuniqueTreeCommand::readNamesFile() { - try { - m->names.clear(); - numUniquesInName = 0; - - ifstream in; - m->openInputFile(namefile, in); - - string first, second; - map::iterator itNames; - - while(!in.eof()) { - in >> first >> second; m->gobble(in); - - numUniquesInName++; - - itNames = m->names.find(first); - if (itNames == m->names.end()) { - m->names[first] = second; - - //we need a list of names in your namefile to use above when removing extra seqs above so we don't remove them - vector dupNames; - m->splitAtComma(second, dupNames); - - for (int i = 0; i < dupNames.size(); i++) { - nameMap[dupNames[i]] = dupNames[i]; - if (i != 0) { tmap->addSeq(dupNames[i], "Group1"); } - } - }else { m->mothurOut(first + " has already been seen in namefile, aborting."); m->mothurOutEndLine(); in.close(); m->names.clear(); m->control_pressed = true; return 1; } - } - in.close(); - - return 0; - } - catch(exception& e) { - m->errorOut(e, "DeuniqueTreeCommand", "readNamesFile"); - exit(1); - } -} /***********************************************************/