forked from dpryan79/MethylDackel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MBias.c
349 lines (323 loc) · 12.2 KB
/
MBias.c
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
#include "htslib/sam.h"
#include "htslib/hts.h"
#include "htslib/faidx.h"
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <assert.h>
#include "PileOMeth.h"
strandMeth *growStrandMeth(strandMeth *s, int32_t l) {
int32_t m;
int i;
l++;
m = kroundup32(l);
if(m<32) m=32; //Enforce a minimum length
s->unmeth1 = realloc(s->unmeth1, sizeof(uint32_t)*m);
s->meth1 = realloc(s->meth1, sizeof(uint32_t)*m);
s->unmeth2 = realloc(s->unmeth2, sizeof(uint32_t)*m);
s->meth2 = realloc(s->meth2, sizeof(uint32_t)*m);
assert(s->unmeth1);
assert(s->unmeth2);
assert(s->meth1);
assert(s->meth2);
for(i=s->m; i<m; i++) {
s->unmeth1[i] = 0;
s->meth1[i] = 0;
s->unmeth2[i] = 0;
s->meth2[i] = 0;
}
s->m = m;
return s;
}
void extractMBias(Config *config, char *opref, int SVG, int txt) {
bam_hdr_t *hdr = sam_hdr_read(config->fp);
bam_mplp_t iter;
int ret, tid, pos, i, seqlen, rv, o = 0;
int beg0 = 0, end0 = 1u<<29;
int strand;
int n_plp; //This will need to be modified for multiple input files
int ctid = -1; //The tid of the contig whose sequence is stored in "seq"
int idxBED = 0;
const bam_pileup1_t **plp = NULL;
char *seq = NULL, base;
mplp_data *data = NULL;
strandMeth *meths[4];
for(i=0; i<4; i++) {
meths[i] = calloc(1, sizeof(strandMeth));
assert(meths[i]);
}
data = calloc(1,sizeof(mplp_data));
if(data == NULL) {
fprintf(stderr, "Couldn't allocate space for the data structure in extractCalls()!\n");
return;
}
data->config = config;
data->hdr = hdr;
if (config->reg) {
if((data->iter = sam_itr_querys(config->bai, hdr, config->reg)) == 0){
fprintf(stderr, "failed to parse regions %s", config->reg);
return;
}
}
if(config->bedName) {
config->bed = parseBED(config->bedName, hdr);
if(config->bed == NULL) return;
}
plp = calloc(1, sizeof(bam_pileup1_t *)); //This will have to be modified for multiple input files
if(plp == NULL) {
fprintf(stderr, "Couldn't allocate space for the plp structure in extractCalls()!\n");
return;
}
//Start the pileup
iter = bam_mplp_init(1, filter_func, (void **) &data);
bam_mplp_set_maxcnt(iter, config->maxDepth);
while((ret = bam_mplp_auto(iter, &tid, &pos, &n_plp, plp)) > 0) {
//Do we need to process this position?
if (config->reg){
beg0 = data->iter->beg, end0 = data->iter->end;
if ((pos < beg0 || pos >= end0)) continue; // out of the region requested
}
if(tid != ctid) {
if(seq != NULL) free(seq);
seq = faidx_fetch_seq(config->fai, hdr->target_name[tid], 0, faidx_seq_len(config->fai, hdr->target_name[tid]), &seqlen);
if(seqlen < 0) {
fprintf(stderr, "faidx_fetch_seq returned %i while trying to fetch the sequence for tid %i (%s)!\n",\
seqlen, tid, hdr->target_name[tid]);
fprintf(stderr, "Note that the output will be truncated!\n");
return;
}
ctid = tid;
}
if(config->bed) { //Handle -l
while((o = posOverlapsBED(tid, pos, config->bed, idxBED)) == -1) idxBED++;
if(o == 0) continue; //Wrong strand
}
if(isCpG(seq, pos, seqlen)) {
if(!config->keepCpG) continue;
} else if(isCHG(seq, pos, seqlen)) {
if(!config->keepCHG) continue;
} else if(isCHH(seq, pos, seqlen)) {
if(!config->keepCHH) continue;
} else {
continue;
}
base = *(seq+pos);
for(i=0; i<n_plp; i++) {
if(config->bed) if(!readStrandOverlapsBED(plp[0][i].b, config->bed->region[idxBED])) continue;
strand = getStrand((plp[0]+i)->b);
if(strand & 1) {
if(base != 'C' && base != 'c') continue;
} else {
if(base != 'G' && base != 'g') continue;
}
rv = updateMetrics(config, plp[0]+i);
if(rv != 0) {
if((plp[0]+i)->qpos >= meths[strand-1]->m)
meths[strand-1] = growStrandMeth(meths[strand-1], (plp[0]+i)->qpos);
if(rv < 0) {
if((plp[0]+i)->b->core.flag & BAM_FREAD2) {
assert((meths[strand-1]->unmeth2[(plp[0]+i)->qpos]) < 0xFFFFFFFF);
meths[strand-1]->unmeth2[(plp[0]+i)->qpos]++;
} else {
assert((meths[strand-1]->unmeth1[(plp[0]+i)->qpos]) < 0xFFFFFFFF);
meths[strand-1]->unmeth1[(plp[0]+i)->qpos]++;
}
} else {
if((plp[0]+i)->b->core.flag & BAM_FREAD2) {
assert((meths[strand-1]->meth2[(plp[0]+i)->qpos]) < 0xFFFFFFFF);
meths[strand-1]->meth2[(plp[0]+i)->qpos]++;
} else {
assert((meths[strand-1]->meth1[(plp[0]+i)->qpos]) < 0xFFFFFFFF);
meths[strand-1]->meth1[(plp[0]+i)->qpos]++;
}
}
if((plp[0]+i)->qpos+1 > meths[strand-1]->l) meths[strand-1]->l = (plp[0]+i)->qpos+1;
}
}
}
//Report some output
if(SVG) makeSVGs(opref, meths, config->keepCpG + 2*config->keepCHG + 4*config->keepCHH);
if(txt) makeTXT(meths);
//Clean up
bam_hdr_destroy(hdr);
if(data->iter) hts_itr_destroy(data->iter);
bam_mplp_destroy(iter);
free(data);
free(plp);
if(seq != NULL) free(seq);
for(i=0; i<4; i++) {
if(meths[i]->meth1) free(meths[i]->meth1);
if(meths[i]->unmeth1) free(meths[i]->unmeth1);
if(meths[i]->meth2) free(meths[i]->meth2);
if(meths[i]->unmeth2) free(meths[i]->unmeth2);
free(meths[i]);
}
}
void mbias_usage() {
fprintf(stderr, "\nUsage: PileOMeth mbias [OPTIONS] <ref.fa> <sorted_alignments.bam> <output.prefix>\n");
fprintf(stderr,
"\n"
"Options:\n"
" -q INT Minimum MAPQ threshold to include an alignment (default 10)\n"
" -p INT Minimum Phred threshold to include a base (default 5). This\n"
" must be >0.\n"
" -D INT Maximum per-base depth (default 2000)\n"
" -r STR Region string in which to extract methylation\n"
" -l FILE A BED file listing regions for inclusion. Note that unlike\n"
" samtools mpileup, this option will utilize the strand column\n"
" (column 6) if present. Thus, if a region has a '+' in this\n"
" column, then only metrics from the top strand will be\n"
" output. Note that the -r option can be used to limit the\n"
" regions of -l.\n"
" --keepDupes By default, any alignment marked as a duplicate is ignored.\n"
" This option causes them to be incorporated.\n"
" --keepSingleton By default, if only one read in a pair aligns (a singleton)\n"
" then it's ignored.\n"
" --keepDiscordant By default, paired-end alignments with the properly-paired bit\n"
" unset in the FLAG field are ignored. Note that the definition\n"
" of concordant and discordant is based on your aligner\n"
" settings.\n"
" --txt Output tab separated metrics to the screen. These can be\n"
" imported into R or another program for manual plotting and\n"
" analysis.\n"
" --noSVG Don't produce the SVG files. This option implies --txt. Note\n"
" that an output prefix is no longer required with this option.\n"
" --noCpG Do not output CpG methylation metrics\n"
" --CHG Output CHG methylation metrics\n"
" --CHH Output CHH methylation metrics\n");
}
int mbias_main(int argc, char *argv[]) {
char *opref = NULL;
int c, i, SVG = 1, txt = 0;
Config config;
//Defaults
config.keepCpG = 1; config.keepCHG = 0; config.keepCHH = 0;
config.minMapq = 10; config.minPhred = 5; config.keepDupes = 0;
config.keepSingleton = 0, config.keepDiscordant = 0;
config.fai = NULL;
config.fp = NULL;
config.bai = NULL;
config.reg = NULL;
config.bedName = NULL;
config.bed = NULL;
for(i=0; i<16; i++) config.bounds[i] = 0;
static struct option lopts[] = {
{"noCpG", 0, NULL, 1},
{"CHG", 0, NULL, 2},
{"CHH", 0, NULL, 3},
{"keepDupes", 0, NULL, 4},
{"keepSingleton", 0, NULL, 5},
{"keepDiscordant", 0, NULL, 6},
{"txt", 0, NULL, 7},
{"noSVG", 0, NULL, 8},
{"help", 0, NULL, 'h'},
{0, 0, NULL, 0}
};
while((c = getopt_long(argc, argv, "q:p:r:l:D:", lopts,NULL)) >= 0) {
switch(c) {
case 'h' :
mbias_usage();
return 0;
case 'D' :
config.maxDepth = atoi(optarg);
break;
case 'r':
config.reg = strdup(optarg);
break;
case 'l' :
config.bedName = optarg;
break;
case 1 :
config.keepCpG = 0;
break;
case 2 :
config.keepCHG = 1;
break;
case 3 :
config.keepCHH = 1;
break;
case 4 :
config.keepDupes = 1;
break;
case 5 :
config.keepSingleton = 1;
break;
case 6 :
config.keepDiscordant = 1;
break;
case 7 :
txt = 1;
break;
case 8 :
SVG = 0;
txt = 1;
break;
case 'q' :
config.minMapq = atoi(optarg);
break;
case 'p' :
config.minPhred = atoi(optarg);
break;
default :
fprintf(stderr, "Invalid option '%c'\n", c);
mbias_usage();
return 1;
}
}
if(argc == 1) {
mbias_usage();
return 0;
}
if((SVG && argc-optind != 3) || (!SVG && argc-optind < 2)) {
fprintf(stderr, "You must supply a reference genome in fasta format, an input BAM file, and an output prefix!!!\n");
mbias_usage();
return -1;
}
//Are the options reasonable?
if(config.minPhred < 1) {
fprintf(stderr, "-p %i is invalid. resetting to 1, which is the lowest possible value.\n", config.minPhred);
config.minPhred = 1;
}
if(config.minMapq < 0) {
fprintf(stderr, "-q %i is invalid. Resetting to 0, which is the lowest possible value.\n", config.minMapq);
config.minMapq = 0;
}
//Is there still a metric to output?
if(!(config.keepCpG + config.keepCHG + config.keepCHH)) {
fprintf(stderr, "You haven't specified any metrics to output!\nEither don't use the --noCpG option or specify --CHG and/or --CHH.\n");
return -1;
}
//Open the files
if((config.fai = fai_load(argv[optind])) == NULL) {
fprintf(stderr, "Couldn't open the index for %s!\n", argv[optind]);
mbias_usage();
return -2;
}
if((config.fp = hts_open(argv[optind+1], "rb")) == NULL) {
fprintf(stderr, "Couldn't open %s for reading!\n", argv[optind+1]);
return -4;
}
if((config.bai = sam_index_load(config.fp, argv[optind+1])) == NULL) {
fprintf(stderr, "Couldn't load the index for %s, will attempt to build it.\n", argv[optind+1]);
if(bam_index_build(argv[optind+1], 0) < 0) {
fprintf(stderr, "Couldn't build the index for %s! File corrupted?\n", argv[optind+1]);
return -5;
}
if((config.bai = sam_index_load(config.fp, argv[optind+1])) == NULL) {
fprintf(stderr, "Still couldn't load the index, quiting.\n");
return -5;
}
}
//Output files (this needs to be filled in)
if(SVG) opref = argv[optind+2];
//Run the pileup
extractMBias(&config, opref, SVG, txt);
//Close things up
hts_close(config.fp);
fai_destroy(config.fai);
hts_idx_destroy(config.bai);
if(config.reg) free(config.reg);
return 0;
}