]> git.donarmstrong.com Git - mothur.git/blob - mergegroupscommand.cpp
removed read.dist, read.otu, read.tree and globaldata. added current to defaults...
[mothur.git] / mergegroupscommand.cpp
1 /*
2  *  mergegroupscommand.cpp
3  *  mothur
4  *
5  *  Created by westcott on 1/24/11.
6  *  Copyright 2011 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "mergegroupscommand.h"
11 #include "sharedutilities.h"
12
13 //**********************************************************************************************************************
14 vector<string> MergeGroupsCommand::setParameters(){     
15         try {
16                 CommandParameter pshared("shared", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pshared);
17                 CommandParameter pdesign("design", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pdesign);
18                 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
19                 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
20                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
21                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
22                 
23                 vector<string> myArray;
24                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
25                 return myArray;
26         }
27         catch(exception& e) {
28                 m->errorOut(e, "MergeGroupsCommand", "setParameters");
29                 exit(1);
30         }
31 }
32 //**********************************************************************************************************************
33 string MergeGroupsCommand::getHelpString(){     
34         try {
35                 string helpString = "";
36                 helpString += "The merge.groups command reads a shared file and a design file and merges the groups in the shared file that are in the same grouping in the design file.\n";
37                 helpString += "The merge.groups command outputs a .shared file. \n";
38                 helpString += "The merge.groups command parameters are shared, groups, label and design.  The design and shared parameter are required.\n";
39                 helpString += "The design parameter allows you to assign your groups to sets. It is required. \n";
40                 helpString += "The design file looks like the group file.  It is a 2 column tab delimited file, where the first column is the group name and the second column is the set the group belongs to.\n";
41                 helpString += "The groups parameter allows you to specify which of the groups in your shared you would like included. The group names are separated by dashes.\n";
42                 helpString += "The label parameter allows you to select what distance levels you would like, and are also separated by dashes.\n";
43                 helpString += "The merge.groups command should be in the following format: merge.groups(design=yourDesignFile, shared=yourSharedFile).\n";
44                 helpString += "Example merge.groups(design=temp.design, groups=A-B-C, shared=temp.shared).\n";
45                 helpString += "The default value for groups is all the groups in your sharedfile, and all labels in your inputfile will be used.\n";
46                 helpString += "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n";
47                 return helpString;
48         }
49         catch(exception& e) {
50                 m->errorOut(e, "MergeGroupsCommand", "getHelpString");
51                 exit(1);
52         }
53 }
54 //**********************************************************************************************************************
55 MergeGroupsCommand::MergeGroupsCommand(){       
56         try {
57                 abort = true; calledHelp = true; 
58                 setParameters();
59                 vector<string> tempOutNames;
60                 outputTypes["shared"] = tempOutNames;
61         }
62         catch(exception& e) {
63                 m->errorOut(e, "MergeGroupsCommand", "MetaStatsCommand");
64                 exit(1);
65         }
66 }
67 //**********************************************************************************************************************
68
69 MergeGroupsCommand::MergeGroupsCommand(string option) {
70         try {
71                 abort = false; calledHelp = false;   
72                 allLines = 1;
73                 
74                 //allow user to run help
75                 if(option == "help") { help(); abort = true; calledHelp = true; }
76                 
77                 else {
78                         vector<string> myArray = setParameters();
79                         
80                         OptionParser parser(option);
81                         map<string,string> parameters = parser.getParameters();
82                         
83                         ValidParameters validParameter;
84                         
85                         //check to make sure all parameters are valid for command
86                         map<string,string>::iterator it;
87                         for (it = parameters.begin(); it != parameters.end(); it++) { 
88                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
89                         }
90                         
91                         //initialize outputTypes
92                         vector<string> tempOutNames;
93                         outputTypes["shared"] = tempOutNames;
94                         
95                         //if the user changes the output directory command factory will send this info to us in the output parameter 
96                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = ""; }
97                         
98                         //if the user changes the input directory command factory will send this info to us in the output parameter 
99                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
100                         if (inputDir == "not found"){   inputDir = "";          }
101                         else {
102                                 string path;
103                                 it = parameters.find("design");
104                                 //user has given a template file
105                                 if(it != parameters.end()){ 
106                                         path = m->hasPath(it->second);
107                                         //if the user has not given a path then, add inputdir. else leave path alone.
108                                         if (path == "") {       parameters["design"] = inputDir + it->second;           }
109                                 }
110                                 
111                                 it = parameters.find("shared");
112                                 //user has given a template file
113                                 if(it != parameters.end()){ 
114                                         path = m->hasPath(it->second);
115                                         //if the user has not given a path then, add inputdir. else leave path alone.
116                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
117                                 }
118                         }
119                         
120                         //check for required parameters
121                         designfile = validParameter.validFile(parameters, "design", true);
122                         if (designfile == "not open") { abort = true; }
123                         else if (designfile == "not found") {                           
124                                 //if there is a current shared file, use it
125                                 designfile = m->getDesignFile(); 
126                                 if (designfile != "") { m->mothurOut("Using " + designfile + " as input file for the design parameter."); m->mothurOutEndLine(); }
127                                 else {  m->mothurOut("You have no current designfile and the design parameter is required."); m->mothurOutEndLine(); abort = true; }
128                         }       
129                         
130                         //make sure the user has already run the read.otu command
131                         sharedfile = validParameter.validFile(parameters, "shared", true);
132                         if (sharedfile == "not open") { abort = true; }
133                         else if (sharedfile == "not found") {                           
134                                 //if there is a current shared file, use it
135                                 sharedfile = m->getSharedFile(); 
136                                 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
137                                 else {  m->mothurOut("You have no current sharedfile and the shared parameter is required."); m->mothurOutEndLine(); abort = true; }
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") { label = ""; }
144                         else { 
145                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
146                                 else { allLines = 1;  }
147                         }
148                         
149                         groups = validParameter.validFile(parameters, "groups", false);                 
150                         if (groups == "not found") { groups = "all";  }
151                         m->splitAtDash(groups, Groups);
152                         m->Groups = Groups;
153                 }
154                 
155         }
156         catch(exception& e) {
157                 m->errorOut(e, "MergeGroupsCommand", "MergeGroupsCommand");
158                 exit(1);
159         }
160 }
161 //**********************************************************************************************************************
162
163 int MergeGroupsCommand::execute(){
164         try {
165                 
166                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
167                 
168                 if (outputDir == "") {  outputDir += m->hasPath(sharedfile);  }
169                 string outputFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "merge" +  m->getExtension(sharedfile);
170                 outputTypes["shared"].push_back(outputFileName); outputNames.push_back(outputFileName);
171                 
172                 ofstream out;
173                 m->openOutputFile(outputFileName, out);
174                 
175                 designMap = new GroupMap(designfile);
176                 designMap->readDesignMap();
177                 
178                 InputData input(sharedfile, "sharedfile");
179                 lookup = input.getSharedRAbundVectors();
180                 string lastLabel = lookup[0]->getLabel();
181                 
182                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
183                 set<string> processedLabels;
184                 set<string> userLabels = labels;
185                 
186                 //as long as you are not at the end of the file or done wih the lines you want
187                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
188                         
189                         if (m->control_pressed) {  out.close(); for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } m->Groups.clear();  delete designMap;  for (int i = 0; i < outputNames.size(); i++) {   remove(outputNames[i].c_str()); } return 0; }
190                         
191                         if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
192                                 
193                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
194                                 process(lookup, out);
195                                 
196                                 processedLabels.insert(lookup[0]->getLabel());
197                                 userLabels.erase(lookup[0]->getLabel());
198                         }
199                         
200                         if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
201                                 string saveLabel = lookup[0]->getLabel();
202                                 
203                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }  
204                                 lookup = input.getSharedRAbundVectors(lastLabel);
205                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
206                                 
207                                 process(lookup, out);
208                                 
209                                 processedLabels.insert(lookup[0]->getLabel());
210                                 userLabels.erase(lookup[0]->getLabel());
211                                 
212                                 //restore real lastlabel to save below
213                                 lookup[0]->setLabel(saveLabel);
214                         }
215                         
216                         lastLabel = lookup[0]->getLabel();
217                         //prevent memory leak
218                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
219                         
220                         if (m->control_pressed) {  out.close(); m->Groups.clear();   delete designMap;  for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str()); } return 0; }
221                         
222                         //get next line to process
223                         lookup = input.getSharedRAbundVectors();                                
224                 }
225                 
226                 if (m->control_pressed) { out.close(); m->Groups.clear();  delete designMap;  for (int i = 0; i < outputNames.size(); i++) {    remove(outputNames[i].c_str()); }  return 0; }
227                 
228                 //output error messages about any remaining user labels
229                 set<string>::iterator it;
230                 bool needToRun = false;
231                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
232                         m->mothurOut("Your file does not include the label " + *it); 
233                         if (processedLabels.count(lastLabel) != 1) {
234                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
235                                 needToRun = true;
236                         }else {
237                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
238                         }
239                 }
240                 
241                 //run last label if you need to
242                 if (needToRun == true)  {
243                         for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }  
244                         lookup = input.getSharedRAbundVectors(lastLabel);
245                         
246                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
247                         
248                         process(lookup, out);
249                         
250                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
251                 }
252                 
253                 out.close();
254                 //reset groups parameter
255                 m->Groups.clear();  
256                 delete designMap;
257                 
258                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str()); } return 0;}
259                 
260                 
261                 //set shared file as new current sharedfile
262                 string current = "";
263                 itTypes = outputTypes.find("shared");
264                 if (itTypes != outputTypes.end()) {
265                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
266                 }
267                 
268                 m->mothurOutEndLine();
269                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
270                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
271                 m->mothurOutEndLine();
272                 
273                 return 0;
274         }
275         catch(exception& e) {
276                 m->errorOut(e, "MergeGroupsCommand", "execute");
277                 exit(1);
278         }
279 }
280 //**********************************************************************************************************************
281
282 int MergeGroupsCommand::process(vector<SharedRAbundVector*>& thisLookUp, ofstream& out){
283         try {
284                 
285                 map<string, SharedRAbundVector> merged;
286                 map<string, SharedRAbundVector>::iterator it;
287                 
288                 for (int i = 0; i < thisLookUp.size(); i++) {
289                         
290                         if (m->control_pressed) { return 0; }
291                         
292                         //what grouping does this group belong to
293                         string grouping = designMap->getGroup(thisLookUp[i]->getGroup());
294                         if (grouping == "not found") { m->mothurOut("[ERROR]: " + thisLookUp[i]->getGroup() + " is not in your design file. Ignoring!"); m->mothurOutEndLine(); grouping = "NOTFOUND"; }
295                         
296                         else {
297                                 //do we already have a member of this grouping?
298                                 it = merged.find(grouping);
299                                 
300                                 if (it == merged.end()) { //nope, so create it
301                                         merged[grouping] = *thisLookUp[i];
302                                         merged[grouping].setGroup(grouping);
303                                 }else { //yes, merge it
304                                         
305                                         for (int j = 0; j < thisLookUp[i]->getNumBins(); j++) {
306                                                 int abund = (it->second).getAbundance(j);
307                                                 abund += thisLookUp[i]->getAbundance(j);
308                                                 
309                                                 (it->second).set(j, abund, grouping);
310                                         }
311                                 }
312                         }
313                 }
314                 
315                 //print new file
316                 for (it = merged.begin(); it != merged.end(); it++) {
317                         out << (it->second).getLabel() << '\t' << it->first << '\t';
318                         (it->second).print(out);
319                 }
320                 
321                 return 0;
322                 
323         }
324         catch(exception& e) {
325                 m->errorOut(e, "MergeGroupsCommand", "process");
326                 exit(1);
327         }
328 }
329 //**********************************************************************************************************************
330
331
332