]> git.donarmstrong.com Git - mothur.git/blob - otuhierarchycommand.cpp
changed added group output to indicator command. a few changes to work with the guy
[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::setParameters(){    
14         try {
15                 CommandParameter poutput("output", "Multiple", "name-number", "name", "", "", "",false,false); parameters.push_back(poutput);
16                 CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(plist);
17                 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
18                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
19                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
20                 
21                 vector<string> myArray;
22                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
23                 return myArray;
24         }
25         catch(exception& e) {
26                 m->errorOut(e, "OtuHierarchyCommand", "setParameters");
27                 exit(1);
28         }
29 }
30 //**********************************************************************************************************************
31 string OtuHierarchyCommand::getHelpString(){    
32         try {
33                 string helpString = "";
34                 helpString += "The otu.hierarchy command is used to see how otus relate at two distances. \n";
35                 helpString += "The otu.hierarchy command parameters are list, label and output.  list and label parameters are required. \n";
36                 helpString += "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";
37                 helpString += "The otu.hierarchy command should be in the following format: \n";
38                 helpString += "otu.hierarchy(list=yourListFile, label=yourLabels).\n";
39                 helpString += "Example otu.hierarchy(list=amazon.fn.list, label=0.01-0.03).\n";
40                 helpString += "The otu.hierarchy command outputs a .otu.hierarchy file which is described on the wiki.\n";
41                 helpString += "Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n";
42                 return helpString;
43         }
44         catch(exception& e) {
45                 m->errorOut(e, "OtuHierarchyCommand", "getHelpString");
46                 exit(1);
47         }
48 }
49 //**********************************************************************************************************************
50 string OtuHierarchyCommand::getOutputFileNameTag(string type, string inputName=""){     
51         try {
52         string outputFileName = "";
53                 map<string, vector<string> >::iterator it;
54         
55         //is this a type this command creates
56         it = outputTypes.find(type);
57         if (it == outputTypes.end()) {  m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
58         else {
59             if (type == "otuheirarchy") {  outputFileName =  "otu.hierarchy"; }
60             else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true;  }
61         }
62         return outputFileName;
63         }
64         catch(exception& e) {
65                 m->errorOut(e, "OtuHierarchyCommand", "getOutputFileNameTag");
66                 exit(1);
67         }
68 }
69 //**********************************************************************************************************************
70 OtuHierarchyCommand::OtuHierarchyCommand(){     
71         try {
72                 abort = true; calledHelp = true; 
73                 setParameters();
74                 vector<string> tempOutNames;
75                 outputTypes["otuheirarchy"] = tempOutNames;
76         }
77         catch(exception& e) {
78                 m->errorOut(e, "OtuHierarchyCommand", "OtuHierarchyCommand");
79                 exit(1);
80         }
81 }
82 //**********************************************************************************************************************
83 OtuHierarchyCommand::OtuHierarchyCommand(string option) {
84         try {
85                 abort = false; calledHelp = false;   
86                 
87                 //allow user to run help
88                 if(option == "help") {  help(); abort = true; calledHelp = true; }
89                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
90                 
91                 else {
92                         vector<string> myArray = setParameters();
93                         
94                         OptionParser parser(option);
95                         map<string,string> parameters = parser.getParameters();
96                         
97                         ValidParameters validParameter;
98                         map<string,string>::iterator it;
99                 
100                         //check to make sure all parameters are valid for command
101                         for (it = parameters.begin(); it != parameters.end(); it++) { 
102                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
103                         }
104                         
105                         //initialize outputTypes
106                         vector<string> tempOutNames;
107                         outputTypes["otuheirarchy"] = tempOutNames;
108                         
109                         //if the user changes the input directory command factory will send this info to us in the output parameter 
110                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
111                         if (inputDir == "not found"){   inputDir = "";          }
112                         else {
113                                 string path;
114                                 it = parameters.find("list");
115                                 //user has given a template file
116                                 if(it != parameters.end()){ 
117                                         path = m->hasPath(it->second);
118                                         //if the user has not given a path then, add inputdir. else leave path alone.
119                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
120                                 }
121                         }
122
123                         listFile = validParameter.validFile(parameters, "list", true);
124                         if (listFile == "not found") { 
125                                 listFile = m->getListFile(); 
126                                 if (listFile != "") {  m->mothurOut("Using " + listFile + " as input file for the list parameter."); m->mothurOutEndLine(); }
127                                 else { 
128                                         m->mothurOut("No valid current list file. You must provide a list file."); m->mothurOutEndLine(); 
129                                         abort = true;
130                                 }
131                         }else if (listFile == "not open") { abort = true; }     
132                         else { m->setListFile(listFile); }
133                         
134                         //if the user changes the output directory command factory will send this info to us in the output parameter 
135                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
136                                 outputDir = ""; 
137                                 outputDir += m->hasPath(listFile); //if user entered a file with a path then preserve it        
138                         }
139                         
140                         //check for optional parameter and set defaults
141                         // ...at some point should added some additional type checking...
142                         label = validParameter.validFile(parameters, "label", false);                   
143                         if (label == "not found") { m->mothurOut("label is a required parameter for the otu.hierarchy command."); m->mothurOutEndLine(); abort = true; }
144                         else { 
145                                 m->splitAtDash(label, labels);
146                                 if (labels.size() != 2) { m->mothurOut("You must provide 2 labels."); m->mothurOutEndLine(); abort = true; }
147                         }       
148                         
149                         output = validParameter.validFile(parameters, "output", false);                 if (output == "not found") { output = "name"; }
150                         
151                         if ((output != "name") && (output != "number")) { m->mothurOut("output options are name and number. I will use name."); m->mothurOutEndLine(); output = "name"; }
152                 }
153                 
154         }
155         catch(exception& e) {
156                 m->errorOut(e, "OtuHierarchyCommand", "OtuHierarchyCommand");
157                 exit(1);
158         }                       
159 }
160 //**********************************************************************************************************************
161
162 int OtuHierarchyCommand::execute(){
163         try {
164                 
165                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
166                 
167                 //get listvectors that correspond to labels requested, (or use smart distancing to get closest listvector)
168                 vector<ListVector> lists = getListVectors();
169                 
170                 if (m->control_pressed) { outputTypes.clear(); return 0; }
171                 
172                 //determine which is little and which is big, putting little first
173                 if (lists.size() == 2) {
174                         //if big is first swap them
175                         if (lists[0].getNumBins() < lists[1].getNumBins()) {
176                                 reverse(lists.begin(), lists.end());
177                         }
178                 }else{
179                         m->mothurOut("error getting listvectors, unable to read 2 different vectors, check your label inputs."); m->mothurOutEndLine(); return 0;
180                 }
181                 
182                 //map sequences to bin number in the "little" otu
183                 map<string, int> littleBins; 
184                 for (int i = 0; i < lists[0].getNumBins(); i++) {
185                 
186                         if (m->control_pressed) {  return 0; }
187                         
188                         string names = lists[0].get(i); 
189                         
190                         //parse bin
191                         while (names.find_first_of(',') != -1) { 
192                                 string name = names.substr(0,names.find_first_of(','));
193                                 names = names.substr(names.find_first_of(',')+1, names.length());
194                                 littleBins[name] = i;  
195                         }
196                         
197                         //get last name
198                         littleBins[names] = i;
199                 }
200                 
201                 ofstream out;
202                 string outputFileName = outputDir + m->getRootName(m->getSimpleName(listFile)) + lists[0].getLabel() + "-" + lists[1].getLabel() + "." + getOutputFileNameTag("otuheirarchy");
203                 m->openOutputFile(outputFileName, out);
204                 
205                 //go through each bin in "big" otu and output the bins in "little" otu which created it
206                 for (int i = 0; i < lists[1].getNumBins(); i++) {
207                 
208                         if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(outputFileName); return 0; }
209                         
210                         string names = lists[1].get(i);
211                         
212                         //output column 1
213                         if (output == "name")   {   out << names << '\t';       }
214                         else                                    {       out << i << '\t';               }
215                         
216                         map<int, int> bins; //bin numbers in little that are in this bin in big
217                         map<int, int>::iterator it;
218                         
219                         //parse bin
220                         while (names.find_first_of(',') != -1) { 
221                                 string name = names.substr(0,names.find_first_of(','));
222                                 names = names.substr(names.find_first_of(',')+1, names.length());
223                                 bins[littleBins[name]] = littleBins[name];  
224                         }
225                         
226                         //get last name
227                         bins[littleBins[names]] = littleBins[names]; 
228                         
229                         string col2 = "";
230                         for (it = bins.begin(); it != bins.end(); it++) {
231                                 if (output == "name")   {   col2 += lists[0].get(it->first) + "\t";     }
232                                 else                                    {       col2 += toString(it->first) + "\t";             }
233                         }
234                         
235                         //output column 2
236                         out << col2 << endl;
237                 }
238                 
239                 out.close();
240                 
241                 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outputFileName); return 0; }
242                 
243                 m->mothurOutEndLine();
244                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
245                 m->mothurOut(outputFileName); m->mothurOutEndLine();    outputNames.push_back(outputFileName); outputTypes["otuheirarchy"].push_back(outputFileName); 
246                 m->mothurOutEndLine();
247                 
248                 return 0;
249         }
250         catch(exception& e) {
251                 m->errorOut(e, "OtuHierarchyCommand", "execute");
252                 exit(1);
253         }
254 }
255
256 //**********************************************************************************************************************
257 //returns a vector of listVectors where "little" vector is first
258 vector<ListVector> OtuHierarchyCommand::getListVectors() {
259         try {
260                 
261                 int pos; //to use in smart distancing, position of last read in file
262                 int lastPos;
263                 vector<ListVector> lists;
264                 
265                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
266                 set<string> processedLabels;
267                 set<string> userLabels = labels;
268
269                 //open file
270                 ifstream in;
271                 m->openInputFile(listFile, in);
272                 
273                 //get first list vector in file
274                 ListVector* list = NULL;
275                 string lastLabel = "";
276                 if (!in.eof())  {
277                         pos = in.tellg();
278                         lastPos = pos;
279                         list = new ListVector(in);  
280                         m->gobble(in);
281                         lastLabel = list->getLabel();
282                 }
283                 
284                 while ((list != NULL) && (userLabels.size() != 0)) {
285                 
286                         if (m->control_pressed) {  in.close(); delete list;  return lists; }
287                         
288                         //is this a listvector that we want?
289                         if(labels.count(list->getLabel()) == 1){
290                                 
291                                 //make copy of listvector
292                                 ListVector temp(*list);
293                                 lists.push_back(temp);
294                         
295                                 processedLabels.insert(list->getLabel());
296                                 userLabels.erase(list->getLabel());
297                         }
298                 
299                         //you have a label the user want that is smaller than this label and the last label has not already been processed 
300                         if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
301                                 string saveLabel = list->getLabel();
302                                 int savePos = in.tellg();
303                                 
304                                 //get smart distance line
305                                 delete list;
306                                 in.seekg(lastPos);
307                                 if (!in.eof())  {       
308                                         list = new ListVector(in);  
309                                 }else { list = NULL; }
310                                 
311                                 //make copy of listvector
312                                 ListVector temp(*list);
313                                 lists.push_back(temp);
314                                         
315                                 processedLabels.insert(list->getLabel());
316                                 userLabels.erase(list->getLabel());                                     
317                                                                                 
318                                 //restore real lastlabel to save below
319                                 list->setLabel(saveLabel);
320                                 in.seekg(savePos);
321                         }
322                         
323                         lastLabel = list->getLabel();
324                         lastPos = pos;
325                         
326                         //get next line
327                         delete list;
328                         if (!in.eof())  {       
329                                 pos = in.tellg();
330                                 list = new ListVector(in);  
331                                 m->gobble(in);
332                         }else { list = NULL; }
333                 }
334                 
335                 if (m->control_pressed) { in.close();  return lists; }                          
336                 
337                 //output error messages about any remaining user labels
338                 set<string>::iterator it;
339                 bool needToRun = false;
340                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
341                         m->mothurOut("Your file does not include the label " + *it); 
342                         if (processedLabels.count(lastLabel) != 1) {
343                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
344                                 needToRun = true;
345                         }else {
346                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
347                         }
348                 }
349                 
350                 if (m->control_pressed) {  in.close(); return lists; }
351                 
352                 //run last label if you need to
353                 if (needToRun == true)  {
354                         if (list != NULL) {     delete list;    }
355                         
356                         in.seekg(lastPos);
357                         if (!in.eof())  {       
358                                 list = new ListVector(in); 
359                                 
360                                 //make copy of listvector
361                                 ListVector temp(*list);
362                                 lists.push_back(temp);
363                                 
364                                 delete list;
365                         }
366                 }
367                 
368                 in.close();
369                 return lists;
370         }
371         catch(exception& e) {
372                 m->errorOut(e, "OtuHierarchyCommand", "getListVectors");
373                 exit(1);
374         }
375 }
376
377 //**********************************************************************************************************************
378
379
380
381
382