Debian Codegraph @

docs/content/overview.rst

raw
1 ############
2 Overview
3 ############
4
5 ==========
6 Background
7 ==========
8
9 The development of bedtools was motivated by a need for fast, flexible tools with which to compare large sets of genomic
10 features. Answering fundamental research questions with existing tools was either too slow or required modifications to the
11 way they reported or computed their results. We were aware of the utilities on the UCSC Genome Browser and Galaxy websites, as
12 well as the elegant tools available as part of Jim Kent’s monolithic suite of tools (“Kent source”). However, we found that
13 the web-based tools were too cumbersome when working with large datasets generated by current sequencing technologies.
14 Similarly, we found that the Kent source command line tools often required a local installation of the UCSC Genome Browser.
15 These limitations, combined with the fact that we often wanted an extra option here or there that wasn’t available with
16 existing tools, led us to develop our own from scratch. The initial version of bedtools was publicly released in the spring of
17 2009. The current version has evolved from our research experiences and those of the scientists using the suite over the last
18 year. The bedtools suite enables one to answer common questions of genomic data in a fast and reliable manner. The fact that
19 almost all the utilities accept input from “stdin” allows one to “stream / pipe” several commands together to facilitate more
20 complicated analyses. Also, the tools allow fine control over how output is reported. The initial version of bedtools
21 supported solely 6-column `BED <http://genome.ucsc.edu/FAQ/FAQformat#format1>`_ files. *However, we have subsequently added support for sequence alignments in* `BAM <http://samtools.sourceforge.net/>`_
22 *format, as well as for features in* `GFF <http://genome.ucsc.edu/FAQ/FAQformat#format3>`_ , *“blocked” BED format, and*
23 `VCF <http://www.1000genomes.org/wiki/Analysis/Variant%20Call%20Format/vcf-variant-call-format-version-41>`_ *format*.
24 The tools are quite fast and typically finish in a matter of a few seconds, even for large datasets. This manual seeks to describe the behavior and
25 available functionality for each bedtool. Usage examples are scattered throughout the text, and formal examples are
26 provided in the last two sections, we hope that this document will give you a sense of the flexibility of
27 the toolkit and the types of analyses that are possible with bedtools. If you have further questions, please join the bedtools
28 discussion group, visit the Usage Examples on the Google Code site (usage, advanced usage), or take a look at the nascent
29 “Usage From the Wild” page.
30
31 ===========================
32 Summary of available tools.
33 ===========================
34
35 bedtools support a wide range of operations for interrogating and manipulating genomic features. The table below summarizes
36 the tools available in the suite.
37
38 =============================================== ========================================================================
39 Utility Description
40 =============================================== ========================================================================
41 :doc:`tools/annotate` Annotate coverage of features from multiple files.
42 :doc:`tools/bamtobed` Convert BAM alignments to BED (& other) formats.
43 :doc:`tools/bamtofastq` Convert BAM records to FASTQ records.
44 :doc:`tools/bed12tobed6` Breaks BED12 intervals into discrete BED6 intervals.
45 :doc:`tools/bedpetobam` Convert BEDPE intervals to BAM records.
46 :doc:`tools/bedtobam` Convert intervals to BAM records.
47 :doc:`tools/closest` Find the closest, potentially non-overlapping interval.
48 :doc:`tools/cluster` Cluster (but don't merge) overlapping/nearby intervals.
49 :doc:`tools/complement` Extract intervals _not_ represented by an interval file.
50 :doc:`tools/coverage` Compute the coverage over defined intervals.
51 :doc:`tools/expand` Replicate lines based on lists of values in columns.
52 :doc:`tools/fisher` Calculate Fisher statistic b/w two feature files.
53 :doc:`tools/flank` Create new intervals from the flanks of existing intervals.
54 :doc:`tools/genomecov` Compute the coverage over an entire genome.
55 :doc:`tools/getfasta` Use intervals to extract sequences from a FASTA file.
56 :doc:`tools/groupby` Group by common cols. & summarize oth. cols. (~ SQL "groupBy")
57 :doc:`tools/igv` Create an IGV snapshot batch script.
58 :doc:`tools/intersect` Find overlapping intervals in various ways.
59 :doc:`tools/jaccard` Calculate the Jaccard statistic b/w two sets of intervals.
60 :doc:`tools/links` Create a HTML page of links to UCSC locations.
61 :doc:`tools/makewindows` Make interval "windows" across a genome.
62 :doc:`tools/map` Apply a function to a column for each overlapping interval.
63 :doc:`tools/maskfasta` Use intervals to mask sequences from a FASTA file.
64 :doc:`tools/merge` Combine overlapping/nearby intervals into a single interval.
65 :doc:`tools/multicov` Counts coverage from multiple BAMs at specific intervals.
66 :doc:`tools/multiinter` Identifies common intervals among multiple interval files.
67 :doc:`tools/nuc` Profile the nucleotide content of intervals in a FASTA file.
68 :doc:`tools/overlap` Computes the amount of overlap from two intervals.
69 :doc:`tools/pairtobed` Find pairs that overlap intervals in various ways.
70 :doc:`tools/pairtopair` Find pairs that overlap other pairs in various ways.
71 :doc:`tools/random` Generate random intervals in a genome.
72 :doc:`tools/reldist` Calculate the distribution of relative distances b/w two files.
73 :doc:`tools/sample` Sample random records from file using reservoir sampling.
74 :doc:`tools/shift` Adjust the position of intervals.
75 :doc:`tools/shuffle` Randomly redistribute intervals in a genome.
76 :doc:`tools/slop` Adjust the size of intervals.
77 :doc:`tools/sort` Order the intervals in a file.
78 :doc:`tools/spacing` Sample random records from file using reservoir sampling.
79 :doc:`tools/split` Split a file into multiple files with equal records or base pairs.
80 :doc:`tools/subtract` Remove intervals based on overlaps b/w two files.
81 :doc:`tools/tag` Tag BAM alignments based on overlaps with interval files.
82 :doc:`tools/unionbedg` Combines coverage intervals from multiple BEDGRAPH files.
83 :doc:`tools/window` Find overlapping intervals within a window around an interval.
84 =============================================== ========================================================================
85
86
87
88
89
90
91 ===========================
92 Fundamental concepts.
93 ===========================
94 ------------------------------------------------------
95 What are genome features and how are they represented?
96 ------------------------------------------------------
97 Throughout this manual, we will discuss how to use bedtools to manipulate, compare and ask questions of genome “features”. Genome features can be functional elements (e.g., genes), genetic polymorphisms (e.g.
98 SNPs, INDELs, or structural variants), or other annotations that have been discovered or curated by genome sequencing groups or genome browser groups. In addition, genome features can be custom annotations that
99 an individual lab or researcher defines (e.g., my novel gene or variant).
100
101 The basic characteristics of a genome feature are the chromosome or scaffold on which the feature “resides”, the base pair on which the
102 feature starts (i.e. the “start”), the base pair on which feature ends (i.e. the “end”), the strand on which the feature exists (i.e. “+” or “-“), and the name of the feature if one is applicable.
103
104 The two most widely used formats for representing genome features are the BED (Browser Extensible Data) and GFF (General Feature Format) formats. bedtools was originally written to work exclusively with genome features
105 described using the BED format, but it has been recently extended to seamlessly work with BED, GFF and VCF files.
106
107 Existing annotations for the genomes of many species can be easily downloaded in BED and GFF
108 format from the UCSC Genome Browser’s “Table Browser” (http://genome.ucsc.edu/cgi-bin/hgTables?command=start) or from the “Bulk Downloads” page (http://hgdownload.cse.ucsc.edu/downloads.html). In addition, the
109 Ensemble Genome Browser contains annotations in GFF/GTF format for many species (http://www.ensembl.org/info/data/ftp/index.html)
110
111 -------------------------------------
112 Overlapping / intersecting features.
113 -------------------------------------
114 Two genome features (henceforth referred to as “features”) are said to overlap or intersect if they share at least one base in common.
115 In the figure below, Feature A intersects/overlaps Feature B, but it does not intersect/overlap Feature C.
116
117 **TODO: place figure here**
118
119 --------------------------------------------
120 Comparing features in file “A” and file “B”.
121 --------------------------------------------
122 The previous section briefly introduced a fundamental naming convention used in bedtools. Specifically, all bedtools that compare features contained in two distinct files refer to one file as feature set “A” and the other file as feature set “B”. This is mainly in the interest of brevity, but it also has its roots in set theory.
123 As an example, if one wanted to look for SNPs (file A) that overlap with exons (file B), one would use bedtools intersect in the following manner::
124
125 bedtools intersect –a snps.bed –b exons.bed
126
127 There are two exceptions to this rule: 1) When the “A” file is in BAM format, the “-abam” option must be used. For example::
128
129 bedtools intersect –abam alignedReads.bam –b exons.bed
130
131 And 2) For tools where only one input feature file is needed, the “-i” option is used. For example::
132
133 bedtools merge –i repeats.bed
134
135 -----------------------------------------------------
136 BED starts are zero-based and BED ends are one-based.
137 -----------------------------------------------------
138 bedtools users are sometimes confused by the way the start and end of BED features are represented. Specifically, bedtools uses the UCSC Genome Browser’s internal database convention of making the start position 0-based and the end position 1-based: (http://genome.ucsc.edu/FAQ/FAQtracks#tracks1)
139 In other words, bedtools interprets the “start” column as being 1 basepair higher than what is represented in the file. For example, the following BED feature represents a single base on chromosome 1; namely, the 1st base::
140
141 chr1 0 1 first_base
142
143 Why, you might ask? The advantage of storing features this way is that when computing the length of a feature, one must simply subtract the start from the end. Were the start position 1-based,
144 the calculation would be (slightly) more complex (i.e. (end-start)+1). Thus, storing BED features this way reduces the computational burden.
145
146 -----------------------------------------------------
147 GFF starts and ends are one-based.
148 -----------------------------------------------------
149 In contrast, the GFF format uses 1-based coordinates for both the start and the end positions. bedtools is aware of this and adjusts the positions accordingly.
150 In other words, you don’t need to subtract 1 from the start positions of your GFF features for them to work correctly with bedtools.
151
152 -----------------------------------------------------
153 VCF coordinates are one-based.
154 -----------------------------------------------------
155 The VCF format uses 1-based coordinates. As in GFF, bedtools is aware of this and adjusts the positions accordingly.
156 In other words, you don’t need to subtract 1 from the start positions of your VCF features for them to work correctly with bedtools.
157
158 -----------------------------------------------------
159 File B is loaded into memory (most of the time).
160 -----------------------------------------------------
161 Whenever a bedtool compares two files of features, the “B” file is loaded into memory. By contrast, the “A” file is processed line by line and compared with the features from B.
162 Therefore to minimize memory usage, one should set the smaller of the two files as the B file. One salient example is the comparison of aligned sequence reads from a
163 current DNA sequencer to gene annotations. In this case, the aligned sequence file (in BED format) may have tens of millions of features (the sequence alignments),
164 while the gene annotation file will have tens of thousands of features. In this case, it is wise to sets the reads as file A and the genes as file B.
165
166 -----------------------------------------------------
167 Feature files *must* be tab-delimited.
168 -----------------------------------------------------
169 This is rather self-explanatory. While it is possible to allow BED files to be space-delimited, we have decided to require tab delimiters for three reasons:
170
171 1. By requiring one delimiter type, the processing time is minimized.
172 2. Tab-delimited files are more amenable to other UNIX utilities.
173 3. GFF files can contain spaces within attribute columns. This complicates the use of space-delimited files as spaces must therefore be treated specially depending on the context.
174
175 -------------------------------------------------------------
176 All bedtools allow features to be “piped” via standard input.
177 -------------------------------------------------------------
178
179 In an effort to allow one to combine multiple bedtools and other UNIX utilities into more complicated “pipelines”, all bedtools allow features
180 to be passed to them via standard input. Only one feature file may be passed to a bedtool via standard input.
181 The convention used by all bedtools is to set either file A or file B to “stdin” or "-". For example::
182
183 cat snps.bed | bedtools intersect –a stdin –b exons.bed
184 cat snps.bed | bedtools intersect –a - –b exons.bed
185
186 In addition, all bedtools that simply require one main input file (the -i file) will assume that input is
187 coming from standard input if the -i parameter is ignored. For example, the following are equivalent::
188
189 cat snps.bed | bedtools sort –i stdin
190 cat snps.bed | bedtools sort
191
192 ------------------------------------------------------
193 Most bedtools write their results to standard output.
194 ------------------------------------------------------
195 To allow one to combine multiple bedtools and other UNIX utilities into more complicated “pipelines”,
196 most bedtools report their output to standard output, rather than to a named file. If one wants to write the output to a named file, one can use the UNIX “file redirection” symbol “>” to do so.
197 Writing to standard output (the default)::
198
199 bedtools intersect –a snps.bed –b exons.bed
200 chr1 100100 100101 rs233454
201 chr1 200100 200101 rs446788
202 chr1 300100 300101 rs645678
203
204 Writing to a file::
205
206 bedtools intersect –a snps.bed –b exons.bed > snps.in.exons.bed
207
208 cat snps.in.exons.bed
209 chr1 100100 100101 rs233454
210 chr1 200100 200101 rs446788
211 chr1 300100 300101 rs645678
212
213 ------------------------
214 What is a “genome” file?
215 ------------------------
216 Some of the bedtools (e.g., ``genomecov``, ``complement``, ``slop``) need to know the size of
217 the chromosomes for the organism for which your BED files are based. When using the UCSC Genome
218 Browser, Ensemble, or Galaxy, you typically indicate which species / genome build you are working.
219 The way you do this for bedtools is to create a “genome” file, which simply lists the names of the
220 chromosomes (or scaffolds, etc.) and their size (in basepairs).
221 Genome files must be tab-delimited and are structured as follows (this is an example for C. elegans)::
222
223 chrI 15072421
224 chrII 15279323
225 ...
226 chrX 17718854
227 chrM 13794
228
229 bedtools includes predefined genome files for human and mouse in the /genomes directory included
230 in the bedtools distribution. Additionally, the “chromInfo” files/tables available from the UCSC
231 Genome Browser website are acceptable. For example, one can download the hg19 chromInfo file here:
232 http://hgdownload.cse.ucsc.edu/goldenPath/hg19/database/chromInfo.txt.gz
233
234 ------------------------------------
235 Paired-end BED files (BEDPE files).
236 ------------------------------------
237 We have defined a new file format (BEDPE) to concisely describe disjoint genome features, such as
238 structural variations or paired-end sequence alignments. We chose to define a new format because the
239 existing BED block format (i.e. BED12) does not allow inter-chromosomal feature definitions. Moreover,
240 the BED12 format feels rather bloated when one want to describe events with only two blocks.
241
242 ------------------------------------------
243 Use “-h” for help with any bedtool.
244 ------------------------------------------
245 Rather straightforward. If you use the “-h” option with any bedtool, a full menu of example usage
246 and available options (when applicable) will be reported.
247
248 --------------------------------------------------
249 BED features must not contain negative positions.
250 --------------------------------------------------
251 bedtools will typically reject BED features that contain negative positions. In special cases, however,
252 BEDPE positions may be set to -1 to indicate that one or more ends of a BEDPE feature is unaligned.
253
254 ---------------------------------------------------
255 The start position must be <= to the end position.
256 ---------------------------------------------------
257 bedtools will reject BED features where the start position is greater than the end position.
258
259 -----------------------------------------
260 Headers are allowed in GFF and BED files
261 -----------------------------------------
262 bedtools will ignore headers at the beginning of BED and GFF files. Valid header lines begin with a
263 “#” symbol, the work “track”, or the word “browser”. For example, the following examples are valid
264 headers for BED or GFF files::
265
266 track name=aligned_read description="Illumina aligned reads”
267 chr5 100000 500000 read1 50 +
268 chr5 2380000 2386000 read2 60 -
269
270 #This is a fascinating dataset
271 chr5 100000 500000 read1 50 +
272 chr5 2380000 2386000 read2 60 -
273
274 browser position chr22:1-20000
275 chr5 100000 500000 read1 50 +
276 chr5 2380000 2386000 read2 60 -
277
278 -------------------------------------------------------------
279 GZIP support: BED, GFF, VCF, and BEDPE file can be “gzipped”
280 -------------------------------------------------------------
281 bedtools will process gzipped BED, GFF, VCF and BEDPE files in the same manner as
282 uncompressed files. Gzipped files are auto-detected thanks to a helpful contribution from Gordon
283 Assaf.
284
285 ----------------------------------------------------------------------------
286 Support for “split” or “spliced” BAM alignments and “blocked” BED features
287 ----------------------------------------------------------------------------
288 As of Version 2.8.0, five bedtools (``intersect``, ``coverage``, ``genomecov``,
289 ``bamToBed``, and ``bed12ToBed6``) can properly handle “split”/”spliced” BAM alignments (i.e., having an
290 “N” CIGAR operation) and/or “blocked” BED (aka BED12) features.
291
292 ``intersect``, ``coverage``, and ``genomecov`` will optionally handle “split” BAM and/or
293 “blocked” BED by using the ``-split`` option. This will cause intersects or coverage to be computed only
294 for the alignment or feature blocks. In contrast, without this option, the intersects/coverage would be
295 computed for the entire “span” of the alignment or feature, regardless of the size of the gaps between
296 each alignment or feature block. For example, imagine you have a RNA-seq read that originates from
297 the junction of two exons that were spliced together in a mRNA. In the genome, these two exons
298 happen to be 30Kb apart. Thus, when the read is aligned to the reference genome, one portion of the
299 read will align to the first exon, while another portion of the read will align ca. 30Kb downstream to the
300 other exon. The corresponding CIGAR string would be something like (assuming a 76bp read):
301 30M*3000N*46M. In the genome, this alignment “spans” 3076 bp, yet the nucleotides in the sequencing
302 read only align “cover” 76bp. Without the ``-split`` option, coverage or overlaps would be reported for the
303 entire 3076bp span of the alignment. However, with the ``-split`` option, coverage or overlaps will only
304 be reported for the portions of the read that overlap the exons (i.e. 30bp on one exon, and
305 46bp on the other).
306
307
308 Using the -split option with bamToBed causes “spliced/split” alignments to be reported in BED12
309 format. Using the -split option with ``bed12tobed6`` causes “blocked” BED12 features to be reported in
310 BED6 format.
311
312 --------------------------------
313 Writing uncompressed BAM output.
314 --------------------------------
315 When working with a large BAM file using a complex set of tools in a pipe/stream, it is advantageous
316 to pass uncompressed BAM output to each downstream program. This minimizes the amount of time
317 spent compressing and decompressing output from one program to the next. All bedtools that create
318 BAM output (e.g. ``intersect``, ``window``) will now optionally create uncompressed BAM output
319 using the ``-ubam`` option.
320
321
322
323 =====================================
324 Implementation and algorithmic notes.
325 =====================================
326 bedtools was implemented in C++ and makes extensive use of data structures and fundamental
327 algorithms from the Standard Template Library (STL). Many of the core algorithms are based upon the
328 genome binning algorithm described in the original UCSC Genome Browser paper (Kent et al, 2002).
329 The tools have been designed to inherit core data structures from central source files, thus allowing
330 rapid tool development and deployment of improvements and corrections. Support for BAM files is
331 made possible through Derek Barnett’s elegant C++ API called BamTools.
332