]> git.donarmstrong.com Git - mothur.git/blob - getsabundcommand.cpp
added citation function to commands
[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";
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                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
71                 
72                 else {
73                         vector<string> myArray = setParameters();
74                         
75                         OptionParser parser(option);
76                         map<string,string> parameters = parser.getParameters();
77                         map<string,string>::iterator it;
78                         
79                         ValidParameters validParameter;
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["sabund"] = tempOutNames;
89                         
90                         //if the user changes the input directory command factory will send this info to us in the output parameter 
91                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
92                         if (inputDir == "not found"){   inputDir = "";          }
93                         else {
94                                 string path;
95                                 it = parameters.find("list");
96                                 //user has given a template file
97                                 if(it != parameters.end()){ 
98                                         path = m->hasPath(it->second);
99                                         //if the user has not given a path then, add inputdir. else leave path alone.
100                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
101                                 }
102                                 
103                                 it = parameters.find("rabund");
104                                 //user has given a template file
105                                 if(it != parameters.end()){ 
106                                         path = m->hasPath(it->second);
107                                         //if the user has not given a path then, add inputdir. else leave path alone.
108                                         if (path == "") {       parameters["rabund"] = inputDir + it->second;           }
109                                 }
110                         }
111                         
112                         
113                         //check for required parameters
114                         listfile = validParameter.validFile(parameters, "list", true);
115                         if (listfile == "not open") { listfile = ""; abort = true; }
116                         else if (listfile == "not found") { listfile = ""; }
117                         else {  format = "list"; inputfile = listfile; }
118                         
119                         rabundfile = validParameter.validFile(parameters, "rabund", true);
120                         if (rabundfile == "not open") { rabundfile = ""; abort = true; }        
121                         else if (rabundfile == "not found") { rabundfile = ""; }
122                         else {  format = "rabund"; inputfile = rabundfile; }
123                         
124                 
125                                                 //check for optional parameter and set defaults
126                         // ...at some point should added some additional type checking...
127                         label = validParameter.validFile(parameters, "label", false);                   
128                         if (label == "not found") { label = ""; }
129                         else { 
130                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
131                                 else { allLines = 1;  }
132                         }
133                         
134                         if ((listfile == "") && (rabundfile == "")) { 
135                                 //is there are current file available for any of these?
136                                 //give priority to shared, then list, then rabund, then sabund
137                                 //if there is a current shared file, use it
138                                 listfile = m->getListFile(); 
139                                 if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
140                                 else { 
141                                         rabundfile = m->getRabundFile(); 
142                                         if (rabundfile != "") { inputfile = rabundfile; format = "rabund"; m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
143                                         else { 
144                                                 m->mothurOut("No valid current files. You must provide a list or rabund file."); m->mothurOutEndLine(); 
145                                                 abort = true;
146                                         }
147                                 }
148                         }
149                         
150                         
151                         //if the user changes the output directory command factory will send this info to us in the output parameter 
152                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(inputfile);      }                       
153                         
154                         
155                 }
156
157         }
158         catch(exception& e) {
159                 m->errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
160                 exit(1);
161         }
162 }
163 //**********************************************************************************************************************
164
165 int GetSAbundCommand::execute(){
166         try {
167                 
168                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
169                 filename = outputDir + m->getRootName(m->getSimpleName(inputfile)) + "sabund";
170                 m->openOutputFile(filename, out);
171                 
172                 input = new InputData(inputfile, format);
173                 sabund = input->getSAbundVector();
174                 string lastLabel = sabund->getLabel();
175                 
176                                                 
177                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
178                 set<string> processedLabels;
179                 set<string> userLabels = labels;
180                 
181                 if (m->control_pressed) {  outputTypes.clear(); out.close(); remove(filename.c_str());  delete sabund; delete input; return 0; }
182
183                 
184                 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
185                         
186                         if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
187                                         m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
188                                         
189                                         sabund->print(out);
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                                         
206                                         if (m->control_pressed) {  outputTypes.clear(); out.close(); remove(filename.c_str());  delete sabund; delete input;  return 0; }
207
208                                         processedLabels.insert(sabund->getLabel());
209                                         userLabels.erase(sabund->getLabel());
210                                         
211                                         //restore real lastlabel to save below
212                                         sabund->setLabel(saveLabel);
213                         }
214                         
215                         
216                         lastLabel = sabund->getLabel(); 
217                         
218                         delete sabund;          
219                         sabund = (input->getSAbundVector());
220                 }
221                 
222                 //output error messages about any remaining user labels
223                 set<string>::iterator it;
224                 bool needToRun = false;
225                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
226                         m->mothurOut("Your file does not include the label " + *it); 
227                         if (processedLabels.count(lastLabel) != 1) {
228                                 m->mothurOut(". I will use " + lastLabel + ".");  m->mothurOutEndLine();
229                                 needToRun = true;
230                         }else {
231                                 m->mothurOut(". Please refer to " + lastLabel + ".");  m->mothurOutEndLine();
232                         }
233                 }
234                 
235                 //run last label if you need to
236                 if (needToRun == true)  {
237                         if (sabund != NULL) {   delete sabund;  }
238                         sabund = (input->getSAbundVector(lastLabel));
239                         
240                         m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
241                         sabund->print(out);
242                         delete sabund;
243                         
244                         if (m->control_pressed) {  outputTypes.clear(); out.close(); remove(filename.c_str());  delete input; return 0; }
245                         
246                 }
247                 
248                 out.close();
249                 delete input;
250                 
251                 m->mothurOutEndLine();
252                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
253                 m->mothurOut(filename); m->mothurOutEndLine();  outputNames.push_back(filename); outputTypes["sabund"].push_back(filename);
254                 m->mothurOutEndLine();
255                 
256                 //set sabund file as new current sabundfile
257                 string current = "";
258                 itTypes = outputTypes.find("sabund");
259                 if (itTypes != outputTypes.end()) {
260                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
261                 }
262                 
263                 return 0;               
264         }
265
266         catch(exception& e) {
267                 m->errorOut(e, "GetSAbundCommand", "execute");
268                 exit(1);
269         }
270 }
271
272 //**********************************************************************************************************************
273
274