]> git.donarmstrong.com Git - mothur.git/blobdiff - countseqscommand.cpp
sffinfo bug with flow grams right index when clipQualRight=0
[mothur.git] / countseqscommand.cpp
index 210dd9641a1736f8b0ea509f96b210dbbe3bd2e1..dfa012eeff0f0f49c778c00b2098348a489f1855 100644 (file)
 #include "countseqscommand.h"
 #include "groupmap.h"
 #include "sharedutilities.h"
+#include "counttable.h"
 
 //**********************************************************************************************************************
 vector<string> CountSeqsCommand::setParameters(){      
        try {
-               CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pname);
-               CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pgroup);
-        CommandParameter plarge("large", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(plarge);
-               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 pname("name", "InputTypes", "", "", "none", "none", "none","count",false,true,true); parameters.push_back(pname);
+               CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none","",false,false,true); parameters.push_back(pgroup);
+        CommandParameter plarge("large", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(plarge);
+               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);          }
@@ -34,7 +35,7 @@ vector<string> CountSeqsCommand::setParameters(){
 string CountSeqsCommand::getHelpString(){      
        try {
                string helpString = "";
-               helpString += "The count.seqs aka. make.table command reads a name file and outputs a .count.table file.  You may also provide a group file to get the counts broken down by group.\n";
+               helpString += "The count.seqs aka. make.table command reads a name file and outputs a .count_table file.  You may also provide a group file to get the counts broken down by group.\n";
                helpString += "The groups parameter allows you to indicate which groups you want to include in the counts, by default all groups in your groupfile are used.\n";
         helpString += "The large parameter indicates the name and group files are too large to fit in RAM.\n";
                helpString += "When you use the groups parameter and a sequence does not represent any sequences from the groups you specify it is not included in the .count.summary file.\n";
@@ -49,24 +50,19 @@ string CountSeqsCommand::getHelpString(){
        }
 }
 //**********************************************************************************************************************
-string CountSeqsCommand::getOutputFileNameTag(string type, string inputName=""){       
-       try {
-        string outputFileName = "";
-               map<string, vector<string> >::iterator it;
+string CountSeqsCommand::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 == "counttable") {  outputFileName =  "count.table"; }
-            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, "CountSeqsCommand", "getOutputFileNameTag");
-               exit(1);
-       }
+        if (type == "count") {  pattern = "[filename],count_table"; }
+        else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
+        
+        return pattern;
+    }
+    catch(exception& e) {
+        m->errorOut(e, "CountSeqsCommand", "getOutputPattern");
+        exit(1);
+    }
 }
 //**********************************************************************************************************************
 CountSeqsCommand::CountSeqsCommand(){  
@@ -74,7 +70,7 @@ CountSeqsCommand::CountSeqsCommand(){
                abort = true; calledHelp = true; 
                setParameters();
                vector<string> tempOutNames;
-               outputTypes["counttable"] = tempOutNames;
+               outputTypes["count"] = tempOutNames;
        }
        catch(exception& e) {
                m->errorOut(e, "CountSeqsCommand", "CountSeqsCommand");
@@ -106,7 +102,7 @@ CountSeqsCommand::CountSeqsCommand(string option)  {
                        
                        //initialize outputTypes
                        vector<string> tempOutNames;
-                       outputTypes["counttable"] = tempOutNames;
+                       outputTypes["count"] = tempOutNames;
                        
                        
                        //if the user changes the input directory command factory will send this info to us in the output parameter 
@@ -170,16 +166,18 @@ int CountSeqsCommand::execute(){
                
                if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
                
-               string outputFileName = outputDir + m->getRootName(m->getSimpleName(namefile)) + getOutputFileNameTag("counttable");
+        map<string, string> variables; 
+        variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(namefile));
+               string outputFileName = getOutputFileName("count", variables);
                
         int total = 0;
         if (!large) { total = processSmall(outputFileName); }
         else { total = processLarge(outputFileName);  }
-                               
+        
                if (m->control_pressed) { m->mothurRemove(outputFileName); return 0; }
                
         //set rabund file as new current rabundfile
-               itTypes = outputTypes.find("counttable");
+               itTypes = outputTypes.find("count");
                if (itTypes != outputTypes.end()) {
                        if ((itTypes->second).size() != 0) { string current = (itTypes->second)[0]; m->setCountTableFile(current); }
                }
@@ -187,7 +185,7 @@ int CountSeqsCommand::execute(){
         m->mothurOutEndLine();
                m->mothurOut("Total number of sequences: " + toString(total)); m->mothurOutEndLine();
                m->mothurOutEndLine();
-               m->mothurOut("Output File Name: "); m->mothurOutEndLine();
+               m->mothurOut("Output File Names: "); m->mothurOutEndLine();
                m->mothurOut(outputFileName); m->mothurOutEndLine();    
                m->mothurOutEndLine();
                
@@ -204,8 +202,8 @@ int CountSeqsCommand::execute(){
 int CountSeqsCommand::processSmall(string outputFileName){
        try {
         ofstream out;
-        m->openOutputFile(outputFileName, out); outputTypes["counttable"].push_back(outputFileName);
-        outputNames.push_back(outputFileName); outputTypes["counttable"].push_back(outputFileName);
+        m->openOutputFile(outputFileName, out); outputTypes["count"].push_back(outputFileName);
+        outputNames.push_back(outputFileName); outputTypes["count"].push_back(outputFileName);
                out << "Representative_Sequence\ttotal\t";
         
         GroupMap* groupMap;
@@ -238,7 +236,11 @@ int CountSeqsCommand::processSmall(string outputFileName){
                        
                        string firstCol, secondCol;
                        in >> firstCol; m->gobble(in); in >> secondCol; m->gobble(in);
-                       
+            //cout << firstCol << '\t' << secondCol << endl;
+            m->checkName(firstCol);
+            m->checkName(secondCol);
+                       //cout << firstCol << '\t' << secondCol << endl;
+           
                        vector<string> names;
                        m->splitAtChar(secondCol, names, ',');
                        
@@ -298,7 +300,7 @@ int CountSeqsCommand::processLarge(string outputFileName){
         for (set<string>::iterator it = namesOfGroups.begin(); it != namesOfGroups.end(); it++) { initial[(*it)] = 0;  }
         ofstream out;
         m->openOutputFile(outputFileName, out); 
-        outputNames.push_back(outputFileName); outputTypes["counttable"].push_back(outputFileName);
+        outputNames.push_back(outputFileName); outputTypes["count"].push_back(outputFileName);
                out << "Representative_Sequence\ttotal\t";
         if (groupfile == "") { out << endl; }
         
@@ -437,6 +439,8 @@ map<int, string> CountSeqsCommand::processNameFile(string name) {
                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
                 
                 if (pairDone) { 
+                    m->checkName(firstCol);
+                    m->checkName(secondCol);
                     //parse names into vector
                     vector<string> theseNames;
                     m->splitAtComma(secondCol, theseNames);
@@ -450,6 +454,28 @@ map<int, string> CountSeqsCommand::processNameFile(string name) {
                in.close();
         out.close();
                
+        if (rest != "") {
+            vector<string> pieces = m->splitWhiteSpace(rest);
+            
+            for (int i = 0; i < pieces.size(); i++) {
+                if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
+                else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
+                
+                if (pairDone) { 
+                    m->checkName(firstCol);
+                    m->checkName(secondCol);
+                    //parse names into vector
+                    vector<string> theseNames;
+                    m->splitAtComma(secondCol, theseNames);
+                    for (int i = 0; i < theseNames.size(); i++) {  out << theseNames[i] << '\t' << count << endl;  }
+                    indexToNames[count] = firstCol;
+                    pairDone = false; 
+                    count++;
+                }
+            }
+
+        }
+        
         return indexToNames;
     }
        catch(exception& e) {
@@ -489,6 +515,7 @@ map<int, string> CountSeqsCommand::getGroupNames(string filename, set<string>& n
                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
                 
                 if (pairDone) { 
+                    m->checkName(firstCol);
                     it = groupIndex.find(secondCol);
                     if (it == groupIndex.end()) { //add group, assigning the group and number so we can use vectors above
                         groupIndex[secondCol] = count;
@@ -502,6 +529,27 @@ map<int, string> CountSeqsCommand::getGroupNames(string filename, set<string>& n
                }
                in.close();
         out.close();
+        
+        if (rest != "") {
+            vector<string> pieces = m->splitWhiteSpace(rest);
+            
+            for (int i = 0; i < pieces.size(); i++) {
+                if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
+                else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
+                
+                if (pairDone) { 
+                    m->checkName(firstCol);
+                    it = groupIndex.find(secondCol);
+                    if (it == groupIndex.end()) { //add group, assigning the group and number so we can use vectors above
+                        groupIndex[secondCol] = count;
+                        count++;
+                    }
+                    out << firstCol << '\t' << groupIndex[secondCol] << endl; 
+                    namesOfGroups.insert(secondCol);
+                    pairDone = false; 
+                }
+            }
+        }
                
         for (it = groupIndex.begin(); it != groupIndex.end(); it++) {  indexToGroups[it->second] = it->first;  }