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 48 27 299 4 404 1 2 34 5
gene2 36 41 633 66 11 62 2 1 3
gene3 1 35 19 4 1 10 19 57 35
gene4 92 363 24 11 8 285 32 3 1
gene5 1 152 148 211 12 615 578 550 204
gene6 2 1 96 48 2 711 12 1 1
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 8 674 300 14 116 2 12 2
gene2 458 292 167 2 311 3 28 1
gene3 1 4 85 7 8 88 16 150
gene4 322 453 105 1 171 15 87 182
gene5 5 50 1 158 38 77 13 82
gene6 164 200 90 2 1 122 64 304
sample18 sample19 sample20
gene1 144 19 1
gene2 1 1 15
gene3 11 313 3
gene4 9 10 138
gene5 10 2 34
gene6 46 1 12
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 65.12517 -0.73290905 0.84833811 -0.03132002 1
sample2 26.93364 1.08637081 -1.12423817 0.93220515 1
sample3 30.25389 1.21737733 -0.12924792 0.30280197 2
sample4 79.06370 0.09108255 -0.09590579 0.08805205 0
sample5 47.23965 -0.84390970 -0.24917765 -1.12016859 2
sample6 37.31781 0.92537056 0.17455408 0.36954553 2
design is a formula which specifies how to model the
samples. Compared with other packages performing DE analysis including
DESeq2 (Love, Huber, and Anders 2014),
edgeR (Robinson, McCarthy, and Smyth
2010), NBPSeq (Di et al. 2015) and
BBSeq (Zhou, Xia, and Wright 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, Huber, and
Anders 2014), edgeR (Robinson, McCarthy,
and Smyth 2010), NBPSeq (Di et al.
2015) or BBSeq (Zhou, Xia, and Wright
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 106.6096 1.00010 2.7120563 0.0996104 0.415043 215.697 222.667
gene2 102.4462 1.00021 0.1685822 0.6817105 0.927391 216.432 223.402
gene3 34.3576 1.00013 0.0361979 0.8492624 0.948925 190.594 197.565
gene4 89.5404 1.00006 1.0738348 0.3001222 0.714577 234.762 241.732
gene5 126.7458 1.00007 2.7886848 0.0949470 0.415043 242.256 249.226
gene6 73.2106 1.00009 0.4075008 0.5233621 0.840608 208.169 215.139
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 106.6096 0.5577007 0.374912 1.487550 0.1368695 0.526421 215.697
gene2 102.4462 0.0874926 0.404528 0.216283 0.8287672 0.957137 216.432
gene3 34.3576 0.6040517 0.322835 1.871086 0.0613331 0.306666 190.594
gene4 89.5404 0.1075752 0.348677 0.308524 0.7576836 0.957137 234.762
gene5 126.7458 0.2744742 0.355863 0.771293 0.4405336 0.957137 242.256
gene6 73.2106 0.2647825 0.375057 0.705980 0.4802005 0.957137 208.169
BIC
<numeric>
gene1 222.667
gene2 223.402
gene3 197.565
gene4 241.732
gene5 249.226
gene6 215.139
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 106.6096 2.633160 0.970895 2.712096 0.00668593 0.0574923 215.697
gene2 102.4462 2.801214 1.041404 2.689844 0.00714855 0.0574923 216.432
gene3 34.3576 -0.135566 0.827862 -0.163755 0.86992415 0.8699242 190.594
gene4 89.5404 0.850374 0.896506 0.948543 0.34285308 0.5529888 234.762
gene5 126.7458 -1.946942 0.914396 -2.129210 0.03323690 0.1454734 242.256
gene6 73.2106 2.552009 0.963018 2.650011 0.00804893 0.0574923 208.169
BIC
<numeric>
gene1 222.667
gene2 223.402
gene3 197.565
gene4 241.732
gene5 249.226
gene6 215.139
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 BIC
<numeric> <numeric> <numeric> <numeric> <numeric> <numeric> <numeric>
gene14 136.3102 1.00007 9.52636 0.00202677 0.0568071 233.290 240.260
gene46 63.2203 1.00012 9.31649 0.00227228 0.0568071 197.031 204.001
gene7 116.4514 1.00009 7.20016 0.00729431 0.1215719 218.321 225.291
gene47 81.9449 1.00005 6.57605 0.01033937 0.1292421 219.757 226.727
gene40 101.9403 1.00005 4.96578 0.02586274 0.2406028 222.982 229.952
gene16 82.7865 1.00006 4.77550 0.02887234 0.2406028 204.381 211.352
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 24.04.5 LTS
Matrix products: default
BLAS: /home/biocbuild/bbs-3.24-bioc/R/lib/libRblas.so
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0 LAPACK version 3.12.0
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_GB LC_COLLATE=C
[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: America/New_York
tzcode source: system (glibc)
attached base packages:
[1] stats4 stats graphics grDevices utils datasets methods
[8] base
other attached packages:
[1] ggplot2_4.0.4 BiocParallel_1.48.0
[3] NBAMSeq_1.30.0 SummarizedExperiment_1.44.0
[5] Biobase_2.74.0 GenomicRanges_1.66.0
[7] Seqinfo_1.4.0 IRanges_2.48.0
[9] S4Vectors_0.52.0 BiocGenerics_0.60.0
[11] generics_0.1.4 MatrixGenerics_1.26.0
[13] matrixStats_1.5.0
loaded via a namespace (and not attached):
[1] KEGGREST_1.54.0 gtable_0.3.6 xfun_0.62
[4] bslib_0.12.0 lattice_0.23-1 vctrs_0.7.3
[7] tools_4.6.1 parallel_4.6.1 tibble_3.3.1
[10] AnnotationDbi_1.76.0 RSQLite_3.53.3 blob_1.3.0
[13] BiocBaseUtils_1.16.0 pkgconfig_2.0.3 Matrix_1.7-6
[16] RColorBrewer_1.1-3 S7_1.0.0 lifecycle_1.0.5
[19] compiler_4.6.1 farver_2.1.2 Biostrings_2.82.0
[22] DESeq2_1.54.0 codetools_0.2-20 htmltools_0.5.9
[25] sass_0.5.0 yaml_2.3.12 crayon_1.5.3
[28] pillar_1.11.1 jquerylib_0.1.4 DelayedArray_0.40.0
[31] cachem_1.1.0 abind_1.4-8 nlme_3.1-172
[34] genefilter_1.96.0 tidyselect_1.2.1 locfit_1.5-9.12
[37] digest_0.6.39 dplyr_1.2.1 labeling_0.4.3
[40] splines_4.6.1 fastmap_1.2.0 grid_4.6.1
[43] cli_3.6.6 SparseArray_1.14.0 magrittr_2.0.5
[46] S4Arrays_1.14.0 survival_3.8-13 dichromat_2.0-1
[49] XML_3.99-0.25 withr_3.0.3 scales_1.4.0
[52] bit64_4.8.6 rmarkdown_2.32 XVector_0.54.0
[55] httr_1.4.9 bit_4.6.0 otel_0.3.0
[58] png_0.1-9 memoise_2.0.1 evaluate_1.0.5
[61] knitr_1.52 mgcv_1.9-5 rlang_1.3.0
[64] Rcpp_1.1.2 xtable_1.8-8 glue_1.8.1
[67] DBI_1.3.0 annotate_1.92.0 jsonlite_2.0.0
[70] R6_2.6.1