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