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 //create ListVector from uniques
179 ListVector CountTable::getListVector() {
181 ListVector list(indexNameMap.size());
182 for (map<string, int>::iterator it = indexNameMap.begin(); it != indexNameMap.end(); it++) {
183 if (m->control_pressed) { break; }
184 list.set(it->second, it->first);
188 catch(exception& e) {
189 m->errorOut(e, "CountTable", "getListVector");
194 /************************************************************/
195 //returns the names of all unique sequences in file
196 vector<string> CountTable::getNamesOfSeqs() {
198 vector<string> names;
199 for (map<string, int>::iterator it = indexNameMap.begin(); it != indexNameMap.end(); it++) {
200 names.push_back(it->first);
205 catch(exception& e) {
206 m->errorOut(e, "CountTable", "getNamesOfSeqs");
210 /************************************************************/
211 //returns names of seqs
212 int CountTable::mergeCounts(string seq1, string seq2) {
214 map<string, int>::iterator it = indexNameMap.find(seq1);
215 if (it == indexNameMap.end()) {
216 m->mothurOut("[ERROR]: " + seq1 + " is not in your count table. Please correct.\n"); m->control_pressed = true;
218 map<string, int>::iterator it2 = indexNameMap.find(seq2);
219 if (it2 == indexNameMap.end()) {
220 m->mothurOut("[ERROR]: " + seq2 + " is not in your count table. Please correct.\n"); m->control_pressed = true;
223 for (int i = 0; i < groups.size(); i++) {
224 counts[it->second][i] += counts[it2->second][i];
225 counts[it2->second][i] = 0;
227 totals[it->second] += totals[it2->second];
228 totals[it2->second] = 0;
230 indexNameMap.erase(it2);
236 catch(exception& e) {
237 m->errorOut(e, "CountTable", "getNamesOfSeqs");
242 /************************************************************/