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::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, "GetOtusCommand", "getValidParameters");
27 //**********************************************************************************************************************
28 GetOtusCommand::GetOtusCommand(){
30 abort = true; calledHelp = true;
31 vector<string> tempOutNames;
32 outputTypes["group"] = tempOutNames;
33 outputTypes["list"] = tempOutNames;
36 m->errorOut(e, "GetOtusCommand", "GetOtusCommand");
40 //**********************************************************************************************************************
41 vector<string> GetOtusCommand::getRequiredParameters(){
43 string Array[] = {"group","label", "list"};
44 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
48 m->errorOut(e, "GetOtusCommand", "getRequiredParameters");
52 //**********************************************************************************************************************
53 vector<string> GetOtusCommand::getRequiredFiles(){
55 vector<string> myArray;
59 m->errorOut(e, "GetOtusCommand", "getRequiredFiles");
63 //**********************************************************************************************************************
64 GetOtusCommand::GetOtusCommand(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, "GetOtusCommand", "GetOtusCommand");
158 //**********************************************************************************************************************
160 void GetOtusCommand::help(){
162 m->mothurOut("The get.otus command selects 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 from in the those specified groups.\n");
164 m->mothurOut("The get.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 get.otus command should be in the following format: get.otus(accnos=yourAccnos, list=yourListFile, group=yourGroupFile, label=yourLabel).\n");
169 m->mothurOut("Example get.otus(accnos=amazon.accnos, list=amazon.fn.list, group=amazon.groups, label=0.03).\n");
170 m->mothurOut("or get.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, "GetOtusCommand", "help");
179 //**********************************************************************************************************************
181 int GetOtusCommand::execute(){
184 if (abort == true) { if (calledHelp) { return 0; } return 2; }
186 groupMap = new GroupMap(groupfile);
189 //get groups you want to get
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();
211 //set list file as new current listfile
213 itTypes = outputTypes.find("group");
214 if (itTypes != outputTypes.end()) {
215 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
218 itTypes = outputTypes.find("list");
219 if (itTypes != outputTypes.end()) {
220 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
227 catch(exception& e) {
228 m->errorOut(e, "GetOtusCommand", "execute");
232 //**********************************************************************************************************************
233 int GetOtusCommand::readListGroup(){
235 string thisOutputDir = outputDir;
236 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
237 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick." + label + m->getExtension(listfile);
240 m->openOutputFile(outputFileName, out);
242 string GroupOutputDir = outputDir;
243 if (outputDir == "") { GroupOutputDir += m->hasPath(groupfile); }
244 string outputGroupFileName = GroupOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick." + label + m->getExtension(groupfile);
247 m->openOutputFile(outputGroupFileName, outGroup);
249 InputData* input = new InputData(listfile, "list");
250 ListVector* list = input->getListVector();
251 string lastLabel = list->getLabel();
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(); remove(outputFileName.c_str()); remove(outputGroupFileName.c_str());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(); remove(outputFileName.c_str()); remove(outputGroupFileName.c_str()); 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 //**********************************************************************************************************************