]> git.donarmstrong.com Git - mothur.git/blob - rarefact.cpp
added concensus command and updated calcs
[mothur.git] / rarefact.cpp
1 /*
2  *  rarefact.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 11/18/08.
6  *  Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "rarefact.h"
11 //#include "ordervector.hpp"
12
13 /***********************************************************************/
14
15 void Rarefact::getCurve(int increment = 1, int nIters = 1000){
16         try {
17                 RarefactionCurveData* rcd = new RarefactionCurveData();
18                 for(int i=0;i<displays.size();i++){
19                         rcd->registerDisplay(displays[i]);
20                 }
21         
22                 for(int iter=0;iter<nIters;iter++){
23                 
24                         for(int i=0;i<displays.size();i++){
25                                 displays[i]->init(label);
26                         }
27                 
28                         RAbundVector* lookup    = new RAbundVector(order->getNumBins());
29                         SAbundVector* rank      = new SAbundVector(order->getMaxRank()+1);
30                         random_shuffle(order->begin(), order->end());
31                 
32                         for(int i=0;i<numSeqs;i++){
33                         
34                                 int binNumber = order->get(i);
35                                 int abundance = lookup->get(binNumber);
36                         
37                                 rank->set(abundance, rank->get(abundance)-1);
38                                 abundance++;
39                 
40                                 lookup->set(binNumber, abundance);
41                                 rank->set(abundance, rank->get(abundance)+1);
42
43                                 if((i == 0) || (i+1) % increment == 0){
44                                         rcd->updateRankData(rank);
45                                 }
46                         }
47         
48                         if(numSeqs % increment != 0){
49                                 rcd->updateRankData(rank);
50                         }
51
52                         for(int i=0;i<displays.size();i++){
53                                 displays[i]->reset();
54                         }
55                 }
56
57                 for(int i=0;i<displays.size();i++){
58                         displays[i]->close();
59                 }
60         }
61         catch(exception& e) {
62                 cout << "Standard Error: " << e.what() << " has occurred in the Rarefact class Function getCurve. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
63                 exit(1);
64         }
65         catch(...) {
66                 cout << "An unknown error has occurred in the Rarefact class function getCurve. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
67                 exit(1);
68         }
69 }
70
71 /***********************************************************************/
72
73 void Rarefact::getSharedCurve(int increment = 1, int nIters = 1000){
74 try {
75                 globaldata = GlobalData::getInstance();
76                 SharedRarefactionCurveData* rcd = new SharedRarefactionCurveData();
77                 
78                 //register the displays
79                 for(int i=0;i<displays.size();i++){
80                         rcd->registerDisplay(displays[i]);
81                 }
82
83                 for(int iter=0;iter<nIters;iter++){
84                         //clear out old values for new iter
85                         lookup.clear();
86                 
87                         //create and initialize vector of sharedvectors, one for each group
88                         for (int i = 0; i < globaldata->Groups.size(); i++) { 
89                                 SharedRAbundVector* temp = new SharedRAbundVector(sharedorder->getNumBins());
90                                 temp->setLabel(sharedorder->getLabel());
91                                 temp->setGroup(globaldata->Groups[i]);
92                                 lookup.push_back(temp);
93                         }
94                         
95                         for(int i=0;i<displays.size();i++){
96                                 displays[i]->init(label);                 
97                         }
98                 
99                         //sample all the members
100                         for(int i=0;i<numSeqs;i++){
101                                 //get first sample
102                                 individual chosen = sharedorder->get(i);
103                                 int abundance; 
104                                         
105                                 //set info for sharedvector in chosens group
106                                 for (int j = 0; j < lookup.size(); j++) { 
107                                         if (chosen.group == lookup[j]->getGroup()) {
108                                                 abundance = lookup[j]->getAbundance(chosen.bin);
109                                                 lookup[j]->set(chosen.bin, (abundance + 1), chosen.group);
110                                                 break;
111                                         }
112                                 }
113                         }
114                         
115                         //randomize the groups
116                         random_shuffle(lookup.begin(), lookup.end());
117                         
118                         vector<SharedRAbundVector*> subset;
119                         //send each group one at a time
120                         for (int k = 0; k < lookup.size(); k++) { 
121                                 subset.clear(); //clears out old pair of sharedrabunds
122                                 //add in new pair of sharedrabunds
123                                 subset.push_back(lookup[0]); subset.push_back(lookup[k]);
124                                 
125                                 rcd->updateSharedData(subset, k+1, numGroupComb);
126                                 mergeVectors(lookup[0], lookup[k]);
127                         }
128
129                         //resets output files
130                         for(int i=0;i<displays.size();i++){
131                                 displays[i]->reset();
132                         }
133                 }
134                 
135                 for(int i=0;i<displays.size();i++){
136                         displays[i]->close();
137                 }
138
139         }
140         catch(exception& e) {
141                 cout << "Standard Error: " << e.what() << " has occurred in the Rarefact class Function getSharedCurve. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
142                 exit(1);
143         }
144         catch(...) {
145                 cout << "An unknown error has occurred in the Rarefact class function getSharedCurve. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
146                 exit(1);
147         }
148
149 }
150
151 /**************************************************************************************/
152 void Rarefact::mergeVectors(SharedRAbundVector* shared1, SharedRAbundVector* shared2) {
153         try{
154                 for (int k = 0; k < shared1->size(); k++) {
155                         //merge new species into shared1
156                         if ((shared1->getAbundance(k) == 0) && (shared2->getAbundance(k) != 0)) {
157                                 shared1->set(k, shared2->getAbundance(k), "combo");  //set to 'combo' since this vector now contains multiple groups
158                         }
159                 }
160         }
161         catch(exception& e) {
162                 cout << "Standard Error: " << e.what() << " has occurred in the Rarefact class Function mergeVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
163                 exit(1);
164         }
165         catch(...) {
166                 cout << "An unknown error has occurred in the Rarefact class function mergeVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
167                 exit(1);
168         }       
169 }
170