]> git.donarmstrong.com Git - mothur.git/blob - sharedordervector.cpp
heatmap
[mothur.git] / sharedordervector.cpp
1 /*
2  *  sharedSharedOrderVector.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 12/9/08.
6  *  Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 using namespace std;
11
12
13 #include "sharedordervector.h"
14 #include "utilities.hpp"
15
16
17 /***********************************************************************/
18
19 SharedOrderVector::SharedOrderVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0)  {}
20
21 /***********************************************************************/
22
23 SharedOrderVector::SharedOrderVector(string id, vector<individual>  ov) : 
24                                                                                         DataVector(id), data(ov)
25 {
26                 updateStats();
27 }
28
29 /***********************************************************************/
30 //This function is used to read a .shared file for the collect.shared, rarefaction.shared and summary.shared commands
31 //if you don't use a list and groupfile.  
32
33 SharedOrderVector::SharedOrderVector(ifstream& f) : DataVector() {
34         try {
35                 globaldata = GlobalData::getInstance();
36                 maxRank = 0; numBins = 0; numSeqs = 0;
37                 
38                 if (globaldata->gGroupmap == NULL) {  groupmap = new GroupMap(); }
39                 
40                 int num, inputData, pos, count;
41                 count = 0;  numSeqs = 0;
42                 string holdLabel, nextLabel, groupN;
43                 individual newguy;
44                 
45                 //read in first row since you know there is at least 1 group.
46                 f >> label >> groupN >> num;
47                 holdLabel = label;
48                 
49                 if (globaldata->gGroupmap == NULL) { 
50                         //save group in groupmap
51                         groupmap->namesOfGroups.push_back(groupN);
52                         groupmap->groupIndex[groupN] = 0;
53                 }
54                 
55                 for(int i=0;i<num;i++){
56                         f >> inputData;
57                         
58                         for (int j = 0; j < inputData; j++) {
59                                 push_back(i+1, i+1, groupN);
60                                 numSeqs++;
61                         }
62                 }
63                 
64                 //save position in file in case next line is a new label.
65                 pos = f.tellg();
66                 
67                 if (f.eof() != true) { f >> nextLabel; }
68                 
69                 //read the rest of the groups info in
70                 while ((nextLabel == holdLabel) && (f.eof() != true)) {
71                         f >> groupN >> num;
72                         count++;
73                         
74                         if (globaldata->gGroupmap == NULL) { 
75                                 //save group in groupmap
76                                 groupmap->namesOfGroups.push_back(groupN);
77                                 groupmap->groupIndex[groupN] = count;
78                         }
79                         
80                         for(int i=0;i<num;i++){
81                                 f >> inputData;
82                                 
83                                 for (int j = 0; j < inputData; j++) {
84                                         push_back(i+1, i+1, groupN);
85                                         numSeqs++;
86                                 }
87                         }
88                         
89                         //save position in file in case next line is a new label.
90                         pos = f.tellg();
91         
92                         if (f.eof() != true) { f >> nextLabel; }
93
94                 }
95                 
96                 //put file pointer back since you are now at a new distance label
97                 f.seekg(pos, ios::beg);
98         
99                 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap; }
100         
101                 updateStats();
102         }
103         catch(exception& e) {
104                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function SharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
105                 exit(1);
106         }
107         catch(...) {
108                 cout << "An unknown error has occurred in the SharedOrderVector class function SharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
109                 exit(1);
110         }
111 }
112 /***********************************************************************/
113
114 int SharedOrderVector::getNumBins(){
115         if(needToUpdate == 1){  updateStats();  }
116         return numBins;
117 }
118
119 /***********************************************************************/
120
121 int SharedOrderVector::getNumSeqs(){
122         if(needToUpdate == 1){  updateStats();  }
123         return numSeqs;
124 }
125
126 /***********************************************************************/
127
128 int SharedOrderVector::getMaxRank(){
129         if(needToUpdate == 1){  updateStats();  }
130         return maxRank;
131 }
132
133
134 /***********************************************************************/
135
136
137
138 void SharedOrderVector::set(int index, int binNumber, int abund, string groupName){
139         
140         data[index].group = groupName;
141         data[index].bin = binNumber;
142         data[index].abundance = abund;
143         needToUpdate = 1;
144         
145 }
146
147 /***********************************************************************/
148
149 individual SharedOrderVector::get(int index){
150         return data[index];                     
151 }
152
153
154 /***********************************************************************/
155
156 void SharedOrderVector::push_back(int binNumber, int abund, string groupName){
157         individual newGuy;
158         newGuy.group = groupName;
159         newGuy.abundance = abund;
160         newGuy.bin = binNumber;
161         data.push_back(newGuy);
162         needToUpdate = 1;
163         
164 }
165
166 /***********************************************************************/
167
168 void SharedOrderVector::print(ostream& output){
169         try {
170                 output << label << '\t' << numSeqs << '\t';
171         
172                 for(int i=0;i<data.size();i++){
173                         output << data[i].bin << '\t';
174                 }
175                 output << endl;
176         }
177         catch(exception& e) {
178                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
179                 exit(1);
180         }
181         catch(...) {
182                 cout << "An unknown error has occurred in the SharedOrderVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
183                 exit(1);
184         }
185 }
186
187
188 /***********************************************************************/
189
190 void SharedOrderVector::resize(int){
191         cout << "resize() did nothing in class SharedOrderVector";
192 }
193
194 /***********************************************************************/
195
196
197 vector<individual>::iterator SharedOrderVector::begin(){
198         return data.begin();    
199 }
200
201 /***********************************************************************/
202
203 vector<individual>::iterator SharedOrderVector::end(){
204         return data.end();              
205 }
206
207 /***********************************************************************/
208
209 int SharedOrderVector::size(){
210         return data.size();                                     
211 }
212
213 /***********************************************************************/
214
215 RAbundVector SharedOrderVector::getRAbundVector(){
216         try {
217                 RAbundVector rav(data.size());
218         
219                 for(int i=0;i<numSeqs;i++){
220                         rav.set(data[i].bin, rav.get(data[i].bin) + 1);
221                 }       
222                 sort(rav.rbegin(), rav.rend());
223                 for(int i=numSeqs-1;i>=0;i--){
224                         if(rav.get(i) == 0){    rav.pop_back(); }
225                         else{
226                                 break;
227                         }
228                 }
229                 rav.setLabel(label);
230
231                 return rav;
232         }
233         catch(exception& e) {
234                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
235                 exit(1);
236         }
237         catch(...) {
238                 cout << "An unknown error has occurred in the SharedOrderVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
239                 exit(1);
240         }       
241 }
242 /***********************************************************************/
243
244 OrderVector SharedOrderVector::getOrderVector(map<string,int>* nameMap = NULL) {
245         try {
246                 OrderVector ov;
247         
248                 for (int i = 0; i < data.size(); i++) {
249                         ov.push_back(data[i].bin);
250                 }
251                 
252                 random_shuffle(ov.begin(), ov.end());
253
254                 ov.setLabel(label);     
255                 return ov;
256         }
257         catch(exception& e) {
258                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
259                 exit(1);
260         }
261         catch(...) {
262                 cout << "An unknown error has occurred in the SharedOrderVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
263                 exit(1);
264         }
265 }
266
267
268 /***********************************************************************/
269
270 SAbundVector SharedOrderVector::getSAbundVector(){
271         
272         RAbundVector rav(this->getRAbundVector());
273         return rav.getSAbundVector();
274
275 }
276 /***********************************************************************/
277 SharedRAbundVector SharedOrderVector::getSharedRAbundVector(string group) {
278         try {
279                 SharedRAbundVector sharedRav(data.size());
280                 
281                 sharedRav.setLabel(label);
282                 sharedRav.setGroup(group);
283                 
284                 for (int i = 0; i < data.size(); i++) {
285                         if (data[i].group == group) {
286                                 sharedRav.set(data[i].abundance, sharedRav.getAbundance(data[i].abundance) + 1, data[i].group);
287                         }
288                 }
289                 return sharedRav;
290         }
291         catch(exception& e) {
292                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
293                 exit(1);
294         }
295         catch(...) {
296                 cout << "An unknown error has occurred in the SharedOrderVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
297                 exit(1);
298         }
299         
300 }
301 /***********************************************************************/
302 SharedSAbundVector SharedOrderVector::getSharedSAbundVector(string group) {
303         try {
304                 
305                 SharedRAbundVector sharedRav(this->getSharedRAbundVector(group));
306                 return sharedRav.getSharedSAbundVector();
307                                 
308         }
309         catch(exception& e) {
310                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
311                 exit(1);
312         }
313         catch(...) {
314                 cout << "An unknown error has occurred in the SharedOrderVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
315                 exit(1);
316         }
317         
318 }
319
320 /***********************************************************************/
321
322 SharedOrderVector SharedOrderVector::getSharedOrderVector(){
323         return *this;                   
324 }
325
326 /***********************************************************************/
327
328 void SharedOrderVector::updateStats(){
329         try {
330                 needToUpdate = 0;
331                 numSeqs = 0;
332                 numBins = 0;
333                 maxRank = 0;
334         
335                 for(int i=0;i<data.size();i++){
336                         if(data[i].bin != -1){
337                                 numSeqs++;
338                         }
339                 }
340         
341                 vector<individual> hold(numSeqs);
342         
343                 for(int i=0;i<numSeqs;i++){
344                         if(data[i].bin != -1){
345                                 hold[data[i].bin].bin = hold[data[i].bin].bin+1;
346                         }
347                 }       
348
349                 for(int i=0;i<numSeqs;i++){
350                         if(data[i].bin > numBins) { numBins = data[i].bin;      } 
351                         if(data[i].abundance > maxRank) {       maxRank = data[i].abundance;    }
352                 }
353                 numBins++; //if you have 10 bins largest .bin is 9 since we start at 0.
354         }
355         catch(exception& e) {
356                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function updateStats. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
357                 exit(1);
358         }
359         catch(...) {
360                 cout << "An unknown error has occurred in the SharedOrderVector class function updateStats. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
361                 exit(1);
362         }       
363 }
364
365 /***********************************************************************/
366
367