5 // Created by Sarah Westcott on 6/26/12.
6 // Copyright (c) 2012 Schloss Lab. All rights reserved.
9 #include "counttable.h"
12 /************************************************************/
13 int CountTable::readTable(string file) {
17 m->openInputFile(filename, in);
19 string headers = m->getline(in); m->gobble(in);
20 vector<string> columnHeaders = m->splitWhiteSpace(headers);
25 indexGroupMap.clear();
28 map<int, string> originalGroupIndexes;
29 if (columnHeaders.size() > 2) { hasGroups = true; numGroups = columnHeaders.size() - 2; }
30 for (int i = 2; i < columnHeaders.size(); i++) { groups.push_back(columnHeaders[i]); originalGroupIndexes[i-2] = columnHeaders[i]; totalGroups.push_back(0); }
31 //sort groups to keep consistent with how we store the groups in groupmap
32 sort(groups.begin(), groups.end());
33 for (int i = 0; i < groups.size(); i++) { indexGroupMap[groups[i]] = i; }
34 m->setAllGroups(groups);
43 if (m->control_pressed) { break; }
45 in >> name >> thisTotal; m->gobble(in);
47 //if group info, then read it
48 vector<int> groupCounts; groupCounts.resize(numGroups, 0);
49 for (int i = 0; i < numGroups; i++) { int thisIndex = indexGroupMap[originalGroupIndexes[i]]; in >> groupCounts[thisIndex]; m->gobble(in); totalGroups[thisIndex] += groupCounts[thisIndex]; }
51 map<string, int>::iterator it = indexNameMap.find(name);
52 if (it == indexNameMap.end()) {
53 if (hasGroups) { counts.push_back(groupCounts); }
54 indexNameMap[name] = uniques;
55 totals.push_back(thisTotal);
60 m->mothurOut("[ERROR]: Your count table contains more than 1 sequence named " + name + ", sequence names must be unique. Please correct."); m->mothurOutEndLine();
65 if (error) { m->control_pressed = true; }
70 m->errorOut(e, "CountTable", "readTable");
74 /************************************************************/
75 //group counts for a seq
76 vector<int> CountTable::getGroupCounts(string seqName) {
80 map<string, int>::iterator it = indexNameMap.find(seqName);
81 if (it == indexNameMap.end()) {
82 m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
84 temp = counts[it->second];
86 }else{ m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n"); m->control_pressed = true; }
91 m->errorOut(e, "CountTable", "getGroupCounts");
95 /************************************************************/
96 //total number of sequences for the group
97 int CountTable::getGroupCount(string groupName) {
100 map<string, int>::iterator it = indexGroupMap.find(groupName);
101 if (it == indexGroupMap.end()) {
102 m->mothurOut("[ERROR]: " + groupName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
104 return totalGroups[it->second];
106 }else{ m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n"); m->control_pressed = true; }
110 catch(exception& e) {
111 m->errorOut(e, "CountTable", "getGroupCount");
115 /************************************************************/
116 //total number of sequences for the seq for the group
117 int CountTable::getGroupCount(string seqName, string groupName) {
120 map<string, int>::iterator it = indexGroupMap.find(groupName);
121 if (it == indexGroupMap.end()) {
122 m->mothurOut("[ERROR]: " + groupName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
124 map<string, int>::iterator it2 = indexNameMap.find(seqName);
125 if (it2 == indexNameMap.end()) {
126 m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
128 return counts[it2->second][it->second];
131 }else{ m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n"); m->control_pressed = true; }
135 catch(exception& e) {
136 m->errorOut(e, "CountTable", "getGroupCount");
140 /************************************************************/
141 //total number of seqs represented by seq
142 int CountTable::getNumSeqs(string seqName) {
145 map<string, int>::iterator it = indexNameMap.find(seqName);
146 if (it == indexNameMap.end()) {
147 m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
149 return totals[it->second];
154 catch(exception& e) {
155 m->errorOut(e, "CountTable", "getNumSeqs");
159 /************************************************************/
160 //returns names of seqs
161 vector<string> CountTable::getNamesOfSeqs() {
163 vector<string> names;
164 for (map<string, int>::iterator it = indexNameMap.begin(); it != indexNameMap.end(); it++) {
165 names.push_back(it->first);
170 catch(exception& e) {
171 m->errorOut(e, "CountTable", "getNamesOfSeqs");
175 /************************************************************/
176 //returns names of seqs
177 int CountTable::mergeCounts(string seq1, string seq2) {
179 map<string, int>::iterator it = indexNameMap.find(seq1);
180 if (it == indexNameMap.end()) {
181 m->mothurOut("[ERROR]: " + seq1 + " is not in your count table. Please correct.\n"); m->control_pressed = true;
183 map<string, int>::iterator it2 = indexNameMap.find(seq2);
184 if (it2 == indexNameMap.end()) {
185 m->mothurOut("[ERROR]: " + seq2 + " is not in your count table. Please correct.\n"); m->control_pressed = true;
188 for (int i = 0; i < groups.size(); i++) {
189 counts[it->second][i] += counts[it2->second][i];
190 counts[it2->second][i] = 0;
192 totals[it->second] += totals[it2->second];
193 totals[it2->second] = 0;
195 indexNameMap.erase(it2);
201 catch(exception& e) {
202 m->errorOut(e, "CountTable", "getNamesOfSeqs");
207 /************************************************************/