RNA sequencing (RNA-seq) libraries may contain genomic DNA (gDNA) contamination due to an absent or inefficient gDNA digestion step (with DNase) during RNA extraction or library preparation. In fact, some protocols do not include a DNase treatment step, or they include it as optional.
While gDNA contamination is not a major issue in libraries built with poly(A) selected RNA molecules, it can remarkably affect gene expression quantification from libraries of total RNA. When present, gDNA contamination can lead to a misleading attribution of expression to unannotated regions of the genome. For this reason, it is important to check the levels of gDNA contamination during quality control before performing further analyses, specially when total RNA has been sequenced.
Here we illustrate the use of the gDNAx package for producing different diagnostics and how do they reveal different gDNA contamination levels. We use a subset of the data in (Li et al. 2022), which consists of 9 paired-end samples of total RNA-seq with increasing levels of gDNA contamination: 0% (no contamination), 1% and 10%, with 3 replicates each. The data is available through the Bioconductor experiment data package gDNAinRNAseqData, which allows one to download 9 BAM files, containing about 100,000 alignments, sampled uniformly at random from the complete BAM files.
library(gDNAinRNAseqData)
Warning: replacing previous import 'BiocGenerics::version' by
'BiocManager::version' when loading 'AnnotationHub'
# Retrieve BAM files
bamfiles <- LiYu22subsetBAMfiles()
bamfiles
[1] "/tmp/RtmpXR3cCK/s32gDNA0.bam" "/tmp/RtmpXR3cCK/s33gDNA0.bam"
[3] "/tmp/RtmpXR3cCK/s34gDNA0.bam" "/tmp/RtmpXR3cCK/s26gDNA1.bam"
[5] "/tmp/RtmpXR3cCK/s27gDNA1.bam" "/tmp/RtmpXR3cCK/s28gDNA1.bam"
[7] "/tmp/RtmpXR3cCK/s23gDNA10.bam" "/tmp/RtmpXR3cCK/s24gDNA10.bam"
[9] "/tmp/RtmpXR3cCK/s25gDNA10.bam"
# Retrieve information on the gDNA concentrations of each BAM file
pdat <- LiYu22phenoData(bamfiles)
pdat
gDNA
s32gDNA0 0
s33gDNA0 0
s34gDNA0 0
s26gDNA1 1
s27gDNA1 1
s28gDNA1 1
s23gDNA10 10
s24gDNA10 10
s25gDNA10 10Diagnosing the presence of gDNA contamination requires using an
annotation of genes and transcripts. The gDNAx package expects
that we provide such an annotation using a so-called TxDb
package, either as a TxDb object, created once such a
package is loaded into the R session, or by specifying the name of the
package. The Bioconductor website
provides a number of TxDb packages, but if the we do not
find the one we are looking for, we can build a TxDb object
using the function makeTxDbFromGFF() on a given GFF or
GTF
file, or any of the other makeTxDbFrom*() functions,
available in the GenomicFeatures
package. Here we load the TxDb package corresponding to the
GENCODE annotation provided by the UCSC Genome Browser.
library(TxDb.Hsapiens.UCSC.hg38.knownGene)
txdb <- TxDb.Hsapiens.UCSC.hg38.knownGene
txdb
TxDb object:
# Db type: TxDb
# Supporting package: GenomicFeatures
# Data source: UCSC
# Genome: hg38
# Organism: Homo sapiens
# Taxonomy ID: 9606
# UCSC Table: knownGene
# UCSC Track: GENCODE V50
# Resource URL: https://genome.ucsc.edu/
# Type of Gene ID: Entrez Gene ID
# Full dataset: yes
# Nb of transcripts: 670670
# Db created by: txdbmaker package from Bioconductor
# Creation time: 2026-10-23 15:30:00 -0700 (Fri, 23 Oct 2026)
# txdbmaker version at creation time: 1.9.0
# RSQLite version at creation time: 3.53.3
# DBSCHEMAVERSION: 1.2We can calculate diagnostics for gDNA contamination using the
function gDNAdx() as follows.
library(gDNAx)
gdnax <- gDNAdx(bamfiles, txdb)
class(gdnax)
[1] "gDNAx"
attr(,"package")
[1] "gDNAx"
gdnax
gDNAx object
# BAM files (9): s32gDNA0.bam, ..., s25gDNA10.bam
# Library layout: paired-end, 9 (2x50nt)
# Library protocol: unstranded (9 out of 9)
# Sequences: only assembled molecules
# Annotation pkg: TxDb.Hsapiens.UCSC.hg38.knownGene
# Alignments employed: first 100000The previous call will show progress through its calculations unless
we set the argument verbose=FALSE, and return an object of
class gDNAx once it has finished. We have let the
gDNAdx() function figure out the library layout and
protocol, but if we already knew those parameters from the data, we
could set them through the arguments singleEnd and
strandMode and speed up calculations. Another way to speed
up calculations, which may be advantageous specially when analysing a
high number of BAM files, is to use the BPPARAM argument to
set a number of parallel threads of execution; see the help page of
gDNAdx() for full details on how to specify non-default
values to all these parameters.
Calling the plot() function with the resulting object
gDNAx object as the first argument will plot several
diagnostics. Here below, we also use a parameter called
group to automatically color samples, in this case, by the
gDNA contamination levels included in the experimental design of the
data; see (Li et al. 2022) for full
details on it.
Diagnostics. Default diagnostics obtained with the function
plot() on a gDNAx object.
The previous figure contains three diagnostic plots, each one showing the following values as a function of the percentage of read alignments fully contained in intergenic regions (IGC):
These data appear to come from an unstranded library, but if they would be stranded, a fourth diagnostic plot would appear showing an estimated value of the strandedness of each sample as function of the percentage of intergenic alignments. In stranded RNA-seq data, we should expect strandedness values close to 1, which imply that most reads align to the same strand than the annotated transcripts. Lower strandedness values can be indicative of gDNA contamination because reads sequenced from DNA are expected to align in equal proportions to both strands.
Because IGC alignments mainly originate from gDNA contamination, we may expect a negative correlation between the percentage of SCJ or SCE alignments and the percentage of IGC alignments. On the other hand, INT alignments may originate either from primary unprocessed transcripts in the nucleus, or from gDNA contamination as well. Therefore, we may also expect some positive correlation between the percentages of INT and IGC alignments, as it happens in this data.
Using the function getDx() on the gDNAx
object, we obtain all the values used in the diagnostics.
dx <- getDx(gdnax)
dx
IGC INT SCJ SCE JNC IGCFLM SCJFLM
s32gDNA0 1.851294 27.06301 14.34678 40.26891 19.56452 746.333 165.074
s33gDNA0 1.832608 26.94746 14.36696 40.48990 19.55383 281.373 161.672
s34gDNA0 1.891941 27.06298 14.57677 40.34669 19.70788 1153.737 161.660
s26gDNA1 2.983174 28.53017 13.97429 39.06303 18.73132 784.494 160.063
s27gDNA1 3.003338 29.34665 13.72466 38.44715 18.31765 1255.527 159.833
s28gDNA1 3.251656 29.89030 13.31932 37.95409 17.96053 773.779 161.632
s23gDNA10 9.769006 35.04399 10.65076 31.39172 14.41110 926.374 164.096
s24gDNA10 10.395435 36.02848 10.29140 30.48432 13.91748 1347.507 158.943
s25gDNA10 9.377143 36.00286 10.63476 31.21848 14.20691 1063.908 162.020
SCEFLM INTFLM STRAND
s32gDNA0 156.066 159.178 NA
s33gDNA0 165.080 171.396 NA
s34gDNA0 155.947 160.121 NA
s26gDNA1 161.639 160.839 NA
s27gDNA1 161.669 162.410 NA
s28gDNA1 162.055 165.525 NA
s23gDNA10 164.793 171.794 NA
s24gDNA10 162.596 161.127 NA
s25gDNA10 159.255 184.308 NAThe column JNC contains the percentage of alignments
that include one or more junctions, irrespective of whether those
alignments are compatible with an spliced transcript in the given
annotation. The columns with the suffix FLM contain an
estimation of the fragment length mean in the alignments originating in
the corresponding region, and the column STRAND stores the
strandedness values, which in this case are NA because this
dataset is not strand-specific.
We can directly plot the estimated fragments length distributions
with the function plotFrgLength().
Fragments length distributions. Density and location of the estimated fragments length distribution, by the origin of the alignments.
Another way to represent some of diagnostic measurements is to examine the origin of the alignments per sample in percentages. Fluctuations of these proportions across samples can help quantifying the amount of gDNA contamination per sample.
Alignment origins.
In the previous plot, OTH indicates the percentage of
read alignments that do not fall fully within any of the other
categories (SCJ, SCE, INT and
IGC), these may typically correspond to read alignments
overlapping two of these previous categories. If we are interested in
knowing exactly which annotations of intergenic and intronic regions
have been used to compute these diagnostics, we can retrieve them using
the functions getIgc() and getInt() on the
output gDNAx object, respectively.
igcann <- getIgc(gdnax)
igcann
GRanges object with 18487 ranges and 0 metadata columns:
seqnames ranges strand
<Rle> <IRanges> <Rle>
[1] chr1 1-11120 *
[2] chr1 31110-34552 *
[3] chr1 37596-65418 *
[4] chr1 71586-91176 *
[5] chr1 201245-257912 *
... ... ... ...
[18483] chrY 26305897-57067746 *
[18484] chrY 57130290-57184170 *
[18485] chrY 57208757-57212176 *
[18486] chrY 57215635-57227415 *
[18487] chrM 1-16569 *
-------
seqinfo: 25 sequences (1 circular) from hg38 genome
intann <- getInt(gdnax)
intann
GRanges object with 297470 ranges and 0 metadata columns:
seqnames ranges strand
<Rle> <IRanges> <Rle>
[1] chr1 11212-11409 *
[2] chr1 12722-12974 *
[3] chr1 13053-13220 *
[4] chr1 18062-18267 *
[5] chr1 18380-18496 *
... ... ... ...
[297466] chrY 57208313-57208518 *
[297467] chrY 57213358-57213525 *
[297468] chrY 57213603-57213855 *
[297469] chrY 57213983-57214349 *
[297470] chrY 57214731-57215366 *
-------
seqinfo: 25 sequences (1 circular) from hg38 genomeSince we have let the gDNAdx() function to estimate
strandedness, we can examine those estimated values using the getter
function strandedness() on the gDNAx
object.
strandedness(gdnax)
strandMode1 strandMode2 ambig Nalignments
s32gDNA0 0.5018379 0.4981621 0.06348602 68267
s33gDNA0 0.5016728 0.4983272 0.06435948 68366
s34gDNA0 0.5049083 0.4950917 0.06392801 68233
s26gDNA1 0.5043512 0.4956488 0.06251709 65822
s27gDNA1 0.5071014 0.4928986 0.06364071 64817
s28gDNA1 0.5010471 0.4989529 0.06344956 64240
s23gDNA10 0.5017865 0.4982135 0.06346323 52298
s24gDNA10 0.4975935 0.5024065 0.06266745 50760
s25gDNA10 0.5008642 0.4991358 0.06400108 51921Using the function classifyStrandMode() we can obtain a
classification of the most likely strand mode for each BAM file, given
some default cutoff values.
classifyStrandMode(strandedness(gdnax))
s32gDNA0 s33gDNA0 s34gDNA0 s26gDNA1 s27gDNA1 s28gDNA1 s23gDNA10 s24gDNA10
NA NA NA NA NA NA NA NA
s25gDNA10
NA Li et al. (2022) report in their
publication that “sequencing libraries were generated using a TruSeq
Stranded Total RNA Library Prep Kit”. However, we can see that the
proportion of alignments overlapping transcripts in the column
strandMode1 is very similar to the one in the column
strandMode2, which is compatible with an unstranded library
and the reason why we obtain NA values in the output of
classifyStrandMode(). We reach the same conclusion if we
use the RSeQC tool infer_experiment.py (Wang et al. 2012) and by visual inspection of
the alignment data in the Integrative Genomics Viewer (IGV) (Robinson et al. 2011).
Following the recommendations made by Signal
and Kahlke (2022), gDNAx attempts to use at least
200,000 alignments overlapping exonic regions to estimate strandedness.
In the subset of data used in this vignette, the number of alignments
used for that estimation is close to 60,000, which is the total number
of exonic alignments present in the BAM files.
If we are only interested in the estimation of strandedness values,
we can can also directly call strandedness() with a
character string vector of BAM filenames and a TxDb
annotation object; see the help page of strandedness().
We can attempt removing read alignments from putative gDNA origin
using the function gDNAtx(), which should be called with
the gDNAx object returned by gDNAdx() and a
path in the filesystem where to stored the filtered BAM files. By
default, these filtered BAM files include splice-compatible read
alignments (SCJ and SCE) that are found in a genomic window enriched for
stranded alignments. For further fine tuning of this filtering strategy
please use the function filterBAMtx().
## fbf <- filterBAMtxFlag(isSpliceCompatibleJunction=TRUE,
## isSpliceCompatibleExonic=TRUE)
## fstats <- filterBAMtx(gdnax, path=tmpdir, txflag=fbf)
## fstats
tmpdir <- tempdir()
fstats <- gDNAtx(gdnax, path=tmpdir)
fstats NALN NIGC NINT NSCJ NSCE NSTW NNCH
s32gDNA0 99660 NA NA 15134 39905 46336 340
s33gDNA0 99694 NA NA 15170 40189 46823 306
s34gDNA0 99686 NA NA 15352 40068 46529 314
s26gDNA1 99726 NA NA 14743 38586 45061 274
s27gDNA1 99456 NA NA 14466 38011 45083 544
s28gDNA1 99457 NA NA 14023 37470 43892 543
s23gDNA10 99007 NA NA 11115 30690 36424 993
s24gDNA10 99005 NA NA 10746 29716 35032 995
s25gDNA10 99156 NA NA 11103 30394 36039 844
The first column NALN corresponds to the total number of
read alignments processed. Columns NIGC to
NSCE contain the number of selected alignments from each
corresponding origin, where NA indicates that that type of
alignment was not selected for filtering. The column NSTW
corresponds to selected alignments occurring in stranded windows, and
therefore this number will be always equal or smaller than the number of
the previous columns. The column NNCH corresponds to
discarded read alignments ocurring in non-standard chromosomes.
sessionInfo()
R version 4.6.2 (2026-10-29)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 26.04.1 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.32.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] stats4 stats graphics grDevices utils datasets methods
[8] base
other attached packages:
[1] gDNAx_1.11.0
[2] TxDb.Hsapiens.UCSC.hg38.knownGene_3.24.0
[3] GenomicFeatures_1.65.0
[4] AnnotationDbi_1.75.2
[5] Biobase_2.73.2
[6] Rsamtools_2.29.0
[7] Biostrings_2.81.9
[8] XVector_0.53.0
[9] GenomicRanges_1.65.4
[10] IRanges_2.47.5
[11] S4Vectors_0.51.10
[12] Seqinfo_1.3.2
[13] BiocGenerics_0.59.13
[14] generics_0.1.4
[15] gDNAinRNAseqData_1.13.0
[16] knitr_1.52
[17] BiocStyle_2.41.0
loaded via a namespace (and not attached):
[1] tidyselect_1.2.1 dplyr_1.2.1
[3] blob_1.3.0 filelock_1.0.3
[5] bitops_1.1-0 fastmap_1.2.0
[7] RCurl_1.98-1.20 BiocFileCache_3.3.3
[9] VariantAnnotation_1.59.4 GenomicAlignments_1.49.2
[11] XML_3.99-0.25 digest_0.6.39
[13] lifecycle_1.0.5 KEGGREST_1.53.6
[15] RSQLite_3.53.3 magrittr_2.0.5
[17] compiler_4.6.2 rlang_1.3.0
[19] sass_0.4.10 tools_4.6.2
[21] plotrix_3.8-14 yaml_2.3.12
[23] rtracklayer_1.73.0 S4Arrays_1.13.2
[25] bit_4.6.0 curl_8.1.0
[27] DelayedArray_0.39.8 RColorBrewer_1.1-3
[29] abind_1.4-8 BiocParallel_1.47.0
[31] withr_3.0.3 purrr_1.2.2
[33] sys_3.4.3 grid_4.6.2
[35] ExperimentHub_3.3.2 SummarizedExperiment_1.43.0
[37] cli_3.6.6 rmarkdown_2.32
[39] crayon_1.5.3 httr_1.4.9
[41] rjson_0.2.23 BiocBaseUtils_1.15.1
[43] DBI_1.3.0 cachem_1.1.0
[45] parallel_4.6.2 BiocManager_1.30.27
[47] restfulr_0.0.17 matrixStats_1.5.0
[49] vctrs_0.7.3 Matrix_1.7-6
[51] jsonlite_2.0.0 bit64_4.8.6
[53] maketools_1.3.2 GenomicFiles_1.49.0
[55] jquerylib_0.1.4 glue_1.8.1
[57] codetools_0.2-20 BiocVersion_3.24.0
[59] GenomeInfoDb_1.49.2 BiocIO_1.23.3
[61] UCSC.utils_1.9.0 tibble_3.3.1
[63] pillar_1.11.1 rappdirs_0.3.4
[65] htmltools_0.5.9 BSgenome_1.81.1
[67] R6_2.6.1 dbplyr_2.6.0
[69] httr2_1.3.0 lattice_0.23-1
[71] evaluate_1.0.5 AnnotationHub_4.3.3
[73] png_0.1-9 cigarillo_1.3.1
[75] memoise_2.0.1 bslib_0.12.0
[77] SparseArray_1.13.4 xfun_0.62
[79] MatrixGenerics_1.25.0 buildtools_1.0.0
[81] pkgconfig_2.0.3