]> git.donarmstrong.com Git - mothur.git/blob - removegroupscommand.cpp
added set.current and get.current commands and modified existing commands to update...
[mothur.git] / removegroupscommand.cpp
1 /*
2  *  removegroupscommand.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 "removegroupscommand.h"
11 #include "sequence.hpp"
12 #include "listvector.hpp"
13 #include "sharedutilities.h"
14
15 //**********************************************************************************************************************
16 vector<string> RemoveGroupsCommand::getValidParameters(){       
17         try {
18                 string Array[] =  {"fasta","name", "group", "accnos", "groups","list","taxonomy","outputdir","inputdir" };
19                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
20                 return myArray;
21         }
22         catch(exception& e) {
23                 m->errorOut(e, "RemoveGroupsCommand", "getValidParameters");
24                 exit(1);
25         }
26 }
27 //**********************************************************************************************************************
28 RemoveGroupsCommand::RemoveGroupsCommand(){     
29         try {
30                 abort = true; calledHelp = true; 
31                 vector<string> tempOutNames;
32                 outputTypes["fasta"] = tempOutNames;
33                 outputTypes["taxonomy"] = tempOutNames;
34                 outputTypes["name"] = tempOutNames;
35                 outputTypes["group"] = tempOutNames;
36                 outputTypes["list"] = tempOutNames;
37         }
38         catch(exception& e) {
39                 m->errorOut(e, "RemoveGroupsCommand", "RemoveGroupsCommand");
40                 exit(1);
41         }
42 }
43 //**********************************************************************************************************************
44 vector<string> RemoveGroupsCommand::getRequiredParameters(){    
45         try {
46                 string Array[] =  {"group"};
47                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
48                 return myArray;
49         }
50         catch(exception& e) {
51                 m->errorOut(e, "RemoveGroupsCommand", "getRequiredParameters");
52                 exit(1);
53         }
54 }
55 //**********************************************************************************************************************
56 vector<string> RemoveGroupsCommand::getRequiredFiles(){ 
57         try {
58                 vector<string> myArray;
59                 return myArray;
60         }
61         catch(exception& e) {
62                 m->errorOut(e, "RemoveGroupsCommand", "getRequiredFiles");
63                 exit(1);
64         }
65 }
66 //**********************************************************************************************************************
67 RemoveGroupsCommand::RemoveGroupsCommand(string option)  {
68         try {
69                 abort = false; calledHelp = false;   
70                 
71                 //allow user to run help
72                 if(option == "help") { help(); abort = true; calledHelp = true; }
73                 
74                 else {
75                         //valid paramters for this command
76                         string Array[] =  {"fasta","name", "group", "accnos", "groups", "list","taxonomy","outputdir","inputdir" };
77                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
78                         
79                         OptionParser parser(option);
80                         map<string,string> parameters = parser.getParameters();
81                         
82                         ValidParameters validParameter;
83                         map<string,string>::iterator it;
84                         
85                         //check to make sure all parameters are valid for command
86                         for (it = parameters.begin(); it != parameters.end(); it++) { 
87                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
88                         }
89                         
90                         //initialize outputTypes
91                         vector<string> tempOutNames;
92                         outputTypes["fasta"] = tempOutNames;
93                         outputTypes["taxonomy"] = tempOutNames;
94                         outputTypes["name"] = tempOutNames;
95                         outputTypes["group"] = tempOutNames;
96                         outputTypes["list"] = tempOutNames;
97                         
98                         
99                         //if the user changes the output directory command factory will send this info to us in the output parameter 
100                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
101                         
102                         //if the user changes the input directory command factory will send this info to us in the output parameter 
103                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
104                         if (inputDir == "not found"){   inputDir = "";          }
105                         else {
106                                 string path;
107                                 it = parameters.find("fasta");
108                                 //user has given a template file
109                                 if(it != parameters.end()){ 
110                                         path = m->hasPath(it->second);
111                                         //if the user has not given a path then, add inputdir. else leave path alone.
112                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
113                                 }
114                                 
115                                 it = parameters.find("accnos");
116                                 //user has given a template file
117                                 if(it != parameters.end()){ 
118                                         path = m->hasPath(it->second);
119                                         //if the user has not given a path then, add inputdir. else leave path alone.
120                                         if (path == "") {       parameters["accnos"] = inputDir + it->second;           }
121                                 }
122                                 
123                                 it = parameters.find("list");
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["list"] = inputDir + it->second;             }
129                                 }
130                                 
131                                 it = parameters.find("name");
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["name"] = 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                                 it = parameters.find("taxonomy");
148                                 //user has given a template file
149                                 if(it != parameters.end()){ 
150                                         path = m->hasPath(it->second);
151                                         //if the user has not given a path then, add inputdir. else leave path alone.
152                                         if (path == "") {       parameters["taxonomy"] = inputDir + it->second;         }
153                                 }
154                         }
155                         
156                         
157                         //check for required parameters
158                         accnosfile = validParameter.validFile(parameters, "accnos", true);
159                         if (accnosfile == "not open") { abort = true; }
160                         else if (accnosfile == "not found") {  accnosfile = ""; }       
161                         
162                         fastafile = validParameter.validFile(parameters, "fasta", true);
163                         if (fastafile == "not open") { abort = true; }
164                         else if (fastafile == "not found") {  fastafile = "";  }        
165                         
166                         namefile = validParameter.validFile(parameters, "name", true);
167                         if (namefile == "not open") { abort = true; }
168                         else if (namefile == "not found") {  namefile = "";  }  
169                         
170                         groupfile = validParameter.validFile(parameters, "group", true);
171                         if (groupfile == "not open") { abort = true; }
172                         else if (groupfile == "not found") {  groupfile = "";  m->mothurOut("You must provide a group file."); m->mothurOutEndLine(); abort = true; }   
173                         
174                         listfile = validParameter.validFile(parameters, "list", true);
175                         if (listfile == "not open") { abort = true; }
176                         else if (listfile == "not found") {  listfile = "";  }
177                         
178                         taxfile = validParameter.validFile(parameters, "taxonomy", true);
179                         if (taxfile == "not open") { abort = true; }
180                         else if (taxfile == "not found") {  taxfile = "";  }
181                         
182                         groups = validParameter.validFile(parameters, "groups", false);                 
183                         if (groups == "not found") { groups = ""; }
184                         else { 
185                                 m->splitAtDash(groups, Groups);
186                         }
187                         
188                         if ((accnosfile == "") && (Groups.size() == 0)) { m->mothurOut("You must provide an accnos file or specify groups using the groups parameter."); m->mothurOutEndLine(); abort = true; }
189                         
190                         if ((fastafile == "") && (namefile == "") && (groupfile == "")  && (listfile == "") && (taxfile == ""))  { m->mothurOut("You must provide at least one of the following: fasta, name, taxonomy, group or list."); m->mothurOutEndLine(); abort = true; }
191                 }
192                 
193         }
194         catch(exception& e) {
195                 m->errorOut(e, "RemoveGroupsCommand", "RemoveGroupsCommand");
196                 exit(1);
197         }
198 }
199 //**********************************************************************************************************************
200
201 void RemoveGroupsCommand::help(){
202         try {
203                 m->mothurOut("The remove.groups command removes sequences from a specfic group or set of groups from the following file types: fasta, name, group, list, taxonomy.\n");
204                 m->mothurOut("It outputs a file containing the sequences NOT in the those specified groups.\n");
205                 m->mothurOut("The remove.groups command parameters are accnos, fasta, name, group, list, taxonomy and groups. The group parameter is required.\n");
206                 m->mothurOut("You must also provide an accnos containing the list of groups to remove or set the groups parameter to the groups you wish to remove.\n");
207                 m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like removed.  You can separate group names with dashes.\n");
208                 m->mothurOut("The remove.groups command should be in the following format: remove.groups(accnos=yourAccnos, fasta=yourFasta, group=yourGroupFile).\n");
209                 m->mothurOut("Example remove.seqs(accnos=amazon.accnos, fasta=amazon.fasta, group=amazon.groups).\n");
210                 m->mothurOut("or remove.seqs(groups=pasture, fasta=amazon.fasta, amazon.groups).\n");
211                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
212         }
213         catch(exception& e) {
214                 m->errorOut(e, "RemoveGroupsCommand", "help");
215                 exit(1);
216         }
217 }
218
219 //**********************************************************************************************************************
220
221 int RemoveGroupsCommand::execute(){
222         try {
223                 
224                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
225                 
226                 groupMap = new GroupMap(groupfile);
227                 groupMap->readMap();
228                 
229                 //get groups you want to remove
230                 if (accnosfile != "") { readAccnos(); }
231                 
232                 //make sure groups are valid
233                 //takes care of user setting groupNames that are invalid or setting groups=all
234                 SharedUtil* util = new SharedUtil();
235                 util->setGroups(Groups, groupMap->namesOfGroups);
236                 delete util;
237                 
238                 //fill names with names of sequences that are from the groups we want to remove 
239                 fillNames();
240                 
241                 if (m->control_pressed) { delete groupMap; return 0; }
242                 
243                 //read through the correct file and output lines you want to keep
244                 if (namefile != "")                     {               readName();             }
245                 if (fastafile != "")            {               readFasta();    }
246                 if (groupfile != "")            {               readGroup();    }
247                 if (listfile != "")                     {               readList();             }
248                 if (taxfile != "")                      {               readTax();              }
249                 
250                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str()); } return 0; }
251                 
252                 m->mothurOut("Removed " + toString(names.size()) + " sequences. From the groups: "); m->mothurOutEndLine();
253                 for (int i = 0; i < Groups.size(); i++) {       m->mothurOut(Groups[i]); m->mothurOutEndLine(); }
254                 m->mothurOutEndLine();
255                 
256                 if (outputNames.size() != 0) {
257                         m->mothurOutEndLine();
258                         m->mothurOut("Output File names: "); m->mothurOutEndLine();
259                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
260                         m->mothurOutEndLine();
261                         
262                         //set fasta file as new current fastafile
263                         string current = "";
264                         itTypes = outputTypes.find("fasta");
265                         if (itTypes != outputTypes.end()) {
266                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
267                         }
268                         
269                         itTypes = outputTypes.find("name");
270                         if (itTypes != outputTypes.end()) {
271                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
272                         }
273                         
274                         itTypes = outputTypes.find("group");
275                         if (itTypes != outputTypes.end()) {
276                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
277                         }
278                         
279                         itTypes = outputTypes.find("list");
280                         if (itTypes != outputTypes.end()) {
281                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
282                         }
283                         
284                         itTypes = outputTypes.find("taxonomy");
285                         if (itTypes != outputTypes.end()) {
286                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
287                         }
288                 }
289                 
290                 return 0;               
291         }
292         
293         catch(exception& e) {
294                 m->errorOut(e, "RemoveGroupsCommand", "execute");
295                 exit(1);
296         }
297 }
298
299 //**********************************************************************************************************************
300 int RemoveGroupsCommand::readFasta(){
301         try {
302                 string thisOutputDir = outputDir;
303                 if (outputDir == "") {  thisOutputDir += m->hasPath(fastafile);  }
304                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
305                 
306                 ofstream out;
307                 m->openOutputFile(outputFileName, out);
308                 
309                 ifstream in;
310                 m->openInputFile(fastafile, in);
311                 string name;
312                 
313                 bool wroteSomething = false;
314                 
315                 while(!in.eof()){
316                         if (m->control_pressed) { in.close();  out.close();  remove(outputFileName.c_str());  return 0; }
317                         
318                         Sequence currSeq(in);
319                         name = currSeq.getName();
320                         
321                         if (name != "") {
322                                 //if this name is in the accnos file
323                                 if (names.count(name) == 0) {
324                                         wroteSomething = true;
325                                         
326                                         currSeq.printSequence(out);
327                                 }
328                         }
329                         m->gobble(in);
330                 }
331                 in.close();     
332                 out.close();
333                 
334                 if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine();  }
335                 outputTypes["fasta"].push_back(outputFileName);  outputNames.push_back(outputFileName);
336                 
337                 return 0;
338                 
339         }
340         catch(exception& e) {
341                 m->errorOut(e, "RemoveGroupsCommand", "readFasta");
342                 exit(1);
343         }
344 }
345
346 //**********************************************************************************************************************
347 int RemoveGroupsCommand::readList(){
348         try {
349                 string thisOutputDir = outputDir;
350                 if (outputDir == "") {  thisOutputDir += m->hasPath(listfile);  }
351                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" +  m->getExtension(listfile);
352                 
353                 ofstream out;
354                 m->openOutputFile(outputFileName, out);
355                 
356                 ifstream in;
357                 m->openInputFile(listfile, in);
358                 
359                 bool wroteSomething = false;
360                 
361                 while(!in.eof()){
362                         //read in list vector
363                         ListVector list(in);
364                         
365                         //make a new list vector
366                         ListVector newList;
367                         newList.setLabel(list.getLabel());
368                         
369                         //for each bin
370                         for (int i = 0; i < list.getNumBins(); i++) {
371                                 if (m->control_pressed) { in.close();  out.close();  remove(outputFileName.c_str());  return 0; }
372                                 
373                                 //parse out names that are in accnos file
374                                 string binnames = list.get(i);
375                                 
376                                 string newNames = "";
377                                 while (binnames.find_first_of(',') != -1) { 
378                                         string name = binnames.substr(0,binnames.find_first_of(','));
379                                         binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
380                                         
381                                         //if that name is in the .accnos file, add it
382                                         if (names.count(name) == 0) {  newNames += name + ",";  }
383                                 }
384                                 
385                                 //get last name
386                                 if (names.count(binnames) == 0) {  newNames += binnames + ",";  }
387                                 
388                                 //if there are names in this bin add to new list
389                                 if (newNames != "") {  
390                                         newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
391                                         newList.push_back(newNames);    
392                                 }
393                         }
394                         
395                         //print new listvector
396                         if (newList.getNumBins() != 0) {
397                                 wroteSomething = true;
398                                 newList.print(out);
399                         }
400                         
401                         m->gobble(in);
402                 }
403                 in.close();     
404                 out.close();
405                 
406                 if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine();  }
407                 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
408                 
409                 return 0;
410                 
411         }
412         catch(exception& e) {
413                 m->errorOut(e, "RemoveGroupsCommand", "readList");
414                 exit(1);
415         }
416 }
417 //**********************************************************************************************************************
418 int RemoveGroupsCommand::readName(){
419         try {
420                 string thisOutputDir = outputDir;
421                 if (outputDir == "") {  thisOutputDir += m->hasPath(namefile);  }
422                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
423                 
424                 ofstream out;
425                 m->openOutputFile(outputFileName, out);
426                 
427                 ifstream in;
428                 m->openInputFile(namefile, in);
429                 string name, firstCol, secondCol;
430                 
431                 bool wroteSomething = false;
432                 
433                 while(!in.eof()){
434                         if (m->control_pressed) { in.close();  out.close();  remove(outputFileName.c_str());  return 0; }
435                         
436                         in >> firstCol;         m->gobble(in);          
437                         in >> secondCol;                        
438                         
439                         vector<string> parsedNames;
440                         m->splitAtComma(secondCol, parsedNames);
441                                                 
442                         vector<string> validSecond;  validSecond.clear();
443                         for (int i = 0; i < parsedNames.size(); i++) {
444                                 if (names.count(parsedNames[i]) == 0) {
445                                         validSecond.push_back(parsedNames[i]);
446                                 }
447                         }
448                         
449                         
450                         //if the name in the first column is in the set then print it and any other names in second column also in set
451                         if (names.count(firstCol) == 0) {
452                                 
453                                 wroteSomething = true;
454                                 
455                                 out << firstCol << '\t';
456                                 
457                                 //you know you have at least one valid second since first column is valid
458                                 for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
459                                 out << validSecond[validSecond.size()-1] << endl;
460                                 
461                                 //make first name in set you come to first column and then add the remaining names to second column
462                         }else {
463                                 
464                                 //you want part of this row
465                                 if (validSecond.size() != 0) {
466                                         
467                                         wroteSomething = true;
468                                         
469                                         out << validSecond[0] << '\t';
470                                         
471                                         //you know you have at least one valid second since first column is valid
472                                         for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
473                                         out << validSecond[validSecond.size()-1] << endl;
474                                 }
475                         }
476                         
477                         m->gobble(in);
478                 }
479                 in.close();
480                 out.close();
481                 
482                 if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine();  }
483                 outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName);
484                 
485                 return 0;
486         }
487         catch(exception& e) {
488                 m->errorOut(e, "RemoveGroupsCommand", "readName");
489                 exit(1);
490         }
491 }
492
493 //**********************************************************************************************************************
494 int RemoveGroupsCommand::readGroup(){
495         try {
496                 string thisOutputDir = outputDir;
497                 if (outputDir == "") {  thisOutputDir += m->hasPath(groupfile);  }
498                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
499                 
500                 ofstream out;
501                 m->openOutputFile(outputFileName, out);
502                 
503                 ifstream in;
504                 m->openInputFile(groupfile, in);
505                 string name, group;
506                 
507                 bool wroteSomething = false;
508                 
509                 while(!in.eof()){
510                         if (m->control_pressed) { in.close();  out.close();  remove(outputFileName.c_str());  return 0; }
511                         
512                         in >> name;                             //read from first column
513                         in >> group;                    //read from second column
514                         
515                         //if this name is in the accnos file
516                         if (names.count(name) == 0) {
517                                 wroteSomething = true;
518                                 out << name << '\t' << group << endl;
519                         }
520                         
521                         m->gobble(in);
522                 }
523                 in.close();
524                 out.close();
525                 
526                 if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine();  }
527                 outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
528                 
529                 return 0;
530         }
531         catch(exception& e) {
532                 m->errorOut(e, "RemoveGroupsCommand", "readGroup");
533                 exit(1);
534         }
535 }
536 //**********************************************************************************************************************
537 int RemoveGroupsCommand::readTax(){
538         try {
539                 string thisOutputDir = outputDir;
540                 if (outputDir == "") {  thisOutputDir += m->hasPath(taxfile);  }
541                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
542                 ofstream out;
543                 m->openOutputFile(outputFileName, out);
544                 
545                 ifstream in;
546                 m->openInputFile(taxfile, in);
547                 string name, tax;
548                 
549                 bool wroteSomething = false;
550                 
551                 while(!in.eof()){
552                         if (m->control_pressed) { in.close();  out.close();  remove(outputFileName.c_str());  return 0; }
553                         
554                         in >> name;                             //read from first column
555                         in >> tax;                      //read from second column
556                         
557                         //if this name is in the accnos file
558                         if (names.count(name) == 0) {
559                                 wroteSomething = true;
560                                 out << name << '\t' << tax << endl;
561                         }
562                         
563                         m->gobble(in);
564                 }
565                 in.close();
566                 out.close();
567                 
568                 if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine();  }
569                 outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName);
570                 
571                 return 0;
572         }
573         catch(exception& e) {
574                 m->errorOut(e, "RemoveGroupsCommand", "readTax");
575                 exit(1);
576         }
577 }
578 //**********************************************************************************************************************
579 void RemoveGroupsCommand::readAccnos(){
580         try {
581                 Groups.clear();
582                 
583                 ifstream in;
584                 m->openInputFile(accnosfile, in);
585                 string name;
586                 
587                 while(!in.eof()){
588                         in >> name;
589                         
590                         Groups.push_back(name);
591                         
592                         m->gobble(in);
593                 }
594                 in.close();             
595                 
596         }
597         catch(exception& e) {
598                 m->errorOut(e, "RemoveGroupsCommand", "readAccnos");
599                 exit(1);
600         }
601 }
602 //**********************************************************************************************************************
603 int RemoveGroupsCommand::fillNames(){
604         try {
605                 vector<string> seqs = groupMap->getNamesSeqs();
606                 
607                 for (int i = 0; i < seqs.size(); i++) {
608                         
609                         if (m->control_pressed) { return 0; }
610                         
611                         string group = groupMap->getGroup(seqs[i]);
612                         
613                         if (m->inUsersGroups(group, Groups)) {
614                                 names.insert(seqs[i]);
615                         }
616                 }
617                 
618                 return 0;
619         }
620         catch(exception& e) {
621                 m->errorOut(e, "RemoveGroupsCommand", "fillNames");
622                 exit(1);
623         }
624 }
625
626 //**********************************************************************************************************************
627
628
629