]> git.donarmstrong.com Git - mothur.git/blob - getsabundcommand.cpp
broke up globaldata and moved error checking and help into 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
14 GetSAbundCommand::GetSAbundCommand(string option){
15         try {
16                 globaldata = GlobalData::getInstance();
17                 abort = false;
18                 allLines = 1;
19                 lines.clear();
20                 labels.clear();
21                 
22                 //allow user to run help
23                 if(option == "help") { help(); abort = true; }
24                 
25                 else {
26                         //valid paramters for this command
27                         string Array[] =  {"line","label"};
28                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
29                         
30                         parser = new OptionParser();
31                         parser->parse(option, parameters);  delete parser;
32                         
33                         ValidParameters* validParameter = new ValidParameters();
34                 
35                         //check to make sure all parameters are valid for command
36                         for (it = parameters.begin(); it != parameters.end(); it++) { 
37                                 if (validParameter->isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
38                         }
39                         
40                         //make sure the user has already run the read.otu command
41                         if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "")) { cout << "You must read a list or rabund before you can use the get.sabund command." << endl; abort = true; }
42                         
43                         //check for optional parameter and set defaults
44                         // ...at some point should added some additional type checking...
45                         line = validParameter->validFile(parameters, "line", false);                            
46                         if (line == "not found") { line = "";  }
47                         else { 
48                                 if(line != "all") {  splitAtDash(line, lines);  allLines = 0;  }
49                                 else { allLines = 1;  }
50                         }
51                         
52                         label = validParameter->validFile(parameters, "label", false);                  
53                         if (label == "not found") { label = ""; }
54                         else { 
55                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
56                                 else { allLines = 1;  }
57                         }
58                         
59                         //make sure user did not use both the line and label parameters
60                         if ((line != "") && (label != "")) { cout << "You cannot use both the line and label parameters at the same time. " << endl; abort = true; }
61                         //if the user has not specified any line or labels use the ones from read.otu
62                         else if((line == "") && (label == "")) {  
63                                 allLines = globaldata->allLines; 
64                                 labels = globaldata->labels; 
65                                 lines = globaldata->lines;
66                         }
67                                 
68                         delete validParameter;
69                         
70                         if (abort == false) {
71                                 filename = getRootName(globaldata->inputFileName) + "sabund";
72                                 openOutputFile(filename, out);
73                         }
74                 }
75
76         }
77         catch(exception& e) {
78                 cout << "Standard Error: " << e.what() << " has occurred in the GetSAbundCommand class Function GetSAbundCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
79                 exit(1);
80         }
81         catch(...) {
82                 cout << "An unknown error has occurred in the GetSAbundCommand class function GetSAbundCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
83                 exit(1);
84         }       
85                         
86 }
87 //**********************************************************************************************************************
88
89 void GetSAbundCommand::help(){
90         try {
91                 cout << "The get.sabund command can only be executed after a successful read.otu of a listfile." << "\n";
92                 cout << "The get.sabund command parameters are line and label.  No parameters are required, and you may not use line and label at the same time." << "\n";
93                 cout << "The line and label allow you to select what distance levels you would like included in your .sabund file, and are separated by dashes." << "\n";
94                 cout << "The get.sabund command should be in the following format: get.sabund(line=yourLines, label=yourLabels)." << "\n";
95                 cout << "Example get.sabund(line=1-3-5)." << "\n";
96                 cout << "The default value for line and label are all lines in your inputfile." << "\n";
97                 cout << "The get.sabund command outputs a .sabund file containing the lines you selected." << "\n";
98                 cout << "Note: No spaces between parameter labels (i.e. line), '=' and parameters (i.e.yourLines)." << "\n" << "\n";
99         }
100         catch(exception& e) {
101                 cout << "Standard Error: " << e.what() << " has occurred in the GetSAbundCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
102                 exit(1);
103         }
104         catch(...) {
105                 cout << "An unknown error has occurred in the GetSAbundCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
106                 exit(1);
107         }       
108 }
109
110 //**********************************************************************************************************************
111
112 GetSAbundCommand::~GetSAbundCommand(){
113 }
114
115 //**********************************************************************************************************************
116
117 int GetSAbundCommand::execute(){
118         try {
119                 
120                 if (abort == true) { return 0; }
121         
122                 int count = 1;
123                 
124                 //using order vector so you don't have to distinguish between the list and rabund files
125                 read = new ReadOTUFile(globaldata->inputFileName);      
126                 read->read(&*globaldata); 
127                 
128                 order = globaldata->gorder;
129                 lastOrder = order;
130                 input = globaldata->ginput;
131                                                 
132                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
133                 set<string> processedLabels;
134                 set<string> userLabels = labels;
135                 set<int> userLines = lines;
136
137                 
138                 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
139                         
140                         if(allLines == 1 || lines.count(count) == 1 || labels.count(order->getLabel()) == 1){
141                                         cout << order->getLabel() << '\t' << count << endl;
142                                         sabund = new SAbundVector();
143                                         *sabund = (order->getSAbundVector());
144                                         sabund->print(out);
145                                         delete sabund;
146
147                                         processedLabels.insert(order->getLabel());
148                                         userLabels.erase(order->getLabel());
149                                         userLines.erase(count);
150                         }
151                         
152                         if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastOrder->getLabel()) != 1)) {
153                                         cout << lastOrder->getLabel() << '\t' << count << endl;
154                                         sabund = new SAbundVector();
155                                         *sabund = (lastOrder->getSAbundVector());
156                                         sabund->print(out);
157                                         delete sabund;
158
159                                         processedLabels.insert(lastOrder->getLabel());
160                                         userLabels.erase(lastOrder->getLabel());
161                         }
162                         
163                         if (count != 1) { delete lastOrder; }
164                         lastOrder = order;      
165                                         
166                         order = (input->getOrderVector());
167                         count++;
168                 }
169                 
170                 //output error messages about any remaining user labels
171                 set<string>::iterator it;
172                 bool needToRun = false;
173                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
174                         cout << "Your file does not include the label "<< *it; 
175                         if (processedLabels.count(lastOrder->getLabel()) != 1) {
176                                 cout << ". I will use " << lastOrder->getLabel() << "." << endl;
177                                 needToRun = true;
178                         }else {
179                                 cout << ". Please refer to " << lastOrder->getLabel() << "." << endl;
180                         }
181                 }
182                 
183                 //run last line if you need to
184                 if (needToRun == true)  {
185                         cout << lastOrder->getLabel() << '\t' << count << endl;
186                         sabund = new SAbundVector();
187                         *sabund = (lastOrder->getSAbundVector());
188                         sabund->print(out);
189                         delete sabund;
190                 }
191                 delete lastOrder;
192
193                 out.close();
194                 return 0;               
195         }
196
197         catch(exception& e) {
198                 cout << "Standard Error: " << e.what() << " has occurred in the GetSAbundCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
199                 exit(1);
200         }
201         catch(...) {
202                 cout << "An unknown error has occurred in the GetSAbundCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
203                 exit(1);
204         }       
205 }
206
207 //**********************************************************************************************************************
208
209