-
Notifications
You must be signed in to change notification settings - Fork 3
/
CoverageMapStatsCollector.cc
80 lines (65 loc) · 2.92 KB
/
CoverageMapStatsCollector.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
#include "CoverageMapStatsCollector.h"
using namespace BamstatsAlive;
using namespace std;
CoverageMapStatsCollector::CoverageMapStatsCollector(const GenomicRegionStore::GenomicRegionT * currentRegion, const coverageHistT& existingHistogram) :
AbstractStatCollector(),
_currentRegion(currentRegion),
_coveredLength(0),
_existingCoverageHist(existingHistogram)
{
auto regionLength = _currentRegion->endPos - _currentRegion->startPos + 1;
_regionalCoverageMap = new unsigned int [regionLength];
memset(_regionalCoverageMap, 0, sizeof(unsigned int) * regionLength);
LOGS<<"new CoverageMapStatsCollector!!"<<std::endl;
}
CoverageMapStatsCollector::~CoverageMapStatsCollector() {
delete [] _regionalCoverageMap;
}
void CoverageMapStatsCollector::processAlignmentImpl(const BamTools::BamAlignment& al, const BamTools::RefVector& refVector) {
if(!_currentRegion->contains(_currentRegion->chrom, al.Position) && !_currentRegion->contains(_currentRegion->chrom, al.Position + al.Length))
return;
auto regionLength = _currentRegion->endPos - _currentRegion->startPos + 1;
auto readMappedStartPos = al.Position < _currentRegion->startPos ? 0 : al.Position - _currentRegion->startPos;
auto readMappedEndPos = al.Position + al.Length > _currentRegion->endPos ? regionLength - 1 : al.Position + al.Length - _currentRegion->startPos;
for(auto i=readMappedStartPos; i <= readMappedEndPos; i++) _regionalCoverageMap[i]++;
if(readMappedStartPos > _coveredLength) {
for(size_t i=_coveredLength; i<readMappedStartPos; i++) {
auto cov = _regionalCoverageMap[i];
auto covEntryIter = _coverageHist.find(cov);
if(covEntryIter != _coverageHist.end())
_coverageHist[cov]++;
else
_coverageHist[cov] = 1;
}
_coveredLength = readMappedStartPos;
}
}
CoverageMapStatsCollector::coverageHistT CoverageMapStatsCollector::getEffectiveHistogram(unsigned int& totalPos) {
totalPos = 0;
coverageHistT effHist;
for(auto it = _coverageHist.cbegin(); it != _coverageHist.cend(); it++) {
if(_existingCoverageHist.find(it->first) != _existingCoverageHist.cend())
effHist[it->first] = _existingCoverageHist.at(it->first) + it->second;
else
effHist[it->first] = it->second;
totalPos += effHist[it->first];
}
for(auto it = _existingCoverageHist.cbegin(); it != _existingCoverageHist.cend(); it++) {
if(effHist.find(it->first) == effHist.cend()) {
effHist[it->first] = it->second;
totalPos += effHist[it->first];
}
}
return effHist;
}
void CoverageMapStatsCollector::appendJsonImpl(json_t * jsonRootObj) {
// Coverage Histogram
json_t * j_cov_hist = json_object();
unsigned int totalPos = 0;
coverageHistT effHist = getEffectiveHistogram(totalPos);
for(auto it = effHist.begin(); it != effHist.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>(totalPos)));
}
json_object_set_new(jsonRootObj, "coverage_hist", j_cov_hist);
}