5 * Created by Sarah Westcott on 6/2/09.
6 * Copyright 2009 __MyCompanyName__. All rights reserved.
10 #include "getrabundcommand.h"
12 //**********************************************************************************************************************
14 GetRAbundCommand::GetRAbundCommand(string option){
16 globaldata = GlobalData::getInstance();
22 //allow user to run help
23 if(option == "help") { help(); abort = true; }
26 //valid paramters for this command
27 string Array[] = {"line","label"};
28 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
30 OptionParser parser(option);
31 map<string,string> parameters = parser.getParameters();
33 ValidParameters validParameter;
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; }
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; }
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 = ""; }
48 if(line != "all") { splitAtDash(line, lines); allLines = 0; }
49 else { allLines = 1; }
52 label = validParameter.validFile(parameters, "label", false);
53 if (label == "not found") { label = ""; }
55 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
56 else { allLines = 1; }
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;
69 filename = getRootName(globaldata->inputFileName) + "rabund";
70 openOutputFile(filename, out);
76 cout << "Standard Error: " << e.what() << " has occurred in the GetRAbundCommand class Function GetRAbundCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
80 cout << "An unknown error has occurred in the GetRAbundCommand class function GetRAbundCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
85 //**********************************************************************************************************************
87 void GetRAbundCommand::help(){
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";
99 cout << "Standard Error: " << e.what() << " has occurred in the GetRAbundCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
103 cout << "An unknown error has occurred in the GetRAbundCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
108 //**********************************************************************************************************************
110 GetRAbundCommand::~GetRAbundCommand(){
111 if (abort == false) { globaldata->gListVector = NULL; }
114 //**********************************************************************************************************************
116 int GetRAbundCommand::execute(){
119 if (abort == true) { return 0; }
124 read = new ReadOTUFile(globaldata->inputFileName);
125 read->read(&*globaldata);
127 input = globaldata->ginput;
128 list = globaldata->gListVector;
129 string lastLabel = list->getLabel();
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;
137 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
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());
146 processedLabels.insert(list->getLabel());
147 userLabels.erase(list->getLabel());
148 userLines.erase(count);
151 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
153 list = input->getListVector(lastLabel);
155 cout << list->getLabel() << '\t' << count << endl;
156 rabund = new RAbundVector();
157 *rabund = (list->getRAbundVector());
161 processedLabels.insert(list->getLabel());
162 userLabels.erase(list->getLabel());
165 lastLabel = list->getLabel();
168 list = input->getListVector();
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;
181 cout << ". Please refer to " << lastLabel << "." << endl;
185 //run last line if you need to
186 if (needToRun == true) {
188 list = input->getListVector(lastLabel);
190 cout << list->getLabel() << '\t' << count << endl;
191 rabund = new RAbundVector();
192 *rabund = (list->getRAbundVector());
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";
207 cout << "An unknown error has occurred in the GetRAbundCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
212 //**********************************************************************************************************************