]> git.donarmstrong.com Git - mothur.git/blobdiff - removegroupscommand.cpp
rewrote metastats command in c++, added mothurRemove function to handle ~ error....
[mothur.git] / removegroupscommand.cpp
index 2c98e814220432e2d27cf82499235a3fe3e928bb..c9908fbc715f33eb33f75ff2005d51c9347678ff 100644 (file)
 #include "sharedutilities.h"
 
 //**********************************************************************************************************************
-vector<string> RemoveGroupsCommand::getValidParameters(){      
+vector<string> RemoveGroupsCommand::setParameters(){   
        try {
-               string Array[] =  {"fasta","name", "group", "accnos", "groups","list","taxonomy","outputdir","inputdir" };
-               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+               CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pfasta);
+               CommandParameter pname("name", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pname);
+               CommandParameter pgroup("group", "InputTypes", "", "", "none", "FNGLT", "none",false,true); parameters.push_back(pgroup);
+               CommandParameter plist("list", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(plist);
+               CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(ptaxonomy);
+               CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(paccnos);
+               CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
+               CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
+               CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
+               
+               vector<string> myArray;
+               for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
                return myArray;
        }
        catch(exception& e) {
-               m->errorOut(e, "RemoveGroupsCommand", "getValidParameters");
+               m->errorOut(e, "RemoveGroupsCommand", "setParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+string RemoveGroupsCommand::getHelpString(){   
+       try {
+               string helpString = "";
+               helpString += "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";
+               helpString += "It outputs a file containing the sequences NOT in the those specified groups.\n";
+               helpString += "The remove.groups command parameters are accnos, fasta, name, group, list, taxonomy and groups. The group parameter is required, unless you have a current group file.\n";
+               helpString += "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";
+               helpString += "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";
+               helpString += "The remove.groups command should be in the following format: remove.groups(accnos=yourAccnos, fasta=yourFasta, group=yourGroupFile).\n";
+               helpString += "Example remove.groups(accnos=amazon.accnos, fasta=amazon.fasta, group=amazon.groups).\n";
+               helpString += "or remove.groups(groups=pasture, fasta=amazon.fasta, amazon.groups).\n";
+               helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
+               return helpString;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "RemoveGroupsCommand", "getHelpString");
                exit(1);
        }
 }
+
 //**********************************************************************************************************************
 RemoveGroupsCommand::RemoveGroupsCommand(){    
        try {
-               abort = true;
-               //initialize outputTypes
+               abort = true; calledHelp = true; 
+               setParameters();
                vector<string> tempOutNames;
                outputTypes["fasta"] = tempOutNames;
                outputTypes["taxonomy"] = tempOutNames;
@@ -42,40 +73,16 @@ RemoveGroupsCommand::RemoveGroupsCommand(){
        }
 }
 //**********************************************************************************************************************
-vector<string> RemoveGroupsCommand::getRequiredParameters(){   
-       try {
-               string Array[] =  {"group"};
-               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
-               return myArray;
-       }
-       catch(exception& e) {
-               m->errorOut(e, "RemoveGroupsCommand", "getRequiredParameters");
-               exit(1);
-       }
-}
-//**********************************************************************************************************************
-vector<string> RemoveGroupsCommand::getRequiredFiles(){        
-       try {
-               vector<string> myArray;
-               return myArray;
-       }
-       catch(exception& e) {
-               m->errorOut(e, "RemoveGroupsCommand", "getRequiredFiles");
-               exit(1);
-       }
-}
-//**********************************************************************************************************************
 RemoveGroupsCommand::RemoveGroupsCommand(string option)  {
        try {
-               abort = false;
+               abort = false; calledHelp = false;   
                
                //allow user to run help
-               if(option == "help") { help(); abort = true; }
+               if(option == "help") { help(); abort = true; calledHelp = true; }
+               else if(option == "citation") { citation(); abort = true; calledHelp = true;}
                
                else {
-                       //valid paramters for this command
-                       string Array[] =  {"fasta","name", "group", "accnos", "groups", "list","taxonomy","outputdir","inputdir" };
-                       vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+                       vector<string> myArray = setParameters();
                        
                        OptionParser parser(option);
                        map<string,string> parameters = parser.getParameters();
@@ -159,26 +166,36 @@ RemoveGroupsCommand::RemoveGroupsCommand(string option)  {
                        accnosfile = validParameter.validFile(parameters, "accnos", true);
                        if (accnosfile == "not open") { abort = true; }
                        else if (accnosfile == "not found") {  accnosfile = ""; }       
+                       else { m->setAccnosFile(accnosfile); }
                        
                        fastafile = validParameter.validFile(parameters, "fasta", true);
                        if (fastafile == "not open") { abort = true; }
                        else if (fastafile == "not found") {  fastafile = "";  }        
+                       else { m->setFastaFile(fastafile); }
                        
                        namefile = validParameter.validFile(parameters, "name", true);
                        if (namefile == "not open") { abort = true; }
                        else if (namefile == "not found") {  namefile = "";  }  
+                       else { m->setNameFile(namefile); }
                        
                        groupfile = validParameter.validFile(parameters, "group", true);
                        if (groupfile == "not open") { abort = true; }
-                       else if (groupfile == "not found") {  groupfile = "";  m->mothurOut("You must provide a group file."); m->mothurOutEndLine(); abort = true; }   
+                       else if (groupfile == "not found") {            
+                               //if there is a current group file, use it
+                               groupfile = m->getGroupFile(); 
+                               if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
+                               else {  m->mothurOut("You have no current groupfile and the group parameter is required."); m->mothurOutEndLine(); abort = true; }
+                       }else { m->setGroupFile(groupfile); }   
                        
                        listfile = validParameter.validFile(parameters, "list", true);
                        if (listfile == "not open") { abort = true; }
                        else if (listfile == "not found") {  listfile = "";  }
+                       else { m->setListFile(listfile); }
                        
                        taxfile = validParameter.validFile(parameters, "taxonomy", true);
                        if (taxfile == "not open") { abort = true; }
                        else if (taxfile == "not found") {  taxfile = "";  }
+                       else { m->setTaxonomyFile(taxfile); }
                        
                        groups = validParameter.validFile(parameters, "groups", false);                 
                        if (groups == "not found") { groups = ""; }
@@ -199,30 +216,10 @@ RemoveGroupsCommand::RemoveGroupsCommand(string option)  {
 }
 //**********************************************************************************************************************
 
-void RemoveGroupsCommand::help(){
-       try {
-               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");
-               m->mothurOut("It outputs a file containing the sequences NOT in the those specified groups.\n");
-               m->mothurOut("The remove.groups command parameters are accnos, fasta, name, group, list, taxonomy and groups. The group parameter is required.\n");
-               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");
-               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");
-               m->mothurOut("The remove.groups command should be in the following format: remove.groups(accnos=yourAccnos, fasta=yourFasta, group=yourGroupFile).\n");
-               m->mothurOut("Example remove.seqs(accnos=amazon.accnos, fasta=amazon.fasta, group=amazon.groups).\n");
-               m->mothurOut("or remove.seqs(groups=pasture, fasta=amazon.fasta, amazon.groups).\n");
-               m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
-       }
-       catch(exception& e) {
-               m->errorOut(e, "RemoveGroupsCommand", "help");
-               exit(1);
-       }
-}
-
-//**********************************************************************************************************************
-
 int RemoveGroupsCommand::execute(){
        try {
                
-               if (abort == true) { return 0; }
+               if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
                
                groupMap = new GroupMap(groupfile);
                groupMap->readMap();
@@ -248,10 +245,10 @@ int RemoveGroupsCommand::execute(){
                if (listfile != "")                     {               readList();             }
                if (taxfile != "")                      {               readTax();              }
                
-               if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str()); } return 0; }
+               if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]); } return 0; }
                
                m->mothurOut("Removed " + toString(names.size()) + " sequences. From the groups: "); m->mothurOutEndLine();
-               for (int i = 0; i < Groups.size(); i++) {       m->mothurOut(Groups[i]); m->mothurOutEndLine(); }
+               for (int i = 0; i < Groups.size(); i++) {       m->mothurOut(Groups[i]); m->mothurOut("\t" + toString(groupMap->getNumSeqs(Groups[i]))); m->mothurOutEndLine(); }
                m->mothurOutEndLine();
                
                if (outputNames.size() != 0) {
@@ -259,6 +256,33 @@ int RemoveGroupsCommand::execute(){
                        m->mothurOut("Output File names: "); m->mothurOutEndLine();
                        for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
                        m->mothurOutEndLine();
+                       
+                       //set fasta file as new current fastafile
+                       string current = "";
+                       itTypes = outputTypes.find("fasta");
+                       if (itTypes != outputTypes.end()) {
+                               if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
+                       }
+                       
+                       itTypes = outputTypes.find("name");
+                       if (itTypes != outputTypes.end()) {
+                               if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
+                       }
+                       
+                       itTypes = outputTypes.find("group");
+                       if (itTypes != outputTypes.end()) {
+                               if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
+                       }
+                       
+                       itTypes = outputTypes.find("list");
+                       if (itTypes != outputTypes.end()) {
+                               if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
+                       }
+                       
+                       itTypes = outputTypes.find("taxonomy");
+                       if (itTypes != outputTypes.end()) {
+                               if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
+                       }
                }
                
                return 0;               
@@ -287,7 +311,7 @@ int RemoveGroupsCommand::readFasta(){
                bool wroteSomething = false;
                
                while(!in.eof()){
-                       if (m->control_pressed) { in.close();  out.close();  remove(outputFileName.c_str());  return 0; }
+                       if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
                        
                        Sequence currSeq(in);
                        name = currSeq.getName();
@@ -342,7 +366,7 @@ int RemoveGroupsCommand::readList(){
                        
                        //for each bin
                        for (int i = 0; i < list.getNumBins(); i++) {
-                               if (m->control_pressed) { in.close();  out.close();  remove(outputFileName.c_str());  return 0; }
+                               if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
                                
                                //parse out names that are in accnos file
                                string binnames = list.get(i);
@@ -405,7 +429,7 @@ int RemoveGroupsCommand::readName(){
                bool wroteSomething = false;
                
                while(!in.eof()){
-                       if (m->control_pressed) { in.close();  out.close();  remove(outputFileName.c_str());  return 0; }
+                       if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
                        
                        in >> firstCol;         m->gobble(in);          
                        in >> secondCol;                        
@@ -481,7 +505,7 @@ int RemoveGroupsCommand::readGroup(){
                bool wroteSomething = false;
                
                while(!in.eof()){
-                       if (m->control_pressed) { in.close();  out.close();  remove(outputFileName.c_str());  return 0; }
+                       if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
                        
                        in >> name;                             //read from first column
                        in >> group;                    //read from second column
@@ -523,7 +547,7 @@ int RemoveGroupsCommand::readTax(){
                bool wroteSomething = false;
                
                while(!in.eof()){
-                       if (m->control_pressed) { in.close();  out.close();  remove(outputFileName.c_str());  return 0; }
+                       if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
                        
                        in >> name;                             //read from first column
                        in >> tax;                      //read from second column