]> git.donarmstrong.com Git - mothur.git/blob - getsabundcommand.cpp
fixes while testing
[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::getValidParameters(){  
14         try {
15                 string Array[] =  {"label","outputdir","inputdir"};
16                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
17                 return myArray;
18         }
19         catch(exception& e) {
20                 m->errorOut(e, "GetSAbundCommand", "getValidParameters");
21                 exit(1);
22         }
23 }
24 //**********************************************************************************************************************
25 GetSAbundCommand::GetSAbundCommand(){   
26         try {
27                 abort = true;
28                 //initialize outputTypes
29                 vector<string> tempOutNames;
30                 outputTypes["sabund"] = tempOutNames;
31         }
32         catch(exception& e) {
33                 m->errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
34                 exit(1);
35         }
36 }
37 //**********************************************************************************************************************
38 vector<string> GetSAbundCommand::getRequiredParameters(){       
39         try {
40                 vector<string> myArray;
41                 return myArray;
42         }
43         catch(exception& e) {
44                 m->errorOut(e, "GetSAbundCommand", "getRequiredParameters");
45                 exit(1);
46         }
47 }
48 //**********************************************************************************************************************
49 vector<string> GetSAbundCommand::getRequiredFiles(){    
50         try {
51                 string Array[] =  {"list","rabund"};
52                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
53                 return myArray;
54         }
55         catch(exception& e) {
56                 m->errorOut(e, "GetSAbundCommand", "getRequiredFiles");
57                 exit(1);
58         }
59 }
60 //**********************************************************************************************************************
61 GetSAbundCommand::GetSAbundCommand(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 Array[] =  {"label","outputdir","inputdir"};
74                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
75                         
76                         OptionParser parser(option);
77                         map<string,string> parameters = parser.getParameters();
78                         
79                         ValidParameters validParameter;
80                         
81                         //check to make sure all parameters are valid for command
82                         for (map<string,string>::iterator 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 output directory command factory will send this info to us in the output parameter 
91                         string outputDir = validParameter.validFile(parameters, "outputdir", false);            if (outputDir == "not found"){  
92                                 outputDir = ""; 
93                                 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it       
94                         }
95
96                         //make sure the user has already run the read.otu command
97                         if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "")) { m->mothurOut("You must read a list or rabund before you can use the get.sabund command."); m->mothurOutEndLine(); abort = true; }
98                         
99                         //check for optional parameter and set defaults
100                         // ...at some point should added some additional type checking...
101                         label = validParameter.validFile(parameters, "label", false);                   
102                         if (label == "not found") { label = ""; }
103                         else { 
104                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
105                                 else { allLines = 1;  }
106                         }
107                         
108                         //if the user has not specified any labels use the ones from read.otu
109                         if(label == "") {  
110                                 allLines = globaldata->allLines; 
111                                 labels = globaldata->labels; 
112                         }
113                                 
114                         if (abort == false) {
115                                 filename = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + "sabund";
116                                 m->openOutputFile(filename, out);
117                         }
118                 }
119
120         }
121         catch(exception& e) {
122                 m->errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
123                 exit(1);
124         }
125 }
126 //**********************************************************************************************************************
127
128 void GetSAbundCommand::help(){
129         try {
130                 m->mothurOut("The get.sabund command can only be executed after a successful read.otu of a listfile or rabundfile.\n");
131                 m->mothurOut("The get.sabund command parameters is label.  No parameters are required.\n");
132                 m->mothurOut("The label parameter allows you to select what distance levels you would like included in your .sabund file, and are separated by dashes.\n");
133                 m->mothurOut("The get.sabund command should be in the following format: get.sabund(label=yourLabels).\n");
134                 m->mothurOut("Example get.sabund().\n");
135                 m->mothurOut("The default value for label is all labels in your inputfile.\n");
136                 m->mothurOut("The get.sabund command outputs a .sabund file containing the labels you selected.\n");
137                 m->mothurOut("Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabel).\n\n");
138         }
139         catch(exception& e) {
140                 m->errorOut(e, "GetSAbundCommand", "help");
141                 exit(1);
142         }
143 }
144
145 //**********************************************************************************************************************
146
147 GetSAbundCommand::~GetSAbundCommand(){
148 }
149
150 //**********************************************************************************************************************
151
152 int GetSAbundCommand::execute(){
153         try {
154                 
155                 if (abort == true) { return 0; }
156         
157                 //using order vector so you don't have to distinguish between the list and rabund files
158                 read = new ReadOTUFile(globaldata->inputFileName);      
159                 read->read(&*globaldata); 
160                 
161                 order = globaldata->gorder;
162                 string lastLabel = order->getLabel();
163                 input = globaldata->ginput;
164                                                 
165                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
166                 set<string> processedLabels;
167                 set<string> userLabels = labels;
168                 
169                 if (m->control_pressed) {  outputTypes.clear(); out.close(); remove(filename.c_str());  delete order; globaldata->gorder = NULL;  return 0; }
170
171                 
172                 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
173                         
174                         if(allLines == 1 || labels.count(order->getLabel()) == 1){
175                                         m->mothurOut(order->getLabel());  m->mothurOutEndLine();
176                                         sabund = new SAbundVector();
177                                         *sabund = (order->getSAbundVector());
178                                         sabund->print(out);
179                                         delete sabund;
180                                         
181                                         if (m->control_pressed) { outputTypes.clear();  out.close(); remove(filename.c_str());  delete order; globaldata->gorder = NULL;  return 0; }
182
183                                         processedLabels.insert(order->getLabel());
184                                         userLabels.erase(order->getLabel());
185                         }
186                         
187                         if ((m->anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
188                                         string saveLabel = order->getLabel();
189                                         
190                                         delete order;           
191                                         order = (input->getOrderVector(lastLabel));
192                                         
193                                         m->mothurOut(order->getLabel());  m->mothurOutEndLine();
194                                         sabund = new SAbundVector();
195                                         *sabund = (order->getSAbundVector());
196                                         sabund->print(out);
197                                         delete sabund;
198                                         
199                                         if (m->control_pressed) {  outputTypes.clear(); out.close(); remove(filename.c_str());  delete order; globaldata->gorder = NULL;  return 0; }
200
201                                         processedLabels.insert(order->getLabel());
202                                         userLabels.erase(order->getLabel());
203                                         
204                                         //restore real lastlabel to save below
205                                         order->setLabel(saveLabel);
206                         }
207                         
208                         
209                         lastLabel = order->getLabel();  
210                         
211                         delete order;           
212                         order = (input->getOrderVector());
213                 }
214                 
215                 //output error messages about any remaining user labels
216                 set<string>::iterator it;
217                 bool needToRun = false;
218                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
219                         m->mothurOut("Your file does not include the label " + *it); 
220                         if (processedLabels.count(lastLabel) != 1) {
221                                 m->mothurOut(". I will use " + lastLabel + ".");  m->mothurOutEndLine();
222                                 needToRun = true;
223                         }else {
224                                 m->mothurOut(". Please refer to " + lastLabel + ".");  m->mothurOutEndLine();
225                         }
226                 }
227                 
228                 //run last label if you need to
229                 if (needToRun == true)  {
230                         if (order != NULL) {    delete order;   }
231                         order = (input->getOrderVector(lastLabel));
232                         
233                         m->mothurOut(order->getLabel());  m->mothurOutEndLine();
234                         sabund = new SAbundVector();
235                         *sabund = (order->getSAbundVector());
236                         sabund->print(out);
237                         delete sabund;
238                         
239                         if (m->control_pressed) {  outputTypes.clear(); out.close(); remove(filename.c_str());  delete order; globaldata->gorder = NULL;  return 0; }
240                         
241                         delete order;
242                 }
243                 globaldata->gorder = NULL;
244
245                 out.close();
246                 
247                 m->mothurOutEndLine();
248                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
249                 m->mothurOut(filename); m->mothurOutEndLine();  outputNames.push_back(filename); outputTypes["sabund"].push_back(filename);
250                 m->mothurOutEndLine();
251                 
252                 return 0;               
253         }
254
255         catch(exception& e) {
256                 m->errorOut(e, "GetSAbundCommand", "execute");
257                 exit(1);
258         }
259 }
260
261 //**********************************************************************************************************************
262
263