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 78 1 534 1 1 5 158 3 12
gene2 14 375 23 1 106 1 7 80 355
gene3 808 1 61 49 440 518 29 587 177
gene4 124 13 2 22 239 122 8 8 95
gene5 51 92 1 189 58 246 108 16 5
gene6 55 1 25 1 20 17 510 70 203
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 238 58 2 4 71 38 7 1
gene2 31 1 69 1 4 73 2 18
gene3 349 36 258 3 1 1 280 16
gene4 15 2 49 5 1 10 42 1
gene5 63 21 2 57 1 16 251 52
gene6 31 61 3 336 4 1 75 59
sample18 sample19 sample20
gene1 48 203 2
gene2 13 136 6
gene3 1 3 177
gene4 1467 297 1
gene5 181 355 2
gene6 61 43 37
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 56.26184 0.4651506 -0.38889571 0.74250174 0
sample2 60.19626 1.4625902 -0.07242863 0.04894054 0
sample3 79.84873 1.1603969 -0.10579025 -1.19299103 2
sample4 21.11003 -1.0029060 -0.84281128 0.91089799 2
sample5 22.39980 0.9917519 1.02071391 -0.17346598 0
sample6 25.89588 -0.1541380 -0.29073336 0.08104339 1
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 62.5795 1.00030 0.08912393 0.765908 0.870350 200.844 207.815
gene2 49.2855 1.00009 0.00754922 0.931163 0.977512 209.057 216.027
gene3 147.6528 1.00006 0.84222951 0.358801 0.578711 244.713 251.683
gene4 70.2925 1.00007 0.47257226 0.491830 0.683097 210.137 217.107
gene5 68.1473 1.00008 0.16779685 0.682166 0.852554 227.500 234.470
gene6 62.5698 1.00006 0.19525218 0.658574 0.852554 216.654 223.624
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 62.5795 1.193725 0.482201 2.475573 0.0133022 0.137443 200.844
gene2 49.2855 0.211859 0.498410 0.425069 0.6707862 0.859982 209.057
gene3 147.6528 -0.645195 0.491997 -1.311380 0.1897294 0.493094 244.713
gene4 70.2925 -0.602794 0.471338 -1.278901 0.2009319 0.493094 210.137
gene5 68.1473 -0.212429 0.448893 -0.473230 0.6360493 0.859982 227.500
gene6 62.5698 1.032723 0.429373 2.405189 0.0161641 0.137443 216.654
BIC
<numeric>
gene1 207.815
gene2 216.027
gene3 251.683
gene4 217.107
gene5 234.470
gene6 223.624
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 62.5795 2.2770307 1.021623 2.228837 0.0258247 0.258247 200.844
gene2 49.2855 -0.0408023 1.059498 -0.038511 0.9692802 0.989061 209.057
gene3 147.6528 -1.4016881 1.045345 -1.340886 0.1799575 0.562367 244.713
gene4 70.2925 -2.4125740 1.002978 -2.405411 0.0161543 0.258247 210.137
gene5 68.1473 -0.4423372 0.955427 -0.462973 0.6433837 0.953164 227.500
gene6 62.5698 1.7687594 0.908419 1.947075 0.0515257 0.368041 216.654
BIC
<numeric>
gene1 207.815
gene2 216.027
gene3 251.683
gene4 217.107
gene5 234.470
gene6 223.624
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>
gene50 103.2176 1.00005 10.32925 0.00130974 0.0416764 222.568 229.538
gene29 112.0500 1.00004 9.21630 0.00239955 0.0416764 215.376 222.346
gene8 169.0703 1.00004 9.14092 0.00250059 0.0416764 227.629 234.599
gene19 117.1119 1.00006 6.47899 0.01091870 0.1248261 234.559 241.529
gene43 68.6785 1.00018 6.17377 0.01296791 0.1248261 205.487 212.457
gene22 111.9889 1.00018 5.70075 0.01697420 0.1248261 244.531 251.502
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.47.0
[3] NBAMSeq_1.29.0 SummarizedExperiment_1.43.0
[5] Biobase_2.73.2 GenomicRanges_1.65.4
[7] Seqinfo_1.3.2 IRanges_2.47.5
[9] S4Vectors_0.51.10 BiocGenerics_0.59.12
[11] generics_0.1.4 MatrixGenerics_1.25.0
[13] matrixStats_1.5.0 rmarkdown_2.32
loaded via a namespace (and not attached):
[1] KEGGREST_1.53.6 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.75.2
[10] RSQLite_3.53.3 blob_1.3.0 BiocBaseUtils_1.15.1
[13] Matrix_1.7-6 RColorBrewer_1.1-3 S7_0.2.2
[16] lifecycle_1.0.5 compiler_4.6.1 farver_2.1.2
[19] Biostrings_2.81.9 DESeq2_1.53.3 codetools_0.2-20
[22] htmltools_0.5.9 sys_3.4.3 buildtools_1.0.0
[25] sass_0.4.10 yaml_2.3.12 crayon_1.5.3
[28] jquerylib_0.1.4 DelayedArray_0.39.6 cachem_1.1.0
[31] abind_1.4-8 nlme_3.1-171 genefilter_1.95.0
[34] locfit_1.5-9.12 digest_0.6.39 labeling_0.4.3
[37] splines_4.6.1 maketools_1.3.2 fastmap_1.2.0
[40] grid_4.6.1 cli_3.6.6 SparseArray_1.13.2
[43] S4Arrays_1.13.0 survival_3.8-12 XML_3.99-0.24
[46] withr_3.0.3 scales_1.4.0 bit64_4.8.6
[49] XVector_0.53.0 httr_1.4.9 bit_4.6.0
[52] otel_0.2.0 png_0.1-9 memoise_2.0.1
[55] evaluate_1.0.5 knitr_1.52 mgcv_1.9-4
[58] rlang_1.3.0 Rcpp_1.1.2 xtable_1.8-8
[61] glue_1.8.1 DBI_1.3.0 annotate_1.91.0
[64] jsonlite_2.0.0 R6_2.6.1