5 // Created by Sarah Westcott on 6/26/12.
6 // Copyright (c) 2012 Schloss Lab. All rights reserved.
9 #include "counttable.h"
11 /************************************************************/
12 bool CountTable::testGroups(string file) {
14 m = MothurOut::getInstance(); hasGroups = false; total = 0;
16 m->openInputFile(file, in);
18 string headers = m->getline(in); m->gobble(in);
19 vector<string> columnHeaders = m->splitWhiteSpace(headers);
20 if (columnHeaders.size() > 2) { hasGroups = true; }
24 m->errorOut(e, "CountTable", "readTable");
28 /************************************************************/
29 int CountTable::readTable(string file) {
33 m->openInputFile(filename, in);
35 string headers = m->getline(in); m->gobble(in);
36 vector<string> columnHeaders = m->splitWhiteSpace(headers);
41 indexGroupMap.clear();
44 map<int, string> originalGroupIndexes;
45 if (columnHeaders.size() > 2) { hasGroups = true; numGroups = columnHeaders.size() - 2; }
46 for (int i = 2; i < columnHeaders.size(); i++) { groups.push_back(columnHeaders[i]); originalGroupIndexes[i-2] = columnHeaders[i]; totalGroups.push_back(0); }
47 //sort groups to keep consistent with how we store the groups in groupmap
48 sort(groups.begin(), groups.end());
49 for (int i = 0; i < groups.size(); i++) { indexGroupMap[groups[i]] = i; }
50 m->setAllGroups(groups);
59 if (m->control_pressed) { break; }
61 in >> name; m->gobble(in); in >> thisTotal; m->gobble(in);
62 if (m->debug) { m->mothurOut("[DEBUG]: " + name + '\t' + toString(thisTotal) + "\n"); }
64 //if group info, then read it
65 vector<int> groupCounts; groupCounts.resize(numGroups, 0);
66 for (int i = 0; i < numGroups; i++) { int thisIndex = indexGroupMap[originalGroupIndexes[i]]; in >> groupCounts[thisIndex]; m->gobble(in); totalGroups[thisIndex] += groupCounts[thisIndex]; }
68 map<string, int>::iterator it = indexNameMap.find(name);
69 if (it == indexNameMap.end()) {
70 if (hasGroups) { counts.push_back(groupCounts); }
71 indexNameMap[name] = uniques;
72 totals.push_back(thisTotal);
77 m->mothurOut("[ERROR]: Your count table contains more than 1 sequence named " + name + ", sequence names must be unique. Please correct."); m->mothurOutEndLine();
82 if (error) { m->control_pressed = true; }
87 m->errorOut(e, "CountTable", "readTable");
91 /************************************************************/
92 //group counts for a seq
93 vector<int> CountTable::getGroupCounts(string seqName) {
97 map<string, int>::iterator it = indexNameMap.find(seqName);
98 if (it == indexNameMap.end()) {
99 m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
101 temp = counts[it->second];
103 }else{ m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n"); m->control_pressed = true; }
107 catch(exception& e) {
108 m->errorOut(e, "CountTable", "getGroupCounts");
112 /************************************************************/
113 //total number of sequences for the group
114 int CountTable::getGroupCount(string groupName) {
117 map<string, int>::iterator it = indexGroupMap.find(groupName);
118 if (it == indexGroupMap.end()) {
119 m->mothurOut("[ERROR]: " + groupName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
121 return totalGroups[it->second];
123 }else{ m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n"); m->control_pressed = true; }
127 catch(exception& e) {
128 m->errorOut(e, "CountTable", "getGroupCount");
132 /************************************************************/
133 //total number of sequences for the seq for the group
134 int CountTable::getGroupCount(string seqName, string groupName) {
137 map<string, int>::iterator it = indexGroupMap.find(groupName);
138 if (it == indexGroupMap.end()) {
139 m->mothurOut("[ERROR]: " + groupName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
141 map<string, int>::iterator it2 = indexNameMap.find(seqName);
142 if (it2 == indexNameMap.end()) {
143 m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
145 return counts[it2->second][it->second];
148 }else{ m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n"); m->control_pressed = true; }
152 catch(exception& e) {
153 m->errorOut(e, "CountTable", "getGroupCount");
157 /************************************************************/
158 //total number of seqs represented by seq
159 int CountTable::getNumSeqs(string seqName) {
162 map<string, int>::iterator it = indexNameMap.find(seqName);
163 if (it == indexNameMap.end()) {
164 m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
166 return totals[it->second];
171 catch(exception& e) {
172 m->errorOut(e, "CountTable", "getNumSeqs");
176 /************************************************************/
177 //returns unique index for sequence like get in NameAssignment
178 int CountTable::get(string seqName) {
181 map<string, int>::iterator it = indexNameMap.find(seqName);
182 if (it == indexNameMap.end()) {
183 m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
184 }else { return it->second; }
188 catch(exception& e) {
189 m->errorOut(e, "CountTable", "get");
193 /************************************************************/
194 //add seqeunce without group info
195 int CountTable::push_back(string seqName) {
197 map<string, int>::iterator it = indexNameMap.find(seqName);
198 if (it == indexNameMap.end()) {
199 if (hasGroups) { m->mothurOut("[ERROR]: Your count table has groups and I have no group information for " + seqName + "."); m->mothurOutEndLine(); m->control_pressed = true; }
200 indexNameMap[seqName] = uniques;
205 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;
210 catch(exception& e) {
211 m->errorOut(e, "CountTable", "push_back");
215 /************************************************************/
216 //add seqeunce without group info
217 int CountTable::push_back(string seqName, int thisTotal) {
219 map<string, int>::iterator it = indexNameMap.find(seqName);
220 if (it == indexNameMap.end()) {
221 if (hasGroups) { m->mothurOut("[ERROR]: Your count table has groups and I have no group information for " + seqName + "."); m->mothurOutEndLine(); m->control_pressed = true; }
222 indexNameMap[seqName] = uniques;
223 totals.push_back(thisTotal);
227 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;
232 catch(exception& e) {
233 m->errorOut(e, "CountTable", "push_back");
237 /************************************************************/
238 //add sequence with group info
239 int CountTable::push_back(string seqName, vector<int> groupCounts) {
241 map<string, int>::iterator it = indexNameMap.find(seqName);
242 if (it == indexNameMap.end()) {
243 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; }
245 for (int i = 0; i < getNumGroups(); i++) { totalGroups[i] += groupCounts[i]; thisTotal += groupCounts[i]; }
246 indexNameMap[seqName] = uniques;
247 totals.push_back(thisTotal);
251 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;
256 catch(exception& e) {
257 m->errorOut(e, "CountTable", "push_back");
262 /************************************************************/
263 //create ListVector from uniques
264 ListVector CountTable::getListVector() {
266 ListVector list(indexNameMap.size());
267 for (map<string, int>::iterator it = indexNameMap.begin(); it != indexNameMap.end(); it++) {
268 if (m->control_pressed) { break; }
269 list.set(it->second, it->first);
273 catch(exception& e) {
274 m->errorOut(e, "CountTable", "getListVector");
279 /************************************************************/
280 //returns the names of all unique sequences in file
281 vector<string> CountTable::getNamesOfSeqs() {
283 vector<string> names;
284 for (map<string, int>::iterator it = indexNameMap.begin(); it != indexNameMap.end(); it++) {
285 names.push_back(it->first);
290 catch(exception& e) {
291 m->errorOut(e, "CountTable", "getNamesOfSeqs");
295 /************************************************************/
296 //returns names of seqs
297 int CountTable::mergeCounts(string seq1, string seq2) {
299 map<string, int>::iterator it = indexNameMap.find(seq1);
300 if (it == indexNameMap.end()) {
301 m->mothurOut("[ERROR]: " + seq1 + " is not in your count table. Please correct.\n"); m->control_pressed = true;
303 map<string, int>::iterator it2 = indexNameMap.find(seq2);
304 if (it2 == indexNameMap.end()) {
305 m->mothurOut("[ERROR]: " + seq2 + " is not in your count table. Please correct.\n"); m->control_pressed = true;
308 for (int i = 0; i < groups.size(); i++) {
309 counts[it->second][i] += counts[it2->second][i];
310 counts[it2->second][i] = 0;
312 totals[it->second] += totals[it2->second];
313 totals[it2->second] = 0;
315 indexNameMap.erase(it2);
321 catch(exception& e) {
322 m->errorOut(e, "CountTable", "getNamesOfSeqs");
327 /************************************************************/