5 * Created by westcott on 11/10/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "getotuscommand.h"
11 #include "inputdata.h"
12 #include "sharedutilities.h"
15 //**********************************************************************************************************************
16 vector<string> GetOtusCommand::setParameters(){
18 CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pgroup);
19 CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(plist);
20 CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(paccnos);
21 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
22 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
23 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
24 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
26 vector<string> myArray;
27 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
31 m->errorOut(e, "GetOtusCommand", "setParameters");
35 //**********************************************************************************************************************
36 string GetOtusCommand::getHelpString(){
38 string helpString = "";
39 helpString += "The get.otus command selects otus containing sequences from a specfic group or set of groups.\n";
40 helpString += "It outputs a new list file containing the otus containing sequences from in the those specified groups.\n";
41 helpString += "The get.otus command parameters are accnos, group, list, label and groups. The group and list parameters are required, unless you have valid current files.\n";
42 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";
43 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";
44 helpString += "The label parameter allows you to specify which distance you want to process.\n";
45 helpString += "The get.otus command should be in the following format: get.otus(accnos=yourAccnos, list=yourListFile, group=yourGroupFile, label=yourLabel).\n";
46 helpString += "Example get.otus(accnos=amazon.accnos, list=amazon.fn.list, group=amazon.groups, label=0.03).\n";
47 helpString += "or get.otus(groups=pasture, list=amazon.fn.list, amazon.groups, label=0.03).\n";
48 helpString += "Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n";
52 m->errorOut(e, "GetOtusCommand", "getHelpString");
57 //**********************************************************************************************************************
58 GetOtusCommand::GetOtusCommand(){
60 abort = true; calledHelp = true;
62 vector<string> tempOutNames;
63 outputTypes["group"] = tempOutNames;
64 outputTypes["list"] = tempOutNames;
67 m->errorOut(e, "GetOtusCommand", "GetOtusCommand");
71 //**********************************************************************************************************************
72 GetOtusCommand::GetOtusCommand(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, "GetOtusCommand", "GetOtusCommand");
174 //**********************************************************************************************************************
176 int GetOtusCommand::execute(){
179 if (abort == true) { if (calledHelp) { return 0; } return 2; }
181 groupMap = new GroupMap(groupfile);
184 //get groups you want to get
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> gNamesOfGroups = groupMap->getNamesOfGroups();
191 util->setGroups(Groups, gNamesOfGroups);
192 groupMap->setNamesOfGroups(gNamesOfGroups);
195 if (m->control_pressed) { delete groupMap; return 0; }
197 //read through the list file keeping any otus that contain any sequence from the groups selected
200 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
202 if (outputNames.size() != 0) {
203 m->mothurOutEndLine();
204 m->mothurOut("Output File names: "); m->mothurOutEndLine();
205 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
206 m->mothurOutEndLine();
208 //set list file as new current listfile
210 itTypes = outputTypes.find("group");
211 if (itTypes != outputTypes.end()) {
212 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
215 itTypes = outputTypes.find("list");
216 if (itTypes != outputTypes.end()) {
217 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
224 catch(exception& e) {
225 m->errorOut(e, "GetOtusCommand", "execute");
229 //**********************************************************************************************************************
230 int GetOtusCommand::readListGroup(){
232 string thisOutputDir = outputDir;
233 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
234 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick." + label + m->getExtension(listfile);
237 m->openOutputFile(outputFileName, out);
239 string GroupOutputDir = outputDir;
240 if (outputDir == "") { GroupOutputDir += m->hasPath(groupfile); }
241 string outputGroupFileName = GroupOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick." + label + m->getExtension(groupfile);
244 m->openOutputFile(outputGroupFileName, outGroup);
246 InputData* input = new InputData(listfile, "list");
247 ListVector* list = input->getListVector();
248 string lastLabel = list->getLabel();
250 //using first label seen if none is provided
251 if (label == "") { label = lastLabel; }
253 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
254 set<string> labels; labels.insert(label);
255 set<string> processedLabels;
256 set<string> userLabels = labels;
258 bool wroteSomething = false;
260 //as long as you are not at the end of the file or done wih the lines you want
261 while((list != NULL) && (userLabels.size() != 0)) {
263 if (m->control_pressed) { delete list; delete input; out.close(); outGroup.close(); m->mothurRemove(outputFileName); m->mothurRemove(outputGroupFileName);return 0; }
265 if(labels.count(list->getLabel()) == 1){
266 processList(list, groupMap, out, outGroup, wroteSomething);
268 processedLabels.insert(list->getLabel());
269 userLabels.erase(list->getLabel());
272 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
273 string saveLabel = list->getLabel();
277 list = input->getListVector(lastLabel);
279 processList(list, groupMap, out, outGroup, wroteSomething);
281 processedLabels.insert(list->getLabel());
282 userLabels.erase(list->getLabel());
284 //restore real lastlabel to save below
285 list->setLabel(saveLabel);
288 lastLabel = list->getLabel();
290 delete list; list = NULL;
292 //get next line to process
293 list = input->getListVector();
297 if (m->control_pressed) { if (list != NULL) { delete list; } delete input; out.close(); outGroup.close(); m->mothurRemove(outputFileName); m->mothurRemove(outputGroupFileName); return 0; }
299 //output error messages about any remaining user labels
300 set<string>::iterator it;
301 bool needToRun = false;
302 for (it = userLabels.begin(); it != userLabels.end(); it++) {
303 m->mothurOut("Your file does not include the label " + *it);
304 if (processedLabels.count(lastLabel) != 1) {
305 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
308 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
312 //run last label if you need to
313 if (needToRun == true) {
314 if (list != NULL) { delete list; }
316 list = input->getListVector(lastLabel);
318 processList(list, groupMap, out, outGroup, wroteSomething);
320 delete list; list = NULL;
326 if (wroteSomething == false) { m->mothurOut("At distance " + label + " your file does NOT contain any otus containing sequences from the groups you wish to get."); m->mothurOutEndLine(); }
327 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
328 outputTypes["group"].push_back(outputGroupFileName); outputNames.push_back(outputGroupFileName);
333 catch(exception& e) {
334 m->errorOut(e, "GetOtusCommand", "readList");
338 //**********************************************************************************************************************
339 int GetOtusCommand::processList(ListVector*& list, GroupMap*& groupMap, ofstream& out, ofstream& outGroup, bool& wroteSomething){
342 //make a new list vector
344 newList.setLabel(list->getLabel());
348 for (int i = 0; i < list->getNumBins(); i++) {
349 if (m->control_pressed) { return 0; }
351 //parse out names that are in accnos file
352 string binnames = list->get(i);
354 bool keepBin = false;
355 string groupFileOutput = "";
358 string individual = "";
359 int length = binnames.length();
360 for(int j=0;j<length;j++){
361 if(binnames[j] == ','){
362 string group = groupMap->getGroup(individual);
363 if (group == "not found") { m->mothurOut("[ERROR]: " + individual + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; }
365 if (m->inUsersGroups(group, Groups)) { keepBin = true; }
366 groupFileOutput += individual + "\t" + group + "\n";
370 else{ individual += binnames[j]; }
373 string group = groupMap->getGroup(individual);
374 if (group == "not found") { m->mothurOut("[ERROR]: " + individual + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; }
376 if (m->inUsersGroups(group, Groups)) { keepBin = true; }
377 groupFileOutput += individual + "\t" + group + "\n";
379 //if there are sequences from the groups we want in this bin add to new list, output to groupfile
381 newList.push_back(binnames);
382 outGroup << groupFileOutput;
387 //print new listvector
388 if (newList.getNumBins() != 0) {
389 wroteSomething = true;
393 m->mothurOut(newList.getLabel() + " - selected " + toString(numOtus) + " of the " + toString(list->getNumBins()) + " OTUs."); m->mothurOutEndLine();
397 catch(exception& e) {
398 m->errorOut(e, "GetOtusCommand", "processList");
402 //**********************************************************************************************************************
403 void GetOtusCommand::readAccnos(){
408 m->openInputFile(accnosfile, in);
414 Groups.push_back(name);
421 catch(exception& e) {
422 m->errorOut(e, "GetOtusCommand", "readAccnos");
426 //**********************************************************************************************************************