X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=blobdiff_plain;f=splitgroupscommand.cpp;h=5c0103708bc99daaf6d0046435882c72a6c631a7;hp=16d90a48372f7c97d46f7fd4d0b640fde6b533fb;hb=d1c97b8c04bb75faca1e76ffad60b37a4d789d3d;hpb=348de0f8b17d84ede77081dcf67bd6ef43496677 diff --git a/splitgroupscommand.cpp b/splitgroupscommand.cpp index 16d90a4..5c01037 100644 --- a/splitgroupscommand.cpp +++ b/splitgroupscommand.cpp @@ -9,49 +9,76 @@ #include "splitgroupscommand.h" #include "sharedutilities.h" +#include "sequenceparser.h" +#include "counttable.h" //********************************************************************************************************************** -vector SplitGroupCommand::getValidParameters(){ - try { - string Array[] = {"name","group","groups","fasta","outputdir","inputdir"}; - vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); +vector SplitGroupCommand::setParameters(){ + try { + CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none","fasta",false,true,true); parameters.push_back(pfasta); + CommandParameter pname("name", "InputTypes", "", "", "NameCount", "none", "none","name",false,false,true); parameters.push_back(pname); + CommandParameter pcount("count", "InputTypes", "", "", "NameCount-CountGroup", "CountGroup", "none","count",false,false,true); parameters.push_back(pcount); + CommandParameter pgroup("group", "InputTypes", "", "", "CountGroup", "CountGroup", "none","group",false,false,true); parameters.push_back(pgroup); + CommandParameter pgroups("groups", "String", "", "", "", "", "","",false,false); parameters.push_back(pgroups); + CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir); + CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir); + + vector myArray; + for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); } return myArray; } catch(exception& e) { - m->errorOut(e, "SplitGroupCommand", "getValidParameters"); + m->errorOut(e, "SplitGroupCommand", "setParameters"); exit(1); } } //********************************************************************************************************************** -vector SplitGroupCommand::getRequiredParameters(){ +string SplitGroupCommand::getHelpString(){ try { - string Array[] = {"fasta","group"}; - vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); - return myArray; + string helpString = ""; + helpString += "The split.groups command reads a group or count file, and parses your fasta and names or count files by groups. \n"; + helpString += "The split.groups command parameters are fasta, name, group, count and groups.\n"; + helpString += "The fasta and group or count parameters are required.\n"; + helpString += "The groups parameter allows you to select groups to create files for. \n"; + helpString += "For example if you set groups=A-B-C, you will get a .A.fasta, .A.names, .B.fasta, .B.names, .C.fasta, .C.names files. \n"; + helpString += "If you want .fasta and .names files for all groups, set groups=all. \n"; + helpString += "The split.groups command should be used in the following format: split.group(fasta=yourFasta, group=yourGroupFile).\n"; + helpString += "Example: split.groups(fasta=abrecovery.fasta, group=abrecovery.groups).\n"; + helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n"; + return helpString; } catch(exception& e) { - m->errorOut(e, "SplitGroupCommand", "getRequiredParameters"); + m->errorOut(e, "SplitGroupCommand", "getHelpString"); exit(1); } } //********************************************************************************************************************** -vector SplitGroupCommand::getRequiredFiles(){ - try { - vector myArray; - return myArray; - } - catch(exception& e) { - m->errorOut(e, "SplitGroupCommand", "getRequiredFiles"); - exit(1); - } +string SplitGroupCommand::getOutputPattern(string type) { + try { + string pattern = ""; + + if (type == "fasta") { pattern = "[filename],[group],fasta"; } + else if (type == "name") { pattern = "[filename],[group],names"; } + else if (type == "count") { pattern = "[filename],[group],count_table"; } + else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; } + + return pattern; + } + catch(exception& e) { + m->errorOut(e, "SplitGroupCommand", "getOutputPattern"); + exit(1); + } } + //********************************************************************************************************************** SplitGroupCommand::SplitGroupCommand(){ try { abort = true; calledHelp = true; + setParameters(); vector tempOutNames; outputTypes["fasta"] = tempOutNames; outputTypes["name"] = tempOutNames; + outputTypes["count"] = tempOutNames; } catch(exception& e) { m->errorOut(e, "SplitGroupCommand", "SplitGroupCommand"); @@ -65,11 +92,10 @@ 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 { - //valid paramters for this command - string Array[] = {"name","group","groups","fasta","outputdir","inputdir"}; - vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + vector myArray = setParameters(); OptionParser parser(option); map parameters = parser.getParameters(); @@ -86,6 +112,7 @@ SplitGroupCommand::SplitGroupCommand(string option) { vector tempOutNames; outputTypes["fasta"] = tempOutNames; outputTypes["name"] = tempOutNames; + outputTypes["count"] = tempOutNames; //if the user changes the input directory command factory will send this info to us in the output parameter string inputDir = validParameter.validFile(parameters, "inputdir", false); @@ -115,28 +142,82 @@ SplitGroupCommand::SplitGroupCommand(string option) { //if the user has not given a path then, add inputdir. else leave path alone. if (path == "") { parameters["name"] = inputDir + it->second; } } - + + it = parameters.find("count"); + //user has given a template file + if(it != parameters.end()){ + path = m->hasPath(it->second); + //if the user has not given a path then, add inputdir. else leave path alone. + if (path == "") { parameters["count"] = inputDir + it->second; } + } } 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; } - else if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the split.group command. "); m->mothurOutEndLine(); abort = true; } + else if (fastafile == "not found") { + 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; } - else if (groupfile == "not found") { groupfile = ""; m->mothurOut("group is a required parameter for the split.group command. "); m->mothurOutEndLine(); abort = true; } + else if (groupfile == "not found") { groupfile = ""; + }else { m->setGroupFile(groupfile); } + + countfile = validParameter.validFile(parameters, "count", true); + if (countfile == "not open") { countfile = ""; abort = true; } + else if (countfile == "not found") { countfile = ""; } + else { m->setCountTableFile(countfile); } + + if ((countfile != "") && (namefile != "")) { m->mothurOut("You must enter ONLY ONE of the following: count or name."); m->mothurOutEndLine(); abort = true; } + + if ((countfile != "") && (groupfile != "")) { m->mothurOut("You must enter ONLY ONE of the following: count or group."); m->mothurOutEndLine(); abort = true; } + + if ((countfile == "") && (groupfile == "")) { + if (namefile == "") { //check for count then group + countfile = m->getCountTableFile(); + if (countfile != "") { m->mothurOut("Using " + countfile + " as input file for the count parameter."); m->mothurOutEndLine(); } + else { + groupfile = m->getGroupFile(); + if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); } + else { + m->mothurOut("You need to provide a count or group file."); m->mothurOutEndLine(); + abort = true; + } + } + }else { //check for group + groupfile = m->getGroupFile(); + if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); } + else { + m->mothurOut("You need to provide a count or group file."); m->mothurOutEndLine(); + abort = true; + } + } + } groups = validParameter.validFile(parameters, "groups", false); if (groups == "not found") { groups = ""; } else { m->splitAtDash(groups, Groups); } //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); } + outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ + if (groupfile != "") { outputDir = m->hasPath(groupfile); } + else { outputDir = m->hasPath(countfile); } + } + + if (countfile == "") { + if (namefile == "") { + vector files; files.push_back(fastafile); + parser.getNameFile(files); + } + } } } @@ -146,43 +227,15 @@ SplitGroupCommand::SplitGroupCommand(string option) { } } //********************************************************************************************************************** -void SplitGroupCommand::help(){ - try { - m->mothurOut("The split.group command reads a group file, and parses your fasta and names files by groups. \n"); - m->mothurOut("The split.group command parameters are fasta, name, group and groups.\n"); - m->mothurOut("The fasta and group parameters are required.\n"); - m->mothurOut("The groups parameter allows you to select groups to create files for. \n"); - m->mothurOut("For example if you set groups=A-B-C, you will get a .A.fasta, .A.names, .B.fasta, .B.names, .C.fasta, .C.names files. \n"); - m->mothurOut("If you want .fasta and .names files for all groups, set groups=all. \n"); - m->mothurOut("The split.group command should be used in the following format: split.group(fasta=yourFasta, group=yourGroupFile).\n"); - m->mothurOut("Example: split.group(fasta=abrecovery.fasta, group=abrecovery.groups).\n"); - m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n"); - - } - catch(exception& e) { - m->errorOut(e, "SplitGroupCommand", "help"); - exit(1); - } -} -//********************************************************************************************************************** -SplitGroupCommand::~SplitGroupCommand(){ } -//********************************************************************************************************************** int SplitGroupCommand::execute(){ try { if (abort == true) { if (calledHelp) { return 0; } return 2; } - groupMap = new GroupMap(groupfile); - groupMap->readMap(); - - SharedUtil util; util.setGroups(Groups, groupMap->namesOfGroups); - - if (namefile != "") { readNames(); } - splitFasta(); - - delete groupMap; - - if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; } + if (countfile == "" ) { runNameGroup(); } + else { runCount(); } + + if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; } string current = ""; itTypes = outputTypes.find("fasta"); @@ -194,6 +247,11 @@ int SplitGroupCommand::execute(){ if (itTypes != outputTypes.end()) { if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); } } + + itTypes = outputTypes.find("count"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setCountTableFile(current); } + } m->mothurOutEndLine(); m->mothurOut("Output File Names: "); m->mothurOutEndLine(); @@ -207,147 +265,125 @@ int SplitGroupCommand::execute(){ exit(1); } } -/**********************************************************************************************************************/ -int SplitGroupCommand::readNames() { +//********************************************************************************************************************** +int SplitGroupCommand::runNameGroup(){ try { - //open input file - ifstream in; - m->openInputFile(namefile, in); + SequenceParser* parser; + if (namefile == "") { parser = new SequenceParser(groupfile, fastafile); } + else { parser = new SequenceParser(groupfile, fastafile, namefile); } + + if (m->control_pressed) { delete parser; return 0; } + + vector namesGroups = parser->getNamesOfGroups(); + SharedUtil util; util.setGroups(Groups, namesGroups); - while (!in.eof()) { - if (m->control_pressed) { break; } + string fastafileRoot = outputDir + m->getRootName(m->getSimpleName(fastafile)); + string namefileRoot = outputDir + m->getRootName(m->getSimpleName(namefile)); + + m->mothurOutEndLine(); + for (int i = 0; i < Groups.size(); i++) { - string firstCol, secondCol; - in >> firstCol >> secondCol; m->gobble(in); + m->mothurOut("Processing group: " + Groups[i]); m->mothurOutEndLine(); - vector names; - m->splitAtComma(secondCol, names); + map variables; + variables["[filename]"] = fastafileRoot; + variables["[group]"] = Groups[i]; + + string newFasta = getOutputFileName("fasta",variables); + variables["[filename]"] = namefileRoot; + string newName = getOutputFileName("name",variables); + + 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); + } - nameMap[firstCol] = names; + if (m->control_pressed) { delete parser; for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; } } - in.close(); - - return 0; + + delete parser; + + return 0; - } + } catch(exception& e) { - m->errorOut(e, "SplitGroupCommand", "readNames"); + m->errorOut(e, "SplitGroupCommand", "runNameGroup"); exit(1); } } - -/**********************************************************************************************************************/ -int SplitGroupCommand::splitFasta() { +//********************************************************************************************************************** +int SplitGroupCommand::runCount(){ 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; + + CountTable ct; + ct.readTable(countfile, true); + if (!ct.hasGroupInfo()) { m->mothurOut("[ERROR]: your count file does not contain group info, cannot split by group.\n"); m->control_pressed = true; } + + if (m->control_pressed) { return 0; } + + vector namesGroups = ct.getNamesOfGroups(); + SharedUtil util; util.setGroups(Groups, namesGroups); + + //fill filehandles with neccessary ofstreams + map ffiles; + map cfiles; + ofstream* temp; + for (int i=0; i variables; + variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(fastafile)); + variables["[group]"] = Groups[i]; + string newFasta = getOutputFileName("fasta",variables); + outputNames.push_back(newFasta); outputTypes["fasta"].push_back(newFasta); + m->openOutputFile(newFasta, (*temp)); + temp = new ofstream; + cfiles[Groups[i]] = temp; + variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(countfile)); + string newCount = getOutputFileName("count",variables); + m->openOutputFile(newCount, (*temp)); + outputNames.push_back(newCount); outputTypes["count"].push_back(newCount); + (*temp) << "Representative_Sequence\ttotal\t" << Groups[i] << endl; + } + + ifstream in; + m->openInputFile(fastafile, in); + + while (!in.eof()) { + Sequence seq(in); m->gobble(in); + + if (m->control_pressed) { break; } + if (seq.getName() != "") { + vector thisSeqsGroups = ct.getGroups(seq.getName()); + for (int i = 0; i < thisSeqsGroups.size(); i++) { + if (m->inUsersGroups(thisSeqsGroups[i], Groups)) { //if this sequence belongs to a group we want them print + seq.printSequence(*(ffiles[thisSeqsGroups[i]])); + int numSeqs = ct.getGroupCount(seq.getName(), thisSeqsGroups[i]); + (*(cfiles[thisSeqsGroups[i]])) << seq.getName() << '\t' << numSeqs << '\t' << numSeqs << endl; + } + } + } + } + in.close(); + + //close and delete ofstreams + for (int i=0; ierrorOut(e, "SplitGroupCommand", "splitFasta"); + m->errorOut(e, "SplitGroupCommand", "runCount"); exit(1); } } -/**********************************************************************************************************************/ +//********************************************************************************************************************** +