2 * getlistcountcommand.cpp
5 * Created by westcott on 10/12/09.
6 * Copyright 2009 Schloss Lab. All rights reserved.
10 #include "getlistcountcommand.h"
12 //**********************************************************************************************************************
13 vector<string> GetListCountCommand::getValidParameters(){
15 string Array[] = {"list","label","sort","outputdir","inputdir"};
16 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
20 m->errorOut(e, "GetListCountCommand", "getValidParameters");
24 //**********************************************************************************************************************
25 GetListCountCommand::GetListCountCommand(){
28 //initialize outputTypes
29 vector<string> tempOutNames;
30 outputTypes["otu"] = tempOutNames;
33 m->errorOut(e, "GetListCountCommand", "GetListCountCommand");
37 //**********************************************************************************************************************
38 vector<string> GetListCountCommand::getRequiredParameters(){
40 string Array[] = {"list"};
41 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
45 m->errorOut(e, "GetListCountCommand", "getRequiredParameters");
49 //**********************************************************************************************************************
50 vector<string> GetListCountCommand::getRequiredFiles(){
52 vector<string> myArray;
56 m->errorOut(e, "GetListCountCommand", "getRequiredFiles");
60 //**********************************************************************************************************************
61 GetListCountCommand::GetListCountCommand(string option) {
63 globaldata = GlobalData::getInstance();
68 //allow user to run help
69 if(option == "help") { help(); abort = true; }
72 //valid paramters for this command
73 string AlignArray[] = {"list","label","sort","outputdir","inputdir"};
74 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/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["otu"] = tempOutNames;
91 string ranRead = globaldata->getListFile();
93 //if the user changes the input directory command factory will send this info to us in the output parameter
94 string inputDir = validParameter.validFile(parameters, "inputdir", false);
95 if (inputDir == "not found"){ inputDir = ""; }
98 it = parameters.find("list");
99 //user has given a template file
100 if(it != parameters.end()){
101 path = m->hasPath(it->second);
102 //if the user has not given a path then, add inputdir. else leave path alone.
103 if (path == "") { parameters["list"] = inputDir + it->second; }
107 //if the user changes the output directory command factory will send this info to us in the output parameter
108 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
110 //check for required parameters
111 listfile = validParameter.validFile(parameters, "list", true);
112 if ((listfile == "not found") && (globaldata->getListFile() == "")) { m->mothurOut("You must read a listfile before running the get.listcount command."); m->mothurOutEndLine(); abort = true; }
113 else if ((listfile == "not found") && (globaldata->getListFile() != "")) { listfile = globaldata->getListFile(); }
114 else if (listfile == "not open") { abort = true; }
115 else { globaldata->setListFile(listfile); }
117 //check for optional parameter and set defaults
118 // ...at some point should added some additional type checking...
119 sort = validParameter.validFile(parameters, "sort", false); if (sort == "not found") { sort = "otu"; }
120 if ((sort != "otu") && (sort != "name")) { m->mothurOut( sort + " is not a valid sort option. Options are otu and name. I will use otu."); m->mothurOutEndLine(); sort = "otu"; }
122 label = validParameter.validFile(parameters, "label", false);
123 if (label == "not found") { label = ""; }
125 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
126 else { allLines = 1; }
129 //if the user has not specified any labels use the ones from read.otu
130 if ((label == "") && (ranRead != "")) {
131 allLines = globaldata->allLines;
132 labels = globaldata->labels;
136 catch(exception& e) {
137 m->errorOut(e, "GetListCountCommand", "GetListCountCommand");
141 //**********************************************************************************************************************
143 void GetListCountCommand::help(){
145 m->mothurOut("The get.otulist command can only be executed after a successful read.otu command of a listfile or providing a list file using the list parameter.\n");
146 m->mothurOut("The get.otulist command parameters are list, sort and label. No parameters are required.\n");
147 m->mothurOut("The label parameter allows you to select what distance levels you would like a output files created for, and are separated by dashes.\n");
148 m->mothurOut("The sort parameter allows you to select how you want the output displayed. Options are otu and name.\n");
149 m->mothurOut("If otu is selected the output will be otu number followed by the list of names in that otu.\n");
150 m->mothurOut("If name is selected the output will be a sequence name followed by its otu number.\n");
151 m->mothurOut("The get.otulist command should be in the following format: get.otulist(list=yourlistFile, label=yourLabels).\n");
152 m->mothurOut("Example get.otulist(list=amazon.fn.list, label=0.10).\n");
153 m->mothurOut("The default value for label is all lines in your inputfile.\n");
154 m->mothurOut("The get.otulist command outputs a .otu file for each distance you specify listing the bin number and the names of the sequences in that bin.\n");
155 m->mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n\n");
157 catch(exception& e) {
158 m->errorOut(e, "GetListCountCommand", "help");
163 //**********************************************************************************************************************
165 GetListCountCommand::~GetListCountCommand(){}
167 //**********************************************************************************************************************
169 int GetListCountCommand::execute(){
171 if (abort == true) { return 0; }
173 globaldata->setFormat("list");
176 read = new ReadOTUFile(listfile);
177 read->read(&*globaldata);
179 input = globaldata->ginput;
180 list = globaldata->gListVector;
181 string lastLabel = list->getLabel();
183 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
184 set<string> processedLabels;
185 set<string> userLabels = labels;
187 if (m->control_pressed) {
191 globaldata->gListVector = NULL;
192 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
196 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
198 if(allLines == 1 || labels.count(list->getLabel()) == 1){
202 if (m->control_pressed) {
206 globaldata->gListVector = NULL;
207 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
211 processedLabels.insert(list->getLabel());
212 userLabels.erase(list->getLabel());
215 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
216 string saveLabel = list->getLabel();
219 list = input->getListVector(lastLabel);
223 if (m->control_pressed) {
227 globaldata->gListVector = NULL;
228 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
232 processedLabels.insert(list->getLabel());
233 userLabels.erase(list->getLabel());
235 //restore real lastlabel to save below
236 list->setLabel(saveLabel);
239 lastLabel = list->getLabel();
242 list = input->getListVector();
246 //output error messages about any remaining user labels
247 set<string>::iterator it;
248 bool needToRun = false;
249 for (it = userLabels.begin(); it != userLabels.end(); it++) {
250 m->mothurOut("Your file does not include the label " + *it);
251 if (processedLabels.count(lastLabel) != 1) {
252 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
255 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
259 //run last label if you need to
260 if (needToRun == true) {
261 if (list != NULL) { delete list; }
262 list = input->getListVector(lastLabel);
266 if (m->control_pressed) {
270 globaldata->gListVector = NULL;
271 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
280 globaldata->gListVector = NULL;
282 m->mothurOutEndLine();
283 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
284 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
285 m->mothurOutEndLine();
289 catch(exception& e) {
290 m->errorOut(e, "GetListCountCommand", "execute");
295 //**********************************************************************************************************************
296 //return 1 if error, 0 otherwise
297 void GetListCountCommand::process(ListVector* list) {
300 if (outputDir == "") { outputDir += m->hasPath(listfile); }
301 string outputFileName = outputDir + m->getRootName(m->getSimpleName(listfile)) + list->getLabel() + ".otu";
302 m->openOutputFile(outputFileName, out);
303 outputNames.push_back(outputFileName); outputTypes["otu"].push_back(outputFileName);
305 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
307 //for each bin in the list vector
308 for (int i = 0; i < list->getNumBins(); i++) {
309 if (m->control_pressed) { break; }
311 binnames = list->get(i);
314 out << i+1 << '\t' << binnames << endl;
316 vector<string> names;
317 m->splitAtComma(binnames, names);
319 for (int j = 0; j < names.size(); j++) {
320 out << names[j] << '\t' << i+1 << endl;
327 catch(exception& e) {
328 m->errorOut(e, "GetListCountCommand", "process");
332 //**********************************************************************************************************************