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