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