]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundvector.cpp
check in for trip
[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         }
160         catch(exception& e) {
161                 m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
162                 exit(1);
163         }
164 }
165
166 /***********************************************************************/
167
168 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
169         try {
170                 int oldBinSize = data[binNumber].abundance;
171                 data[binNumber].abundance = newBinSize;
172                 data[binNumber].group = groupname;
173         
174                 if(newBinSize > maxRank)        {       maxRank = newBinSize;   }
175         
176                 numSeqs += (newBinSize - oldBinSize);
177         }
178         catch(exception& e) {
179                 m->errorOut(e, "SharedRAbundVector", "set");
180                 exit(1);
181         }
182 }
183 /***********************************************************************/
184
185 void SharedRAbundVector::setData(vector <individual> newData){
186         data = newData;
187 }
188
189 /***********************************************************************/
190
191 int SharedRAbundVector::getAbundance(int index){
192         return data[index].abundance;
193         
194 }
195 /***********************************************************************/
196
197 int SharedRAbundVector::numNZ(){
198         int sum = 0;
199         for(int i = 1; i < numBins; i++)
200                 if(data[i].abundance > 0)
201                         sum++;
202         return sum;
203 }
204 /***********************************************************************/
205
206 void SharedRAbundVector::sortD(){
207         struct individual indObj;
208         sort(data.begin()+1, data.end(), indObj);
209 }
210 /***********************************************************************/
211
212 individual SharedRAbundVector::get(int index){
213         return data[index];
214         
215 }
216 /***********************************************************************/
217
218 vector <individual> SharedRAbundVector::getData(){
219         return data;
220 }
221 /***********************************************************************/
222
223 void SharedRAbundVector::clear(){
224         numBins = 0;
225         maxRank = 0;
226         numSeqs = 0;
227         data.clear();
228         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
229         lookup.clear();
230 }
231 /***********************************************************************/
232
233 void SharedRAbundVector::push_back(int binSize, string groupName){
234         try {
235                 individual newGuy;
236                 newGuy.abundance = binSize;
237                 newGuy.group = groupName;
238                 newGuy.bin = data.size();
239                 
240                 data.push_back(newGuy);
241                 numBins++;
242         
243                 if(binSize > maxRank){
244                         maxRank = binSize;
245                 }
246         
247                 numSeqs += binSize;
248         }
249         catch(exception& e) {
250                 m->errorOut(e, "SharedRAbundVector", "push_back");
251                 exit(1);
252         }
253 }
254
255 /***********************************************************************/
256
257 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
258         try {
259                 individual newGuy;
260                 newGuy.abundance = binSize;
261                 newGuy.group = groupName;
262                 newGuy.bin = otu;
263                 
264                 data.insert(data.begin()+otu, newGuy);
265                 numBins++;
266         
267                 if(binSize > maxRank){
268                         maxRank = binSize;
269                 }
270         
271                 numSeqs += binSize;
272         }
273         catch(exception& e) {
274                 m->errorOut(e, "SharedRAbundVector", "insert");
275                 exit(1);
276         }
277 }
278
279 /***********************************************************************/
280
281 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
282         try {
283                 individual newGuy;
284                 newGuy.abundance = binSize;
285                 newGuy.group = groupName;
286                 newGuy.bin = otu;
287                 
288                 data.insert(data.begin(), newGuy);
289                 numBins++;
290         
291                 if(binSize > maxRank){
292                         maxRank = binSize;
293                 }
294         
295                 numSeqs += binSize;
296         }
297         catch(exception& e) {
298                 m->errorOut(e, "SharedRAbundVector", "push_front");
299                 exit(1);
300         }
301 }
302
303 /***********************************************************************/
304 void SharedRAbundVector::pop_back(){
305         numSeqs -= data[data.size()-1].abundance;
306         numBins--;
307         return data.pop_back();
308 }
309
310 /***********************************************************************/
311
312
313 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
314         return data.rbegin();                           
315 }
316
317 /***********************************************************************/
318
319 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
320         return data.rend();                                     
321 }
322
323 /***********************************************************************/
324 void SharedRAbundVector::resize(int size){
325         
326         data.resize(size);
327 }
328
329 /***********************************************************************/
330
331 int SharedRAbundVector::size(){
332         return data.size();
333 }
334
335
336 /***********************************************************************/
337 void SharedRAbundVector::printHeaders(ostream& output){
338         try {
339                 string snumBins = toString(numBins);
340                 output << "label\tGroup\tnumOtus\t";
341                 if (m->sharedHeaderMode == "tax") {
342                         for (int i = 0; i < numBins; i++) {  
343                                 
344                                 //if there is a bin label use it otherwise make one
345                                 string binLabel = "PhyloType";
346                                 string sbinNumber = toString(i+1);
347                                 if (sbinNumber.length() < snumBins.length()) { 
348                                         int diff = snumBins.length() - sbinNumber.length();
349                                         for (int h = 0; h < diff; h++) { binLabel += "0"; }
350                                 }
351                                 binLabel += sbinNumber;
352                                 if (i < m->currentBinLabels.size()) {  binLabel = m->currentBinLabels[i]; }
353                                 
354                                 output << binLabel << '\t'; 
355                         }
356                         output << endl;
357                 }else {
358                         for (int i = 0; i < numBins; i++) {  
359                                 //if there is a bin label use it otherwise make one
360                                 string binLabel = "Otu";
361                                 string sbinNumber = toString(i+1);
362                                 if (sbinNumber.length() < snumBins.length()) { 
363                                         int diff = snumBins.length() - sbinNumber.length();
364                                         for (int h = 0; h < diff; h++) { binLabel += "0"; }
365                                 }
366                                 binLabel += sbinNumber;
367                                 if (i < m->currentBinLabels.size()) {  binLabel = m->currentBinLabels[i]; }
368                                 
369                                 output << binLabel << '\t'; 
370                         }
371                         
372                         output << endl;
373                 }
374                 m->printedHeaders = true;
375         }
376         catch(exception& e) {
377                 m->errorOut(e, "SharedRAbundVector", "printHeaders");
378                 exit(1);
379         }
380 }
381 /***********************************************************************/
382 void SharedRAbundVector::print(ostream& output) {
383         try {
384                 output << numBins << '\t';
385         
386                 for(int i=0;i<data.size();i++){         output << data[i].abundance << '\t';            }
387                 output << endl;
388         }
389         catch(exception& e) {
390                 m->errorOut(e, "SharedRAbundVector", "print");
391                 exit(1);
392         }
393 }
394 /***********************************************************************/
395 string SharedRAbundVector::getGroup(){
396         return group;
397 }
398
399 /***********************************************************************/
400
401 void SharedRAbundVector::setGroup(string groupName){
402         group = groupName;
403 }
404 /***********************************************************************/
405 int SharedRAbundVector::getGroupIndex()  { return index; }
406 /***********************************************************************/
407 void SharedRAbundVector::setGroupIndex(int vIndex)      { index = vIndex; }
408 /***********************************************************************/
409 int SharedRAbundVector::getNumBins(){
410                 return numBins;
411 }
412
413 /***********************************************************************/
414
415 int SharedRAbundVector::getNumSeqs(){
416         return numSeqs;
417 }
418
419 /***********************************************************************/
420
421 int SharedRAbundVector::getMaxRank(){
422         return maxRank;
423 }
424 /***********************************************************************/
425
426 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
427         return *this;                   
428 }
429 /***********************************************************************/
430 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
431         try {
432                 SharedUtil* util;
433                 util = new SharedUtil();
434                 
435                 vector<string> Groups = m->getGroups();
436                 vector<string> allGroups = m->getAllGroups();
437                 util->setGroups(Groups, allGroups);
438                 m->setGroups(Groups);
439                 
440                 bool remove = false;
441                 for (int i = 0; i < lookup.size(); i++) {
442                         //if this sharedrabund is not from a group the user wants then delete it.
443                         if (util->isValidGroup(lookup[i]->getGroup(), m->getGroups()) == false) { 
444                                 remove = true;
445                                 delete lookup[i]; lookup[i] = NULL;
446                                 lookup.erase(lookup.begin()+i); 
447                                 i--; 
448                         }
449                 }
450                 
451                 delete util;
452                 
453                 if (remove) { eliminateZeroOTUS(lookup); }
454         
455                 return lookup;
456         }
457         catch(exception& e) {
458                 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
459                 exit(1);
460         }
461 }
462 //**********************************************************************************************************************
463 int SharedRAbundVector::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
464                 try {
465                         
466                         vector<SharedRAbundVector*> newLookup;
467                         for (int i = 0; i < thislookup.size(); i++) {
468                                 SharedRAbundVector* temp = new SharedRAbundVector();
469                                 temp->setLabel(thislookup[i]->getLabel());
470                                 temp->setGroup(thislookup[i]->getGroup());
471                                 newLookup.push_back(temp);
472                         }
473                         
474                         //for each bin
475                         vector<string> newBinLabels;
476                         string snumBins = toString(thislookup[0]->getNumBins());
477                         for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
478                                 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) {  delete newLookup[j];  } return 0; }
479                                 
480                                 //look at each sharedRabund and make sure they are not all zero
481                                 bool allZero = true;
482                                 for (int j = 0; j < thislookup.size(); j++) {
483                                         if (thislookup[j]->getAbundance(i) != 0) { allZero = false;  break;  }
484                                 }
485                                 
486                                 //if they are not all zero add this bin
487                                 if (!allZero) {
488                                         for (int j = 0; j < thislookup.size(); j++) {
489                                                 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
490                                         }
491                                         
492                                         //if there is a bin label use it otherwise make one
493                                         string binLabel = "Otu";
494                                         string sbinNumber = toString(i+1);
495                                         if (sbinNumber.length() < snumBins.length()) { 
496                                                 int diff = snumBins.length() - sbinNumber.length();
497                                                 for (int h = 0; h < diff; h++) { binLabel += "0"; }
498                                         }
499                                         binLabel += sbinNumber; 
500                                         if (i < m->currentBinLabels.size()) {  binLabel = m->currentBinLabels[i]; }
501                                         
502                                         newBinLabels.push_back(binLabel);
503                                 }
504                         }
505                         
506                         for (int j = 0; j < thislookup.size(); j++) {  delete thislookup[j];  }
507                         
508                         thislookup = newLookup;
509                         m->currentBinLabels = newBinLabels;
510                         
511                         return 0;
512                         
513                 }
514                 catch(exception& e) {
515                         m->errorOut(e, "SharedRAbundVector", "eliminateZeroOTUS");
516                         exit(1);
517                 }
518         }
519         
520 /***********************************************************************/
521 vector<SharedRAbundFloatVector*> SharedRAbundVector::getSharedRAbundFloatVectors(vector<SharedRAbundVector*> thislookup){
522         try {
523                 vector<SharedRAbundFloatVector*> newLookupFloat;        
524                 for (int i = 0; i < lookup.size(); i++) {
525                         SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
526                         temp->setLabel(thislookup[i]->getLabel());
527                         temp->setGroup(thislookup[i]->getGroup());
528                         newLookupFloat.push_back(temp);
529                 }
530                 
531                 for (int i = 0; i < thislookup.size(); i++) {
532                         
533                         for (int j = 0; j < thislookup[i]->getNumBins(); j++) {
534                                 
535                                 if (m->control_pressed) { return newLookupFloat; }
536                                 
537                                 int abund = thislookup[i]->getAbundance(j);
538                                 
539                                 float relabund = abund / (float) thislookup[i]->getNumSeqs();
540                                 
541                                 newLookupFloat[i]->push_back(relabund, thislookup[i]->getGroup());
542                         }
543                 }
544                 
545                 return newLookupFloat;
546         }
547         catch(exception& e) {
548                 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
549                 exit(1);
550         }
551 }
552 /***********************************************************************/
553
554 RAbundVector SharedRAbundVector::getRAbundVector() {
555         try {
556                 RAbundVector rav;
557                 
558                 for (int i = 0; i < data.size(); i++) {
559                         if(data[i].abundance != 0) {
560                                 rav.push_back(data[i].abundance);
561                         }
562                 }
563                 
564                 rav.setLabel(label);
565                 return rav;
566         }
567         catch(exception& e) {
568                 m->errorOut(e, "SharedRAbundVector", "getRAbundVector");
569                 exit(1);
570         }
571 }
572 /***********************************************************************/
573
574 RAbundVector SharedRAbundVector::getRAbundVector2() {
575         try {
576                 RAbundVector rav;
577                 for(int i = 0; i < numBins; i++)
578                         if(data[i].abundance != 0)
579                                 rav.push_back(data[i].abundance-1);
580                 return rav;
581         }
582         catch(exception& e) {
583                 m->errorOut(e, "SharedRAbundVector", "getRAbundVector2");
584                 exit(1);
585         }
586 }
587 /***********************************************************************/
588
589 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
590         try {
591                 SharedSAbundVector sav(maxRank+1);
592                 
593                 for(int i=0;i<data.size();i++){
594                         int abund = data[i].abundance;
595                         sav.set(abund, sav.getAbundance(abund) + 1, group);
596                 }
597                 
598                 sav.set(0, 0, group);
599                 sav.setLabel(label);
600                 sav.setGroup(group);
601                 
602                 return sav;
603         }
604         catch(exception& e) {
605                 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
606                 exit(1);
607         }
608 }
609 /***********************************************************************/
610
611 SAbundVector SharedRAbundVector::getSAbundVector() {
612         try {
613                 SAbundVector sav(maxRank+1);
614                 
615                 for(int i=0;i<data.size();i++){
616                         int abund = data[i].abundance;
617                         sav.set(abund, sav.get(abund) + 1);
618                 }
619                 sav.set(0, 0);
620                 sav.setLabel(label);
621                 return sav;
622         }
623         catch(exception& e) {
624                 m->errorOut(e, "SharedRAbundVector", "getSAbundVector");                
625                 exit(1);
626         }
627 }
628
629 /***********************************************************************/
630
631 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
632         try {
633                 SharedOrderVector ov;
634         
635                 for(int i=0;i<data.size();i++){
636                         for(int j=0;j<data[i].abundance;j++){
637                                 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
638                         }
639                 }
640                 random_shuffle(ov.begin(), ov.end());
641
642                 ov.setLabel(label);     
643                 ov.updateStats();
644                 
645                 return ov;
646         }
647         catch(exception& e) {
648                 m->errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
649                 exit(1);
650         }
651 }
652 /***********************************************************************/
653
654 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
655         try {
656                 OrderVector ov;
657                 for(int i=0;i<numBins;i++){
658                         for(int j=0;j<data[i].abundance;j++){
659                                 ov.push_back(i);
660                         }
661                 }
662                 random_shuffle(ov.begin(), ov.end());
663                 
664                 ov.setLabel(label);     
665
666                 return ov;
667         }
668         catch(exception& e) {
669                 m->errorOut(e, "SharedRAbundVector", "getOrderVector");
670                 exit(1);
671         }
672 }
673
674 /***********************************************************************/
675