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