The BED format
BED (Browser Extensible Data) format is a simple text format for location-oriented data (genomic regions) developed to support UCSC Genome Browser tracks. Standard BED files have 3 to 6 Tab-separated columns, although up to 12 columns are defined. (Read more about the UCSC Genome Browser's official BED format.)
Memorize the 6 main BED fields
These 6 BED fields are so important that you should memorize them. Keep repeating "chrom, start, end, name, score, strand" until the words trip off your tongue
- chrom (required) – string naming the chromosome or other contig
- start (required) – the 0-based start position of the region
- end (required) – the 1-based end position of the region
- name (optional) – an arbitrary string describing the region
- for BED files loaded as UCSC Genome Browser tracks, this text is displayed above the region
- score (optional) – an integer score for the region
- for BED files to be loaded as UCSC Genome Browser tracks, this should be a number between 0 and 1000, higher = "better"
- for non-GenBrowse BED files, this can be any integer value (e.g. the length of the region)
- strand (optional) - a single character describing the region's strand
- + – plus strand (Watson strand) region
- - – minus strand (Crick strand) region
- . – no strand – the region is not associated with a strand (e.g. a transcription factor binding region)
Important rules for BED format:
- The number of fields per line must be consistent throughout any single BED file
- e.g. they must all have 3 fields or all have 6 fields
- The first base on a contig is numbered 0
- versus 1 for BAM file positions
- so the a BED start of 99 is actually the 100th base on the contig
- but end positions are 1-based
- so a BED end of 200 is the 200th base on the contig
- the length of a BED region is end - start
- not end - start + 1, as it would be if both coordinates with 0-based or both 1-based
- this difference is the single greatest source of errors dealing with BED files!
Note that the UCSC Genome Browser also defines many BED-like data formats (e.g. bedGraph, narrowPeak, tagAlign and various RNA element formats). See supported UCSC Genome Browser data formats for more information and examples.
In addition to standard-format BED files, one can create custom BED files that have at least 3 of the standard fields (chrom, start, end), followed by any number of custom fields. For example:
- A BED3+ file contains the 3 required BED fields, followed by some number of user-defined columns (all records with the same number)
- A BED6+ file contains the 3 required BED fields, 3 additional standard BED fields (name, score, strand), followed by some number of user-defined columns
As we will see, BEDTools functions require BED3+ input files, or BED6+ if strand-specific operations are requested.
The BEDTools suite is a set of utilities for manipulating BED and BAM files. We call it the "Swiss army knife" for genomic region analyses because its sub-commands are so numerous and versatile. Some of the most common bedtools operations perform set-theory functions on regions: intersection (intersect), union (merge), set difference (subtract) – but there are many others. The table below lists some of the most useful sub-commands along with applicable use cases.
|bamtobed||Convert BAM files to BED format.||You want to have the contig, start, end, and strand information for each mapped alignment record in separate fields. Recall that the strand is encoded in a BAM flag (0x10) and the exact end coordinate requires parsing the CIGAR string.|
|bamtofastq||Extract FASTQ sequences from BAM alignment records.||You have downloaded a BAM file from a public database, but it was not aligned against the reference version you want to use (e.g. it is hg19 and you want an hg38 alignment). To re-process, you need to start with the original FASTQ sequences.|
|getfasta||Get FASTA entries corresponding to regions.||You want to run motif analysis, which requires the original FASTA sequences, on a set of regions of interest. In addition to the BAM file, you must provide FASTA file(s) for the genome/reference used for alignment (e.g. the FASTA file used to build the aligner index).|
|multicov||Count overlaps between one or more BAM files and a set of regions of interest.|
|merge||Combine a set of possibly-overlapping regions into a single set of non-overlapping regions.||Collapse overlapping gene annotations into per-strand non-overlapping regions before counting (e.g with featureCounts or HTSeq). If this is not done, the source regions will potentially be counted multiple times, once for each (overlapping) target region it intersects.|
|subtract||Remove unwanted regions.||Remove rRNA gene regions from a merged gene annotations file before counting.|
|intersect||Determine the overlap between two sets of regions.||Similar to multicov, but can also report (not just count) the overlapping regions.|
|closest||Find the genomic features nearest to a set of regions.||For a set of significant ChIP-seq transcription factor (TF) binding regions ("peaks") that have been identified, determine nearby genes that may be targets of TF regulation.|
We will explore a few of these functions in our exercises.
BEDTools is under active development and is always being refined and extended. Unfortunately, sometimes changes are made that are incompatible with previous BEDTools versions. For example, a major change to the way bedtool merge functions was made after bedtools v2.17.0.
So it is important to know which version of BEDTools you are using, and read the documentation carefully to see if changes have been made since your version.
Login to stampede2, start and idev session, then load the BioContainers bedtools module, and check its version.
Input format considerations
- Most BEDTools functions now accept either BAM or BED files as input.
- BED format files must be BED3+, or BED6+ if strand-specific operations are requested.
- When comparing against a set of regions, those regions are usually supplied in either BED or GTF/GFF.
- All text-format input files (BED, GTF/GFF, VCF) should use Unix line endings (linefeed only).
The most important thing to remember about comparing regions using BEDTools, is that all region files must share the same set contig names and be based on the same reference! For example, if an alignment was performed against a human GRCh38 reference genome from Gencode, use annotations from the corresponding GFF/GTF annotations.
By default many bedtools utilities that perform overlapping, consider reads overlapping the feature on either strand, but can be made strand-specific with the -s or -S option. This strandedness options for bedtools utilities refers the orientation of the R1 read with respect to the feature's (gene's) strand.
- -s says the R1 read is sense stranded (on the same strand as the gene).
- -S says the R1 read is antisense stranded (the opposite strand as the gene).
RNA-seq libraries can be constructed with 3 types of strandedness:
- sense stranded – the R1 read should be on the same strand as the gene.
- antisense stranded – the R1 read should be on the opposite strand as the gene.
- unstranded – the R1 could be on either strand
Which type of RNA-seq library you have depends on the library preparation method – so ask your sequencing center! Our yeast RNA-seq library is sense stranded (note that most RNA-seq libraries prepared by GSAF are antisense stranded).
If you have a stranded RNA-seq library, you should use either -s or -S to avoid false counting against a gene on the wrong strand.
About GFF/GTF annotation files
Unfortunately, both formats are obscure and hard to work with directly. While bedtools does accept annotation files in GFF/GTF format, you will not like the results. This is because the most useful information in a GFF/GTF file is in a loosely-structured attributes field.
Also unfortunately, there are a number of variations of both annotation formats However both GTF and GFF share the first 8 Tab-separated fields:
- seqname - The name of the chromosome or scaffold.
- source - Name of the program that generated this feature, or other data source (e.g. database)
- feature_type - Type of the feature. Examples of common feature types include:
- Some examples of common feature types are:
- CDS (coding sequence), exon
- gene, transcript
- start_codon, stop_codon
- Some examples of common feature types are:
- start - Start position of the feature, with sequence numbering starting at 1.
- end - End position of the feature, with sequence numbering starting at 1.
- score - A numeric value. Often but not always an integer.
- strand - Defined as + (forward), - (reverse), or . (no relevant strand)
- frame - For a CDS, one of 0, 1 or 2, specifying the reading frame of the first base; otherwise '.'
The Tab-separated columns will care about are (1) seqname, (3) feature_type and (4,5) start, end. The reason we care is that when working with annotations, we usually only want to look at annotations of a particular type, most commonly gene, but also transcript or exon.
So where is the real annotation information, such as the unique gene ID or gene name? Both formats also have a 9th field, which is usually populated by a set of name/value pair attributes, and that's where the useful information is (e.g. the unique feature identifier, name, and so forth).
Take a quick look at a yeast annotation file, sacCer_R64-1-1_20110208.gff using less.
In addition to comment lines (starting with #), you can see the chrI contig names in column 1 and various feature types in column 3. You see also see tags like Name=YAL067C;gene=SEO1; among the attributes on some records, but in general the attributes column information is really ugly.
To summarize, we have two problems to solve:
- We only care about a subset of feature types (here genes), and
- We want the important annotation information – gene names and IDs – to appear as regular columns instead of weird name/value pairs.
Filter annotations based on desired feature type
One of the first things you want to know about your annotation file is what gene features it contains. Here's how to find that: (Read more about what's going on here at Piping a histogram.)
You should see something like this.
Let's create a file that contains only the 6607 gene entries:
The line count of sc_genes.gff should be 6607 – one for each gene entry.
Convert GFF/GTF format to BED with ID in the name field
Our sc_genes.gff annotation subset now contains only the 6607 genes in the Saccharomyces cerevisiae genome. This addresses our first problem, but entries in this file still have the important information – the gene ID and name – in the loosely-structured 9th attributes field.
If we want to associate reads with features, we need to have the feature names where they are easy to extract!
What most folks to is find some way to convert their GFF/GTF file to a BED file, parsing out some (or all) of the name/value attribute pairs into BED file columns after the standard 6. You can find such conversion programs on the web – or write one yourself. Or you could use the BioITeam conversion script, /work2/projects/BioITeam/common/script/gtf_to_bed.pl. While it will not work 100% of the time, it manages to do a decent job on most GFF/GTF files. And it's pretty easy to run.
Let Anna know if you run into problems
If this script doesn't work on your annotation file, please let Anna know. She is always looking for cases where the conversion fails, and will try to fix it.
Here we just give the script the GFF file to convert, plus a 1 that tells it to URL decode weird looking text (e.g. our Note attribute values).
The program reads the input file twice – once to gather all the attribute names, and then a second time to write the attribute values in well-defined columns. You'll see output like this:
To find out what the resulting columns are, look at the header line out the output BED file:
For me the resulting 16 attributes are as follows (they may have a different order for you). I've numbered them below for convenience
The final transformation is to do a bit of re-ordering, dropping some fields. We'll do this with awk, becuase cut can't re-order fields. While this is not strictly required, it can be helpful to have the critical fields (including the gene ID) in the 1st 6 columns. We do this separately for the header line and the rest of the file so that the BED file we give bedtools does not have a header (but we know what those fields are). We would normally preserve valuable annotation information such as Ontology_term, dbxref and Note, but drop them here for simplicity.
One final detail. Annotation files you download may have non-Unix (linefeed-only) line endings. Specifically, they may use Windows line endings (carriage return + linefeed). (Read about Line ending nightmares.) The expression sed 's/\r//' uses the sed (substitution editor) tool to replace carriage return characters ( \r ) with nothing, removing them from the output.
Finally, the 8 re-ordered attributes are:
**Whew**! That was a lot of work. Welcome to the world of annotation wrangling – it's never pretty! But at least the result is much nicer looking. Examine the results using more or less or head:
Doesn't this look better? (I've tidied up the output a bit below.)
Note that value in the 8th column. In the yeast annotations from SGD there are 3 gene classifications: Verified, Uncharacterized and Dubious. The Dubious ones have no experimental evidence so are generally excluded.
Exercise: How many genes in our sc_genes.bed file are in each category?
Use cut to isolate that field, sort to sort the resulting values into blocks, then uniq -c to count the members of each block.
You should see this:
If you want to further order this output listing the most abundant category first, add another sort statement:
The -k 1,1nr options says to sort on the 1st field (whitespace delimited) of input, using numeric sorting, in reverse order (i.e., largest first). Which produces:
We're now (finally!) actually going to do some gene-based analyses of a yeast RNA-seq dataset using bedtools and the BED-formatted yeast gene annotation file we created above.
Get the RNA-seq BAM
Make sure you're in an idev session, since we will be doing some significant computation, and make bedtools and samtools available.
Copy over the yeast RNA-seq files we'll need (also copy the GFF gene annotation file if you didn't make one).
Exercises: How many reads are represented in the yeast_mrna.sort.filt.bam file? How many mapped? How many proper pairs? How many duplicates? What is the distribution of mapping qualities? What is the average mapping quality?
samtools flagstat for the different read counts.
samtools view + cut + sort + uniq -c for mapping quality distribution
samtools view + awk for average mapping quality
There are 3323242 total reads, all mapped and all properly paired. So this must be a quality-filtered BAM. There are 922114 duplicates, or about 28%.
To get the distribution of mapping qualities:
To compute average mapping quality:
Mapping qualities range from 20 to 60 – excellent quality! Because the majority reads have mapping quality 60, the average is 59. So again, there must have been quality filtering performed on upstream alignment records.
Use bedtools multicov to count feature overlaps
In this section we'll use bedtools multicov to count RNA-seq reads that overlap our gene features. The bedtools multicov command (http://bedtools.readthedocs.io/en/latest/content/tools/multicov.html) takes a feature file (GFF/BED/VCF) and counts how many reads from one or more input BAM files overlap those feature. The input BAM file(s) must be position-sorted and indexed.
Here's how to run bedtools multicov, directing the standard output to a file:
Exercise: How may records of output were written? Where is the count of overlaps per output record?
6607 records were written, one for each feature in the sc_genes.bed file.
The overlap count was added as the last field in each output record (here field 9, since the input annotation file had 8 columns).
Exercise: How many features have non-zero overlap counts?
Most of the genes (6235/6607) have non-zero read overlap counts.
Exercise: What is the total count of reads mapping to gene features?
There are 1144990 overlapping reads in 6235 gene annotations.
Recall that in the yeast annotations from SGD there are 3 gene classifications: Verified, Uncharacterized and Dubious, and the Dubious ones have no experimental evidence.
Exercise: What is the total count of reads mapping to gene features other than Dubious?
There are 1093140 overlapping reads in 5578 non-Dubious genes
Use bedtools merge to collapse overlapping annotations
One issue that often arises when dealing with BED regions is that they can overlap one another. For example, on the yeast genome, which has very few non-coding areas, there are some overlapping ORFs (Open Reading Frames), especially Dubious ORFs that overlap Verified or Uncharacterized ones. When bedtools looks for overlaps, it will count a read that overlaps any of those overlapping ORFs – so some reads can be counted twice.
One way to avoid this double-counting is to collapse the overlapping regions into a merged set of non-overlapping regions – and that's what the bedtools merge utility does (http://bedtools.readthedocs.io/en/latest/content/tools/merge.html).
Here we're going to use bedtools merge to collapse our gene annotations into a non-overlapping set, first for all genes, then for only non-Dubious genes.
The output from bedtools merge always starts with 3 columns: chrom, start and end of the merged region only.
Using the -c (column) and -o (operation) options, you can have information added in subsequent fields. Each comma-separated column number following -c specifies a column to operate on, and the corresponding comma-separated function name following the -o specifies the operation to perform on that column in order to produce an additional output field.
For example, our sc_genes.bed file has a gene name in column 4, and for each (possibly merged) gene region, we want to know the number of gene regions that were collapsed into the region, and also which gene names were collapsed.
We can do this with -c 6,4,4 -o distinct,count,collapse, which says that three custom output columns should be added:
- the 1st custom column should result from collapsing distinct (unique) values of gene file column 6 (the strand, + or -)
- since we will ask for stranded merging, the merged regions will always be on the same strand, so this value will always be + or -
- the 2nd custom output column should result from counting the gene names in column 4 for all genes that were merged, and
- the 3rd custom output should be a comma-separated collapsed list of those same column 4 gene names
bedtools merge also requires that the input BED file be sorted by locus (chrom + start), so we do that first, then we request a strand-specific merge (-s):
The first few lines of the merged.sc_genes.txt file look like this (I've tidied it up a bit):
Output column 4 has the region's strand. Column 5 is the count of merged regions, and column 6 is a comma-separated list of the merged gene names.
Exercise: Compare the number of regions in the merged and before-merge gene files.
There were 6607 genes before merging and 6485 after.
Exercise: How many regions represent only 1 gene, 2 genes, or more?
Output column 5 has the gene count.
Produces this histogram:
There are 111 regions (105 + 4 + 1 + 1) where more than one gene contributed.
Exercise: Repeat the steps above, but first create a good.sc_genes.bed file that does not include Dubious ORFs.
There were 5797 "good" (non-Dubious) genes before merging and 5770 after.
Produces this histogram:
Now there are only 20 regions where more than one gene was collapsed. Clearly eliminating the Dubious ORFs helped.
Exercise: Why did we name the merged file with the extension .txt instead of .bed? What would we need to do to convert it to a proper BED6 file?
The output does not follow the BED6 specification: "chrom, start, end, name, score, strand"
The first 3 output columns comply with the BED3 standard (chrom, start, end), but if strand is to be included, it should be in column 6. Column 4 should be name (we'll put the collapsed gene name list there), and column 5 a score (we'll put the region count there).
We can use awk to re-order the fields: