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();
21 //allow user to run help
22 if(option == "help") { help(); abort = true; }
25 //valid paramters for this command
26 string Array[] = {"label"};
27 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
29 OptionParser parser(option);
30 map<string,string> parameters = parser.getParameters();
32 ValidParameters validParameter;
34 //check to make sure all parameters are valid for command
35 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
36 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
39 //make sure the user has already run the read.otu command
40 if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "")) { mothurOut("You must read a list or rabund before you can use the get.sabund command."); mothurOutEndLine(); abort = true; }
42 //check for optional parameter and set defaults
43 // ...at some point should added some additional type checking...
44 label = validParameter.validFile(parameters, "label", false);
45 if (label == "not found") { label = ""; }
47 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
48 else { allLines = 1; }
51 //if the user has not specified any labels use the ones from read.otu
53 allLines = globaldata->allLines;
54 labels = globaldata->labels;
58 filename = getRootName(globaldata->inputFileName) + "sabund";
59 openOutputFile(filename, out);
65 errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
69 //**********************************************************************************************************************
71 void GetSAbundCommand::help(){
73 mothurOut("The get.sabund command can only be executed after a successful read.otu of a listfile or rabundfile.\n");
74 mothurOut("The get.sabund command parameters is label. No parameters are required.\n");
75 mothurOut("The label parameter allows you to select what distance levels you would like included in your .sabund file, and are separated by dashes.\n");
76 mothurOut("The get.sabund command should be in the following format: get.sabund(label=yourLabels).\n");
77 mothurOut("Example get.sabund().\n");
78 mothurOut("The default value for label is all labels in your inputfile.\n");
79 mothurOut("The get.sabund command outputs a .sabund file containing the labels you selected.\n");
80 mothurOut("Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabel).\n\n");
83 errorOut(e, "GetSAbundCommand", "help");
88 //**********************************************************************************************************************
90 GetSAbundCommand::~GetSAbundCommand(){
93 //**********************************************************************************************************************
95 int GetSAbundCommand::execute(){
98 if (abort == true) { return 0; }
100 //using order vector so you don't have to distinguish between the list and rabund files
101 read = new ReadOTUFile(globaldata->inputFileName);
102 read->read(&*globaldata);
104 order = globaldata->gorder;
105 string lastLabel = order->getLabel();
106 input = globaldata->ginput;
108 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
109 set<string> processedLabels;
110 set<string> userLabels = labels;
112 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
114 if(allLines == 1 || labels.count(order->getLabel()) == 1){
115 mothurOut(order->getLabel()); mothurOutEndLine();
116 sabund = new SAbundVector();
117 *sabund = (order->getSAbundVector());
121 processedLabels.insert(order->getLabel());
122 userLabels.erase(order->getLabel());
125 if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
127 order = (input->getOrderVector(lastLabel));
129 mothurOut(order->getLabel()); mothurOutEndLine();
130 sabund = new SAbundVector();
131 *sabund = (order->getSAbundVector());
135 processedLabels.insert(order->getLabel());
136 userLabels.erase(order->getLabel());
140 lastLabel = order->getLabel();
143 order = (input->getOrderVector());
146 //output error messages about any remaining user labels
147 set<string>::iterator it;
148 bool needToRun = false;
149 for (it = userLabels.begin(); it != userLabels.end(); it++) {
150 mothurOut("Your file does not include the label " + *it);
151 if (processedLabels.count(lastLabel) != 1) {
152 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
155 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
159 //run last label if you need to
160 if (needToRun == true) {
161 if (order != NULL) { delete order; }
162 order = (input->getOrderVector(lastLabel));
164 mothurOut(order->getLabel()); mothurOutEndLine();
165 sabund = new SAbundVector();
166 *sabund = (order->getSAbundVector());
171 globaldata->gorder = NULL;
177 catch(exception& e) {
178 errorOut(e, "GetSAbundCommand", "execute");
183 //**********************************************************************************************************************