]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundvector.cpp
1.23.0
[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 #include "sharedrabundvector.h" 
11 #include "sabundvector.hpp"
12 #include "ordervector.hpp"
13 #include "sharedutilities.h"
14
15
16 /***********************************************************************/
17 SharedRAbundVector::SharedRAbundVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0) {} 
18 /***********************************************************************/
19
20 SharedRAbundVector::~SharedRAbundVector() {
21         //for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
22
23 }
24
25 /***********************************************************************/
26
27 SharedRAbundVector::SharedRAbundVector(int n) : DataVector(), maxRank(0), numBins(n), numSeqs(0) {
28                 individual newGuy;
29                 //initialize data
30                 for (int i=0; i< n; i++) {
31                         newGuy.bin = i;
32                         newGuy.abundance = 0;
33                         data.push_back(newGuy);
34                 }
35 }
36
37 /***********************************************************************
38
39 SharedRAbundVector::SharedRAbundVector(string id, vector<individual> rav) : DataVector(id), data(rav) {
40         try {
41                 numBins = 0;
42                 maxRank = 0;
43                 numSeqs = 0;
44                 
45                 for(int i=0;i<data.size();i++){
46                         if(data[i].abundance != 0)              {       numBins = i+1;          }
47                         if(data[i].abundance > maxRank) {       maxRank = data[i].abundance;    }
48                         numSeqs += data[i].abundance;
49                 }
50         }
51         catch(exception& e) {
52                 m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
53                 exit(1);
54         }
55 }
56
57
58 /***********************************************************************/
59 //reads a shared file
60 SharedRAbundVector::SharedRAbundVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
61         try {
62                 m->clearAllGroups();
63                 vector<string> allGroups;
64                 
65                 int num, inputData, count;
66                 count = 0;  
67                 string holdLabel, nextLabel, groupN;
68                 individual newguy;
69                 
70                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }  lookup.clear();
71                 
72                 //are we at the beginning of the file??
73                 if (m->saveNextLabel == "") {  
74                         f >> label; 
75         
76                         //is this a shared file that has headers
77                         if (label == "label") { 
78                                 //gets "group"
79                                 f >> label; m->gobble(f);
80                                 
81                                 //gets "numOtus"
82                                 f >> label; m->gobble(f);
83                                 
84                                 //eat rest of line
85                                 label = m->getline(f); m->gobble(f);
86                                 
87                                 //parse labels to save
88                                 istringstream iStringStream(label);
89                                 m->binLabelsInFile.clear();
90                                 while(!iStringStream.eof()){
91                                         if (m->control_pressed) { break; }
92                                         string temp;
93                                         iStringStream >> temp;  m->gobble(iStringStream);
94                 
95                                         m->binLabelsInFile.push_back(temp);
96                                 }
97                                 
98                                 f >> label;
99                         }
100                 }else { label = m->saveNextLabel; }
101                 
102                 //reset labels, currentLabels may have gotten changed as otus were eliminated because of group choices or sampling
103                 m->currentBinLabels = m->binLabelsInFile;
104                 
105                 //read in first row since you know there is at least 1 group.
106                 f >> groupN >> num;
107                 
108                 holdLabel = label;
109                 
110                 //add new vector to lookup
111                 SharedRAbundVector* temp = new SharedRAbundVector();
112                 lookup.push_back(temp);
113                 lookup[0]->setLabel(label);
114                 lookup[0]->setGroup(groupN);
115                 
116                 allGroups.push_back(groupN);
117                 
118                 //fill vector.  data = first sharedrabund in file
119                 for(int i=0;i<num;i++){
120                         f >> inputData;
121                         
122                         lookup[0]->push_back(inputData, groupN); //abundance, bin, group
123                         push_back(inputData, groupN);
124                         
125                         if (inputData > maxRank) { maxRank = inputData; }
126                 }
127                 
128                 m->gobble(f);
129                 
130                 if (!(f.eof())) { f >> nextLabel; }
131         
132                 //read the rest of the groups info in
133                 while ((nextLabel == holdLabel) && (f.eof() != true)) {
134                         f >> groupN >> num;
135                         count++;
136                         
137                         allGroups.push_back(groupN);
138                         
139                         //add new vector to lookup
140                         temp = new SharedRAbundVector();
141                         lookup.push_back(temp);
142                         lookup[count]->setLabel(label);
143                         lookup[count]->setGroup(groupN);
144
145                         //fill vector.  
146                         for(int i=0;i<num;i++){
147                                 f >> inputData;
148                                 
149                                 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
150                         }
151                         
152                         m->gobble(f);
153                                 
154                         if (f.eof() != true) { f >> nextLabel; }
155                 }
156                 m->saveNextLabel = nextLabel;
157                 m->setAllGroups(allGroups);
158         }
159         catch(exception& e) {
160                 m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
161                 exit(1);
162         }
163 }
164
165 /***********************************************************************/
166
167 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
168         try {
169                 int oldBinSize = data[binNumber].abundance;
170                 data[binNumber].abundance = newBinSize;
171                 data[binNumber].group = groupname;
172         
173                 if(newBinSize > maxRank)        {       maxRank = newBinSize;   }
174         
175                 numSeqs += (newBinSize - oldBinSize);
176         }
177         catch(exception& e) {
178                 m->errorOut(e, "SharedRAbundVector", "set");
179                 exit(1);
180         }
181 }
182 /***********************************************************************/
183
184 void SharedRAbundVector::setData(vector <individual> newData){
185         data = newData;
186 }
187
188 /***********************************************************************/
189
190 int SharedRAbundVector::getAbundance(int index){
191         return data[index].abundance;
192         
193 }
194 /***********************************************************************/
195
196 int SharedRAbundVector::numNZ(){
197         int sum = 0;
198         for(int i = 1; i < numBins; i++)
199                 if(data[i].abundance > 0)
200                         sum++;
201         return sum;
202 }
203 /***********************************************************************/
204
205 void SharedRAbundVector::sortD(){
206         struct individual indObj;
207         sort(data.begin()+1, data.end(), indObj);
208 }
209 /***********************************************************************/
210
211 individual SharedRAbundVector::get(int index){
212         return data[index];
213         
214 }
215 /***********************************************************************/
216
217 vector <individual> SharedRAbundVector::getData(){
218         return data;
219 }
220 /***********************************************************************/
221
222 void SharedRAbundVector::clear(){
223         numBins = 0;
224         maxRank = 0;
225         numSeqs = 0;
226         data.clear();
227         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
228         lookup.clear();
229 }
230 /***********************************************************************/
231
232 void SharedRAbundVector::push_back(int binSize, string groupName){
233         try {
234                 individual newGuy;
235                 newGuy.abundance = binSize;
236                 newGuy.group = groupName;
237                 newGuy.bin = data.size();
238                 
239                 data.push_back(newGuy);
240                 numBins++;
241         
242                 if(binSize > maxRank){
243                         maxRank = binSize;
244                 }
245         
246                 numSeqs += binSize;
247         }
248         catch(exception& e) {
249                 m->errorOut(e, "SharedRAbundVector", "push_back");
250                 exit(1);
251         }
252 }
253
254 /***********************************************************************/
255
256 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
257         try {
258                 individual newGuy;
259                 newGuy.abundance = binSize;
260                 newGuy.group = groupName;
261                 newGuy.bin = otu;
262                 
263                 data.insert(data.begin()+otu, newGuy);
264                 numBins++;
265         
266                 if(binSize > maxRank){
267                         maxRank = binSize;
268                 }
269         
270                 numSeqs += binSize;
271         }
272         catch(exception& e) {
273                 m->errorOut(e, "SharedRAbundVector", "insert");
274                 exit(1);
275         }
276 }
277
278 /***********************************************************************/
279
280 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
281         try {
282                 individual newGuy;
283                 newGuy.abundance = binSize;
284                 newGuy.group = groupName;
285                 newGuy.bin = otu;
286                 
287                 data.insert(data.begin(), newGuy);
288                 numBins++;
289         
290                 if(binSize > maxRank){
291                         maxRank = binSize;
292                 }
293         
294                 numSeqs += binSize;
295         }
296         catch(exception& e) {
297                 m->errorOut(e, "SharedRAbundVector", "push_front");
298                 exit(1);
299         }
300 }
301
302 /***********************************************************************/
303 void SharedRAbundVector::pop_back(){
304         numSeqs -= data[data.size()-1].abundance;
305         numBins--;
306         return data.pop_back();
307 }
308
309 /***********************************************************************/
310
311
312 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
313         return data.rbegin();                           
314 }
315
316 /***********************************************************************/
317
318 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
319         return data.rend();                                     
320 }
321
322 /***********************************************************************/
323 void SharedRAbundVector::resize(int size){
324         
325         data.resize(size);
326 }
327
328 /***********************************************************************/
329
330 int SharedRAbundVector::size(){
331         return data.size();
332 }
333
334
335 /***********************************************************************/
336 void SharedRAbundVector::printHeaders(ostream& output){
337         try {
338                 string snumBins = toString(numBins);
339                 output << "label\tGroup\tnumOtus\t";
340                 if (m->sharedHeaderMode == "tax") {
341                         for (int i = 0; i < numBins; i++) {  
342                                 
343                                 //if there is a bin label use it otherwise make one
344                                 string binLabel = "PhyloType";
345                                 string sbinNumber = toString(i+1);
346                                 if (sbinNumber.length() < snumBins.length()) { 
347                                         int diff = snumBins.length() - sbinNumber.length();
348                                         for (int h = 0; h < diff; h++) { binLabel += "0"; }
349                                 }
350                                 binLabel += sbinNumber;
351                                 if (i < m->currentBinLabels.size()) {  binLabel = m->currentBinLabels[i]; }
352                                 
353                                 output << binLabel << '\t'; 
354                         }
355                         output << endl;
356                 }else {
357                         for (int i = 0; i < numBins; i++) {  
358                                 //if there is a bin label use it otherwise make one
359                                 string binLabel = "Otu";
360                                 string sbinNumber = toString(i+1);
361                                 if (sbinNumber.length() < snumBins.length()) { 
362                                         int diff = snumBins.length() - sbinNumber.length();
363                                         for (int h = 0; h < diff; h++) { binLabel += "0"; }
364                                 }
365                                 binLabel += sbinNumber;
366                                 if (i < m->currentBinLabels.size()) {  binLabel = m->currentBinLabels[i]; }
367                                 
368                                 output << binLabel << '\t'; 
369                         }
370                         
371                         output << endl;
372                 }
373                 m->printedHeaders = true;
374         }
375         catch(exception& e) {
376                 m->errorOut(e, "SharedRAbundVector", "printHeaders");
377                 exit(1);
378         }
379 }
380 /***********************************************************************/
381 void SharedRAbundVector::print(ostream& output) {
382         try {
383                 output << numBins << '\t';
384         
385                 for(int i=0;i<data.size();i++){         output << data[i].abundance << '\t';            }
386                 output << endl;
387         }
388         catch(exception& e) {
389                 m->errorOut(e, "SharedRAbundVector", "print");
390                 exit(1);
391         }
392 }
393 /***********************************************************************/
394 string SharedRAbundVector::getGroup(){
395         return group;
396 }
397
398 /***********************************************************************/
399
400 void SharedRAbundVector::setGroup(string groupName){
401         group = groupName;
402 }
403 /***********************************************************************/
404 int SharedRAbundVector::getGroupIndex()  { return index; }
405 /***********************************************************************/
406 void SharedRAbundVector::setGroupIndex(int vIndex)      { index = vIndex; }
407 /***********************************************************************/
408 int SharedRAbundVector::getNumBins(){
409                 return numBins;
410 }
411
412 /***********************************************************************/
413
414 int SharedRAbundVector::getNumSeqs(){
415         return numSeqs;
416 }
417
418 /***********************************************************************/
419
420 int SharedRAbundVector::getMaxRank(){
421         return maxRank;
422 }
423 /***********************************************************************/
424
425 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
426         return *this;                   
427 }
428 /***********************************************************************/
429 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
430         try {
431                 SharedUtil* util;
432                 util = new SharedUtil();
433                 
434                 vector<string> Groups = m->getGroups();
435                 vector<string> allGroups = m->getAllGroups();
436                 util->setGroups(Groups, allGroups);
437                 m->setGroups(Groups);
438                 
439                 bool remove = false;
440                 for (int i = 0; i < lookup.size(); i++) {
441                         //if this sharedrabund is not from a group the user wants then delete it.
442                         if (util->isValidGroup(lookup[i]->getGroup(), m->getGroups()) == false) { 
443                                 remove = true;
444                                 delete lookup[i]; lookup[i] = NULL;
445                                 lookup.erase(lookup.begin()+i); 
446                                 i--; 
447                         }
448                 }
449                 
450                 delete util;
451                 
452                 if (remove) { eliminateZeroOTUS(lookup); }
453         
454                 return lookup;
455         }
456         catch(exception& e) {
457                 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
458                 exit(1);
459         }
460 }
461 //**********************************************************************************************************************
462 int SharedRAbundVector::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
463                 try {
464                         
465                         vector<SharedRAbundVector*> newLookup;
466                         for (int i = 0; i < thislookup.size(); i++) {
467                                 SharedRAbundVector* temp = new SharedRAbundVector();
468                                 temp->setLabel(thislookup[i]->getLabel());
469                                 temp->setGroup(thislookup[i]->getGroup());
470                                 newLookup.push_back(temp);
471                         }
472                         
473                         //for each bin
474                         vector<string> newBinLabels;
475                         string snumBins = toString(thislookup[0]->getNumBins());
476                         for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
477                                 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) {  delete newLookup[j];  } return 0; }
478                                 
479                                 //look at each sharedRabund and make sure they are not all zero
480                                 bool allZero = true;
481                                 for (int j = 0; j < thislookup.size(); j++) {
482                                         if (thislookup[j]->getAbundance(i) != 0) { allZero = false;  break;  }
483                                 }
484                                 
485                                 //if they are not all zero add this bin
486                                 if (!allZero) {
487                                         for (int j = 0; j < thislookup.size(); j++) {
488                                                 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
489                                         }
490                                         
491                                         //if there is a bin label use it otherwise make one
492                                         string binLabel = "Otu";
493                                         string sbinNumber = toString(i+1);
494                                         if (sbinNumber.length() < snumBins.length()) { 
495                                                 int diff = snumBins.length() - sbinNumber.length();
496                                                 for (int h = 0; h < diff; h++) { binLabel += "0"; }
497                                         }
498                                         binLabel += sbinNumber; 
499                                         if (i < m->currentBinLabels.size()) {  binLabel = m->currentBinLabels[i]; }
500                                         
501                                         newBinLabels.push_back(binLabel);
502                                 }
503                         }
504                         
505                         for (int j = 0; j < thislookup.size(); j++) {  delete thislookup[j];  }
506                         
507                         thislookup = newLookup;
508                         m->currentBinLabels = newBinLabels;
509                         
510                         return 0;
511                         
512                 }
513                 catch(exception& e) {
514                         m->errorOut(e, "SharedRAbundVector", "eliminateZeroOTUS");
515                         exit(1);
516                 }
517         }
518         
519 /***********************************************************************/
520 vector<SharedRAbundFloatVector*> SharedRAbundVector::getSharedRAbundFloatVectors(vector<SharedRAbundVector*> thislookup){
521         try {
522                 vector<SharedRAbundFloatVector*> newLookupFloat;        
523                 for (int i = 0; i < lookup.size(); i++) {
524                         SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
525                         temp->setLabel(thislookup[i]->getLabel());
526                         temp->setGroup(thislookup[i]->getGroup());
527                         newLookupFloat.push_back(temp);
528                 }
529                 
530                 for (int i = 0; i < thislookup.size(); i++) {
531                         
532                         for (int j = 0; j < thislookup[i]->getNumBins(); j++) {
533                                 
534                                 if (m->control_pressed) { return newLookupFloat; }
535                                 
536                                 int abund = thislookup[i]->getAbundance(j);
537                                 
538                                 float relabund = abund / (float) thislookup[i]->getNumSeqs();
539                                 
540                                 newLookupFloat[i]->push_back(relabund, thislookup[i]->getGroup());
541                         }
542                 }
543                 
544                 return newLookupFloat;
545         }
546         catch(exception& e) {
547                 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
548                 exit(1);
549         }
550 }
551 /***********************************************************************/
552
553 RAbundVector SharedRAbundVector::getRAbundVector() {
554         try {
555                 RAbundVector rav;
556                 
557                 for (int i = 0; i < data.size(); i++) {
558                         if(data[i].abundance != 0) {
559                                 rav.push_back(data[i].abundance);
560                         }
561                 }
562                 
563                 rav.setLabel(label);
564                 return rav;
565         }
566         catch(exception& e) {
567                 m->errorOut(e, "SharedRAbundVector", "getRAbundVector");
568                 exit(1);
569         }
570 }
571 /***********************************************************************/
572
573 RAbundVector SharedRAbundVector::getRAbundVector2() {
574         try {
575                 RAbundVector rav;
576                 for(int i = 0; i < numBins; i++)
577                         if(data[i].abundance != 0)
578                                 rav.push_back(data[i].abundance-1);
579                 return rav;
580         }
581         catch(exception& e) {
582                 m->errorOut(e, "SharedRAbundVector", "getRAbundVector2");
583                 exit(1);
584         }
585 }
586 /***********************************************************************/
587
588 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
589         try {
590                 SharedSAbundVector sav(maxRank+1);
591                 
592                 for(int i=0;i<data.size();i++){
593                         int abund = data[i].abundance;
594                         sav.set(abund, sav.getAbundance(abund) + 1, group);
595                 }
596                 
597                 sav.set(0, 0, group);
598                 sav.setLabel(label);
599                 sav.setGroup(group);
600                 
601                 return sav;
602         }
603         catch(exception& e) {
604                 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
605                 exit(1);
606         }
607 }
608 /***********************************************************************/
609
610 SAbundVector SharedRAbundVector::getSAbundVector() {
611         try {
612                 SAbundVector sav(maxRank+1);
613                 
614                 for(int i=0;i<data.size();i++){
615                         int abund = data[i].abundance;
616                         sav.set(abund, sav.get(abund) + 1);
617                 }
618                 sav.set(0, 0);
619                 sav.setLabel(label);
620                 return sav;
621         }
622         catch(exception& e) {
623                 m->errorOut(e, "SharedRAbundVector", "getSAbundVector");                
624                 exit(1);
625         }
626 }
627
628 /***********************************************************************/
629
630 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
631         try {
632                 SharedOrderVector ov;
633         
634                 for(int i=0;i<data.size();i++){
635                         for(int j=0;j<data[i].abundance;j++){
636                                 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
637                         }
638                 }
639                 random_shuffle(ov.begin(), ov.end());
640
641                 ov.setLabel(label);     
642                 ov.updateStats();
643                 
644                 return ov;
645         }
646         catch(exception& e) {
647                 m->errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
648                 exit(1);
649         }
650 }
651 /***********************************************************************/
652
653 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
654         try {
655                 OrderVector ov;
656                 for(int i=0;i<numBins;i++){
657                         for(int j=0;j<data[i].abundance;j++){
658                                 ov.push_back(i);
659                         }
660                 }
661                 random_shuffle(ov.begin(), ov.end());
662                 
663                 ov.setLabel(label);     
664
665                 return ov;
666         }
667         catch(exception& e) {
668                 m->errorOut(e, "SharedRAbundVector", "getOrderVector");
669                 exit(1);
670         }
671 }
672
673 /***********************************************************************/
674