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"};
22 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
24 OptionParser parser(option);
25 map<string,string> parameters = parser.getParameters();
27 ValidParameters validParameter;
29 //check to make sure all parameters are valid for command
30 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
31 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
34 listFile = validParameter.validFile(parameters, "list", true);
35 if (listFile == "not found") { mothurOut("list is a required parameter for the otu.hierarchy command."); mothurOutEndLine(); abort = true; }
36 else if (listFile == "not open") { abort = true; }
39 //check for optional parameter and set defaults
40 // ...at some point should added some additional type checking...
41 label = validParameter.validFile(parameters, "label", false);
42 if (label == "not found") { mothurOut("label is a required parameter for the otu.hierarchy command."); mothurOutEndLine(); abort = true; }
44 splitAtDash(label, labels);
45 if (labels.size() != 2) { mothurOut("You must provide 2 labels."); mothurOutEndLine(); abort = true; }
51 errorOut(e, "OtuHierarchyCommand", "OtuHierarchyCommand");
55 //**********************************************************************************************************************
57 void OtuHierarchyCommand::help(){
59 mothurOut("The otu.hierarchy command is used to see how otus relate at two distances. \n");
60 mothurOut("The otu.hierarchy command parameters are list and label. Both parameters are required. \n");
61 mothurOut("The otu.hierarchy command should be in the following format: \n");
62 mothurOut("otu.hierarchy(list=yourListFile, label=yourLabels).\n");
63 mothurOut("Example otu.hierarchy(list=amazon.fn.list, label=0.01-0.03).\n");
64 mothurOut("The otu.hierarchy command outputs a .otu.hierarchy file which is described on the wiki.\n");
65 mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n\n");
68 errorOut(e, "OtuHierarchyCommand", "help");
73 //**********************************************************************************************************************
75 OtuHierarchyCommand::~OtuHierarchyCommand(){}
77 //**********************************************************************************************************************
79 int OtuHierarchyCommand::execute(){
82 if (abort == true) { return 0; }
84 //get listvectors that correspond to labels requested, (or use smart distancing to get closest listvector)
85 vector<ListVector> lists = getListVectors();
87 //determine which is little and which is big, putting little first
88 if (lists.size() == 2) {
89 //if big is first swap them
90 if (lists[0].getNumBins() < lists[1].getNumBins()) {
91 reverse(lists.begin(), lists.end());
94 mothurOut("error getting listvectors, unable to read 2 different vectors, check your label inputs."); mothurOutEndLine(); return 0;
97 //map sequences to bin number in the "little" otu
98 map<string, int> littleBins;
99 for (int i = 0; i < lists[0].getNumBins(); i++) {
100 string names = lists[0].get(i);
103 while (names.find_first_of(',') != -1) {
104 string name = names.substr(0,names.find_first_of(','));
105 names = names.substr(names.find_first_of(',')+1, names.length());
106 littleBins[name] = i;
110 littleBins[names] = i;
114 string outputFileName = getRootName(listFile) + lists[0].getLabel() + "-" + lists[1].getLabel() + ".otu.hierarchy";
115 openOutputFile(outputFileName, out);
117 //go through each bin in "big" otu and output the bins in "little" otu which created it
118 for (int i = 0; i < lists[1].getNumBins(); i++) {
120 string names = lists[1].get(i);
123 out << names << '\t';
125 map<int, int> bins; //bin numbers in little that are in this bin in big
126 map<int, int>::iterator it;
129 while (names.find_first_of(',') != -1) {
130 string name = names.substr(0,names.find_first_of(','));
131 names = names.substr(names.find_first_of(',')+1, names.length());
132 bins[littleBins[name]] = littleBins[name];
136 bins[littleBins[names]] = littleBins[names];
139 for (it = bins.begin(); it != bins.end(); it++) {
140 col2 += lists[0].get(it->first) + "\t";
151 catch(exception& e) {
152 errorOut(e, "OtuHierarchyCommand", "execute");
157 //**********************************************************************************************************************
158 //returns a vector of listVectors where "little" vector is first
159 vector<ListVector> OtuHierarchyCommand::getListVectors() {
162 int pos; //to use in smart distancing, position of last read in file
164 vector<ListVector> lists;
166 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
167 set<string> processedLabels;
168 set<string> userLabels = labels;
172 openInputFile(listFile, in);
174 //get first list vector in file
175 ListVector* list = NULL;
176 string lastLabel = "";
180 list = new ListVector(in);
182 lastLabel = list->getLabel();
185 while ((list != NULL) && (userLabels.size() != 0)) {
187 //is this a listvector that we want?
188 if(labels.count(list->getLabel()) == 1){
190 //make copy of listvector
191 ListVector temp(*list);
192 lists.push_back(temp);
194 processedLabels.insert(list->getLabel());
195 userLabels.erase(list->getLabel());
198 //you have a label the user want that is smaller than this label and the last label has not already been processed
199 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
200 string saveLabel = list->getLabel();
201 int savePos = in.tellg();
203 //get smart distance line
207 list = new ListVector(in);
208 }else { list = NULL; }
210 //make copy of listvector
211 ListVector temp(*list);
212 lists.push_back(temp);
214 processedLabels.insert(list->getLabel());
215 userLabels.erase(list->getLabel());
217 //restore real lastlabel to save below
218 list->setLabel(saveLabel);
222 lastLabel = list->getLabel();
229 list = new ListVector(in);
231 }else { list = NULL; }
236 //output error messages about any remaining user labels
237 set<string>::iterator it;
238 bool needToRun = false;
239 for (it = userLabels.begin(); it != userLabels.end(); it++) {
240 mothurOut("Your file does not include the label " + *it);
241 if (processedLabels.count(lastLabel) != 1) {
242 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
245 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
249 //run last label if you need to
250 if (needToRun == true) {
251 if (list != NULL) { delete list; }
255 list = new ListVector(in);
257 //make copy of listvector
258 ListVector temp(*list);
259 lists.push_back(temp);
268 catch(exception& e) {
269 errorOut(e, "OtuHierarchyCommand", "getListVectors");
274 //**********************************************************************************************************************