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","sort","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...
67 sort = validParameter.validFile(parameters, "sort", false); if (sort == "not found") { sort = "otu"; }
68 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"; }
70 label = validParameter.validFile(parameters, "label", false);
71 if (label == "not found") { label = ""; }
73 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
74 else { allLines = 1; }
77 //if the user has not specified any labels use the ones from read.otu
78 if ((label == "") && (ranRead != "")) {
79 allLines = globaldata->allLines;
80 labels = globaldata->labels;
85 m->errorOut(e, "GetListCountCommand", "GetListCountCommand");
89 //**********************************************************************************************************************
91 void GetListCountCommand::help(){
93 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");
94 m->mothurOut("The get.otulist command parameters are list, sort and label. No parameters are required.\n");
95 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");
96 m->mothurOut("The sort parameter allows you to select how you want the output displayed. Options are otu and name.\n");
97 m->mothurOut("If otu is selected the output will be otu number followed by the list of names in that otu.\n");
98 m->mothurOut("If name is selected the output will be a sequence name followed by its otu number.\n");
99 m->mothurOut("The get.otulist command should be in the following format: get.otulist(list=yourlistFile, label=yourLabels).\n");
100 m->mothurOut("Example get.otulist(list=amazon.fn.list, label=0.10).\n");
101 m->mothurOut("The default value for label is all lines in your inputfile.\n");
102 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");
103 m->mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n\n");
105 catch(exception& e) {
106 m->errorOut(e, "GetListCountCommand", "help");
111 //**********************************************************************************************************************
113 GetListCountCommand::~GetListCountCommand(){}
115 //**********************************************************************************************************************
117 int GetListCountCommand::execute(){
119 if (abort == true) { return 0; }
121 globaldata->setFormat("list");
124 read = new ReadOTUFile(listfile);
125 read->read(&*globaldata);
127 input = globaldata->ginput;
128 list = globaldata->gListVector;
129 string lastLabel = list->getLabel();
131 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
132 set<string> processedLabels;
133 set<string> userLabels = labels;
135 if (m->control_pressed) {
139 globaldata->gListVector = NULL;
140 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
144 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
146 if(allLines == 1 || labels.count(list->getLabel()) == 1){
150 if (m->control_pressed) {
154 globaldata->gListVector = NULL;
155 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
159 processedLabels.insert(list->getLabel());
160 userLabels.erase(list->getLabel());
163 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
164 string saveLabel = list->getLabel();
167 list = input->getListVector(lastLabel);
171 if (m->control_pressed) {
175 globaldata->gListVector = NULL;
176 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
180 processedLabels.insert(list->getLabel());
181 userLabels.erase(list->getLabel());
183 //restore real lastlabel to save below
184 list->setLabel(saveLabel);
187 lastLabel = list->getLabel();
190 list = input->getListVector();
194 //output error messages about any remaining user labels
195 set<string>::iterator it;
196 bool needToRun = false;
197 for (it = userLabels.begin(); it != userLabels.end(); it++) {
198 m->mothurOut("Your file does not include the label " + *it);
199 if (processedLabels.count(lastLabel) != 1) {
200 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
203 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
207 //run last label if you need to
208 if (needToRun == true) {
209 if (list != NULL) { delete list; }
210 list = input->getListVector(lastLabel);
214 if (m->control_pressed) {
218 globaldata->gListVector = NULL;
219 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
228 globaldata->gListVector = NULL;
230 m->mothurOutEndLine();
231 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
232 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
233 m->mothurOutEndLine();
237 catch(exception& e) {
238 m->errorOut(e, "GetListCountCommand", "execute");
243 //**********************************************************************************************************************
244 //return 1 if error, 0 otherwise
245 void GetListCountCommand::process(ListVector* list) {
248 if (outputDir == "") { outputDir += hasPath(listfile); }
249 string outputFileName = outputDir + getRootName(getSimpleName(listfile)) + list->getLabel() + ".otu";
250 openOutputFile(outputFileName, out);
251 outputNames.push_back(outputFileName);
253 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
255 //for each bin in the list vector
256 for (int i = 0; i < list->getNumBins(); i++) {
257 if (m->control_pressed) { break; }
259 binnames = list->get(i);
262 out << i+1 << '\t' << binnames << endl;
264 vector<string> names;
265 splitAtComma(binnames, names);
267 for (int j = 0; j < names.size(); j++) {
268 out << names[j] << '\t' << i+1 << endl;
275 catch(exception& e) {
276 m->errorOut(e, "GetListCountCommand", "process");
280 //**********************************************************************************************************************