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::getValidParameters(){
15 string Array[] = {"list","label","output","outputdir","inputdir"};
16 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
20 m->errorOut(e, "OtuHierarchyCommand", "getValidParameters");
24 //**********************************************************************************************************************
25 OtuHierarchyCommand::OtuHierarchyCommand(){
27 abort = true; calledHelp = true;
28 vector<string> tempOutNames;
29 outputTypes["otuheirarchy"] = tempOutNames;
32 m->errorOut(e, "OtuHierarchyCommand", "OtuHierarchyCommand");
36 //**********************************************************************************************************************
37 vector<string> OtuHierarchyCommand::getRequiredParameters(){
39 string Array[] = {"list","label"};
40 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
44 m->errorOut(e, "OtuHierarchyCommand", "getRequiredParameters");
48 //**********************************************************************************************************************
49 vector<string> OtuHierarchyCommand::getRequiredFiles(){
51 vector<string> myArray;
55 m->errorOut(e, "OtuHierarchyCommand", "getRequiredFiles");
59 //**********************************************************************************************************************
60 OtuHierarchyCommand::OtuHierarchyCommand(string option) {
62 abort = false; calledHelp = false;
64 //allow user to run help
65 if(option == "help") { help(); abort = true; calledHelp = true; }
68 //valid paramters for this command
69 string Array[] = {"list","label","output","outputdir","inputdir"};
70 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
72 OptionParser parser(option);
73 map<string,string> parameters = parser.getParameters();
75 ValidParameters validParameter;
76 map<string,string>::iterator it;
78 //check to make sure all parameters are valid for command
79 for (it = parameters.begin(); it != parameters.end(); it++) {
80 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
83 //initialize outputTypes
84 vector<string> tempOutNames;
85 outputTypes["otuheirarchy"] = tempOutNames;
87 //if the user changes the input directory command factory will send this info to us in the output parameter
88 string inputDir = validParameter.validFile(parameters, "inputdir", false);
89 if (inputDir == "not found"){ inputDir = ""; }
92 it = parameters.find("list");
93 //user has given a template file
94 if(it != parameters.end()){
95 path = m->hasPath(it->second);
96 //if the user has not given a path then, add inputdir. else leave path alone.
97 if (path == "") { parameters["list"] = inputDir + it->second; }
101 listFile = validParameter.validFile(parameters, "list", true);
102 if (listFile == "not found") { m->mothurOut("list is a required parameter for the otu.hierarchy command."); m->mothurOutEndLine(); abort = true; }
103 else if (listFile == "not open") { abort = true; }
105 //if the user changes the output directory command factory will send this info to us in the output parameter
106 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
108 outputDir += m->hasPath(listFile); //if user entered a file with a path then preserve it
111 //check for optional parameter and set defaults
112 // ...at some point should added some additional type checking...
113 label = validParameter.validFile(parameters, "label", false);
114 if (label == "not found") { m->mothurOut("label is a required parameter for the otu.hierarchy command."); m->mothurOutEndLine(); abort = true; }
116 m->splitAtDash(label, labels);
117 if (labels.size() != 2) { m->mothurOut("You must provide 2 labels."); m->mothurOutEndLine(); abort = true; }
120 output = validParameter.validFile(parameters, "output", false); if (output == "not found") { output = "name"; }
122 if ((output != "name") && (output != "number")) { m->mothurOut("output options are name and number. I will use name."); m->mothurOutEndLine(); output = "name"; }
126 catch(exception& e) {
127 m->errorOut(e, "OtuHierarchyCommand", "OtuHierarchyCommand");
131 //**********************************************************************************************************************
133 void OtuHierarchyCommand::help(){
135 m->mothurOut("The otu.hierarchy command is used to see how otus relate at two distances. \n");
136 m->mothurOut("The otu.hierarchy command parameters are list, label and output. list and label parameters are required. \n");
137 m->mothurOut("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");
138 m->mothurOut("The otu.hierarchy command should be in the following format: \n");
139 m->mothurOut("otu.hierarchy(list=yourListFile, label=yourLabels).\n");
140 m->mothurOut("Example otu.hierarchy(list=amazon.fn.list, label=0.01-0.03).\n");
141 m->mothurOut("The otu.hierarchy command outputs a .otu.hierarchy file which is described on the wiki.\n");
142 m->mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n\n");
144 catch(exception& e) {
145 m->errorOut(e, "OtuHierarchyCommand", "help");
150 //**********************************************************************************************************************
152 OtuHierarchyCommand::~OtuHierarchyCommand(){}
154 //**********************************************************************************************************************
156 int OtuHierarchyCommand::execute(){
159 if (abort == true) { if (calledHelp) { return 0; } return 2; }
161 //get listvectors that correspond to labels requested, (or use smart distancing to get closest listvector)
162 vector<ListVector> lists = getListVectors();
164 if (m->control_pressed) { outputTypes.clear(); return 0; }
166 //determine which is little and which is big, putting little first
167 if (lists.size() == 2) {
168 //if big is first swap them
169 if (lists[0].getNumBins() < lists[1].getNumBins()) {
170 reverse(lists.begin(), lists.end());
173 m->mothurOut("error getting listvectors, unable to read 2 different vectors, check your label inputs."); m->mothurOutEndLine(); return 0;
176 //map sequences to bin number in the "little" otu
177 map<string, int> littleBins;
178 for (int i = 0; i < lists[0].getNumBins(); i++) {
180 if (m->control_pressed) { return 0; }
182 string names = lists[0].get(i);
185 while (names.find_first_of(',') != -1) {
186 string name = names.substr(0,names.find_first_of(','));
187 names = names.substr(names.find_first_of(',')+1, names.length());
188 littleBins[name] = i;
192 littleBins[names] = i;
196 string outputFileName = outputDir + m->getRootName(m->getSimpleName(listFile)) + lists[0].getLabel() + "-" + lists[1].getLabel() + ".otu.hierarchy";
197 m->openOutputFile(outputFileName, out);
199 //go through each bin in "big" otu and output the bins in "little" otu which created it
200 for (int i = 0; i < lists[1].getNumBins(); i++) {
202 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(outputFileName.c_str()); return 0; }
204 string names = lists[1].get(i);
207 if (output == "name") { out << names << '\t'; }
208 else { out << i << '\t'; }
210 map<int, int> bins; //bin numbers in little that are in this bin in big
211 map<int, int>::iterator it;
214 while (names.find_first_of(',') != -1) {
215 string name = names.substr(0,names.find_first_of(','));
216 names = names.substr(names.find_first_of(',')+1, names.length());
217 bins[littleBins[name]] = littleBins[name];
221 bins[littleBins[names]] = littleBins[names];
224 for (it = bins.begin(); it != bins.end(); it++) {
225 if (output == "name") { col2 += lists[0].get(it->first) + "\t"; }
226 else { col2 += toString(it->first) + "\t"; }
235 if (m->control_pressed) { outputTypes.clear(); remove(outputFileName.c_str()); return 0; }
237 m->mothurOutEndLine();
238 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
239 m->mothurOut(outputFileName); m->mothurOutEndLine(); outputNames.push_back(outputFileName); outputTypes["otuheirarchy"].push_back(outputFileName);
240 m->mothurOutEndLine();
244 catch(exception& e) {
245 m->errorOut(e, "OtuHierarchyCommand", "execute");
250 //**********************************************************************************************************************
251 //returns a vector of listVectors where "little" vector is first
252 vector<ListVector> OtuHierarchyCommand::getListVectors() {
255 int pos; //to use in smart distancing, position of last read in file
257 vector<ListVector> lists;
259 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
260 set<string> processedLabels;
261 set<string> userLabels = labels;
265 m->openInputFile(listFile, in);
267 //get first list vector in file
268 ListVector* list = NULL;
269 string lastLabel = "";
273 list = new ListVector(in);
275 lastLabel = list->getLabel();
278 while ((list != NULL) && (userLabels.size() != 0)) {
280 if (m->control_pressed) { in.close(); delete list; return lists; }
282 //is this a listvector that we want?
283 if(labels.count(list->getLabel()) == 1){
285 //make copy of listvector
286 ListVector temp(*list);
287 lists.push_back(temp);
289 processedLabels.insert(list->getLabel());
290 userLabels.erase(list->getLabel());
293 //you have a label the user want that is smaller than this label and the last label has not already been processed
294 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
295 string saveLabel = list->getLabel();
296 int savePos = in.tellg();
298 //get smart distance line
302 list = new ListVector(in);
303 }else { list = NULL; }
305 //make copy of listvector
306 ListVector temp(*list);
307 lists.push_back(temp);
309 processedLabels.insert(list->getLabel());
310 userLabels.erase(list->getLabel());
312 //restore real lastlabel to save below
313 list->setLabel(saveLabel);
317 lastLabel = list->getLabel();
324 list = new ListVector(in);
326 }else { list = NULL; }
329 if (m->control_pressed) { in.close(); return lists; }
331 //output error messages about any remaining user labels
332 set<string>::iterator it;
333 bool needToRun = false;
334 for (it = userLabels.begin(); it != userLabels.end(); it++) {
335 m->mothurOut("Your file does not include the label " + *it);
336 if (processedLabels.count(lastLabel) != 1) {
337 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
340 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
344 if (m->control_pressed) { in.close(); return lists; }
346 //run last label if you need to
347 if (needToRun == true) {
348 if (list != NULL) { delete list; }
352 list = new ListVector(in);
354 //make copy of listvector
355 ListVector temp(*list);
356 lists.push_back(temp);
365 catch(exception& e) {
366 m->errorOut(e, "OtuHierarchyCommand", "getListVectors");
371 //**********************************************************************************************************************