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 OtuHierarchyCommand::OtuHierarchyCommand(string option){
16 //allow user to run help
17 if(option == "help") { help(); abort = true; }
20 //valid paramters for this command
21 string Array[] = {"list","label","outputdir","inputdir"};
22 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
24 OptionParser parser(option);
25 map<string,string> parameters = parser.getParameters();
27 ValidParameters validParameter;
28 map<string,string>::iterator it;
30 //check to make sure all parameters are valid for command
31 for (it = parameters.begin(); it != parameters.end(); it++) {
32 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
35 //if the user changes the input directory command factory will send this info to us in the output parameter
36 string inputDir = validParameter.validFile(parameters, "inputdir", false);
37 if (inputDir == "not found"){ inputDir = ""; }
40 it = parameters.find("list");
41 //user has given a template file
42 if(it != parameters.end()){
43 path = hasPath(it->second);
44 //if the user has not given a path then, add inputdir. else leave path alone.
45 if (path == "") { parameters["list"] = inputDir + it->second; }
49 listFile = validParameter.validFile(parameters, "list", true);
50 if (listFile == "not found") { mothurOut("list is a required parameter for the otu.hierarchy command."); mothurOutEndLine(); abort = true; }
51 else if (listFile == "not open") { abort = true; }
53 //if the user changes the output directory command factory will send this info to us in the output parameter
54 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
56 outputDir += hasPath(listFile); //if user entered a file with a path then preserve it
59 //check for optional parameter and set defaults
60 // ...at some point should added some additional type checking...
61 label = validParameter.validFile(parameters, "label", false);
62 if (label == "not found") { mothurOut("label is a required parameter for the otu.hierarchy command."); mothurOutEndLine(); abort = true; }
64 splitAtDash(label, labels);
65 if (labels.size() != 2) { mothurOut("You must provide 2 labels."); mothurOutEndLine(); abort = true; }
71 errorOut(e, "OtuHierarchyCommand", "OtuHierarchyCommand");
75 //**********************************************************************************************************************
77 void OtuHierarchyCommand::help(){
79 mothurOut("The otu.hierarchy command is used to see how otus relate at two distances. \n");
80 mothurOut("The otu.hierarchy command parameters are list and label. Both parameters are required. \n");
81 mothurOut("The otu.hierarchy command should be in the following format: \n");
82 mothurOut("otu.hierarchy(list=yourListFile, label=yourLabels).\n");
83 mothurOut("Example otu.hierarchy(list=amazon.fn.list, label=0.01-0.03).\n");
84 mothurOut("The otu.hierarchy command outputs a .otu.hierarchy file which is described on the wiki.\n");
85 mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n\n");
88 errorOut(e, "OtuHierarchyCommand", "help");
93 //**********************************************************************************************************************
95 OtuHierarchyCommand::~OtuHierarchyCommand(){}
97 //**********************************************************************************************************************
99 int OtuHierarchyCommand::execute(){
102 if (abort == true) { return 0; }
104 //get listvectors that correspond to labels requested, (or use smart distancing to get closest listvector)
105 vector<ListVector> lists = getListVectors();
107 //determine which is little and which is big, putting little first
108 if (lists.size() == 2) {
109 //if big is first swap them
110 if (lists[0].getNumBins() < lists[1].getNumBins()) {
111 reverse(lists.begin(), lists.end());
114 mothurOut("error getting listvectors, unable to read 2 different vectors, check your label inputs."); mothurOutEndLine(); return 0;
117 //map sequences to bin number in the "little" otu
118 map<string, int> littleBins;
119 for (int i = 0; i < lists[0].getNumBins(); i++) {
120 string names = lists[0].get(i);
123 while (names.find_first_of(',') != -1) {
124 string name = names.substr(0,names.find_first_of(','));
125 names = names.substr(names.find_first_of(',')+1, names.length());
126 littleBins[name] = i;
130 littleBins[names] = i;
134 string outputFileName = outputDir + getRootName(getSimpleName(listFile)) + lists[0].getLabel() + "-" + lists[1].getLabel() + ".otu.hierarchy";
135 openOutputFile(outputFileName, out);
137 //go through each bin in "big" otu and output the bins in "little" otu which created it
138 for (int i = 0; i < lists[1].getNumBins(); i++) {
140 string names = lists[1].get(i);
143 out << names << '\t';
145 map<int, int> bins; //bin numbers in little that are in this bin in big
146 map<int, int>::iterator it;
149 while (names.find_first_of(',') != -1) {
150 string name = names.substr(0,names.find_first_of(','));
151 names = names.substr(names.find_first_of(',')+1, names.length());
152 bins[littleBins[name]] = littleBins[name];
156 bins[littleBins[names]] = littleBins[names];
159 for (it = bins.begin(); it != bins.end(); it++) {
160 col2 += lists[0].get(it->first) + "\t";
171 catch(exception& e) {
172 errorOut(e, "OtuHierarchyCommand", "execute");
177 //**********************************************************************************************************************
178 //returns a vector of listVectors where "little" vector is first
179 vector<ListVector> OtuHierarchyCommand::getListVectors() {
182 int pos; //to use in smart distancing, position of last read in file
184 vector<ListVector> lists;
186 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
187 set<string> processedLabels;
188 set<string> userLabels = labels;
192 openInputFile(listFile, in);
194 //get first list vector in file
195 ListVector* list = NULL;
196 string lastLabel = "";
200 list = new ListVector(in);
202 lastLabel = list->getLabel();
205 while ((list != NULL) && (userLabels.size() != 0)) {
207 //is this a listvector that we want?
208 if(labels.count(list->getLabel()) == 1){
210 //make copy of listvector
211 ListVector temp(*list);
212 lists.push_back(temp);
214 processedLabels.insert(list->getLabel());
215 userLabels.erase(list->getLabel());
218 //you have a label the user want that is smaller than this label and the last label has not already been processed
219 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
220 string saveLabel = list->getLabel();
221 int savePos = in.tellg();
223 //get smart distance line
227 list = new ListVector(in);
228 }else { list = NULL; }
230 //make copy of listvector
231 ListVector temp(*list);
232 lists.push_back(temp);
234 processedLabels.insert(list->getLabel());
235 userLabels.erase(list->getLabel());
237 //restore real lastlabel to save below
238 list->setLabel(saveLabel);
242 lastLabel = list->getLabel();
249 list = new ListVector(in);
251 }else { list = NULL; }
256 //output error messages about any remaining user labels
257 set<string>::iterator it;
258 bool needToRun = false;
259 for (it = userLabels.begin(); it != userLabels.end(); it++) {
260 mothurOut("Your file does not include the label " + *it);
261 if (processedLabels.count(lastLabel) != 1) {
262 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
265 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
269 //run last label if you need to
270 if (needToRun == true) {
271 if (list != NULL) { delete list; }
275 list = new ListVector(in);
277 //make copy of listvector
278 ListVector temp(*list);
279 lists.push_back(temp);
288 catch(exception& e) {
289 errorOut(e, "OtuHierarchyCommand", "getListVectors");
294 //**********************************************************************************************************************