5 * Created by westcott on 10/27/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "subsamplecommand.h"
12 //**********************************************************************************************************************
13 vector<string> SubSampleCommand::getValidParameters(){
15 string Array[] = {"groups","label","outputdir","inputdir"};
16 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
20 m->errorOut(e, "SubSampleCommand", "getValidParameters");
24 //**********************************************************************************************************************
25 SubSampleCommand::SubSampleCommand(){
28 //initialize outputTypes
29 vector<string> tempOutNames;
30 outputTypes["shared"] = tempOutNames;
31 outputTypes["list"] = tempOutNames;
32 outputTypes["rabund"] = tempOutNames;
33 outputTypes["sabund"] = tempOutNames;
36 m->errorOut(e, "SubSampleCommand", "GetRelAbundCommand");
40 //**********************************************************************************************************************
41 vector<string> SubSampleCommand::getRequiredParameters(){
43 vector<string> myArray;
47 m->errorOut(e, "SubSampleCommand", "getRequiredParameters");
51 //**********************************************************************************************************************
52 vector<string> SubSampleCommand::getRequiredFiles(){
54 string Array[] = {"shared","list","rabund","sabund","or"};
55 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
59 m->errorOut(e, "SubSampleCommand", "getRequiredFiles");
63 //**********************************************************************************************************************
64 SubSampleCommand::SubSampleCommand(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","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 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
86 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
89 //initialize outputTypes
90 vector<string> tempOutNames;
91 outputTypes["shared"] = tempOutNames;
92 outputTypes["list"] = tempOutNames;
93 outputTypes["rabund"] = tempOutNames;
94 outputTypes["sabund"] = 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"){
99 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
102 //make sure the user has already run the read.otu command
103 if ((globaldata->getSharedFile() == "") && (globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "")) { m->mothurOut("You must read a list, sabund, rabund or shared file before you can use the sub.sample command."); m->mothurOutEndLine(); abort = true; }
105 //check for optional parameter and set defaults
106 // ...at some point should added some additional type checking...
107 label = validParameter.validFile(parameters, "label", false);
108 if (label == "not found") { label = ""; }
110 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
111 else { allLines = 1; }
114 //if the user has not specified any labels use the ones from read.otu
116 allLines = globaldata->allLines;
117 labels = globaldata->labels;
120 groups = validParameter.validFile(parameters, "groups", false);
121 if (groups == "not found") { groups = ""; pickedGroups = false; }
124 m->splitAtDash(groups, Groups);
125 globaldata->Groups = Groups;
131 catch(exception& e) {
132 m->errorOut(e, "SubSampleCommand", "SubSampleCommand");
137 //**********************************************************************************************************************
139 void SubSampleCommand::help(){
141 m->mothurOut("The get.relabund command can only be executed after a successful read.otu command of a list and group or shared file.\n");
142 m->mothurOut("The get.relabund command parameters are groups, scale and label. No parameters are required.\n");
143 m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included. The group names are separated by dashes.\n");
144 m->mothurOut("The label parameter allows you to select what distance levels you would like, and are also separated by dashes.\n");
145 m->mothurOut("The scale parameter allows you to select what scale you would like to use. Choices are totalgroup, totalotu, averagegroup, averageotu, default is totalgroup.\n");
146 m->mothurOut("The get.relabund command should be in the following format: get.relabund(groups=yourGroups, label=yourLabels).\n");
147 m->mothurOut("Example get.relabund(groups=A-B-C, scale=averagegroup).\n");
148 m->mothurOut("The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n");
149 m->mothurOut("The get.relabund command outputs a .relabund file.\n");
150 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
153 catch(exception& e) {
154 m->errorOut(e, "SubSampleCommand", "help");
159 //**********************************************************************************************************************
161 SubSampleCommand::~SubSampleCommand(){
164 //**********************************************************************************************************************
166 int SubSampleCommand::execute(){
169 if (abort == true) { return 0; }
171 string outputFileName = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + "subsample" + m->getExtension(globaldata->inputFileName);
173 m->openOutputFile(outputFileName, out);
174 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
176 string format = globaldata->getFormat();
178 read = new ReadOTUFile(globaldata->inputFileName);
179 read->read(&*globaldata);
180 input = globaldata->ginput;
182 if (format == "sharedfile") {
183 lookup = input->getSharedRAbundVectors();
184 outputTypes["shared"].push_back(outputFileName);
185 getSubSampleShared(lookup, out);
186 }else if (format == "list") {
187 list = globaldata->glist;
188 outputTypes["list"].push_back(outputFileName);
189 //getSubSamplesList();
190 }else if (format == "rabund") {
191 rabund = globaldata->rabund;
192 outputTypes["rabund"].push_back(outputFileName);
193 //getSubSamplesRabund();
195 }else if (format == "sabund") {
196 sabund = globaldata->sabund;
197 outputTypes["sabund"].push_back(outputFileName);
198 //getSubSamplesSabund();
203 //reset groups parameter
204 delete input; globaldata->ginput = NULL;
207 if (m->control_pressed) { outputTypes.clear(); remove(outputFileName.c_str()); return 0;}
209 m->mothurOutEndLine();
210 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
211 m->mothurOut(outputFileName); m->mothurOutEndLine(); outputNames.push_back(outputFileName);
212 m->mothurOutEndLine();
216 catch(exception& e) {
217 m->errorOut(e, "SubSampleCommand", "execute");
221 //**********************************************************************************************************************
222 int SubSampleCommand::getSubSampleShared(vector<SharedRAbundVector*>& thislookup, ofstream& filename) {
225 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
226 set<string> processedLabels;
227 set<string> userLabels = labels;
229 string lastLabel = lookup[0]->getLabel();
231 //as long as you are not at the end of the file or done wih the lines you want
232 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
233 if (m->control_pressed) { return 0; }
235 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
237 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
243 processedLabels.insert(lookup[0]->getLabel());
244 userLabels.erase(lookup[0]->getLabel());
247 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
248 string saveLabel = lookup[0]->getLabel();
250 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
252 lookup = input->getSharedRAbundVectors(lastLabel);
253 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
256 processedLabels.insert(lookup[0]->getLabel());
257 userLabels.erase(lookup[0]->getLabel());
259 //restore real lastlabel to save below
260 lookup[0]->setLabel(saveLabel);
263 lastLabel = lookup[0]->getLabel();
264 //prevent memory leak
265 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
267 //get next line to process
268 lookup = input->getSharedRAbundVectors();
272 if (m->control_pressed) { return 0; }
274 //output error messages about any remaining user labels
275 set<string>::iterator it;
276 bool needToRun = false;
277 for (it = userLabels.begin(); it != userLabels.end(); it++) {
278 m->mothurOut("Your file does not include the label " + *it);
279 if (processedLabels.count(lastLabel) != 1) {
280 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
283 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
287 //run last label if you need to
288 if (needToRun == true) {
289 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
290 lookup = input->getSharedRAbundVectors(lastLabel);
292 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
298 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
301 //reset groups parameter
302 globaldata->Groups.clear();
307 catch(exception& e) {
308 m->errorOut(e, "SubSampleCommand", "getSubSampleShared");
314 //**********************************************************************************************************************
315 int SubSampleCommand::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
318 vector<SharedRAbundVector*> newLookup;
319 for (int i = 0; i < thislookup.size(); i++) {
320 SharedRAbundVector* temp = new SharedRAbundVector();
321 temp->setLabel(thislookup[i]->getLabel());
322 temp->setGroup(thislookup[i]->getGroup());
323 newLookup.push_back(temp);
327 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
328 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
330 //look at each sharedRabund and make sure they are not all zero
332 for (int j = 0; j < thislookup.size(); j++) {
333 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
336 //if they are not all zero add this bin
338 for (int j = 0; j < thislookup.size(); j++) {
339 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
344 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
346 thislookup = newLookup;
351 catch(exception& e) {
352 m->errorOut(e, "SubSampleCommand", "eliminateZeroOTUS");
357 //**********************************************************************************************************************