To install and load NBAMSeq
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
::install("NBAMSeq") BiocManager
library(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
= 50 ## n stands for number of genes
n = 20 ## m stands for sample size
m = matrix(rnbinom(n*m, mu=100, size=1/3), ncol = m) + 1
countData 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 3 1100 167 72 238 232 74 108 497
gene2 12 1 131 60 14 13 11 140 54
gene3 12 209 334 21 175 20 460 171 46
gene4 13 54 3 9 38 588 4 1 5
gene5 57 148 2 52 150 36 3 28 24
gene6 38 111 378 178 88 151 23 326 1
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 13 1 3 2 68 1 14 1
gene2 2 6 52 940 192 73 1 12
gene3 1 1 26 201 2 13 23 113
gene4 1 7 8 445 1 62 44 1
gene5 489 27 2 18 11 157 115 174
gene6 21 399 191 1024 1 16 109 197
sample18 sample19 sample20
gene1 353 1 24
gene2 18 27 3
gene3 45 135 348
gene4 144 1 1
gene5 59 33 10
gene6 20 145 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
= runif(m, 20, 80)
pheno = rnorm(m)
var1 = rnorm(m)
var2 = rnorm(m)
var3 = as.factor(sample(c(0,1,2), m, replace = TRUE))
var4 = data.frame(pheno = pheno, var1 = var1, var2 = var2,
colData var3 = var3, var4 = var4)
rownames(colData) = paste0("sample", 1:m)
head(colData)
pheno var1 var2 var3 var4
sample1 60.89504 0.2867188 -0.9660655 0.59879712 1
sample2 41.43134 -0.6325066 1.6286422 0.77715354 0
sample3 27.70834 -0.6552219 0.8297284 -0.06018658 2
sample4 37.58138 -0.1784748 2.6503799 1.82794968 2
sample5 26.88234 -1.3387167 2.2828828 -1.51035477 2
sample6 33.81681 -1.0825636 0.6167613 -0.85197024 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:
= ~ s(pheno) + var1 + var2 + var3 + var4 design
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
:
= NBAMSeqDataSet(countData = countData, colData = colData, design = design)
gsd gsd
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:
= NBAMSeq(gsd) gsd
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
:
library(BiocParallel)
= NBAMSeq(gsd, parallel = TRUE) gsd
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.
= results(gsd, name = "pheno")
res1 head(res1)
DataFrame with 6 rows and 7 columns
baseMean edf stat pvalue padj AIC BIC
<numeric> <numeric> <numeric> <numeric> <numeric> <numeric> <numeric>
gene1 128.0058 1.00025 0.8346790 0.360887 0.694014 226.723 233.693
gene2 87.3451 1.00003 0.2729840 0.601366 0.848077 212.463 219.433
gene3 84.4142 1.00005 0.0404992 0.840661 0.955296 231.523 238.494
gene4 65.1228 1.00004 2.0378143 0.153436 0.655630 190.777 197.747
gene5 83.7423 1.00005 0.8953343 0.344037 0.694014 222.720 229.690
gene6 161.7722 1.00005 0.6991959 0.403050 0.746388 251.171 258.141
For linear continuous covariates, base mean, estimated coefficient, standard error, test statistics, p-value, and adjusted p-value will be returned.
= results(gsd, name = "var1")
res2 head(res2)
DataFrame with 6 rows and 8 columns
baseMean coef SE stat pvalue padj AIC
<numeric> <numeric> <numeric> <numeric> <numeric> <numeric> <numeric>
gene1 128.0058 -1.4642664 0.537110 -2.726195 0.00640691 0.106782 226.723
gene2 87.3451 1.5738552 0.483859 3.252715 0.00114308 0.028577 212.463
gene3 84.4142 -0.8051793 0.428001 -1.881254 0.05993743 0.369699 231.523
gene4 65.1228 0.0589299 0.532607 0.110644 0.91189856 0.965502 190.777
gene5 83.7423 -0.8066308 0.424815 -1.898782 0.05759312 0.369699 222.720
gene6 161.7722 0.1271021 0.482001 0.263697 0.79201355 0.920946 251.171
BIC
<numeric>
gene1 233.693
gene2 219.433
gene3 238.494
gene4 197.747
gene5 229.690
gene6 258.141
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
.
= results(gsd, contrast = c("var4", "2", "0"))
res3 head(res3)
DataFrame with 6 rows and 8 columns
baseMean coef SE stat pvalue padj AIC
<numeric> <numeric> <numeric> <numeric> <numeric> <numeric> <numeric>
gene1 128.0058 -0.760557 1.130682 -0.672653 0.5011678 0.746207 226.723
gene2 87.3451 0.491204 1.017718 0.482652 0.6293428 0.802816 212.463
gene3 84.4142 -1.713778 0.903308 -1.897225 0.0577983 0.346413 231.523
gene4 65.1228 -1.092411 1.120492 -0.974939 0.3295903 0.746207 190.777
gene5 83.7423 -0.922870 0.899891 -1.025536 0.3051104 0.746207 222.720
gene6 161.7722 -0.840060 1.017388 -0.825702 0.4089729 0.746207 251.171
BIC
<numeric>
gene1 233.693
gene2 219.433
gene3 238.494
gene4 197.747
gene5 229.690
gene6 258.141
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[order(res1$pvalue),]
res1 = rownames(res1)[1]
topgene = getsf(gsd) ## get the estimated size factors
sf ## divide raw count by size factors to obtain normalized counts
= t(t(countData)/sf)
countnorm head(res1)
DataFrame with 6 rows and 7 columns
baseMean edf stat pvalue padj AIC BIC
<numeric> <numeric> <numeric> <numeric> <numeric> <numeric> <numeric>
gene27 65.3540 1.00008 7.63320 0.00573257 0.167029 201.435 208.405
gene16 74.9659 1.00012 7.22646 0.00719012 0.167029 217.738 224.708
gene48 60.5203 1.00016 6.63237 0.01002172 0.167029 204.697 211.667
gene31 34.7013 1.00008 5.93522 0.01484535 0.185567 178.318 185.288
gene26 104.1157 1.00008 5.24012 0.02207251 0.220725 234.108 241.078
gene12 73.4832 1.00012 4.89543 0.02693193 0.224433 188.920 195.890
library(ggplot2)
= topgene
setTitle = data.frame(pheno = pheno, logcount = log2(countnorm[topgene,]+1))
df 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))
sessionInfo()
R version 4.2.1 (2022-06-23)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.0
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib
locale:
[1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats4 stats graphics grDevices utils datasets methods
[8] base
other attached packages:
[1] ggplot2_3.3.6 BiocParallel_1.32.1
[3] NBAMSeq_1.14.0 SummarizedExperiment_1.28.0
[5] Biobase_2.58.0 GenomicRanges_1.50.1
[7] GenomeInfoDb_1.34.2 IRanges_2.32.0
[9] S4Vectors_0.36.0 BiocGenerics_0.44.0
[11] MatrixGenerics_1.10.0 matrixStats_0.62.0
loaded via a namespace (and not attached):
[1] httr_1.4.3 sass_0.4.1 bit64_4.0.5
[4] jsonlite_1.8.0 splines_4.2.1 bslib_0.3.1
[7] assertthat_0.2.1 highr_0.9 blob_1.2.3
[10] GenomeInfoDbData_1.2.8 yaml_2.3.5 pillar_1.7.0
[13] RSQLite_2.2.14 lattice_0.20-45 glue_1.6.2
[16] digest_0.6.29 RColorBrewer_1.1-3 XVector_0.38.0
[19] colorspace_2.0-3 htmltools_0.5.2 Matrix_1.4-1
[22] DESeq2_1.38.0 XML_3.99-0.10 pkgconfig_2.0.3
[25] genefilter_1.80.0 zlibbioc_1.44.0 purrr_0.3.4
[28] xtable_1.8-4 scales_1.2.0 tibble_3.1.7
[31] annotate_1.76.0 mgcv_1.8-40 KEGGREST_1.38.0
[34] farver_2.1.1 generics_0.1.3 ellipsis_0.3.2
[37] withr_2.5.0 cachem_1.0.6 cli_3.3.0
[40] survival_3.3-1 magrittr_2.0.3 crayon_1.5.1
[43] memoise_2.0.1 evaluate_0.15 fansi_1.0.3
[46] nlme_3.1-158 tools_4.2.1 lifecycle_1.0.1
[49] stringr_1.4.0 locfit_1.5-9.6 munsell_0.5.0
[52] DelayedArray_0.24.0 AnnotationDbi_1.60.0 Biostrings_2.66.0
[55] compiler_4.2.1 jquerylib_0.1.4 rlang_1.0.4
[58] grid_4.2.1 RCurl_1.98-1.7 labeling_0.4.2
[61] bitops_1.0-7 rmarkdown_2.14 gtable_0.3.0
[64] codetools_0.2-18 DBI_1.1.3 R6_2.5.1
[67] knitr_1.39 dplyr_1.0.9 fastmap_1.1.0
[70] bit_4.0.4 utf8_1.2.2 stringi_1.7.8
[73] parallel_4.2.1 Rcpp_1.0.9 vctrs_0.4.1
[76] geneplotter_1.76.0 png_0.1-7 tidyselect_1.1.2
[79] xfun_0.31