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 parser = new OptionParser();
31 parser->parse(option, parameters); delete parser;
33 ValidParameters* validParameter = new ValidParameters();
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; }
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;
68 delete validParameter;
71 filename = getRootName(globaldata->inputFileName) + "rabund";
72 openOutputFile(filename, out);
78 cout << "Standard Error: " << e.what() << " has occurred in the GetRAbundCommand class Function GetRAbundCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
82 cout << "An unknown error has occurred in the GetRAbundCommand class function GetRAbundCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
87 //**********************************************************************************************************************
89 void GetRAbundCommand::help(){
91 cout << "The get.rabund command can only be executed after a successful read.otu of a listfile." << "\n";
92 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";
93 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";
94 cout << "The get.rabund command should be in the following format: get.rabund(line=yourLines, label=yourLabels)." << "\n";
95 cout << "Example get.rabund(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.rabund command outputs a .rabund 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";
100 catch(exception& e) {
101 cout << "Standard Error: " << e.what() << " has occurred in the GetRAbundCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
105 cout << "An unknown error has occurred in the GetRAbundCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
110 //**********************************************************************************************************************
112 GetRAbundCommand::~GetRAbundCommand(){
115 //**********************************************************************************************************************
117 int GetRAbundCommand::execute(){
120 if (abort == true) { return 0; }
125 read = new ReadOTUFile(globaldata->inputFileName);
126 read->read(&*globaldata);
128 input = globaldata->ginput;
129 list = globaldata->gListVector;
130 ListVector* lastList = list;
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;
138 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
140 if(allLines == 1 || lines.count(count) == 1 || labels.count(list->getLabel()) == 1){
141 cout << list->getLabel() << '\t' << count << endl;
142 rabund = new RAbundVector();
143 *rabund = (list->getRAbundVector());
147 processedLabels.insert(list->getLabel());
148 userLabels.erase(list->getLabel());
149 userLines.erase(count);
152 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastList->getLabel()) != 1)) {
153 cout << lastList->getLabel() << '\t' << count << endl;
154 rabund = new RAbundVector();
155 *rabund = (lastList->getRAbundVector());
159 processedLabels.insert(lastList->getLabel());
160 userLabels.erase(lastList->getLabel());
163 if (count != 1) { delete lastList; }
166 list = input->getListVector();
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(lastList->getLabel()) != 1) {
176 cout << ". I will use " << lastList->getLabel() << "." << endl;
179 cout << ". Please refer to " << lastList->getLabel() << "." << endl;
183 //run last line if you need to
184 if (needToRun == true) {
185 cout << lastList->getLabel() << '\t' << count << endl;
186 rabund = new RAbundVector();
187 *rabund = (lastList->getRAbundVector());
197 catch(exception& e) {
198 cout << "Standard Error: " << e.what() << " has occurred in the GetRAbundCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
202 cout << "An unknown error has occurred in the GetRAbundCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
207 //**********************************************************************************************************************