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; }
78 vector<string> myArray = setParameters();
80 OptionParser parser(option);
81 map<string,string> parameters = parser.getParameters();
83 ValidParameters validParameter;
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; }
91 //initialize outputTypes
92 vector<string> tempOutNames;
93 outputTypes["shared"] = tempOutNames;
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 = ""; }
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 = ""; }
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; }
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; }
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; }
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; }
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 = ""; }
145 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
146 else { allLines = 1; }
149 groups = validParameter.validFile(parameters, "groups", false);
150 if (groups == "not found") { groups = "all"; }
151 m->splitAtDash(groups, Groups);
156 catch(exception& e) {
157 m->errorOut(e, "MergeGroupsCommand", "MergeGroupsCommand");
161 //**********************************************************************************************************************
163 int MergeGroupsCommand::execute(){
166 if (abort == true) { if (calledHelp) { return 0; } return 2; }
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);
173 m->openOutputFile(outputFileName, out);
175 designMap = new GroupMap(designfile);
176 designMap->readDesignMap();
178 InputData input(sharedfile, "sharedfile");
179 lookup = input.getSharedRAbundVectors();
180 string lastLabel = lookup[0]->getLabel();
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;
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))) {
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; }
191 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
193 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
194 process(lookup, out);
196 processedLabels.insert(lookup[0]->getLabel());
197 userLabels.erase(lookup[0]->getLabel());
200 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
201 string saveLabel = lookup[0]->getLabel();
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();
207 process(lookup, out);
209 processedLabels.insert(lookup[0]->getLabel());
210 userLabels.erase(lookup[0]->getLabel());
212 //restore real lastlabel to save below
213 lookup[0]->setLabel(saveLabel);
216 lastLabel = lookup[0]->getLabel();
217 //prevent memory leak
218 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
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; }
222 //get next line to process
223 lookup = input.getSharedRAbundVectors();
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; }
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();
237 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
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);
246 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
248 process(lookup, out);
250 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
254 //reset groups parameter
258 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0;}
261 //set shared file as new current sharedfile
263 itTypes = outputTypes.find("shared");
264 if (itTypes != outputTypes.end()) {
265 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
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();
275 catch(exception& e) {
276 m->errorOut(e, "MergeGroupsCommand", "execute");
280 //**********************************************************************************************************************
282 int MergeGroupsCommand::process(vector<SharedRAbundVector*>& thisLookUp, ofstream& out){
285 map<string, SharedRAbundVector> merged;
286 map<string, SharedRAbundVector>::iterator it;
288 for (int i = 0; i < thisLookUp.size(); i++) {
290 if (m->control_pressed) { return 0; }
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"; }
297 //do we already have a member of this grouping?
298 it = merged.find(grouping);
300 if (it == merged.end()) { //nope, so create it
301 merged[grouping] = *thisLookUp[i];
302 merged[grouping].setGroup(grouping);
303 }else { //yes, merge it
305 for (int j = 0; j < thisLookUp[i]->getNumBins(); j++) {
306 int abund = (it->second).getAbundance(j);
307 abund += thisLookUp[i]->getAbundance(j);
309 (it->second).set(j, abund, grouping);
316 for (it = merged.begin(); it != merged.end(); it++) {
317 out << (it->second).getLabel() << '\t' << it->first << '\t';
318 (it->second).print(out);
324 catch(exception& e) {
325 m->errorOut(e, "MergeGroupsCommand", "process");
329 //**********************************************************************************************************************