]> git.donarmstrong.com Git - mothur.git/blob - getsabundcommand.cpp
removed read.dist, read.otu, read.tree and globaldata. added current to defaults...
[mothur.git] / getsabundcommand.cpp
1 /*
2  *  getsabundcommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 6/2/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "getsabundcommand.h"
11
12 //**********************************************************************************************************************
13 vector<string> GetSAbundCommand::setParameters(){       
14         try {
15                 CommandParameter plist("list", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(plist);
16                 CommandParameter prabund("rabund", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(prabund);            
17                 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
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, "GetSAbundCommand", "setParameters");
27                 exit(1);
28         }
29 }
30 //**********************************************************************************************************************
31 string GetSAbundCommand::getHelpString(){       
32         try {
33                 string helpString = "";
34                 helpString += "The get.sabund command parameters is list, rabund and label.  list or rabund is required unless a valid current file exists.\n";
35                 helpString += "The label parameter allows you to select what distance levels you would like included in your .sabund file, and are separated by dashes.\n";
36                 helpString += "The get.sabund command should be in the following format: get.sabund(label=yourLabels).\n";
37                 helpString += "Example get.sabund().\n";
38                 helpString += "The default value for label is all labels in your inputfile.\n";
39                 helpString += "The get.sabund command outputs a .sabund file containing the labels you selected.\n";
40                 helpString += "Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabel).\n\n";
41                 return helpString;
42         }
43         catch(exception& e) {
44                 m->errorOut(e, "GetSAbundCommand", "getHelpString");
45                 exit(1);
46         }
47 }
48
49 //**********************************************************************************************************************
50 GetSAbundCommand::GetSAbundCommand(){   
51         try {
52                 abort = true; calledHelp = true; 
53                 setParameters();
54                 vector<string> tempOutNames;
55                 outputTypes["sabund"] = tempOutNames;
56         }
57         catch(exception& e) {
58                 m->errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
59                 exit(1);
60         }
61 }
62 //**********************************************************************************************************************
63 GetSAbundCommand::GetSAbundCommand(string option)  {
64         try {
65                 abort = false; calledHelp = false;   
66                 allLines = 1;
67                 
68                 //allow user to run help
69                 if(option == "help") { help(); abort = true; calledHelp = true; }
70                 
71                 else {
72                         vector<string> myArray = setParameters();
73                         
74                         OptionParser parser(option);
75                         map<string,string> parameters = parser.getParameters();
76                         map<string,string>::iterator it;
77                         
78                         ValidParameters validParameter;
79                         
80                         //check to make sure all parameters are valid for command
81                         for (it = parameters.begin(); it != parameters.end(); it++) { 
82                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
83                         }
84                         
85                         //initialize outputTypes
86                         vector<string> tempOutNames;
87                         outputTypes["sabund"] = tempOutNames;
88                         
89                         //if the user changes the input directory command factory will send this info to us in the output parameter 
90                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
91                         if (inputDir == "not found"){   inputDir = "";          }
92                         else {
93                                 string path;
94                                 it = parameters.find("list");
95                                 //user has given a template file
96                                 if(it != parameters.end()){ 
97                                         path = m->hasPath(it->second);
98                                         //if the user has not given a path then, add inputdir. else leave path alone.
99                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
100                                 }
101                                 
102                                 it = parameters.find("rabund");
103                                 //user has given a template file
104                                 if(it != parameters.end()){ 
105                                         path = m->hasPath(it->second);
106                                         //if the user has not given a path then, add inputdir. else leave path alone.
107                                         if (path == "") {       parameters["rabund"] = inputDir + it->second;           }
108                                 }
109                         }
110                         
111                         
112                         //check for required parameters
113                         listfile = validParameter.validFile(parameters, "list", true);
114                         if (listfile == "not open") { listfile = ""; abort = true; }
115                         else if (listfile == "not found") { listfile = ""; }
116                         else {  format = "list"; inputfile = listfile; }
117                         
118                         rabundfile = validParameter.validFile(parameters, "rabund", true);
119                         if (rabundfile == "not open") { rabundfile = ""; abort = true; }        
120                         else if (rabundfile == "not found") { rabundfile = ""; }
121                         else {  format = "rabund"; inputfile = rabundfile; }
122                         
123                 
124                                                 //check for optional parameter and set defaults
125                         // ...at some point should added some additional type checking...
126                         label = validParameter.validFile(parameters, "label", false);                   
127                         if (label == "not found") { label = ""; }
128                         else { 
129                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
130                                 else { allLines = 1;  }
131                         }
132                         
133                         if ((listfile == "") && (rabundfile == "")) { 
134                                 //is there are current file available for any of these?
135                                 //give priority to shared, then list, then rabund, then sabund
136                                 //if there is a current shared file, use it
137                                 listfile = m->getListFile(); 
138                                 if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
139                                 else { 
140                                         rabundfile = m->getRabundFile(); 
141                                         if (rabundfile != "") { inputfile = rabundfile; format = "rabund"; m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
142                                         else { 
143                                                 m->mothurOut("No valid current files. You must provide a list or rabund file."); m->mothurOutEndLine(); 
144                                                 abort = true;
145                                         }
146                                 }
147                         }
148                         
149                         
150                         //if the user changes the output directory command factory will send this info to us in the output parameter 
151                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(inputfile);      }                       
152                         
153                         
154                 }
155
156         }
157         catch(exception& e) {
158                 m->errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
159                 exit(1);
160         }
161 }
162 //**********************************************************************************************************************
163
164 int GetSAbundCommand::execute(){
165         try {
166                 
167                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
168                 filename = outputDir + m->getRootName(m->getSimpleName(inputfile)) + "sabund";
169                 m->openOutputFile(filename, out);
170                 
171                 input = new InputData(inputfile, format);
172                 sabund = input->getSAbundVector();
173                 string lastLabel = sabund->getLabel();
174                 
175                                                 
176                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
177                 set<string> processedLabels;
178                 set<string> userLabels = labels;
179                 
180                 if (m->control_pressed) {  outputTypes.clear(); out.close(); remove(filename.c_str());  delete sabund; delete input; return 0; }
181
182                 
183                 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
184                         
185                         if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
186                                         m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
187                                         
188                                         sabund->print(out);
189                                         delete sabund;
190                                         
191                                 if (m->control_pressed) { outputTypes.clear();  out.close(); remove(filename.c_str());  delete sabund; delete input;  return 0; }
192
193                                         processedLabels.insert(sabund->getLabel());
194                                         userLabels.erase(sabund->getLabel());
195                         }
196                         
197                         if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
198                                         string saveLabel = sabund->getLabel();
199                                         
200                                         delete sabund;          
201                                         sabund = (input->getSAbundVector(lastLabel));
202                                         
203                                         m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
204                                         sabund->print(out);
205                                         delete sabund;
206                                         
207                                         if (m->control_pressed) {  outputTypes.clear(); out.close(); remove(filename.c_str());  delete sabund; delete input;  return 0; }
208
209                                         processedLabels.insert(sabund->getLabel());
210                                         userLabels.erase(sabund->getLabel());
211                                         
212                                         //restore real lastlabel to save below
213                                         sabund->setLabel(saveLabel);
214                         }
215                         
216                         
217                         lastLabel = sabund->getLabel(); 
218                         
219                         delete sabund;          
220                         sabund = (input->getSAbundVector());
221                 }
222                 
223                 //output error messages about any remaining user labels
224                 set<string>::iterator it;
225                 bool needToRun = false;
226                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
227                         m->mothurOut("Your file does not include the label " + *it); 
228                         if (processedLabels.count(lastLabel) != 1) {
229                                 m->mothurOut(". I will use " + lastLabel + ".");  m->mothurOutEndLine();
230                                 needToRun = true;
231                         }else {
232                                 m->mothurOut(". Please refer to " + lastLabel + ".");  m->mothurOutEndLine();
233                         }
234                 }
235                 
236                 //run last label if you need to
237                 if (needToRun == true)  {
238                         if (sabund != NULL) {   delete sabund;  }
239                         sabund = (input->getSAbundVector(lastLabel));
240                         
241                         m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
242                         sabund->print(out);
243                         delete sabund;
244                         
245                         if (m->control_pressed) {  outputTypes.clear(); out.close(); remove(filename.c_str());  delete input; return 0; }
246                         
247                 }
248                 
249                 out.close();
250                 delete input;
251                 
252                 m->mothurOutEndLine();
253                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
254                 m->mothurOut(filename); m->mothurOutEndLine();  outputNames.push_back(filename); outputTypes["sabund"].push_back(filename);
255                 m->mothurOutEndLine();
256                 
257                 //set sabund file as new current sabundfile
258                 string current = "";
259                 itTypes = outputTypes.find("sabund");
260                 if (itTypes != outputTypes.end()) {
261                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
262                 }
263                 
264                 return 0;               
265         }
266
267         catch(exception& e) {
268                 m->errorOut(e, "GetSAbundCommand", "execute");
269                 exit(1);
270         }
271 }
272
273 //**********************************************************************************************************************
274
275