]> git.donarmstrong.com Git - mothur.git/blob - removeotuscommand.cpp
added multiple processors option for Windows users to align.seqs, dist.seqs, summary...
[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                         else { m->setAccnosFile(accnosfile); }
139                         
140                         groupfile = validParameter.validFile(parameters, "group", true);
141                         if (groupfile == "not open") { abort = true; }
142                         else if (groupfile == "not found") {                            
143                                 groupfile = m->getGroupFile(); 
144                                 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
145                                 else {  m->mothurOut("You have no current group file and the group parameter is required."); m->mothurOutEndLine(); abort = true; }
146                         }else { m->setGroupFile(groupfile); }   
147                         
148                         listfile = validParameter.validFile(parameters, "list", true);
149                         if (listfile == "not open") { abort = true; }
150                         else if (listfile == "not found") {                             
151                                 listfile = m->getListFile(); 
152                                 if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
153                                 else {  m->mothurOut("You have no current list file and the list parameter is required."); m->mothurOutEndLine(); abort = true; }
154                         }else { m->setListFile(listfile); }     
155                         
156                         groups = validParameter.validFile(parameters, "groups", false);                 
157                         if (groups == "not found") { groups = ""; }
158                         else { 
159                                 m->splitAtDash(groups, Groups);
160                         }
161                         
162                         label = validParameter.validFile(parameters, "label", false);                   
163                         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=""; }  
164                         
165                         if ((accnosfile == "") && (Groups.size() == 0)) { m->mothurOut("You must provide an accnos file or specify groups using the groups parameter."); m->mothurOutEndLine(); abort = true; }
166                 }
167                 
168         }
169         catch(exception& e) {
170                 m->errorOut(e, "RemoveOtusCommand", "RemoveOtusCommand");
171                 exit(1);
172         }
173 }
174 //**********************************************************************************************************************
175
176 int RemoveOtusCommand::execute(){
177         try {
178                 
179                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
180                 
181                 groupMap = new GroupMap(groupfile);
182                 groupMap->readMap();
183                 
184                 //get groups you want to remove
185                 if (accnosfile != "") { readAccnos(); }
186                 
187                 //make sure groups are valid
188                 //takes care of user setting groupNames that are invalid or setting groups=all
189                 SharedUtil* util = new SharedUtil();
190                 vector<string> allGroups = groupMap->getNamesOfGroups();
191                 util->setGroups(Groups, allGroups);
192                 delete util;
193                 
194                 if (m->control_pressed) { delete groupMap; return 0; }
195                 
196                 //read through the list file keeping any otus that contain any sequence from the groups selected
197                 readListGroup();
198                 
199                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]); } return 0; }
200                 
201                 if (outputNames.size() != 0) {
202                         m->mothurOutEndLine();
203                         m->mothurOut("Output File names: "); m->mothurOutEndLine();
204                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
205                         m->mothurOutEndLine();
206                         
207                         //set fasta file as new current fastafile
208                         string current = "";
209                         itTypes = outputTypes.find("group");
210                         if (itTypes != outputTypes.end()) {
211                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
212                         }
213                         
214                         itTypes = outputTypes.find("list");
215                         if (itTypes != outputTypes.end()) {
216                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
217                         }
218                 }
219                 
220                 return 0;               
221         }
222         
223         catch(exception& e) {
224                 m->errorOut(e, "RemoveOtusCommand", "execute");
225                 exit(1);
226         }
227 }
228 //**********************************************************************************************************************
229 int RemoveOtusCommand::readListGroup(){
230         try {
231                 string thisOutputDir = outputDir;
232                 if (outputDir == "") {  thisOutputDir += m->hasPath(listfile);  }
233                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick." + label +  m->getExtension(listfile);
234                 
235                 ofstream out;
236                 m->openOutputFile(outputFileName, out);
237                 
238                 string GroupOutputDir = outputDir;
239                 if (outputDir == "") {  GroupOutputDir += m->hasPath(groupfile);  }
240                 string outputGroupFileName = GroupOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick." + label  + m->getExtension(groupfile);
241                 
242                 ofstream outGroup;
243                 m->openOutputFile(outputGroupFileName, outGroup);
244                 
245                 InputData* input = new InputData(listfile, "list");
246                 ListVector* list = input->getListVector();
247                 string lastLabel = list->getLabel();
248                 
249                 //using first label seen if none is provided
250                 if (label == "") { label = lastLabel; }
251                 
252                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
253                 set<string> labels; labels.insert(label);
254                 set<string> processedLabels;
255                 set<string> userLabels = labels;
256                 
257                 bool wroteSomething = false;
258                 
259                 //as long as you are not at the end of the file or done wih the lines you want
260                 while((list != NULL) && (userLabels.size() != 0)) {
261                         
262                         if (m->control_pressed) {  delete list; delete input; out.close();  outGroup.close(); m->mothurRemove(outputFileName);  m->mothurRemove(outputGroupFileName);return 0;  }
263                         
264                         if(labels.count(list->getLabel()) == 1){
265                                 processList(list, groupMap, out, outGroup, wroteSomething);
266                                 
267                                 processedLabels.insert(list->getLabel());
268                                 userLabels.erase(list->getLabel());
269                         }
270                         
271                         if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
272                                 string saveLabel = list->getLabel();
273                                 
274                                 delete list; 
275                                 
276                                 list = input->getListVector(lastLabel);
277                                 
278                                 processList(list, groupMap, out, outGroup, wroteSomething);
279                                 
280                                 processedLabels.insert(list->getLabel());
281                                 userLabels.erase(list->getLabel());
282                                 
283                                 //restore real lastlabel to save below
284                                 list->setLabel(saveLabel);
285                         }
286                         
287                         lastLabel = list->getLabel();
288                         
289                         delete list; list = NULL;
290                         
291                         //get next line to process
292                         list = input->getListVector();                          
293                 }
294                 
295                 
296                 if (m->control_pressed) {  if (list != NULL) { delete list; } delete input; out.close(); outGroup.close(); m->mothurRemove(outputFileName);  m->mothurRemove(outputGroupFileName); return 0;  }
297                 
298                 //output error messages about any remaining user labels
299                 set<string>::iterator it;
300                 bool needToRun = false;
301                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
302                         m->mothurOut("Your file does not include the label " + *it); 
303                         if (processedLabels.count(lastLabel) != 1) {
304                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
305                                 needToRun = true;
306                         }else {
307                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
308                         }
309                 }
310                 
311                 //run last label if you need to
312                 if (needToRun == true)  {
313                         if (list != NULL) { delete list; }
314                         
315                         list = input->getListVector(lastLabel);
316                         
317                         processList(list, groupMap, out, outGroup, wroteSomething);
318                         
319                         delete list; list = NULL;
320                 }
321                 
322                 out.close();
323                 outGroup.close();
324                 
325                 if (wroteSomething == false) {  m->mothurOut("At distance " + label + " your file ONLY contains otus containing sequences from the groups you wish to remove."); m->mothurOutEndLine();  }
326                 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
327                 outputTypes["group"].push_back(outputGroupFileName); outputNames.push_back(outputGroupFileName);
328                 
329                 return 0;
330                 
331         }
332         catch(exception& e) {
333                 m->errorOut(e, "RemoveOtusCommand", "readList");
334                 exit(1);
335         }
336 }
337 //**********************************************************************************************************************
338 int RemoveOtusCommand::processList(ListVector*& list, GroupMap*& groupMap, ofstream& out, ofstream& outGroup, bool& wroteSomething){
339         try {
340                 
341                 //make a new list vector
342                 ListVector newList;
343                 newList.setLabel(list->getLabel());
344                 
345                 int numOtus = 0;
346                 //for each bin
347                 for (int i = 0; i < list->getNumBins(); i++) {
348                         if (m->control_pressed) { return 0; }
349                         
350                         //parse out names that are in accnos file
351                         string binnames = list->get(i);
352                         
353                         bool removeBin = false;
354                         string groupFileOutput = "";
355                         
356                         //parse names
357                         string individual = "";
358                         int length = binnames.length();
359                         for(int j=0;j<length;j++){
360                                 if(binnames[j] == ','){
361                                         string group = groupMap->getGroup(individual);
362                                         if (group == "not found") { m->mothurOut("[ERROR]: " + individual + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; }
363                                         
364                                         if (m->inUsersGroups(group, Groups)) {  removeBin = true; break; }
365                                         groupFileOutput += individual + "\t" + group + "\n";
366                                         individual = "";        
367                                         
368                                 }
369                                 else{  individual += binnames[j];  }
370                         }
371                         
372                         if (!removeBin) { 
373                                 //get last name
374                                 string group = groupMap->getGroup(individual);
375                                 if (group == "not found") { m->mothurOut("[ERROR]: " + individual + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; }
376                                 
377                                 if (m->inUsersGroups(group, Groups)) {  removeBin = true; }
378                                 groupFileOutput += individual + "\t" + group + "\n";                            
379                                 
380                                 if (!removeBin) {
381                                         //if there are no sequences from the groups we want to remove in this bin add to new list, output to groupfile
382                                         newList.push_back(binnames);    
383                                         outGroup << groupFileOutput;
384                                 }else {
385                                         numOtus++;
386                                 }
387                         }else {
388                                 numOtus++;
389                         }
390                         
391                 }
392                 
393                 //print new listvector
394                 if (newList.getNumBins() != 0) {
395                         wroteSomething = true;
396                         newList.print(out);
397                 }
398                 
399                 m->mothurOut(newList.getLabel() + " - removed " + toString(numOtus) + " of the " + toString(list->getNumBins()) + " OTUs."); m->mothurOutEndLine();
400                 
401                 return 0;
402                 
403         }
404         catch(exception& e) {
405                 m->errorOut(e, "RemoveOtusCommand", "processList");
406                 exit(1);
407         }
408 }
409 //**********************************************************************************************************************
410 void RemoveOtusCommand::readAccnos(){
411         try {
412                 Groups.clear();
413                 
414                 ifstream in;
415                 m->openInputFile(accnosfile, in);
416                 string name;
417                 
418                 while(!in.eof()){
419                         in >> name;
420                         
421                         Groups.push_back(name);
422                         
423                         m->gobble(in);
424                 }
425                 in.close();             
426                 
427         }
428         catch(exception& e) {
429                 m->errorOut(e, "RemoveOtusCommand", "readAccnos");
430                 exit(1);
431         }
432 }
433 //**********************************************************************************************************************
434
435
436