]> git.donarmstrong.com Git - mothur.git/blob - getsabundcommand.cpp
fixed some bugs
[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                         OptionParser parser(option);
31                         map<string,string> parameters = parser.getParameters();
32                         
33                         ValidParameters validParameter;
34                         
35                         //check to make sure all parameters are valid for command
36                         for (map<string,string>::iterator 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                         if (abort == false) {
69                                 filename = getRootName(globaldata->inputFileName) + "sabund";
70                                 openOutputFile(filename, out);
71                         }
72                 }
73
74         }
75         catch(exception& e) {
76                 cout << "Standard Error: " << e.what() << " has occurred in the GetSAbundCommand class Function GetSAbundCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
77                 exit(1);
78         }
79         catch(...) {
80                 cout << "An unknown error has occurred in the GetSAbundCommand class function GetSAbundCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
81                 exit(1);
82         }       
83                         
84 }
85 //**********************************************************************************************************************
86
87 void GetSAbundCommand::help(){
88         try {
89                 cout << "The get.sabund command can only be executed after a successful read.otu of a listfile." << "\n";
90                 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";
91                 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";
92                 cout << "The get.sabund command should be in the following format: get.sabund(line=yourLines, label=yourLabels)." << "\n";
93                 cout << "Example get.sabund(line=1-3-5)." << "\n";
94                 cout << "The default value for line and label are all lines in your inputfile." << "\n";
95                 cout << "The get.sabund command outputs a .sabund file containing the lines you selected." << "\n";
96                 cout << "Note: No spaces between parameter labels (i.e. line), '=' and parameters (i.e.yourLines)." << "\n" << "\n";
97         }
98         catch(exception& e) {
99                 cout << "Standard Error: " << e.what() << " has occurred in the GetSAbundCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
100                 exit(1);
101         }
102         catch(...) {
103                 cout << "An unknown error has occurred in the GetSAbundCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
104                 exit(1);
105         }       
106 }
107
108 //**********************************************************************************************************************
109
110 GetSAbundCommand::~GetSAbundCommand(){
111 }
112
113 //**********************************************************************************************************************
114
115 int GetSAbundCommand::execute(){
116         try {
117                 
118                 if (abort == true) { return 0; }
119         
120                 int count = 1;
121                 
122                 //using order vector so you don't have to distinguish between the list and rabund files
123                 read = new ReadOTUFile(globaldata->inputFileName);      
124                 read->read(&*globaldata); 
125                 
126                 order = globaldata->gorder;
127                 lastOrder = order;
128                 input = globaldata->ginput;
129                                                 
130                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
131                 set<string> processedLabels;
132                 set<string> userLabels = labels;
133                 set<int> userLines = lines;
134
135                 
136                 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
137                         
138                         if(allLines == 1 || lines.count(count) == 1 || labels.count(order->getLabel()) == 1){
139                                         cout << order->getLabel() << '\t' << count << endl;
140                                         sabund = new SAbundVector();
141                                         *sabund = (order->getSAbundVector());
142                                         sabund->print(out);
143                                         delete sabund;
144
145                                         processedLabels.insert(order->getLabel());
146                                         userLabels.erase(order->getLabel());
147                                         userLines.erase(count);
148                         }
149                         
150                         if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastOrder->getLabel()) != 1)) {
151                                         cout << lastOrder->getLabel() << '\t' << count << endl;
152                                         sabund = new SAbundVector();
153                                         *sabund = (lastOrder->getSAbundVector());
154                                         sabund->print(out);
155                                         delete sabund;
156
157                                         processedLabels.insert(lastOrder->getLabel());
158                                         userLabels.erase(lastOrder->getLabel());
159                         }
160                         
161                         if (count != 1) { delete lastOrder; }
162                         lastOrder = order;      
163                                         
164                         order = (input->getOrderVector());
165                         count++;
166                 }
167                 
168                 //output error messages about any remaining user labels
169                 set<string>::iterator it;
170                 bool needToRun = false;
171                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
172                         cout << "Your file does not include the label "<< *it; 
173                         if (processedLabels.count(lastOrder->getLabel()) != 1) {
174                                 cout << ". I will use " << lastOrder->getLabel() << "." << endl;
175                                 needToRun = true;
176                         }else {
177                                 cout << ". Please refer to " << lastOrder->getLabel() << "." << endl;
178                         }
179                 }
180                 
181                 //run last line if you need to
182                 if (needToRun == true)  {
183                         cout << lastOrder->getLabel() << '\t' << count << endl;
184                         sabund = new SAbundVector();
185                         *sabund = (lastOrder->getSAbundVector());
186                         sabund->print(out);
187                         delete sabund;
188                 }
189                 delete lastOrder;  globaldata->gorder = NULL;
190
191                 out.close();
192                 return 0;               
193         }
194
195         catch(exception& e) {
196                 cout << "Standard Error: " << e.what() << " has occurred in the GetSAbundCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
197                 exit(1);
198         }
199         catch(...) {
200                 cout << "An unknown error has occurred in the GetSAbundCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
201                 exit(1);
202         }       
203 }
204
205 //**********************************************************************************************************************
206
207