]> git.donarmstrong.com Git - mothur.git/blob - otuhierarchycommand.cpp
fixes while testing
[mothur.git] / otuhierarchycommand.cpp
1 /*
2  *  otuhierarchycommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 1/19/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "otuhierarchycommand.h"
11
12 //**********************************************************************************************************************
13 vector<string> OtuHierarchyCommand::getValidParameters(){       
14         try {
15                 string Array[] =  {"list","label","output","outputdir","inputdir"};
16                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
17                 return myArray;
18         }
19         catch(exception& e) {
20                 m->errorOut(e, "OtuHierarchyCommand", "getValidParameters");
21                 exit(1);
22         }
23 }
24 //**********************************************************************************************************************
25 OtuHierarchyCommand::OtuHierarchyCommand(){     
26         try {
27                 abort = true;
28                 //initialize outputTypes
29                 vector<string> tempOutNames;
30                 outputTypes["otuheirarchy"] = tempOutNames;
31         }
32         catch(exception& e) {
33                 m->errorOut(e, "OtuHierarchyCommand", "OtuHierarchyCommand");
34                 exit(1);
35         }
36 }
37 //**********************************************************************************************************************
38 vector<string> OtuHierarchyCommand::getRequiredParameters(){    
39         try {
40                 string Array[] =  {"list","label"};
41                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
42                 return myArray;
43         }
44         catch(exception& e) {
45                 m->errorOut(e, "OtuHierarchyCommand", "getRequiredParameters");
46                 exit(1);
47         }
48 }
49 //**********************************************************************************************************************
50 vector<string> OtuHierarchyCommand::getRequiredFiles(){ 
51         try {
52                 vector<string> myArray;
53                 return myArray;
54         }
55         catch(exception& e) {
56                 m->errorOut(e, "OtuHierarchyCommand", "getRequiredFiles");
57                 exit(1);
58         }
59 }
60 //**********************************************************************************************************************
61 OtuHierarchyCommand::OtuHierarchyCommand(string option) {
62         try {
63                 abort = false;
64                 
65                 //allow user to run help
66                 if(option == "help") {  help(); abort = true; }
67                 
68                 else {
69                         //valid paramters for this command
70                         string Array[] =  {"list","label","output","outputdir","inputdir"};
71                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
72                         
73                         OptionParser parser(option);
74                         map<string,string> parameters = parser.getParameters();
75                         
76                         ValidParameters validParameter;
77                         map<string,string>::iterator it;
78                 
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;  }
82                         }
83                         
84                         //initialize outputTypes
85                         vector<string> tempOutNames;
86                         outputTypes["otuheirarchy"] = tempOutNames;
87                         
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 = "";          }
91                         else {
92                                 string path;
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;             }
99                                 }
100                         }
101
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; }      
105                         
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"){  
108                                 outputDir = ""; 
109                                 outputDir += m->hasPath(listFile); //if user entered a file with a path then preserve it        
110                         }
111                         
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; }
116                         else { 
117                                 m->splitAtDash(label, labels);
118                                 if (labels.size() != 2) { m->mothurOut("You must provide 2 labels."); m->mothurOutEndLine(); abort = true; }
119                         }       
120                         
121                         output = validParameter.validFile(parameters, "output", false);                 if (output == "not found") { output = "name"; }
122                         
123                         if ((output != "name") && (output != "number")) { m->mothurOut("output options are name and number. I will use name."); m->mothurOutEndLine(); output = "name"; }
124                 }
125                 
126         }
127         catch(exception& e) {
128                 m->errorOut(e, "OtuHierarchyCommand", "OtuHierarchyCommand");
129                 exit(1);
130         }                       
131 }
132 //**********************************************************************************************************************
133
134 void OtuHierarchyCommand::help(){
135         try {
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");
144         }
145         catch(exception& e) {
146                 m->errorOut(e, "OtuHierarchyCommand", "help");
147                 exit(1);
148         }
149 }
150
151 //**********************************************************************************************************************
152
153 OtuHierarchyCommand::~OtuHierarchyCommand(){}
154
155 //**********************************************************************************************************************
156
157 int OtuHierarchyCommand::execute(){
158         try {
159                 
160                 if (abort == true) { return 0; }
161                 
162                 //get listvectors that correspond to labels requested, (or use smart distancing to get closest listvector)
163                 vector<ListVector> lists = getListVectors();
164                 
165                 if (m->control_pressed) { outputTypes.clear(); return 0; }
166                 
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());
172                         }
173                 }else{
174                         m->mothurOut("error getting listvectors, unable to read 2 different vectors, check your label inputs."); m->mothurOutEndLine(); return 0;
175                 }
176                 
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++) {
180                 
181                         if (m->control_pressed) {  return 0; }
182                         
183                         string names = lists[0].get(i); 
184                         
185                         //parse bin
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;  
190                         }
191                         
192                         //get last name
193                         littleBins[names] = i;
194                 }
195                 
196                 ofstream out;
197                 string outputFileName = outputDir + m->getRootName(m->getSimpleName(listFile)) + lists[0].getLabel() + "-" + lists[1].getLabel() + ".otu.hierarchy";
198                 m->openOutputFile(outputFileName, out);
199                 
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++) {
202                 
203                         if (m->control_pressed) { outputTypes.clear(); out.close(); remove(outputFileName.c_str()); return 0; }
204                         
205                         string names = lists[1].get(i);
206                         
207                         //output column 1
208                         if (output == "name")   {   out << names << '\t';       }
209                         else                                    {       out << i << '\t';               }
210                         
211                         map<int, int> bins; //bin numbers in little that are in this bin in big
212                         map<int, int>::iterator it;
213                         
214                         //parse bin
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];  
219                         }
220                         
221                         //get last name
222                         bins[littleBins[names]] = littleBins[names]; 
223                         
224                         string col2 = "";
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";             }
228                         }
229                         
230                         //output column 2
231                         out << col2 << endl;
232                 }
233                 
234                 out.close();
235                 
236                 if (m->control_pressed) { outputTypes.clear(); remove(outputFileName.c_str()); return 0; }
237                 
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();
242                 
243                 return 0;
244         }
245         catch(exception& e) {
246                 m->errorOut(e, "OtuHierarchyCommand", "execute");
247                 exit(1);
248         }
249 }
250
251 //**********************************************************************************************************************
252 //returns a vector of listVectors where "little" vector is first
253 vector<ListVector> OtuHierarchyCommand::getListVectors() {
254         try {
255                 
256                 int pos; //to use in smart distancing, position of last read in file
257                 int lastPos;
258                 vector<ListVector> lists;
259                 
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;
263
264                 //open file
265                 ifstream in;
266                 m->openInputFile(listFile, in);
267                 
268                 //get first list vector in file
269                 ListVector* list = NULL;
270                 string lastLabel = "";
271                 if (!in.eof())  {
272                         pos = in.tellg();
273                         lastPos = pos;
274                         list = new ListVector(in);  
275                         m->gobble(in);
276                         lastLabel = list->getLabel();
277                 }
278                 
279                 while ((list != NULL) && (userLabels.size() != 0)) {
280                 
281                         if (m->control_pressed) {  in.close(); delete list;  return lists; }
282                         
283                         //is this a listvector that we want?
284                         if(labels.count(list->getLabel()) == 1){
285                                 
286                                 //make copy of listvector
287                                 ListVector temp(*list);
288                                 lists.push_back(temp);
289                         
290                                 processedLabels.insert(list->getLabel());
291                                 userLabels.erase(list->getLabel());
292                         }
293                 
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();
298                                 
299                                 //get smart distance line
300                                 delete list;
301                                 in.seekg(lastPos);
302                                 if (!in.eof())  {       
303                                         list = new ListVector(in);  
304                                 }else { list = NULL; }
305                                 
306                                 //make copy of listvector
307                                 ListVector temp(*list);
308                                 lists.push_back(temp);
309                                         
310                                 processedLabels.insert(list->getLabel());
311                                 userLabels.erase(list->getLabel());                                     
312                                                                                 
313                                 //restore real lastlabel to save below
314                                 list->setLabel(saveLabel);
315                                 in.seekg(savePos);
316                         }
317                         
318                         lastLabel = list->getLabel();
319                         lastPos = pos;
320                         
321                         //get next line
322                         delete list;
323                         if (!in.eof())  {       
324                                 pos = in.tellg();
325                                 list = new ListVector(in);  
326                                 m->gobble(in);
327                         }else { list = NULL; }
328                 }
329                 
330                 if (m->control_pressed) { in.close();  return lists; }                          
331                 
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();
339                                 needToRun = true;
340                         }else {
341                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
342                         }
343                 }
344                 
345                 if (m->control_pressed) {  in.close(); return lists; }
346                 
347                 //run last label if you need to
348                 if (needToRun == true)  {
349                         if (list != NULL) {     delete list;    }
350                         
351                         in.seekg(lastPos);
352                         if (!in.eof())  {       
353                                 list = new ListVector(in); 
354                                 
355                                 //make copy of listvector
356                                 ListVector temp(*list);
357                                 lists.push_back(temp);
358                                 
359                                 delete list;
360                         }
361                 }
362                 
363                 in.close();
364                 return lists;
365         }
366         catch(exception& e) {
367                 m->errorOut(e, "OtuHierarchyCommand", "getListVectors");
368                 exit(1);
369         }
370 }
371
372 //**********************************************************************************************************************
373
374
375
376
377