-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHistogramStatsCollector.cc
188 lines (152 loc) · 6.35 KB
/
HistogramStatsCollector.cc
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
#include "HistogramStatsCollector.h"
#include <cmath>
using namespace BamstatsAlive;
using namespace std;
HistogramStatsCollector::HistogramStatsCollector(std::map<int32_t, std::string>& chromIDNameMap, unsigned int skipFactor, GenomicRegionStore* regionStore) :
_chromIDNameMap(chromIDNameMap),
kCovHistSkipFactor(skipFactor),
m_covHistAccumu(0),
_currentRegion(nullptr),
_regionStore(regionStore),
_coverageCollector(nullptr),
m_covHistTotalPos(0)
{
memset(m_mappingQualHist, 0, sizeof(unsigned int) * 256);
memset(m_baseQualHist, 0, sizeof(unsigned int) * 51);
m_lengthHist.clear();
m_fragHist.clear();
}
HistogramStatsCollector::~HistogramStatsCollector() {
}
void HistogramStatsCollector::updateReferenceHistogram(const BamTools::BamAlignment& al, const BamTools::RefVector& refVector) {
// increment ref aln counter
if ( al.RefID != -1) m_refAlnHist[ refVector[al.RefID].RefName ]++;
}
void HistogramStatsCollector::updateMappingQualityHistogram(const BamTools::BamAlignment& al) {
m_mappingQualHist[al.MapQuality]++;
}
void HistogramStatsCollector::updateReadLengthHistogram(const BamTools::BamAlignment& al) {
if(m_lengthHist.find(al.Length) != m_lengthHist.end())
m_lengthHist[al.Length]++;
else
m_lengthHist[al.Length] = 1;
}
void HistogramStatsCollector::updateFragmentSizeHistogram(const BamTools::BamAlignment& al) {
if ( al.IsPaired() && al.IsMapped() && al.IsMateMapped()) {
if( al.RefID == al.MateRefID && al.MatePosition > al.Position ) {
int32_t frag = al.InsertSize; //al.MatePosition - al.Position;
if(m_fragHist.find(frag) != m_fragHist.end())
m_fragHist[frag]++;
else
m_fragHist[frag] = 1;
}
}
}
void HistogramStatsCollector::updateBaseQualityHistogram(const BamTools::BamAlignment& al) {
const unsigned char *q = (const unsigned char *)al.Qualities.c_str();
if (q[0] != 0xff) {
for (int i = 0; i < al.Qualities.length(); ++i) {
unsigned int qual = (unsigned int)(q[i]) - 33;
if(qual >50) qual = 50;
m_baseQualHist[qual]++;
}
}
}
void HistogramStatsCollector::updateRegionalStats(const BamTools::BamAlignment& al, const BamTools::RefVector& refVector) {
if(!_regionStore) return;
decltype(_currentRegion) _thisReadRegion = nullptr;
// locate the region the current read is in
const GenomicRegionStore::GenomicRegionT& regionWithStart = _regionStore->locateRegion(_chromIDNameMap[al.RefID].c_str(), al.Position);
if(®ionWithStart != &GenomicRegionStore::kRegionNotFound()) {
_thisReadRegion = ®ionWithStart;
}
else {
const GenomicRegionStore::GenomicRegionT& regionWithEnd = _regionStore->locateRegion(_chromIDNameMap[al.RefID].c_str(), al.Position + al.Length);
if(®ionWithEnd != &GenomicRegionStore::kRegionNotFound()) {
_thisReadRegion = ®ionWithEnd;
}
}
if(_currentRegion != _thisReadRegion) {
if(_currentRegion != nullptr ) {
if(m_covHistAccumu == 0) {
// switched outside of pile up region
// update histogram and delete coverage collector
m_covHist = _coverageCollector->getEffectiveHistogram(m_covHistTotalPos);
delete _coverageCollector;
_coverageCollector = nullptr;
}
m_covHistAccumu++;
}
_currentRegion = _thisReadRegion;
}
if(m_covHistAccumu >= kCovHistSkipFactor) m_covHistAccumu = 0;
// not even in a pileup region
if(m_covHistAccumu != 0) return;
if(_currentRegion == nullptr) return;
updateBaseQualityHistogram(al);
// feed pileup
//_currentRegionLength = _currentRegion->endPos - _currentRegion->startPos + 1;
if(_coverageCollector == nullptr) {
_coverageCollector = new CoverageMapStatsCollector(_currentRegion, m_covHist);
}
_coverageCollector->processAlignment(al, refVector);
}
void HistogramStatsCollector::processAlignmentImpl(const BamTools::BamAlignment& al, const BamTools::RefVector& refVector) {
updateReferenceHistogram(al, refVector);
updateMappingQualityHistogram(al);
updateReadLengthHistogram(al);
updateFragmentSizeHistogram(al);
updateRegionalStats(al, refVector);
}
void HistogramStatsCollector::appendJsonImpl(json_t *jsonRootObj) {
// Mapping quality map
json_t * j_mapq_hist = json_object();
for(size_t i=0; i<256; i++) {
if (m_mappingQualHist[i] > 0) {
stringstream labelSS; labelSS << i;
json_object_set_new(j_mapq_hist, labelSS.str().c_str(), json_integer(m_mappingQualHist[i]));
}
}
json_object_set_new(jsonRootObj, "mapq_hist", j_mapq_hist);
// Base quality map
json_t * j_baseq_hist = json_object();
for(size_t i=0; i<=50; i++) {
if (m_baseQualHist[i] > 0) {
stringstream labelSS; labelSS << i;
json_object_set_new(j_baseq_hist, labelSS.str().c_str(), json_integer(m_baseQualHist[i]));
}
}
json_object_set_new(jsonRootObj, "baseq_hist", j_baseq_hist);
// Fragment length hisogram array
json_t * j_frag_hist = json_object();
for(map<int32_t, unsigned int>::iterator it = m_fragHist.begin(); it!=m_fragHist.end(); it++) {
stringstream labelSS; labelSS<<it->first;
json_object_set_new(j_frag_hist, labelSS.str().c_str(), json_integer(it->second));
}
json_object_set_new(jsonRootObj, "frag_hist", j_frag_hist);
// Read length histogram array
json_t * j_length_hist = json_object();
for(map<int32_t, unsigned int>::iterator it = m_lengthHist.begin(); it!=m_lengthHist.end(); it++) {
stringstream labelSS; labelSS << it->first;
json_object_set_new(j_length_hist, labelSS.str().c_str(), json_integer(it->second));
}
json_object_set_new(jsonRootObj, "length_hist", j_length_hist);
// Reference alignment histogram array
json_t * j_refAln_hist = json_object();
for(map<std::string, unsigned int>::iterator it = m_refAlnHist.begin(); it!=m_refAlnHist.end(); it++) {
stringstream labelSS; labelSS << it->first;
json_object_set_new(j_refAln_hist, labelSS.str().c_str(), json_integer(it->second));
}
json_object_set_new(jsonRootObj, "refAln_hist", j_refAln_hist);
// coverage histogram
if(_coverageCollector != nullptr)
_coverageCollector->appendJson(jsonRootObj);
else {
json_t * j_cov_hist = json_object();
for(auto it = m_covHist.begin(); it != m_covHist.end(); it++) {
stringstream labelSS; labelSS << it->first;
json_object_set_new(j_cov_hist, labelSS.str().c_str(), json_real( it->second / static_cast<double>(m_covHistTotalPos)));
}
json_object_set_new(jsonRootObj, "coverage_hist", j_cov_hist);
}
}