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