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 names of seqs
162 vector<string> CountTable::getNamesOfSeqs() {
164 vector<string> names;
165 for (map<string, int>::iterator it = indexNameMap.begin(); it != indexNameMap.end(); it++) {
166 names.push_back(it->first);
171 catch(exception& e) {
172 m->errorOut(e, "CountTable", "getNamesOfSeqs");
176 /************************************************************/
177 //returns names of seqs
178 int CountTable::mergeCounts(string seq1, string seq2) {
180 map<string, int>::iterator it = indexNameMap.find(seq1);
181 if (it == indexNameMap.end()) {
182 m->mothurOut("[ERROR]: " + seq1 + " is not in your count table. Please correct.\n"); m->control_pressed = true;
184 map<string, int>::iterator it2 = indexNameMap.find(seq2);
185 if (it2 == indexNameMap.end()) {
186 m->mothurOut("[ERROR]: " + seq2 + " is not in your count table. Please correct.\n"); m->control_pressed = true;
189 for (int i = 0; i < groups.size(); i++) {
190 counts[it->second][i] += counts[it2->second][i];
191 counts[it2->second][i] = 0;
193 totals[it->second] += totals[it2->second];
194 totals[it2->second] = 0;
196 indexNameMap.erase(it2);
202 catch(exception& e) {
203 m->errorOut(e, "CountTable", "getNamesOfSeqs");
208 /************************************************************/