]> git.donarmstrong.com Git - mothur.git/blob - getlistcountcommand.cpp
added getCommandInfoCommand for gui
[mothur.git] / getlistcountcommand.cpp
1 /*
2  *  getlistcountcommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 10/12/09.
6  *  Copyright 2009 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "getlistcountcommand.h"
11
12 //**********************************************************************************************************************
13 vector<string> GetListCountCommand::setParameters(){    
14         try {
15                 CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(plist);
16                 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
17                 CommandParameter parasort("sort", "Multiple", "name-otu", "otu", "", "", "",false,false); parameters.push_back(parasort);
18                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
19                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
20                 
21                 vector<string> myArray;
22                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
23                 return myArray;
24         }
25         catch(exception& e) {
26                 m->errorOut(e, "GetListCountCommand", "setParameters");
27                 exit(1);
28         }
29 }
30 //**********************************************************************************************************************
31 string GetListCountCommand::getHelpString(){    
32         try {
33                 string helpString = "";
34                 helpString += "The get.otulist command parameters are list, sort and label.  list is required, unless you have a valid current list file.\n";
35                 helpString += "The label parameter allows you to select what distance levels you would like a output files created for, and are separated by dashes.\n";
36                 helpString += "The sort parameter allows you to select how you want the output displayed. Options are otu and name.\n";
37                 helpString += "If otu is selected the output will be otu number followed by the list of names in that otu.\n";
38                 helpString += "If name is selected the output will be a sequence name followed by its otu number.\n";
39                 helpString += "The get.otulist command should be in the following format: get.otulist(list=yourlistFile, label=yourLabels).\n";
40                 helpString += "Example get.otulist(list=amazon.fn.list, label=0.10).\n";
41                 helpString += "The default value for label is all lines in your inputfile.\n";
42                 helpString += "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";
43                 helpString += "Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n";
44                 return helpString;
45         }
46         catch(exception& e) {
47                 m->errorOut(e, "GetListCountCommand", "getHelpString");
48                 exit(1);
49         }
50 }
51 //**********************************************************************************************************************
52 GetListCountCommand::GetListCountCommand(){     
53         try {
54                 abort = true; calledHelp = true; 
55                 setParameters();
56                 vector<string> tempOutNames;
57                 outputTypes["otu"] = tempOutNames;
58         }
59         catch(exception& e) {
60                 m->errorOut(e, "GetListCountCommand", "GetListCountCommand");
61                 exit(1);
62         }
63 }
64 //**********************************************************************************************************************
65 GetListCountCommand::GetListCountCommand(string option)  {
66         try {
67                 abort = false; calledHelp = false;   
68                 allLines = 1;
69                                 
70                 //allow user to run help
71                 if(option == "help") { help(); abort = true; calledHelp = true; }
72                 
73                 else {
74                         vector<string> myArray = setParameters();
75                         
76                         OptionParser parser(option);
77                         map<string, string> parameters = parser.getParameters();
78                         
79                         ValidParameters validParameter;
80                         map<string, string>::iterator it;
81                         
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;  }
85                         }
86                         
87                         //initialize outputTypes
88                         vector<string> tempOutNames;
89                         outputTypes["otu"] = tempOutNames;
90                         
91                         //if the user changes the input directory command factory will send this info to us in the output parameter 
92                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
93                         if (inputDir == "not found"){   inputDir = "";          }
94                         else {
95                                 string path;
96                                 it = parameters.find("list");
97                                 //user has given a template file
98                                 if(it != parameters.end()){ 
99                                         path = m->hasPath(it->second);
100                                         //if the user has not given a path then, add inputdir. else leave path alone.
101                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
102                                 }
103                         }
104
105                         //if the user changes the output directory command factory will send this info to us in the output parameter 
106                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
107                         
108                         //check for required parameters
109                         listfile = validParameter.validFile(parameters, "list", true);
110                         if (listfile == "not found")  {                                 
111                                 listfile = m->getListFile(); 
112                                 if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
113                                 else {  m->mothurOut("You have no current list file and the list parameter is required."); m->mothurOutEndLine(); abort = true; }
114                         }
115                         else if (listfile == "not open") { abort = true; }      
116                         
117                 
118                         //check for optional parameter and set defaults
119                         // ...at some point should added some additional type checking...
120                         sort = validParameter.validFile(parameters, "sort", false);       if (sort == "not found") { sort = "otu"; }
121                         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                         
123                         label = validParameter.validFile(parameters, "label", false);                   
124                         if (label == "not found") { label = ""; }
125                         else { 
126                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
127                                 else { allLines = 1;  }
128                         }
129                 }
130         }
131         catch(exception& e) {
132                 m->errorOut(e, "GetListCountCommand", "GetListCountCommand");
133                 exit(1);
134         }
135 }
136 //**********************************************************************************************************************
137
138 int GetListCountCommand::execute(){
139         try {
140                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
141                 
142                 input = new InputData(listfile, "list");
143                 list = input->getListVector();
144                 string lastLabel = list->getLabel();
145
146                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
147                 set<string> processedLabels;
148                 set<string> userLabels = labels;
149                 
150                 if (m->control_pressed) { delete input; delete list; for (int i = 0; i < outputNames.size(); i++) {     remove(outputNames[i].c_str()); } return 0;  }
151                 
152                 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
153                         
154                         if(allLines == 1 || labels.count(list->getLabel()) == 1){
155                         
156                                 process(list);
157                                 
158                                 if (m->control_pressed) { delete input; delete list; for (int i = 0; i < outputNames.size(); i++) {     remove(outputNames[i].c_str()); } return 0;  }
159                                                         
160                                 processedLabels.insert(list->getLabel());
161                                 userLabels.erase(list->getLabel());
162                         }
163                         
164                         if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
165                                 string saveLabel = list->getLabel();
166                                 
167                                 delete list;
168                                 list = input->getListVector(lastLabel);
169                                 
170                                 process(list);
171                                 
172                                 if (m->control_pressed) { delete input; delete list; for (int i = 0; i < outputNames.size(); i++) {     remove(outputNames[i].c_str()); } return 0;  }
173
174                                                                                                         
175                                 processedLabels.insert(list->getLabel());
176                                 userLabels.erase(list->getLabel());
177                                 
178                                 //restore real lastlabel to save below
179                                 list->setLabel(saveLabel);
180                         }
181                         
182                         lastLabel = list->getLabel();                   
183                         
184                         delete list;
185                         list = input->getListVector();
186                 }
187                 
188                 
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();
196                                 needToRun = true;
197                         }else {
198                                 m->mothurOut(". Please refer to " + lastLabel + ".");  m->mothurOutEndLine();
199                         }
200                 }
201                 
202                 //run last label if you need to
203                 if (needToRun == true)  {
204                         if (list != NULL) {             delete list;    }
205                         list = input->getListVector(lastLabel);
206                                 
207                         process(list);  
208                         
209                         if (m->control_pressed) { delete input; delete list; for (int i = 0; i < outputNames.size(); i++) {     remove(outputNames[i].c_str()); } return 0;  }
210                         
211                         delete list;  
212                 }
213                 
214                 delete input;
215                 
216                 m->mothurOutEndLine();
217                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
218                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
219                 m->mothurOutEndLine();
220                 
221                 return 0;
222         }
223         catch(exception& e) {
224                 m->errorOut(e, "GetListCountCommand", "execute");
225                 exit(1);
226         }
227 }
228
229 //**********************************************************************************************************************
230 //return 1 if error, 0 otherwise
231 void GetListCountCommand::process(ListVector* list) {
232         try {
233                 string binnames;
234                 if (outputDir == "") { outputDir += m->hasPath(listfile); }
235                 string outputFileName = outputDir + m->getRootName(m->getSimpleName(listfile)) + list->getLabel() + ".otu";
236                 m->openOutputFile(outputFileName, out);
237                 outputNames.push_back(outputFileName); outputTypes["otu"].push_back(outputFileName);
238                 
239                 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
240                 
241                 //for each bin in the list vector
242                 for (int i = 0; i < list->getNumBins(); i++) {
243                         if (m->control_pressed) { break; }
244                         
245                         binnames = list->get(i);
246                         
247                         if (sort == "otu") {
248                                 out << i+1 << '\t' << binnames << endl;
249                         }else{ //sort = name
250                                 vector<string> names;
251                                 m->splitAtComma(binnames, names);
252                                 
253                                 for (int j = 0; j < names.size(); j++) {
254                                         out << names[j] << '\t' << i+1 << endl;
255                                 }
256                         }
257                 }
258                 
259                 out.close();
260         }
261         catch(exception& e) {
262                 m->errorOut(e, "GetListCountCommand", "process");
263                 exit(1);
264         }
265 }
266 //**********************************************************************************************************************
267
268