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(){
113 //**********************************************************************************************************************
115 int GetRAbundCommand::execute(){
118 if (abort == true) { return 0; }
123 read = new ReadOTUFile(globaldata->inputFileName);
124 read->read(&*globaldata);
126 input = globaldata->ginput;
127 list = globaldata->gListVector;
128 ListVector* lastList = list;
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;
136 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
138 if(allLines == 1 || lines.count(count) == 1 || labels.count(list->getLabel()) == 1){
139 cout << list->getLabel() << '\t' << count << endl;
140 rabund = new RAbundVector();
141 *rabund = (list->getRAbundVector());
145 processedLabels.insert(list->getLabel());
146 userLabels.erase(list->getLabel());
147 userLines.erase(count);
150 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastList->getLabel()) != 1)) {
151 cout << lastList->getLabel() << '\t' << count << endl;
152 rabund = new RAbundVector();
153 *rabund = (lastList->getRAbundVector());
157 processedLabels.insert(lastList->getLabel());
158 userLabels.erase(lastList->getLabel());
161 if (count != 1) { delete lastList; }
164 list = input->getListVector();
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(lastList->getLabel()) != 1) {
174 cout << ". I will use " << lastList->getLabel() << "." << endl;
177 cout << ". Please refer to " << lastList->getLabel() << "." << endl;
181 //run last line if you need to
182 if (needToRun == true) {
183 cout << lastList->getLabel() << '\t' << count << endl;
184 rabund = new RAbundVector();
185 *rabund = (lastList->getRAbundVector());
195 catch(exception& e) {
196 cout << "Standard Error: " << e.what() << " has occurred in the GetRAbundCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
200 cout << "An unknown error has occurred in the GetRAbundCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
205 //**********************************************************************************************************************