]> git.donarmstrong.com Git - mothur.git/blob - getlistcountcommand.cpp
added [ERROR] flag if command aborts
[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::getValidParameters(){       
14         try {
15                 string Array[] =  {"list","label","sort","outputdir","inputdir"};
16                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
17                 return myArray;
18         }
19         catch(exception& e) {
20                 m->errorOut(e, "GetListCountCommand", "getValidParameters");
21                 exit(1);
22         }
23 }
24 //**********************************************************************************************************************
25 GetListCountCommand::GetListCountCommand(){     
26         try {
27                 abort = true; calledHelp = true; 
28                 vector<string> tempOutNames;
29                 outputTypes["otu"] = tempOutNames;
30         }
31         catch(exception& e) {
32                 m->errorOut(e, "GetListCountCommand", "GetListCountCommand");
33                 exit(1);
34         }
35 }
36 //**********************************************************************************************************************
37 vector<string> GetListCountCommand::getRequiredParameters(){    
38         try {
39                 string Array[] =  {"list"};
40                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
41                 return myArray;
42         }
43         catch(exception& e) {
44                 m->errorOut(e, "GetListCountCommand", "getRequiredParameters");
45                 exit(1);
46         }
47 }
48 //**********************************************************************************************************************
49 vector<string> GetListCountCommand::getRequiredFiles(){ 
50         try {
51                 vector<string> myArray;
52                 return myArray;
53         }
54         catch(exception& e) {
55                 m->errorOut(e, "GetListCountCommand", "getRequiredFiles");
56                 exit(1);
57         }
58 }
59 //**********************************************************************************************************************
60 GetListCountCommand::GetListCountCommand(string option)  {
61         try {
62                 globaldata = GlobalData::getInstance();
63                 abort = false; calledHelp = false;   
64                 allLines = 1;
65                 labels.clear();
66                                 
67                 //allow user to run help
68                 if(option == "help") { help(); abort = true; calledHelp = true; }
69                 
70                 else {
71                         //valid paramters for this command
72                         string AlignArray[] =  {"list","label","sort","outputdir","inputdir"};
73                         vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
74                         
75                         OptionParser parser(option);
76                         map<string, string> parameters = parser.getParameters();
77                         
78                         ValidParameters validParameter;
79                         map<string, string>::iterator it;
80                         
81                         //check to make sure all parameters are valid for command
82                         for (it = parameters.begin(); it != parameters.end(); it++) { 
83                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
84                         }
85                         
86                         //initialize outputTypes
87                         vector<string> tempOutNames;
88                         outputTypes["otu"] = tempOutNames;
89                 
90                         string ranRead = globaldata->getListFile();
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") && (globaldata->getListFile() == ""))  { m->mothurOut("You must read a listfile before running the get.listcount command.");  m->mothurOutEndLine(); abort = true; }
112                         else if ((listfile == "not found") && (globaldata->getListFile() != "")) { listfile = globaldata->getListFile(); }
113                         else if (listfile == "not open") { abort = true; }      
114                         else { globaldata->setListFile(listfile); }
115                 
116                         //check for optional parameter and set defaults
117                         // ...at some point should added some additional type checking...
118                         sort = validParameter.validFile(parameters, "sort", false);       if (sort == "not found") { sort = "otu"; }
119                         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"; }
120                         
121                         label = validParameter.validFile(parameters, "label", false);                   
122                         if (label == "not found") { label = ""; }
123                         else { 
124                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
125                                 else { allLines = 1;  }
126                         }
127                         
128                         //if the user has not specified any labels use the ones from read.otu
129                         if ((label == "") && (ranRead != "")) {  
130                                 allLines = globaldata->allLines; 
131                                 labels = globaldata->labels; 
132                         }
133                 }
134         }
135         catch(exception& e) {
136                 m->errorOut(e, "GetListCountCommand", "GetListCountCommand");
137                 exit(1);
138         }
139 }
140 //**********************************************************************************************************************
141
142 void GetListCountCommand::help(){
143         try {
144                 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");
145                 m->mothurOut("The get.otulist command parameters are list, sort and label.  No parameters are required.\n");
146                 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");
147                 m->mothurOut("The sort parameter allows you to select how you want the output displayed. Options are otu and name.\n");
148                 m->mothurOut("If otu is selected the output will be otu number followed by the list of names in that otu.\n");
149                 m->mothurOut("If name is selected the output will be a sequence name followed by its otu number.\n");
150                 m->mothurOut("The get.otulist command should be in the following format: get.otulist(list=yourlistFile, label=yourLabels).\n");
151                 m->mothurOut("Example get.otulist(list=amazon.fn.list, label=0.10).\n");
152                 m->mothurOut("The default value for label is all lines in your inputfile.\n");
153                 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");
154                 m->mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n\n");
155         }
156         catch(exception& e) {
157                 m->errorOut(e, "GetListCountCommand", "help");
158                 exit(1);
159         }
160 }
161
162 //**********************************************************************************************************************
163
164 GetListCountCommand::~GetListCountCommand(){}
165
166 //**********************************************************************************************************************
167
168 int GetListCountCommand::execute(){
169         try {
170                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
171
172                 globaldata->setFormat("list");
173                 
174                 //read list file
175                 read = new ReadOTUFile(listfile);       
176                 read->read(&*globaldata); 
177                 
178                 input = globaldata->ginput;
179                 list = globaldata->gListVector;
180                 string lastLabel = list->getLabel();
181
182                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
183                 set<string> processedLabels;
184                 set<string> userLabels = labels;
185                 
186                 if (m->control_pressed) { 
187                         delete read;
188                         delete input;
189                         delete list;
190                         globaldata->gListVector = NULL;  
191                         for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str()); }
192                         return 0; 
193                 }
194                 
195                 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
196                         
197                         if(allLines == 1 || labels.count(list->getLabel()) == 1){
198                         
199                                 process(list);
200                                 
201                                 if (m->control_pressed) { 
202                                         delete read;
203                                         delete input;
204                                         delete list;
205                                         globaldata->gListVector = NULL;  
206                                         for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str()); } outputTypes.clear();
207                                         return 0; 
208                                 }
209                                                         
210                                 processedLabels.insert(list->getLabel());
211                                 userLabels.erase(list->getLabel());
212                         }
213                         
214                         if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
215                                 string saveLabel = list->getLabel();
216                                 
217                                 delete list;
218                                 list = input->getListVector(lastLabel);
219                                 
220                                 process(list);
221                                 
222                                 if (m->control_pressed) { 
223                                         delete read;
224                                         delete input;
225                                         delete list;
226                                         globaldata->gListVector = NULL;  
227                                         for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str()); } outputTypes.clear();
228                                         return 0; 
229                                 }
230                                                                                                         
231                                 processedLabels.insert(list->getLabel());
232                                 userLabels.erase(list->getLabel());
233                                 
234                                 //restore real lastlabel to save below
235                                 list->setLabel(saveLabel);
236                         }
237                         
238                         lastLabel = list->getLabel();                   
239                         
240                         delete list;
241                         list = input->getListVector();
242                 }
243                 
244                 
245                 //output error messages about any remaining user labels
246                 set<string>::iterator it;
247                 bool needToRun = false;
248                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
249                         m->mothurOut("Your file does not include the label " + *it); 
250                         if (processedLabels.count(lastLabel) != 1) {
251                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
252                                 needToRun = true;
253                         }else {
254                                 m->mothurOut(". Please refer to " + lastLabel + ".");  m->mothurOutEndLine();
255                         }
256                 }
257                 
258                 //run last label if you need to
259                 if (needToRun == true)  {
260                         if (list != NULL) {             delete list;    }
261                         list = input->getListVector(lastLabel);
262                                 
263                         process(list);  
264                         
265                         if (m->control_pressed) { 
266                                         delete read;
267                                         delete input;
268                                         delete list;
269                                         globaldata->gListVector = NULL;  
270                                         for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str()); } outputTypes.clear();
271                                         return 0; 
272                         }
273                         
274                         delete list;  
275                 }
276                 
277                 delete read;
278                 delete input;
279                 globaldata->gListVector = NULL;
280                 
281                 m->mothurOutEndLine();
282                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
283                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
284                 m->mothurOutEndLine();
285                 
286                 return 0;
287         }
288         catch(exception& e) {
289                 m->errorOut(e, "GetListCountCommand", "execute");
290                 exit(1);
291         }
292 }
293
294 //**********************************************************************************************************************
295 //return 1 if error, 0 otherwise
296 void GetListCountCommand::process(ListVector* list) {
297         try {
298                 string binnames;
299                 if (outputDir == "") { outputDir += m->hasPath(listfile); }
300                 string outputFileName = outputDir + m->getRootName(m->getSimpleName(listfile)) + list->getLabel() + ".otu";
301                 m->openOutputFile(outputFileName, out);
302                 outputNames.push_back(outputFileName); outputTypes["otu"].push_back(outputFileName);
303                 
304                 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
305                 
306                 //for each bin in the list vector
307                 for (int i = 0; i < list->getNumBins(); i++) {
308                         if (m->control_pressed) { break; }
309                         
310                         binnames = list->get(i);
311                         
312                         if (sort == "otu") {
313                                 out << i+1 << '\t' << binnames << endl;
314                         }else{ //sort = name
315                                 vector<string> names;
316                                 m->splitAtComma(binnames, names);
317                                 
318                                 for (int j = 0; j < names.size(); j++) {
319                                         out << names[j] << '\t' << i+1 << endl;
320                                 }
321                         }
322                 }
323                 
324                 out.close();
325         }
326         catch(exception& e) {
327                 m->errorOut(e, "GetListCountCommand", "process");
328                 exit(1);
329         }
330 }
331 //**********************************************************************************************************************
332
333