]> git.donarmstrong.com Git - mothur.git/blob - getotuscommand.cpp
changing command name classify.shared to classifyrf.shared
[mothur.git] / getotuscommand.cpp
1 /*
2  *  getotuscommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 11/10/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "getotuscommand.h"
11 #include "inputdata.h"
12 #include "sharedutilities.h"
13
14
15 //**********************************************************************************************************************
16 vector<string> GetOtusCommand::setParameters(){ 
17         try {
18                 CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none","group",false,true, true); parameters.push_back(pgroup);
19                 CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none","list",false,true, true); parameters.push_back(plist);
20                 CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none","",false,false); parameters.push_back(paccnos);
21                 CommandParameter pgroups("groups", "String", "", "", "", "", "","",false,false); parameters.push_back(pgroups);
22                 CommandParameter plabel("label", "String", "", "", "", "", "","",false,false); parameters.push_back(plabel);
23                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
24                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
25                 
26                 vector<string> myArray;
27                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
28                 return myArray;
29         }
30         catch(exception& e) {
31                 m->errorOut(e, "GetOtusCommand", "setParameters");
32                 exit(1);
33         }
34 }
35 //**********************************************************************************************************************
36 string GetOtusCommand::getHelpString(){ 
37         try {
38                 string helpString = "";
39                 helpString += "The get.otus command selects otus containing sequences from a specfic group or set of groups.\n";
40                 helpString += "It outputs a new list file containing the otus containing sequences from in the those specified groups.\n";
41                 helpString += "The get.otus command parameters are accnos, group, list, label and groups. The group and list parameters are required, unless you have valid current files.\n";
42                 helpString += "You must also provide an accnos containing the list of groups to get or set the groups parameter to the groups you wish to select.\n";
43                 helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like.  You can separate group names with dashes.\n";
44                 helpString += "The label parameter allows you to specify which distance you want to process.\n";
45                 helpString += "The get.otus command should be in the following format: get.otus(accnos=yourAccnos, list=yourListFile, group=yourGroupFile, label=yourLabel).\n";
46                 helpString += "Example get.otus(accnos=amazon.accnos, list=amazon.fn.list, group=amazon.groups, label=0.03).\n";
47                 helpString += "or get.otus(groups=pasture, list=amazon.fn.list, amazon.groups, label=0.03).\n";
48                 helpString += "Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListFile).\n";
49                 return helpString;
50         }
51         catch(exception& e) {
52                 m->errorOut(e, "GetOtusCommand", "getHelpString");
53                 exit(1);
54         }
55 }
56 //**********************************************************************************************************************
57 string GetOtusCommand::getOutputPattern(string type) {
58     try {
59         string pattern = "";
60         
61         if (type == "group")       {   pattern = "[filename],[tag],pick,[extension]";    }
62         else if (type == "list")        {   pattern = "[filename],[tag],pick,[extension]";    }
63         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
64         
65         return pattern;
66     }
67     catch(exception& e) {
68         m->errorOut(e, "GetOtusCommand", "getOutputPattern");
69         exit(1);
70     }
71 }
72 //**********************************************************************************************************************
73 GetOtusCommand::GetOtusCommand(){       
74         try {
75                 abort = true; calledHelp = true;
76                 setParameters();
77                 vector<string> tempOutNames;
78                 outputTypes["group"] = tempOutNames;
79                 outputTypes["list"] = tempOutNames;
80         }
81         catch(exception& e) {
82                 m->errorOut(e, "GetOtusCommand", "GetOtusCommand");
83                 exit(1);
84         }
85 }
86 //**********************************************************************************************************************
87 GetOtusCommand::GetOtusCommand(string option)  {
88         try {
89                 abort = false; calledHelp = false;   
90                 
91                 //allow user to run help
92                 if(option == "help") { help(); abort = true; calledHelp = true; }
93                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
94                 
95                 else {
96                         vector<string> myArray = setParameters();
97                         
98                         OptionParser parser(option);
99                         map<string,string> parameters = parser.getParameters();
100                         
101                         ValidParameters validParameter;
102                         map<string,string>::iterator it;
103                         
104                         //check to make sure all parameters are valid for command
105                         for (it = parameters.begin(); it != parameters.end(); it++) { 
106                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
107                         }
108                         
109                         //initialize outputTypes
110                         vector<string> tempOutNames;
111                         outputTypes["group"] = tempOutNames;
112                         outputTypes["list"] = tempOutNames;
113                         
114                         
115                         //if the user changes the output directory command factory will send this info to us in the output parameter 
116                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
117                         
118                         //if the user changes the input directory command factory will send this info to us in the output parameter 
119                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
120                         if (inputDir == "not found"){   inputDir = "";          }
121                         else {
122                                 string path;
123                                 it = parameters.find("accnos");
124                                 //user has given a template file
125                                 if(it != parameters.end()){ 
126                                         path = m->hasPath(it->second);
127                                         //if the user has not given a path then, add inputdir. else leave path alone.
128                                         if (path == "") {       parameters["accnos"] = inputDir + it->second;           }
129                                 }
130                                 
131                                 it = parameters.find("list");
132                                 //user has given a template file
133                                 if(it != parameters.end()){ 
134                                         path = m->hasPath(it->second);
135                                         //if the user has not given a path then, add inputdir. else leave path alone.
136                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
137                                 }
138                                 
139                                 it = parameters.find("group");
140                                 //user has given a template file
141                                 if(it != parameters.end()){ 
142                                         path = m->hasPath(it->second);
143                                         //if the user has not given a path then, add inputdir. else leave path alone.
144                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
145                                 }
146                         }
147                         
148                         
149                         //check for required parameters
150                         accnosfile = validParameter.validFile(parameters, "accnos", true);
151                         if (accnosfile == "not open") { abort = true; }
152                         else if (accnosfile == "not found") {  accnosfile = ""; }
153                         else { m->setAccnosFile(accnosfile); }
154                         
155                         groupfile = validParameter.validFile(parameters, "group", true);
156                         if (groupfile == "not open") { abort = true; }
157                         else if (groupfile == "not found") {                            
158                                 groupfile = m->getGroupFile(); 
159                                 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
160                                 else {  m->mothurOut("You have no current group file and the group parameter is required."); m->mothurOutEndLine(); abort = true; }
161                         }else { m->setGroupFile(groupfile); }   
162                         
163                         listfile = validParameter.validFile(parameters, "list", true);
164                         if (listfile == "not open") { abort = true; }
165                         else if (listfile == "not found") {                             
166                                 listfile = m->getListFile(); 
167                                 if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
168                                 else {  m->mothurOut("You have no current list file and the list parameter is required."); m->mothurOutEndLine(); abort = true; }
169                         }else { m->setListFile(listfile); }     
170                         
171                         groups = validParameter.validFile(parameters, "groups", false);                 
172                         if (groups == "not found") { groups = ""; }
173                         else { 
174                                 m->splitAtDash(groups, Groups);
175                         }
176                         
177                         label = validParameter.validFile(parameters, "label", false);                   
178                         if (label == "not found") { label = ""; m->mothurOut("You did not provide a label, I will use the first label in your inputfile."); m->mothurOutEndLine(); label=""; }  
179                         
180                         if ((accnosfile == "") && (Groups.size() == 0)) { m->mothurOut("You must provide an accnos file or specify groups using the groups parameter."); m->mothurOutEndLine(); abort = true; }
181                 }
182                 
183         }
184         catch(exception& e) {
185                 m->errorOut(e, "GetOtusCommand", "GetOtusCommand");
186                 exit(1);
187         }
188 }
189 //**********************************************************************************************************************
190
191 int GetOtusCommand::execute(){
192         try {
193                 
194                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
195                 
196                 groupMap = new GroupMap(groupfile);
197                 groupMap->readMap();
198                 
199                 //get groups you want to get
200                 if (accnosfile != "") { m->readAccnos(accnosfile, Groups); m->setGroups(Groups); }
201                 
202                 //make sure groups are valid
203                 //takes care of user setting groupNames that are invalid or setting groups=all
204                 SharedUtil* util = new SharedUtil();
205                 vector<string> gNamesOfGroups = groupMap->getNamesOfGroups();
206                 util->setGroups(Groups, gNamesOfGroups);
207                 groupMap->setNamesOfGroups(gNamesOfGroups);
208                 delete util;
209                 
210                 if (m->control_pressed) { delete groupMap; return 0; }
211                 
212                 //read through the list file keeping any otus that contain any sequence from the groups selected
213                 readListGroup();
214                 
215                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]); } return 0; }
216                                 
217                 if (outputNames.size() != 0) {
218                         m->mothurOutEndLine();
219                         m->mothurOut("Output File names: "); m->mothurOutEndLine();
220                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
221                         m->mothurOutEndLine();
222                         
223                         //set list file as new current listfile
224                         string current = "";
225                         itTypes = outputTypes.find("group");
226                         if (itTypes != outputTypes.end()) {
227                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
228                         }
229                         
230                         itTypes = outputTypes.find("list");
231                         if (itTypes != outputTypes.end()) {
232                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
233                         }
234                 }
235                 
236                 return 0;               
237         }
238         
239         catch(exception& e) {
240                 m->errorOut(e, "GetOtusCommand", "execute");
241                 exit(1);
242         }
243 }
244 //**********************************************************************************************************************
245 int GetOtusCommand::readListGroup(){
246         try {
247                 string thisOutputDir = outputDir;
248                 if (outputDir == "") {  thisOutputDir += m->hasPath(listfile);  }
249         map<string, string> variables; 
250         variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(listfile));
251         variables["[tag]"] = label;
252         variables["[extension]"] = m->getExtension(listfile);
253                 string outputFileName = getOutputFileName("list", variables);
254                 
255                 ofstream out;
256                 m->openOutputFile(outputFileName, out);
257                 
258                 string GroupOutputDir = outputDir;
259                 if (outputDir == "") {  GroupOutputDir += m->hasPath(groupfile);  }
260         variables["[filename]"] = GroupOutputDir + m->getRootName(m->getSimpleName(groupfile));
261         variables["[extension]"] = m->getExtension(groupfile);
262                 string outputGroupFileName = getOutputFileName("group", variables);
263                 
264                 ofstream outGroup;
265                 m->openOutputFile(outputGroupFileName, outGroup);
266                         
267                 InputData* input = new InputData(listfile, "list");
268                 ListVector* list = input->getListVector();
269                 string lastLabel = list->getLabel();
270                 
271                 //using first label seen if none is provided
272                 if (label == "") { label = lastLabel; }
273                 
274                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
275                 set<string> labels; labels.insert(label);
276                 set<string> processedLabels;
277                 set<string> userLabels = labels;
278                 
279                 bool wroteSomething = false;
280
281                 //as long as you are not at the end of the file or done wih the lines you want
282                 while((list != NULL) && (userLabels.size() != 0)) {
283                         
284                         if (m->control_pressed) {  delete list; delete input; out.close();  outGroup.close(); m->mothurRemove(outputFileName);  m->mothurRemove(outputGroupFileName);return 0;  }
285                         
286                         if(labels.count(list->getLabel()) == 1){
287                                 processList(list, groupMap, out, outGroup, wroteSomething);
288                                 
289                                 processedLabels.insert(list->getLabel());
290                                 userLabels.erase(list->getLabel());
291                         }
292                         
293                         if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
294                                 string saveLabel = list->getLabel();
295                                 
296                                 delete list; 
297                                 
298                                 list = input->getListVector(lastLabel);
299                                 
300                                 processList(list, groupMap, out, outGroup, wroteSomething);
301                                 
302                                 processedLabels.insert(list->getLabel());
303                                 userLabels.erase(list->getLabel());
304                                 
305                                 //restore real lastlabel to save below
306                                 list->setLabel(saveLabel);
307                         }
308                         
309                         lastLabel = list->getLabel();
310                         
311                         delete list; list = NULL;
312                         
313                         //get next line to process
314                         list = input->getListVector();                          
315                 }
316                 
317                 
318                 if (m->control_pressed) {  if (list != NULL) { delete list; } delete input; out.close(); outGroup.close(); m->mothurRemove(outputFileName);  m->mothurRemove(outputGroupFileName); return 0;  }
319                 
320                 //output error messages about any remaining user labels
321                 set<string>::iterator it;
322                 bool needToRun = false;
323                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
324                         m->mothurOut("Your file does not include the label " + *it); 
325                         if (processedLabels.count(lastLabel) != 1) {
326                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
327                                 needToRun = true;
328                         }else {
329                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
330                         }
331                 }
332                 
333                 //run last label if you need to
334                 if (needToRun == true)  {
335                         if (list != NULL) { delete list; }
336                         
337                         list = input->getListVector(lastLabel);
338                         
339                         processList(list, groupMap, out, outGroup, wroteSomething);
340                         
341                         delete list; list = NULL;
342                 }
343                                         
344                 out.close();
345                 outGroup.close();
346                 
347                 if (wroteSomething == false) {  m->mothurOut("At distance " + label + " your file does NOT contain any otus containing sequences from the groups you wish to get."); m->mothurOutEndLine();  }
348                 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
349                 outputTypes["group"].push_back(outputGroupFileName); outputNames.push_back(outputGroupFileName);
350                 
351                 return 0;
352                 
353         }
354         catch(exception& e) {
355                 m->errorOut(e, "GetOtusCommand", "readList");
356                 exit(1);
357         }
358 }
359 //**********************************************************************************************************************
360 int GetOtusCommand::processList(ListVector*& list, GroupMap*& groupMap, ofstream& out, ofstream& outGroup, bool& wroteSomething){
361         try {
362                 
363                 //make a new list vector
364                 ListVector newList;
365                 newList.setLabel(list->getLabel());
366                 
367                 int numOtus = 0;
368                 //for each bin
369                 for (int i = 0; i < list->getNumBins(); i++) {
370                         if (m->control_pressed) { return 0; }
371                         
372                         //parse out names that are in accnos file
373                         string binnames = list->get(i);
374                         
375                         bool keepBin = false;
376                         string groupFileOutput = "";
377                         
378                         //parse names
379                         string individual = "";
380                         int length = binnames.length();
381                         for(int j=0;j<length;j++){
382                                 if(binnames[j] == ','){
383                                         string group = groupMap->getGroup(individual);
384                                         if (group == "not found") { m->mothurOut("[ERROR]: " + individual + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; }
385                                         
386                                         if (m->inUsersGroups(group, Groups)) {  keepBin = true; }
387                                         groupFileOutput += individual + "\t" + group + "\n";
388                                         individual = "";        
389                                         
390                                 }
391                                 else{  individual += binnames[j];  }
392                         }
393                         
394                         string group = groupMap->getGroup(individual);
395                         if (group == "not found") { m->mothurOut("[ERROR]: " + individual + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; }
396                         
397                         if (m->inUsersGroups(group, Groups)) {  keepBin = true; }
398                         groupFileOutput += individual + "\t" + group + "\n";
399                         
400                         //if there are sequences from the groups we want in this bin add to new list, output to groupfile
401                         if (keepBin) {  
402                                 newList.push_back(binnames);    
403                                 outGroup << groupFileOutput;
404                                 numOtus++;
405                         }
406                 }
407                 
408                 //print new listvector
409                 if (newList.getNumBins() != 0) {
410                         wroteSomething = true;
411                         newList.print(out);
412                 }
413                 
414                 m->mothurOut(newList.getLabel() + " - selected " + toString(numOtus) + " of the " + toString(list->getNumBins()) + " OTUs."); m->mothurOutEndLine();
415         
416                 return 0;
417         }
418         catch(exception& e) {
419                 m->errorOut(e, "GetOtusCommand", "processList");
420                 exit(1);
421         }
422 }
423 //**********************************************************************************************************************
424
425