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