-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatsTracker.hpp
executable file
·444 lines (384 loc) · 13.7 KB
/
StatsTracker.hpp
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
* StatsTracker.hpp
*
* Created on: 2020年5月1日
* Author: fenghe
* Edited by: CRE 20210705
*/
#pragma once
#include<iostream>
#include <cstring>
#include <cassert>
#include <functional>
#include <iosfwd>
#include <map>
#include <vector>
#include "htslib/htslib/sam.h"
inline bool bam_is_paired(bam1_t* _bp) { return ((_bp->core.flag & BAM_FPAIRED) != 0);}
inline bool bam_is_proper_pair(bam1_t* _bp) { return ((_bp->core.flag & BAM_FPROPER_PAIR) != 0);}
inline bool bam_is_unmapped(bam1_t* _bp) { return ((_bp->core.flag & BAM_FUNMAP) != 0);}
inline bool bam_is_mate_unmapped(bam1_t* _bp) { return ((_bp->core.flag & BAM_FMUNMAP) != 0);}
inline bool bam_is_fwd_strand(bam1_t* _bp) { return (((_bp->core.flag & BAM_FREVERSE) == 0));}
inline bool bam_is_mate_fwd_strand(bam1_t* _bp) { return (((_bp->core.flag & BAM_FMREVERSE) == 0));}
inline bool bam_is_dup(bam1_t* _bp) { return ((_bp->core.flag & BAM_FDUP) != 0);}
inline bool bam_is_filter(bam1_t* _bp) { return ((_bp->core.flag & BAM_FQCFAIL) != 0);}
inline bool bam_is_first(bam1_t* _bp) { return ((_bp->core.flag & BAM_FREAD1) != 0);}
inline bool bam_is_second(bam1_t* _bp) { return ((_bp->core.flag & BAM_FREAD2) != 0);}
inline bool bam_is_secondary(bam1_t* _bp) { return ((_bp->core.flag & BAM_FSECONDARY) != 0);}
inline bool bam_is_supplementary(bam1_t* _bp) { return ((_bp->core.flag & BAM_FSUPPLEMENTARY) != 0);}
enum align_t { CIGAR_NONE, CIGAR_MATCH, CIGAR_INSERT, CIGAR_DELETE, CIGAR_SKIP, CIGAR_SOFT_CLIP,
CIGAR_HARD_CLIP, CIGAR_PAD, CIGAR_SEQ_MATCH, CIGAR_SEQ_MISMATCH };
extern "C" {
//#include "../clib/bam_file.h"
}
/****************PART ONE: Size Distribution***************************/
/// \brief Accumulate size observations and provide cdf/quantile/smoothed-pdf for the distribution
///
struct SizeData {
SizeData(unsigned initCount = 0, float initCprob = 0.) :
count(initCount), cprob(initCprob) {
}
unsigned count;
float cprob;
};
typedef std::map<int, SizeData, std::greater<int>> map_type;
struct SizeDistribution {
SizeDistribution() :
_isStatsComputed(false), _totalCount(0), _quantiles(_quantileNum, 0) {
}
/// \brief Implements the quantile function for this distribution
int quantile(const float prob) const; /// \return The size at which all sizes equal or less are observed with probability \p prob
float cdf(const int x) const;/// \return Probability of observing value <= \p x
float pdf(const int x) const;/// \return Probability of observing value \p x, (with a smoothing window)
unsigned totalObservations() const {
return _totalCount;
}
void addObservation(const int size) {
_isStatsComputed = false;
_totalCount++;
_sizeMap[size].count++;
}
void filterObservationsOverQuantile(const float prob);/// filter high value outliers:
bool isStatSetMatch(const SizeDistribution &pss2);/// compare distributions to determine stats convergence
int getSizeCount(int isize) const{
return _sizeMap[isize].count;
}
private:
void calcStats() const;
static const int _quantileNum = 1000;
mutable bool _isStatsComputed;
unsigned _totalCount;
mutable std::vector<int> _quantiles;
mutable map_type _sizeMap;
};
//********************************PART TWO: Read PAIR DIRECTION**************************/
typedef int32_t pos_t;
namespace PAIR_ORIENT {
enum index_t { UNKNOWN, Fm, Fp, Rm, Rp, SIZE};
inline const char* label(const index_t i) {
switch (i) {
case Fm: return "Fm";
case Fp: return "Fp";
case Rm: return "Rm";
case Rp: return "Rp";
default:
return "UNKNOWN";
}
}
inline index_t get_index(const pos_t pos1, const bool is_fwd_strand1,
const pos_t pos2, const bool is_fwd_strand2) {
const bool is_read1_left(pos1 < pos2);
if (is_fwd_strand1 != is_fwd_strand2) {
// special-case very short fragments as innies:
//
// a few bases of overhang are allowed to account for random matches of
// the reverse read to the primer
//
if (std::abs(pos1 - pos2) <= 2)
return Rp;
const bool left_strand(is_read1_left ? is_fwd_strand1 : is_fwd_strand2);
return (left_strand ? Rp : Rm);
} else {
return ((is_read1_left == is_fwd_strand1) ? Fp : Fm);
}
}
/// inefficient label to id lookup, returns SIZE for unknown string:
inline index_t get_index(const char *str) {
for (int i(0); i < SIZE; ++i) {
if (0 == strcmp(str, label(static_cast<index_t>(i))))
return static_cast<index_t>(i);
}
return SIZE;
}
} // END namespace PAIR_ORIENT
/// pair orientation status wrapper:
struct ReadPairOrient {
ReadPairOrient() :
_val(PAIR_ORIENT::UNKNOWN) {
}
PAIR_ORIENT::index_t val() const {
return _val;
}
void setVal(const unsigned newVal) {
assert(newVal < PAIR_ORIENT::SIZE);
_val = static_cast<PAIR_ORIENT::index_t>(newVal);
}
private:
PAIR_ORIENT::index_t _val;
};
//********************************PART Three: ReadCounter**************************/
/// \brief Accumulate read statistics scanned for insert size estimation
struct ReadCounter {
ReadCounter() :
_totalReadCount(0), _totalPairedReadCount(0), _totalUnpairedReadCount(
0), _totalPairedLowMapqReadCount(0), _totalHighConfidenceReadPairCount(
0) {}
unsigned totalReadCount() const { return _totalReadCount;}
unsigned totalPairedReadCount() const { return _totalPairedReadCount;}
unsigned totalUnpairedReadCount() const {return _totalUnpairedReadCount;}
unsigned totalPairedLowMapqReadCount() const {return _totalPairedLowMapqReadCount;}
unsigned totalHighConfidenceReadPairCount() const { return _totalHighConfidenceReadPairCount;}
void addReadCount() { _totalReadCount++;}
void addPairedReadCount() { _totalPairedReadCount++;}
void addUnpairedReadCount() { _totalUnpairedReadCount++;}
void addPairedLowMapqReadCount() { _totalPairedLowMapqReadCount++;}
void addHighConfidenceReadPairCount() { _totalHighConfidenceReadPairCount += 1;}
friend std::ostream& operator<<(std::ostream &os, const ReadCounter &rs) {
os << "\tTotal sampled reads: " + std::to_string(rs.totalReadCount()) + "\n"
<< "\tTotal sampled paired reads: "
+ std::to_string(rs.totalPairedReadCount()) + "\n"
<< "\tTotal sampled paired reads passing MAPQ filter: "
+ std::to_string(
rs.totalPairedReadCount()
- rs.totalPairedLowMapqReadCount()) + "\n"
<< "\tTotal sampled high-confidence read pairs passing all filters: "
+ std::to_string(rs.totalHighConfidenceReadPairCount())
+ "\n";
return os;
}
private:
///////////////////////////////////// data:
unsigned _totalReadCount;
unsigned _totalPairedReadCount;
unsigned _totalUnpairedReadCount;
unsigned _totalPairedLowMapqReadCount;
unsigned _totalHighConfidenceReadPairCount;
};
//********************************PART Four: Unique Stats**************************/
/// Read pair insert stats can be computed for each sample or read group, this
/// class represents the statistics for one group:
///
struct UniqueStats {
public:
SizeDistribution fragStats;
ReadPairOrient relOrients;
ReadCounter readCounter;
};
struct StatLabel {
/// if isCopyPtrs then the strings are copied and alloced/de-alloced by
/// the object, if false the client is responsible these pointers over
/// the lifetime of the label:
StatLabel(const char *bamLabelInit, const char *rgLabelInit,
const bool isCopyPtrsInit = true) :
isCopyPtrs(isCopyPtrsInit), bamLabel(
(isCopyPtrs && (nullptr != bamLabelInit)) ?
strdup(bamLabelInit) : bamLabelInit), rgLabel(
(isCopyPtrs && (nullptr != rgLabelInit)) ?
strdup(rgLabelInit) : rgLabelInit) {
assert(nullptr != bamLabel);
assert(nullptr != rgLabel);
}
StatLabel(const StatLabel &rhs) :
isCopyPtrs(rhs.isCopyPtrs), bamLabel(
isCopyPtrs ? strdup(rhs.bamLabel) : rhs.bamLabel), rgLabel(
isCopyPtrs ? strdup(rhs.rgLabel) : rhs.rgLabel) {
}
StatLabel& operator=(const StatLabel &rhs) {
if (this == &rhs)
return *this;
clear();
isCopyPtrs = rhs.isCopyPtrs;
bamLabel = (isCopyPtrs ? strdup(rhs.bamLabel) : rhs.bamLabel);
rgLabel = (isCopyPtrs ? strdup(rhs.rgLabel) : rhs.rgLabel);
return *this;
}
public:
~StatLabel() {
clear();
}
/// sort allowing for nullptr string pointers in primary and secondary key:
bool operator<(const StatLabel &rhs) const {
const int scval(strcmp(bamLabel, rhs.bamLabel));
if (scval < 0)
return true;
if (scval == 0) {
return (strcmp(rgLabel, rhs.rgLabel) < 0);
}
return false;
}
friend std::ostream& operator<<(std::ostream &os,
const StatLabel &rgl) {
os << "read group '" << rgl.rgLabel << "' in bam file '"
<< rgl.bamLabel;
return os;
}
private:
void clear() {
if (isCopyPtrs) {
if (nullptr != bamLabel)
free(const_cast<char*>(bamLabel));
if (nullptr != rgLabel)
free(const_cast<char*>(rgLabel));
}
}
bool isCopyPtrs;
public:
const char *bamLabel;
const char *rgLabel;
};
/// track pair orientation so that a consensus can be found for a read group
struct OrientTracker {
OrientTracker(const char *bamLabel, const char *rgLabel) :
_isFinalized(false), _totalOrientCount(0), _rgLabel(bamLabel,
rgLabel) {
std::fill(_orientCount.begin(), _orientCount.end(), 0);
}
void addOrient(const PAIR_ORIENT::index_t ori) {
static const unsigned maxOrientCount(100000);
if (_totalOrientCount >= maxOrientCount)
return;
if (ori == PAIR_ORIENT::UNKNOWN)
return;
addOrientImpl(ori);
}
const ReadPairOrient& getConsensusOrient(const ReadCounter &readCounter) {
finalize(readCounter);
return _finalOrient;
}
unsigned getMinCount() {
static const unsigned minCount(100);
return minCount;
}
bool isOrientCountGood() {
return (_totalOrientCount >= getMinCount());
}
private:
void addOrientImpl(const PAIR_ORIENT::index_t ori) {
assert(!_isFinalized);
assert(ori < PAIR_ORIENT::SIZE);
_orientCount[ori]++;
_totalOrientCount++;
}
void finalize(const ReadCounter &readCounter);
bool _isFinalized;
unsigned _totalOrientCount;
const StatLabel _rgLabel;
std::array<unsigned, PAIR_ORIENT::SIZE> _orientCount;
ReadPairOrient _finalOrient;
};
struct SimpleRead {
SimpleRead(PAIR_ORIENT::index_t ort, unsigned sz) :
_orient(ort), _insertSize(sz) {
}
PAIR_ORIENT::index_t _orient;
unsigned _insertSize;
};
struct ReadBuffer {
ReadBuffer() :
_abnormalRpCount(0), _observationRpCount(0) {
}
void updateBuffer(PAIR_ORIENT::index_t ort, unsigned sz) {
_readInfo.emplace_back(ort, sz);
if (ort == PAIR_ORIENT::Rp) {
_observationRpCount++;
if (sz >= 5000)
_abnormalRpCount++;
}
}
bool isBufferFull() const {
return (_observationRpCount >= 1000);
}
bool isBufferNormal() const {
if (_observationRpCount == 0)
return false;
return ((_abnormalRpCount / (float) _observationRpCount) < 0.01);
}
unsigned getAbnormalCount() {
return _abnormalRpCount;
}
unsigned getObservationCount() {
return _observationRpCount;
}
const std::vector<SimpleRead>& getBufferedReads() {
return _readInfo;
}
void clearBuffer() {
_abnormalRpCount = 0;
_observationRpCount = 0;
_readInfo.clear();
}
private:
unsigned _abnormalRpCount;
unsigned _observationRpCount;
std::vector<SimpleRead> _readInfo;
};
#define statsCheckCnt 100000
enum RGT_RETURN {
RGT_CONTINUE, RGT_BREAK, RGT_NORMAL
};
struct StatsTracker {
StatsTracker(const char *bamLabel = nullptr, const char *rgLabel =
nullptr, const std::string &defaultStatsFilename = "") :
_rgLabel(bamLabel, rgLabel), _orientInfo(bamLabel, rgLabel), _defaultStatsFilename(
defaultStatsFilename) {
}
unsigned insertSizeObservations() const { return _stats.fragStats.totalObservations();}
unsigned getMinObservationCount() const { static const unsigned minObservations(100); return minObservations; }
bool isObservationCountGood() const { return (insertSizeObservations() >= getMinObservationCount()); }
void checkInsertSizeCount() { if ((insertSizeObservations() % statsCheckCnt) == 0) _isChecked = true; }
bool isInsertSizeChecked() const { return _isChecked; }
void clearChecked() { _isChecked = false; }
bool isInsertSizeConverged() const { return _isInsertSizeConverged; }
bool isCheckedOrConverged() const { return (_isChecked || isInsertSizeConverged()); }
void updateInsertSizeConvergenceTest() { // check convergence
if (_oldInsertSize.totalObservations() > 0) {
_isInsertSizeConverged = _oldInsertSize.isStatSetMatch(
_stats.fragStats);
}
_oldInsertSize = _stats.fragStats;
}
ReadBuffer& getBuffer() { return _buffer; }
void addBufferedData();
const OrientTracker& getOrientInfo() const { return _orientInfo; }
const UniqueStats& getStats() const { assert(_isFinalized); return _stats; } /// getting a const ref of the stats forces finalization steps:
void addReadCount() { _stats.readCounter.addReadCount();}
void addPairedReadCount() { _stats.readCounter.addPairedReadCount();}
void addUnpairedReadCount() { _stats.readCounter.addUnpairedReadCount();}
void addPairedLowMapqReadCount() { _stats.readCounter.addPairedLowMapqReadCount();}
void addHighConfidenceReadPairCount() { _stats.readCounter.addHighConfidenceReadPairCount();}
/// Add one observation to the buffer
/// If the buffer is full, AND if the fragment size distribution in the buffer looks normal, add the buffered data;
/// otherwise, discard the buffer and move to the next region
bool addObservation(PAIR_ORIENT::index_t ori, unsigned sz);
void finalize();
void handleReadRecordBasic(bam1_t *b);
RGT_RETURN handleReadRecordCheck(bam1_t *b);
private:
void addOrient(const PAIR_ORIENT::index_t ori) {
assert(!_isFinalized);
_orientInfo.addOrient(ori);
}
void addInsertSize(const int size) {
assert(!_isFinalized);
_stats.fragStats.addObservation(size);
}
bool _isFinalized = false;
const StatLabel _rgLabel;
OrientTracker _orientInfo;
bool _isChecked = false;
bool _isInsertSizeConverged = false;
SizeDistribution _oldInsertSize; // previous fragment distribution is stored to determine convergence
ReadBuffer _buffer;
UniqueStats _stats;
const std::string _defaultStatsFilename;
};