-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbench.cpp
368 lines (308 loc) · 8.72 KB
/
bench.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include "myutils.h"
#include "syncmerindex.h"
#include "randseq.h"
uint BENCHL = 1024*1024;
static FILE *fConsTab;
static FILE *fWinTab;
static byte *Seq;
static uint PctIds[] = { 99 , 98, 97, 95, 90, 85, 80 };
static const uint IDS = sizeof(PctIds)/sizeof(PctIds[0]);
static byte *MutatedSeqs[IDS];
static uint TestCount;
static uint WindowSize = 64;
static uint ConsBetterCount1;
static uint ConsBetterCount2;
static uint ConsTieCount;
static uint CovBetterCount1;
static uint CovBetterCount2;
static uint CovTieCount;
static uint SizeBetterCount1;
static uint SizeBetterCount2;
static uint SizeTieCount;
static uint WinBetterCount1;
static uint WinBetterCount2;
static uint WinTieCount;
static vector<double> CovDeltas;
static vector<double> ConsDeltas;
static vector<double> SizeDeltas;
static vector<double> ConsDeltasPctIds[IDS];
void InitBench2(uint WinSiz)
{
WindowSize = WinSiz;
fConsTab = CreateStdioFile(opt(tabbedout));
if (optset_seqlength)
{
extern uint BENCHL;
BENCHL = opt(seqlength);
}
ResetRand(1);
Seq = myalloc(byte, BENCHL);
MakeRandSeq(Seq, BENCHL);
for (uint i = 0; i < IDS; ++i)
{
uint PctId = PctIds[i];
MutatedSeqs[i] = myalloc(byte, BENCHL);
MutateSeq(Seq, BENCHL, PctId, MutatedSeqs[i]);
}
}
void Bench2(uint k, uint t, uint w, const string &Name1, const string &Name2)
{
//if (t + 1 >= k)
// return;
SyncmerType ST1 = StrToST(Name1);
SyncmerType ST2 = StrToST(Name2);
if (!SyncmerIndex::ValidParams(ST1, k, t, w))
return;
if (!SyncmerIndex::ValidParams(ST2, k, t, w))
return;
SyncmerIndex SI1;
SyncmerIndex SI2;
SI1.Create(ST1, k, t, w, Seq, BENCHL);
SI2.Create(ST2, k, t, w, Seq, BENCHL);
vector<SyncmerIndex> SI1s(IDS);
vector<SyncmerIndex> SI2s(IDS);
for (uint i = 0; i < IDS; ++i)
{
const byte *MutatedSeq = MutatedSeqs[i];
SI1s[i].Create(ST1, k, t, w, MutatedSeq, BENCHL);
SI2s[i].Create(ST2, k, t, w, MutatedSeq, BENCHL);
}
uint Size1 = SI1.GetIndexSize();
uint Size2 = SI2.GetIndexSize();
int dSize = int(Size1) - int(Size2);
double t1 = 1.0/SI1.GetFractKmersIndexed();
double t2 = 1.0/SI2.GetFractKmersIndexed();
double Cov1 = SI1.GetFractBasesCovered();
double Cov2 = SI2.GetFractBasesCovered();
double dCov = Cov1 - Cov2;
static bool HdrDone = false;
if (!HdrDone)
{
Pf(fConsTab, "k");
Pf(fConsTab, "\tt");
for (uint i = 0; i < IDS; ++i)
{
uint PctId = PctIds[i];
Pf(fConsTab, "\t%u%%", PctId);
}
Pf(fConsTab, "\tall\n");
Pf(fWinTab, "k");
Pf(fWinTab, "\tt");
for (uint i = 0; i < IDS; ++i)
{
uint PctId = PctIds[i];
Pf(fWinTab, "\t%u%%", PctId);
}
Pf(fWinTab, "\tall\n");
HdrDone = true;
}
Pf(fConsTab, "%u", k);
Pf(fConsTab, "\t%u", t);
Pf(fWinTab, "%u", k);
Pf(fWinTab, "\t%u", t);
Log("k=%u", k);
Log(", t=%u", t);
Log(", w=%u", w);
Log(", t1=%.1f", double(t1));
Log(", t2=%.1f", double(t2));
Log(", Size1=%.3g", double(Size1));
Log(", Size2=%.3g", double(Size2));
Log(", dSize=%+.1f%%", double(dSize*200)/double(Size1+Size2));
Log(", Cov1=%.4f", Cov1);
Log(", Cov2=%.4f", Cov2);
Log(", dCov=%.4f", dCov);
CovDeltas.push_back(dCov);
if (Cov1 > Cov2)
++CovBetterCount1;
else if (Cov2 > Cov1)
++CovBetterCount2;
else
{
asserta(Cov1 == Cov2);
++CovTieCount;
}
if (Size1 < Size2)
++SizeBetterCount1;
else if (Size2 < Size1)
++SizeBetterCount2;
else
{
asserta(Size1 == Size2);
++SizeTieCount;
}
double SumDeltaCons = 0.0;
double SumDeltaWin = 0.0;
for (uint i = 0; i < IDS; ++i)
{
uint PctId = PctIds[i];
double C1 = GetConservedSyncmerFract(SI1, SI1s[i]);
double C2 = GetConservedSyncmerFract(SI2, SI2s[i]);
++TestCount;
if (C1 > C2)
++ConsBetterCount1;
else if (C2 > C1)
++ConsBetterCount2;
else
{
asserta(C1 == C2);
++ConsTieCount;
}
double DeltaCons = C1 - C2;
SumDeltaCons += DeltaCons;
Pf(fConsTab, "\t%.4f", DeltaCons);
ConsDeltas.push_back(DeltaCons);
ConsDeltasPctIds[i].push_back(DeltaCons);
Log(", Cons1_%u=%.4f", PctId, C1);
Log(", Cons2_%u=%.4f", PctId, C2);
Log(", dCons_%u=%.4f", PctId, DeltaCons);
double W1 = GetFractWindowsWithSyncmer(SI1, SI1s[i], WindowSize);
double W2 = GetFractWindowsWithSyncmer(SI2, SI2s[i], WindowSize);
double DeltaW = W1 - W2;
SumDeltaWin += DeltaW;
Pf(fWinTab, "\t%.4f", DeltaW);
Log(", W1_%u=%.4f", PctId, W1);
Log(", W2_%u=%.4f", PctId, W2);
Log(", dW_%u=%.4f", PctId, DeltaW);
if (W1 > W2)
++WinBetterCount1;
else if (W2 > W1)
++WinBetterCount2;
else
{
asserta(W1 == W2);
++WinTieCount;
}
}
double MeanDeltaCons = SumDeltaCons/IDS;
Pf(fConsTab, "\t%.4f", MeanDeltaCons);
Pf(fConsTab, "\n");
double MeanDeltaWin = SumDeltaWin/IDS;
Pf(fWinTab, "\t%.4f", MeanDeltaWin);
Pf(fWinTab, "\n");
Log("\n");
SI1.Clear();
SI2.Clear();
for (uint i = 0; i < IDS; ++i)
{
SI1s[i].Clear();
SI2s[i].Clear();
}
}
void cmd_bench()
{
const string Names = opt(bench);
vector<string> Fields;
Split(Names, Fields, '+');
asserta(SIZE(Fields) == 2);
const string &Name1 = Fields[0];
const string &Name2 = Fields[1];
fConsTab = CreateStdioFile(opt(constabbedout));
fWinTab = CreateStdioFile(opt(wintabbedout));
if (optset_seqlength)
{
extern uint BENCHL;
BENCHL = opt(seqlength);
}
if (optset_window)
WindowSize = opt(window);
ResetRand(1);
Seq = myalloc(byte, BENCHL);
MakeRandSeq(Seq, BENCHL);
for (uint i = 0; i < IDS; ++i)
{
uint PctId = PctIds[i];
MutatedSeqs[i] = myalloc(byte, BENCHL);
MutateSeq(Seq, BENCHL, PctId, MutatedSeqs[i]);
}
if (optset_k)
{
uint k = opt(k);
uint t = opt(t);
uint w = opt(w);
Progress("name1=%s, name2=%s, k=%u, t=%u, w=%u\n",
Name1.c_str(), Name2.c_str(), k, t, w);
Bench2(k, t, w, Name1, Name2);
}
else if (opt(tile))
{
for (uint k = 8; k <= 16; ++k)
{
uint t = k - 1;
Progress("tile k=%u\n", k);
uint w = (optset_w ? opt(w) : SyncmerIndex::EstimateWindow(k, t));
Bench2(k, t, w, Name1, Name2);
}
}
else
{
for (uint k = 8; k <= 16; ++k)
{
Progress("k=%u\n", k);
for (uint t = 2; t <= 10; ++t)
{
uint w = (optset_w ? opt(w) : SyncmerIndex::EstimateWindow(k, t));
Bench2(k, t, w, Name1, Name2);
}
}
}
ProgressLog("Cons: %s better %u (%.1f%%)",
Name1.c_str(), ConsBetterCount1, GetPct(ConsBetterCount1, TestCount));
ProgressLog(", %s better %u (%.1f%%)",
Name2.c_str(), ConsBetterCount2, GetPct(ConsBetterCount2, TestCount));
if (ConsTieCount > 0)
ProgressLog(", ties %u (%.1f%%)",
ConsTieCount, GetPct(ConsTieCount, TestCount));
else
ProgressLog(", no ties");
ProgressLog("\n");
ProgressLog("Cov: %s better %u (%.1f%%)",
Name1.c_str(), CovBetterCount1, GetPct(CovBetterCount1, TestCount));
ProgressLog(", %s better %u (%.1f%%)",
Name2.c_str(), CovBetterCount2, GetPct(CovBetterCount2, TestCount));
if (CovTieCount > 0)
ProgressLog(", ties %u (%.1f%%)",
CovTieCount, GetPct(CovTieCount, TestCount));
else
ProgressLog(", no ties");
ProgressLog("\n");
ProgressLog("Size: %s better %u (%.1f%%)",
Name1.c_str(), SizeBetterCount1, GetPct(SizeBetterCount1, TestCount));
ProgressLog(", %s better %u (%.1f%%)",
Name2.c_str(), SizeBetterCount2, GetPct(SizeBetterCount2, TestCount));
if (SizeTieCount > 0)
ProgressLog(", ties %u (%.1f%%)",
SizeTieCount, GetPct(SizeTieCount, TestCount));
else
ProgressLog(", no ties");
ProgressLog("\n");
ProgressLog("Win: %s better %u (%.1f%%)",
Name1.c_str(), WinBetterCount1, GetPct(WinBetterCount1, TestCount));
ProgressLog(", %s better %u (%.1f%%)",
Name2.c_str(), WinBetterCount2, GetPct(WinBetterCount2, TestCount));
if (WinTieCount > 0)
ProgressLog(", ties %u (%.1f%%)",
WinTieCount, GetPct(WinTieCount, TestCount));
else
ProgressLog(", no ties");
ProgressLog("\n");
sort(CovDeltas.begin(), CovDeltas.end());
double MedianCovDelta = CovDeltas[SIZE(CovDeltas)/2];
ProgressLog("Cov difference : median %+.2f%%\n", MedianCovDelta*100.0);
sort(ConsDeltas.begin(), ConsDeltas.end());
double MedianConsDelta = ConsDeltas[TestCount/2];
ProgressLog("Cons difference: median %+.2f%%\n", MedianConsDelta*100.0);
ProgressLog("Cons by pctid: ");
for (uint i = 0; i < IDS; ++i)
{
uint PctId = PctIds[i];
vector<double> &ConsDeltas = ConsDeltasPctIds[i];
uint n = SIZE(ConsDeltas);
sort(ConsDeltas.begin(), ConsDeltas.end());
MedianConsDelta = ConsDeltas[n/2];
if (i > 0)
ProgressLog(", ");
ProgressLog("%u%%=%+.2f%%", PctId, MedianConsDelta*100.0);
}
ProgressLog("\n");
CloseStdioFile(fConsTab);
}