]> git.donarmstrong.com Git - mothur.git/blob - getrelabundcommand.cpp
added mpi code to cluster.split command
[mothur.git] / getrelabundcommand.cpp
1 /*
2  *  getrelabundcommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 6/21/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "getrelabundcommand.h"
11
12 //**********************************************************************************************************************
13
14 GetRelAbundCommand::GetRelAbundCommand(string option) {
15         try {
16                 globaldata = GlobalData::getInstance();
17                 abort = false;
18                 allLines = 1;
19                 labels.clear();
20                 
21                 //allow user to run help
22                 if(option == "help") { help(); abort = true; }
23                 
24                 else {
25                         //valid paramters for this command
26                         string AlignArray[] =  {"groups","label","scale","outputdir","inputdir"};
27                         vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
28                         
29                         OptionParser parser(option);
30                         map<string,string> parameters = parser.getParameters();
31                         
32                         ValidParameters validParameter;
33                         
34                         //check to make sure all parameters are valid for command
35                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
36                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
37                         }
38                         
39                         //if the user changes the output directory command factory will send this info to us in the output parameter 
40                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
41                                 outputDir = ""; 
42                                 outputDir += hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it  
43                         }
44                         
45                         //make sure the user has already run the read.otu command
46                         if ((globaldata->getSharedFile() == "")) {
47                                  m->mothurOut("You must read a list and a group, or a shared file before you can use the get.relabund command."); m->mothurOutEndLine(); abort = true; 
48                         }
49
50                         //check for optional parameter and set defaults
51                         // ...at some point should added some additional type checking...
52                         label = validParameter.validFile(parameters, "label", false);                   
53                         if (label == "not found") { label = ""; }
54                         else { 
55                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
56                                 else { allLines = 1;  }
57                         }
58                         
59                         //if the user has not specified any labels use the ones from read.otu
60                         if (label == "") {  
61                                 allLines = globaldata->allLines; 
62                                 labels = globaldata->labels; 
63                         }
64                         
65                         groups = validParameter.validFile(parameters, "groups", false);                 
66                         if (groups == "not found") { groups = ""; }
67                         else { 
68                                 splitAtDash(groups, Groups);
69                                 globaldata->Groups = Groups;
70                         }
71                         
72                         scale = validParameter.validFile(parameters, "scale", false);                           if (scale == "not found") { scale = "totalgroup"; }
73                         
74                         if ((scale != "totalgroup") && (scale != "totalgroup") && (scale != "totalgroup") && (scale != "totalgroup")) {
75                                 m->mothurOut(scale + " is not a valid scaling option for the get.relabund command. Choices are totalgroup, totalotu, averagegroup, averageotu."); m->mothurOutEndLine(); abort = true; 
76                         }
77                 }
78
79         }
80         catch(exception& e) {
81                 m->errorOut(e, "GetRelAbundCommand", "GetRelAbundCommand");
82                 exit(1);
83         }
84 }
85
86 //**********************************************************************************************************************
87
88 void GetRelAbundCommand::help(){
89         try {
90                 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");
91                 m->mothurOut("The get.relabund command parameters are groups, scale and label.  No parameters are required.\n");
92                 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");
93                 m->mothurOut("The label parameter allows you to select what distance levels you would like, and are also separated by dashes.\n");
94                 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");
95                 m->mothurOut("The get.relabund command should be in the following format: get.relabund(groups=yourGroups, label=yourLabels).\n");
96                 m->mothurOut("Example get.relabund(groups=A-B-C, scale=log10).\n");
97                 m->mothurOut("The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n");
98                 m->mothurOut("The get.relabund command outputs a .relabund file.\n");
99                 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
100
101         }
102         catch(exception& e) {
103                 m->errorOut(e, "GetRelAbundCommand", "help");
104                 exit(1);
105         }
106 }
107
108 //**********************************************************************************************************************
109
110 GetRelAbundCommand::~GetRelAbundCommand(){
111 }
112
113 //**********************************************************************************************************************
114
115 int GetRelAbundCommand::execute(){
116         try {
117         
118                 if (abort == true) { return 0; }
119                 
120                 string outputFileName = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + "relabund";
121                 ofstream out;
122                 openOutputFile(outputFileName, out);
123                 
124                 read = new ReadOTUFile(globaldata->inputFileName);      
125                 read->read(&*globaldata); 
126                 input = globaldata->ginput;
127                 lookup = input->getSharedRAbundVectors();
128                 string lastLabel = lookup[0]->getLabel();
129                 
130                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
131                 set<string> processedLabels;
132                 set<string> userLabels = labels;
133
134                 //as long as you are not at the end of the file or done wih the lines you want
135                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
136                         
137                         if (m->control_pressed) {  for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } globaldata->Groups.clear(); delete read;  out.close(); remove(outputFileName.c_str()); return 0; }
138         
139                         if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
140
141                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
142                                 getRelAbundance(lookup, out);
143                                 
144                                 processedLabels.insert(lookup[0]->getLabel());
145                                 userLabels.erase(lookup[0]->getLabel());
146                         }
147                         
148                         if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
149                                 string saveLabel = lookup[0]->getLabel();
150                         
151                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }  
152                                 lookup = input->getSharedRAbundVectors(lastLabel);
153                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
154                                 
155                                 getRelAbundance(lookup, out);
156                                 
157                                 processedLabels.insert(lookup[0]->getLabel());
158                                 userLabels.erase(lookup[0]->getLabel());
159                                 
160                                 //restore real lastlabel to save below
161                                 lookup[0]->setLabel(saveLabel);
162                         }
163                         
164                         lastLabel = lookup[0]->getLabel();
165                         //prevent memory leak
166                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
167                                                 
168                         //get next line to process
169                         lookup = input->getSharedRAbundVectors();                               
170                 }
171                 
172                 if (m->control_pressed) { globaldata->Groups.clear(); delete read;  out.close(); remove(outputFileName.c_str());  return 0; }
173
174                 //output error messages about any remaining user labels
175                 set<string>::iterator it;
176                 bool needToRun = false;
177                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
178                         m->mothurOut("Your file does not include the label " + *it); 
179                         if (processedLabels.count(lastLabel) != 1) {
180                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
181                                 needToRun = true;
182                         }else {
183                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
184                         }
185                 }
186         
187                 //run last label if you need to
188                 if (needToRun == true)  {
189                         for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }  
190                         lookup = input->getSharedRAbundVectors(lastLabel);
191                         
192                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
193                         
194                         getRelAbundance(lookup, out);
195                         
196                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
197                 }
198         
199                 //reset groups parameter
200                 globaldata->Groups.clear();  
201                 delete input; globaldata->ginput = NULL;
202                 delete read;
203                 out.close();
204                 
205                 if (m->control_pressed) { remove(outputFileName.c_str()); return 0;}
206                 
207                 m->mothurOutEndLine();
208                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
209                 m->mothurOut(outputFileName); m->mothurOutEndLine();
210                 m->mothurOutEndLine();
211                 
212                 return 0;
213         }
214         catch(exception& e) {
215                 m->errorOut(e, "GetRelAbundCommand", "execute");
216                 exit(1);
217         }
218 }
219 //**********************************************************************************************************************
220
221 int GetRelAbundCommand::getRelAbundance(vector<SharedRAbundVector*> thisLookUp, ofstream& out){
222         try {
223         
224         
225         }
226         catch(exception& e) {
227                 m->errorOut(e, "GetRelAbundCommand", "getRelAbundance");
228                 exit(1);
229         }
230 }
231 //**********************************************************************************************************************
232
233