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