-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCBigWigReader.cpp
More file actions
305 lines (277 loc) · 9.76 KB
/
CBigWigReader.cpp
File metadata and controls
305 lines (277 loc) · 9.76 KB
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: CBigWigReader.cpp
* Author: mwittig
*
* Created on August 7, 2019, 9:52 AM
*/
#include <cstdlib>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <libgen.h>
#include <iostream>
#include <cstring>
#include <limits>
#include <htslib/sam.h>
#include "meinetools.h"
#include "CBigWigReader.h"
using namespace std;
int CBigWigReader::m_instance_counter = 0;
CBigWigReader::CBigWigReader(const std::string& filename)
{
m_fp = NULL;
if(!CMyTools::file_exists(filename))
throw(CMyException("File does not exist: ")+filename);
// BAM erkennen
if (filename.size() >= 4 && filename.substr(filename.size()-4) == ".bam") {
if (!initBam(filename)) throw(CMyException("Failed to open BAM: ")+filename);
m_useBam = true;
} else {
if (!init(filename)) throw(CMyException("Failed to open BigWig: ")+filename);
m_useBam = false;
}
}
CBigWigReader::CBigWigReader(const CBigWigReader& orig)
{
m_fp = NULL;
m_useBam = false;
m_bam = nullptr; m_bamHdr = nullptr; m_bamIdx = nullptr;
// memcpy( m_fp, orig.m_fp, sizeof(*orig.m_fp) );
}
CBigWigReader::~CBigWigReader()
{
if(m_fp) bwClose(m_fp);
if(m_bamIdx) hts_idx_destroy(m_bamIdx);
if(m_bamHdr) bam_hdr_destroy(m_bamHdr);
if(m_bam) sam_close(m_bam);
if(--m_instance_counter == 0)
bwCleanup();
}
bool CBigWigReader::ready() const {
if (m_useBam) return m_bam && m_bamHdr && m_bamIdx;
return m_fp != nullptr;
}
double CBigWigReader::getCoverage(const std::string& chrom, int start, int end, bwStatsType type)const
{
if (m_useBam) {
return getCoverageFromBam(chrom, start, end, type);
}
else {
/// enum bwStatsType:
/// doesNotExist = -1, /*!< This does nothing */
/// mean = 0, /*!< The mean value */
/// average = 0, /*!< The mean value */
/// stdev = 1, /*!< The standard deviation of the values */
/// dev = 1, /*!< The standard deviation of the values */
/// max = 2, /*!< The maximum value */
/// min = 3, /*!< The minimum value */
/// cov = 4, /*!< The number of bases covered */
/// coverage = 4, /*!<The number of bases covered */
/// sum = 5 /*!< The sum of per-base values */
double bRet = std::numeric_limits<float>::quiet_NaN();
if(ready()) {
string str = chrom;
char *cstr = &str[0];
double *stats = bwStatsFromFull(m_fp, cstr,start, end, 1, type);
if(stats) {
bRet = stats[0];
free(stats);
}
}
return bRet;
}
}
bool CBigWigReader::isCovered(const std::string& chrom, int start, int end, double min_required)
{
double bVal = getAverageCoverage( chrom, start, end);
if(bVal != bVal || bVal < min_required)
return false;
return true;
}
double CBigWigReader::getPercentCoveredBases(const std::string& chrom, int start, int end)
{
if (m_useBam) {
return getPercentCoveredFromBam(chrom, start, end);
}
else {
double bRet = std::numeric_limits<double>::quiet_NaN();
if(ready())
{
/*
typedef struct {
uint32_t l; //<Number of intervals held
uint32_t m; //<Maximum number of values/intervals the struct can hold
uint32_t *start; //<The start positions (0-based half open)
uint32_t *end; //<The end positions (0-based half open)
float *value; //<The value associated with each position
} bwOverlappingIntervals_t;
*/
string str = chrom;
char *cstr = &str[0];
bwOverlappingIntervals_t* intervals = bwGetValues(m_fp, cstr,start, end,0);
if(intervals)
{
int length = 0;
for(uint32_t i = 0; i < intervals->l; i++)
{
if(intervals->value[i] == intervals->value[i])
length++;
}
bRet = 100.0*static_cast<double>(length)/static_cast<double>(end-start);
bwDestroyOverlappingIntervals(intervals);
}
}
return bRet;
}
}
bool CBigWigReader::init(const std::string& filename)
{
//Initialize enough space to hold 128KiB (1<<17) of data at a time
if(m_instance_counter++ == 0)
{
if(bwInit(1<<17) != 0)
return false;
}
string str = filename;
char *cstr = &str[0];
if(!bwIsBigWig(cstr, NULL))
{
cerr << "An error occured while opening " << filename << ", which does not seem to be a bigWig file."<< endl;
return false;
}
m_fp = bwOpen(cstr, NULL, "r");
if(!m_fp) {
cerr << "An error occured while opening " << filename << endl;
return false;
}
return true;
}
bool CBigWigReader::initBam(const std::string& filename)
{
// BAM öffnen, Header lesen, Index laden
m_bam = sam_open(filename.c_str(), "r");
if(!m_bam) {
std::cerr << "Failed to open BAM " << filename << std::endl;
return false;
}
m_bamHdr = sam_hdr_read(m_bam);
if(!m_bamHdr) {
std::cerr << "Failed to read BAM header " << filename << std::endl;
return false;
}
m_bamIdx = sam_index_load(m_bam, filename.c_str());
if(!m_bamIdx) {
std::cerr << "BAM index (.bai/.csi) not found for " << filename << std::endl;
return false;
}
return true;
}
// -------- BAM-Coverage-Helfer --------
static inline void add_block_cov(std::vector<uint32_t>& cov, int32_t rpos, uint32_t len, int winStart, int winEnd) {
if (len == 0) return;
int32_t s = rpos;
int32_t e = rpos + (int32_t)len;
if (e <= winStart || s >= winEnd) return;
if (s < winStart) s = winStart;
if (e > winEnd) e = winEnd;
for (int32_t i = s; i < e; ++i) cov[(size_t)(i - winStart)] += 1u;
}
double CBigWigReader::getCoverageFromBam(const std::string& chrom, int start, int end, bwStatsType type) const {
if (!ready() || start >= end) return std::numeric_limits<double>::quiet_NaN();
int tid = bam_name2id(m_bamHdr, chrom.c_str());
if (tid < 0) return std::numeric_limits<double>::quiet_NaN();
const int winStart = start;
const int winEnd = end;
const size_t winLen = (size_t)(winEnd - winStart);
std::vector<uint32_t> cov(winLen, 0u);
hts_itr_t* itr = sam_itr_queryi(m_bamIdx, tid, winStart, winEnd);
if (!itr) return std::numeric_limits<double>::quiet_NaN();
bam1_t* b = bam_init1();
while (sam_itr_next(m_bam, itr, b) >= 0) {
const bam1_core_t& c = b->core;
if (c.tid != tid) continue;
int32_t rpos = c.pos; // 0-based
uint32_t* cigar = bam_get_cigar(b);
for (uint32_t k = 0; k < c.n_cigar; ++k) {
uint32_t op = bam_cigar_op(cigar[k]);
uint32_t len = bam_cigar_oplen(cigar[k]);
switch (op) {
case BAM_CMATCH: // M
case BAM_CEQUAL: // =
case BAM_CDIFF: // X
add_block_cov(cov, rpos, len, winStart, winEnd);
rpos += len; break;
case BAM_CDEL:
case BAM_CREF_SKIP:
rpos += len; break;
case BAM_CINS:
case BAM_CSOFT_CLIP:
case BAM_CHARD_CLIP:
case BAM_CPAD:
default:
// keine Referenzfortschreibung
break;
}
}
}
bam_destroy1(b);
hts_itr_destroy(itr);
// Statistiken
if (type == bwStatsType::cov) {
// Anzahl bedeckter Basen (>0)
size_t covered = 0;
for (auto v: cov) if (v > 0) ++covered;
return static_cast<double>(covered);
}
// sum/min/max/mean über per-base coverage
uint64_t sum = 0;
uint32_t vmax = 0;
uint32_t vmin = std::numeric_limits<uint32_t>::max();
for (auto v: cov) {
sum += v;
if (v > vmax) vmax = v;
if (v < vmin) vmin = v;
}
if (winLen == 0) return std::numeric_limits<double>::quiet_NaN();
switch (type) {
case bwStatsType::sum:
return static_cast<double>(sum);
case bwStatsType::max:
return static_cast<double>(vmax);
case bwStatsType::min:
return static_cast<double>(vmin == std::numeric_limits<uint32_t>::max() ? 0.0 : (double)vmin);
case bwStatsType::average:
return static_cast<double>(sum) / static_cast<double>(winLen);
default:
return std::numeric_limits<double>::quiet_NaN();
}
}
double CBigWigReader::getPercentCoveredFromBam(const std::string& chrom, int start, int end) const {
double covBases = getCoverageFromBam(chrom, start, end, bwStatsType::cov);
if (!(covBases == covBases)) return std::numeric_limits<double>::quiet_NaN();
double len = static_cast<double>(end - start);
if (len <= 0) return std::numeric_limits<double>::quiet_NaN();
return 100.0 * covBases / len;
}
double CBigWigReader::getMinCoverage(const std::string& chrom, int start, int end)const
{
return getCoverage(chrom, start, end, bwStatsType::min);
}
double CBigWigReader::getAverageCoverage(const std::string& chrom, int start, int end)const
{
return getCoverage(chrom, start, end, bwStatsType::average);
}
double CBigWigReader::getMaxCoverage(const std::string& chrom, int start, int end)const
{
return getCoverage(chrom, start, end, bwStatsType::max);
}
double CBigWigReader::getSumCoverage(const std::string& chrom, int start, int end)const
{
return getCoverage(chrom, start, end, bwStatsType::sum);
}