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