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; m->gobble(in); in >> thisTotal; m->gobble(in);
46 if (m->debug) { m->mothurOut("[DEBUG]: " + name + '\t' + toString(thisTotal) + "\n"); }
48 //if group info, then read it
49 vector<int> groupCounts; groupCounts.resize(numGroups, 0);
50 for (int i = 0; i < numGroups; i++) { int thisIndex = indexGroupMap[originalGroupIndexes[i]]; in >> groupCounts[thisIndex]; m->gobble(in); totalGroups[thisIndex] += groupCounts[thisIndex]; }
52 map<string, int>::iterator it = indexNameMap.find(name);
53 if (it == indexNameMap.end()) {
54 if (hasGroups) { counts.push_back(groupCounts); }
55 indexNameMap[name] = uniques;
56 totals.push_back(thisTotal);
61 m->mothurOut("[ERROR]: Your count table contains more than 1 sequence named " + name + ", sequence names must be unique. Please correct."); m->mothurOutEndLine();
66 if (error) { m->control_pressed = true; }
71 m->errorOut(e, "CountTable", "readTable");
75 /************************************************************/
76 //group counts for a seq
77 vector<int> CountTable::getGroupCounts(string seqName) {
81 map<string, int>::iterator it = indexNameMap.find(seqName);
82 if (it == indexNameMap.end()) {
83 m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
85 temp = counts[it->second];
87 }else{ m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n"); m->control_pressed = true; }
92 m->errorOut(e, "CountTable", "getGroupCounts");
96 /************************************************************/
97 //total number of sequences for the group
98 int CountTable::getGroupCount(string groupName) {
101 map<string, int>::iterator it = indexGroupMap.find(groupName);
102 if (it == indexGroupMap.end()) {
103 m->mothurOut("[ERROR]: " + groupName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
105 return totalGroups[it->second];
107 }else{ m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n"); m->control_pressed = true; }
111 catch(exception& e) {
112 m->errorOut(e, "CountTable", "getGroupCount");
116 /************************************************************/
117 //total number of sequences for the seq for the group
118 int CountTable::getGroupCount(string seqName, string groupName) {
121 map<string, int>::iterator it = indexGroupMap.find(groupName);
122 if (it == indexGroupMap.end()) {
123 m->mothurOut("[ERROR]: " + groupName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
125 map<string, int>::iterator it2 = indexNameMap.find(seqName);
126 if (it2 == indexNameMap.end()) {
127 m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
129 return counts[it2->second][it->second];
132 }else{ m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n"); m->control_pressed = true; }
136 catch(exception& e) {
137 m->errorOut(e, "CountTable", "getGroupCount");
141 /************************************************************/
142 //total number of seqs represented by seq
143 int CountTable::getNumSeqs(string seqName) {
146 map<string, int>::iterator it = indexNameMap.find(seqName);
147 if (it == indexNameMap.end()) {
148 m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
150 return totals[it->second];
155 catch(exception& e) {
156 m->errorOut(e, "CountTable", "getNumSeqs");
160 /************************************************************/
161 //returns unique index for sequence like get in NameAssignment
162 int CountTable::get(string seqName) {
165 map<string, int>::iterator it = indexNameMap.find(seqName);
166 if (it == indexNameMap.end()) {
167 m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
168 }else { return it->second; }
172 catch(exception& e) {
173 m->errorOut(e, "CountTable", "get");
177 /************************************************************/
178 //add seqeunce without group info
179 int CountTable::push_back(string seqName) {
181 map<string, int>::iterator it = indexNameMap.find(seqName);
182 if (it == indexNameMap.end()) {
183 if (hasGroups) { m->mothurOut("[ERROR]: Your count table has groups and I have no group information for " + seqName + "."); m->mothurOutEndLine(); m->control_pressed = true; }
184 indexNameMap[seqName] = uniques;
189 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;
194 catch(exception& e) {
195 m->errorOut(e, "CountTable", "push_back");
199 /************************************************************/
200 //add seqeunce without group info
201 int CountTable::push_back(string seqName, int thisTotal) {
203 map<string, int>::iterator it = indexNameMap.find(seqName);
204 if (it == indexNameMap.end()) {
205 if (hasGroups) { m->mothurOut("[ERROR]: Your count table has groups and I have no group information for " + seqName + "."); m->mothurOutEndLine(); m->control_pressed = true; }
206 indexNameMap[seqName] = uniques;
207 totals.push_back(thisTotal);
211 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;
216 catch(exception& e) {
217 m->errorOut(e, "CountTable", "push_back");
221 /************************************************************/
222 //add sequence with group info
223 int CountTable::push_back(string seqName, vector<int> groupCounts) {
225 map<string, int>::iterator it = indexNameMap.find(seqName);
226 if (it == indexNameMap.end()) {
227 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; }
229 for (int i = 0; i < getNumGroups(); i++) { totalGroups[i] += groupCounts[i]; thisTotal += groupCounts[i]; }
230 indexNameMap[seqName] = uniques;
231 totals.push_back(thisTotal);
235 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;
240 catch(exception& e) {
241 m->errorOut(e, "CountTable", "push_back");
246 /************************************************************/
247 //create ListVector from uniques
248 ListVector CountTable::getListVector() {
250 ListVector list(indexNameMap.size());
251 for (map<string, int>::iterator it = indexNameMap.begin(); it != indexNameMap.end(); it++) {
252 if (m->control_pressed) { break; }
253 list.set(it->second, it->first);
257 catch(exception& e) {
258 m->errorOut(e, "CountTable", "getListVector");
263 /************************************************************/
264 //returns the names of all unique sequences in file
265 vector<string> CountTable::getNamesOfSeqs() {
267 vector<string> names;
268 for (map<string, int>::iterator it = indexNameMap.begin(); it != indexNameMap.end(); it++) {
269 names.push_back(it->first);
274 catch(exception& e) {
275 m->errorOut(e, "CountTable", "getNamesOfSeqs");
279 /************************************************************/
280 //returns names of seqs
281 int CountTable::mergeCounts(string seq1, string seq2) {
283 map<string, int>::iterator it = indexNameMap.find(seq1);
284 if (it == indexNameMap.end()) {
285 m->mothurOut("[ERROR]: " + seq1 + " is not in your count table. Please correct.\n"); m->control_pressed = true;
287 map<string, int>::iterator it2 = indexNameMap.find(seq2);
288 if (it2 == indexNameMap.end()) {
289 m->mothurOut("[ERROR]: " + seq2 + " is not in your count table. Please correct.\n"); m->control_pressed = true;
292 for (int i = 0; i < groups.size(); i++) {
293 counts[it->second][i] += counts[it2->second][i];
294 counts[it2->second][i] = 0;
296 totals[it->second] += totals[it2->second];
297 totals[it2->second] = 0;
299 indexNameMap.erase(it2);
305 catch(exception& e) {
306 m->errorOut(e, "CountTable", "getNamesOfSeqs");
311 /************************************************************/