]> git.donarmstrong.com Git - mothur.git/blob - rarefact.cpp
created mothurOut class to handle logfiles
[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                         delete lookup;
57                         delete rank;
58                 }
59
60                 for(int i=0;i<displays.size();i++){
61                         displays[i]->close();
62                 }
63                 delete rcd;
64         }
65         catch(exception& e) {
66                 m->errorOut(e, "Rarefact", "getCurve");
67                 exit(1);
68         }
69 }
70
71 /***********************************************************************/
72
73 void Rarefact::getSharedCurve(int increment = 1, int nIters = 1000){
74 try {
75                 SharedRarefactionCurveData* rcd = new SharedRarefactionCurveData();
76                 
77                 label = lookup[0]->getLabel();
78                 
79                 //register the displays
80                 for(int i=0;i<displays.size();i++){
81                         rcd->registerDisplay(displays[i]);
82                 }
83                 
84                 //if jumble is false all iters will be the same
85                 if (globaldata->jumble == false)  {  nIters = 1;  }
86                 
87                 for(int iter=0;iter<nIters;iter++){
88                 
89                         for(int i=0;i<displays.size();i++){
90                                 displays[i]->init(label);                 
91                         }
92                         
93                         if (globaldata->jumble == true)  {
94                                 //randomize the groups
95                                 random_shuffle(lookup.begin(), lookup.end());
96                         }
97                         
98                         //make merge the size of lookup[0]
99                         SharedRAbundVector* merge = new SharedRAbundVector(lookup[0]->size());
100                         
101                         //make copy of lookup zero
102                         for(int i = 0; i<lookup[0]->size(); i++) {
103                                 merge->set(i, lookup[0]->getAbundance(i), "merge");
104                         }
105                         
106                         vector<SharedRAbundVector*> subset;
107                         //send each group one at a time
108                         for (int k = 0; k < lookup.size(); k++) { 
109                                 subset.clear(); //clears out old pair of sharedrabunds
110                                 //add in new pair of sharedrabunds
111                                 subset.push_back(merge); subset.push_back(lookup[k]);
112                                 
113                                 rcd->updateSharedData(subset, k+1, numGroupComb);
114                                 mergeVectors(merge, lookup[k]);
115                         }
116
117                         //resets output files
118                         for(int i=0;i<displays.size();i++){
119                                 displays[i]->reset();
120                         }
121                         
122                         delete merge;
123                 }
124                 
125                 for(int i=0;i<displays.size();i++){
126                         displays[i]->close();
127                 }
128                 
129                 delete rcd;
130         }
131         catch(exception& e) {
132                 m->errorOut(e, "Rarefact", "getSharedCurve");
133                 exit(1);
134         }
135 }
136
137 /**************************************************************************************/
138 void Rarefact::mergeVectors(SharedRAbundVector* shared1, SharedRAbundVector* shared2) {
139         try{
140                 for (int k = 0; k < shared1->size(); k++) {
141                         //merge new species into shared1
142                         shared1->set(k, (shared1->getAbundance(k) + shared2->getAbundance(k)), "combo");  //set to 'combo' since this vector now contains multiple groups
143                 }
144         }
145         catch(exception& e) {
146                 m->errorOut(e, "Rarefact", "mergeVectors");
147                 exit(1);
148         }
149 }
150