]> git.donarmstrong.com Git - mothur.git/blob - getrabundcommand.cpp
b8c692bf7f64b4566311a3f9232f933aec100800
[mothur.git] / getrabundcommand.cpp
1 /*
2  *  getrabundcommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 6/2/09.
6  *  Copyright 2009 __MyCompanyName__. All rights reserved.
7  *
8  */
9
10 #include "getrabundcommand.h"
11
12 //**********************************************************************************************************************
13
14 GetRAbundCommand::GetRAbundCommand(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() == "") { cout << "You must read a listfile before you can use the get.rabund 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) + "rabund";
70                                 openOutputFile(filename, out);
71                         }
72                 }
73
74         }
75         catch(exception& e) {
76                 cout << "Standard Error: " << e.what() << " has occurred in the GetRAbundCommand class Function GetRAbundCommand. 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 GetRAbundCommand class function GetRAbundCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
81                 exit(1);
82         }       
83                         
84 }
85 //**********************************************************************************************************************
86
87 void GetRAbundCommand::help(){
88         try {
89                 cout << "The get.rabund command can only be executed after a successful read.otu of a listfile." << "\n";
90                 cout << "The get.rabund 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 .rabund file, and are separated by dashes." << "\n";
92                 cout << "The get.rabund command should be in the following format: get.rabund(line=yourLines, label=yourLabels)." << "\n";
93                 cout << "Example get.rabund(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.rabund command outputs a .rabund 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 GetRAbundCommand 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 GetRAbundCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
104                 exit(1);
105         }       
106 }
107
108 //**********************************************************************************************************************
109
110 GetRAbundCommand::~GetRAbundCommand(){
111         if (abort == false) {  globaldata->gListVector = NULL; }
112 }
113
114 //**********************************************************************************************************************
115
116 int GetRAbundCommand::execute(){
117         try {
118         
119                 if (abort == true) { return 0; }
120                 
121                 int count = 1;
122                 
123                 //read first line
124                 read = new ReadOTUFile(globaldata->inputFileName);      
125                 read->read(&*globaldata); 
126                         
127                 input = globaldata->ginput;
128                 list = globaldata->gListVector;
129                 string lastLabel = list->getLabel();
130                 
131                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
132                 set<string> processedLabels;
133                 set<string> userLabels = labels;
134                 set<int> userLines = lines;
135
136                 
137                 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
138                         
139                         if(allLines == 1 || lines.count(count) == 1 || labels.count(list->getLabel()) == 1){
140                                         cout << list->getLabel() << '\t' << count << endl;
141                                         rabund = new RAbundVector();
142                                         *rabund = (list->getRAbundVector());
143                                         rabund->print(out);
144                                         delete rabund;
145                                                                                                                         
146                                         processedLabels.insert(list->getLabel());
147                                         userLabels.erase(list->getLabel());
148                                         userLines.erase(count);
149                         }
150                         
151                         if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
152                                         delete list;
153                                         list = input->getListVector(lastLabel);
154                                         
155                                         cout << list->getLabel() << '\t' << count << endl;
156                                         rabund = new RAbundVector();
157                                         *rabund = (list->getRAbundVector());
158                                         rabund->print(out);
159                                         delete rabund;
160
161                                         processedLabels.insert(list->getLabel());
162                                         userLabels.erase(list->getLabel());
163                         }
164                         
165                         lastLabel = list->getLabel();           
166                         
167                         delete list;
168                         list = input->getListVector();
169                         count++;
170                 }
171                 
172                 //output error messages about any remaining user labels
173                 set<string>::iterator it;
174                 bool needToRun = false;
175                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
176                         cout << "Your file does not include the label "<< *it; 
177                         if (processedLabels.count(lastLabel) != 1) {
178                                 cout << ". I will use " << lastLabel << "." << endl;
179                                 needToRun = true;
180                         }else {
181                                 cout << ". Please refer to " << lastLabel << "." << endl;
182                         }
183                 }
184                 
185                 //run last line if you need to
186                 if (needToRun == true)  {
187                         delete list;
188                         list = input->getListVector(lastLabel);
189                         
190                         cout << list->getLabel() << '\t' << count << endl;
191                         rabund = new RAbundVector();
192                         *rabund = (list->getRAbundVector());
193                         rabund->print(out);
194                         delete rabund;
195                         delete list;
196                 }
197
198                 out.close(); 
199                 return 0;               
200         }
201
202         catch(exception& e) {
203                 cout << "Standard Error: " << e.what() << " has occurred in the GetRAbundCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
204                 exit(1);
205         }
206         catch(...) {
207                 cout << "An unknown error has occurred in the GetRAbundCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
208                 exit(1);
209         }       
210 }
211
212 //**********************************************************************************************************************
213
214