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 175 12 1 13 6 121 1 156 204
gene2 15 17 54 184 43 59 3 41 9
gene3 14 1 183 6 80 14 13 474 8
gene4 348 15 12 38 14 42 1 8 2
gene5 11 9 409 30 12 14 191 210 3
gene6 10 1 19 2 30 9 55 1 57
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 1 3 871 75 242 1 2 167
gene2 157 44 1 7 141 3 196 126
gene3 11 96 5 37 67 1 38 72
gene4 158 1 13 8 6 2 447 2
gene5 35 44 2 243 16 64 11 4
gene6 9 51 1149 19 678 62 413 390
sample18 sample19 sample20
gene1 373 1 32
gene2 1 67 9
gene3 41 118 450
gene4 3 9 20
gene5 256 99 801
gene6 22 1 1
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.34094 -0.6100957 0.4672050 -1.8132724 0
sample2 33.65232 -1.0686367 -0.6364292 2.0382729 0
sample3 59.39372 0.3640429 -1.1113236 -0.6408232 0
sample4 58.16883 -1.2463602 0.9636996 -0.6537368 2
sample5 35.44490 1.6482213 1.8124296 -1.6471270 2
sample6 57.03579 -0.1139685 -0.9044815 -2.2998239 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 116.7038 1.00021 0.527886 0.4675202 0.745858 222.765 229.736
gene2 41.7594 1.00010 0.141216 0.7071735 0.841873 207.565 214.535
gene3 93.2705 1.00008 2.539663 0.1110453 0.384265 220.148 227.118
gene4 53.4731 1.00011 4.414956 0.0356318 0.234685 192.249 199.219
gene5 140.9416 1.00004 5.096815 0.0239747 0.234685 226.435 233.406
gene6 116.8702 1.09666 0.674096 0.4177218 0.745858 225.040 232.106
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 116.7038 -0.4820176 0.421731 -1.142951 0.2530589 0.524326 222.765
gene2 41.7594 0.2408847 0.317848 0.757861 0.4485345 0.706055 207.565
gene3 93.2705 0.3885797 0.346546 1.121293 0.2621632 0.524326 220.148
gene4 53.4731 -0.0715292 0.378735 -0.188863 0.8501999 0.944667 192.249
gene5 140.9416 -0.3738109 0.330085 -1.132467 0.2574380 0.524326 226.435
gene6 116.8702 0.9495943 0.421223 2.254376 0.0241725 0.172661 225.040
BIC
<numeric>
gene1 229.736
gene2 214.535
gene3 227.118
gene4 199.219
gene5 233.406
gene6 232.106
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 116.7038 0.0913159 0.966148 0.0945154 0.92469974 0.9643575 222.765
gene2 41.7594 -0.0560547 0.724322 -0.0773892 0.93831393 0.9643575 207.565
gene3 93.2705 -0.4331158 0.795779 -0.5442663 0.58625820 0.8323247 220.148
gene4 53.4731 -1.3277877 0.866444 -1.5324561 0.12540991 0.4309844 192.249
gene5 140.9416 -2.2274554 0.758764 -2.9356353 0.00332865 0.0832163 226.435
gene6 116.8702 1.1129741 0.966173 1.1519411 0.24934529 0.5194693 225.040
BIC
<numeric>
gene1 229.736
gene2 214.535
gene3 227.118
gene4 199.219
gene5 233.406
gene6 232.106
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>
gene12 101.4883 1.00005 9.69756 0.00184606 0.0617439 213.809 220.779
gene37 22.9803 1.00008 8.89230 0.00286565 0.0617439 173.911 180.881
gene35 60.3622 1.00004 8.42384 0.00370464 0.0617439 177.780 184.751
gene14 82.5013 1.00008 7.06090 0.00788173 0.0985216 198.091 205.061
gene5 140.9416 1.00004 5.09681 0.02397468 0.2346855 226.435 233.406
gene25 43.2938 1.00012 4.79149 0.02861362 0.2346855 208.615 215.585
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.0 RC (2025-04-04 r88126)
Platform: x86_64-apple-darwin20
Running under: macOS Monterey 12.7.6
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.5-x86_64/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_3.5.2 BiocParallel_1.42.0
[3] NBAMSeq_1.24.1 SummarizedExperiment_1.38.1
[5] Biobase_2.68.0 GenomicRanges_1.60.0
[7] GenomeInfoDb_1.44.0 IRanges_2.42.0
[9] S4Vectors_0.46.0 BiocGenerics_0.54.0
[11] generics_0.1.3 MatrixGenerics_1.20.0
[13] matrixStats_1.5.0
loaded via a namespace (and not attached):
[1] KEGGREST_1.48.0 gtable_0.3.6 xfun_0.52
[4] bslib_0.9.0 lattice_0.22-7 vctrs_0.6.5
[7] tools_4.5.0 parallel_4.5.0 tibble_3.2.1
[10] AnnotationDbi_1.70.0 RSQLite_2.3.11 blob_1.2.4
[13] pkgconfig_2.0.3 Matrix_1.7-3 RColorBrewer_1.1-3
[16] lifecycle_1.0.4 GenomeInfoDbData_1.2.14 compiler_4.5.0
[19] farver_2.1.2 Biostrings_2.76.0 DESeq2_1.48.1
[22] codetools_0.2-20 htmltools_0.5.8.1 sass_0.4.10
[25] yaml_2.3.10 pillar_1.10.2 crayon_1.5.3
[28] jquerylib_0.1.4 DelayedArray_0.34.1 cachem_1.1.0
[31] abind_1.4-8 nlme_3.1-168 genefilter_1.90.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.0
[40] fastmap_1.2.0 grid_4.5.0 cli_3.6.5
[43] SparseArray_1.8.0 magrittr_2.0.3 S4Arrays_1.8.0
[46] survival_3.8-3 dichromat_2.0-0.1 XML_3.99-0.18
[49] withr_3.0.2 scales_1.4.0 UCSC.utils_1.4.0
[52] bit64_4.6.0-1 rmarkdown_2.29 XVector_0.48.0
[55] httr_1.4.7 bit_4.6.0 png_0.1-8
[58] memoise_2.0.1 evaluate_1.0.3 knitr_1.50
[61] mgcv_1.9-3 rlang_1.1.6 Rcpp_1.0.14
[64] xtable_1.8-4 glue_1.8.0 DBI_1.2.3
[67] annotate_1.86.0 jsonlite_2.0.0 R6_2.6.1
Di, Y, DW Schafer, JS Cumbie, and JH Chang. 2015. “NBPSeq: Negative Binomial Models for Rna-Sequencing Data.” R Package Version 0.3. 0, URL Http://CRAN. R-Project. Org/Package= NBPSeq.
Love, Michael I, Wolfgang Huber, and Simon Anders. 2014. “Moderated Estimation of Fold Change and Dispersion for Rna-Seq Data with Deseq2.” Genome Biology 15 (12): 550.
Robinson, Mark D, Davis J McCarthy, and Gordon K Smyth. 2010. “EdgeR: A Bioconductor Package for Differential Expression Analysis of Digital Gene Expression Data.” Bioinformatics 26 (1): 139–40.
Wood, Simon, and Maintainer Simon Wood. 2015. “Package ’Mgcv’.” R Package Version 1: 29.
Zhou, Yi-Hui, Kai Xia, and Fred A Wright. 2011. “A Powerful and Flexible Approach to the Analysis of Rna Sequence Count Data.” Bioinformatics 27 (19): 2672–8.