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