X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=sharedcommand.cpp;h=63f83e19a61fec97a6a64121da4f5ea973b5815a;hb=57b3c96832667c1b70d4d526331f52e3d49e8237;hp=b322007bc5e8ef6cba50d4c6d2ff5fce0b87a0fa;hpb=dbcb14c17c128987e59147f510950f32dbeb6698;p=mothur.git diff --git a/sharedcommand.cpp b/sharedcommand.cpp index b322007..63f83e1 100644 --- a/sharedcommand.cpp +++ b/sharedcommand.cpp @@ -18,7 +18,7 @@ vector SharedCommand::setParameters(){ try { CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(plist); CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pgroup); - CommandParameter pordergroup("ordergroup", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pordergroup); + //CommandParameter pordergroup("ordergroup", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pordergroup); CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel); CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups); CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir); @@ -41,7 +41,7 @@ string SharedCommand::getHelpString(){ helpString += "The make.shared command parameters are list, group, ordergroup, groups and label. list and group are required unless a current file is available.\n"; helpString += "The groups parameter allows you to indicate which groups you want to include, group names should be separated by dashes. ex. groups=A-B-C. Default is all groups in your groupfile.\n"; helpString += "The label parameter allows you to indicate which labels you want to include, label names should be separated by dashes. Default is all labels in your list file.\n"; - helpString += "The ordergroup parameter allows you to indicate the order of the groups in the sharedfile, by default the groups are listed alphabetically.\n"; + //helpString += "The ordergroup parameter allows you to indicate the order of the groups in the sharedfile, by default the groups are listed alphabetically.\n"; return helpString; } catch(exception& e) { @@ -131,7 +131,7 @@ SharedCommand::SharedCommand(string option) { listfile = m->getListFile(); if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); } else { m->mothurOut("You have no current list file and the list parameter is required."); m->mothurOutEndLine(); abort = true; } - } + }else { m->setListFile(listfile); } ordergroupfile = validParameter.validFile(parameters, "ordergroup", true); if (ordergroupfile == "not open") { abort = true; } @@ -147,7 +147,8 @@ SharedCommand::SharedCommand(string option) { int error = groupMap->readMap(); if (error == 1) { abort = true; } - m->namesOfGroups = groupMap->namesOfGroups; + vector allGroups = groupMap->getNamesOfGroups(); + m->setAllGroups(allGroups); } else { m->mothurOut("You have no current group file and the group parameter is required."); m->mothurOutEndLine(); abort = true; } }else { @@ -155,7 +156,8 @@ SharedCommand::SharedCommand(string option) { int error = groupMap->readMap(); if (error == 1) { abort = true; } - m->namesOfGroups = groupMap->namesOfGroups; + vector allGroups = groupMap->getNamesOfGroups(); + m->setAllGroups(allGroups); m->setGroupFile(groupfile); } @@ -163,7 +165,7 @@ SharedCommand::SharedCommand(string option) { if (groups == "not found") { groups = ""; } else { m->splitAtDash(groups, Groups); - m->Groups = Groups; + m->setGroups(Groups); } //check for optional parameter and set defaults @@ -203,7 +205,7 @@ int SharedCommand::execute(){ //if hte user has not specified any groups then use them all if (Groups.size() == 0) { - Groups = groupMap->namesOfGroups; m->Groups = Groups; + Groups = groupMap->getNamesOfGroups(); m->setGroups(Groups); }else { pickedGroups = true; } //fill filehandles with neccessary ofstreams @@ -219,7 +221,7 @@ int SharedCommand::execute(){ //clears file before we start to write to it below for (int i=0; imothurRemove((fileroot + Groups[i] + ".rabund")); outputNames.push_back((fileroot + Groups[i] + ".rabund")); outputTypes["rabund"].push_back((fileroot + Groups[i] + ".rabund")); } @@ -239,16 +241,19 @@ int SharedCommand::execute(){ if (m->control_pressed) { delete input; delete SharedList; delete groupMap; for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; } - out.close(); remove(filename.c_str()); - for (int i=0; imothurRemove(filename); + for (int i=0; imothurRemove((fileroot + Groups[i] + ".rabund")); } return 0; } - - if ((m->Groups.size() == 0) && (SharedList->getNumSeqs() != groupMap->getNumSeqs())) { //if the user has not specified any groups and their files don't match exit with error + + //sanity check + int error = ListGroupSameSeqs(); + + if ((!pickedGroups) && (SharedList->getNumSeqs() != groupMap->getNumSeqs())) { //if the user has not specified any groups and their files don't match exit with error m->mothurOut("Your group file contains " + toString(groupMap->getNumSeqs()) + " sequences and list file contains " + toString(SharedList->getNumSeqs()) + " sequences. Please correct."); m->mothurOutEndLine(); out.close(); - remove(filename.c_str()); //remove blank shared file you made + m->mothurRemove(filename); //remove blank shared file you made createMisMatchFile(); @@ -262,12 +267,14 @@ int SharedCommand::execute(){ return 0; } + if (error == 1) { m->control_pressed = true; } + //if user has specified groups make new groupfile for them if (pickedGroups) { //make new group file string groups = ""; - if (m->Groups.size() < 4) { - for (int i = 0; i < m->Groups.size(); i++) { - groups += m->Groups[i] + "."; + if (m->getNumGroups() < 4) { + for (int i = 0; i < m->getNumGroups(); i++) { + groups += (m->getGroups())[i] + "."; } }else { groups = "merge"; } @@ -281,7 +288,7 @@ int SharedCommand::execute(){ string groupName; for (int i = 0; i < names.size(); i++) { groupName = groupMap->getGroup(names[i]); - if (isValidGroup(groupName, m->Groups)) { + if (isValidGroup(groupName, m->getGroups())) { outGroups << names[i] << '\t' << groupName << endl; } } @@ -296,8 +303,8 @@ int SharedCommand::execute(){ if (m->control_pressed) { delete input; delete SharedList; delete groupMap; for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; } - out.close(); remove(filename.c_str()); - for (int i=0; imothurRemove(filename); + for (int i=0; imothurRemove((fileroot + Groups[i] + ".rabund")); } return 0; } @@ -314,8 +321,8 @@ int SharedCommand::execute(){ delete input; delete SharedList; delete groupMap; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; } - out.close(); remove(filename.c_str()); - for (int i=0; imothurRemove(filename); + for (int i=0; imothurRemove((fileroot + Groups[i] + ".rabund")); } return 0; } @@ -344,8 +351,8 @@ int SharedCommand::execute(){ delete input; delete SharedList; delete groupMap; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; } - out.close(); remove(filename.c_str()); - for (int i=0; imothurRemove(filename); + for (int i=0; imothurRemove((fileroot + Groups[i] + ".rabund")); } return 0; } @@ -390,8 +397,8 @@ int SharedCommand::execute(){ if (m->control_pressed) { delete input; delete groupMap; for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; } - out.close(); remove(filename.c_str()); - for (int i=0; imothurRemove(filename); + for (int i=0; imothurRemove((fileroot + Groups[i] + ".rabund")); } return 0; } @@ -410,8 +417,8 @@ int SharedCommand::execute(){ delete input; delete groupMap; if (m->control_pressed) { - remove(filename.c_str()); - for (int i=0; imothurRemove(filename); + for (int i=0; imothurRemove((fileroot + Groups[i] + ".rabund")); } return 0; } @@ -452,20 +459,22 @@ void SharedCommand::printSharedData(vector thislookup) { if (order.size() == 0) { //user has not specified an order so do aplabetically sort(thislookup.begin(), thislookup.end(), compareSharedRabunds); - m->Groups.clear(); + m->clearGroups(); + vector Groups; //initialize bin values for (int i = 0; i < thislookup.size(); i++) { out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t'; thislookup[i]->print(out); - m->Groups.push_back(thislookup[i]->getGroup()); + Groups.push_back(thislookup[i]->getGroup()); RAbundVector rav = thislookup[i]->getRAbundVector(); m->openOutputFileAppend(fileroot + thislookup[i]->getGroup() + ".rabund", *(filehandles[thislookup[i]->getGroup()])); rav.print(*(filehandles[thislookup[i]->getGroup()])); (*(filehandles[thislookup[i]->getGroup()])).close(); } + m->setGroups(Groups); }else{ //create a map from groupName to each sharedrabund map myMap; @@ -475,7 +484,8 @@ void SharedCommand::printSharedData(vector thislookup) { myMap[thislookup[i]->getGroup()] = thislookup[i]; } - m->Groups.clear(); + m->clearGroups(); + vector Groups; //loop through ordered list and print the rabund for (int i = 0; i < order.size(); i++) { @@ -485,7 +495,7 @@ void SharedCommand::printSharedData(vector thislookup) { out << (myIt->second)->getLabel() << '\t' << (myIt->second)->getGroup() << '\t'; (myIt->second)->print(out); - m->Groups.push_back((myIt->second)->getGroup()); + Groups.push_back((myIt->second)->getGroup()); RAbundVector rav = (myIt->second)->getRAbundVector(); m->openOutputFileAppend(fileroot + (myIt->second)->getGroup() + ".rabund", *(filehandles[(myIt->second)->getGroup()])); @@ -495,6 +505,8 @@ void SharedCommand::printSharedData(vector thislookup) { m->mothurOut("Can't find shared info for " + order[i] + ", skipping."); m->mothurOutEndLine(); } } + + m->setGroups(Groups); } @@ -531,6 +543,7 @@ int SharedCommand::eliminateZeroOTUS(vector& thislookup) { for (int j = 0; j < thislookup.size(); j++) { newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup()); } + //if there is a bin label use it otherwise make one } //else{ cout << "bin # " << i << " is all zeros" << endl; } } @@ -559,35 +572,28 @@ int SharedCommand::createMisMatchFile() { m->openOutputFile(outputMisMatchName, outMisMatch); - map listNames; - map::iterator itList; + set listNames; + set::iterator itList; //go through list and if group returns "not found" output it for (int i = 0; i < SharedList->getNumBins(); i++) { - if (m->control_pressed) { outMisMatch.close(); remove(outputMisMatchName.c_str()); return 0; } + if (m->control_pressed) { outMisMatch.close(); m->mothurRemove(outputMisMatchName); return 0; } string names = SharedList->get(i); - while (names.find_first_of(',') != -1) { - string name = names.substr(0,names.find_first_of(',')); - names = names.substr(names.find_first_of(',')+1, names.length()); + vector binNames; + m->splitAtComma(names, binNames); + + for (int j = 0; j < binNames.size(); j++) { + string name = binNames[j]; string group = groupMap->getGroup(name); if(group == "not found") { outMisMatch << name << endl; } itList = listNames.find(name); if (itList != listNames.end()) { m->mothurOut(name + " is in your list file more than once. Sequence names must be unique. please correct."); m->mothurOutEndLine(); } - else { listNames[name] = name; } + else { listNames.insert(name); } } - - //get last name - string group = groupMap->getGroup(names); - if(group == "not found") { outMisMatch << names << endl; } - - itList = listNames.find(names); - if (itList != listNames.end()) { m->mothurOut(names + " is in your list file more than once. Sequence names must be unique. please correct."); m->mothurOutEndLine(); } - else { listNames[names] = names; } - } outMisMatch.close(); @@ -608,9 +614,12 @@ int SharedCommand::createMisMatchFile() { string names = SharedList->get(i); - while (names.find_first_of(',') != -1) { - string name = names.substr(0,names.find_first_of(',')); - names = names.substr(names.find_first_of(',')+1, names.length()); + vector binNames; + m->splitAtComma(names, binNames); + + for (int j = 0; j < binNames.size(); j++) { + + string name = binNames[j]; itList = namesInList.find(name); if (itList != namesInList.end()) { m->mothurOut(name + " is in your list file more than once. Sequence names must be unique. please correct."); m->mothurOutEndLine(); } @@ -618,12 +627,6 @@ int SharedCommand::createMisMatchFile() { namesInList[name] = name; } - - itList = namesInList.find(names); - if (itList != namesInList.end()) { m->mothurOut(names + " is in your list file more than once. Sequence names must be unique. please correct."); m->mothurOutEndLine(); } - - //get last name - namesInList[names] = names; } //get names of sequences in groupfile @@ -635,7 +638,7 @@ int SharedCommand::createMisMatchFile() { //loop through names in seqNames and if they aren't in namesIn list output them for (int i = 0; i < seqNames.size(); i++) { - if (m->control_pressed) { outMisMatch.close(); remove(outputMisMatchName.c_str()); return 0; } + if (m->control_pressed) { outMisMatch.close(); m->mothurRemove(outputMisMatchName); return 0; } itMatch = namesInList.find(seqNames[i]); @@ -654,7 +657,48 @@ int SharedCommand::createMisMatchFile() { exit(1); } } - +//********************************************************************************************************************** +int SharedCommand::ListGroupSameSeqs() { + try { + + int error = 0; + + vector groupMapsSeqs = groupMap->getNamesSeqs(); + + set groupNamesSeqs; + for(int i = 0; i < groupMapsSeqs.size(); i++) { + groupNamesSeqs.insert(groupMapsSeqs[i]); + } + + //go through list and if group returns "not found" output it + for (int i = 0; i < SharedList->getNumBins(); i++) { + if (m->control_pressed) { return 0; } + + string names = SharedList->get(i); + + vector listNames; + m->splitAtComma(names, listNames); + + for (int j = 0; j < listNames.size(); j++) { + int num = groupNamesSeqs.count(listNames[j]); + + if (num == 0) { error = 1; m->mothurOut("[ERROR]: " + listNames[j] + " is in your listfile and not in your groupfile. Please correct."); m->mothurOutEndLine(); } + else { groupNamesSeqs.erase(listNames[j]); } + } + } + + for (set::iterator itGroupSet = groupNamesSeqs.begin(); itGroupSet != groupNamesSeqs.end(); itGroupSet++) { + error = 1; + m->mothurOut("[ERROR]: " + (*itGroupSet) + " is in your groupfile and not your listfile. Please correct."); m->mothurOutEndLine(); + } + + return error; + } + catch(exception& e) { + m->errorOut(e, "SharedCommand", "ListGroupSameSeqs"); + exit(1); + } +} //********************************************************************************************************************** SharedCommand::~SharedCommand(){