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"
14 //**********************************************************************************************************************
15 vector<string> RemoveOtusCommand::setParameters(){
17 CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pgroup);
18 CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(plist);
19 CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(paccnos);
20 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
21 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
22 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
23 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
25 vector<string> myArray;
26 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
30 m->errorOut(e, "RemoveOtusCommand", "setParameters");
34 //**********************************************************************************************************************
35 string RemoveOtusCommand::getHelpString(){
37 string helpString = "";
38 helpString += "The remove.otus command removes otus containing sequences from a specfic group or set of groups.\n";
39 helpString += "It outputs a new list file containing the otus containing sequences NOT from in the those specified groups.\n";
40 helpString += "The remove.otus command parameters are accnos, group, list, label and groups. The group and list parameters are required, unless you have valid current files.\n";
41 helpString += "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";
42 helpString += "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";
43 helpString += "The label parameter allows you to specify which distance you want to process.\n";
44 helpString += "The remove.otus command should be in the following format: remove.otus(accnos=yourAccnos, list=yourListFile, group=yourGroupFile, label=yourLabel).\n";
45 helpString += "Example remove.otus(accnos=amazon.accnos, list=amazon.fn.list, group=amazon.groups, label=0.03).\n";
46 helpString += "or remove.otus(groups=pasture, list=amazon.fn.list, amazon.groups, label=0.03).\n";
47 helpString += "Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n";
51 m->errorOut(e, "RemoveOtusCommand", "getHelpString");
56 ///**********************************************************************************************************************
57 RemoveOtusCommand::RemoveOtusCommand(){
59 abort = true; calledHelp = true;
61 vector<string> tempOutNames;
62 outputTypes["group"] = tempOutNames;
63 outputTypes["list"] = tempOutNames;
66 m->errorOut(e, "RemoveOtusCommand", "RemoveOtusCommand");
71 //**********************************************************************************************************************
72 RemoveOtusCommand::RemoveOtusCommand(string option) {
74 abort = false; calledHelp = false;
76 //allow user to run help
77 if(option == "help") { help(); abort = true; calledHelp = true; }
78 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
81 vector<string> myArray = setParameters();
83 OptionParser parser(option);
84 map<string,string> parameters = parser.getParameters();
86 ValidParameters validParameter;
87 map<string,string>::iterator it;
89 //check to make sure all parameters are valid for command
90 for (it = parameters.begin(); it != parameters.end(); it++) {
91 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
94 //initialize outputTypes
95 vector<string> tempOutNames;
96 outputTypes["group"] = tempOutNames;
97 outputTypes["list"] = tempOutNames;
100 //if the user changes the output directory command factory will send this info to us in the output parameter
101 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
103 //if the user changes the input directory command factory will send this info to us in the output parameter
104 string inputDir = validParameter.validFile(parameters, "inputdir", false);
105 if (inputDir == "not found"){ inputDir = ""; }
108 it = parameters.find("accnos");
109 //user has given a template file
110 if(it != parameters.end()){
111 path = m->hasPath(it->second);
112 //if the user has not given a path then, add inputdir. else leave path alone.
113 if (path == "") { parameters["accnos"] = inputDir + it->second; }
116 it = parameters.find("list");
117 //user has given a template file
118 if(it != parameters.end()){
119 path = m->hasPath(it->second);
120 //if the user has not given a path then, add inputdir. else leave path alone.
121 if (path == "") { parameters["list"] = inputDir + it->second; }
124 it = parameters.find("group");
125 //user has given a template file
126 if(it != parameters.end()){
127 path = m->hasPath(it->second);
128 //if the user has not given a path then, add inputdir. else leave path alone.
129 if (path == "") { parameters["group"] = inputDir + it->second; }
134 //check for required parameters
135 accnosfile = validParameter.validFile(parameters, "accnos", true);
136 if (accnosfile == "not open") { abort = true; }
137 else if (accnosfile == "not found") { accnosfile = ""; }
138 else { m->setAccnosFile(accnosfile); }
140 groupfile = validParameter.validFile(parameters, "group", true);
141 if (groupfile == "not open") { abort = true; }
142 else if (groupfile == "not found") {
143 groupfile = m->getGroupFile();
144 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
145 else { m->mothurOut("You have no current group file and the group parameter is required."); m->mothurOutEndLine(); abort = true; }
146 }else { m->setGroupFile(groupfile); }
148 listfile = validParameter.validFile(parameters, "list", true);
149 if (listfile == "not open") { abort = true; }
150 else if (listfile == "not found") {
151 listfile = m->getListFile();
152 if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
153 else { m->mothurOut("You have no current list file and the list parameter is required."); m->mothurOutEndLine(); abort = true; }
154 }else { m->setListFile(listfile); }
156 groups = validParameter.validFile(parameters, "groups", false);
157 if (groups == "not found") { groups = ""; }
159 m->splitAtDash(groups, Groups);
162 label = validParameter.validFile(parameters, "label", false);
163 if (label == "not found") { label = ""; m->mothurOut("You did not provide a label, I will use the first label in your inputfile."); m->mothurOutEndLine(); label=""; }
165 if ((accnosfile == "") && (Groups.size() == 0)) { m->mothurOut("You must provide an accnos file or specify groups using the groups parameter."); m->mothurOutEndLine(); abort = true; }
169 catch(exception& e) {
170 m->errorOut(e, "RemoveOtusCommand", "RemoveOtusCommand");
174 //**********************************************************************************************************************
176 int RemoveOtusCommand::execute(){
179 if (abort == true) { if (calledHelp) { return 0; } return 2; }
181 groupMap = new GroupMap(groupfile);
184 //get groups you want to remove
185 if (accnosfile != "") { readAccnos(); }
187 //make sure groups are valid
188 //takes care of user setting groupNames that are invalid or setting groups=all
189 SharedUtil* util = new SharedUtil();
190 vector<string> allGroups = groupMap->getNamesOfGroups();
191 util->setGroups(Groups, allGroups);
194 if (m->control_pressed) { delete groupMap; return 0; }
196 //read through the list file keeping any otus that contain any sequence from the groups selected
199 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
201 if (outputNames.size() != 0) {
202 m->mothurOutEndLine();
203 m->mothurOut("Output File names: "); m->mothurOutEndLine();
204 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
205 m->mothurOutEndLine();
207 //set fasta file as new current fastafile
209 itTypes = outputTypes.find("group");
210 if (itTypes != outputTypes.end()) {
211 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
214 itTypes = outputTypes.find("list");
215 if (itTypes != outputTypes.end()) {
216 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
223 catch(exception& e) {
224 m->errorOut(e, "RemoveOtusCommand", "execute");
228 //**********************************************************************************************************************
229 int RemoveOtusCommand::readListGroup(){
231 string thisOutputDir = outputDir;
232 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
233 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick." + label + m->getExtension(listfile);
236 m->openOutputFile(outputFileName, out);
238 string GroupOutputDir = outputDir;
239 if (outputDir == "") { GroupOutputDir += m->hasPath(groupfile); }
240 string outputGroupFileName = GroupOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick." + label + m->getExtension(groupfile);
243 m->openOutputFile(outputGroupFileName, outGroup);
245 InputData* input = new InputData(listfile, "list");
246 ListVector* list = input->getListVector();
247 string lastLabel = list->getLabel();
249 //using first label seen if none is provided
250 if (label == "") { label = lastLabel; }
252 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
253 set<string> labels; labels.insert(label);
254 set<string> processedLabels;
255 set<string> userLabels = labels;
257 bool wroteSomething = false;
259 //as long as you are not at the end of the file or done wih the lines you want
260 while((list != NULL) && (userLabels.size() != 0)) {
262 if (m->control_pressed) { delete list; delete input; out.close(); outGroup.close(); m->mothurRemove(outputFileName); m->mothurRemove(outputGroupFileName);return 0; }
264 if(labels.count(list->getLabel()) == 1){
265 processList(list, groupMap, out, outGroup, wroteSomething);
267 processedLabels.insert(list->getLabel());
268 userLabels.erase(list->getLabel());
271 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
272 string saveLabel = list->getLabel();
276 list = input->getListVector(lastLabel);
278 processList(list, groupMap, out, outGroup, wroteSomething);
280 processedLabels.insert(list->getLabel());
281 userLabels.erase(list->getLabel());
283 //restore real lastlabel to save below
284 list->setLabel(saveLabel);
287 lastLabel = list->getLabel();
289 delete list; list = NULL;
291 //get next line to process
292 list = input->getListVector();
296 if (m->control_pressed) { if (list != NULL) { delete list; } delete input; out.close(); outGroup.close(); m->mothurRemove(outputFileName); m->mothurRemove(outputGroupFileName); return 0; }
298 //output error messages about any remaining user labels
299 set<string>::iterator it;
300 bool needToRun = false;
301 for (it = userLabels.begin(); it != userLabels.end(); it++) {
302 m->mothurOut("Your file does not include the label " + *it);
303 if (processedLabels.count(lastLabel) != 1) {
304 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
307 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
311 //run last label if you need to
312 if (needToRun == true) {
313 if (list != NULL) { delete list; }
315 list = input->getListVector(lastLabel);
317 processList(list, groupMap, out, outGroup, wroteSomething);
319 delete list; list = NULL;
325 if (wroteSomething == false) { m->mothurOut("At distance " + label + " your file ONLY contains otus containing sequences from the groups you wish to remove."); m->mothurOutEndLine(); }
326 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
327 outputTypes["group"].push_back(outputGroupFileName); outputNames.push_back(outputGroupFileName);
332 catch(exception& e) {
333 m->errorOut(e, "RemoveOtusCommand", "readList");
337 //**********************************************************************************************************************
338 int RemoveOtusCommand::processList(ListVector*& list, GroupMap*& groupMap, ofstream& out, ofstream& outGroup, bool& wroteSomething){
341 //make a new list vector
343 newList.setLabel(list->getLabel());
347 for (int i = 0; i < list->getNumBins(); i++) {
348 if (m->control_pressed) { return 0; }
350 //parse out names that are in accnos file
351 string binnames = list->get(i);
353 bool removeBin = false;
354 string groupFileOutput = "";
357 string individual = "";
358 int length = binnames.length();
359 for(int j=0;j<length;j++){
360 if(binnames[j] == ','){
361 string group = groupMap->getGroup(individual);
362 if (group == "not found") { m->mothurOut("[ERROR]: " + individual + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; }
364 if (m->inUsersGroups(group, Groups)) { removeBin = true; break; }
365 groupFileOutput += individual + "\t" + group + "\n";
369 else{ individual += binnames[j]; }
374 string group = groupMap->getGroup(individual);
375 if (group == "not found") { m->mothurOut("[ERROR]: " + individual + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; }
377 if (m->inUsersGroups(group, Groups)) { removeBin = true; }
378 groupFileOutput += individual + "\t" + group + "\n";
381 //if there are no sequences from the groups we want to remove in this bin add to new list, output to groupfile
382 newList.push_back(binnames);
383 outGroup << groupFileOutput;
393 //print new listvector
394 if (newList.getNumBins() != 0) {
395 wroteSomething = true;
399 m->mothurOut(newList.getLabel() + " - removed " + toString(numOtus) + " of the " + toString(list->getNumBins()) + " OTUs."); m->mothurOutEndLine();
404 catch(exception& e) {
405 m->errorOut(e, "RemoveOtusCommand", "processList");
409 //**********************************************************************************************************************
410 void RemoveOtusCommand::readAccnos(){
415 m->openInputFile(accnosfile, in);
421 Groups.push_back(name);
428 catch(exception& e) {
429 m->errorOut(e, "RemoveOtusCommand", "readAccnos");
433 //**********************************************************************************************************************