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