2 * getlistcountcommand.cpp
5 * Created by westcott on 10/12/09.
6 * Copyright 2009 Schloss Lab. All rights reserved.
10 #include "getlistcountcommand.h"
12 //**********************************************************************************************************************
13 GetListCountCommand::GetListCountCommand(string option){
15 globaldata = GlobalData::getInstance();
20 //allow user to run help
21 if(option == "help") { help(); abort = true; }
24 //valid paramters for this command
25 string AlignArray[] = {"list","label"};
26 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
28 OptionParser parser(option);
29 map<string, string> parameters = parser.getParameters();
31 ValidParameters validParameter;
33 //check to make sure all parameters are valid for command
34 for (map<string, string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
35 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
38 string ranRead = globaldata->getListFile();
40 //check for required parameters
41 listfile = validParameter.validFile(parameters, "list", true);
42 if ((listfile == "not found") && (globaldata->getListFile() == "")) { mothurOut("You must read a listfile before running the get.listcount command."); mothurOutEndLine(); abort = true; }
43 else if ((listfile == "not found") && (globaldata->getListFile() != "")) { listfile = globaldata->getListFile(); }
44 else if (listfile == "not open") { abort = true; }
45 else { globaldata->setListFile(listfile); }
47 //check for optional parameter and set defaults
48 // ...at some point should added some additional type checking...
50 label = validParameter.validFile(parameters, "label", false);
51 if (label == "not found") { label = ""; }
53 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
54 else { allLines = 1; }
57 //if the user has not specified any labels use the ones from read.otu
58 if ((label == "") && (ranRead != "")) {
59 allLines = globaldata->allLines;
60 labels = globaldata->labels;
65 errorOut(e, "GetListCountCommand", "GetListCountCommand");
69 //**********************************************************************************************************************
71 void GetListCountCommand::help(){
73 mothurOut("The get.listcount command can only be executed after a successful read.otu command of a listfile or providing a list file using the list parameter.\n");
74 mothurOut("The get.listcount command parameters are list and label. No parameters are required.\n");
75 mothurOut("The label parameter allows you to select what distance levels you would like a output files created for, and are separated by dashes.\n");
76 mothurOut("The get.listcount command should be in the following format: get.listcount(list=yourlistFile, label=yourLabels).\n");
77 mothurOut("Example get.listcount(list=amazon.fn.list, label=0.10).\n");
78 mothurOut("The default value for label is all lines in your inputfile.\n");
79 mothurOut("The get.listcount command outputs a .otu file for each distance you specify listing the bin number and the names of the sequences in that bin.\n");
80 mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n\n");
83 errorOut(e, "GetListCountCommand", "help");
88 //**********************************************************************************************************************
90 GetListCountCommand::~GetListCountCommand(){}
92 //**********************************************************************************************************************
94 int GetListCountCommand::execute(){
96 if (abort == true) { return 0; }
98 globaldata->setFormat("list");
101 read = new ReadOTUFile(listfile);
102 read->read(&*globaldata);
104 input = globaldata->ginput;
105 list = globaldata->gListVector;
106 string lastLabel = list->getLabel();
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((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
114 if(allLines == 1 || labels.count(list->getLabel()) == 1){
118 processedLabels.insert(list->getLabel());
119 userLabels.erase(list->getLabel());
122 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
123 string saveLabel = list->getLabel();
126 list = input->getListVector(lastLabel);
130 processedLabels.insert(list->getLabel());
131 userLabels.erase(list->getLabel());
133 //restore real lastlabel to save below
134 list->setLabel(saveLabel);
137 lastLabel = list->getLabel();
140 list = input->getListVector();
144 //output error messages about any remaining user labels
145 set<string>::iterator it;
146 bool needToRun = false;
147 for (it = userLabels.begin(); it != userLabels.end(); it++) {
148 mothurOut("Your file does not include the label " + *it);
149 if (processedLabels.count(lastLabel) != 1) {
150 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
153 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
157 //run last label if you need to
158 if (needToRun == true) {
159 if (list != NULL) { delete list; }
160 list = input->getListVector(lastLabel);
167 globaldata->gListVector = NULL;
171 catch(exception& e) {
172 errorOut(e, "GetListCountCommand", "execute");
177 //**********************************************************************************************************************
178 //return 1 if error, 0 otherwise
179 void GetListCountCommand::process(ListVector* list) {
181 string binnames, name, sequence;
182 string outputFileName = getRootName(listfile) + list->getLabel() + ".otu";
183 openOutputFile(outputFileName, out);
185 mothurOut(list->getLabel()); mothurOutEndLine();
187 //for each bin in the list vector
188 for (int i = 0; i < list->getNumBins(); i++) {
189 binnames = list->get(i);
190 out << i+1 << '\t' << binnames << endl;
195 catch(exception& e) {
196 errorOut(e, "GetListCountCommand", "process");
200 //**********************************************************************************************************************