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 2 14 3 107 139 566 4 152 333
gene2 17 120 232 44 64 172 1 82 25
gene3 195 3 164 89 1 10 66 577 1
gene4 996 15 16 55 1 42 28 31 78
gene5 26 6 10 63 10 138 9 205 21
gene6 165 9 52 113 66 33 489 15 3
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 53 3 64 63 108 2 4 18
gene2 118 8 325 144 2 10 6 11
gene3 1 13 22 1 271 15 4 1
gene4 9 13 610 400 44 1 3 10
gene5 1 136 19 2 6 855 1 94
gene6 20 317 1 330 3 4 7 9
sample18 sample19 sample20
gene1 2 23 42
gene2 9 7 320
gene3 50 76 1
gene4 221 184 34
gene5 1 25 168
gene6 104 2 56
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 47.47630 -0.50844573 0.8444654 1.7657766 2
sample2 27.09050 3.00503740 -1.1390582 -0.3297468 1
sample3 45.10318 0.67057257 -0.2991542 -1.0058662 2
sample4 66.43527 -0.64650174 -0.2173750 -0.6524994 0
sample5 52.81253 -0.08645734 1.3812596 -0.1688391 0
sample6 73.25089 -0.42856981 -0.5161098 -0.3328764 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 81.5905 1.00011 4.250535345 0.0392339 0.217966 220.821 227.791
gene2 81.7964 1.00006 0.083289376 0.7729888 0.962631 227.037 234.007
gene3 57.3312 1.00021 0.514968064 0.4732035 0.845006 202.164 209.134
gene4 106.7823 1.00010 0.186583574 0.6658814 0.962631 233.508 240.478
gene5 81.8982 1.00007 0.000342507 0.9870132 0.988737 208.308 215.278
gene6 73.9153 1.00017 0.994122571 0.3188422 0.693135 224.998 231.968
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 81.5905 -0.0917766 0.316776 -0.289721 0.77202980 0.9223307 220.821
gene2 81.7964 0.3209239 0.298478 1.075201 0.28228493 0.5818009 227.037
gene3 57.3312 -0.9287660 0.327820 -2.833161 0.00460901 0.0749855 202.164
gene4 106.7823 0.1178443 0.314359 0.374872 0.70775572 0.9073791 233.508
gene5 81.8982 -0.8746992 0.298906 -2.926332 0.00342984 0.0749855 208.308
gene6 73.9153 -0.2429122 0.324901 -0.747650 0.45467103 0.7116158 224.998
BIC
<numeric>
gene1 227.791
gene2 234.007
gene3 209.134
gene4 240.478
gene5 215.278
gene6 231.968
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 81.5905 0.00487856 1.013834 0.0048120 0.9961606 0.996161 220.821
gene2 81.7964 -0.02766236 0.958122 -0.0288715 0.9769671 0.996161 227.037
gene3 57.3312 2.10487296 1.040255 2.0234195 0.0430299 0.269747 202.164
gene4 106.7823 -0.60462447 1.006321 -0.6008264 0.5479556 0.899988 233.508
gene5 81.8982 2.31029578 0.958465 2.4104119 0.0159345 0.199181 208.308
gene6 73.9153 -0.51812941 1.040175 -0.4981174 0.6184013 0.899988 224.998
BIC
<numeric>
gene1 227.791
gene2 234.007
gene3 209.134
gene4 240.478
gene5 215.278
gene6 231.968
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>
gene9 43.8037 1.00006 12.08903 0.000507588 0.0169980 179.439 186.410
gene31 35.8852 1.00010 11.54452 0.000679919 0.0169980 182.119 189.089
gene30 98.8739 1.00008 8.79875 0.003015749 0.0455271 225.777 232.747
gene19 100.5479 1.00005 8.45476 0.003642166 0.0455271 227.430 234.400
gene32 38.8564 1.29690 6.82945 0.010638396 0.1063840 186.422 193.688
gene38 63.2716 1.00013 6.07410 0.013726559 0.1143880 209.216 216.186
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.5.2 (2025-10-31)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 24.04.3 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=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: 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.1 BiocParallel_1.44.0
[3] NBAMSeq_1.26.0 SummarizedExperiment_1.40.0
[5] Biobase_2.70.0 GenomicRanges_1.62.0
[7] Seqinfo_1.0.0 IRanges_2.44.0
[9] S4Vectors_0.48.0 BiocGenerics_0.56.0
[11] generics_0.1.4 MatrixGenerics_1.22.0
[13] matrixStats_1.5.0 rmarkdown_2.30
loaded via a namespace (and not attached):
[1] KEGGREST_1.50.0 gtable_0.3.6 xfun_0.54
[4] bslib_0.9.0 lattice_0.22-7 vctrs_0.6.5
[7] tools_4.5.2 parallel_4.5.2 AnnotationDbi_1.72.0
[10] RSQLite_2.4.4 blob_1.2.4 Matrix_1.7-4
[13] RColorBrewer_1.1-3 S7_0.2.1 lifecycle_1.0.4
[16] compiler_4.5.2 farver_2.1.2 Biostrings_2.78.0
[19] DESeq2_1.50.2 codetools_0.2-20 htmltools_0.5.8.1
[22] sys_3.4.3 buildtools_1.0.0 sass_0.4.10
[25] yaml_2.3.10 crayon_1.5.3 jquerylib_0.1.4
[28] DelayedArray_0.36.0 cachem_1.1.0 abind_1.4-8
[31] nlme_3.1-168 genefilter_1.92.0 locfit_1.5-9.12
[34] digest_0.6.38 labeling_0.4.3 splines_4.5.2
[37] maketools_1.3.2 fastmap_1.2.0 grid_4.5.2
[40] cli_3.6.5 SparseArray_1.10.2 S4Arrays_1.10.0
[43] survival_3.8-3 XML_3.99-0.20 withr_3.0.2
[46] scales_1.4.0 bit64_4.6.0-1 XVector_0.50.0
[49] httr_1.4.7 bit_4.6.0 png_0.1-8
[52] memoise_2.0.1 evaluate_1.0.5 knitr_1.50
[55] mgcv_1.9-4 rlang_1.1.6 Rcpp_1.1.0
[58] xtable_1.8-4 glue_1.8.0 DBI_1.2.3
[61] annotate_1.88.0 jsonlite_2.0.0 R6_2.6.1