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