]> git.donarmstrong.com Git - mothur.git/blob - otuhierarchycommand.cpp
added set.dir command and modified commands to redirect input and output, removed...
[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                 //allow user to run help
17                 if(option == "help") {  help(); abort = true; }
18                 
19                 else {
20                         //valid paramters for this command
21                         string Array[] =  {"list","label","outputdir","inputdir"};
22                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
23                         
24                         OptionParser parser(option);
25                         map<string,string> parameters = parser.getParameters();
26                         
27                         ValidParameters validParameter;
28                         map<string,string>::iterator it;
29                 
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;  }
33                         }
34                         
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 = "";          }
38                         else {
39                                 string path;
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;             }
46                                 }
47                         }
48
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; }      
52                         
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"){  
55                                 outputDir = ""; 
56                                 outputDir += hasPath(listFile); //if user entered a file with a path then preserve it   
57                         }
58                         
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; }
63                         else { 
64                                 splitAtDash(label, labels);
65                                 if (labels.size() != 2) { mothurOut("You must provide 2 labels."); mothurOutEndLine(); abort = true; }
66                         }                               
67                 }
68                 
69         }
70         catch(exception& e) {
71                 errorOut(e, "OtuHierarchyCommand", "OtuHierarchyCommand");
72                 exit(1);
73         }                       
74 }
75 //**********************************************************************************************************************
76
77 void OtuHierarchyCommand::help(){
78         try {
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");
86         }
87         catch(exception& e) {
88                 errorOut(e, "OtuHierarchyCommand", "help");
89                 exit(1);
90         }
91 }
92
93 //**********************************************************************************************************************
94
95 OtuHierarchyCommand::~OtuHierarchyCommand(){}
96
97 //**********************************************************************************************************************
98
99 int OtuHierarchyCommand::execute(){
100         try {
101                 
102                 if (abort == true) { return 0; }
103                 
104                 //get listvectors that correspond to labels requested, (or use smart distancing to get closest listvector)
105                 vector<ListVector> lists = getListVectors();
106                 
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());
112                         }
113                 }else{
114                         mothurOut("error getting listvectors, unable to read 2 different vectors, check your label inputs."); mothurOutEndLine(); return 0;
115                 }
116                 
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); 
121                         
122                         //parse bin
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;  
127                         }
128                         
129                         //get last name
130                         littleBins[names] = i;
131                 }
132                 
133                 ofstream out;
134                 string outputFileName = outputDir + getRootName(getSimpleName(listFile)) + lists[0].getLabel() + "-" + lists[1].getLabel() + ".otu.hierarchy";
135                 openOutputFile(outputFileName, out);
136                 
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++) {
139                 
140                         string names = lists[1].get(i);
141                         
142                         //output column 1
143                         out << names << '\t';
144                         
145                         map<int, int> bins; //bin numbers in little that are in this bin in big
146                         map<int, int>::iterator it;
147                         
148                         //parse bin
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];  
153                         }
154                         
155                         //get last name
156                         bins[littleBins[names]] = littleBins[names]; 
157                         
158                         string col2 = "";
159                         for (it = bins.begin(); it != bins.end(); it++) {
160                                 col2 += lists[0].get(it->first) + "\t";
161                         }
162                         
163                         //output column 2
164                         out << col2 << endl;
165                 }
166                 
167                 out.close();
168                 
169                 return 0;
170         }
171         catch(exception& e) {
172                 errorOut(e, "OtuHierarchyCommand", "execute");
173                 exit(1);
174         }
175 }
176
177 //**********************************************************************************************************************
178 //returns a vector of listVectors where "little" vector is first
179 vector<ListVector> OtuHierarchyCommand::getListVectors() {
180         try {
181                 
182                 int pos; //to use in smart distancing, position of last read in file
183                 int lastPos;
184                 vector<ListVector> lists;
185                 
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;
189
190                 //open file
191                 ifstream in;
192                 openInputFile(listFile, in);
193                 
194                 //get first list vector in file
195                 ListVector* list = NULL;
196                 string lastLabel = "";
197                 if (!in.eof())  {
198                         pos = in.tellg();
199                         lastPos = pos;
200                         list = new ListVector(in);  
201                         gobble(in);
202                         lastLabel = list->getLabel();
203                 }
204                 
205                 while ((list != NULL) && (userLabels.size() != 0)) {
206                         
207                         //is this a listvector that we want?
208                         if(labels.count(list->getLabel()) == 1){
209                                 
210                                 //make copy of listvector
211                                 ListVector temp(*list);
212                                 lists.push_back(temp);
213                         
214                                 processedLabels.insert(list->getLabel());
215                                 userLabels.erase(list->getLabel());
216                         }
217                 
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();
222                                 
223                                 //get smart distance line
224                                 delete list;
225                                 in.seekg(lastPos);
226                                 if (!in.eof())  {       
227                                         list = new ListVector(in);  
228                                 }else { list = NULL; }
229                                 
230                                 //make copy of listvector
231                                 ListVector temp(*list);
232                                 lists.push_back(temp);
233                                         
234                                 processedLabels.insert(list->getLabel());
235                                 userLabels.erase(list->getLabel());                                     
236                                                                                 
237                                 //restore real lastlabel to save below
238                                 list->setLabel(saveLabel);
239                                 in.seekg(savePos);
240                         }
241                         
242                         lastLabel = list->getLabel();
243                         lastPos = pos;
244                         
245                         //get next line
246                         delete list;
247                         if (!in.eof())  {       
248                                 pos = in.tellg();
249                                 list = new ListVector(in);  
250                                 gobble(in);
251                         }else { list = NULL; }
252                 }
253                 
254                                                 
255                 
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();
263                                 needToRun = true;
264                         }else {
265                                 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
266                         }
267                 }
268                 
269                 //run last label if you need to
270                 if (needToRun == true)  {
271                         if (list != NULL) {     delete list;    }
272                         
273                         in.seekg(lastPos);
274                         if (!in.eof())  {       
275                                 list = new ListVector(in); 
276                                 
277                                 //make copy of listvector
278                                 ListVector temp(*list);
279                                 lists.push_back(temp);
280                                 
281                                 delete list;
282                         }
283                 }
284                 
285                 
286                 return lists;
287         }
288         catch(exception& e) {
289                 errorOut(e, "OtuHierarchyCommand", "getListVectors");
290                 exit(1);
291         }
292 }
293
294 //**********************************************************************************************************************
295
296
297
298
299