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