]> git.donarmstrong.com Git - mothur.git/blobdiff - countgroupscommand.cpp
fixes while testing 1.33.0
[mothur.git] / countgroupscommand.cpp
index 24b4214dd5600ef0f0625ba7acf0da2a8f328108..ce3358ef43b9cee9d7887314fb0378cb203b7ebe 100644 (file)
 //**********************************************************************************************************************
 vector<string> CountGroupsCommand::setParameters(){    
        try {
-               CommandParameter pshared("shared", "InputTypes", "", "", "sharedGroup", "sharedGroup", "none",false,false); parameters.push_back(pshared);
-               CommandParameter pgroup("group", "InputTypes", "", "", "sharedGroup", "sharedGroup", "none",false,false); parameters.push_back(pgroup);
-               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 pshared("shared", "InputTypes", "", "", "sharedGroup", "sharedGroup", "none","summary",false,false,true); parameters.push_back(pshared);
+               CommandParameter pgroup("group", "InputTypes", "", "", "sharedGroup", "sharedGroup", "none","summary",false,false,true); parameters.push_back(pgroup);
+        CommandParameter pcount("count", "InputTypes", "", "", "sharedGroup", "sharedGroup", "none","summary",false,false,true); parameters.push_back(pcount);
+               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);          }
@@ -31,10 +32,25 @@ vector<string> CountGroupsCommand::setParameters(){
        }
 }
 //**********************************************************************************************************************
+string CountGroupsCommand::getOutputPattern(string type) {
+    try {
+        string pattern = "";
+        
+        if (type == "summary") {  pattern = "[filename],count.summary"; }
+        else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
+        
+        return pattern;
+    }
+    catch(exception& e) {
+        m->errorOut(e, "PrimerDesignCommand", "getOutputPattern");
+        exit(1);
+    }
+}
+//**********************************************************************************************************************
 string CountGroupsCommand::getHelpString(){    
        try {
                string helpString = "";
-               helpString += "The count.groups command counts sequences from a specfic group or set of groups from the following file types: group or shared file.\n";
+               helpString += "The count.groups command counts sequences from a specific group or set of groups from the following file types: group, count or shared file.\n";
                helpString += "The count.groups command parameters are accnos, group, shared and groups. You must provide a group or shared file.\n";
                helpString += "The accnos parameter allows you to provide a file containing the list of groups.\n";
                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";
@@ -54,6 +70,8 @@ CountGroupsCommand::CountGroupsCommand(){
        try {
                abort = true; calledHelp = true;
                setParameters();
+        vector<string> tempOutNames;
+               outputTypes["summary"] = tempOutNames;
        }
        catch(exception& e) {
                m->errorOut(e, "CountGroupsCommand", "CountGroupsCommand");
@@ -114,8 +132,18 @@ CountGroupsCommand::CountGroupsCommand(string option)  {
                                        //if the user has not given a path then, add inputdir. else leave path alone.
                                        if (path == "") {       parameters["shared"] = inputDir + it->second;           }
                                }
+                
+                it = parameters.find("count");
+                               //user has given a template file
+                               if(it != parameters.end()){ 
+                                       path = m->hasPath(it->second);
+                                       //if the user has not given a path then, add inputdir. else leave path alone.
+                                       if (path == "") {       parameters["count"] = inputDir + it->second;            }
+                               }
                        }
                        
+            vector<string> tempOutNames;
+            outputTypes["summary"] = tempOutNames;
                        
                        //check for required parameters
                        accnosfile = validParameter.validFile(parameters, "accnos", true);
@@ -138,9 +166,23 @@ CountGroupsCommand::CountGroupsCommand(string option)  {
                        groupfile = validParameter.validFile(parameters, "group", true);
                        if (groupfile == "not open") { groupfile = ""; abort = true; }
                        else if (groupfile == "not found") {    groupfile = ""; }
-                       else { m->setGroupFile(groupfile); }    
+                       else { m->setGroupFile(groupfile); }
+            
+            countfile = validParameter.validFile(parameters, "count", true);
+            if (countfile == "not open") { countfile = ""; abort = true; }
+            else if (countfile == "not found") { countfile = "";  }    
+            else { 
+                m->setCountTableFile(countfile); 
+                CountTable ct;
+                if (!ct.testGroups(countfile)) { m->mothurOut("[ERROR]: Your count file does not have any group information, aborting."); m->mothurOutEndLine(); abort=true; }
+            }
+            
+            if ((groupfile != "") && (countfile != "")) {
+                m->mothurOut("[ERROR]: you may only use one of the following: group or count."); m->mothurOutEndLine(); abort=true;
+            }
+
                        
-                       if ((sharedfile == "") && (groupfile == "")) { 
+                       if ((sharedfile == "") && (groupfile == "") && (countfile == "")) { 
                                //give priority to shared, then group
                                sharedfile = m->getSharedFile(); 
                                if (sharedfile != "") {  m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
@@ -148,7 +190,11 @@ CountGroupsCommand::CountGroupsCommand(string option)  {
                                        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 or sharedfile and one is required."); m->mothurOutEndLine(); abort = true;
+                                               countfile = m->getCountTableFile(); 
+                        if (countfile != "") { m->mothurOut("Using " + countfile + " as input file for the count parameter."); m->mothurOutEndLine(); }
+                        else { 
+                            m->mothurOut("You have no current groupfile, countfile or sharedfile and one is required."); m->mothurOutEndLine(); abort = true;
+                        }
                                        }
                                }
                        }
@@ -170,9 +216,18 @@ int CountGroupsCommand::execute(){
                if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
                
                //get groups you want to remove
-               if (accnosfile != "") { readAccnos(); }
+               if (accnosfile != "") { m->readAccnos(accnosfile, Groups); m->setGroups(Groups); }
                
                if (groupfile != "") {
+            map<string, string> variables; 
+            string thisOutputDir = outputDir;
+            if (outputDir == "") {  thisOutputDir += m->hasPath(groupfile);  }
+            variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(groupfile));
+            string outputFileName = getOutputFileName("summary", variables);
+            outputNames.push_back(outputFileName); outputTypes["summary"].push_back(outputFileName);
+            ofstream out;
+            m->openOutputFile(outputFileName, out);
+            
                        GroupMap groupMap(groupfile);
                        groupMap.readMap();
                        
@@ -182,9 +237,48 @@ int CountGroupsCommand::execute(){
                        vector<string> nameGroups = groupMap.getNamesOfGroups();
                        util.setGroups(Groups, nameGroups);
                        
+            int total = 0;
                        for (int i = 0; i < Groups.size(); i++) {
-                               m->mothurOut(Groups[i] + " contains " + toString(groupMap.getNumSeqs(Groups[i])) + "."); m->mothurOutEndLine();
+                int num = groupMap.getNumSeqs(Groups[i]);
+                total += num;
+                               m->mothurOut(Groups[i] + " contains " + toString(num) + "."); m->mothurOutEndLine();
+                out << Groups[i] << '\t' << num << endl;
                        }
+            out.close();
+            m->mothurOut("\nTotal seqs: " + toString(total) + "."); m->mothurOutEndLine();
+               }
+        
+        if (m->control_pressed) { return 0; }
+        
+        if (countfile != "") {
+            map<string, string> variables; 
+            string thisOutputDir = outputDir;
+            if (outputDir == "") {  thisOutputDir += m->hasPath(countfile);  }
+            variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(countfile));
+            string outputFileName = getOutputFileName("summary", variables);
+            outputNames.push_back(outputFileName); outputTypes["summary"].push_back(outputFileName);
+            ofstream out;
+            m->openOutputFile(outputFileName, out);
+            
+                       CountTable ct;
+                       ct.readTable(countfile, true, false);
+            
+                       //make sure groups are valid
+                       //takes care of user setting groupNames that are invalid or setting groups=all
+                       SharedUtil util;
+                       vector<string> nameGroups = ct.getNamesOfGroups();
+                       util.setGroups(Groups, nameGroups);
+                       
+            int total = 0;
+                       for (int i = 0; i < Groups.size(); i++) {
+                int num = ct.getGroupCount(Groups[i]);
+                total += num;
+                               m->mothurOut(Groups[i] + " contains " + toString(num) + "."); m->mothurOutEndLine();
+                out << Groups[i] << '\t' << num << endl;
+                       }
+            out.close();
+            
+            m->mothurOut("\nTotal seqs: " + toString(total) + "."); m->mothurOutEndLine();
                }
                
                if (m->control_pressed) { return 0; }
@@ -193,12 +287,33 @@ int CountGroupsCommand::execute(){
                        InputData input(sharedfile, "sharedfile");
                        vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
                        
+            map<string, string> variables; 
+            string thisOutputDir = outputDir;
+            if (outputDir == "") {  thisOutputDir += m->hasPath(countfile);  }
+            variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(countfile));
+            string outputFileName = getOutputFileName("summary", variables);
+            outputNames.push_back(outputFileName); outputTypes["summary"].push_back(outputFileName);
+            ofstream out;
+            m->openOutputFile(outputFileName, out);
+            
+            int total = 0;
                        for (int i = 0; i < lookup.size(); i++) {
-                               m->mothurOut(lookup[i]->getGroup() + " contains " + toString(lookup[i]->getNumSeqs()) + "."); m->mothurOutEndLine();
+                int num = lookup[i]->getNumSeqs();
+                total += num;
+                               m->mothurOut(lookup[i]->getGroup() + " contains " + toString(num) + "."); m->mothurOutEndLine();
                                delete lookup[i];
-                       }                       
+                out << lookup[i]->getGroup() << '\t' << num << endl;
+                       }
+            out.close();
+                       
+            m->mothurOut("\nTotal seqs: " + toString(total) + "."); m->mothurOutEndLine();
                }
-                               
+                       
+        m->mothurOutEndLine();
+               m->mothurOut("Output File Names: "); m->mothurOutEndLine();
+               for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }       
+               m->mothurOutEndLine();
+        
                return 0;               
        }
        
@@ -208,31 +323,5 @@ int CountGroupsCommand::execute(){
        }
 }
 //**********************************************************************************************************************
-void CountGroupsCommand::readAccnos(){
-       try {
-               Groups.clear();
-               
-               ifstream in;
-               m->openInputFile(accnosfile, in);
-               string name;
-               
-               while(!in.eof()){
-                       in >> name;
-                       
-                       Groups.push_back(name);
-                       
-                       m->gobble(in);
-               }
-               in.close();             
-               
-               m->setGroups(Groups);
-               
-       }
-       catch(exception& e) {
-               m->errorOut(e, "CountGroupsCommand", "readAccnos");
-               exit(1);
-       }
-}
-//**********************************************************************************************************************