]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundvector.cpp
1.18.1
[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                 if (m->saveNextLabel == "") {  f >> label;  }
72                 else { label = m->saveNextLabel; }
73                 
74                 //read in first row since you know there is at least 1 group.
75                 f >> groupN >> num;
76
77                 holdLabel = label;
78                 
79                 //add new vector to lookup
80                 SharedRAbundVector* temp = new SharedRAbundVector();
81                 lookup.push_back(temp);
82                 lookup[0]->setLabel(label);
83                 lookup[0]->setGroup(groupN);
84                 
85                 m->namesOfGroups.push_back(groupN);
86                 
87                 //fill vector.  data = first sharedrabund in file
88                 for(int i=0;i<num;i++){
89                         f >> inputData;
90                         
91                         lookup[0]->push_back(inputData, groupN); //abundance, bin, group
92                         push_back(inputData, groupN);
93                         //numSeqs += inputData;
94                         //numBins++;
95                         if (inputData > maxRank) { maxRank = inputData; }
96                 }
97                 
98                 m->gobble(f);
99                 
100                 if (!(f.eof())) { f >> nextLabel; }
101         
102                 //read the rest of the groups info in
103                 while ((nextLabel == holdLabel) && (f.eof() != true)) {
104                         f >> groupN >> num;
105                         count++;
106                         
107                         m->namesOfGroups.push_back(groupN);
108                         
109                         //add new vector to lookup
110                         temp = new SharedRAbundVector();
111                         lookup.push_back(temp);
112                         lookup[count]->setLabel(label);
113                         lookup[count]->setGroup(groupN);
114
115                         //fill vector.  
116                         for(int i=0;i<num;i++){
117                                 f >> inputData;
118                                 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
119                         }
120                         
121                         m->gobble(f);
122                                 
123                         if (f.eof() != true) { f >> nextLabel; }
124                 }
125                         m->saveNextLabel = nextLabel;
126                 
127         }
128         catch(exception& e) {
129                 m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
130                 exit(1);
131         }
132 }
133
134 /***********************************************************************/
135
136 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
137         try {
138                 int oldBinSize = data[binNumber].abundance;
139                 data[binNumber].abundance = newBinSize;
140                 data[binNumber].group = groupname;
141         
142                 if(newBinSize > maxRank)        {       maxRank = newBinSize;   }
143         
144                 numSeqs += (newBinSize - oldBinSize);
145         }
146         catch(exception& e) {
147                 m->errorOut(e, "SharedRAbundVector", "set");
148                 exit(1);
149         }
150 }
151 /***********************************************************************/
152
153 void SharedRAbundVector::setData(vector <individual> newData){
154         data = newData;
155 }
156
157 /***********************************************************************/
158
159 int SharedRAbundVector::getAbundance(int index){
160         return data[index].abundance;
161         
162 }
163 /***********************************************************************/
164
165 int SharedRAbundVector::numNZ(){
166         int sum = 0;
167         for(int i = 1; i < numBins; i++)
168                 if(data[i].abundance > 0)
169                         sum++;
170         return sum;
171 }
172 /***********************************************************************/
173
174 void SharedRAbundVector::sortD(){
175         struct individual indObj;
176         sort(data.begin()+1, data.end(), indObj);
177 }
178 /***********************************************************************/
179
180 individual SharedRAbundVector::get(int index){
181         return data[index];
182         
183 }
184 /***********************************************************************/
185
186 vector <individual> SharedRAbundVector::getData(){
187         return data;
188 }
189 /***********************************************************************/
190
191 void SharedRAbundVector::clear(){
192         numBins = 0;
193         maxRank = 0;
194         numSeqs = 0;
195         data.clear();
196         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
197         lookup.clear();
198 }
199 /***********************************************************************/
200
201 void SharedRAbundVector::push_back(int binSize, string groupName){
202         try {
203                 individual newGuy;
204                 newGuy.abundance = binSize;
205                 newGuy.group = groupName;
206                 newGuy.bin = data.size();
207                 
208                 data.push_back(newGuy);
209                 numBins++;
210         
211                 if(binSize > maxRank){
212                         maxRank = binSize;
213                 }
214         
215                 numSeqs += binSize;
216         }
217         catch(exception& e) {
218                 m->errorOut(e, "SharedRAbundVector", "push_back");
219                 exit(1);
220         }
221 }
222
223 /***********************************************************************/
224
225 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
226         try {
227                 individual newGuy;
228                 newGuy.abundance = binSize;
229                 newGuy.group = groupName;
230                 newGuy.bin = otu;
231                 
232                 data.insert(data.begin()+otu, newGuy);
233                 numBins++;
234         
235                 if(binSize > maxRank){
236                         maxRank = binSize;
237                 }
238         
239                 numSeqs += binSize;
240         }
241         catch(exception& e) {
242                 m->errorOut(e, "SharedRAbundVector", "insert");
243                 exit(1);
244         }
245 }
246
247 /***********************************************************************/
248
249 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
250         try {
251                 individual newGuy;
252                 newGuy.abundance = binSize;
253                 newGuy.group = groupName;
254                 newGuy.bin = otu;
255                 
256                 data.insert(data.begin(), newGuy);
257                 numBins++;
258         
259                 if(binSize > maxRank){
260                         maxRank = binSize;
261                 }
262         
263                 numSeqs += binSize;
264         }
265         catch(exception& e) {
266                 m->errorOut(e, "SharedRAbundVector", "push_front");
267                 exit(1);
268         }
269 }
270
271 /***********************************************************************/
272 void SharedRAbundVector::pop_back(){
273         numSeqs -= data[data.size()-1].abundance;
274         numBins--;
275         return data.pop_back();
276 }
277
278 /***********************************************************************/
279
280
281 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
282         return data.rbegin();                           
283 }
284
285 /***********************************************************************/
286
287 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
288         return data.rend();                                     
289 }
290
291 /***********************************************************************/
292 void SharedRAbundVector::resize(int size){
293         
294         data.resize(size);
295 }
296
297 /***********************************************************************/
298
299 int SharedRAbundVector::size(){
300         return data.size();
301 }
302
303 /***********************************************************************/
304 void SharedRAbundVector::print(ostream& output){
305         try {
306                 output << numBins << '\t';
307         
308                 for(int i=0;i<data.size();i++){         output << data[i].abundance << '\t';            }
309                 output << endl;
310         }
311         catch(exception& e) {
312                 m->errorOut(e, "SharedRAbundVector", "print");
313                 exit(1);
314         }
315 }
316 /***********************************************************************/
317 string SharedRAbundVector::getGroup(){
318         return group;
319 }
320
321 /***********************************************************************/
322
323 void SharedRAbundVector::setGroup(string groupName){
324         group = groupName;
325 }
326 /***********************************************************************/
327 int SharedRAbundVector::getGroupIndex()  { return index; }
328 /***********************************************************************/
329 void SharedRAbundVector::setGroupIndex(int vIndex)      { index = vIndex; }
330 /***********************************************************************/
331 int SharedRAbundVector::getNumBins(){
332         return numBins;
333 }
334
335 /***********************************************************************/
336
337 int SharedRAbundVector::getNumSeqs(){
338         return numSeqs;
339 }
340
341 /***********************************************************************/
342
343 int SharedRAbundVector::getMaxRank(){
344         return maxRank;
345 }
346 /***********************************************************************/
347
348 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
349         return *this;                   
350 }
351 /***********************************************************************/
352 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
353         try {
354                 SharedUtil* util;
355                 util = new SharedUtil();
356                 
357                 util->setGroups(m->Groups, m->namesOfGroups);
358                 
359                 bool remove = false;
360                 for (int i = 0; i < lookup.size(); i++) {
361                         //if this sharedrabund is not from a group the user wants then delete it.
362                         if (util->isValidGroup(lookup[i]->getGroup(), m->Groups) == false) { 
363                                 remove = true;
364                                 delete lookup[i]; lookup[i] = NULL;
365                                 lookup.erase(lookup.begin()+i); 
366                                 i--; 
367                         }
368                 }
369                 
370                 delete util;
371                 
372                 if (remove) { eliminateZeroOTUS(lookup); }
373         
374                 return lookup;
375         }
376         catch(exception& e) {
377                 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
378                 exit(1);
379         }
380 }
381 //**********************************************************************************************************************
382 int SharedRAbundVector::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
383                 try {
384                         
385                         vector<SharedRAbundVector*> newLookup;
386                         for (int i = 0; i < thislookup.size(); i++) {
387                                 SharedRAbundVector* temp = new SharedRAbundVector();
388                                 temp->setLabel(thislookup[i]->getLabel());
389                                 temp->setGroup(thislookup[i]->getGroup());
390                                 newLookup.push_back(temp);
391                         }
392                         
393                         //for each bin
394                         for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
395                                 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) {  delete newLookup[j];  } return 0; }
396                                 
397                                 //look at each sharedRabund and make sure they are not all zero
398                                 bool allZero = true;
399                                 for (int j = 0; j < thislookup.size(); j++) {
400                                         if (thislookup[j]->getAbundance(i) != 0) { allZero = false;  break;  }
401                                 }
402                                 
403                                 //if they are not all zero add this bin
404                                 if (!allZero) {
405                                         for (int j = 0; j < thislookup.size(); j++) {
406                                                 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
407                                         }
408                                 }
409                         }
410                         
411                         for (int j = 0; j < thislookup.size(); j++) {  delete thislookup[j];  }
412                         
413                         thislookup = newLookup;
414                         
415                         return 0;
416                         
417                 }
418                 catch(exception& e) {
419                         m->errorOut(e, "SharedRAbundVector", "eliminateZeroOTUS");
420                         exit(1);
421                 }
422         }
423         
424 /***********************************************************************/
425 vector<SharedRAbundFloatVector*> SharedRAbundVector::getSharedRAbundFloatVectors(vector<SharedRAbundVector*> thislookup){
426         try {
427                 vector<SharedRAbundFloatVector*> newLookupFloat;        
428                 for (int i = 0; i < lookup.size(); i++) {
429                         SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
430                         temp->setLabel(thislookup[i]->getLabel());
431                         temp->setGroup(thislookup[i]->getGroup());
432                         newLookupFloat.push_back(temp);
433                 }
434                 
435                 for (int i = 0; i < thislookup.size(); i++) {
436                         
437                         for (int j = 0; j < thislookup[i]->getNumBins(); j++) {
438                                 
439                                 if (m->control_pressed) { return newLookupFloat; }
440                                 
441                                 int abund = thislookup[i]->getAbundance(j);
442                                 
443                                 float relabund = abund / (float) thislookup[i]->getNumSeqs();
444                                 
445                                 newLookupFloat[i]->push_back(relabund, thislookup[i]->getGroup());
446                         }
447                 }
448                 
449                 return newLookupFloat;
450         }
451         catch(exception& e) {
452                 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
453                 exit(1);
454         }
455 }
456 /***********************************************************************/
457
458 RAbundVector SharedRAbundVector::getRAbundVector() {
459         try {
460                 RAbundVector rav;
461                 
462                 for (int i = 0; i < data.size(); i++) {
463                         if(data[i].abundance != 0) {
464                                 rav.push_back(data[i].abundance);
465                         }
466                 }
467                 
468                 rav.setLabel(label);
469                 return rav;
470         }
471         catch(exception& e) {
472                 m->errorOut(e, "SharedRAbundVector", "getRAbundVector");
473                 exit(1);
474         }
475 }
476 /***********************************************************************/
477
478 RAbundVector SharedRAbundVector::getRAbundVector2() {
479         try {
480                 RAbundVector rav;
481                 for(int i = 0; i < numBins; i++)
482                         if(data[i].abundance != 0)
483                                 rav.push_back(data[i].abundance-1);
484                 return rav;
485         }
486         catch(exception& e) {
487                 m->errorOut(e, "SharedRAbundVector", "getRAbundVector2");
488                 exit(1);
489         }
490 }
491 /***********************************************************************/
492
493 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
494         try {
495                 SharedSAbundVector sav(maxRank+1);
496                 
497                 for(int i=0;i<data.size();i++){
498                         int abund = data[i].abundance;
499                         sav.set(abund, sav.getAbundance(abund) + 1, group);
500                 }
501                 
502                 sav.set(0, 0, group);
503                 sav.setLabel(label);
504                 sav.setGroup(group);
505                 
506                 return sav;
507         }
508         catch(exception& e) {
509                 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
510                 exit(1);
511         }
512 }
513 /***********************************************************************/
514
515 SAbundVector SharedRAbundVector::getSAbundVector() {
516         try {
517                 SAbundVector sav(maxRank+1);
518                 
519                 for(int i=0;i<data.size();i++){
520                         int abund = data[i].abundance;
521                         sav.set(abund, sav.get(abund) + 1);
522                 }
523                 sav.set(0, 0);
524                 sav.setLabel(label);
525                 return sav;
526         }
527         catch(exception& e) {
528                 m->errorOut(e, "SharedRAbundVector", "getSAbundVector");                
529                 exit(1);
530         }
531 }
532
533 /***********************************************************************/
534
535 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
536         try {
537                 SharedOrderVector ov;
538         
539                 for(int i=0;i<data.size();i++){
540                         for(int j=0;j<data[i].abundance;j++){
541                                 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
542                         }
543                 }
544                 random_shuffle(ov.begin(), ov.end());
545
546                 ov.setLabel(label);     
547                 ov.updateStats();
548                 
549                 return ov;
550         }
551         catch(exception& e) {
552                 m->errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
553                 exit(1);
554         }
555 }
556 /***********************************************************************/
557
558 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
559         try {
560                 OrderVector ov;
561                 for(int i=0;i<numBins;i++){
562                         for(int j=0;j<data[i].abundance;j++){
563                                 ov.push_back(i);
564                         }
565                 }
566                 random_shuffle(ov.begin(), ov.end());
567                 
568                 ov.setLabel(label);     
569
570                 return ov;
571         }
572         catch(exception& e) {
573                 m->errorOut(e, "SharedRAbundVector", "getOrderVector");
574                 exit(1);
575         }
576 }
577
578 /***********************************************************************/
579