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