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 GetListCountCommand::GetListCountCommand(string option) {
15 globaldata = GlobalData::getInstance();
20 //allow user to run help
21 if(option == "help") { help(); abort = true; }
24 //valid paramters for this command
25 string AlignArray[] = {"list","label","outputdir","inputdir"};
26 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
28 OptionParser parser(option);
29 map<string, string> parameters = parser.getParameters();
31 ValidParameters validParameter;
32 map<string, string>::iterator it;
34 //check to make sure all parameters are valid for command
35 for (it = parameters.begin(); it != parameters.end(); it++) {
36 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
39 string ranRead = globaldata->getListFile();
41 //if the user changes the input directory command factory will send this info to us in the output parameter
42 string inputDir = validParameter.validFile(parameters, "inputdir", false);
43 if (inputDir == "not found"){ inputDir = ""; }
46 it = parameters.find("list");
47 //user has given a template file
48 if(it != parameters.end()){
49 path = hasPath(it->second);
50 //if the user has not given a path then, add inputdir. else leave path alone.
51 if (path == "") { parameters["list"] = inputDir + it->second; }
55 //if the user changes the output directory command factory will send this info to us in the output parameter
56 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
58 //check for required parameters
59 listfile = validParameter.validFile(parameters, "list", true);
60 if ((listfile == "not found") && (globaldata->getListFile() == "")) { m->mothurOut("You must read a listfile before running the get.listcount command."); m->mothurOutEndLine(); abort = true; }
61 else if ((listfile == "not found") && (globaldata->getListFile() != "")) { listfile = globaldata->getListFile(); }
62 else if (listfile == "not open") { abort = true; }
63 else { globaldata->setListFile(listfile); }
65 //check for optional parameter and set defaults
66 // ...at some point should added some additional type checking...
68 label = validParameter.validFile(parameters, "label", false);
69 if (label == "not found") { label = ""; }
71 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
72 else { allLines = 1; }
75 //if the user has not specified any labels use the ones from read.otu
76 if ((label == "") && (ranRead != "")) {
77 allLines = globaldata->allLines;
78 labels = globaldata->labels;
83 m->errorOut(e, "GetListCountCommand", "GetListCountCommand");
87 //**********************************************************************************************************************
89 void GetListCountCommand::help(){
91 m->mothurOut("The get.listcount command can only be executed after a successful read.otu command of a listfile or providing a list file using the list parameter.\n");
92 m->mothurOut("The get.listcount command parameters are list and label. No parameters are required.\n");
93 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");
94 m->mothurOut("The get.listcount command should be in the following format: get.listcount(list=yourlistFile, label=yourLabels).\n");
95 m->mothurOut("Example get.listcount(list=amazon.fn.list, label=0.10).\n");
96 m->mothurOut("The default value for label is all lines in your inputfile.\n");
97 m->mothurOut("The get.listcount command outputs a .otu file for each distance you specify listing the bin number and the names of the sequences in that bin.\n");
98 m->mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n\n");
100 catch(exception& e) {
101 m->errorOut(e, "GetListCountCommand", "help");
106 //**********************************************************************************************************************
108 GetListCountCommand::~GetListCountCommand(){}
110 //**********************************************************************************************************************
112 int GetListCountCommand::execute(){
114 if (abort == true) { return 0; }
116 globaldata->setFormat("list");
119 read = new ReadOTUFile(listfile);
120 read->read(&*globaldata);
122 input = globaldata->ginput;
123 list = globaldata->gListVector;
124 string lastLabel = list->getLabel();
126 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
127 set<string> processedLabels;
128 set<string> userLabels = labels;
130 if (m->control_pressed) {
134 globaldata->gListVector = NULL;
135 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
139 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
141 if(allLines == 1 || labels.count(list->getLabel()) == 1){
145 if (m->control_pressed) {
149 globaldata->gListVector = NULL;
150 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
154 processedLabels.insert(list->getLabel());
155 userLabels.erase(list->getLabel());
158 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
159 string saveLabel = list->getLabel();
162 list = input->getListVector(lastLabel);
166 if (m->control_pressed) {
170 globaldata->gListVector = NULL;
171 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
175 processedLabels.insert(list->getLabel());
176 userLabels.erase(list->getLabel());
178 //restore real lastlabel to save below
179 list->setLabel(saveLabel);
182 lastLabel = list->getLabel();
185 list = input->getListVector();
189 //output error messages about any remaining user labels
190 set<string>::iterator it;
191 bool needToRun = false;
192 for (it = userLabels.begin(); it != userLabels.end(); it++) {
193 m->mothurOut("Your file does not include the label " + *it);
194 if (processedLabels.count(lastLabel) != 1) {
195 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
198 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
202 //run last label if you need to
203 if (needToRun == true) {
204 if (list != NULL) { delete list; }
205 list = input->getListVector(lastLabel);
209 if (m->control_pressed) {
213 globaldata->gListVector = NULL;
214 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
223 globaldata->gListVector = NULL;
225 m->mothurOutEndLine();
226 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
227 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
228 m->mothurOutEndLine();
232 catch(exception& e) {
233 m->errorOut(e, "GetListCountCommand", "execute");
238 //**********************************************************************************************************************
239 //return 1 if error, 0 otherwise
240 void GetListCountCommand::process(ListVector* list) {
242 string binnames, name, sequence;
243 if (outputDir == "") { outputDir += hasPath(listfile); }
244 string outputFileName = outputDir + getRootName(getSimpleName(listfile)) + list->getLabel() + ".otu";
245 openOutputFile(outputFileName, out);
246 outputNames.push_back(outputFileName);
248 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
250 //for each bin in the list vector
251 for (int i = 0; i < list->getNumBins(); i++) {
252 if (m->control_pressed) { break; }
254 binnames = list->get(i);
255 out << i+1 << '\t' << binnames << endl;
260 catch(exception& e) {
261 m->errorOut(e, "GetListCountCommand", "process");
265 //**********************************************************************************************************************