]> git.donarmstrong.com Git - mothur.git/blob - counttable.cpp
Merge branch 'master' of https://github.com/mothur/mothur.git into development
[mothur.git] / counttable.cpp
1 //
2 //  counttable.cpp
3 //  Mothur
4 //
5 //  Created by Sarah Westcott on 6/26/12.
6 //  Copyright (c) 2012 Schloss Lab. All rights reserved.
7 //
8
9 #include "counttable.h"
10
11
12 /************************************************************/
13 int CountTable::readTable(string file) {
14     try {
15         filename = file;
16         ifstream in;
17         m->openInputFile(filename, in);
18         
19         string headers = m->getline(in); m->gobble(in);
20         vector<string> columnHeaders = m->splitWhiteSpace(headers);
21         
22         int numGroups = 0;
23         groups.clear();
24         totalGroups.clear();
25         indexGroupMap.clear();
26         indexNameMap.clear();
27         counts.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);
35         
36         bool error = false;
37         string name;
38         int thisTotal;
39         uniques = 0;
40         total = 0;
41         while (!in.eof()) {
42             
43             if (m->control_pressed) { break; }
44             
45             in >> name >> thisTotal; m->gobble(in);
46             
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];  }
50             
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);
56                 total += thisTotal;
57                 uniques++;
58             }else {
59                 error = true;
60                 m->mothurOut("[ERROR]: Your count table contains more than 1 sequence named " + name + ", sequence names must be unique. Please correct."); m->mothurOutEndLine(); 
61             }
62         }
63         in.close();
64         
65         if (error) { m->control_pressed = true; }
66         
67         return 0;
68     }
69         catch(exception& e) {
70                 m->errorOut(e, "CountTable", "readTable");
71                 exit(1);
72         }
73 }
74 /************************************************************/
75 //group counts for a seq
76 vector<int> CountTable::getGroupCounts(string seqName) {
77     try {
78         vector<int> temp;
79         if (hasGroups) {
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;
83             }else { 
84                 temp = counts[it->second];
85             }
86         }else{  m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n"); m->control_pressed = true; }
87         
88         return temp;
89     }
90         catch(exception& e) {
91                 m->errorOut(e, "CountTable", "getGroupCounts");
92                 exit(1);
93         }
94 }
95 /************************************************************/
96 //total number of sequences for the group
97 int CountTable::getGroupCount(string groupName) {
98     try {
99         if (hasGroups) {
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;
103             }else { 
104                 return totalGroups[it->second];
105             }
106         }else{  m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n");  m->control_pressed = true; }
107
108         return 0;
109     }
110         catch(exception& e) {
111                 m->errorOut(e, "CountTable", "getGroupCount");
112                 exit(1);
113         }
114 }
115 /************************************************************/
116 //total number of sequences for the seq for the group
117 int CountTable::getGroupCount(string seqName, string groupName) {
118     try {
119         if (hasGroups) {
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;
123             }else { 
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;
127                 }else { 
128                     return counts[it2->second][it->second];
129                 }
130             }
131         }else{  m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n");  m->control_pressed = true; }
132         
133         return 0;
134     }
135         catch(exception& e) {
136                 m->errorOut(e, "CountTable", "getGroupCount");
137                 exit(1);
138         }
139 }
140 /************************************************************/
141 //total number of seqs represented by seq
142 int CountTable::getNumSeqs(string seqName) {
143     try {
144                 
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;
148         }else { 
149             return totals[it->second];
150         }
151
152         return 0;
153     }
154         catch(exception& e) {
155                 m->errorOut(e, "CountTable", "getNumSeqs");
156                 exit(1);
157         }
158 }
159 /************************************************************/
160 //returns names of seqs
161 vector<string> CountTable::getNamesOfSeqs() {
162     try {
163         vector<string> names;
164         for (map<string, int>::iterator it = indexNameMap.begin(); it != indexNameMap.end(); it++) {
165             names.push_back(it->first);
166         }
167                 
168         return names;
169     }
170         catch(exception& e) {
171                 m->errorOut(e, "CountTable", "getNamesOfSeqs");
172                 exit(1);
173         }
174 }
175 /************************************************************/
176 //returns names of seqs
177 int CountTable::mergeCounts(string seq1, string seq2) {
178     try {
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;
182         }else { 
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;
186             }else { 
187                 //merge data
188                 for (int i = 0; i < groups.size(); i++) {
189                     counts[it->second][i] += counts[it2->second][i];
190                     counts[it2->second][i] = 0;
191                 }
192                 totals[it->second] += totals[it2->second];
193                 totals[it2->second] = 0;
194                 uniques--;
195                 indexNameMap.erase(it2); 
196             }
197         }
198         
199         return 0;
200     }
201         catch(exception& e) {
202                 m->errorOut(e, "CountTable", "getNamesOfSeqs");
203                 exit(1);
204         }
205 }
206
207 /************************************************************/
208
209