2 * otuhierarchycommand.cpp
5 * Created by westcott on 1/19/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "otuhierarchycommand.h"
12 //**********************************************************************************************************************
13 vector<string> OtuHierarchyCommand::setParameters(){
15 CommandParameter poutput("output", "Multiple", "name-number", "name", "", "", "",false,false); parameters.push_back(poutput);
16 CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(plist);
17 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
18 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
19 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
21 vector<string> myArray;
22 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
26 m->errorOut(e, "OtuHierarchyCommand", "setParameters");
30 //**********************************************************************************************************************
31 string OtuHierarchyCommand::getHelpString(){
33 string helpString = "";
34 helpString += "The otu.hierarchy command is used to see how otus relate at two distances. \n";
35 helpString += "The otu.hierarchy command parameters are list, label and output. list and label parameters are required. \n";
36 helpString += "The output parameter allows you to output the names of the sequence in the OTUs or the OTU numbers. Options are name and number, default is name. \n";
37 helpString += "The otu.hierarchy command should be in the following format: \n";
38 helpString += "otu.hierarchy(list=yourListFile, label=yourLabels).\n";
39 helpString += "Example otu.hierarchy(list=amazon.fn.list, label=0.01-0.03).\n";
40 helpString += "The otu.hierarchy command outputs a .otu.hierarchy file which is described on the wiki.\n";
41 helpString += "Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n";
45 m->errorOut(e, "OtuHierarchyCommand", "getHelpString");
50 //**********************************************************************************************************************
51 OtuHierarchyCommand::OtuHierarchyCommand(){
53 abort = true; calledHelp = true;
55 vector<string> tempOutNames;
56 outputTypes["otuheirarchy"] = tempOutNames;
59 m->errorOut(e, "OtuHierarchyCommand", "OtuHierarchyCommand");
63 //**********************************************************************************************************************
64 OtuHierarchyCommand::OtuHierarchyCommand(string option) {
66 abort = false; calledHelp = false;
68 //allow user to run help
69 if(option == "help") { help(); abort = true; calledHelp = true; }
70 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
73 vector<string> myArray = setParameters();
75 OptionParser parser(option);
76 map<string,string> parameters = parser.getParameters();
78 ValidParameters validParameter;
79 map<string,string>::iterator it;
81 //check to make sure all parameters are valid for command
82 for (it = parameters.begin(); it != parameters.end(); it++) {
83 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
86 //initialize outputTypes
87 vector<string> tempOutNames;
88 outputTypes["otuheirarchy"] = tempOutNames;
90 //if the user changes the input directory command factory will send this info to us in the output parameter
91 string inputDir = validParameter.validFile(parameters, "inputdir", false);
92 if (inputDir == "not found"){ inputDir = ""; }
95 it = parameters.find("list");
96 //user has given a template file
97 if(it != parameters.end()){
98 path = m->hasPath(it->second);
99 //if the user has not given a path then, add inputdir. else leave path alone.
100 if (path == "") { parameters["list"] = inputDir + it->second; }
104 listFile = validParameter.validFile(parameters, "list", true);
105 if (listFile == "not found") {
106 listFile = m->getListFile();
107 if (listFile != "") { m->mothurOut("Using " + listFile + " as input file for the list parameter."); m->mothurOutEndLine(); }
109 m->mothurOut("No valid current list file. You must provide a list file."); m->mothurOutEndLine();
112 }else if (listFile == "not open") { abort = true; }
114 //if the user changes the output directory command factory will send this info to us in the output parameter
115 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
117 outputDir += m->hasPath(listFile); //if user entered a file with a path then preserve it
120 //check for optional parameter and set defaults
121 // ...at some point should added some additional type checking...
122 label = validParameter.validFile(parameters, "label", false);
123 if (label == "not found") { m->mothurOut("label is a required parameter for the otu.hierarchy command."); m->mothurOutEndLine(); abort = true; }
125 m->splitAtDash(label, labels);
126 if (labels.size() != 2) { m->mothurOut("You must provide 2 labels."); m->mothurOutEndLine(); abort = true; }
129 output = validParameter.validFile(parameters, "output", false); if (output == "not found") { output = "name"; }
131 if ((output != "name") && (output != "number")) { m->mothurOut("output options are name and number. I will use name."); m->mothurOutEndLine(); output = "name"; }
135 catch(exception& e) {
136 m->errorOut(e, "OtuHierarchyCommand", "OtuHierarchyCommand");
140 //**********************************************************************************************************************
142 int OtuHierarchyCommand::execute(){
145 if (abort == true) { if (calledHelp) { return 0; } return 2; }
147 //get listvectors that correspond to labels requested, (or use smart distancing to get closest listvector)
148 vector<ListVector> lists = getListVectors();
150 if (m->control_pressed) { outputTypes.clear(); return 0; }
152 //determine which is little and which is big, putting little first
153 if (lists.size() == 2) {
154 //if big is first swap them
155 if (lists[0].getNumBins() < lists[1].getNumBins()) {
156 reverse(lists.begin(), lists.end());
159 m->mothurOut("error getting listvectors, unable to read 2 different vectors, check your label inputs."); m->mothurOutEndLine(); return 0;
162 //map sequences to bin number in the "little" otu
163 map<string, int> littleBins;
164 for (int i = 0; i < lists[0].getNumBins(); i++) {
166 if (m->control_pressed) { return 0; }
168 string names = lists[0].get(i);
171 while (names.find_first_of(',') != -1) {
172 string name = names.substr(0,names.find_first_of(','));
173 names = names.substr(names.find_first_of(',')+1, names.length());
174 littleBins[name] = i;
178 littleBins[names] = i;
182 string outputFileName = outputDir + m->getRootName(m->getSimpleName(listFile)) + lists[0].getLabel() + "-" + lists[1].getLabel() + ".otu.hierarchy";
183 m->openOutputFile(outputFileName, out);
185 //go through each bin in "big" otu and output the bins in "little" otu which created it
186 for (int i = 0; i < lists[1].getNumBins(); i++) {
188 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(outputFileName.c_str()); return 0; }
190 string names = lists[1].get(i);
193 if (output == "name") { out << names << '\t'; }
194 else { out << i << '\t'; }
196 map<int, int> bins; //bin numbers in little that are in this bin in big
197 map<int, int>::iterator it;
200 while (names.find_first_of(',') != -1) {
201 string name = names.substr(0,names.find_first_of(','));
202 names = names.substr(names.find_first_of(',')+1, names.length());
203 bins[littleBins[name]] = littleBins[name];
207 bins[littleBins[names]] = littleBins[names];
210 for (it = bins.begin(); it != bins.end(); it++) {
211 if (output == "name") { col2 += lists[0].get(it->first) + "\t"; }
212 else { col2 += toString(it->first) + "\t"; }
221 if (m->control_pressed) { outputTypes.clear(); remove(outputFileName.c_str()); return 0; }
223 m->mothurOutEndLine();
224 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
225 m->mothurOut(outputFileName); m->mothurOutEndLine(); outputNames.push_back(outputFileName); outputTypes["otuheirarchy"].push_back(outputFileName);
226 m->mothurOutEndLine();
230 catch(exception& e) {
231 m->errorOut(e, "OtuHierarchyCommand", "execute");
236 //**********************************************************************************************************************
237 //returns a vector of listVectors where "little" vector is first
238 vector<ListVector> OtuHierarchyCommand::getListVectors() {
241 int pos; //to use in smart distancing, position of last read in file
243 vector<ListVector> lists;
245 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
246 set<string> processedLabels;
247 set<string> userLabels = labels;
251 m->openInputFile(listFile, in);
253 //get first list vector in file
254 ListVector* list = NULL;
255 string lastLabel = "";
259 list = new ListVector(in);
261 lastLabel = list->getLabel();
264 while ((list != NULL) && (userLabels.size() != 0)) {
266 if (m->control_pressed) { in.close(); delete list; return lists; }
268 //is this a listvector that we want?
269 if(labels.count(list->getLabel()) == 1){
271 //make copy of listvector
272 ListVector temp(*list);
273 lists.push_back(temp);
275 processedLabels.insert(list->getLabel());
276 userLabels.erase(list->getLabel());
279 //you have a label the user want that is smaller than this label and the last label has not already been processed
280 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
281 string saveLabel = list->getLabel();
282 int savePos = in.tellg();
284 //get smart distance line
288 list = new ListVector(in);
289 }else { list = NULL; }
291 //make copy of listvector
292 ListVector temp(*list);
293 lists.push_back(temp);
295 processedLabels.insert(list->getLabel());
296 userLabels.erase(list->getLabel());
298 //restore real lastlabel to save below
299 list->setLabel(saveLabel);
303 lastLabel = list->getLabel();
310 list = new ListVector(in);
312 }else { list = NULL; }
315 if (m->control_pressed) { in.close(); return lists; }
317 //output error messages about any remaining user labels
318 set<string>::iterator it;
319 bool needToRun = false;
320 for (it = userLabels.begin(); it != userLabels.end(); it++) {
321 m->mothurOut("Your file does not include the label " + *it);
322 if (processedLabels.count(lastLabel) != 1) {
323 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
326 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
330 if (m->control_pressed) { in.close(); return lists; }
332 //run last label if you need to
333 if (needToRun == true) {
334 if (list != NULL) { delete list; }
338 list = new ListVector(in);
340 //make copy of listvector
341 ListVector temp(*list);
342 lists.push_back(temp);
351 catch(exception& e) {
352 m->errorOut(e, "OtuHierarchyCommand", "getListVectors");
357 //**********************************************************************************************************************