]> git.donarmstrong.com Git - mothur.git/blob - collect.cpp
This is v.1.4.0
[mothur.git] / collect.cpp
1 /*
2  *  collect.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 "collect.h"
11
12 /***********************************************************************/
13
14 void Collect::getCurve(int increment = 1){
15         try {
16                 RAbundVector* lookup = new RAbundVector(order->getNumBins());
17                 SAbundVector* rank        = new SAbundVector(order->getMaxRank()+1);
18
19                 CollectorsCurveData* ccd = new CollectorsCurveData();
20         
21                 for(int i=0;i<displays.size();i++){
22                         ccd->registerDisplay(displays[i]); //adds a display[i] to cdd
23                         displays[i]->init(label);                   //sets displays label
24                 }                                                                           
25                 for(int i=0;i<numSeqs;i++){
26
27                         int binNumber = order->get(i);
28                         int abundance = lookup->get(binNumber);
29                 
30                         rank->set(abundance, rank->get(abundance)-1); 
31                 
32                         abundance++;
33                 
34                         lookup->set(binNumber, abundance);
35                         rank->set(abundance, rank->get(abundance)+1); //increment rank(abundance)
36
37                         if((i == 0) || (i+1) % increment == 0){
38                                 ccd->updateRankData(rank);
39                         }
40                 }
41         
42                 if(numSeqs % increment != 0){
43                         ccd->updateRankData(rank);
44                 }
45         
46                 for(int i=0;i<displays.size();i++){
47                         displays[i]->reset();
48                 }
49                                 
50                                 delete lookup;
51                                 delete rank;
52                                 delete ccd;
53         }
54         catch(exception& e) {
55                 cout << "Standard Error: " << e.what() << " has occurred in the Collect class Function getCurve. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
56                 exit(1);
57         }
58         catch(...) {
59                 cout << "An unknown error has occurred in the Collect class function getCurve. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
60                 exit(1);
61         }
62
63 }
64
65 /***********************************************************************/
66 void Collect::getSharedCurve(int increment = 1){
67 try {
68                 globaldata = GlobalData::getInstance();
69                 vector<SharedRAbundVector*> lookup; 
70                                 vector<SharedRAbundVector*> subset;
71
72                 //create and initialize vector of sharedvectors, one for each group
73                 for (int i = 0; i < globaldata->Groups.size(); i++) { 
74                         SharedRAbundVector* temp = new SharedRAbundVector(sharedorder->getNumBins());
75                         temp->setLabel(sharedorder->getLabel());
76                         temp->setGroup(globaldata->Groups[i]);
77                         temp->setGroupIndex(globaldata->gGroupmap->groupIndex[globaldata->Groups[i]]);
78                         lookup.push_back(temp);
79                 }
80
81                 SharedCollectorsCurveData* ccd = new SharedCollectorsCurveData();
82         
83                 //initialize labels for output
84                 //makes  'uniqueAB         uniqueAC  uniqueBC' if your groups are A, B, C
85                 getGroupComb();
86                 groupLabel = ""; 
87                 for (int s = 0; s < groupComb.size(); s++) {
88                         groupLabel = groupLabel + label + groupComb[s] + "\t";
89                 }
90                                 
91                                 //for multi displays
92                                 string groupLabelAll = groupLabel + label + "all\t";
93                                 
94                 for(int i=0;i<displays.size();i++){
95                         ccd->registerDisplay(displays[i]); //adds a display[i] to cdd
96                                                 if (displays[i]->isCalcMultiple() == true)  {   displays[i]->init(groupLabelAll); }
97                                                 else {  displays[i]->init(groupLabel);  }           
98                 }
99                 
100                 //sample all the members
101                 for(int i=0;i<numSeqs;i++){
102                         //get first sample
103                         individual chosen = sharedorder->get(i);
104                         int abundance; 
105                                         
106                         //set info for sharedvector in chosens group
107                         for (int j = 0; j < lookup.size(); j++) { 
108                                                         if (chosen.group == lookup[j]->getGroup()) {
109                                                                 abundance = lookup[j]->getAbundance(chosen.bin);
110                                                                 lookup[j]->set(chosen.bin, (abundance + 1), chosen.group);
111                                                                 break;
112                                                         }
113                         }
114                         
115         
116                         //calculate at 0 and the given increment
117                         if((i == 0) || (i+1) % increment == 0){
118
119                                                                 //how many comparisons to make i.e. for group a, b, c = ab, ac, bc.
120
121                                 int n = 1;
122                                 for (int k = 0; k < (lookup.size() - 1); k++) { // pass cdd each set of groups to commpare
123                                         for (int l = n; l < lookup.size(); l++) {
124                                                                                                 subset.clear(); //clear out old pair of sharedrabunds
125                                                                                                 //add new pair of sharedrabund vectors
126                                                                                                 subset.push_back(lookup[k]); subset.push_back(lookup[l]);
127                                                 ccd->updateSharedData(subset, i+1, globaldata->Groups.size());
128                                         }
129                                         n++;
130                                 }
131                                                                 //if this is a calculator that can do multiples then do them
132                                                                 ccd->updateSharedData(lookup, i+1, globaldata->Groups.size()); 
133                         }
134                         totalNumSeq = i+1;
135                 }
136                 
137                 //calculate last line if you haven't already
138                 if(numSeqs % increment != 0){
139                         //how many comparisons to make i.e. for group a, b, c = ab, ac, bc.
140                         int n = 1;
141                         for (int k = 0; k < (lookup.size() - 1); k++) { // pass cdd each set of groups to commpare
142                                 for (int l = n; l < lookup.size(); l++) {
143                                                                                 subset.clear(); //clear out old pair of sharedrabunds
144                                                                                 //add new pair of sharedrabund vectors
145                                                                                 subset.push_back(lookup[k]); subset.push_back(lookup[l]);
146                                                                                 ccd->updateSharedData(subset, totalNumSeq, globaldata->Groups.size());
147                                 }
148                                 n++;
149                         }
150                                                 //if this is a calculator that can do multiples then do them
151                                                 ccd->updateSharedData(lookup, totalNumSeq, globaldata->Groups.size()); 
152                 }
153                 
154                 //resets output files
155                 for(int i=0;i<displays.size();i++){
156                         displays[i]->reset();
157                 }
158                                 
159                                 //memory cleanup
160                                 delete ccd;
161                                 for (int i = 0; i < lookup.size(); i++) {
162                                         delete lookup[i];
163                                 }
164
165         }
166         catch(exception& e) {
167                 cout << "Standard Error: " << e.what() << " has occurred in the Collect class Function getSharedCurve. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
168                 exit(1);
169         }
170         catch(...) {
171                 cout << "An unknown error has occurred in the Collect class function getSharedCurve. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
172                 exit(1);
173         }
174
175 }
176
177 /**************************************************************************************/
178
179 void Collect::getGroupComb() {
180         string group;
181                 
182         numGroupComb = 0;
183                 
184         int n = 1;
185         for (int i = 0; i < (globaldata->Groups.size() - 1); i++) {
186                 for (int l = n; l < globaldata->Groups.size(); l++) {
187                         group = globaldata->Groups[i] + globaldata->Groups[l];
188                         groupComb.push_back(group);        
189                         numGroupComb++;
190                 }
191                 n++;
192         }
193
194 }
195
196 /**************************************************************************************/