-
Notifications
You must be signed in to change notification settings - Fork 1
/
bismark.c
207 lines (186 loc) · 8.73 KB
/
bismark.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
#include "generic.h"
int bismark_usage(){
fprintf(stderr, "\nAnalyzing SAM/BAM generated by Bismark.\nInput multilple bam files need be separated by comma, support at most 100 bam/sam files.\n");
fprintf(stderr, "Default output stats over each CpG, using -b to generate bismark like output, which based on each C (in CpG)\n");
fprintf(stderr, "This tool was way faster than bismark_methylation_extractor, but have limit functions.\nCurrently only works with bismark output with bowtie 1.\nUsers are recommended to use the original bismark_methylation_extractor tool for a comprehensive result.\n\n");
fprintf(stderr, "Usage: methylQA bismark [options] <chromosome size file> <CpG bed file> <bam/sam alignment file1,file2,file3...>\n\n");
fprintf(stderr, "Options: -S input is SAM [off]\n");
fprintf(stderr, " -C Add 'chr' string as prefix of reference sequence [off]\n");
fprintf(stderr, " -s only output report stats [off]\n");
fprintf(stderr, " -b bismark-like output [off]\n");
fprintf(stderr, " -F full output, will generate 9 files [off], if speficied, will set -s off, -b on\n");
fprintf(stderr, " -B keep the density bed files [off]\n");
fprintf(stderr, " -c read coverage threshold [5]\n");
fprintf(stderr, " -I Insert length threshold [500]\n");
fprintf(stderr, " -o output prefix [basename of first input filename without extension]\n");
fprintf(stderr, " -h help message\n");
fprintf(stderr, " -? help message\n");
fprintf(stderr, "\n");
return 1;
}
/* main function */
int main_bismark (int argc, char *argv[]) {
char *output, *outReportfile, *outCpGfile, *outbedGraphfile, *row[100], *samfilecopy;
char *forwardcg, *forwardchg, *forwardchh, *forwardread, *forwardread1;
char *reversecg, *reversechg, *reversechh, *reverseread, *reverseread1;
unsigned long long int *cnt;
unsigned long long int *cnt2 = NULL;
int optSam = 0, c, optaddChr = 0, optStats = 0, optBis = 0, optFull = 0, optKeep = 0;
unsigned int optisize = 500;
int optcov = 5;
char *optoutput = NULL;
struct hash *cpgHash = newHash(0);
struct hash *chgHash = newHash(0);
struct hash *chhHash = newHash(0);
time_t start_time, end_time;
start_time = time(NULL);
while ((c = getopt(argc, argv, "SCsbFBo:c:I:h?")) >= 0) {
switch (c) {
case 'S': optSam = 1; break;
case 'C': optaddChr = 1; break;
case 's': optStats = 1; break;
case 'b': optBis = 1; break;
case 'F': optFull = 1; break;
case 'B': optKeep = 1; break;
case 'c': optcov = (int)strtol(optarg, 0, 0); break;
case 'I': optisize = (unsigned int)strtol(optarg, 0, 0); break;
case 'o': optoutput = strdup(optarg); break;
case 'h':
case '?': return bismark_usage(); break;
default: return 1;
}
}
if (optind + 3 > argc)
return bismark_usage();
char *chr_size_file = argv[optind];
char *cpg_bed_file = argv[optind+1];
char *sam_file = argv[optind+2];
fprintf(stderr, "* CpG file provided: %s\n", cpg_bed_file);
fprintf(stderr, "* Insert size cutoff: %u\n", optisize);
fprintf(stderr, "* Read coverage threshold: %i\n", optcov);
struct hash *chrHash = hashNameIntFile(chr_size_file);
samfilecopy = cloneString(sam_file);
int numFields = chopByChar(samfilecopy, ',', row, ArraySize(row));
fprintf(stderr, "* Provided %i BAM/SAM file(s)\n", numFields);
if(optFull) {
fprintf(stderr, "* Warning: will run in Full mode, 8 track files and 1 report file will be generated\n");
fprintf(stderr, "* Warning: will output stats over each C (in CHG)\n");
fprintf(stderr, "* Warning: will output stats over each C (in CHH)\n");
optStats = 0;
optBis = 1;
}
if(optStats) {
fprintf(stderr, "* Warning: will report stats only as -s specified\n");
}
// if use select bismark like output, read cpgHash at each C stats
if(optBis) {
fprintf(stderr, "* Warning: will output stats over each C (in CpG)\n");
cpgHash = cpgBed2BinKeeperHashBismark(chrHash, cpg_bed_file);
}else{
fprintf(stderr, "* Warning: will output stats over each CpG\n");
cpgHash = cpgBed2BinKeeperHash(chrHash, cpg_bed_file);
}
if(optoutput) {
output = optoutput;
} else {
output = cloneString(get_filename_without_ext(basename(row[0])));
}
if(asprintf(&outCpGfile, "%s.CpG.bedGraph", output) < 0)
errAbort("Mem Error.\n");
if(asprintf(&outbedGraphfile, "%s.density.bedGraph", output) < 0)
errAbort("Mem Error.\n");
if (asprintf(&outReportfile, "%s.report", output) < 0)
errAbort("Preparing output wrong");
if (asprintf(&forwardcg, "%s.forward.CG.bedGraph", output) < 0)
errAbort("Preparing output wrong");
if (asprintf(&forwardchg, "%s.forward.CHG.bedGraph", output) < 0)
errAbort("Preparing output wrong");
if (asprintf(&forwardchh, "%s.forward.CHH.bedGraph", output) < 0)
errAbort("Preparing output wrong");
if (asprintf(&forwardread, "%s.forward.Density.bed", output) < 0)
errAbort("Preparing output wrong");
if (asprintf(&forwardread1, "%s.forward.Density.bedGraph", output) < 0)
errAbort("Preparing output wrong");
if (asprintf(&reversecg, "%s.reverse.CG.bedGraph", output) < 0)
errAbort("Preparing output wrong");
if (asprintf(&reversechg, "%s.reverse.CHG.bedGraph", output) < 0)
errAbort("Preparing output wrong");
if (asprintf(&reversechh, "%s.reverse.CHH.bedGraph", output) < 0)
errAbort("Preparing output wrong");
if (asprintf(&reverseread, "%s.reverse.Density.bed", output) < 0)
errAbort("Preparing output wrong");
if (asprintf(&reverseread1, "%s.reverse.Density.bedGraph", output) < 0)
errAbort("Preparing output wrong");
//sam file to bed file
//fprintf(stderr, "* Parsing the SAM/BAM file\n");
cnt = bismarkBamParse(sam_file, chrHash, cpgHash, chgHash, chhHash, forwardread, reverseread, optSam, optaddChr, optFull, optisize);
//write to file
if (optFull){
fprintf(stderr, "* Output CpG methylation calls\n");
writecpgBismarkLite(cpgHash, forwardcg, reversecg, optcov);
fprintf(stderr, "* Output CHG methylation calls\n");
writecpgBismarkLiteHash(chgHash, forwardchg, reversechg, optcov);
fprintf(stderr, "* Output CHH methylation calls\n");
writecpgBismarkLiteHash(chhHash, forwardchh, reversechh, optcov);
fprintf(stderr, "* Sorting methylation calls\n");
sortBedfile(forwardcg);
sortBedfile(reversecg);
sortBedfile(forwardchg);
sortBedfile(reversechg);
sortBedfile(forwardchh);
sortBedfile(reversechh);
fprintf(stderr, "* Sorting density bed\n");
sortBedfile(forwardread);
sortBedfile(reverseread);
fprintf(stderr, "* Generating density bedGraph\n");
bedItemOverlapCount(chrHash, forwardread, forwardread1);
bedItemOverlapCount(chrHash, reverseread, reverseread1);
}else{
cnt2 = writecpgBismark(cpgHash, outbedGraphfile, outCpGfile, optStats, optcov);
//sort output
if(!optStats) {
fprintf(stderr, "* Sorting output density\n");
sortBedfile(outbedGraphfile);
}
//sort output
if(!optStats) {
fprintf(stderr, "* Sorting output CpG methylation call\n");
sortBedfile(outCpGfile);
}
}
//generate bigWig
//fprintf(stderr, "* Generating bigWig\n");
//bigWigFileCreate(outbedGraphfile, chr_size_file, 256, 1024, 0, 1, outbigWigfile);
//bedGraphToBigWig(outbedGraphfile, chr_size_file, outbigWigfile);
//write report file
fprintf(stderr, "* Preparing report file\n");
writeReportBismark(outReportfile, cnt, cnt2, numFields, row, optBis, hashIntSum(chrHash));
if(!optKeep){
fprintf(stderr, "* Deleting (huge) density bed files\n");
unlink(forwardread);
unlink(reverseread);
}
//cleaning
hashFree(&chrHash);
hashFree(&cpgHash);
hashFree(&chgHash);
hashFree(&chhHash);
free(outCpGfile);
free(outbedGraphfile);
//free(outbigWigfile);
free(outReportfile);
free(samfilecopy);
free(forwardcg);
free(forwardchg);
free(forwardchh);
free(forwardread);
free(forwardread1);
free(reversecg);
free(reversechg);
free(reversechh);
free(reverseread);
free(reverseread1);
end_time = time(NULL);
fprintf(stderr, "* Done, time used %.0f seconds.\n", difftime(end_time, start_time));
return 0;
}