-
Notifications
You must be signed in to change notification settings - Fork 44
/
mergeContext.c
236 lines (215 loc) · 6.96 KB
/
mergeContext.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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <getopt.h>
#include <inttypes.h>
#include "htslib/faidx.h"
#include "htslib/kseq.h"
void print_version(void);
int fgets2(FILE*f, unsigned char *s, int l) {
return (fgets((char*) s, l, f))?strlen((char*)s):0;
}
KSTREAM_INIT(FILE*, fgets2, 65536)
struct lastCall{
char *chrom;
int32_t start, end;
uint32_t nmethyl, nunmethyl;
};
static inline void printRecord(FILE *of, char *chr, int32_t start, int32_t end, uint32_t nmethyl, uint32_t nunmethyl) {
fprintf(of, "%s\t%"PRId32"\t%"PRId32"\t%i\t%"PRIu32"\t%"PRIu32"\n", chr, \
start, end, (int) (100.0 * ((double) nmethyl)/(nmethyl+nunmethyl)), \
nmethyl, nunmethyl);
}
void MergeOrPrint(FILE *of, struct lastCall *last, char *chr, int32_t start, int32_t width, uint32_t nmethyl, uint32_t nunmethyl) {
int32_t end;
if(width>0) {
end = start+width;
} else {
end = start+1;
start = end+width;
}
if(last->chrom && strcmp(last->chrom, chr)==0 && last->start==start && last->end==end) {
printRecord(of, chr, start, end, nmethyl+last->nmethyl, nunmethyl+last->nunmethyl);
free(last->chrom);
free(chr);
last->chrom=NULL;
} else {
if(last->chrom) {
printRecord(of, last->chrom, last->start, last->end, last->nmethyl, last->nunmethyl);
free(last->chrom);
}
last->chrom = chr;
last->start = start;
last->end = end;
last->nmethyl = nmethyl;
last->nunmethyl = nunmethyl;
}
}
int getContext(faidx_t *fai, char *chr, int32_t pos, int *width) {
int len = faidx_seq_len(fai, chr);
int rv = 2;
int32_t start = (pos>2) ? pos-2 : 0;
int32_t end = (pos+2<len) ? pos+2 : len-1;
char *seq = faidx_fetch_seq(fai, chr, start, end, &len);
if(!seq) return 3;
char *base = seq+(pos-start);
if(toupper(*base) == 'C') {
if(end-pos) {
if(toupper(*(base+1)) == 'G') {
*width = 2;
rv = 0;
} else if(end-pos==2) {
if(toupper(*(base+2)) == 'G') {
*width = 3;
rv = 1;
}
}
}
} else {
assert(toupper(*base) == 'G');
if(pos-start) {
if(toupper(*(base-1)) == 'C') {
*width = -2;
rv = 0;
} else if(pos-start==2) {
if(toupper(*(base-2)) == 'C') {
*width = -3;
rv = 1;
}
}
}
}
free(seq);
return rv;
}
void mergeContext(FILE *ifile, faidx_t *fai, FILE *ofile) {
struct lastCall *lastCpG = calloc(1, sizeof(struct lastCall));
struct lastCall *lastCHG = calloc(1, sizeof(struct lastCall));
assert(lastCpG);
assert(lastCHG);
char *p, *p2;
int ret, type, width;
char *chr;
int32_t start, end;
uint32_t nmethyl, nunmethyl;
kstring_t str;
str.s = 0; str.l = str.m = 0;
kstream_t *ks = ks_init(ifile);
assert(ks);
while(ks_getuntil(ks, KS_SEP_LINE, &str, &ret) >= 0) {
assert(str.l>0);
if(strncmp(str.s, "track", 5) == 0) continue;
p = strtok(str.s, "\t");
chr = strdup(p);
p = strtok(NULL, "\t");
start = strtoll(p, &p2, 10);
assert(p2!=p);
p = strtok(NULL, "\t");
end = strtoll(p, &p2, 10);
assert(p2!=p);
p = strtok(NULL, "\t");
p = strtok(NULL, "\t");
nmethyl = strtoul(p, &p2, 10);
assert(p2!=p);
p = strtok(NULL, "\n");
nunmethyl = strtoul(p, &p2, 10);
assert(p2!=p);
type = getContext(fai, chr, start, &width);
if(type==0) {
MergeOrPrint(ofile, lastCpG, chr, start, width, nmethyl, nunmethyl);
} else if(type==1) {
MergeOrPrint(ofile, lastCHG, chr, start, width, nmethyl, nunmethyl);
} else if(type==2) {
printRecord(ofile, chr, start, end, nmethyl, nunmethyl);
free(chr);
} else {
fprintf(stderr, "[mergeContext] Error, %s is an unknown chromosome name!\n", chr);
free(chr);
break;
}
}
if(lastCpG->chrom) {
printRecord(ofile, lastCpG->chrom, lastCpG->start, lastCpG->end, lastCpG->nmethyl, lastCpG->nunmethyl);
free(lastCpG->chrom);
}
if(lastCHG->chrom) {
printRecord(ofile, lastCHG->chrom, lastCHG->start, lastCHG->end, lastCHG->nmethyl, lastCHG->nunmethyl);
free(lastCHG->chrom);
}
if(str.m) free(str.s);
ks_destroy(ks);
free(lastCpG);
free(lastCHG);
}
void mergeContext_usage() {
fprintf(stderr, "\nUsage: MethylDackel mergeContext [OPTIONS] <ref.fa> <input>\n");
fprintf(stderr,
"\n"
"This program will merge single Cytosine methylation metrics into per-CpG/CHG\n"
"metrics. The input file must be coordinate sorted but is not required to contain\n"
"only a single sequence context, though not doing so may result in an unsorted\n"
"result.\n"
"\n"
" ref.fa Reference genome in fasta format. This must be indexed with\n"
" samtools faidx\n"
" input An input file such as that produced by MethylDackel extract. Specifying\n"
" - allows reading from a pipe.\n"
"\nOptions:\n"
" -o STR Output file name [stdout]\n"
" --version Print version and quit\n"
);
}
int mergeContext_main(int argc, char *argv[]) {
faidx_t *fai;
FILE *ifile, *ofile = stdout;
char c;
static struct option lopts[] = {
{"help", 0, NULL, 'h'},
{"version", 0, NULL, 'v'},
{0, 0, NULL, 0}
};
while((c = getopt_long(argc, argv, "hvo:", lopts, NULL)) >= 0) {
switch(c) {
case 'h' :
mergeContext_usage();
return 0;
case 'v' :
print_version();
return 0;
case 'o' :
if((ofile = fopen(optarg, "w")) == NULL) {
fprintf(stderr, "Couldn't open %s for writing\n", optarg);
return 2;
}
break;
default :
fprintf(stderr, "Invalid option '%c'\n", c);
mergeContext_usage();
return 1;
}
}
if(argc == 1) {
mergeContext_usage();
return 0;
}
if(argc - optind != 2) {
fprintf(stderr, "You must supply a reference genome in fasta format and an input bedGraph files\n");
mergeContext_usage();
return -1;
}
if((fai = fai_load(argv[optind])) == NULL) {
fprintf(stderr, "Couldn't open the index for %s!\n", argv[optind]);
mergeContext_usage();
return -2;
}
if((ifile = fopen(argv[optind+1], "r")) == NULL) {
fprintf(stderr, "Couldn't open %s for reading!\n", argv[optind+1]);
return -3;
}
fprintf(ofile, "track type=\"bedGraph\" description=\"merged Methylation metrics\"\n");
mergeContext(ifile, fai, ofile);
if(ofile != stdout) fclose(ofile);
fclose(ifile);
fai_destroy(fai);
return 0;
}