X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=splitgroupscommand.cpp;h=af3ca665b5791d0001141f27192b78aab171d71a;hb=28bcfc4a41b8b82f66636587e0d4d355d07cbdd1;hp=bdb6e7c0151b10cb96fc6d0aa59d66199dba2e6f;hpb=ca9ac1d80c62f57270b0dcd49410ebe08a8aecd6;p=mothur.git diff --git a/splitgroupscommand.cpp b/splitgroupscommand.cpp index bdb6e7c..af3ca66 100644 --- a/splitgroupscommand.cpp +++ b/splitgroupscommand.cpp @@ -9,6 +9,7 @@ #include "splitgroupscommand.h" #include "sharedutilities.h" +#include "sequenceparser.h" //********************************************************************************************************************** vector SplitGroupCommand::setParameters(){ @@ -41,7 +42,7 @@ string SplitGroupCommand::getHelpString(){ helpString += "If you want .fasta and .names files for all groups, set groups=all. \n"; helpString += "The split.group command should be used in the following format: split.group(fasta=yourFasta, group=yourGroupFile).\n"; helpString += "Example: split.group(fasta=abrecovery.fasta, group=abrecovery.groups).\n"; - helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n"; + helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n"; return helpString; } catch(exception& e) { @@ -50,6 +51,27 @@ string SplitGroupCommand::getHelpString(){ } } //********************************************************************************************************************** +string SplitGroupCommand::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 == "fasta") { outputFileName = "fasta"; } + else if (type == "name") { outputFileName = "names"; } + 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, "SplitGroupCommand", "getOutputFileNameTag"); + exit(1); + } +} +//********************************************************************************************************************** SplitGroupCommand::SplitGroupCommand(){ try { abort = true; calledHelp = true; @@ -70,6 +92,7 @@ SplitGroupCommand::SplitGroupCommand(string option) { //allow user to run help if(option == "help") { help(); abort = true; calledHelp = true; } + else if(option == "citation") { citation(); abort = true; calledHelp = true;} else { vector myArray = setParameters(); @@ -123,8 +146,9 @@ SplitGroupCommand::SplitGroupCommand(string option) { namefile = validParameter.validFile(parameters, "name", true); - if (namefile == "not open") { abort = true; } + if (namefile == "not open") { namefile = ""; abort = true; } else if (namefile == "not found") { namefile = ""; } + else { m->setNameFile(namefile); } fastafile = validParameter.validFile(parameters, "fasta", true); if (fastafile == "not open") { abort = true; } @@ -132,7 +156,7 @@ SplitGroupCommand::SplitGroupCommand(string option) { fastafile = m->getFastaFile(); if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); } else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; } - } + }else { m->setFastaFile(fastafile); } groupfile = validParameter.validFile(parameters, "group", true); if (groupfile == "not open") { groupfile = ""; abort = true; } @@ -140,7 +164,7 @@ SplitGroupCommand::SplitGroupCommand(string option) { groupfile = m->getGroupFile(); if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); } else { m->mothurOut("You have no current groupfile and the group parameter is required."); m->mothurOutEndLine(); abort = true; } - } + }else { m->setGroupFile(groupfile); } groups = validParameter.validFile(parameters, "groups", false); if (groups == "not found") { groups = ""; } @@ -148,6 +172,11 @@ SplitGroupCommand::SplitGroupCommand(string option) { //if the user changes the output directory command factory will send this info to us in the output parameter outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(groupfile); } + + if (namefile == "") { + vector files; files.push_back(fastafile); + parser.getNameFile(files); + } } } @@ -162,17 +191,42 @@ int SplitGroupCommand::execute(){ if (abort == true) { if (calledHelp) { return 0; } return 2; } - groupMap = new GroupMap(groupfile); - groupMap->readMap(); + SequenceParser* parser; - SharedUtil util; util.setGroups(Groups, groupMap->namesOfGroups); + if (namefile == "") { parser = new SequenceParser(groupfile, fastafile); } + else { parser = new SequenceParser(groupfile, fastafile, namefile); } - if (namefile != "") { readNames(); } - splitFasta(); + if (m->control_pressed) { delete parser; return 0; } + + vector namesGroups = parser->getNamesOfGroups(); + SharedUtil util; util.setGroups(Groups, namesGroups); - delete groupMap; + string fastafileRoot = outputDir + m->getRootName(m->getSimpleName(fastafile)); + string namefileRoot = outputDir + m->getRootName(m->getSimpleName(namefile)); - if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; } + m->mothurOutEndLine(); + for (int i = 0; i < Groups.size(); i++) { + + m->mothurOut("Processing group: " + Groups[i]); m->mothurOutEndLine(); + + string newFasta = fastafileRoot + Groups[i] + "." + getOutputFileNameTag("fasta"); + string newName = namefileRoot + Groups[i] + "." + getOutputFileNameTag("name"); + + parser->getSeqs(Groups[i], newFasta, false); + outputNames.push_back(newFasta); outputTypes["fasta"].push_back(newFasta); + if (m->control_pressed) { delete parser; for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; } + + if (namefile != "") { + parser->getNameMap(Groups[i], newName); + outputNames.push_back(newName); outputTypes["name"].push_back(newName); + } + + if (m->control_pressed) { delete parser; for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; } + } + + delete parser; + + if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; } string current = ""; itTypes = outputTypes.find("fasta"); @@ -197,147 +251,6 @@ int SplitGroupCommand::execute(){ exit(1); } } -/**********************************************************************************************************************/ -int SplitGroupCommand::readNames() { - try { - //open input file - ifstream in; - m->openInputFile(namefile, in); - - while (!in.eof()) { - if (m->control_pressed) { break; } - - string firstCol, secondCol; - in >> firstCol >> secondCol; m->gobble(in); - - vector names; - m->splitAtComma(secondCol, names); - - nameMap[firstCol] = names; - } - in.close(); - - return 0; - - } - catch(exception& e) { - m->errorOut(e, "SplitGroupCommand", "readNames"); - exit(1); - } -} - -/**********************************************************************************************************************/ -int SplitGroupCommand::splitFasta() { - try { - - string filerootFasta = outputDir + m->getRootName(m->getSimpleName(fastafile)); - string filerootName = outputDir + m->getRootName(m->getSimpleName(namefile)); - ofstream* temp; - ofstream* temp2; - map filehandles; - map::iterator it3; - - for (int i=0; iopenOutputFile(filerootFasta + Groups[i] + ".fasta", *(filehandles[Groups[i]+"fasta"])); - outputNames.push_back(filerootFasta + Groups[i] + ".fasta"); outputTypes["fasta"].push_back(filerootFasta + Groups[i] + ".fasta"); - - if (namefile != "") { - temp2 = new ofstream; - filehandles[Groups[i]+"name"] = temp2; - m->openOutputFile(filerootName + Groups[i] + ".names", *(filehandles[Groups[i]+"name"])); - outputNames.push_back(filerootName + Groups[i] + ".names"); outputTypes["name"].push_back(filerootFasta + Groups[i] + ".names"); - } - } - - //open input file - ifstream in; - m->openInputFile(fastafile, in); - - while (!in.eof()) { - if (m->control_pressed) { break; } - - Sequence seq(in); m->gobble(in); - - if (seq.getName() != "") { - - itNames = nameMap.find(seq.getName()); - - if (itNames == nameMap.end()) { - if (namefile != "") { m->mothurOut(seq.getName() + " is not in your namesfile, ignoring."); m->mothurOutEndLine(); } - else { //no names file given - string group = groupMap->getGroup(seq.getName()); - - if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want - seq.printSequence(*(filehandles[group+"fasta"])); - }else if(group == "not found") { - m->mothurOut(seq.getName() + " is not in your groupfile. Ignoring."); m->mothurOutEndLine(); - } - } - }else{ - vector names = itNames->second; - map group2Names; - map::iterator it; - - for (int i = 0; i < names.size(); i++) { //build strings for new group names file, will select rep below - string group = groupMap->getGroup(names[i]); - - if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want - it = group2Names.find(group); - if (it == group2Names.end()) { - group2Names[group] = names[i] + ","; - }else{ - group2Names[group] += names[i] + ","; - } - }else if(group == "not found") { - m->mothurOut(names[i] + " is not in your groupfile. Ignoring."); m->mothurOutEndLine(); - } - } - - for (map::iterator itGroups = group2Names.begin(); itGroups != group2Names.end(); itGroups++) { - //edit names string, by grabbing the first guy as the rep and removing the last comma - string newNames = itGroups->second; - newNames = newNames.substr(0, newNames.length()-1); - string repName = ""; - - int pos = newNames.find_first_of(','); - if (pos == newNames.npos) { //only one sequence so it represents itself - repName = newNames; - }else{ - repName = newNames.substr(0, pos); - } - - *(filehandles[itGroups->first+"name"]) << repName << '\t' << newNames << endl; - *(filehandles[itGroups->first+"fasta"]) << ">" << repName << endl << seq.getAligned() << endl; - } - } - } - } - - in.close(); - - for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { - (*(filehandles[it3->first])).close(); - delete it3->second; - } - - vector newOutputNames; - //remove blank files - for (int i = 0; i < outputNames.size(); i++) { - if (m->isBlank(outputNames[i])) { - remove(outputNames[i].c_str()); - }else { newOutputNames.push_back(outputNames[i]); } - } - outputNames = newOutputNames; - - return 0; +//********************************************************************************************************************** - } - catch(exception& e) { - m->errorOut(e, "SplitGroupCommand", "splitFasta"); - exit(1); - } -} -/**********************************************************************************************************************/