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 40 1 3 27 8 5 9 353 1
gene2 23 42 23 1 245 93 240 457 56
gene3 56 1 60 2 92 261 210 1 1
gene4 4 500 91 136 32 1 85 99 359
gene5 1 139 2 52 1 394 14 105 110
gene6 814 228 4 5 22 24 214 118 32
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 92 7 1 4 19 16 704 40
gene2 1 5 897 150 12 311 1 92
gene3 1 174 22 231 1 274 1 309
gene4 85 48 122 141 400 137 1 6
gene5 3 8 248 74 58 2 388 1
gene6 119 54 577 204 236 169 256 9
sample18 sample19 sample20
gene1 181 599 1
gene2 278 197 5
gene3 29 676 92
gene4 58 1239 3
gene5 133 245 62
gene6 1 13 320
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 21.40170 0.2714576 -1.22550991 -0.80369325 2
sample2 60.30619 -0.3704915 1.91007235 -0.97565825 2
sample3 51.24872 -0.8944761 0.72315187 0.14447035 2
sample4 21.63642 1.4497722 -0.33343934 0.04347379 1
sample5 66.67100 0.7515454 0.29603416 0.20625632 2
sample6 64.95039 -0.5050200 -0.05671248 1.10128147 1
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 69.3705 1.00011 1.863138 0.17230701 0.5067853 208.396 215.366
gene2 116.6821 1.00089 1.074481 0.30066999 0.6354451 242.275 249.246
gene3 98.8326 1.00007 1.242828 0.26495947 0.6354451 224.019 230.989
gene4 167.8775 1.00026 0.102611 0.74913962 0.8512950 248.467 255.437
gene5 99.0039 1.00014 8.244383 0.00409077 0.0511347 226.475 233.446
gene6 143.6720 1.00006 0.663242 0.41545485 0.6924248 249.327 256.297
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 69.3705 0.252698 0.418987 0.603117 0.5464306 0.843203 208.396
gene2 116.6821 -0.127479 0.398194 -0.320142 0.7488603 0.843203 242.275
gene3 98.8326 -0.906677 0.415637 -2.181415 0.0291528 0.291528 224.019
gene4 167.8775 0.218816 0.393649 0.555865 0.5783031 0.843203 248.467
gene5 99.0039 0.372228 0.403561 0.922357 0.3563422 0.809834 226.475
gene6 143.6720 0.120045 0.359582 0.333846 0.7384955 0.843203 249.327
BIC
<numeric>
gene1 215.366
gene2 249.246
gene3 230.989
gene4 255.437
gene5 233.446
gene6 256.297
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 69.3705 -2.1145181 1.030996 -2.050947 0.0402721 0.287658 208.396
gene2 116.6821 1.1578585 0.979572 1.182005 0.2372037 0.697658 242.275
gene3 98.8326 0.5945500 1.017445 0.584356 0.5589808 0.901582 224.019
gene4 167.8775 -2.0956957 0.968615 -2.163601 0.0304950 0.287658 248.467
gene5 99.0039 -2.1051857 0.996254 -2.113101 0.0345921 0.287658 226.475
gene6 143.6720 -0.0928946 0.884693 -0.105002 0.9163742 0.988393 249.327
BIC
<numeric>
gene1 215.366
gene2 249.246
gene3 230.989
gene4 255.437
gene5 233.446
gene6 256.297
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 92.0704 1.00003 9.93405 0.00162286 0.0511347 227.721 234.691
gene7 60.1714 1.00007 9.19684 0.00242562 0.0511347 197.199 204.169
gene18 124.7000 1.00007 8.66168 0.00324995 0.0511347 236.425 243.395
gene5 99.0039 1.00014 8.24438 0.00409077 0.0511347 226.475 233.446
gene16 110.7169 1.00007 7.16551 0.00743419 0.0627205 221.779 228.749
gene22 99.7123 1.00007 7.14340 0.00752646 0.0627205 229.479 236.449
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 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows Server 2022 x64 (build 20348)
Matrix products: default
LAPACK version 3.12.1
locale:
[1] LC_COLLATE=C
[2] LC_CTYPE=English_United States.utf8
[3] LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8
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.0 SummarizedExperiment_1.38.0
[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.9 blob_1.2.4
[13] pkgconfig_2.0.3 Matrix_1.7-3 lifecycle_1.0.4
[16] GenomeInfoDbData_1.2.14 farver_2.1.2 compiler_4.5.0
[19] Biostrings_2.76.0 munsell_0.5.1 DESeq2_1.48.0
[22] codetools_0.2-20 snow_0.4-4 htmltools_0.5.8.1
[25] sass_0.4.10 yaml_2.3.10 pillar_1.10.2
[28] crayon_1.5.3 jquerylib_0.1.4 DelayedArray_0.34.0
[31] cachem_1.1.0 abind_1.4-8 nlme_3.1-168
[34] genefilter_1.90.0 tidyselect_1.2.1 locfit_1.5-9.12
[37] digest_0.6.37 dplyr_1.1.4 labeling_0.4.3
[40] splines_4.5.0 fastmap_1.2.0 grid_4.5.0
[43] colorspace_2.1-1 cli_3.6.4 SparseArray_1.8.0
[46] magrittr_2.0.3 S4Arrays_1.8.0 survival_3.8-3
[49] XML_3.99-0.18 withr_3.0.2 scales_1.3.0
[52] UCSC.utils_1.4.0 bit64_4.6.0-1 rmarkdown_2.29
[55] XVector_0.48.0 httr_1.4.7 bit_4.6.0
[58] png_0.1-8 memoise_2.0.1 evaluate_1.0.3
[61] knitr_1.50 mgcv_1.9-3 rlang_1.1.6
[64] Rcpp_1.0.14 xtable_1.8-4 glue_1.8.0
[67] DBI_1.2.3 annotate_1.86.0 jsonlite_2.0.0
[70] 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.