2 * removeotuscommand.cpp
5 * Created by westcott on 11/12/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "removeotuscommand.h"
11 #include "inputdata.h"
12 #include "sharedutilities.h"
15 //**********************************************************************************************************************
16 vector<string> RemoveOtusCommand::getValidParameters(){
18 string Array[] = { "group", "accnos","label", "groups","list","outputdir","inputdir" };
19 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
23 m->errorOut(e, "RemoveOtusCommand", "getValidParameters");
27 //**********************************************************************************************************************
28 RemoveOtusCommand::RemoveOtusCommand(){
30 abort = true; calledHelp = true;
31 vector<string> tempOutNames;
32 outputTypes["group"] = tempOutNames;
33 outputTypes["list"] = tempOutNames;
36 m->errorOut(e, "RemoveOtusCommand", "RemoveOtusCommand");
40 //**********************************************************************************************************************
41 vector<string> RemoveOtusCommand::getRequiredParameters(){
43 string Array[] = {"group","label", "list"};
44 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
48 m->errorOut(e, "RemoveOtusCommand", "getRequiredParameters");
52 //**********************************************************************************************************************
53 vector<string> RemoveOtusCommand::getRequiredFiles(){
55 vector<string> myArray;
59 m->errorOut(e, "RemoveOtusCommand", "getRequiredFiles");
63 //**********************************************************************************************************************
64 RemoveOtusCommand::RemoveOtusCommand(string option) {
66 abort = false; calledHelp = false;
68 //allow user to run help
69 if(option == "help") { help(); abort = true; calledHelp = true; }
72 //valid paramters for this command
73 string Array[] = { "group", "accnos","label", "groups", "list","outputdir","inputdir" };
74 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
76 OptionParser parser(option);
77 map<string,string> parameters = parser.getParameters();
79 ValidParameters validParameter;
80 map<string,string>::iterator it;
82 //check to make sure all parameters are valid for command
83 for (it = parameters.begin(); it != parameters.end(); it++) {
84 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
87 //initialize outputTypes
88 vector<string> tempOutNames;
89 outputTypes["group"] = tempOutNames;
90 outputTypes["list"] = tempOutNames;
93 //if the user changes the output directory command factory will send this info to us in the output parameter
94 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
96 //if the user changes the input directory command factory will send this info to us in the output parameter
97 string inputDir = validParameter.validFile(parameters, "inputdir", false);
98 if (inputDir == "not found"){ inputDir = ""; }
101 it = parameters.find("accnos");
102 //user has given a template file
103 if(it != parameters.end()){
104 path = m->hasPath(it->second);
105 //if the user has not given a path then, add inputdir. else leave path alone.
106 if (path == "") { parameters["accnos"] = inputDir + it->second; }
109 it = parameters.find("list");
110 //user has given a template file
111 if(it != parameters.end()){
112 path = m->hasPath(it->second);
113 //if the user has not given a path then, add inputdir. else leave path alone.
114 if (path == "") { parameters["list"] = inputDir + it->second; }
117 it = parameters.find("group");
118 //user has given a template file
119 if(it != parameters.end()){
120 path = m->hasPath(it->second);
121 //if the user has not given a path then, add inputdir. else leave path alone.
122 if (path == "") { parameters["group"] = inputDir + it->second; }
127 //check for required parameters
128 accnosfile = validParameter.validFile(parameters, "accnos", true);
129 if (accnosfile == "not open") { abort = true; }
130 else if (accnosfile == "not found") { accnosfile = ""; }
132 groupfile = validParameter.validFile(parameters, "group", true);
133 if (groupfile == "not open") { abort = true; }
134 else if (groupfile == "not found") { groupfile = ""; m->mothurOut("You must provide a group file."); m->mothurOutEndLine(); abort = true; }
136 listfile = validParameter.validFile(parameters, "list", true);
137 if (listfile == "not open") { abort = true; }
138 else if (listfile == "not found") { listfile = ""; m->mothurOut("You must provide a list file."); m->mothurOutEndLine(); abort = true; }
140 groups = validParameter.validFile(parameters, "groups", false);
141 if (groups == "not found") { groups = ""; }
143 m->splitAtDash(groups, Groups);
146 label = validParameter.validFile(parameters, "label", false);
147 if (label == "not found") { label = ""; m->mothurOut("You must provide a label to process."); m->mothurOutEndLine(); abort = true; }
149 if ((accnosfile == "") && (Groups.size() == 0)) { m->mothurOut("You must provide an accnos file or specify groups using the groups parameter."); m->mothurOutEndLine(); abort = true; }
153 catch(exception& e) {
154 m->errorOut(e, "RemoveOtusCommand", "RemoveOtusCommand");
158 //**********************************************************************************************************************
160 void RemoveOtusCommand::help(){
162 m->mothurOut("The remove.otus command removes otus containing sequences from a specfic group or set of groups.\n");
163 m->mothurOut("It outputs a new list file containing the otus containing sequences NOT from in the those specified groups.\n");
164 m->mothurOut("The remove.otus command parameters are accnos, group, list, label and groups. The group, list and label parameters are required.\n");
165 m->mothurOut("You must also provide an accnos containing the list of groups to get or set the groups parameter to the groups you wish to select.\n");
166 m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like. You can separate group names with dashes.\n");
167 m->mothurOut("The label parameter allows you to specify which distance you want to process.\n");
168 m->mothurOut("The remove.otus command should be in the following format: remove.otus(accnos=yourAccnos, list=yourListFile, group=yourGroupFile, label=yourLabel).\n");
169 m->mothurOut("Example remove.otus(accnos=amazon.accnos, list=amazon.fn.list, group=amazon.groups, label=0.03).\n");
170 m->mothurOut("or remove.otus(groups=pasture, list=amazon.fn.list, amazon.groups, label=0.03).\n");
171 m->mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n\n");
173 catch(exception& e) {
174 m->errorOut(e, "RemoveOtusCommand", "help");
179 //**********************************************************************************************************************
181 int RemoveOtusCommand::execute(){
184 if (abort == true) { if (calledHelp) { return 0; } return 2; }
186 groupMap = new GroupMap(groupfile);
189 //get groups you want to remove
190 if (accnosfile != "") { readAccnos(); }
192 //make sure groups are valid
193 //takes care of user setting groupNames that are invalid or setting groups=all
194 SharedUtil* util = new SharedUtil();
195 util->setGroups(Groups, groupMap->namesOfGroups);
198 if (m->control_pressed) { delete groupMap; return 0; }
200 //read through the list file keeping any otus that contain any sequence from the groups selected
203 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
205 if (outputNames.size() != 0) {
206 m->mothurOutEndLine();
207 m->mothurOut("Output File names: "); m->mothurOutEndLine();
208 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
209 m->mothurOutEndLine();
215 catch(exception& e) {
216 m->errorOut(e, "RemoveOtusCommand", "execute");
220 //**********************************************************************************************************************
221 int RemoveOtusCommand::readListGroup(){
223 string thisOutputDir = outputDir;
224 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
225 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick." + label + m->getExtension(listfile);
228 m->openOutputFile(outputFileName, out);
230 string GroupOutputDir = outputDir;
231 if (outputDir == "") { GroupOutputDir += m->hasPath(groupfile); }
232 string outputGroupFileName = GroupOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick." + label + m->getExtension(groupfile);
235 m->openOutputFile(outputGroupFileName, outGroup);
237 InputData* input = new InputData(listfile, "list");
238 ListVector* list = input->getListVector();
239 string lastLabel = list->getLabel();
241 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
242 set<string> labels; labels.insert(label);
243 set<string> processedLabels;
244 set<string> userLabels = labels;
246 bool wroteSomething = false;
248 //as long as you are not at the end of the file or done wih the lines you want
249 while((list != NULL) && (userLabels.size() != 0)) {
251 if (m->control_pressed) { delete list; delete input; out.close(); outGroup.close(); remove(outputFileName.c_str()); remove(outputGroupFileName.c_str());return 0; }
253 if(labels.count(list->getLabel()) == 1){
254 processList(list, groupMap, out, outGroup, wroteSomething);
256 processedLabels.insert(list->getLabel());
257 userLabels.erase(list->getLabel());
260 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
261 string saveLabel = list->getLabel();
265 list = input->getListVector(lastLabel);
267 processList(list, groupMap, out, outGroup, wroteSomething);
269 processedLabels.insert(list->getLabel());
270 userLabels.erase(list->getLabel());
272 //restore real lastlabel to save below
273 list->setLabel(saveLabel);
276 lastLabel = list->getLabel();
278 delete list; list = NULL;
280 //get next line to process
281 list = input->getListVector();
285 if (m->control_pressed) { if (list != NULL) { delete list; } delete input; out.close(); outGroup.close(); remove(outputFileName.c_str()); remove(outputGroupFileName.c_str()); return 0; }
287 //output error messages about any remaining user labels
288 set<string>::iterator it;
289 bool needToRun = false;
290 for (it = userLabels.begin(); it != userLabels.end(); it++) {
291 m->mothurOut("Your file does not include the label " + *it);
292 if (processedLabels.count(lastLabel) != 1) {
293 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
296 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
300 //run last label if you need to
301 if (needToRun == true) {
302 if (list != NULL) { delete list; }
304 list = input->getListVector(lastLabel);
306 processList(list, groupMap, out, outGroup, wroteSomething);
308 delete list; list = NULL;
314 if (wroteSomething == false) { m->mothurOut("At distance " + label + " your file ONLY contains otus containing sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
315 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
316 outputTypes["group"].push_back(outputGroupFileName); outputNames.push_back(outputGroupFileName);
321 catch(exception& e) {
322 m->errorOut(e, "RemoveOtusCommand", "readList");
326 //**********************************************************************************************************************
327 int RemoveOtusCommand::processList(ListVector*& list, GroupMap*& groupMap, ofstream& out, ofstream& outGroup, bool& wroteSomething){
330 //make a new list vector
332 newList.setLabel(list->getLabel());
336 for (int i = 0; i < list->getNumBins(); i++) {
337 if (m->control_pressed) { return 0; }
339 //parse out names that are in accnos file
340 string binnames = list->get(i);
342 bool removeBin = false;
343 string groupFileOutput = "";
346 string individual = "";
347 int length = binnames.length();
348 for(int j=0;j<length;j++){
349 if(binnames[j] == ','){
350 string group = groupMap->getGroup(individual);
351 if (group == "not found") { m->mothurOut("[ERROR]: " + individual + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; }
353 if (m->inUsersGroups(group, Groups)) { removeBin = true; break; }
354 groupFileOutput += individual + "\t" + group + "\n";
358 else{ individual += binnames[j]; }
363 string group = groupMap->getGroup(individual);
364 if (group == "not found") { m->mothurOut("[ERROR]: " + individual + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; }
366 if (m->inUsersGroups(group, Groups)) { removeBin = true; }
367 groupFileOutput += individual + "\t" + group + "\n";
370 //if there are no sequences from the groups we want to remove in this bin add to new list, output to groupfile
371 newList.push_back(binnames);
372 outGroup << groupFileOutput;
382 //print new listvector
383 if (newList.getNumBins() != 0) {
384 wroteSomething = true;
388 m->mothurOut(newList.getLabel() + " - removed " + toString(numOtus) + " of the " + toString(list->getNumBins()) + " OTUs."); m->mothurOutEndLine();
393 catch(exception& e) {
394 m->errorOut(e, "RemoveOtusCommand", "processList");
398 //**********************************************************************************************************************
399 void RemoveOtusCommand::readAccnos(){
404 m->openInputFile(accnosfile, in);
410 Groups.push_back(name);
417 catch(exception& e) {
418 m->errorOut(e, "RemoveOtusCommand", "readAccnos");
422 //**********************************************************************************************************************