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