Debian Codegraph @

docs/content/tools/coverage.rst

raw
1 .. _coverage:
2
3 ###############
4 *coverage*
5 ###############
6 The ``bedtools coverage`` tool computes both the *depth* and *breadth* of coverage of features in file B on the features
7 in file A. For example, ``bedtools coverage`` can compute the coverage of sequence alignments (file B) across 1
8 kilobase (arbitrary) windows (file A) tiling a genome of interest. One advantage that ``bedtools coverage``
9 offers is that it not only *counts* the number of features that overlap an interval in file A, it also
10 computes the fraction of bases in the interval in A that were overlapped by one or more features. Thus,
11 ``bedtools coverage`` also computes the *breadth* of coverage observed for each interval in A.
12
13
14 .. note::
15
16 If you are trying to compute coverage for very large files and are having trouble
17 with excessive memory usage, please presort your data by chromosome and
18 then by start position (e.g., ``sort -k1,1 -k2,2n in.bed > in.sorted.bed``
19 for BED files) and then use the ``-sorted`` option. This invokes a
20 memory-efficient algorithm designed for large files.
21
22 .. important::
23
24 As of version 2.24.0, the `coverage` tool has changed such that the coverage is
25 computed for the A file, not the B file. This changes the command line interface
26 to be consistent with the other tools. Also, the `coverage` tool
27 can accept multiple files for the `-b` option. This allows one to measure
28 coverage between a single query (`-a`) file and multiple database files (`-b`) at once!
29
30
31 .. seealso::
32
33 :doc:`../tools/intersect`
34 :doc:`../tools/genomecov`
35
36 ===============================
37 Usage and option summary
38 ===============================
39 **Usage**:
40 ::
41
42 bedtools coverage [OPTIONS] -a <FILE> \
43 -b <FILE1, FILE2, ..., FILEN>
44
45 **(or)**:
46 ::
47
48 coverageBed [OPTIONS] -a <FILE> \
49 -b <FILE1, FILE2, ..., FILEN>
50
51
52 =========================== =========================================================================================================================================================
53 Option Description
54 =========================== =========================================================================================================================================================
55 **-a** BAM/BED/GFF/VCF file "A". Each feature in A is compared to B in search of overlaps. Use "stdin" if passing A with a UNIX pipe.
56 **-b** One or more BAM/BED/GFF/VCF file(s) "B". Use "stdin" if passing B with a UNIX pipe.
57 **NEW!!!**: -b may be followed with multiple databases and/or wildcard (*) character(s).
58 **-abam** BAM file A. Each BAM alignment in A is compared to B in search of overlaps. Use "stdin" if passing A with a UNIX pipe: For example: samtools view -b <BAM> | bedtools intersect -abam stdin -b genes.bed. **Note**: no longer necessary after version 2.19.0
59 **-hist** | Report a histogram of coverage for each feature in A as well as a summary histogram for _all_ features in A.
60 | Output (tab delimited) after each feature in A:
61 | 1) depth
62 | 2) # bases at depth
63 | 3) size of A
64 | 4) % of A at depth
65 **-d** Report the depth at each position in each A feature. Positions reported are one based. Each position and depth follow the complete A feature.
66 **-counts** Only report the count of overlaps, don't compute fraction, etc. Restricted by -f and -r.
67 **-f** Minimum overlap required as a fraction of A. Default is 1E-9 (i.e. 1bp).
68 **-F** Minimum overlap required as a fraction of B. Default is 1E-9 (i.e., 1bp).
69 **-r** Require that the fraction of overlap be reciprocal for A and B. In other words, if -f is 0.90 and -r is used, this requires that B overlap at least 90% of A and that A also overlaps at least 90% of B.
70 **-e** Require that the minimum fraction be satisfied for A _OR_ B. In other words, if -e is used with -f 0.90 and -F 0.10 this requires that either 90% of A is covered OR 10% of B is covered. Without -e, both fractions would have to be satisfied.
71 **-s** Force "strandedness". That is, only report hits in B that overlap A on the same strand. By default, overlaps are reported without respect to strand.
72 **-S** Require different strandedness. That is, only report hits in B that overlap A on the _opposite_ strand. By default, overlaps are reported without respect to strand.
73 **-split** Treat "split" BAM (i.e., having an "N" CIGAR operation) or BED12 entries as distinct BED intervals.
74 **-sorted** For very large B files, invoke a "sweeping" algorithm that requires position-sorted (e.g., ``sort -k1,1 -k2,2n`` for BED files) input. When using -sorted, memory usage remains low even for very large files.
75 **-g** Specify a genome file the defines the expected chromosome order in the input files for use with the ``-sorted`` option.
76 **-header** Print the header from the A file prior to results.
77 **-sortout** When using *multiple databases* (`-b`), sort the output DB hits for each record.
78 **-nobuf** Disable buffered output. Using this option will cause each line of output to be printed as it is generated, rather than saved in a buffer. This will make printing large output files noticeably slower, but can be useful in conjunction with other software tools and scripts that need to process one line of bedtools output at a time.
79 **-iobuf** Follow with desired integer size of read buffer. Optional suffixes `K/M/G` supported. **Note**: currently has no effect with compressed files.
80 =========================== =========================================================================================================================================================
81
82
83
84 ==========================================================================
85 Default behavior
86 ==========================================================================
87 After each interval in A, ``bedtools coverage`` will report:
88
89 1) The number of features in B that overlapped (by at least one base pair) the A interval.
90 2) The number of bases in A that had non-zero coverage from features in B.
91 3) The length of the entry in A.
92 4) The fraction of bases in A that had non-zero coverage from features in B.
93
94 Below are the number of features in B (N=...) overlapping A and fraction of bases in A with coverage.
95
96 ::
97
98 Chromosome ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
99
100 BED FILE A *************** *************** ****** **************
101
102 BED File B ^^^^ ^^^^ ^^ ^^^^^^^^^ ^^^ ^^ ^^^^
103 ^^^^^^^^ ^^^^^ ^^^^^ ^^
104
105 Result [ N=3, 10/15 ] [ N=1, 2/15 ] [N=1,6/6] [N=6, 12/14 ]
106
107
108 For example:
109
110 .. code-block:: bash
111
112 $ cat A.bed
113 chr1 0 100
114 chr1 100 200
115 chr2 0 100
116
117 $ cat B.bed
118 chr1 10 20
119 chr1 20 30
120 chr1 30 40
121 chr1 100 200
122
123 $ bedtools coverage -a A.bed -b B.bed
124 chr1 0 100 3 30 100 0.3000000
125 chr1 100 200 1 100 100 1.0000000
126 chr2 0 100 0 0 100 0.0000000
127
128
129
130 ==========================================================================
131 ``-s`` Calculating coverage by strand
132 ==========================================================================
133 Use the "**-s**" option if one wants to only count coverage if features in A are on the same strand as the
134 feature / window in A. This is especially useful for RNA-seq experiments.
135
136 For example (note the difference in coverage with and without **-s**:
137
138 .. code-block:: bash
139
140 $ cat A.bed
141 chr1 0 100 b1 1 +
142 chr1 100 200 b2 1 -
143 chr2 0 100 b3 1 +
144
145 $ cat B.bed
146 chr1 10 20 a1 1 -
147 chr1 20 30 a2 1 -
148 chr1 30 40 a3 1 -
149 chr1 100 200 a4 1 +
150
151 $ bedtools coverage -a A.bed -b B.bed
152 chr1 0 100 b1 1 + 3 30 100 0.3000000
153 chr1 100 200 b2 1 - 1 100 100 1.0000000
154 chr2 0 100 b3 1 + 0 0 100 0.0000000
155
156 $ bedtools coverage -a A.bed -b B.bed -s
157 chr1 0 100 b1 1 + 0 0 100 0.0000000
158 chr1 100 200 b2 1 - 0 0 100 0.0000000
159 chr2 0 100 b3 1 + 0 0 100 0.0000000
160
161
162 ==========================================================================
163 ``-hist`` Creating a histogram of coverage for each feature in the A file
164 ==========================================================================
165 One should use the "**-hist**" option to create, for each interval in A, a histogram of coverage of the
166 features in B across A.
167
168 In this case, each entire feature in A will be reported, followed by the depth of coverage, the number of
169 bases at that depth, the size of the feature, and the fraction covered. After all of the features in A have
170 been reported, a histogram summarizing the coverage among all features in A will be reported.
171
172 .. code-block:: bash
173
174 $ cat A.bed
175 chr1 0 100 b1 1 +
176 chr1 100 200 b2 1 -
177 chr2 0 100 b3 1 +
178
179 $ cat B.bed
180 chr1 10 20 a1 1 -
181 chr1 20 30 a2 1 -
182 chr1 30 40 a3 1 -
183 chr1 100 200 a4 1 +
184
185 $ bedtools coverage -a A.bed -b B.bed -hist
186 chr1 0 100 b1 1 + 0 70 100 0.7000000
187 chr1 0 100 b1 1 + 1 30 100 0.3000000
188 chr1 100 200 b2 1 - 1 100 100 1.0000000
189 chr2 0 100 b3 1 + 0 100 100 1.0000000
190 all 0 170 300 0.5666667
191 all 1 130 300 0.4333333
192
193
194 ===========================================================================
195 ``-d`` Reporting the per-base of coverage for each feature in the A file
196 ===========================================================================
197 One should use the "**-d**" option to create, for each interval in A, a detailed list of coverage at each of the
198 positions across each A interval.
199
200 The output will consist of a line for each one-based position in each A feature, followed by the coverage
201 detected at that position.
202
203 .. code-block:: bash
204
205 $ cat A.bed
206 chr1 0 10
207
208 $ cat B.bed
209 chr1 0 5
210 chr1 3 8
211 chr1 4 8
212 chr1 5 9
213
214 $ bedtools coverage -a A.bed -b B.bed -d
215 chr1 0 10 B 1 1
216 chr1 0 10 B 2 1
217 chr1 0 10 B 3 1
218 chr1 0 10 B 4 2
219 chr1 0 10 B 5 3
220 chr1 0 10 B 6 3
221 chr1 0 10 B 7 3
222 chr1 0 10 B 8 3
223 chr1 0 10 B 9 1
224 chr1 0 10 B 10 0