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 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() == "") && (globaldata->getRabundFile() == "")) { mothurOut("You must read a list or rabund before you can use the get.sabund command."); mothurOutEndLine(); 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 != "")) { mothurOut("You cannot use both the line and label parameters at the same time. "); mothurOutEndLine(); 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) + "sabund";
70 openOutputFile(filename, out);
76 errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
80 //**********************************************************************************************************************
82 void GetSAbundCommand::help(){
84 mothurOut("The get.sabund command can only be executed after a successful read.otu of a listfile or rabundfile.\n");
85 mothurOut("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");
86 mothurOut("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");
87 mothurOut("The get.sabund command should be in the following format: get.sabund(line=yourLines, label=yourLabels).\n");
88 mothurOut("Example get.sabund(line=1-3-5).\n");
89 mothurOut("The default value for line and label are all lines in your inputfile.\n");
90 mothurOut("The get.sabund command outputs a .sabund file containing the lines you selected.\n");
91 mothurOut("Note: No spaces between parameter labels (i.e. line), '=' and parameters (i.e.yourLines).\n\n");
94 errorOut(e, "GetSAbundCommand", "help");
99 //**********************************************************************************************************************
101 GetSAbundCommand::~GetSAbundCommand(){
104 //**********************************************************************************************************************
106 int GetSAbundCommand::execute(){
109 if (abort == true) { return 0; }
113 //using order vector so you don't have to distinguish between the list and rabund files
114 read = new ReadOTUFile(globaldata->inputFileName);
115 read->read(&*globaldata);
117 order = globaldata->gorder;
118 string lastLabel = order->getLabel();
119 input = globaldata->ginput;
121 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
122 set<string> processedLabels;
123 set<string> userLabels = labels;
124 set<int> userLines = lines;
127 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
129 if(allLines == 1 || lines.count(count) == 1 || labels.count(order->getLabel()) == 1){
130 mothurOut(order->getLabel()); mothurOutEndLine();
131 sabund = new SAbundVector();
132 *sabund = (order->getSAbundVector());
136 processedLabels.insert(order->getLabel());
137 userLabels.erase(order->getLabel());
138 userLines.erase(count);
141 if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
143 order = (input->getOrderVector(lastLabel));
145 mothurOut(order->getLabel()); mothurOutEndLine();
146 sabund = new SAbundVector();
147 *sabund = (order->getSAbundVector());
151 processedLabels.insert(order->getLabel());
152 userLabels.erase(order->getLabel());
156 lastLabel = order->getLabel();
159 order = (input->getOrderVector());
163 //output error messages about any remaining user labels
164 set<string>::iterator it;
165 bool needToRun = false;
166 for (it = userLabels.begin(); it != userLabels.end(); it++) {
167 mothurOut("Your file does not include the label " + *it);
168 if (processedLabels.count(lastLabel) != 1) {
169 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
172 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
176 //run last line if you need to
177 if (needToRun == true) {
179 order = (input->getOrderVector(lastLabel));
181 mothurOut(order->getLabel()); mothurOutEndLine();
182 sabund = new SAbundVector();
183 *sabund = (order->getSAbundVector());
188 globaldata->gorder = NULL;
194 catch(exception& e) {
195 errorOut(e, "GetSAbundCommand", "execute");
200 //**********************************************************************************************************************