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::getValidParameters(){
16 string Array[] = {"shared","label","outputdir","design","groups","processors","inputdir"};
17 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
21 m->errorOut(e, "MergeGroupsCommand", "getValidParameters");
25 //**********************************************************************************************************************
26 MergeGroupsCommand::MergeGroupsCommand(){
29 //initialize outputTypes
30 vector<string> tempOutNames;
31 outputTypes["shared"] = tempOutNames;
34 m->errorOut(e, "MergeGroupsCommand", "MetaStatsCommand");
38 //**********************************************************************************************************************
39 vector<string> MergeGroupsCommand::getRequiredParameters(){
41 string Array[] = {"design", "shared"};
42 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
46 m->errorOut(e, "MergeGroupsCommand", "getRequiredParameters");
50 //**********************************************************************************************************************
51 vector<string> MergeGroupsCommand::getRequiredFiles(){
54 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
58 m->errorOut(e, "MergeGroupsCommand", "getRequiredFiles");
62 //**********************************************************************************************************************
64 MergeGroupsCommand::MergeGroupsCommand(string option) {
66 globaldata = GlobalData::getInstance();
71 //allow user to run help
72 if(option == "help") { help(); abort = true; }
75 //valid paramters for this command
76 string AlignArray[] = {"groups","label","outputdir","shared","design","processors","inputdir"};
77 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
79 OptionParser parser(option);
80 map<string,string> parameters = parser.getParameters();
82 ValidParameters validParameter;
84 //check to make sure all parameters are valid for command
85 map<string,string>::iterator it;
86 for (it = parameters.begin(); it != parameters.end(); it++) {
87 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
90 //initialize outputTypes
91 vector<string> tempOutNames;
92 outputTypes["shared"] = tempOutNames;
94 //if the user changes the output directory command factory will send this info to us in the output parameter
95 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
97 //if the user changes the input directory command factory will send this info to us in the output parameter
98 string inputDir = validParameter.validFile(parameters, "inputdir", false);
99 if (inputDir == "not found"){ inputDir = ""; }
102 it = parameters.find("design");
103 //user has given a template file
104 if(it != parameters.end()){
105 path = m->hasPath(it->second);
106 //if the user has not given a path then, add inputdir. else leave path alone.
107 if (path == "") { parameters["design"] = inputDir + it->second; }
110 it = parameters.find("shared");
111 //user has given a template file
112 if(it != parameters.end()){
113 path = m->hasPath(it->second);
114 //if the user has not given a path then, add inputdir. else leave path alone.
115 if (path == "") { parameters["shared"] = inputDir + it->second; }
119 //check for required parameters
120 designfile = validParameter.validFile(parameters, "design", true);
121 if (designfile == "not open") { abort = true; }
122 else if (designfile == "not found") { designfile = ""; m->mothurOut("You must provide a design file."); m->mothurOutEndLine(); abort = true; }
124 //make sure the user has already run the read.otu command
125 sharedfile = validParameter.validFile(parameters, "shared", true);
126 if (sharedfile == "not open") { abort = true; }
127 else if (sharedfile == "not found") { sharedfile = ""; m->mothurOut("You must provide a shared file."); m->mothurOutEndLine(); abort = true; }
129 //check for optional parameter and set defaults
130 // ...at some point should added some additional type checking...
131 label = validParameter.validFile(parameters, "label", false);
132 if (label == "not found") { label = ""; }
134 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
135 else { allLines = 1; }
138 groups = validParameter.validFile(parameters, "groups", false);
139 if (groups == "not found") { groups = "all"; }
140 m->splitAtDash(groups, Groups);
141 globaldata->Groups = Groups;
145 catch(exception& e) {
146 m->errorOut(e, "MergeGroupsCommand", "MergeGroupsCommand");
151 //**********************************************************************************************************************
153 void MergeGroupsCommand::help(){
155 m->mothurOut("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");
156 m->mothurOut("The merge.groups command outputs a .shared file. \n");
157 m->mothurOut("The merge.groups command parameters are shared, groups, label and design. The design and shared parameter are required.\n");
158 m->mothurOut("The design parameter allows you to assign your groups to sets when you are running metastat. mothur will run all pairwise comparisons of the sets. It is required. \n");
159 m->mothurOut("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");
160 m->mothurOut("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");
161 m->mothurOut("The label parameter allows you to select what distance levels you would like, and are also separated by dashes.\n");
162 m->mothurOut("The merge.groups command should be in the following format: merge.groups(design=yourDesignFile, shared=yourSharedFile).\n");
163 m->mothurOut("Example merge.groups(design=temp.design, groups=A-B-C, shared=temp.shared).\n");
164 m->mothurOut("The default value for groups is all the groups in your sharedfile, and all labels in your inputfile will be used.\n");
165 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
168 catch(exception& e) {
169 m->errorOut(e, "MergeGroupsCommand", "help");
174 //**********************************************************************************************************************
176 MergeGroupsCommand::~MergeGroupsCommand(){}
178 //**********************************************************************************************************************
180 int MergeGroupsCommand::execute(){
183 if (abort == true) { return 0; }
185 if (outputDir == "") { outputDir += m->hasPath(sharedfile); }
186 string outputFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "merge" + m->getExtension(sharedfile);
187 outputTypes["shared"].push_back(outputFileName); outputNames.push_back(outputFileName);
190 m->openOutputFile(outputFileName, out);
192 designMap = new GroupMap(designfile);
193 designMap->readDesignMap();
195 InputData input(sharedfile, "sharedfile");
196 lookup = input.getSharedRAbundVectors();
197 string lastLabel = lookup[0]->getLabel();
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;
203 //as long as you are not at the end of the file or done wih the lines you want
204 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
206 if (m->control_pressed) { out.close(); for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } globaldata->Groups.clear(); delete designMap; for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
208 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
210 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
211 process(lookup, out);
213 processedLabels.insert(lookup[0]->getLabel());
214 userLabels.erase(lookup[0]->getLabel());
217 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
218 string saveLabel = lookup[0]->getLabel();
220 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
221 lookup = input.getSharedRAbundVectors(lastLabel);
222 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
224 process(lookup, out);
226 processedLabels.insert(lookup[0]->getLabel());
227 userLabels.erase(lookup[0]->getLabel());
229 //restore real lastlabel to save below
230 lookup[0]->setLabel(saveLabel);
233 lastLabel = lookup[0]->getLabel();
234 //prevent memory leak
235 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
237 if (m->control_pressed) { out.close(); globaldata->Groups.clear(); delete designMap; for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
239 //get next line to process
240 lookup = input.getSharedRAbundVectors();
243 if (m->control_pressed) { out.close(); globaldata->Groups.clear(); delete designMap; for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
245 //output error messages about any remaining user labels
246 set<string>::iterator it;
247 bool needToRun = false;
248 for (it = userLabels.begin(); it != userLabels.end(); it++) {
249 m->mothurOut("Your file does not include the label " + *it);
250 if (processedLabels.count(lastLabel) != 1) {
251 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
254 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
258 //run last label if you need to
259 if (needToRun == true) {
260 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
261 lookup = input.getSharedRAbundVectors(lastLabel);
263 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
265 process(lookup, out);
267 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
271 //reset groups parameter
272 globaldata->Groups.clear();
275 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0;}
277 m->mothurOutEndLine();
278 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
279 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
280 m->mothurOutEndLine();
284 catch(exception& e) {
285 m->errorOut(e, "MergeGroupsCommand", "execute");
289 //**********************************************************************************************************************
291 int MergeGroupsCommand::process(vector<SharedRAbundVector*>& thisLookUp, ofstream& out){
294 map<string, SharedRAbundVector> merged;
295 map<string, SharedRAbundVector>::iterator it;
297 for (int i = 0; i < thisLookUp.size(); i++) {
299 if (m->control_pressed) { return 0; }
301 //what grouping does this group belong to
302 string grouping = designMap->getGroup(thisLookUp[i]->getGroup());
303 if (grouping == "not found") { m->mothurOut("[ERROR]: " + thisLookUp[i]->getGroup() + " is not in your design file. Ignoring!"); m->mothurOutEndLine(); grouping = "NOTFOUND"; }
306 //do we already have a member of this grouping?
307 it = merged.find(grouping);
309 if (it == merged.end()) { //nope, so create it
310 merged[grouping] = *thisLookUp[i];
311 merged[grouping].setGroup(grouping);
312 }else { //yes, merge it
314 for (int j = 0; j < thisLookUp[i]->getNumBins(); j++) {
315 int abund = (it->second).getAbundance(j);
316 abund += thisLookUp[i]->getAbundance(j);
318 (it->second).set(j, abund, grouping);
325 for (it = merged.begin(); it != merged.end(); it++) {
326 out << (it->second).getLabel() << '\t' << it->first << '\t';
327 (it->second).print(out);
333 catch(exception& e) {
334 m->errorOut(e, "MergeGroupsCommand", "process");
338 //**********************************************************************************************************************