To install and load NBAMSeq
High-throughput sequencing experiments followed by differential expression analysis is a widely used approach to detect genomic biomarkers. A fundamental step in differential expression analysis is to model the association between gene counts and covariates of interest. NBAMSeq is a flexible statistical model based on the generalized additive model and allows for information sharing across genes in variance estimation. Specifically, we model the logarithm of mean gene counts as sums of smooth functions with the smoothing parameters and coefficients estimated simultaneously by a nested iteration. The variance is estimated by the Bayesian shrinkage approach to fully exploit the information across all genes.
The workflow of NBAMSeq contains three main steps:
Step 1: Data input using NBAMSeqDataSet;
Step 2: Differential expression (DE) analysis using
NBAMSeq function;
Step 3: Pulling out DE results using results
function.
Here we illustrate each of these steps respectively.
Users are expected to provide three parts of input,
i.e. countData, colData, and
design.
countData is a matrix of gene counts generated by RNASeq
experiments.
## An example of countData
n = 50 ## n stands for number of genes
m = 20 ## m stands for sample size
countData = matrix(rnbinom(n*m, mu=100, size=1/3), ncol = m) + 1
mode(countData) = "integer"
colnames(countData) = paste0("sample", 1:m)
rownames(countData) = paste0("gene", 1:n)
head(countData) sample1 sample2 sample3 sample4 sample5 sample6 sample7 sample8 sample9
gene1 362 99 30 198 50 205 1004 1 4
gene2 68 203 21 1 3 7 383 136 1
gene3 100 2 72 411 1 94 1 24 1016
gene4 75 1 5 3 239 136 1 112 159
gene5 1 2 2 247 1 14 8 42 1
gene6 99 4 161 1 58 292 84 78 327
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 1 114 137 621 86 37 55 390
gene2 1 1 59 302 4 120 1 1
gene3 2 214 107 2 3 1 14 350
gene4 154 5 1 1 257 202 8 60
gene5 91 3 1 15 611 11 133 55
gene6 6 419 75 2 124 11 28 208
sample18 sample19 sample20
gene1 4 1 66
gene2 1 3 29
gene3 133 18 1
gene4 9 944 19
gene5 2 449 1
gene6 1 1 78
colData is a data frame which contains the covariates of
samples. The sample order in colData should match the
sample order in countData.
## An example of colData
pheno = runif(m, 20, 80)
var1 = rnorm(m)
var2 = rnorm(m)
var3 = rnorm(m)
var4 = as.factor(sample(c(0,1,2), m, replace = TRUE))
colData = data.frame(pheno = pheno, var1 = var1, var2 = var2,
var3 = var3, var4 = var4)
rownames(colData) = paste0("sample", 1:m)
head(colData) pheno var1 var2 var3 var4
sample1 71.46650 -0.5844264 -0.4820340 -0.4881706 2
sample2 68.09191 -0.8131958 -0.2271750 0.6425904 2
sample3 60.89671 0.7265568 -1.0284718 -0.3172808 1
sample4 57.37460 1.3896376 0.2626334 -0.3929028 1
sample5 58.91808 -0.1657358 -0.1899554 -0.1090908 2
sample6 34.87496 -0.9335635 -1.2951848 -0.6174054 2
design is a formula which specifies how to model the
samples. Compared with other packages performing DE analysis including
DESeq2 (Love et al. 2014), edgeR (Robinson et al. 2010), NBPSeq (Di et al. 2015) and BBSeq (Zhou et al. 2011), NBAMSeq supports the
nonlinear model of covariates via mgcv (Wood and
Wood 2015). To indicate the nonlinear covariate in the model,
users are expected to use s(variable_name) in the
design formula. In our example, if we would like to model
pheno as a nonlinear covariate, the design
formula should be:
Several notes should be made regarding the design
formula:
multiple nonlinear covariates are supported,
e.g. design = ~ s(pheno) + s(var1) + var2 + var3 + var4;
the nonlinear covariate cannot be a discrete variable, e.g.
design = ~ s(pheno) + var1 + var2 + var3 + s(var4) as
var4 is a factor, and it makes no sense to model a factor
as nonlinear;
at least one nonlinear covariate should be provided in
design. If all covariates are assumed to have linear effect
on gene count, use DESeq2 (Love et al.
2014), edgeR (Robinson et al.
2010), NBPSeq (Di et al. 2015) or
BBSeq (Zhou et al. 2011) instead. e.g.
design = ~ pheno + var1 + var2 + var3 + var4 is not
supported in NBAMSeq;
design matrix is not supported.
We then construct the NBAMSeqDataSet using
countData, colData, and
design:
class: NBAMSeqDataSet
dim: 50 20
metadata(1): fitted
assays(1): counts
rownames(50): gene1 gene2 ... gene49 gene50
rowData names(0):
colnames(20): sample1 sample2 ... sample19 sample20
colData names(5): pheno var1 var2 var3 var4
Differential expression analysis can be performed by
NBAMSeq function:
Several other arguments in NBAMSeq function are
available for users to customize the analysis.
gamma argument can be used to control the smoothness
of the nonlinear function. Higher gamma means the nonlinear
function will be more smooth. See the gamma argument of gam
function in mgcv (Wood and Wood 2015) for
details. Default gamma is 2.5;
fitlin is either TRUE or
FALSE indicating whether linear model should be fitted
after fitting the nonlinear model;
parallel is either TRUE or
FALSE indicating whether parallel should be used. e.g. Run
NBAMSeq with parallel = TRUE:
Results of DE analysis can be pulled out by results
function. For continuous covariates, the name argument
should be specified indicating the covariate of interest. For nonlinear
continuous covariates, base mean, effective degrees of freedom (edf),
test statistics, p-value, and adjusted p-value will be returned.
DataFrame with 6 rows and 7 columns
baseMean edf stat pvalue padj AIC BIC
<numeric> <numeric> <numeric> <numeric> <numeric> <numeric> <numeric>
gene1 150.7387 1.00013 1.249270 0.263789 0.628069 239.461 246.431
gene2 64.5329 1.00008 0.309777 0.577916 0.760416 196.604 203.574
gene3 87.0460 1.00013 2.062489 0.151060 0.545348 214.961 221.931
gene4 83.6408 1.00005 1.688653 0.193812 0.545348 221.024 227.994
gene5 67.0265 1.00009 0.705505 0.401047 0.661271 195.399 202.369
gene6 73.4943 1.00008 0.773750 0.379115 0.661271 227.415 234.385
For linear continuous covariates, base mean, estimated coefficient, standard error, test statistics, p-value, and adjusted p-value will be returned.
DataFrame with 6 rows and 8 columns
baseMean coef SE stat pvalue padj AIC
<numeric> <numeric> <numeric> <numeric> <numeric> <numeric> <numeric>
gene1 150.7387 0.712834 0.419415 1.699592 0.0892077 0.319007 239.461
gene2 64.5329 0.243510 0.525999 0.462948 0.6434019 0.804252 196.604
gene3 87.0460 -0.458662 0.488137 -0.939618 0.3474133 0.637882 214.961
gene4 83.6408 -0.959662 0.488031 -1.966395 0.0492530 0.262808 221.024
gene5 67.0265 0.320697 0.502104 0.638706 0.5230142 0.670531 195.399
gene6 73.4943 -0.988854 0.451283 -2.191208 0.0284368 0.203120 227.415
BIC
<numeric>
gene1 246.431
gene2 203.574
gene3 221.931
gene4 227.994
gene5 202.369
gene6 234.385
For discrete covariates, the contrast argument should be
specified. e.g. contrast = c("var4", "2", "0") means
comparing level 2 vs. level 0 in var4.
DataFrame with 6 rows and 8 columns
baseMean coef SE stat pvalue padj AIC
<numeric> <numeric> <numeric> <numeric> <numeric> <numeric> <numeric>
gene1 150.7387 1.020057 1.02308 0.997048 0.3187410 0.667994 239.461
gene2 64.5329 1.388972 1.28611 1.079976 0.2801528 0.667994 196.604
gene3 87.0460 -2.210757 1.18267 -1.869291 0.0615823 0.621327 214.961
gene4 83.6408 -0.980174 1.17224 -0.836155 0.4030677 0.683528 221.024
gene5 67.0265 -1.887256 1.22779 -1.537114 0.1242654 0.621327 195.399
gene6 73.4943 -2.100280 1.09421 -1.919446 0.0549279 0.621327 227.415
BIC
<numeric>
gene1 246.431
gene2 203.574
gene3 221.931
gene4 227.994
gene5 202.369
gene6 234.385
We suggest two approaches to visualize the nonlinear associations.
The first approach is to plot the smooth components of a fitted negative
binomial additive model by plot.gam function in mgcv (Wood and Wood 2015). This can be done by
calling makeplot function and passing in
NBAMSeqDataSet object. Users are expected to provide the
phenotype of interest in phenoname argument and gene of
interest in genename argument.
## assuming we are interested in the nonlinear relationship between gene10's
## expression and "pheno"
makeplot(gsd, phenoname = "pheno", genename = "gene10", main = "gene10")In addition, to explore the nonlinear association of covariates, it is also instructive to look at log normalized counts vs. variable scatter plot. Below we show how to produce such plot.
## here we explore the most significant nonlinear association
res1 = res1[order(res1$pvalue),]
topgene = rownames(res1)[1]
sf = getsf(gsd) ## get the estimated size factors
## divide raw count by size factors to obtain normalized counts
countnorm = t(t(countData)/sf)
head(res1)DataFrame with 6 rows and 7 columns
baseMean edf stat pvalue padj AIC
<numeric> <numeric> <numeric> <numeric> <numeric> <numeric>
gene29 65.3758 1.00005 19.27253 1.10635e-05 0.000553173 192.335
gene8 68.7211 1.00009 5.86938 1.54103e-02 0.296901740 208.664
gene23 25.8957 1.00002 5.41628 1.99521e-02 0.296901740 178.977
gene41 139.3659 1.00104 5.12524 2.37521e-02 0.296901740 235.926
gene42 45.6660 1.00008 4.53856 3.31486e-02 0.331485615 195.997
gene47 35.3567 1.00004 3.34273 6.75148e-02 0.545348067 183.351
BIC
<numeric>
gene29 199.305
gene8 215.634
gene23 185.947
gene41 242.898
gene42 202.967
gene47 190.321
library(ggplot2)
setTitle = topgene
df = data.frame(pheno = pheno, logcount = log2(countnorm[topgene,]+1))
ggplot(df, aes(x=pheno, y=logcount))+geom_point(shape=19,size=1)+
geom_smooth(method='loess')+xlab("pheno")+ylab("log(normcount + 1)")+
annotate("text", x = max(df$pheno)-5, y = max(df$logcount)-1,
label = paste0("edf: ", signif(res1[topgene,"edf"],digits = 4)))+
ggtitle(setTitle)+
theme(text = element_text(size=10), plot.title = element_text(hjust = 0.5))R version 4.6.1 (2026-06-24)
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] ggplot2_4.0.3 BiocParallel_1.46.0
[3] NBAMSeq_1.28.0 SummarizedExperiment_1.42.0
[5] Biobase_2.72.0 GenomicRanges_1.64.0
[7] Seqinfo_1.2.0 IRanges_2.46.0
[9] S4Vectors_0.50.3 BiocGenerics_0.58.1
[11] generics_0.1.4 MatrixGenerics_1.24.0
[13] matrixStats_1.5.0 rmarkdown_2.32
loaded via a namespace (and not attached):
[1] KEGGREST_1.52.2 gtable_0.3.6 xfun_0.61
[4] bslib_0.12.0 lattice_0.23-1 vctrs_0.7.3
[7] tools_4.6.1 parallel_4.6.1 AnnotationDbi_1.74.0
[10] RSQLite_3.53.3 blob_1.3.0 Matrix_1.7-6
[13] RColorBrewer_1.1-3 S7_0.2.2 lifecycle_1.0.5
[16] compiler_4.6.1 farver_2.1.2 Biostrings_2.80.2
[19] DESeq2_1.52.0 codetools_0.2-20 htmltools_0.5.9
[22] sys_3.4.3 buildtools_1.0.0 sass_0.4.10
[25] yaml_2.3.12 crayon_1.5.3 jquerylib_0.1.4
[28] DelayedArray_0.38.2 cachem_1.1.0 abind_1.4-8
[31] nlme_3.1-171 genefilter_1.94.0 locfit_1.5-9.12
[34] digest_0.6.39 labeling_0.4.3 splines_4.6.1
[37] maketools_1.3.2 fastmap_1.2.0 grid_4.6.1
[40] cli_3.6.6 SparseArray_1.12.3 S4Arrays_1.12.1
[43] survival_3.8-12 XML_3.99-0.24 withr_3.0.3
[46] scales_1.4.0 bit64_4.8.6 XVector_0.52.0
[49] httr_1.4.9 bit_4.6.0 otel_0.2.0
[52] png_0.1-9 memoise_2.0.1 evaluate_1.0.5
[55] knitr_1.52 mgcv_1.9-4 rlang_1.3.0
[58] Rcpp_1.1.2 xtable_1.8-8 glue_1.8.1
[61] DBI_1.3.0 annotate_1.90.0 jsonlite_2.0.0
[64] R6_2.6.1