]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundvector.cpp
added bootstrap.shared command and fixed some bugs with heatmap
[mothur.git] / sharedrabundvector.cpp
1 /*
2  *  sharedvector.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 12/5/08.
6  *  Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10
11 using namespace std;
12
13 #include "sharedrabundvector.h" 
14 #include "sabundvector.hpp"
15 #include "ordervector.hpp"
16
17
18 /***********************************************************************/
19
20 SharedRAbundVector::SharedRAbundVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0) {};
21
22 /***********************************************************************/
23
24 SharedRAbundVector::SharedRAbundVector(int n) : DataVector(), maxRank(0), numBins(n), numSeqs(0) {
25                 individual newGuy;
26                 //initialize data
27                 for (int i=0; i< n; i++) {
28                         newGuy.bin = i;
29                         newGuy.abundance = 0;
30                         data.push_back(newGuy);
31                 }
32 };
33
34 /***********************************************************************
35
36 SharedRAbundVector::SharedRAbundVector(string id, vector<individual> rav) : DataVector(id), data(rav) {
37         try {
38                 numBins = 0;
39                 maxRank = 0;
40                 numSeqs = 0;
41                 
42                 for(int i=0;i<data.size();i++){
43                         if(data[i].abundance != 0)              {       numBins = i+1;          }
44                         if(data[i].abundance > maxRank) {       maxRank = data[i].abundance;    }
45                         numSeqs += data[i].abundance;
46                 }
47         }
48         catch(exception& e) {
49                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
50                 exit(1);
51         }
52         catch(...) {
53                 cout << "An unknown error has occurred in the SharedRAbundVector class function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
54                 exit(1);
55         }
56 }
57
58
59 /***********************************************************************
60
61
62 SharedRAbundVector::SharedRAbundVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
63         try {
64                 int i, num;
65                 string holdLabel, group
66                 individual newGuy;
67                 
68                 f >> label >> group >> num;
69                 
70                 //initialize data
71                 for (i=0; i<hold; i++) {
72                         newGuy = new individual;
73                         newGuy.abundance = 0;
74                         newGuy.bin = i;
75                         data.push_back(newGuy);
76                 }
77                 int inputData;
78         
79                 for(int i=0;i<hold;i++){
80                         f >> inputData;
81                         set(i, inputData);
82                 }
83         }
84         catch(exception& e) {
85                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
86                 exit(1);
87         }
88         catch(...) {
89                 cout << "An unknown error has occurred in the SharedRAbundVector class function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
90                 exit(1);
91         }
92 }
93
94 /***********************************************************************/
95
96 SharedRAbundVector::~SharedRAbundVector() {
97
98 }
99
100 /***********************************************************************/
101
102 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
103         try {
104                 int oldBinSize = data[binNumber].abundance;
105                 data[binNumber].abundance = newBinSize;
106                 data[binNumber].group = groupname;
107         
108                 if(newBinSize > maxRank)        {       maxRank = newBinSize;   }
109         
110                 numSeqs += (newBinSize - oldBinSize);
111         }
112         catch(exception& e) {
113                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
114                 exit(1);
115         }
116         catch(...) {
117                 cout << "An unknown error has occurred in the SharedRAbundVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
118                 exit(1);
119         }
120 }
121 /***********************************************************************/
122
123 void SharedRAbundVector::setData(vector <individual> newData){
124         data = newData;
125 }
126
127 /***********************************************************************/
128
129 int SharedRAbundVector::getAbundance(int index){
130         return data[index].abundance;
131         
132 }
133 /***********************************************************************/
134
135 int SharedRAbundVector::numNZ(){
136         int sum = 0;
137         for(int i = 1; i < numBins; i++)
138                 if(data[i].abundance > 0)
139                         sum++;
140         return sum;
141 }
142 /***********************************************************************/
143
144 void SharedRAbundVector::sortD(){
145         struct individual indObj;
146         sort(data.begin()+1, data.end(), indObj);
147 }
148 /***********************************************************************/
149
150 individual SharedRAbundVector::get(int index){
151         return data[index];
152         
153 }
154 /***********************************************************************/
155
156 vector <individual> SharedRAbundVector::getData(){
157         return data;
158 }
159 /***********************************************************************/
160
161 void SharedRAbundVector::push_back(int binSize, int otu, string groupName){
162         try {
163                 individual newGuy;
164                 newGuy.abundance = binSize;
165                 newGuy.group = groupName;
166                 newGuy.bin = otu;
167                 
168                 data.push_back(newGuy);
169                 numBins++;
170         
171                 if(binSize > maxRank){
172                         maxRank = binSize;
173                 }
174         
175                 numSeqs += binSize;
176         }
177         catch(exception& e) {
178                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_back. 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 SharedRAbundVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
183                 exit(1);
184         }
185 }
186
187 /***********************************************************************/
188
189 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
190         try {
191                 individual newGuy;
192                 newGuy.abundance = binSize;
193                 newGuy.group = groupName;
194                 newGuy.bin = otu;
195                 
196                 data.insert(data.begin()+otu, newGuy);
197                 numBins++;
198         
199                 if(binSize > maxRank){
200                         maxRank = binSize;
201                 }
202         
203                 numSeqs += binSize;
204         }
205         catch(exception& e) {
206                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function insert. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
207                 exit(1);
208         }
209         catch(...) {
210                 cout << "An unknown error has occurred in the SharedRAbundVector class function insert. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
211                 exit(1);
212         }
213 }
214
215 /***********************************************************************/
216
217 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
218         try {
219                 individual newGuy;
220                 newGuy.abundance = binSize;
221                 newGuy.group = groupName;
222                 newGuy.bin = otu;
223                 
224                 data.insert(data.begin(), newGuy);
225                 numBins++;
226         
227                 if(binSize > maxRank){
228                         maxRank = binSize;
229                 }
230         
231                 numSeqs += binSize;
232         }
233         catch(exception& e) {
234                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_front. 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 SharedRAbundVector class function push_front. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
239                 exit(1);
240         }
241 }
242
243 /***********************************************************************/
244 void SharedRAbundVector::pop_back(){
245         numSeqs -= data[data.size()-1].abundance;
246         numBins--;
247         return data.pop_back();
248 }
249
250 /***********************************************************************/
251
252
253 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
254         return data.rbegin();                           
255 }
256
257 /***********************************************************************/
258
259 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
260         return data.rend();                                     
261 }
262
263 /***********************************************************************/
264 void SharedRAbundVector::resize(int size){
265         
266         data.resize(size);
267 }
268
269 /***********************************************************************/
270
271 int SharedRAbundVector::size(){
272         return data.size();
273 }
274
275 /***********************************************************************/
276 void SharedRAbundVector::print(ostream& output){
277         try {
278                 output << numBins << '\t';
279         
280                 for(int i=0;i<data.size();i++){         output << data[i].abundance << '\t';            }
281                 output << endl;
282         }
283         catch(exception& e) {
284                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
285                 exit(1);
286         }
287         catch(...) {
288                 cout << "An unknown error has occurred in the SharedRAbundVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
289                 exit(1);
290         }
291 }
292 /***********************************************************************/
293 string SharedRAbundVector::getGroup(){
294         return group;
295 }
296
297 /***********************************************************************/
298
299 void SharedRAbundVector::setGroup(string groupName){
300         group = groupName;
301 }
302 /***********************************************************************/
303 int SharedRAbundVector::getGroupIndex()  { return index; }
304 /***********************************************************************/
305 void SharedRAbundVector::setGroupIndex(int vIndex)      { index = vIndex; }
306 /***********************************************************************/
307 int SharedRAbundVector::getNumBins(){
308         return numBins;
309 }
310
311 /***********************************************************************/
312
313 int SharedRAbundVector::getNumSeqs(){
314         return numSeqs;
315 }
316
317 /***********************************************************************/
318
319 int SharedRAbundVector::getMaxRank(){
320         return maxRank;
321 }
322 /***********************************************************************/
323
324 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
325         return *this;                   
326 }
327 /***********************************************************************/
328
329 RAbundVector SharedRAbundVector::getRAbundVector() {
330         try {
331                 RAbundVector rav(data.size());
332                 
333                 for (int i = 0; i < data.size(); i++) {
334                         rav.set(i, data[i].abundance);
335                 }
336         
337                 return rav;
338         }
339         catch(exception& e) {
340                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
341                 exit(1);
342         }
343         catch(...) {
344                 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
345                 exit(1);
346         }
347 }
348 /***********************************************************************/
349
350 RAbundVector SharedRAbundVector::getRAbundVector2() {
351         try {
352                 RAbundVector rav;
353                 for(int i = 0; i < numBins; i++)
354                         if(data[i].abundance != 0)
355                                 rav.push_back(data[i].abundance-1);
356                 return rav;
357         }
358         catch(exception& e) {
359                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
360                 exit(1);
361         }
362         catch(...) {
363                 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
364                 exit(1);
365         }
366 }
367 /***********************************************************************/
368
369 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
370         try {
371                 SharedSAbundVector sav(maxRank+1);
372                 
373                 for(int i=0;i<data.size();i++){
374                         int abund = data[i].abundance;
375                         sav.set(abund, sav.getAbundance(abund) + 1, group);
376                 }
377                 
378                 sav.set(0, 0, group);
379                 sav.setLabel(label);
380                 sav.setGroup(group);
381                 
382                 return sav;
383         }
384         catch(exception& e) {
385                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
386                 exit(1);
387         }
388         catch(...) {
389                 cout << "An unknown error has occurred in the SharedRAbundVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
390                 exit(1);
391         }
392 }
393 /***********************************************************************/
394
395 SAbundVector SharedRAbundVector::getSAbundVector() {
396         try {
397                 SAbundVector sav(maxRank+1);
398                 
399                 for(int i=0;i<data.size();i++){
400                         int abund = data[i].abundance;
401                         sav.set(abund, sav.get(abund) + 1);
402                 }
403                 sav.set(0, 0);
404                 sav.setLabel(label);
405                 return sav;
406         }
407         catch(exception& e) {
408                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
409                 exit(1);
410         }
411         catch(...) {
412                 cout << "An unknown error has occurred in the SharedRAbundVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
413                 exit(1);
414         }
415 }
416
417 /***********************************************************************/
418
419 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
420         try {
421                 SharedOrderVector ov;
422         
423                 for(int i=0;i<data.size();i++){
424                         for(int j=0;j<data[i].abundance;j++){
425                                 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
426                         }
427                 }
428                 random_shuffle(ov.begin(), ov.end());
429
430                 ov.setLabel(label);     
431                 return ov;
432         }
433         catch(exception& e) {
434                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
435                 exit(1);
436         }
437         catch(...) {
438                 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
439                 exit(1);
440         }
441 }
442 /***********************************************************************/
443
444 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
445         try {
446                 OrderVector ov;
447         
448                 for(int i=0;i<data.size();i++){
449                         for(int j=0;j<data[i].abundance;j++){
450                                 ov.push_back(i);
451                         }
452                 }
453                 random_shuffle(ov.begin(), ov.end());
454
455                 ov.setLabel(label);     
456                 return ov;
457         }
458         catch(exception& e) {
459                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
460                 exit(1);
461         }
462         catch(...) {
463                 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
464                 exit(1);
465         }
466 }
467
468 /***********************************************************************/
469