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(){
28 //initialize outputTypes
29 vector<string> tempOutNames;
30 outputTypes["otuheirarchy"] = tempOutNames;
33 m->errorOut(e, "OtuHierarchyCommand", "OtuHierarchyCommand");
37 //**********************************************************************************************************************
38 vector<string> OtuHierarchyCommand::getRequiredParameters(){
40 string Array[] = {"list","label"};
41 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
45 m->errorOut(e, "OtuHierarchyCommand", "getRequiredParameters");
49 //**********************************************************************************************************************
50 vector<string> OtuHierarchyCommand::getRequiredFiles(){
52 vector<string> myArray;
56 m->errorOut(e, "OtuHierarchyCommand", "getRequiredFiles");
60 //**********************************************************************************************************************
61 OtuHierarchyCommand::OtuHierarchyCommand(string option) {
65 //allow user to run help
66 if(option == "help") { help(); abort = true; }
69 //valid paramters for this command
70 string Array[] = {"list","label","output","outputdir","inputdir"};
71 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
73 OptionParser parser(option);
74 map<string,string> parameters = parser.getParameters();
76 ValidParameters validParameter;
77 map<string,string>::iterator it;
79 //check to make sure all parameters are valid for command
80 for (it = parameters.begin(); it != parameters.end(); it++) {
81 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
84 //initialize outputTypes
85 vector<string> tempOutNames;
86 outputTypes["otuheirarchy"] = tempOutNames;
88 //if the user changes the input directory command factory will send this info to us in the output parameter
89 string inputDir = validParameter.validFile(parameters, "inputdir", false);
90 if (inputDir == "not found"){ inputDir = ""; }
93 it = parameters.find("list");
94 //user has given a template file
95 if(it != parameters.end()){
96 path = m->hasPath(it->second);
97 //if the user has not given a path then, add inputdir. else leave path alone.
98 if (path == "") { parameters["list"] = inputDir + it->second; }
102 listFile = validParameter.validFile(parameters, "list", true);
103 if (listFile == "not found") { m->mothurOut("list is a required parameter for the otu.hierarchy command."); m->mothurOutEndLine(); abort = true; }
104 else if (listFile == "not open") { abort = true; }
106 //if the user changes the output directory command factory will send this info to us in the output parameter
107 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
109 outputDir += m->hasPath(listFile); //if user entered a file with a path then preserve it
112 //check for optional parameter and set defaults
113 // ...at some point should added some additional type checking...
114 label = validParameter.validFile(parameters, "label", false);
115 if (label == "not found") { m->mothurOut("label is a required parameter for the otu.hierarchy command."); m->mothurOutEndLine(); abort = true; }
117 m->splitAtDash(label, labels);
118 if (labels.size() != 2) { m->mothurOut("You must provide 2 labels."); m->mothurOutEndLine(); abort = true; }
121 output = validParameter.validFile(parameters, "output", false); if (output == "not found") { output = "name"; }
123 if ((output != "name") && (output != "number")) { m->mothurOut("output options are name and number. I will use name."); m->mothurOutEndLine(); output = "name"; }
127 catch(exception& e) {
128 m->errorOut(e, "OtuHierarchyCommand", "OtuHierarchyCommand");
132 //**********************************************************************************************************************
134 void OtuHierarchyCommand::help(){
136 m->mothurOut("The otu.hierarchy command is used to see how otus relate at two distances. \n");
137 m->mothurOut("The otu.hierarchy command parameters are list, label and output. list and label parameters are required. \n");
138 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");
139 m->mothurOut("The otu.hierarchy command should be in the following format: \n");
140 m->mothurOut("otu.hierarchy(list=yourListFile, label=yourLabels).\n");
141 m->mothurOut("Example otu.hierarchy(list=amazon.fn.list, label=0.01-0.03).\n");
142 m->mothurOut("The otu.hierarchy command outputs a .otu.hierarchy file which is described on the wiki.\n");
143 m->mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n\n");
145 catch(exception& e) {
146 m->errorOut(e, "OtuHierarchyCommand", "help");
151 //**********************************************************************************************************************
153 OtuHierarchyCommand::~OtuHierarchyCommand(){}
155 //**********************************************************************************************************************
157 int OtuHierarchyCommand::execute(){
160 if (abort == true) { return 0; }
162 //get listvectors that correspond to labels requested, (or use smart distancing to get closest listvector)
163 vector<ListVector> lists = getListVectors();
165 if (m->control_pressed) { outputTypes.clear(); return 0; }
167 //determine which is little and which is big, putting little first
168 if (lists.size() == 2) {
169 //if big is first swap them
170 if (lists[0].getNumBins() < lists[1].getNumBins()) {
171 reverse(lists.begin(), lists.end());
174 m->mothurOut("error getting listvectors, unable to read 2 different vectors, check your label inputs."); m->mothurOutEndLine(); return 0;
177 //map sequences to bin number in the "little" otu
178 map<string, int> littleBins;
179 for (int i = 0; i < lists[0].getNumBins(); i++) {
181 if (m->control_pressed) { return 0; }
183 string names = lists[0].get(i);
186 while (names.find_first_of(',') != -1) {
187 string name = names.substr(0,names.find_first_of(','));
188 names = names.substr(names.find_first_of(',')+1, names.length());
189 littleBins[name] = i;
193 littleBins[names] = i;
197 string outputFileName = outputDir + m->getRootName(m->getSimpleName(listFile)) + lists[0].getLabel() + "-" + lists[1].getLabel() + ".otu.hierarchy";
198 m->openOutputFile(outputFileName, out);
200 //go through each bin in "big" otu and output the bins in "little" otu which created it
201 for (int i = 0; i < lists[1].getNumBins(); i++) {
203 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(outputFileName.c_str()); return 0; }
205 string names = lists[1].get(i);
208 if (output == "name") { out << names << '\t'; }
209 else { out << i << '\t'; }
211 map<int, int> bins; //bin numbers in little that are in this bin in big
212 map<int, int>::iterator it;
215 while (names.find_first_of(',') != -1) {
216 string name = names.substr(0,names.find_first_of(','));
217 names = names.substr(names.find_first_of(',')+1, names.length());
218 bins[littleBins[name]] = littleBins[name];
222 bins[littleBins[names]] = littleBins[names];
225 for (it = bins.begin(); it != bins.end(); it++) {
226 if (output == "name") { col2 += lists[0].get(it->first) + "\t"; }
227 else { col2 += toString(it->first) + "\t"; }
236 if (m->control_pressed) { outputTypes.clear(); remove(outputFileName.c_str()); return 0; }
238 m->mothurOutEndLine();
239 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
240 m->mothurOut(outputFileName); m->mothurOutEndLine(); outputNames.push_back(outputFileName); outputTypes["otuheirarchy"].push_back(outputFileName);
241 m->mothurOutEndLine();
245 catch(exception& e) {
246 m->errorOut(e, "OtuHierarchyCommand", "execute");
251 //**********************************************************************************************************************
252 //returns a vector of listVectors where "little" vector is first
253 vector<ListVector> OtuHierarchyCommand::getListVectors() {
256 int pos; //to use in smart distancing, position of last read in file
258 vector<ListVector> lists;
260 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
261 set<string> processedLabels;
262 set<string> userLabels = labels;
266 m->openInputFile(listFile, in);
268 //get first list vector in file
269 ListVector* list = NULL;
270 string lastLabel = "";
274 list = new ListVector(in);
276 lastLabel = list->getLabel();
279 while ((list != NULL) && (userLabels.size() != 0)) {
281 if (m->control_pressed) { in.close(); delete list; return lists; }
283 //is this a listvector that we want?
284 if(labels.count(list->getLabel()) == 1){
286 //make copy of listvector
287 ListVector temp(*list);
288 lists.push_back(temp);
290 processedLabels.insert(list->getLabel());
291 userLabels.erase(list->getLabel());
294 //you have a label the user want that is smaller than this label and the last label has not already been processed
295 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
296 string saveLabel = list->getLabel();
297 int savePos = in.tellg();
299 //get smart distance line
303 list = new ListVector(in);
304 }else { list = NULL; }
306 //make copy of listvector
307 ListVector temp(*list);
308 lists.push_back(temp);
310 processedLabels.insert(list->getLabel());
311 userLabels.erase(list->getLabel());
313 //restore real lastlabel to save below
314 list->setLabel(saveLabel);
318 lastLabel = list->getLabel();
325 list = new ListVector(in);
327 }else { list = NULL; }
330 if (m->control_pressed) { in.close(); return lists; }
332 //output error messages about any remaining user labels
333 set<string>::iterator it;
334 bool needToRun = false;
335 for (it = userLabels.begin(); it != userLabels.end(); it++) {
336 m->mothurOut("Your file does not include the label " + *it);
337 if (processedLabels.count(lastLabel) != 1) {
338 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
341 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
345 if (m->control_pressed) { in.close(); return lists; }
347 //run last label if you need to
348 if (needToRun == true) {
349 if (list != NULL) { delete list; }
353 list = new ListVector(in);
355 //make copy of listvector
356 ListVector temp(*list);
357 lists.push_back(temp);
366 catch(exception& e) {
367 m->errorOut(e, "OtuHierarchyCommand", "getListVectors");
372 //**********************************************************************************************************************