1 /*****************************************************************************
2 genomeCoverageMain.cpp
3
4 (c) 2009 - Aaron Quinlan
5 Hall Laboratory
6 Department of Biochemistry and Molecular Genetics
7 University of Virginia
8 aaronquinlan@gmail.com
9
10 Licenced under the GNU General Public License 2.0 license.
11 ******************************************************************************/
12 #include "genomeCoverageBed.h"
13 #include "version.h"
14
15 using namespace std;
16
17 // define our program name
18 #define PROGRAM_NAME "bedtools genomecov"
19
20
21 // define our parameter checking macro
22 #define PARAMETER_CHECK(param, paramLen, actualLen) (strncmp(argv[i], param, min(actualLen, paramLen))== 0) && (actualLen == paramLen)
23
24 // function declarations
25 void genomecoverage_help(void);
26
genomecoverage_main(int argc,char * argv[])27 int genomecoverage_main(int argc, char* argv[]) {
28
29 // our configuration variables
30 bool showHelp = false;
31
32 // input files
33 string bedFile;
34 string genomeFile;
35 int max = INT_MAX;
36 float scale = 1.0;
37 float fragmentSize = 146; //Nucleosome :)
38
39 bool haveBed = false;
40 bool bamInput = false;
41 bool haveGenome = false;
42 bool startSites = false;
43 bool bedGraph = false;
44 bool bedGraphAll = false;
45 bool eachBase = false;
46 bool eachBaseZeroBased = false;
47 bool obeySplits = false;
48 bool ignoreD = false;
49 bool haveScale = false;
50 bool filterByStrand = false;
51 bool pair_chip = false;
52 bool haveSize = false;
53 bool dUTP = false;
54 bool only_5p_end = false;
55 bool only_3p_end = false;
56 bool add_gb_track_line = false;
57 string gb_track_opts;
58 string requestedStrand = "X";
59
60 // check to see if we should print out some help
61 if(argc <= 1) showHelp = true;
62
63 for(int i = 1; i < argc; i++) {
64 int parameterLength = (int)strlen(argv[i]);
65
66 if((PARAMETER_CHECK("-h", 2, parameterLength)) ||
67 (PARAMETER_CHECK("--help", 5, parameterLength))) {
68 showHelp = true;
69 }
70 }
71
72 if(showHelp) genomecoverage_help();
73
74 // do some parsing (all of these parameters require 2 strings)
75 for(int i = 1; i < argc; i++) {
76
77 int parameterLength = (int)strlen(argv[i]);
78
79 if(PARAMETER_CHECK("-i", 2, parameterLength)) {
80 if ((i+1) < argc) {
81 haveBed = true;
82 bedFile = argv[i + 1];
83 i++;
84 }
85 }
86 else if(PARAMETER_CHECK("-ibam", 5, parameterLength)) {
87 if ((i+1) < argc) {
88 haveBed = true;
89 bamInput = true;
90 bedFile = argv[i + 1];
91 i++;
92 }
93 }
94 else if(PARAMETER_CHECK("-g", 2, parameterLength)) {
95 if ((i+1) < argc) {
96 haveGenome = true;
97 genomeFile = argv[i + 1];
98 i++;
99 }
100 }
101 else if(PARAMETER_CHECK("-d", 2, parameterLength)) {
102 eachBase = true;
103 }
104 else if(PARAMETER_CHECK("-dz", 3, parameterLength)) {
105 eachBase = true;
106 eachBaseZeroBased = true;
107 }
108 else if(PARAMETER_CHECK("-bg", 3, parameterLength)) {
109 bedGraph = true;
110 }
111 else if(PARAMETER_CHECK("-bga", 4, parameterLength)) {
112 bedGraphAll = true;
113 }
114 else if(PARAMETER_CHECK("-max", 4, parameterLength)) {
115 if ((i+1) < argc) {
116 max = atoi(argv[i + 1]);
117 i++;
118 }
119 }
120 else if(PARAMETER_CHECK("-scale", 6, parameterLength)) {
121 if ((i+1) < argc) {
122 haveScale = true;
123 scale = atof(argv[i + 1]);
124 i++;
125 }
126 }
127 else if(PARAMETER_CHECK("-split", 6, parameterLength)) {
128 obeySplits = true;
129 }
130 else if(PARAMETER_CHECK("-ignoreD", 8, parameterLength)) {
131 ignoreD = true;
132 }
133 else if(PARAMETER_CHECK("-strand", 7, parameterLength)) {
134 if ((i+1) < argc) {
135 filterByStrand = true;
136 requestedStrand = argv[i+1][0];
137 if (!(requestedStrand == "-" || requestedStrand == "+")) {
138 cerr << "*****ERROR: invalid -strand value (" << requestedStrand << "). Allowed options are + or -" << endl;
139 showHelp = true;
140 }
141 i++;
142 }
143 else {
144 cerr << "*****ERROR: -strand options requires a value: + or -" << endl;
145 showHelp = true;
146 }
147 }
148 else if(PARAMETER_CHECK("-3", 2, parameterLength)) {
149 only_3p_end = true;
150 }
151 else if(PARAMETER_CHECK("-5", 2, parameterLength)) {
152 only_5p_end = true;
153 }
154 else if(PARAMETER_CHECK("-pc", 3, parameterLength)) {
155 pair_chip = true;
156 }
157 else if(PARAMETER_CHECK("-fs", 3, parameterLength)) {
158 if ((i+1) < argc) {
159 haveSize = true;
160 fragmentSize = atoi(argv[i + 1]);
161 i++;
162 }
163 }
164 else if(PARAMETER_CHECK("-du", 3, parameterLength)) {
165 dUTP = true;
166 }
167 else if(PARAMETER_CHECK("-trackline", 10, parameterLength)) {
168 add_gb_track_line = true;
169 }
170 else if(PARAMETER_CHECK("-trackopts", 10, parameterLength)) {
171 if ((i+1) < argc) {
172 add_gb_track_line = true;
173 gb_track_opts = argv[i+1];
174 i++;
175 } else {
176 cerr << "*****ERROR: -trackopts options requires a value (UCSC/GB track definition parameters)" << endl;
177 showHelp = true;
178 }
179 }
180 else {
181 cerr << endl << "*****ERROR: Unrecognized parameter: " << argv[i] << " *****" << endl << endl;
182 showHelp = true;
183 }
184 }
185
186 // make sure we have both input files
187 if (!haveBed && !haveGenome && !bamInput) {
188 cerr << endl << "*****" << endl << "*****ERROR: Need both a BED (-i) and a genome (-g) file. " << endl << "*****" << endl;
189 showHelp = true;
190 }
191 // make sure we have both input files
192 if (bamInput && haveGenome) {
193 cerr << endl << "*****" << endl << "*****WARNING: Genome (-g) files are ignored when BAM input is provided. " << endl << "*****" << endl;
194 }
195 if (bedGraph && eachBase) {
196 cerr << endl << "*****" << endl << "*****ERROR: Use -d/-dz or -bg, not both" << endl << "*****" << endl;
197 showHelp = true;
198 }
199 if (bedGraphAll && eachBase) {
200 cerr << endl << "*****" << endl << "*****ERROR: Use -d/-dz or -bga, not both" << endl << "*****" << endl;
201 showHelp = true;
202 }
203
204 if (only_3p_end && only_5p_end) {
205 cerr << endl << "*****" << endl << "*****ERROR: Use -3 or -5, not both " << endl << "*****" << endl;
206 showHelp = true;
207 }
208
209 if ( (only_3p_end||only_5p_end) && obeySplits) {
210 cerr << endl << "*****" << endl << "*****ERROR: Use -split can't be used with -3 or -5." << endl << "*****" << endl;
211 showHelp = true;
212 }
213
214 if (add_gb_track_line && !(bedGraph||bedGraphAll)) {
215 cerr << endl << "*****" << endl << "*****ERROR: Using -trackline requires bedGraph output (use -bg or -bga)." << endl << "*****" << endl;
216 showHelp = true;
217 }
218
219 if (haveScale && !(bedGraph||bedGraphAll||eachBase)) {
220 cerr << endl << "*****" << endl << "*****ERROR: Using -scale requires bedGraph output (use -bg or -bga) or per base depth (-d)." << endl << "*****" << endl;
221 showHelp = true;
222 }
223
224 if (!showHelp) {
225 BedGenomeCoverage *bc = new BedGenomeCoverage(bedFile, genomeFile, eachBase,
226 startSites, bedGraph, bedGraphAll,
227 max, scale, bamInput, obeySplits, ignoreD,
228 filterByStrand, requestedStrand,
229 only_5p_end, only_3p_end,
230 pair_chip, haveSize, fragmentSize, dUTP,
231 eachBaseZeroBased,
232 add_gb_track_line, gb_track_opts);
233 delete bc;
234 }
235 else {
236 genomecoverage_help();
237 }
238 return 0;
239 }
240
genomecoverage_help(void)241 void genomecoverage_help(void) {
242
243 cerr << "\nTool: bedtools genomecov (aka genomeCoverageBed)" << endl;
244 cerr << "Version: " << VERSION << "\n";
245 cerr << "Summary: Compute the coverage of a feature file among a genome." << endl << endl;
246
247 cerr << "Usage: " << PROGRAM_NAME << " [OPTIONS] -i <bed/gff/vcf> -g <genome>" << endl << endl;
248
249 cerr << "Options: " << endl;
250
251 cerr << "\t-ibam\t\t" << "The input file is in BAM format." << endl;
252 cerr << "\t\t\tNote: BAM _must_ be sorted by position" << endl << endl;
253
254 cerr << "\t-d\t\t" << "Report the depth at each genome position (with one-based coordinates)." << endl;
255 cerr << "\t\t\tDefault behavior is to report a histogram." << endl << endl;
256
257 cerr << "\t-dz\t\t" << "Report the depth at each genome position (with zero-based coordinates)." << endl;
258 cerr << "\t\t\tReports only non-zero positions." << endl;
259 cerr << "\t\t\tDefault behavior is to report a histogram." << endl << endl;
260
261 cerr << "\t-bg\t\t" << "Report depth in BedGraph format. For details, see:" << endl;
262 cerr << "\t\t\tgenome.ucsc.edu/goldenPath/help/bedgraph.html" << endl << endl;
263
264 cerr << "\t-bga\t\t" << "Report depth in BedGraph format, as above (-bg)." << endl;
265 cerr << "\t\t\tHowever with this option, regions with zero " << endl;
266 cerr << "\t\t\tcoverage are also reported. This allows one to" << endl;
267 cerr << "\t\t\tquickly extract all regions of a genome with 0 " << endl;
268 cerr << "\t\t\tcoverage by applying: \"grep -w 0$\" to the output." << endl << endl;
269
270 cerr << "\t-split\t\t" << "Treat \"split\" BAM or BED12 entries as distinct BED intervals." << endl;
271 cerr << "\t\t\twhen computing coverage." << endl;
272 cerr << "\t\t\tFor BAM files, this uses the CIGAR \"N\" and \"D\" operations " << endl;
273 cerr << "\t\t\tto infer the blocks for computing coverage." << endl;
274 cerr << "\t\t\tFor BED12 files, this uses the BlockCount, BlockStarts, and BlockEnds" << endl;
275 cerr << "\t\t\tfields (i.e., columns 10,11,12)." << endl << endl;
276
277 cerr << "\t-ignoreD\t" << "Ignore local deletions (CIGAR \"D\" operations) in BAM entries" << endl;
278 cerr << "\t\t\twhen computing coverage." << endl << endl;
279
280 cerr << "\t-strand\t\t" << "Calculate coverage of intervals from a specific strand." << endl;
281 cerr << "\t\t\tWith BED files, requires at least 6 columns (strand is column 6). " << endl;
282 cerr << "\t\t\t- (STRING): can be + or -" << endl << endl;
283
284 cerr << "\t-pc\t\t" << "Calculate coverage of pair-end fragments." << endl;
285 cerr << "\t\t\tWorks for BAM files only" << endl;
286
287 cerr << "\t-fs\t\t" << "Force to use provided fragment size instead of read length" << endl;
288 cerr << "\t\t\tWorks for BAM files only" << endl;
289
290 cerr << "\t-du\t\t" << "Change strand af the mate read (so both reads from the same strand) useful for strand specific" << endl;
291 cerr << "\t\t\tWorks for BAM files only" << endl;
292
293 cerr << "\t-5\t\t" << "Calculate coverage of 5\" positions (instead of entire interval)." << endl << endl;
294
295 cerr << "\t-3\t\t" << "Calculate coverage of 3\" positions (instead of entire interval)." << endl << endl;
296
297 cerr << "\t-max\t\t" << "Combine all positions with a depth >= max into" << endl;
298 cerr << "\t\t\ta single bin in the histogram. Irrelevant" << endl;
299 cerr << "\t\t\tfor -d and -bedGraph" << endl;
300 cerr << "\t\t\t- (INTEGER)" << endl << endl;
301
302 cerr << "\t-scale\t\t" << "Scale the coverage by a constant factor." << endl;
303 cerr << "\t\t\tEach coverage value is multiplied by this factor before being reported." << endl;
304 cerr << "\t\t\tUseful for normalizing coverage by, e.g., reads per million (RPM)." << endl;
305 cerr << "\t\t\t- Default is 1.0; i.e., unscaled." << endl;
306 cerr << "\t\t\t- (FLOAT)" << endl << endl;
307
308 cerr << "\t-trackline\t" << "Adds a UCSC/Genome-Browser track line definition in the first line of the output." << endl;
309 cerr <<"\t\t\t- See here for more details about track line definition:" << endl;
310 cerr <<"\t\t\t http://genome.ucsc.edu/goldenPath/help/bedgraph.html" << endl;
311 cerr <<"\t\t\t- NOTE: When adding a trackline definition, the output BedGraph can be easily" << endl;
312 cerr <<"\t\t\t uploaded to the Genome Browser as a custom track," << endl;
313 cerr <<"\t\t\t BUT CAN NOT be converted into a BigWig file (w/o removing the first line)." << endl << endl;
314
315 cerr << "\t-trackopts\t"<<"Writes additional track line definition parameters in the first line." << endl;
316 cerr <<"\t\t\t- Example:" << endl;
317 cerr <<"\t\t\t -trackopts 'name=\"My Track\" visibility=2 color=255,30,30'" << endl;
318 cerr <<"\t\t\t Note the use of single-quotes if you have spaces in your parameters." << endl;
319 cerr <<"\t\t\t- (TEXT)" << endl << endl;
320
321 cerr << "Notes: " << endl;
322 cerr << "\t(1) The genome file should tab delimited and structured as follows:" << endl;
323 cerr << "\t <chromName><TAB><chromSize>" << endl << endl;
324 cerr << "\tFor example, Human (hg19):" << endl;
325 cerr << "\tchr1\t249250621" << endl;
326 cerr << "\tchr2\t243199373" << endl;
327 cerr << "\t..." << endl;
328 cerr << "\tchr18_gl000207_random\t4262" << endl << endl;
329
330 cerr << "\t(2) The input BED (-i) file must be grouped by chromosome." << endl;
331 cerr << "\t A simple \"sort -k 1,1 <BED> > <BED>.sorted\" will suffice."<< endl << endl;
332
333 cerr << "\t(3) The input BAM (-ibam) file must be sorted by position." << endl;
334 cerr << "\t A \"samtools sort <BAM>\" should suffice."<< endl << endl;
335
336 cerr << "Tips: " << endl;
337 cerr << "\tOne can use the UCSC Genome Browser's MySQL database to extract" << endl;
338 cerr << "\tchromosome sizes. For example, H. sapiens:" << endl << endl;
339 cerr << "\tmysql --user=genome --host=genome-mysql.cse.ucsc.edu -A -e \\" << endl;
340 cerr << "\t\"select chrom, size from hg19.chromInfo\" > hg19.genome" << endl << endl;
341
342
343 // end the program here
344 exit(1);
345 }
346
347