The bam2bw function can be used to compute
per-nucleotide or per-bin coverage from alignments and save it to a
bigwig file. In this process, information about individual reads is
lost, but the produced signals are considerably more lightweight and
amenable to visualization. The bigwig format is readily queried from R
or compatible with a variety of tools, including genome browsers.
To introduce the different variations on coverage, let’s assume you’ve go the following single-end reads:
suppressPackageStartupMessages(library(epiwraps))
# we create some arbitrary genomic ranges
gr <- GRanges("chr1", IRanges(c(30,70,120), width=50), strand=c("+","+","-"),
seqlengths=c(chr1=500))
plotSignalTracks(list(reads=gr), region="chr1:1:180", extend=0, genomeAxis=FALSE)For testing purposes, we’ll save this as a bam file and index it:
bam <- tempfile(fileext = ".bam") # temp file name
rtracklayer::export(gr, bam, format="bam")
Rsamtools::indexBam(bam)## /tmp/Rtmpd6e1MB/file1cd75688abd9.bam
## "/tmp/Rtmpd6e1MB/file1cd75688abd9.bam.bai"
Using these example reads, we can illustrate different ways of computing coverages.
First, we can save coverage at different resolutions, from full resolution (each nucleotide is a single bin) to larger bin sizes, the latter giving smaller filesizes:
# Full coverage with bin width of 1 nucleotide (i.e. full resolution)
cov_full_bw1 <- tempfile(fileext = ".bw") # temp file name
bam2bw(bam, cov_full_bw1, binWidth=1L, scaling=FALSE)## `paired` not specified, assuming single-end reads. Set to paired='auto' to automatically detect.
## Reading in signal...
## Writing bigwig...
# Full coverage with larger bins
cov_full_bw25 <- tempfile(fileext = ".bw")
bam2bw(bam, cov_full_bw25, binWidth=25L, scaling=FALSE)## `paired` not specified, assuming single-end reads. Set to paired='auto' to automatically detect.
## Reading in signal...
## Writing bigwig...
plotSignalTracks(list(reads=gr, "binWidth=1"=cov_full_bw1, "binWidth=25"=cov_full_bw25),
region="chr1:1:180", extend=0)Both tracks compile the number of reads that overlap each position, but in the bottom track the signal is by chunks of 25 nucleotides. By default, the maximum signal inside a bin is used, however it is possible to change this:
# Using mean per bin:
cov_full_bw25mean <- tempfile(fileext = ".bw")
bam2bw(bam, cov_full_bw25mean, binWidth=25L, binSummarization = "mean", scaling=FALSE)## `paired` not specified, assuming single-end reads. Set to paired='auto' to automatically detect.
## Reading in signal...
## Writing bigwig...
plotSignalTracks(list(reads=gr, "binWidth=1"=cov_full_bw1,
"binWidth=25\n(max)"=cov_full_bw25,
"binWidth=25\n(mean)"=cov_full_bw25mean),
region="chr1:1:180", extend=0)In most cases, single-end reads are just the beginning of the DNA
fragments obtained, and we know the average size of the fragments from
library prep QC (if not, see the estimateFragSize function,
or the simpler estimate.mean.fraglen function of the chipseq
package). It is therefore common to extend reads to this size when
computing coverage, so as to obtain the number of fragments (rather than
reads) coverage each position. This can be done as follows:
# Here the reads are 50bp, and we want to extend them to 100bp, hence _by_ 50:
cov_full_ext <- tempfile(fileext = ".bw")
bam2bw(bam, cov_full_ext, binWidth=1L, extend=50L, scaling=FALSE)## `paired` not specified, assuming single-end reads. Set to paired='auto' to automatically detect.
## Reading in signal...
## Writing bigwig...
plotSignalTracks(list(reads=gr, "no extension"=cov_full_bw1,
"read extension"=cov_full_ext),
region="chr1:1:190", extend=0)Taking into account read extension shows better the peak at the center, which is indicative of the fact that this is the region that was the most enriched in the captured fragments.
Instead of computing coverage, we could compute the number of reads starting, ending, or being centered at each position:
# Here the reads are 50bp, and we want to extend them to 100bp, hence _by_ 50:
cov_start <- tempfile(fileext = ".bw")
bam2bw(bam, cov_start, binWidth=1L, extend=50L, scaling=FALSE, type="start")## `paired` not specified, assuming single-end reads. Set to paired='auto' to automatically detect.
## Reading in signal...
## Writing bigwig...
cov_center <- tempfile(fileext = ".bw")
bam2bw(bam, cov_center, binWidth=1L, extend=50L, scaling=FALSE, type="center")## `paired` not specified, assuming single-end reads. Set to paired='auto' to automatically detect.
## Reading in signal...
## Writing bigwig...
plotSignalTracks(list(reads=gr, "type=full"=cov_full_bw1,
"type=start"=cov_start, "type=center"=cov_center),
region="chr1:1:190", extend=0)Note that when extending reads, as in this case, the position (e.g. “center”) are relative to the extended read (i.e. extension is applied first).
The following figure, created using epiwraps (see
the vignette on generating such
plots), represent chromatin accessibility (ATAC-seq) signals around
bound CTCF motifs in T-cells. The different signals are based on
different bigwig files derived from the same bam file using the
functions described above.
minFragLength=147, maxFragLength=230, type="center", extend=25L.
Using this we can see nucleosomes well-positioned at some distance from
CTCF binding sites, but a relative depletion of nucleosomes at the bound
site itself.maxFragLength=120. These are
indicative of TF binding, and indeed we only see and enrichment in the
center.trim=4L, binWidth=1L, maxFragLength=120, type="ends". Using
this we can see a nice footprint protected from the transposase by CTCF
binding.For more information about these parameters, see
?bam2bw.
ATAC fragment sizes convey rich information, but they are not perfect. For example, regions of DNA bound by multiple TFs can sometimes be captured as longer fragments which we would mistakenly classify as mono-nucleosome containing.
If you use fragment files (preferably tabix-indexed) rather than bam
files as input, you can still perform most of the above tasks. See the
?frag2bw function for more information.
## R version 4.6.0 (2026-04-24)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.4 LTS
##
## Matrix products: default
## BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so; LAPACK version 3.12.0
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## time zone: Etc/UTC
## tzcode source: system (glibc)
##
## attached base packages:
## [1] grid stats4 stats graphics grDevices utils datasets
## [8] methods base
##
## other attached packages:
## [1] epiwraps_0.99.120 EnrichedHeatmap_1.43.0
## [3] ComplexHeatmap_2.29.0 SummarizedExperiment_1.43.0
## [5] Biobase_2.73.1 GenomicRanges_1.65.0
## [7] Seqinfo_1.3.0 IRanges_2.47.2
## [9] S4Vectors_0.51.3 BiocGenerics_0.59.7
## [11] generics_0.1.4 MatrixGenerics_1.25.0
## [13] matrixStats_1.5.0 BiocStyle_2.41.0
##
## loaded via a namespace (and not attached):
## [1] RColorBrewer_1.1-3 rstudioapi_0.19.0 sys_3.4.3
## [4] jsonlite_2.0.0 shape_1.4.6.1 magrittr_2.0.5
## [7] GenomicFeatures_1.65.0 farver_2.1.2 rmarkdown_2.31
## [10] GlobalOptions_0.1.4 BiocIO_1.23.3 vctrs_0.7.3
## [13] memoise_2.0.1 Rsamtools_2.29.0 RCurl_1.98-1.19
## [16] base64enc_0.1-6 htmltools_0.5.9 S4Arrays_1.13.0
## [19] BiocBaseUtils_1.15.1 progress_1.2.3 curl_7.1.0
## [22] SparseArray_1.13.2 Formula_1.2-5 sass_0.4.10
## [25] bslib_0.11.0 htmlwidgets_1.6.4 Gviz_1.57.0
## [28] httr2_1.2.2 cachem_1.1.0 buildtools_1.0.0
## [31] GenomicAlignments_1.49.0 lifecycle_1.0.5 iterators_1.0.14
## [34] pkgconfig_2.0.3 Matrix_1.7-5 R6_2.6.1
## [37] fastmap_1.2.0 clue_0.3-68 digest_0.6.39
## [40] colorspace_2.1-2 patchwork_1.3.2 AnnotationDbi_1.75.0
## [43] Hmisc_5.2-5 RSQLite_3.53.1 filelock_1.0.3
## [46] httr_1.4.8 abind_1.4-8 compiler_4.6.0
## [49] bit64_4.8.2 doParallel_1.0.17 backports_1.5.1
## [52] htmlTable_2.5.0 S7_0.2.2 BiocParallel_1.47.0
## [55] DBI_1.3.0 biomaRt_2.69.0 rappdirs_0.3.4
## [58] DelayedArray_0.39.3 rjson_0.2.23 tools_4.6.0
## [61] foreign_0.8-91 otel_0.2.0 nnet_7.3-20
## [64] glue_1.8.1 restfulr_0.0.17 checkmate_2.3.4
## [67] cluster_2.1.8.2 gtable_0.3.6 BSgenome_1.81.0
## [70] ensembldb_2.37.3 data.table_1.18.4 hms_1.1.4
## [73] XVector_0.53.0 foreach_1.5.2 pillar_1.11.1
## [76] stringr_1.6.0 circlize_0.4.18 dplyr_1.2.1
## [79] BiocFileCache_3.3.0 lattice_0.22-9 deldir_2.0-4
## [82] rtracklayer_1.73.0 bit_4.6.0 biovizBase_1.61.0
## [85] tidyselect_1.2.1 locfit_1.5-9.12 pbapply_1.7-4
## [88] maketools_1.3.2 Biostrings_2.81.3 knitr_1.51
## [91] gridExtra_2.3 ProtGenerics_1.45.0 xfun_0.58
## [94] stringi_1.8.7 UCSC.utils_1.9.0 lazyeval_0.2.3
## [97] yaml_2.3.12 evaluate_1.0.5 codetools_0.2-20
## [100] cigarillo_1.3.0 interp_1.1-6 GenomicFiles_1.49.0
## [103] tibble_3.3.1 BiocManager_1.30.27 cli_3.6.6
## [106] rpart_4.1.27 jquerylib_0.1.4 dichromat_2.0-0.1
## [109] Rcpp_1.1.1-1.1 GenomeInfoDb_1.49.1 dbplyr_2.5.2
## [112] png_0.1-9 XML_3.99-0.23 parallel_4.6.0
## [115] ggplot2_4.0.3 blob_1.3.0 prettyunits_1.2.0
## [118] jpeg_0.1-11 latticeExtra_0.6-31 AnnotationFilter_1.37.0
## [121] bitops_1.0-9 viridisLite_0.4.3 VariantAnnotation_1.59.0
## [124] scales_1.4.0 crayon_1.5.3 GetoptLong_1.1.1
## [127] rlang_1.2.0 KEGGREST_1.53.0