]> git.donarmstrong.com Git - mothur.git/blob - collect.cpp
1.18.1
[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 int Collect::getCurve(float percentFreq = 0.01){
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                                 
26                                 //convert freq percentage to number
27                                 int increment = 1;
28                                 if (percentFreq < 1.0) {  increment = numSeqs * percentFreq;  }
29                                 else { increment = percentFreq;  }
30                                                                                                                                                                                                                                                         
31                 for(int i=0;i<numSeqs;i++){
32                                                 
33                                                 if (m->control_pressed) { delete lookup; delete rank; delete ccd;  return 1;  }
34                                                 
35                         int binNumber = order->get(i);
36                         int abundance = lookup->get(binNumber);
37                 
38                         rank->set(abundance, rank->get(abundance)-1); 
39                 
40                         abundance++;
41                 
42                         lookup->set(binNumber, abundance);
43                         rank->set(abundance, rank->get(abundance)+1); //increment rank(abundance)
44
45                         if((i == 0) || (i+1) % increment == 0){
46                                 ccd->updateRankData(rank);
47                         }
48                 }
49         
50                 if(numSeqs % increment != 0){
51                         ccd->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                                 delete ccd;
61                                 
62                                 return 0;
63         }
64         catch(exception& e) {
65                         m->errorOut(e, "Collect", "getCurve");
66                         exit(1);
67         }
68 }
69
70 /***********************************************************************/
71 int Collect::getSharedCurve(float percentFreq = 0.01){
72 try {
73                 vector<SharedRAbundVector*> lookup; 
74                                 vector<SharedRAbundVector*> subset;
75
76                 //create and initialize vector of sharedvectors, one for each group
77                 for (int i = 0; i < m->Groups.size(); i++) { 
78                         SharedRAbundVector* temp = new SharedRAbundVector(sharedorder->getNumBins());
79                         temp->setLabel(sharedorder->getLabel());
80                         temp->setGroup(m->Groups[i]);
81                                                 lookup.push_back(temp);
82                 }
83         
84                 SharedCollectorsCurveData* ccd = new SharedCollectorsCurveData();
85         
86                 //initialize labels for output
87                 //makes  'uniqueAB         uniqueAC  uniqueBC' if your groups are A, B, C
88                 getGroupComb();
89                                 
90                 for(int i=0;i<displays.size();i++){
91                         ccd->registerDisplay(displays[i]); //adds a display[i] to cdd
92                                                 bool hasLciHci = displays[i]->hasLciHci();
93                                                 groupLabel = "";
94                                                 for (int s = 0; s < groupComb.size(); s++) {
95                                                         if (hasLciHci) {  groupLabel = groupLabel + label + groupComb[s] + "\t" + label + groupComb[s] + "lci\t" + label + groupComb[s] + "hci\t"; }
96                                                         else{  groupLabel = groupLabel + label + groupComb[s] + "\t";  }
97                                                 }
98
99                                                 string groupLabelAll = groupLabel + label + "all\t"; 
100                                                 if ((displays[i]->isCalcMultiple() == true) && (displays[i]->getAll() == true)) {   displays[i]->init(groupLabelAll); }
101                                                 else {  displays[i]->init(groupLabel);  }           
102                 }
103                 
104                                 //convert freq percentage to number
105                                 int increment = 1;
106                                 if (percentFreq < 1.0) {  increment = numSeqs * percentFreq;  }
107                                 else { increment = percentFreq;  }
108                                 
109                 //sample all the members
110                 for(int i=0;i<numSeqs;i++){
111                                 
112                                                 if (m->control_pressed) { for (int j = 0; j < lookup.size(); j++) {  delete lookup[j]; } delete ccd;  return 1;  }
113                                                 
114                         //get first sample
115                         individual chosen = sharedorder->get(i);
116                         int abundance; 
117                                                      
118                         //set info for sharedvector in chosens group
119                         for (int j = 0; j < lookup.size(); j++) { 
120                                                         if (chosen.group == lookup[j]->getGroup()) {
121                                                                 abundance = lookup[j]->getAbundance(chosen.bin);
122                                                                 lookup[j]->set(chosen.bin, (abundance + 1), chosen.group);
123                                                                 break;
124                                                         }
125                         }
126         
127                         //calculate at 0 and the given increment
128                         if((i == 0) || (i+1) % increment == 0){
129
130                                                                 //how many comparisons to make i.e. for group a, b, c = ab, ac, bc.
131
132                                 int n = 1;
133                                                                 bool pair = true;
134                                 for (int k = 0; k < (lookup.size() - 1); k++) { // pass cdd each set of groups to commpare
135                                         for (int l = n; l < lookup.size(); l++) {
136                                                                                                 subset.clear(); //clear out old pair of sharedrabunds
137                                                                                                 //add new pair of sharedrabund vectors
138                                                                                                 subset.push_back(lookup[k]); subset.push_back(lookup[l]);
139                                                                                         
140                                                                                                 //load subset with rest of lookup for those calcs that need everyone to calc for a pair
141                                                                                                 for (int w = 0; w < lookup.size(); w++) {
142                                                                                                         if ((w != k) && (w != l)) { subset.push_back(lookup[w]); }
143                                                                                                 }
144                                                 
145                                                 ccd->updateSharedData(subset, i+1, m->Groups.size(), pair);
146                                         }
147                                         n++;
148                                 }
149                                                         
150                                                                 //if this is a calculator that can do multiples then do them
151                                                                 pair = false;
152                                                                 ccd->updateSharedData(lookup, i+1, m->Groups.size(), pair); 
153                                                         
154                         }
155                         totalNumSeq = i+1;
156                 }
157                 
158                 //calculate last label if you haven't already
159                 if(numSeqs % increment != 0){
160                         //how many comparisons to make i.e. for group a, b, c = ab, ac, bc.
161                         int n = 1;
162                                                 bool pair = true;
163                         for (int k = 0; k < (lookup.size() - 1); k++) { // pass cdd each set of groups to commpare
164                                 for (int l = n; l < lookup.size(); l++) {
165                                                                                 subset.clear(); //clear out old pair of sharedrabunds
166                                                                                 //add new pair of sharedrabund vectors
167                                                                                 subset.push_back(lookup[k]); subset.push_back(lookup[l]);
168                                                                         
169                                                                                 //load subset with rest of lookup for those calcs that need everyone to calc for a pair
170                                                                                 for (int w = 0; w < lookup.size(); w++) {
171                                                                                         if ((w != k) && (w != l)) { subset.push_back(lookup[w]); }
172                                                                                 }
173                                                                         
174                                                                                 ccd->updateSharedData(subset, totalNumSeq, m->Groups.size(), pair);
175                                 }
176                                 n++;
177                         }
178                                                 //if this is a calculator that can do multiples then do them
179                                                 pair = false;
180                                                 ccd->updateSharedData(lookup, totalNumSeq, m->Groups.size(), pair); 
181                 }
182          
183                 //resets output files
184                 for(int i=0;i<displays.size();i++){
185                         displays[i]->reset();
186                 }
187                                 
188                                 //memory cleanup
189                                 delete ccd;
190                                 for (int i = 0; i < lookup.size(); i++) {
191                                         delete lookup[i];
192                                 }
193                                 
194                                 return 0;
195
196         }
197         catch(exception& e) {
198                 m->errorOut(e, "Collect", "getSharedCurve");
199                                 exit(1);
200         }
201 }
202
203 /**************************************************************************************/
204
205 void Collect::getGroupComb() {
206         string group;
207                 
208         numGroupComb = 0;
209                 
210         int n = 1;
211         for (int i = 0; i < (m->Groups.size() - 1); i++) {
212                 for (int l = n; l < m->Groups.size(); l++) {
213                         group = m->Groups[i] + m->Groups[l];
214                         groupComb.push_back(group);        
215                         numGroupComb++;
216                 }
217                 n++;
218         }
219
220 }
221
222 /**************************************************************************************/