An Erratum to this article was published on 09 January 2017
Next-generation sequencing is making it critical to robustly and rapidly handle genomic ranges within standard pipelines. Standard use-cases include annotating sequence ranges with gene or other genomic annotation, merging multiple experiments together and subsequently quantifying and visualizing the overlap. The most widely-used tools for these tasks work at the command-line (e.g. BEDTools) and the small number of available R packages are either slow or have distinct semantics and features from command-line interfaces.
To provide a robust R-based interface to standard command-line tools for genomic coordinate manipulation, we created bedr. This open-source R package can use either BEDTools or BEDOPS as a back-end and performs data-manipulation extremely quickly, creating R data structures that can be readily interfaced with existing computational pipelines. It includes data-visualization capabilities and a number of data-access functions that interface with standard databases like UCSC and COSMIC.
bedr package provides an open source solution to enable genomic interval data manipulation and restructuring in R programming language which is commonly used in bioinformatics, and therefore would be useful to bioinformaticians and genomic researchers.
With the advent of high-throughput sequencing technologies, data scientists are facing immense challenges in large-scale sequence analysis and in integrating genomic annotations. For instance, comparing new experiments with previously published datasets, translating genomic coordinates between different assemblies of an organism as well as finding cross-species orthologues are some of the common use-cases in basic science experiments. To assist these tasks genomic features are routinely represented and shared using Browser Extensible Display (BED; ), Distributed Annotation System (DAS; ), General Feature Format (GFF), Gene Transfer Format (GTF) and Variant Call Format (VCF). These all enable cross-sectional analysis of genomic studies across multiple programming languages, thereby enabling seamless data-integration.
R is the de facto standard for statistical analysis and visualization in computational biology  for both exploratory prototyping and rigorous production pipelines. To this end R has adopted several packages, such as GenomicRanges and IRanges that expressly deal with genomic intervals . Albeit powerful, these existing tools require understanding of bespoke data structures and classes/objects. To address these issues we implemented a formal BED-operations framework called bedr, which is an R API offering utility functions implementing commonly used genomic operations as well as offering a formal R interface to interact with BEDTools and BEDOPS. bedr is fully compatible with the ubiquitous BED tools [5, 6] paradigm and integrates seamlessly with R-based work-flows.
bedr is implemented in R and supports the two main BED engines: BEDTools and BEDOPS [5, 6]. It works on Unix-like operating systems including Mac OSX. A high-level conceptual overview of the package usage is shown in Fig. 1. bedr functions run on any computing platform (commodity computer, cluster or cloud), and facilitates integration of multiple data sources (e.g. gene annotations) and tools (e.g. BEDTools) with user-specified genomic coordinates, yielding fast results in R data structures such as data.frame and list. All API calls are written in R, which internally calls BEDTools and/or BEDOPS utilities in the native format using system commands. Further, bedr data can be readily converted to BED  and GRanges  objects for wider interoperability.
Results and discussion
The primary input to most bedr methods is a regions object, which is represented as either an R vector of multiple region strings as illustrated below or a data.frame of regions with three columns: chr, start, and end. The regions object returned by various bedr methods matches the input format; vector or data.frame. Here we briefly summarize a subset of key bedr functionalities. For further details on a range of bedr utilities, please see package’s help and vignettes for detailed examples and workflows.
Sort & merge
This functionality enables sorting of genomic regions in both natural and lexographical order using R, unix, BEDTools and BEDOPS engines. Following examples demonstrate the usage of these engines:
regions <- get.example.regions()
region <- regions[]
x = region,
engine = "unix",
method = "natural"
x = region,
engine = "R",
method = "lexicographical"
x = region,
engine = "bedtools"
x = region,
engine = "bedops"
The above code will generate the following outputs of sorted regions:
# natural sort (unix)
# lexicographical sort (R)
# lexicographical sort (bedtools)
# lexicographical sort (bedops)
As shown above, various types of sorting results are presented in a similar R data structures regardless of which sorting engine is used (unix, R, bedtools or bedops) and their respective output style. Also, BEDTools and BEDOPS do not support natural sorting, and if method = “natural” is requested with these two engines, bedr automatically defaults to using engine = “unix” of “R” to perform sorting. Note, sorting of large number of regions through R will be slow and may also result in high memory overhead.
Much of the command-line interaction with BEDTools and BEDOPS is performed through temporary files followed by efficient piping/parsing of the output straight into R data structures. This ensures that memory intensive sorting tasks (or any other genomic operations discussed below) are managed by the optimized engines such as (BEDTools or BEDOPS), and therefore memory operations in R are limited to subsequent parsing of output.
In addition to sort operations, bedr also supports identification of overlapping regions which can be collapsed to avoid downstream analytical challenges such as many:many join results (Fig. 2), e.g.
bedr.merge.region(x = region)
The above code will generate the following output of merged regions:
Sort and merge can be combined into one step given they are generally run as a tandem preprocessing step:
bedr.snm.region(x = region)
The above code will generate the following vector output of sorted and merged regions:
This functionality enables joining two region-based datasets using intervals as an index or primary key. The output is left outer join with respect to the first regions object (Fig. 2), e.g.
regions.a <- bedr.merge.region(
x = regions[]
regions.b <- bedr.merge.region(
x = regions[]
regions.c <- bedr.merge.region(
x = regions[]
x = regions.a,
y = regions.b
The above code will generate the following output, containing regions of regions.a in the first column, while any overlapping regions from regions.b are listed in columns 2 to 4 (chr, start, end). Regions in regions.a with no overlap are encoded as: . and -1
Similarly, another bedr function bedr.join.multiple.region() supports merging of multiple sets of regions (Fig. 2), e.g.
x = list(
a = regions.a,
b = regions.b,
c = regions.c
The above code will generate the output data.frame shown below. The table lists all the sub-regions and their presence across the three sets of region objects (regions.a, regions.b, and regions.c) passed to the function. For instance, sub-region chr1:1-10 (column: index) overlaps with 2 region objects (b and c). This presence is shown as comma separated list in ‘names’ column as well as a truth table in the subsequent columns. The number of columns representing the truth table will match the number of region objects passed to the function bedr.join.multiple.region().
b,c 0 1 1 a 1 0 0 a,c 1 0 1 c 0 0 1 a,c 1 0 1 a,b,c 1 1 1 b,c 0 1 1 a,b,c 1 1 1 b,c 0 1 1 b 0 1 0 b 0 1 0 a 1 0 0 b 0 1 0 b,c 0 1 1 b,c 0 1 1 c 0 0 1 a,c 1 0 1 a 1 0 0 a,c 1 0 1 a,b,c 1 1 1 a 1 0 0 b 0 1 0 b,c 0 1 1 c 0 0 1
Subtract and intersect
The subtract utility identifies regions exclusive to first set of regions, and intersect function identifies sub-regions of first set which overlap with the second set of regions (Fig. 2), e.g.
x = regions.a,
y = regions.b
The above code will generate the following output which lists the sub-regions exclusive to regions.a:
Intersect utility makes use of bed.join.region() and finds regions in the second set which overlap with the regions in the first set. An example is shown in the Results section “Join”. Similarly in.region(x = regions.a, y = regions.b) and its R style convenience operator %in.region% can be used to test (logical) presence of overlapping regions, e.g.
x = regions.a,
y = regions.b
FALSE TRUE TRUE FALSE TRUE FALSE
bedr also provides an interface to find overlapping regions using Tabix . This can be done using the following bedr call:
Given that bedr can process regions data as R’s vector as well as data.frame data structure, it is easily transformable to other third-party sequence and region objects. For instance, bedr provides a utility adaptor to convert regions into BED data.frame as shown below:
regions.a.bed <- convert2bed(
x = regions.a
which can be further converted to a widely compatible GRanges  object, as shown below:
df = regions.a.bed
The above code will create a GRanges object shown in the output below, which can be further customized/extended with additional annotations such as strand and genomic feature names.
GRanges object with 6 ranges and 0 metadata columns:
- - - - - - - seqinfo: 4 sequences from an unspecified genome;no seqlengths
To perform feature meta-analysis and annotation retrieval/conversion (see example workflow in Additional file 1), bedr facilitates downloads from UCSC , COSMIC  and HUGO  including reference genome annotations, repeat sequences, black lists and disease candidate features. Also, bedr has a fully integrated unit-testing framework allowing users to verify integrity of bedr functions when using customized development or installations.
For results of common operations such as intersect, Venn diagrams of overlapping features between 2 to 5 sets of regions (2- to 5-way Venn diagrams) can be generated automatically . The overlap criterion can be defined in a number of ways including unique intervals, gene length or user-specified size as a fraction of sub-region’s length, e.g.
input = list(
a = regions.a,
b = regions.b
feature = "bp",
fraction.overlap = 0.1
The above code will generate a base pair level overlap of sequence objects regions.a and regions.b, and show the results as a Venn diagram highlighting lengths of exclusive and overlapping regions as shown below:
Further, bedr output is ideally suited for alternative complex set visualization tools such as UpSetR  and Gviz .
We created bedr; an R package to support genomic operations using the BEDTools  and BEDOPS  engines. bedr implements an API in R which offers a number of utility functions such as intersect, merge, sort and plotting of genomic intervals as well as provides a unified interface to BEDTools and BEDOPS. These functions are efficient, powerful and perform complex feature annotations and cross-sectional operations on genomic regions. Given that bedr supports two well-established genomic engines, its output is comparable to the native output of these tools, however in R data structures. These features of bedr are urgently needed by bioinformatics research community and will be a timely addition to the catalogue of sequence analysis tools. Further, the interoperability of bedr data structures with BED and GRanges data.frame/objects makes it an easy-to-fit component in existing genomic pipelines. bedr is freely available as an open-source package through CRAN and lends itself for customized extensions needed for in-house sequencing-analysis pipelines as well as future bioinformatics protocols.
Forbes SA, Beare D, Gunasekaran P, Leung K, Bindal N, Boutselakis H, Ding M, Bamford S, Cole C, Ward S, et al. COSMIC: exploring the world’s knowledge of somatic mutations in human cancer. Nucleic Acids Res. 2015;43:D805–11.
We thank the Boutros lab for support and software testing.
This study was supported by the Ontario Institute for Cancer Research (PCB), the Ontario Ministry of Health and Long Term Care (OMOHLTC; FFL), the Dr. Mariano Elia Chair in Head & Neck Cancer Research, and philanthropic support from the Wharton family, Joe’s Team, and Gordon Tozer. Views expressed do not necessarily reflect those of the OMOHLTC. PCB was supported by TFRI New Investigator and CIHR New Investigator Awards. This work was supported by Prostate Cancer Canada and is proudly funded by the Movember Foundation - Grant #RS2014-01. EL was supported by an Ontario Graduate Scholarship.
DW and PCB conceived the project. DW wrote the package with contributions and extensions from SH. SH, EL and CF performed testing and contributed to documentation and use-cases. PCB and FFL supervised the development. SH and PCB wrote the manuscript, which all authors read and approved.
The authors declare that they have no competing interests.
Consent for publication
Ethics approval and consent to participate
Authors and Affiliations
Informatics and Biocomputing Platform, Ontario Institute for Cancer Research, Toronto, M5G 0A3, Canada
Syed Haider, Daryl Waggott, Emilie Lalonde, Clement Fung & Paul C. Boutros
Departments of Radiation Oncology, Pharmacology & Toxicology, and Medical Biophysics, University of Toronto, Toronto, M5G 2M9, Canada
Emilie Lalonde, Fei-Fei Liu & Paul C. Boutros
Ontario Cancer Institute and Campbell Family Institute for Cancer Research, Princess Margaret Hospital, University Health Network, Toronto, M5G 2M9, Canada
Open Access This article is distributed under the terms of the Creative Commons Attribution 4.0 International License (http://creativecommons.org/licenses/by/4.0/), which permits unrestricted use, distribution, and reproduction in any medium, provided you give appropriate credit to the original author(s) and the source, provide a link to the Creative Commons license, and indicate if changes were made. The Creative Commons Public Domain Dedication waiver (http://creativecommons.org/publicdomain/zero/1.0/) applies to the data made available in this article, unless otherwise stated.