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 370 287 35 23 285 1 23 5 11
gene2 712 80 2 1 16 14 7 32 80
gene3 27 97 41 205 6 25 1 12 10
gene4 204 5 45 1 73 150 3 50 212
gene5 24 34 1 92 139 92 4 393 4
gene6 5 130 36 208 62 63 5 426 1
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 90 48 166 120 280 2 33 204
gene2 106 49 1 1 4 17 2 292
gene3 240 104 25 317 100 21 12 4
gene4 170 7 15 1 2 10 14 39
gene5 185 59 128 6 67 1 31 281
gene6 56 41 1 42 24 7 260 68
sample18 sample19 sample20
gene1 1 311 5
gene2 24 51 17
gene3 31 125 2
gene4 1 308 300
gene5 195 228 7
gene6 23 46 331
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 69.35597 0.56802456 -1.46753545 -0.089513655 2
sample2 50.38482 0.22815864 -1.01807551 -0.515074329 1
sample3 34.82989 0.54694640 1.01723857 -1.014899009 1
sample4 58.56710 -1.05534149 1.78144407 -0.005354572 2
sample5 28.47121 -1.22031511 -0.07678577 -0.565775107 0
sample6 71.49422 -0.08771177 -1.82814438 1.482500718 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 94.5310 1.00004 0.3618333 0.547534 0.823121 236.122 243.092
gene2 82.4607 1.00008 1.4372652 0.230628 0.576570 204.474 211.445
gene3 51.9236 1.00005 2.2475776 0.133827 0.445625 210.933 217.903
gene4 71.5804 1.00020 0.6038443 0.437172 0.780663 217.350 224.320
gene5 76.0829 1.00038 2.3386167 0.126341 0.445625 229.154 236.124
gene6 66.7352 1.00004 0.0260124 0.871993 0.944751 221.828 228.798
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 94.5310 -0.323282 0.421095 -0.767718 0.4426547 0.812868 236.122
gene2 82.4607 0.695544 0.420415 1.654425 0.0980412 0.412828 204.474
gene3 51.9236 -0.430925 0.346482 -1.243714 0.2136047 0.485465 210.933
gene4 71.5804 0.217261 0.430220 0.504999 0.6135595 0.841110 217.350
gene5 76.0829 -0.295385 0.384230 -0.768770 0.4420298 0.812868 229.154
gene6 66.7352 -0.626479 0.354343 -1.767998 0.0770612 0.412828 221.828
BIC
<numeric>
gene1 243.092
gene2 211.445
gene3 217.903
gene4 224.320
gene5 236.124
gene6 228.798
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 94.5310 -0.551155 1.53836 -0.358273 0.720139 0.951708 236.122
gene2 82.4607 -0.308438 1.54712 -0.199362 0.841979 0.951708 204.474
gene3 51.9236 -0.137768 1.26550 -0.108864 0.913310 0.951708 210.933
gene4 71.5804 1.375868 1.58442 0.868370 0.385192 0.945544 217.350
gene5 76.0829 -0.570189 1.40461 -0.405941 0.684786 0.951708 229.154
gene6 66.7352 1.645176 1.29495 1.270452 0.203924 0.731182 221.828
BIC
<numeric>
gene1 243.092
gene2 211.445
gene3 217.903
gene4 224.320
gene5 236.124
gene6 228.798
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>
gene19 92.8030 2.25231 25.05292 0.000111098 0.00555492 186.256 194.473
gene33 78.5584 1.00009 9.65220 0.001891731 0.04729328 204.895 211.865
gene23 57.7717 1.00008 8.57337 0.003412229 0.05687049 209.513 216.483
gene36 103.9406 1.00004 7.32616 0.006797333 0.08496666 205.171 212.141
gene34 34.0246 1.00007 6.33343 0.011853821 0.11853821 193.753 200.723
gene9 42.9550 1.00013 4.88027 0.027183675 0.22653063 204.756 211.726
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.1 Patched (2025-09-10 r88807)
Platform: aarch64-apple-darwin20
Running under: macOS Ventura 13.7.7
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.1
locale:
[1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: America/New_York
tzcode source: internal
attached base packages:
[1] stats4 stats graphics grDevices utils datasets methods
[8] base
other attached packages:
[1] ggplot2_4.0.0 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
loaded via a namespace (and not attached):
[1] KEGGREST_1.50.0 gtable_0.3.6 xfun_0.53
[4] bslib_0.9.0 lattice_0.22-7 vctrs_0.6.5
[7] tools_4.5.1 parallel_4.5.1 tibble_3.3.0
[10] AnnotationDbi_1.72.0 RSQLite_2.4.3 blob_1.2.4
[13] pkgconfig_2.0.3 Matrix_1.7-4 RColorBrewer_1.1-3
[16] S7_0.2.0 lifecycle_1.0.4 compiler_4.5.1
[19] farver_2.1.2 Biostrings_2.78.0 DESeq2_1.50.0
[22] codetools_0.2-20 htmltools_0.5.8.1 sass_0.4.10
[25] yaml_2.3.10 crayon_1.5.3 pillar_1.11.1
[28] jquerylib_0.1.4 DelayedArray_0.36.0 cachem_1.1.0
[31] abind_1.4-8 nlme_3.1-168 genefilter_1.92.0
[34] tidyselect_1.2.1 locfit_1.5-9.12 digest_0.6.37
[37] dplyr_1.1.4 labeling_0.4.3 splines_4.5.1
[40] fastmap_1.2.0 grid_4.5.1 cli_3.6.5
[43] SparseArray_1.10.0 magrittr_2.0.4 S4Arrays_1.10.0
[46] survival_3.8-3 dichromat_2.0-0.1 XML_3.99-0.19
[49] withr_3.0.2 scales_1.4.0 bit64_4.6.0-1
[52] rmarkdown_2.30 XVector_0.50.0 httr_1.4.7
[55] bit_4.6.0 png_0.1-8 memoise_2.0.1
[58] evaluate_1.0.5 knitr_1.50 mgcv_1.9-3
[61] rlang_1.1.6 Rcpp_1.1.0 xtable_1.8-4
[64] glue_1.8.0 DBI_1.2.3 annotate_1.88.0
[67] jsonlite_2.0.0 R6_2.6.1