]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundvector.cpp
fixed bug with getting sharedrabunds for venn, heatmap and tree.shared commands,...
[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 #include "sharedutilities.h"
17
18
19 /***********************************************************************/
20
21 SharedRAbundVector::SharedRAbundVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0) {};
22
23 /***********************************************************************/
24
25 SharedRAbundVector::SharedRAbundVector(int n) : DataVector(), maxRank(0), numBins(n), numSeqs(0) {
26                 individual newGuy;
27                 //initialize data
28                 for (int i=0; i< n; i++) {
29                         newGuy.bin = i;
30                         newGuy.abundance = 0;
31                         data.push_back(newGuy);
32                 }
33 };
34
35 /***********************************************************************
36
37 SharedRAbundVector::SharedRAbundVector(string id, vector<individual> rav) : DataVector(id), data(rav) {
38         try {
39                 numBins = 0;
40                 maxRank = 0;
41                 numSeqs = 0;
42                 
43                 for(int i=0;i<data.size();i++){
44                         if(data[i].abundance != 0)              {       numBins = i+1;          }
45                         if(data[i].abundance > maxRank) {       maxRank = data[i].abundance;    }
46                         numSeqs += data[i].abundance;
47                 }
48         }
49         catch(exception& e) {
50                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
51                 exit(1);
52         }
53         catch(...) {
54                 cout << "An unknown error has occurred in the SharedRAbundVector class function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
55                 exit(1);
56         }
57 }
58
59
60 /***********************************************************************/
61 //reads a shared file
62 SharedRAbundVector::SharedRAbundVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
63         try {
64                 globaldata = GlobalData::getInstance();
65                 
66                 if (globaldata->gGroupmap == NULL) {  groupmap = new GroupMap(); }
67                 
68                 int num, inputData, pos, count;
69                 count = 0;  
70                 string holdLabel, nextLabel, groupN;
71                 individual newguy;
72                 
73                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
74                 lookup.clear();
75                 
76                 //read in first row since you know there is at least 1 group.
77                 f >> label >> groupN >> num;
78                 holdLabel = label;
79                 
80                 //add new vector to lookup
81                 lookup.push_back(new SharedRAbundVector(num));
82                 lookup[0]->setLabel(label);
83                 lookup[0]->setGroup(groupN);
84                 
85                 if (globaldata->gGroupmap == NULL) { 
86                         //save group in groupmap
87                         groupmap->namesOfGroups.push_back(groupN);
88                         groupmap->groupIndex[groupN] = 0;
89                 }
90                 
91                 //fill vector.  data = first sharedrabund in file
92                 for(int i=0;i<num;i++){
93                         f >> inputData;
94                         
95                         lookup[0]->push_back(inputData, i, groupN); //abundance, bin, group
96                         push_back(inputData, i, groupN);
97                         numSeqs += inputData;
98                         numBins++;
99                         if (inputData > maxRank) { maxRank = inputData; }
100                         
101                 }
102                 
103                 //save position in file in case next line is a new label.
104                 pos = f.tellg();
105                 
106                 if (f.eof() != true) { f >> nextLabel; }
107                 
108                 //read the rest of the groups info in
109                 while ((nextLabel == holdLabel) && (f.eof() != true)) {
110                         f >> groupN >> num;
111                         count++;
112                         
113                         if (globaldata->gGroupmap == NULL) { 
114                                 //save group in groupmap
115                                 groupmap->namesOfGroups.push_back(groupN);
116                                 groupmap->groupIndex[groupN] = count;
117                         }
118                         
119                         //add new vector to lookup
120                         lookup.push_back(new SharedRAbundVector(num));
121                         lookup[count]->setLabel(label);
122                         lookup[count]->setGroup(groupN);
123
124                         //fill vector.  
125                         for(int i=0;i<num;i++){
126                                 f >> inputData;
127                                 lookup[count]->push_back(inputData, i, groupN); //abundance, bin, group
128                         }
129                         
130                         //save position in file in case next line is a new label.
131                         pos = f.tellg();
132         
133                         if (f.eof() != true) { f >> nextLabel; }
134                 }
135                 
136                 //put file pointer back since you are now at a new distance label
137                 f.seekg(pos, ios::beg);
138         
139                 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap; }
140                 
141         }
142         catch(exception& e) {
143                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
144                 exit(1);
145         }
146         catch(...) {
147                 cout << "An unknown error has occurred in the SharedRAbundVector class function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
148                 exit(1);
149         }
150 }
151
152 /***********************************************************************/
153
154 SharedRAbundVector::~SharedRAbundVector() {
155
156 }
157
158 /***********************************************************************/
159
160 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
161         try {
162                 int oldBinSize = data[binNumber].abundance;
163                 data[binNumber].abundance = newBinSize;
164                 data[binNumber].group = groupname;
165         
166                 if(newBinSize > maxRank)        {       maxRank = newBinSize;   }
167         
168                 numSeqs += (newBinSize - oldBinSize);
169         }
170         catch(exception& e) {
171                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
172                 exit(1);
173         }
174         catch(...) {
175                 cout << "An unknown error has occurred in the SharedRAbundVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
176                 exit(1);
177         }
178 }
179 /***********************************************************************/
180
181 void SharedRAbundVector::setData(vector <individual> newData){
182         data = newData;
183 }
184
185 /***********************************************************************/
186
187 int SharedRAbundVector::getAbundance(int index){
188         return data[index].abundance;
189         
190 }
191 /***********************************************************************/
192
193 int SharedRAbundVector::numNZ(){
194         int sum = 0;
195         for(int i = 1; i < numBins; i++)
196                 if(data[i].abundance > 0)
197                         sum++;
198         return sum;
199 }
200 /***********************************************************************/
201
202 void SharedRAbundVector::sortD(){
203         struct individual indObj;
204         sort(data.begin()+1, data.end(), indObj);
205 }
206 /***********************************************************************/
207
208 individual SharedRAbundVector::get(int index){
209         return data[index];
210         
211 }
212 /***********************************************************************/
213
214 vector <individual> SharedRAbundVector::getData(){
215         return data;
216 }
217 /***********************************************************************/
218
219 void SharedRAbundVector::push_back(int binSize, int otu, string groupName){
220         try {
221                 individual newGuy;
222                 newGuy.abundance = binSize;
223                 newGuy.group = groupName;
224                 newGuy.bin = otu;
225                 
226                 data.push_back(newGuy);
227                 numBins++;
228         
229                 if(binSize > maxRank){
230                         maxRank = binSize;
231                 }
232         
233                 numSeqs += binSize;
234         }
235         catch(exception& e) {
236                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
237                 exit(1);
238         }
239         catch(...) {
240                 cout << "An unknown error has occurred in the SharedRAbundVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
241                 exit(1);
242         }
243 }
244
245 /***********************************************************************/
246
247 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
248         try {
249                 individual newGuy;
250                 newGuy.abundance = binSize;
251                 newGuy.group = groupName;
252                 newGuy.bin = otu;
253                 
254                 data.insert(data.begin()+otu, newGuy);
255                 numBins++;
256         
257                 if(binSize > maxRank){
258                         maxRank = binSize;
259                 }
260         
261                 numSeqs += binSize;
262         }
263         catch(exception& e) {
264                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function insert. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
265                 exit(1);
266         }
267         catch(...) {
268                 cout << "An unknown error has occurred in the SharedRAbundVector class function insert. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
269                 exit(1);
270         }
271 }
272
273 /***********************************************************************/
274
275 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
276         try {
277                 individual newGuy;
278                 newGuy.abundance = binSize;
279                 newGuy.group = groupName;
280                 newGuy.bin = otu;
281                 
282                 data.insert(data.begin(), newGuy);
283                 numBins++;
284         
285                 if(binSize > maxRank){
286                         maxRank = binSize;
287                 }
288         
289                 numSeqs += binSize;
290         }
291         catch(exception& e) {
292                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_front. 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 SharedRAbundVector class function push_front. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
297                 exit(1);
298         }
299 }
300
301 /***********************************************************************/
302 void SharedRAbundVector::pop_back(){
303         numSeqs -= data[data.size()-1].abundance;
304         numBins--;
305         return data.pop_back();
306 }
307
308 /***********************************************************************/
309
310
311 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
312         return data.rbegin();                           
313 }
314
315 /***********************************************************************/
316
317 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
318         return data.rend();                                     
319 }
320
321 /***********************************************************************/
322 void SharedRAbundVector::resize(int size){
323         
324         data.resize(size);
325 }
326
327 /***********************************************************************/
328
329 int SharedRAbundVector::size(){
330         return data.size();
331 }
332
333 /***********************************************************************/
334 void SharedRAbundVector::print(ostream& output){
335         try {
336                 output << numBins << '\t';
337         
338                 for(int i=0;i<data.size();i++){         output << data[i].abundance << '\t';            }
339                 output << endl;
340         }
341         catch(exception& e) {
342                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
343                 exit(1);
344         }
345         catch(...) {
346                 cout << "An unknown error has occurred in the SharedRAbundVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
347                 exit(1);
348         }
349 }
350 /***********************************************************************/
351 string SharedRAbundVector::getGroup(){
352         return group;
353 }
354
355 /***********************************************************************/
356
357 void SharedRAbundVector::setGroup(string groupName){
358         group = groupName;
359 }
360 /***********************************************************************/
361 int SharedRAbundVector::getGroupIndex()  { return index; }
362 /***********************************************************************/
363 void SharedRAbundVector::setGroupIndex(int vIndex)      { index = vIndex; }
364 /***********************************************************************/
365 int SharedRAbundVector::getNumBins(){
366         return numBins;
367 }
368
369 /***********************************************************************/
370
371 int SharedRAbundVector::getNumSeqs(){
372         return numSeqs;
373 }
374
375 /***********************************************************************/
376
377 int SharedRAbundVector::getMaxRank(){
378         return maxRank;
379 }
380 /***********************************************************************/
381
382 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
383         return *this;                   
384 }
385 /***********************************************************************/
386 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
387         try {
388                 SharedUtil* util;
389                 util = new SharedUtil();
390                 
391                 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
392
393                 for (int i = 0; i < lookup.size(); i++) {
394                         //if this sharedrabund is not from a group the user wants then delete it.
395                         if (util->isValidGroup(lookup[i]->getGroup(), globaldata->Groups) == false) { 
396                                 delete lookup[i]; 
397                                 lookup.erase(lookup.begin()+i); 
398                                 i--; 
399                         }
400                 }
401
402                 return lookup;
403         }
404         catch(exception& e) {
405                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSharedRAbundVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
406                 exit(1);
407         }
408         catch(...) {
409                 cout << "An unknown error has occurred in the SharedRAbundVector class function getSharedRAbundVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
410                 exit(1);
411         }
412 }
413 /***********************************************************************/
414
415 RAbundVector SharedRAbundVector::getRAbundVector() {
416         try {
417                 RAbundVector rav(data.size());
418                 
419                 for (int i = 0; i < data.size(); i++) {
420                         rav.set(i, data[i].abundance);
421                 }
422         
423                 return rav;
424         }
425         catch(exception& e) {
426                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
427                 exit(1);
428         }
429         catch(...) {
430                 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
431                 exit(1);
432         }
433 }
434 /***********************************************************************/
435
436 RAbundVector SharedRAbundVector::getRAbundVector2() {
437         try {
438                 RAbundVector rav;
439                 for(int i = 0; i < numBins; i++)
440                         if(data[i].abundance != 0)
441                                 rav.push_back(data[i].abundance-1);
442                 return rav;
443         }
444         catch(exception& e) {
445                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
446                 exit(1);
447         }
448         catch(...) {
449                 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
450                 exit(1);
451         }
452 }
453 /***********************************************************************/
454
455 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
456         try {
457                 SharedSAbundVector sav(maxRank+1);
458                 
459                 for(int i=0;i<data.size();i++){
460                         int abund = data[i].abundance;
461                         sav.set(abund, sav.getAbundance(abund) + 1, group);
462                 }
463                 
464                 sav.set(0, 0, group);
465                 sav.setLabel(label);
466                 sav.setGroup(group);
467                 
468                 return sav;
469         }
470         catch(exception& e) {
471                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
472                 exit(1);
473         }
474         catch(...) {
475                 cout << "An unknown error has occurred in the SharedRAbundVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
476                 exit(1);
477         }
478 }
479 /***********************************************************************/
480
481 SAbundVector SharedRAbundVector::getSAbundVector() {
482         try {
483                 SAbundVector sav(maxRank+1);
484                 
485                 for(int i=0;i<data.size();i++){
486                         int abund = data[i].abundance;
487                         sav.set(abund, sav.get(abund) + 1);
488                 }
489                 sav.set(0, 0);
490                 sav.setLabel(label);
491                 return sav;
492         }
493         catch(exception& e) {
494                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
495                 exit(1);
496         }
497         catch(...) {
498                 cout << "An unknown error has occurred in the SharedRAbundVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
499                 exit(1);
500         }
501 }
502
503 /***********************************************************************/
504
505 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
506         try {
507                 SharedOrderVector ov;
508         
509                 for(int i=0;i<data.size();i++){
510                         for(int j=0;j<data[i].abundance;j++){
511                                 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
512                         }
513                 }
514                 random_shuffle(ov.begin(), ov.end());
515
516                 ov.setLabel(label);     
517                 return ov;
518         }
519         catch(exception& e) {
520                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
521                 exit(1);
522         }
523         catch(...) {
524                 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
525                 exit(1);
526         }
527 }
528 /***********************************************************************/
529
530 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
531         try {
532                 OrderVector ov;
533         
534                 for(int i=0;i<data.size();i++){
535                         for(int j=0;j<data[i].abundance;j++){
536                                 ov.push_back(i);
537                         }
538                 }
539                 random_shuffle(ov.begin(), ov.end());
540
541                 ov.setLabel(label);     
542                 return ov;
543         }
544         catch(exception& e) {
545                 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
546                 exit(1);
547         }
548         catch(...) {
549                 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
550                 exit(1);
551         }
552 }
553
554 /***********************************************************************/
555