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(){
27 //initialize outputTypes
28 vector<string> tempOutNames;
29 outputTypes["otu"] = tempOutNames;
32 m->errorOut(e, "GetListCountCommand", "GetListCountCommand");
36 //**********************************************************************************************************************
37 vector<string> GetListCountCommand::getRequiredParameters(){
39 string Array[] = {"list"};
40 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
44 m->errorOut(e, "GetListCountCommand", "getRequiredParameters");
48 //**********************************************************************************************************************
49 vector<string> GetListCountCommand::getRequiredFiles(){
51 vector<string> myArray;
55 m->errorOut(e, "GetListCountCommand", "getRequiredFiles");
59 //**********************************************************************************************************************
60 GetListCountCommand::GetListCountCommand(string option) {
62 globaldata = GlobalData::getInstance();
67 //allow user to run help
68 if(option == "help") { help(); abort = true; }
71 //valid paramters for this command
72 string AlignArray[] = {"list","label","sort","outputdir","inputdir"};
73 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
75 OptionParser parser(option);
76 map<string, string> parameters = parser.getParameters();
78 ValidParameters validParameter;
79 map<string, string>::iterator it;
81 //check to make sure all parameters are valid for command
82 for (it = parameters.begin(); it != parameters.end(); it++) {
83 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
86 //initialize outputTypes
87 vector<string> tempOutNames;
88 outputTypes["otu"] = tempOutNames;
90 string ranRead = globaldata->getListFile();
92 //if the user changes the input directory command factory will send this info to us in the output parameter
93 string inputDir = validParameter.validFile(parameters, "inputdir", false);
94 if (inputDir == "not found"){ inputDir = ""; }
97 it = parameters.find("list");
98 //user has given a template file
99 if(it != parameters.end()){
100 path = m->hasPath(it->second);
101 //if the user has not given a path then, add inputdir. else leave path alone.
102 if (path == "") { parameters["list"] = inputDir + it->second; }
106 //if the user changes the output directory command factory will send this info to us in the output parameter
107 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
109 //check for required parameters
110 listfile = validParameter.validFile(parameters, "list", true);
111 if ((listfile == "not found") && (globaldata->getListFile() == "")) { m->mothurOut("You must read a listfile before running the get.listcount command."); m->mothurOutEndLine(); abort = true; }
112 else if ((listfile == "not found") && (globaldata->getListFile() != "")) { listfile = globaldata->getListFile(); }
113 else if (listfile == "not open") { abort = true; }
114 else { globaldata->setListFile(listfile); }
116 //check for optional parameter and set defaults
117 // ...at some point should added some additional type checking...
118 sort = validParameter.validFile(parameters, "sort", false); if (sort == "not found") { sort = "otu"; }
119 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"; }
121 label = validParameter.validFile(parameters, "label", false);
122 if (label == "not found") { label = ""; }
124 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
125 else { allLines = 1; }
128 //if the user has not specified any labels use the ones from read.otu
129 if ((label == "") && (ranRead != "")) {
130 allLines = globaldata->allLines;
131 labels = globaldata->labels;
135 catch(exception& e) {
136 m->errorOut(e, "GetListCountCommand", "GetListCountCommand");
140 //**********************************************************************************************************************
142 void GetListCountCommand::help(){
144 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");
145 m->mothurOut("The get.otulist command parameters are list, sort and label. No parameters are required.\n");
146 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");
147 m->mothurOut("The sort parameter allows you to select how you want the output displayed. Options are otu and name.\n");
148 m->mothurOut("If otu is selected the output will be otu number followed by the list of names in that otu.\n");
149 m->mothurOut("If name is selected the output will be a sequence name followed by its otu number.\n");
150 m->mothurOut("The get.otulist command should be in the following format: get.otulist(list=yourlistFile, label=yourLabels).\n");
151 m->mothurOut("Example get.otulist(list=amazon.fn.list, label=0.10).\n");
152 m->mothurOut("The default value for label is all lines in your inputfile.\n");
153 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");
154 m->mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n\n");
156 catch(exception& e) {
157 m->errorOut(e, "GetListCountCommand", "help");
162 //**********************************************************************************************************************
164 GetListCountCommand::~GetListCountCommand(){}
166 //**********************************************************************************************************************
168 int GetListCountCommand::execute(){
170 if (abort == true) { return 0; }
172 globaldata->setFormat("list");
175 read = new ReadOTUFile(listfile);
176 read->read(&*globaldata);
178 input = globaldata->ginput;
179 list = globaldata->gListVector;
180 string lastLabel = list->getLabel();
182 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
183 set<string> processedLabels;
184 set<string> userLabels = labels;
186 if (m->control_pressed) {
190 globaldata->gListVector = NULL;
191 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
195 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
197 if(allLines == 1 || labels.count(list->getLabel()) == 1){
201 if (m->control_pressed) {
205 globaldata->gListVector = NULL;
206 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
210 processedLabels.insert(list->getLabel());
211 userLabels.erase(list->getLabel());
214 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
215 string saveLabel = list->getLabel();
218 list = input->getListVector(lastLabel);
222 if (m->control_pressed) {
226 globaldata->gListVector = NULL;
227 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
231 processedLabels.insert(list->getLabel());
232 userLabels.erase(list->getLabel());
234 //restore real lastlabel to save below
235 list->setLabel(saveLabel);
238 lastLabel = list->getLabel();
241 list = input->getListVector();
245 //output error messages about any remaining user labels
246 set<string>::iterator it;
247 bool needToRun = false;
248 for (it = userLabels.begin(); it != userLabels.end(); it++) {
249 m->mothurOut("Your file does not include the label " + *it);
250 if (processedLabels.count(lastLabel) != 1) {
251 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
254 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
258 //run last label if you need to
259 if (needToRun == true) {
260 if (list != NULL) { delete list; }
261 list = input->getListVector(lastLabel);
265 if (m->control_pressed) {
269 globaldata->gListVector = NULL;
270 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
279 globaldata->gListVector = NULL;
281 m->mothurOutEndLine();
282 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
283 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
284 m->mothurOutEndLine();
288 catch(exception& e) {
289 m->errorOut(e, "GetListCountCommand", "execute");
294 //**********************************************************************************************************************
295 //return 1 if error, 0 otherwise
296 void GetListCountCommand::process(ListVector* list) {
299 if (outputDir == "") { outputDir += m->hasPath(listfile); }
300 string outputFileName = outputDir + m->getRootName(m->getSimpleName(listfile)) + list->getLabel() + ".otu";
301 m->openOutputFile(outputFileName, out);
302 outputNames.push_back(outputFileName); outputTypes["otu"].push_back(outputFileName);
304 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
306 //for each bin in the list vector
307 for (int i = 0; i < list->getNumBins(); i++) {
308 if (m->control_pressed) { break; }
310 binnames = list->get(i);
313 out << i+1 << '\t' << binnames << endl;
315 vector<string> names;
316 m->splitAtComma(binnames, names);
318 for (int j = 0; j < names.size(); j++) {
319 out << names[j] << '\t' << i+1 << endl;
326 catch(exception& e) {
327 m->errorOut(e, "GetListCountCommand", "process");
331 //**********************************************************************************************************************