2 * mergegroupscommand.cpp
5 * Created by westcott on 1/24/11.
6 * Copyright 2011 Schloss Lab. All rights reserved.
10 #include "mergegroupscommand.h"
11 #include "sharedutilities.h"
13 //**********************************************************************************************************************
14 vector<string> MergeGroupsCommand::setParameters(){
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);
23 vector<string> myArray;
24 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
28 m->errorOut(e, "MergeGroupsCommand", "setParameters");
32 //**********************************************************************************************************************
33 string MergeGroupsCommand::getHelpString(){
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";
50 m->errorOut(e, "MergeGroupsCommand", "getHelpString");
54 //**********************************************************************************************************************
55 MergeGroupsCommand::MergeGroupsCommand(){
57 abort = true; calledHelp = true;
59 vector<string> tempOutNames;
60 outputTypes["shared"] = tempOutNames;
63 m->errorOut(e, "MergeGroupsCommand", "MetaStatsCommand");
67 //**********************************************************************************************************************
69 MergeGroupsCommand::MergeGroupsCommand(string option) {
71 abort = false; calledHelp = false;
74 //allow user to run help
75 if(option == "help") { help(); abort = true; calledHelp = true; }
76 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
79 vector<string> myArray = setParameters();
81 OptionParser parser(option);
82 map<string,string> parameters = parser.getParameters();
84 ValidParameters validParameter;
86 //check to make sure all parameters are valid for command
87 map<string,string>::iterator it;
88 for (it = parameters.begin(); it != parameters.end(); it++) {
89 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
92 //initialize outputTypes
93 vector<string> tempOutNames;
94 outputTypes["shared"] = tempOutNames;
96 //if the user changes the output directory command factory will send this info to us in the output parameter
97 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
99 //if the user changes the input directory command factory will send this info to us in the output parameter
100 string inputDir = validParameter.validFile(parameters, "inputdir", false);
101 if (inputDir == "not found"){ inputDir = ""; }
104 it = parameters.find("design");
105 //user has given a template file
106 if(it != parameters.end()){
107 path = m->hasPath(it->second);
108 //if the user has not given a path then, add inputdir. else leave path alone.
109 if (path == "") { parameters["design"] = inputDir + it->second; }
112 it = parameters.find("shared");
113 //user has given a template file
114 if(it != parameters.end()){
115 path = m->hasPath(it->second);
116 //if the user has not given a path then, add inputdir. else leave path alone.
117 if (path == "") { parameters["shared"] = inputDir + it->second; }
121 //check for required parameters
122 designfile = validParameter.validFile(parameters, "design", true);
123 if (designfile == "not open") { abort = true; }
124 else if (designfile == "not found") {
125 //if there is a current shared file, use it
126 designfile = m->getDesignFile();
127 if (designfile != "") { m->mothurOut("Using " + designfile + " as input file for the design parameter."); m->mothurOutEndLine(); }
128 else { m->mothurOut("You have no current designfile and the design parameter is required."); m->mothurOutEndLine(); abort = true; }
131 //make sure the user has already run the read.otu command
132 sharedfile = validParameter.validFile(parameters, "shared", true);
133 if (sharedfile == "not open") { abort = true; }
134 else if (sharedfile == "not found") {
135 //if there is a current shared file, use it
136 sharedfile = m->getSharedFile();
137 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
138 else { m->mothurOut("You have no current sharedfile and the shared parameter is required."); m->mothurOutEndLine(); abort = true; }
141 //check for optional parameter and set defaults
142 // ...at some point should added some additional type checking...
143 label = validParameter.validFile(parameters, "label", false);
144 if (label == "not found") { label = ""; }
146 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
147 else { allLines = 1; }
150 groups = validParameter.validFile(parameters, "groups", false);
151 if (groups == "not found") { groups = "all"; }
152 m->splitAtDash(groups, Groups);
157 catch(exception& e) {
158 m->errorOut(e, "MergeGroupsCommand", "MergeGroupsCommand");
162 //**********************************************************************************************************************
164 int MergeGroupsCommand::execute(){
167 if (abort == true) { if (calledHelp) { return 0; } return 2; }
169 if (outputDir == "") { outputDir += m->hasPath(sharedfile); }
170 string outputFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "merge" + m->getExtension(sharedfile);
171 outputTypes["shared"].push_back(outputFileName); outputNames.push_back(outputFileName);
174 m->openOutputFile(outputFileName, out);
176 designMap = new GroupMap(designfile);
177 designMap->readDesignMap();
179 InputData input(sharedfile, "sharedfile");
180 lookup = input.getSharedRAbundVectors();
181 string lastLabel = lookup[0]->getLabel();
183 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
184 set<string> processedLabels;
185 set<string> userLabels = labels;
187 //as long as you are not at the end of the file or done wih the lines you want
188 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
190 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; }
192 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
194 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
195 process(lookup, out);
197 processedLabels.insert(lookup[0]->getLabel());
198 userLabels.erase(lookup[0]->getLabel());
201 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
202 string saveLabel = lookup[0]->getLabel();
204 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
205 lookup = input.getSharedRAbundVectors(lastLabel);
206 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
208 process(lookup, out);
210 processedLabels.insert(lookup[0]->getLabel());
211 userLabels.erase(lookup[0]->getLabel());
213 //restore real lastlabel to save below
214 lookup[0]->setLabel(saveLabel);
217 lastLabel = lookup[0]->getLabel();
218 //prevent memory leak
219 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
221 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; }
223 //get next line to process
224 lookup = input.getSharedRAbundVectors();
227 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; }
229 //output error messages about any remaining user labels
230 set<string>::iterator it;
231 bool needToRun = false;
232 for (it = userLabels.begin(); it != userLabels.end(); it++) {
233 m->mothurOut("Your file does not include the label " + *it);
234 if (processedLabels.count(lastLabel) != 1) {
235 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
238 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
242 //run last label if you need to
243 if (needToRun == true) {
244 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
245 lookup = input.getSharedRAbundVectors(lastLabel);
247 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
249 process(lookup, out);
251 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
255 //reset groups parameter
259 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0;}
262 //set shared file as new current sharedfile
264 itTypes = outputTypes.find("shared");
265 if (itTypes != outputTypes.end()) {
266 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
269 m->mothurOutEndLine();
270 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
271 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
272 m->mothurOutEndLine();
276 catch(exception& e) {
277 m->errorOut(e, "MergeGroupsCommand", "execute");
281 //**********************************************************************************************************************
283 int MergeGroupsCommand::process(vector<SharedRAbundVector*>& thisLookUp, ofstream& out){
286 map<string, SharedRAbundVector> merged;
287 map<string, SharedRAbundVector>::iterator it;
289 for (int i = 0; i < thisLookUp.size(); i++) {
291 if (m->control_pressed) { return 0; }
293 //what grouping does this group belong to
294 string grouping = designMap->getGroup(thisLookUp[i]->getGroup());
295 if (grouping == "not found") { m->mothurOut("[ERROR]: " + thisLookUp[i]->getGroup() + " is not in your design file. Ignoring!"); m->mothurOutEndLine(); grouping = "NOTFOUND"; }
298 //do we already have a member of this grouping?
299 it = merged.find(grouping);
301 if (it == merged.end()) { //nope, so create it
302 merged[grouping] = *thisLookUp[i];
303 merged[grouping].setGroup(grouping);
304 }else { //yes, merge it
306 for (int j = 0; j < thisLookUp[i]->getNumBins(); j++) {
307 int abund = (it->second).getAbundance(j);
308 abund += thisLookUp[i]->getAbundance(j);
310 (it->second).set(j, abund, grouping);
317 for (it = merged.begin(); it != merged.end(); it++) {
318 out << (it->second).getLabel() << '\t' << it->first << '\t';
319 (it->second).print(out);
325 catch(exception& e) {
326 m->errorOut(e, "MergeGroupsCommand", "process");
330 //**********************************************************************************************************************