5 * Created by Sarah Westcott on 6/2/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "getsabundcommand.h"
12 //**********************************************************************************************************************
14 GetSAbundCommand::GetSAbundCommand(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() == "") && (globaldata->getRabundFile() == "")) { cout << "You must read a list or rabund before you can use the get.sabund 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) + "sabund";
72 openOutputFile(filename, out);
78 cout << "Standard Error: " << e.what() << " has occurred in the GetSAbundCommand class Function GetSAbundCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
82 cout << "An unknown error has occurred in the GetSAbundCommand class function GetSAbundCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
87 //**********************************************************************************************************************
89 void GetSAbundCommand::help(){
91 cout << "The get.sabund command can only be executed after a successful read.otu of a listfile." << "\n";
92 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";
93 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";
94 cout << "The get.sabund command should be in the following format: get.sabund(line=yourLines, label=yourLabels)." << "\n";
95 cout << "Example get.sabund(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.sabund command outputs a .sabund 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 GetSAbundCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
105 cout << "An unknown error has occurred in the GetSAbundCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
110 //**********************************************************************************************************************
112 GetSAbundCommand::~GetSAbundCommand(){
115 //**********************************************************************************************************************
117 int GetSAbundCommand::execute(){
120 if (abort == true) { return 0; }
124 //using order vector so you don't have to distinguish between the list and rabund files
125 read = new ReadOTUFile(globaldata->inputFileName);
126 read->read(&*globaldata);
128 order = globaldata->gorder;
130 input = globaldata->ginput;
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((order != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
140 if(allLines == 1 || lines.count(count) == 1 || labels.count(order->getLabel()) == 1){
141 cout << order->getLabel() << '\t' << count << endl;
142 sabund = new SAbundVector();
143 *sabund = (order->getSAbundVector());
147 processedLabels.insert(order->getLabel());
148 userLabels.erase(order->getLabel());
149 userLines.erase(count);
152 if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastOrder->getLabel()) != 1)) {
153 cout << lastOrder->getLabel() << '\t' << count << endl;
154 sabund = new SAbundVector();
155 *sabund = (lastOrder->getSAbundVector());
159 processedLabels.insert(lastOrder->getLabel());
160 userLabels.erase(lastOrder->getLabel());
163 if (count != 1) { delete lastOrder; }
166 order = (input->getOrderVector());
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(lastOrder->getLabel()) != 1) {
176 cout << ". I will use " << lastOrder->getLabel() << "." << endl;
179 cout << ". Please refer to " << lastOrder->getLabel() << "." << endl;
183 //run last line if you need to
184 if (needToRun == true) {
185 cout << lastOrder->getLabel() << '\t' << count << endl;
186 sabund = new SAbundVector();
187 *sabund = (lastOrder->getSAbundVector());
197 catch(exception& e) {
198 cout << "Standard Error: " << e.what() << " has occurred in the GetSAbundCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
202 cout << "An unknown error has occurred in the GetSAbundCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
207 //**********************************************************************************************************************