]> git.donarmstrong.com Git - mothur.git/blobdiff - removegroupscommand.cpp
fixes while testing 1.33.0
[mothur.git] / removegroupscommand.cpp
index 86ddf946a9d51c1df7e43783e0470bf5aead6301..42f088b953d2f3657dd98dcec4f184f87eb8a70b 100644 (file)
 //**********************************************************************************************************************
 vector<string> RemoveGroupsCommand::setParameters(){   
        try {
-               CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "FNGLT",false,false); parameters.push_back(pfasta);
-               CommandParameter pshared("shared", "InputTypes", "", "", "none", "sharedGroup", "none",false,false); parameters.push_back(pshared);
-        CommandParameter pname("name", "InputTypes", "", "", "NameCount", "none", "none",false,false); parameters.push_back(pname);
-        CommandParameter pcount("count", "InputTypes", "", "", "NameCount-CountGroup", "none", "none",false,false); parameters.push_back(pcount);
-               CommandParameter pgroup("group", "InputTypes", "", "", "CountGroup", "sharedGroup", "FNGLT",false,false); parameters.push_back(pgroup);         CommandParameter pdesign("design", "InputTypes", "", "", "none", "sharedGroup", "FNGLT",false,false); parameters.push_back(pdesign);
-               CommandParameter plist("list", "InputTypes", "", "", "none", "none", "FNGLT",false,false); parameters.push_back(plist);
-               CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "none", "FNGLT",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);
+               CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "FNGLT","fasta",false,false,true); parameters.push_back(pfasta);
+               CommandParameter pshared("shared", "InputTypes", "", "", "none", "sharedGroup", "none","shared",false,false,true); parameters.push_back(pshared);
+        CommandParameter pname("name", "InputTypes", "", "", "NameCount", "none", "none","name",false,false,true); parameters.push_back(pname);
+        CommandParameter pcount("count", "InputTypes", "", "", "NameCount-CountGroup", "none", "none","count",false,false,true); parameters.push_back(pcount);
+               CommandParameter pgroup("group", "InputTypes", "", "", "CountGroup", "sharedGroup", "FNGLT","group",false,false,true); parameters.push_back(pgroup);            
+        CommandParameter pdesign("design", "InputTypes", "", "", "none", "sharedGroup", "FNGLT","design",false,false); parameters.push_back(pdesign);
+               CommandParameter plist("list", "InputTypes", "", "", "none", "none", "FNGLT","list",false,false,true); parameters.push_back(plist);
+               CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "none", "FNGLT","taxonomy",false,false,true); 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);          }
@@ -58,31 +59,26 @@ string RemoveGroupsCommand::getHelpString(){
        }
 }
 //**********************************************************************************************************************
-string RemoveGroupsCommand::getOutputFileNameTag(string type, string inputName=""){    
-       try {
-        string outputFileName = "";
-               map<string, vector<string> >::iterator it;
+string RemoveGroupsCommand::getOutputPattern(string type) {
+    try {
+        string pattern = "";
         
-        //is this a type this command creates
-        it = outputTypes.find(type);
-        if (it == outputTypes.end()) {  m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
-        else {
-            if (type == "fasta")            {   outputFileName =  "pick" + m->getExtension(inputName);   }
-            else if (type == "taxonomy")    {   outputFileName =  "pick" + m->getExtension(inputName);   }
-            else if (type == "name")        {   outputFileName =  "pick" + m->getExtension(inputName);   }
-            else if (type == "group")       {   outputFileName =  "pick" + m->getExtension(inputName);   }
-            else if (type == "count")       {   outputFileName =  "pick" + m->getExtension(inputName);   }
-            else if (type == "list")        {   outputFileName =  "pick" + m->getExtension(inputName);   }
-            else if (type == "shared")      {   outputFileName =  "pick" + m->getExtension(inputName);   }
-            else if (type == "design")      {   outputFileName =  "pick" + m->getExtension(inputName);   }
-            else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true;  }
-        }
-        return outputFileName;
-       }
-       catch(exception& e) {
-               m->errorOut(e, "RemoveGroupsCommand", "getOutputFileNameTag");
-               exit(1);
-       }
+        if (type == "fasta")            {   pattern = "[filename],pick,[extension]";    }
+        else if (type == "taxonomy")    {   pattern = "[filename],pick,[extension]";    }
+        else if (type == "name")        {   pattern = "[filename],pick,[extension]";    }
+        else if (type == "group")       {   pattern = "[filename],pick,[extension]";    }
+        else if (type == "count")       {   pattern = "[filename],pick,[extension]";    }
+        else if (type == "list")        {   pattern = "[filename],pick,[extension]";    }
+        else if (type == "shared")      {   pattern = "[filename],[tag],pick,[extension]";    }
+        else if (type == "design")      {   pattern = "[filename],[tag],pick,[extension]";    }
+        else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
+        
+        return pattern;
+    }
+    catch(exception& e) {
+        m->errorOut(e, "RemoveGroupsCommand", "getOutputPattern");
+        exit(1);
+    }
 }
 //**********************************************************************************************************************
 RemoveGroupsCommand::RemoveGroupsCommand(){    
@@ -359,11 +355,19 @@ int RemoveGroupsCommand::execute(){
                        
                        //make sure groups are valid
                        //takes care of user setting groupNames that are invalid or setting groups=all
-                       SharedUtil* util = new SharedUtil();
                        vector<string> namesGroups = groupMap->getNamesOfGroups();
-                       util->setGroups(Groups, namesGroups);
-                       delete util;
-                       
+                       vector<string> checkedGroups;
+            for (int i = 0; i < Groups.size(); i++) {
+                if (m->inUsersGroups(Groups[i], namesGroups)) { checkedGroups.push_back(Groups[i]); }
+                else {  m->mothurOut("[WARNING]: " + Groups[i] + " is not a valid group in your groupfile, ignoring.\n"); }
+            }
+            
+            if (checkedGroups.size() == 0) { m->mothurOut("[ERROR]: no valid groups, aborting.\n"); delete groupMap; return 0; }
+                       else {
+                Groups = checkedGroups;
+                m->setGroups(Groups);
+            }
+            
                        //fill names with names of sequences that are from the groups we want to remove 
                        fillNames();
                        
@@ -373,7 +377,7 @@ int RemoveGroupsCommand::execute(){
                 m->mothurOut("\n[NOTE]: The count file should contain only unique names, so mothur assumes your fasta, list and taxonomy files also contain only uniques.\n\n");
             }
             CountTable ct;
-            ct.readTable(countfile);
+            ct.readTable(countfile, true, false);
             if (!ct.hasGroupInfo()) { m->mothurOut("[ERROR]: your count file does not contain group info, aborting.\n"); return 0; }
             
             vector<string> gNamesOfGroups = ct.getNamesOfGroups();
@@ -468,7 +472,10 @@ int RemoveGroupsCommand::readFasta(){
        try {
                string thisOutputDir = outputDir;
                if (outputDir == "") {  thisOutputDir += m->hasPath(fastafile);  }
-               string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + getOutputFileNameTag("fasta", fastafile);
+        map<string, string> variables; 
+        variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(fastafile));
+        variables["[extension]"] = m->getExtension(fastafile);
+               string outputFileName = getOutputFileName("fasta", variables);
                
                ofstream out;
                m->openOutputFile(outputFileName, out);
@@ -529,6 +536,10 @@ int RemoveGroupsCommand::readShared(){
                //that way we can take advantage of the reads in inputdata and sharedRabundVector
                InputData* tempInput = new InputData(sharedfile, "sharedfile");
                vector<SharedRAbundVector*> lookup = tempInput->getSharedRAbundVectors();
+        
+        map<string, string> variables; 
+        variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile));
+        variables["[extension]"] = m->getExtension(sharedfile);
        
                //save m->Groups
                vector<string> allGroupsNames = m->getAllGroups();
@@ -549,9 +560,9 @@ int RemoveGroupsCommand::readShared(){
                m->setGroups(groupsToKeep);
                m->clearAllGroups();
                m->saveNextLabel = "";
-               m->printedHeaders = false;
-               m->currentBinLabels.clear();
-               m->binLabelsInFile.clear();
+               m->printedSharedHeaders = false;
+               m->currentSharedBinLabels.clear();
+               m->sharedBinLabelsInFile.clear();
                
                InputData input(sharedfile, "sharedfile");
                lookup = input.getSharedRAbundVectors();
@@ -560,7 +571,8 @@ int RemoveGroupsCommand::readShared(){
                
                while(lookup[0] != NULL) {
                        
-                       string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sharedfile)) + lookup[0]->getLabel() + "." + getOutputFileNameTag("shared", sharedfile);
+                       variables["[tag]"] = lookup[0]->getLabel();
+            string outputFileName = getOutputFileName("shared", variables);
                        ofstream out;
                        m->openOutputFile(outputFileName, out);
                        outputTypes["shared"].push_back(outputFileName);  outputNames.push_back(outputFileName);
@@ -608,11 +620,10 @@ int RemoveGroupsCommand::readList(){
        try {
                string thisOutputDir = outputDir;
                if (outputDir == "") {  thisOutputDir += m->hasPath(listfile);  }
-               string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + getOutputFileNameTag("list", listfile);
-               
-               ofstream out;
-               m->openOutputFile(outputFileName, out);
-               
+               map<string, string> variables; 
+        variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(listfile));
+        variables["[extension]"] = m->getExtension(listfile);
+                               
                ifstream in;
                m->openInputFile(listfile, in);
                
@@ -625,6 +636,16 @@ int RemoveGroupsCommand::readList(){
                        
                        //read in list vector
                        ListVector list(in);
+            
+            variables["[tag]"] = list.getLabel();
+            string outputFileName = getOutputFileName("list", variables);
+                       
+                       ofstream out;
+                       m->openOutputFile(outputFileName, out);
+                       outputTypes["list"].push_back(outputFileName);  outputNames.push_back(outputFileName);
+            
+            vector<string> binLabels = list.getLabels();
+            vector<string> newBinLabels;
                        
                        //make a new list vector
                        ListVector newList;
@@ -665,24 +686,26 @@ int RemoveGroupsCommand::readList(){
                                //if there are names in this bin add to new list
                                if (newNames != "") {  
                                        newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
-                                       newList.push_back(newNames);    
+                                       newList.push_back(newNames);
+                    newBinLabels.push_back(binLabels[i]);
                                }
                        }
                        
                        //print new listvector
                        if (newList.getNumBins() != 0) {
                                wroteSomething = true;
+                               newList.setLabels(newBinLabels);
+                newList.printHeaders(out);
                                newList.print(out);
                        }
                        
                        m->gobble(in);
+            out.close();
                }
                in.close();     
-               out.close();
                
-               if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine();  }
-               outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
                
+               if (wroteSomething == false) {  m->mothurOut("Your file contains only sequences from the groups you wish to remove."); m->mothurOutEndLine();  }
                m->mothurOut("Removed " + toString(removedCount) + " sequences from your list file."); m->mothurOutEndLine();
                
                return 0;
@@ -698,7 +721,10 @@ int RemoveGroupsCommand::readName(){
        try {
                string thisOutputDir = outputDir;
                if (outputDir == "") {  thisOutputDir += m->hasPath(namefile);  }
-               string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + getOutputFileNameTag("name", namefile);            
+               map<string, string> variables; 
+               variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(namefile));
+        variables["[extension]"] = m->getExtension(namefile);
+               string outputFileName = getOutputFileName("name", variables);   
                ofstream out;
                m->openOutputFile(outputFileName, out);
                
@@ -778,7 +804,10 @@ int RemoveGroupsCommand::readGroup(){
        try {
                string thisOutputDir = outputDir;
                if (outputDir == "") {  thisOutputDir += m->hasPath(groupfile);  }
-               string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + getOutputFileNameTag("group", groupfile);         
+        map<string, string> variables; 
+               variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(groupfile));
+        variables["[extension]"] = m->getExtension(groupfile);
+               string outputFileName = getOutputFileName("group", variables);  
                ofstream out;
                m->openOutputFile(outputFileName, out);
                
@@ -824,7 +853,10 @@ int RemoveGroupsCommand::readCount(){
        try {
                string thisOutputDir = outputDir;
                if (outputDir == "") {  thisOutputDir += m->hasPath(countfile);  }
-               string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(countfile)) + getOutputFileNameTag("count", countfile);
+               map<string, string> variables; 
+               variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(countfile));
+        variables["[extension]"] = m->getExtension(countfile);
+               string outputFileName = getOutputFileName("count", variables);
                
                ofstream out;
                m->openOutputFile(outputFileName, out);
@@ -905,7 +937,10 @@ int RemoveGroupsCommand::readDesign(){
        try {
                string thisOutputDir = outputDir;
                if (outputDir == "") {  thisOutputDir += m->hasPath(designfile);  }
-               string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(designfile)) + getOutputFileNameTag("design", designfile);
+        map<string, string> variables; 
+               variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(designfile));
+        variables["[extension]"] = m->getExtension(designfile);
+               string outputFileName = getOutputFileName("design", variables);
                
                ofstream out;
                m->openOutputFile(outputFileName, out);
@@ -953,7 +988,10 @@ int RemoveGroupsCommand::readTax(){
        try {
                string thisOutputDir = outputDir;
                if (outputDir == "") {  thisOutputDir += m->hasPath(taxfile);  }
-               string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + getOutputFileNameTag("taxonomy", taxfile);
+               map<string, string> variables; 
+               variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(taxfile));
+        variables["[extension]"] = m->getExtension(taxfile);
+               string outputFileName = getOutputFileName("taxonomy", variables);
                ofstream out;
                m->openOutputFile(outputFileName, out);