]> git.donarmstrong.com Git - mothur.git/blobdiff - counttable.cpp
added oligos, pdiffs, bdiffs, ldiffs, sdiffs, tiffs parameters to sffinfo to allow...
[mothur.git] / counttable.cpp
index 7f08509897f0765823b4e2e7b65f837809821c45..376bd73487b4031077f7dd26b1c58b648d9ff04d 100644 (file)
@@ -8,7 +8,23 @@
 
 #include "counttable.h"
 
-
+/************************************************************/
+bool CountTable::testGroups(string file) {
+    try {
+        m = MothurOut::getInstance(); hasGroups = false; total = 0;
+        ifstream in;
+        m->openInputFile(file, in);
+    
+        string headers = m->getline(in); m->gobble(in);
+        vector<string> columnHeaders = m->splitWhiteSpace(headers);
+        if (columnHeaders.size() > 2) { hasGroups = true;   }
+        return hasGroups;
+    }
+       catch(exception& e) {
+               m->errorOut(e, "CountTable", "readTable");
+               exit(1);
+       }
+}
 /************************************************************/
 int CountTable::readTable(string file) {
     try {
@@ -42,7 +58,8 @@ int CountTable::readTable(string file) {
             
             if (m->control_pressed) { break; }
             
-            in >> name >> thisTotal; m->gobble(in);
+            in >> name; m->gobble(in); in >> thisTotal; m->gobble(in);
+            if (m->debug) { m->mothurOut("[DEBUG]: " + name + '\t' + toString(thisTotal) + "\n"); }
             
             //if group info, then read it
             vector<int> groupCounts; groupCounts.resize(numGroups, 0);
@@ -157,7 +174,110 @@ int CountTable::getNumSeqs(string seqName) {
        }
 }
 /************************************************************/
-//returns names of seqs
+//returns unique index for sequence like get in NameAssignment
+int CountTable::get(string seqName) {
+    try {
+        
+        map<string, int>::iterator it = indexNameMap.find(seqName);
+        if (it == indexNameMap.end()) {
+            m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
+        }else { return it->second; }
+        
+        return -1;
+    }
+       catch(exception& e) {
+               m->errorOut(e, "CountTable", "get");
+               exit(1);
+       }
+}
+/************************************************************/
+//add seqeunce without group info
+int CountTable::push_back(string seqName) {
+    try {
+        map<string, int>::iterator it = indexNameMap.find(seqName);
+        if (it == indexNameMap.end()) {
+            if (hasGroups) {  m->mothurOut("[ERROR]: Your count table has groups and I have no group information for " + seqName + "."); m->mothurOutEndLine(); m->control_pressed = true;  }
+            indexNameMap[seqName] = uniques;
+            totals.push_back(1);
+            total++;
+            uniques++;
+        }else {
+            m->mothurOut("[ERROR]: Your count table contains more than 1 sequence named " + seqName + ", sequence names must be unique. Please correct."); m->mothurOutEndLine(); m->control_pressed = true;
+        }
+        
+        return 0;
+    }
+       catch(exception& e) {
+               m->errorOut(e, "CountTable", "push_back");
+               exit(1);
+       }
+}
+/************************************************************/
+//add seqeunce without group info
+int CountTable::push_back(string seqName, int thisTotal) {
+    try {
+        map<string, int>::iterator it = indexNameMap.find(seqName);
+        if (it == indexNameMap.end()) {
+            if (hasGroups) {  m->mothurOut("[ERROR]: Your count table has groups and I have no group information for " + seqName + "."); m->mothurOutEndLine(); m->control_pressed = true;  }
+            indexNameMap[seqName] = uniques;
+            totals.push_back(thisTotal);
+            total+=thisTotal;
+            uniques++;
+        }else {
+            m->mothurOut("[ERROR]: Your count table contains more than 1 sequence named " + seqName + ", sequence names must be unique. Please correct."); m->mothurOutEndLine(); m->control_pressed = true;
+        }
+        
+        return 0;
+    }
+       catch(exception& e) {
+               m->errorOut(e, "CountTable", "push_back");
+               exit(1);
+       }
+}
+/************************************************************/
+//add sequence with group info
+int CountTable::push_back(string seqName, vector<int> groupCounts) {
+    try {
+        map<string, int>::iterator it = indexNameMap.find(seqName);
+        if (it == indexNameMap.end()) {
+            if ((hasGroups) && (groupCounts.size() != getNumGroups())) {  m->mothurOut("[ERROR]: Your count table has a " + toString(getNumGroups()) + " groups and " + seqName + " has " + toString(groupCounts.size()) + ", please correct."); m->mothurOutEndLine(); m->control_pressed = true;  }
+            int thisTotal = 0;
+            for (int i = 0; i < getNumGroups(); i++) {   totalGroups[i] += groupCounts[i];  thisTotal += groupCounts[i]; }
+            indexNameMap[seqName] = uniques;
+            totals.push_back(thisTotal);
+            total+= thisTotal;
+            uniques++;
+        }else {
+            m->mothurOut("[ERROR]: Your count table contains more than 1 sequence named " + seqName + ", sequence names must be unique. Please correct."); m->mothurOutEndLine(); m->control_pressed = true;
+        }
+        
+        return 0;
+    }
+       catch(exception& e) {
+               m->errorOut(e, "CountTable", "push_back");
+               exit(1);
+       }
+}
+
+/************************************************************/
+//create ListVector from uniques
+ListVector CountTable::getListVector() {
+    try {
+        ListVector list(indexNameMap.size());
+        for (map<string, int>::iterator it = indexNameMap.begin(); it != indexNameMap.end(); it++) { 
+            if (m->control_pressed) { break; }
+            list.set(it->second, it->first); 
+        }
+        return list;
+    }
+       catch(exception& e) {
+               m->errorOut(e, "CountTable", "getListVector");
+               exit(1);
+       }
+}
+
+/************************************************************/
+//returns the names of all unique sequences in file
 vector<string> CountTable::getNamesOfSeqs() {
     try {
         vector<string> names;