]> git.donarmstrong.com Git - mothur.git/blob - getlistcountcommand.cpp
Revert to previous commit
[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                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
73                 
74                 else {
75                         vector<string> myArray = setParameters();
76                         
77                         OptionParser parser(option);
78                         map<string, string> parameters = parser.getParameters();
79                         
80                         ValidParameters validParameter;
81                         map<string, string>::iterator it;
82                         
83                         //check to make sure all parameters are valid for command
84                         for (it = parameters.begin(); it != parameters.end(); it++) { 
85                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
86                         }
87                         
88                         //initialize outputTypes
89                         vector<string> tempOutNames;
90                         outputTypes["otu"] = tempOutNames;
91                         
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 = "";          }
95                         else {
96                                 string path;
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;             }
103                                 }
104                         }
105
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 = "";         }
108                         
109                         //check for required parameters
110                         listfile = validParameter.validFile(parameters, "list", true);
111                         if (listfile == "not found")  {                                 
112                                 listfile = m->getListFile(); 
113                                 if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
114                                 else {  m->mothurOut("You have no current list file and the list parameter is required."); m->mothurOutEndLine(); abort = true; }
115                         }
116                         else if (listfile == "not open") { abort = true; }      
117                         else { m->setListFile(listfile); }
118                         
119                 
120                         //check for optional parameter and set defaults
121                         // ...at some point should added some additional type checking...
122                         sort = validParameter.validFile(parameters, "sort", false);       if (sort == "not found") { sort = "otu"; }
123                         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"; }
124                         
125                         label = validParameter.validFile(parameters, "label", false);                   
126                         if (label == "not found") { label = ""; }
127                         else { 
128                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
129                                 else { allLines = 1;  }
130                         }
131                 }
132         }
133         catch(exception& e) {
134                 m->errorOut(e, "GetListCountCommand", "GetListCountCommand");
135                 exit(1);
136         }
137 }
138 //**********************************************************************************************************************
139
140 int GetListCountCommand::execute(){
141         try {
142                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
143                 
144                 input = new InputData(listfile, "list");
145                 list = input->getListVector();
146                 string lastLabel = list->getLabel();
147
148                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
149                 set<string> processedLabels;
150                 set<string> userLabels = labels;
151                 
152                 if (m->control_pressed) { delete input; delete list; for (int i = 0; i < outputNames.size(); i++) {     m->mothurRemove(outputNames[i]);        } return 0;  }
153                 
154                 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
155                         
156                         if(allLines == 1 || labels.count(list->getLabel()) == 1){
157                         
158                                 process(list);
159                                 
160                                 if (m->control_pressed) { delete input; delete list; for (int i = 0; i < outputNames.size(); i++) {     m->mothurRemove(outputNames[i]);        } return 0;  }
161                                                         
162                                 processedLabels.insert(list->getLabel());
163                                 userLabels.erase(list->getLabel());
164                         }
165                         
166                         if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
167                                 string saveLabel = list->getLabel();
168                                 
169                                 delete list;
170                                 list = input->getListVector(lastLabel);
171                                 
172                                 process(list);
173                                 
174                                 if (m->control_pressed) { delete input; delete list; for (int i = 0; i < outputNames.size(); i++) {     m->mothurRemove(outputNames[i]);        } return 0;  }
175
176                                                                                                         
177                                 processedLabels.insert(list->getLabel());
178                                 userLabels.erase(list->getLabel());
179                                 
180                                 //restore real lastlabel to save below
181                                 list->setLabel(saveLabel);
182                         }
183                         
184                         lastLabel = list->getLabel();                   
185                         
186                         delete list;
187                         list = input->getListVector();
188                 }
189                 
190                 
191                 //output error messages about any remaining user labels
192                 set<string>::iterator it;
193                 bool needToRun = false;
194                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
195                         m->mothurOut("Your file does not include the label " + *it); 
196                         if (processedLabels.count(lastLabel) != 1) {
197                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
198                                 needToRun = true;
199                         }else {
200                                 m->mothurOut(". Please refer to " + lastLabel + ".");  m->mothurOutEndLine();
201                         }
202                 }
203                 
204                 //run last label if you need to
205                 if (needToRun == true)  {
206                         if (list != NULL) {             delete list;    }
207                         list = input->getListVector(lastLabel);
208                                 
209                         process(list);  
210                         
211                         if (m->control_pressed) { delete input; delete list; for (int i = 0; i < outputNames.size(); i++) {     m->mothurRemove(outputNames[i]);        } return 0;  }
212                         
213                         delete list;  
214                 }
215                 
216                 delete input;
217                 
218                 m->mothurOutEndLine();
219                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
220                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
221                 m->mothurOutEndLine();
222                 
223                 return 0;
224         }
225         catch(exception& e) {
226                 m->errorOut(e, "GetListCountCommand", "execute");
227                 exit(1);
228         }
229 }
230
231 //**********************************************************************************************************************
232 //return 1 if error, 0 otherwise
233 void GetListCountCommand::process(ListVector* list) {
234         try {
235                 string binnames;
236                 if (outputDir == "") { outputDir += m->hasPath(listfile); }
237                 string outputFileName = outputDir + m->getRootName(m->getSimpleName(listfile)) + list->getLabel() + ".otu";
238                 m->openOutputFile(outputFileName, out);
239                 outputNames.push_back(outputFileName); outputTypes["otu"].push_back(outputFileName);
240                 
241                 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
242                 
243                 //for each bin in the list vector
244                 for (int i = 0; i < list->getNumBins(); i++) {
245                         if (m->control_pressed) { break; }
246                         
247                         binnames = list->get(i);
248                         
249                         if (sort == "otu") {
250                                 out << i+1 << '\t' << binnames << endl;
251                         }else{ //sort = name
252                                 vector<string> names;
253                                 m->splitAtComma(binnames, names);
254                                 
255                                 for (int j = 0; j < names.size(); j++) {
256                                         out << names[j] << '\t' << i+1 << endl;
257                                 }
258                         }
259                 }
260                 
261                 out.close();
262         }
263         catch(exception& e) {
264                 m->errorOut(e, "GetListCountCommand", "process");
265                 exit(1);
266         }
267 }
268 //**********************************************************************************************************************
269
270