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","group",false,true, true); parameters.push_back(pgroup);
19 CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none","list",false,true, 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");
56 //**********************************************************************************************************************
57 string GetOtusCommand::getOutputPattern(string type) {
61 if (type == "group") { pattern = "[filename],[tag],pick,[extension]"; }
62 else if (type == "list") { pattern = "[filename],[tag],pick,[extension]"; }
63 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
68 m->errorOut(e, "GetOtusCommand", "getOutputPattern");
72 //**********************************************************************************************************************
73 GetOtusCommand::GetOtusCommand(){
75 abort = true; calledHelp = true;
77 vector<string> tempOutNames;
78 outputTypes["group"] = tempOutNames;
79 outputTypes["list"] = tempOutNames;
82 m->errorOut(e, "GetOtusCommand", "GetOtusCommand");
86 //**********************************************************************************************************************
87 GetOtusCommand::GetOtusCommand(string option) {
89 abort = false; calledHelp = false;
91 //allow user to run help
92 if(option == "help") { help(); abort = true; calledHelp = true; }
93 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
96 vector<string> myArray = setParameters();
98 OptionParser parser(option);
99 map<string,string> parameters = parser.getParameters();
101 ValidParameters validParameter;
102 map<string,string>::iterator it;
104 //check to make sure all parameters are valid for command
105 for (it = parameters.begin(); it != parameters.end(); it++) {
106 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
109 //initialize outputTypes
110 vector<string> tempOutNames;
111 outputTypes["group"] = tempOutNames;
112 outputTypes["list"] = tempOutNames;
115 //if the user changes the output directory command factory will send this info to us in the output parameter
116 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
118 //if the user changes the input directory command factory will send this info to us in the output parameter
119 string inputDir = validParameter.validFile(parameters, "inputdir", false);
120 if (inputDir == "not found"){ inputDir = ""; }
123 it = parameters.find("accnos");
124 //user has given a template file
125 if(it != parameters.end()){
126 path = m->hasPath(it->second);
127 //if the user has not given a path then, add inputdir. else leave path alone.
128 if (path == "") { parameters["accnos"] = inputDir + it->second; }
131 it = parameters.find("list");
132 //user has given a template file
133 if(it != parameters.end()){
134 path = m->hasPath(it->second);
135 //if the user has not given a path then, add inputdir. else leave path alone.
136 if (path == "") { parameters["list"] = inputDir + it->second; }
139 it = parameters.find("group");
140 //user has given a template file
141 if(it != parameters.end()){
142 path = m->hasPath(it->second);
143 //if the user has not given a path then, add inputdir. else leave path alone.
144 if (path == "") { parameters["group"] = inputDir + it->second; }
149 //check for required parameters
150 accnosfile = validParameter.validFile(parameters, "accnos", true);
151 if (accnosfile == "not open") { abort = true; }
152 else if (accnosfile == "not found") { accnosfile = ""; }
153 else { m->setAccnosFile(accnosfile); }
155 groupfile = validParameter.validFile(parameters, "group", true);
156 if (groupfile == "not open") { abort = true; }
157 else if (groupfile == "not found") {
158 groupfile = m->getGroupFile();
159 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
160 else { m->mothurOut("You have no current group file and the group parameter is required."); m->mothurOutEndLine(); abort = true; }
161 }else { m->setGroupFile(groupfile); }
163 listfile = validParameter.validFile(parameters, "list", true);
164 if (listfile == "not open") { abort = true; }
165 else if (listfile == "not found") {
166 listfile = m->getListFile();
167 if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
168 else { m->mothurOut("You have no current list file and the list parameter is required."); m->mothurOutEndLine(); abort = true; }
169 }else { m->setListFile(listfile); }
171 groups = validParameter.validFile(parameters, "groups", false);
172 if (groups == "not found") { groups = ""; }
174 m->splitAtDash(groups, Groups);
177 label = validParameter.validFile(parameters, "label", false);
178 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=""; }
180 if ((accnosfile == "") && (Groups.size() == 0)) { m->mothurOut("You must provide an accnos file or specify groups using the groups parameter."); m->mothurOutEndLine(); abort = true; }
184 catch(exception& e) {
185 m->errorOut(e, "GetOtusCommand", "GetOtusCommand");
189 //**********************************************************************************************************************
191 int GetOtusCommand::execute(){
194 if (abort == true) { if (calledHelp) { return 0; } return 2; }
196 groupMap = new GroupMap(groupfile);
199 //get groups you want to get
200 if (accnosfile != "") { m->readAccnos(accnosfile, Groups); m->setGroups(Groups); }
202 //make sure groups are valid
203 //takes care of user setting groupNames that are invalid or setting groups=all
204 SharedUtil* util = new SharedUtil();
205 vector<string> gNamesOfGroups = groupMap->getNamesOfGroups();
206 util->setGroups(Groups, gNamesOfGroups);
207 groupMap->setNamesOfGroups(gNamesOfGroups);
210 if (m->control_pressed) { delete groupMap; return 0; }
212 //read through the list file keeping any otus that contain any sequence from the groups selected
215 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
217 if (outputNames.size() != 0) {
218 m->mothurOutEndLine();
219 m->mothurOut("Output File names: "); m->mothurOutEndLine();
220 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
221 m->mothurOutEndLine();
223 //set list file as new current listfile
225 itTypes = outputTypes.find("group");
226 if (itTypes != outputTypes.end()) {
227 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
230 itTypes = outputTypes.find("list");
231 if (itTypes != outputTypes.end()) {
232 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
239 catch(exception& e) {
240 m->errorOut(e, "GetOtusCommand", "execute");
244 //**********************************************************************************************************************
245 int GetOtusCommand::readListGroup(){
247 string thisOutputDir = outputDir;
248 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
249 map<string, string> variables;
250 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(listfile));
251 variables["[tag]"] = label;
252 variables["[extension]"] = m->getExtension(listfile);
253 string outputFileName = getOutputFileName("list", variables);
256 m->openOutputFile(outputFileName, out);
258 string GroupOutputDir = outputDir;
259 if (outputDir == "") { GroupOutputDir += m->hasPath(groupfile); }
260 variables["[filename]"] = GroupOutputDir + m->getRootName(m->getSimpleName(groupfile));
261 variables["[extension]"] = m->getExtension(groupfile);
262 string outputGroupFileName = getOutputFileName("group", variables);
265 m->openOutputFile(outputGroupFileName, outGroup);
267 InputData* input = new InputData(listfile, "list");
268 ListVector* list = input->getListVector();
269 string lastLabel = list->getLabel();
271 //using first label seen if none is provided
272 if (label == "") { label = lastLabel; }
274 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
275 set<string> labels; labels.insert(label);
276 set<string> processedLabels;
277 set<string> userLabels = labels;
279 bool wroteSomething = false;
281 //as long as you are not at the end of the file or done wih the lines you want
282 while((list != NULL) && (userLabels.size() != 0)) {
284 if (m->control_pressed) { delete list; delete input; out.close(); outGroup.close(); m->mothurRemove(outputFileName); m->mothurRemove(outputGroupFileName);return 0; }
286 if(labels.count(list->getLabel()) == 1){
287 processList(list, groupMap, out, outGroup, wroteSomething);
289 processedLabels.insert(list->getLabel());
290 userLabels.erase(list->getLabel());
293 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
294 string saveLabel = list->getLabel();
298 list = input->getListVector(lastLabel);
300 processList(list, groupMap, out, outGroup, wroteSomething);
302 processedLabels.insert(list->getLabel());
303 userLabels.erase(list->getLabel());
305 //restore real lastlabel to save below
306 list->setLabel(saveLabel);
309 lastLabel = list->getLabel();
311 delete list; list = NULL;
313 //get next line to process
314 list = input->getListVector();
318 if (m->control_pressed) { if (list != NULL) { delete list; } delete input; out.close(); outGroup.close(); m->mothurRemove(outputFileName); m->mothurRemove(outputGroupFileName); return 0; }
320 //output error messages about any remaining user labels
321 set<string>::iterator it;
322 bool needToRun = false;
323 for (it = userLabels.begin(); it != userLabels.end(); it++) {
324 m->mothurOut("Your file does not include the label " + *it);
325 if (processedLabels.count(lastLabel) != 1) {
326 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
329 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
333 //run last label if you need to
334 if (needToRun == true) {
335 if (list != NULL) { delete list; }
337 list = input->getListVector(lastLabel);
339 processList(list, groupMap, out, outGroup, wroteSomething);
341 delete list; list = NULL;
347 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(); }
348 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
349 outputTypes["group"].push_back(outputGroupFileName); outputNames.push_back(outputGroupFileName);
354 catch(exception& e) {
355 m->errorOut(e, "GetOtusCommand", "readList");
359 //**********************************************************************************************************************
360 int GetOtusCommand::processList(ListVector*& list, GroupMap*& groupMap, ofstream& out, ofstream& outGroup, bool& wroteSomething){
363 //make a new list vector
365 newList.setLabel(list->getLabel());
369 for (int i = 0; i < list->getNumBins(); i++) {
370 if (m->control_pressed) { return 0; }
372 //parse out names that are in accnos file
373 string binnames = list->get(i);
375 bool keepBin = false;
376 string groupFileOutput = "";
379 string individual = "";
380 int length = binnames.length();
381 for(int j=0;j<length;j++){
382 if(binnames[j] == ','){
383 string group = groupMap->getGroup(individual);
384 if (group == "not found") { m->mothurOut("[ERROR]: " + individual + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; }
386 if (m->inUsersGroups(group, Groups)) { keepBin = true; }
387 groupFileOutput += individual + "\t" + group + "\n";
391 else{ individual += binnames[j]; }
394 string group = groupMap->getGroup(individual);
395 if (group == "not found") { m->mothurOut("[ERROR]: " + individual + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; }
397 if (m->inUsersGroups(group, Groups)) { keepBin = true; }
398 groupFileOutput += individual + "\t" + group + "\n";
400 //if there are sequences from the groups we want in this bin add to new list, output to groupfile
402 newList.push_back(binnames);
403 outGroup << groupFileOutput;
408 //print new listvector
409 if (newList.getNumBins() != 0) {
410 wroteSomething = true;
414 m->mothurOut(newList.getLabel() + " - selected " + toString(numOtus) + " of the " + toString(list->getNumBins()) + " OTUs."); m->mothurOutEndLine();
418 catch(exception& e) {
419 m->errorOut(e, "GetOtusCommand", "processList");
423 //**********************************************************************************************************************