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 6 2 31 3 277 2 2 225 125
gene2 144 52 320 227 201 8 1332 101 1
gene3 2 198 53 233 26 428 2 10 1
gene4 11 112 4 17 2 1 2 110 128
gene5 441 1 687 493 1 26 333 39 8
gene6 2 244 196 1 70 2 18 2 74
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 3 208 1 125 76 53 40 4
gene2 90 15 198 193 11 28 1 2
gene3 193 1 1 36 5 104 16 19
gene4 72 1 10 35 299 276 12 1
gene5 65 7 220 71 422 1 17 13
gene6 54 3 17 144 26 19 14 98
sample18 sample19 sample20
gene1 3 50 1
gene2 835 58 62
gene3 5 156 14
gene4 17 94 68
gene5 16 127 5
gene6 97 6 23
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 57.65134 0.5268087 -0.2648824 0.5646721 1
sample2 56.21438 1.2247643 -1.5918931 0.4407165 0
sample3 54.16733 -1.5694569 -3.4102249 0.1754285 0
sample4 79.66353 0.1023319 -2.4764168 1.1539970 1
sample5 41.77933 0.1231109 0.9325656 1.3110931 0
sample6 42.49407 -1.2275982 0.7340348 0.8788385 0
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 50.5974 1.00016 0.0217389 0.883380 0.973057 207.522 214.492
gene2 181.2666 1.00011 0.0173308 0.895588 0.973057 250.038 257.008
gene3 63.6090 1.00010 0.3457426 0.556593 0.713581 207.712 214.682
gene4 52.5189 1.00007 0.5515138 0.457732 0.614579 206.950 213.920
gene5 114.8820 1.00007 0.6811788 0.409214 0.598133 232.496 239.466
gene6 41.1169 1.00013 1.0892859 0.296782 0.529967 199.555 206.525
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 50.5974 0.3184932 0.444484 0.7165460 0.4736543 0.7775811 207.522
gene2 181.2666 0.7763077 0.415537 1.8682036 0.0617337 0.3086685 250.038
gene3 63.6090 -1.0118466 0.406480 -2.4892907 0.0127998 0.0914273 207.712
gene4 52.5189 0.0332261 0.401148 0.0828275 0.9339887 0.9853732 206.950
gene5 114.8820 0.4456503 0.407559 1.0934608 0.2741916 0.6177151 232.496
gene6 41.1169 -0.0126651 0.339556 -0.0372989 0.9702467 0.9853732 199.555
BIC
<numeric>
gene1 214.492
gene2 257.008
gene3 214.682
gene4 213.920
gene5 239.466
gene6 206.525
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 50.5974 -0.119292 1.212595 -0.0983774 0.9216326 0.940441 207.522
gene2 181.2666 -2.798283 1.135954 -2.4633764 0.0137635 0.108602 250.038
gene3 63.6090 -0.431731 1.110548 -0.3887548 0.6974576 0.814497 207.712
gene4 52.5189 -0.700243 1.087133 -0.6441194 0.5194980 0.774408 206.950
gene5 114.8820 -1.207484 1.115359 -1.0825965 0.2789875 0.536515 232.496
gene6 41.1169 -2.236475 0.921317 -2.4274763 0.0152043 0.108602 199.555
BIC
<numeric>
gene1 214.492
gene2 257.008
gene3 214.682
gene4 213.920
gene5 239.466
gene6 206.525
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>
gene40 50.8071 1.00005 14.10899 0.000172707 0.00863533 200.237 207.207
gene9 62.2331 1.00004 8.84270 0.002944605 0.07361513 187.788 194.758
gene19 66.4655 1.00008 5.98469 0.014437934 0.22136323 215.733 222.703
gene39 55.7375 1.00010 4.74010 0.029477130 0.22136323 205.287 212.257
gene42 56.3588 1.00012 4.67781 0.030561844 0.22136323 203.448 210.419
gene41 88.6974 1.00006 4.58586 0.032242411 0.22136323 218.829 225.799
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.1.1 (2021-08-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.3 LTS
Matrix products: default
BLAS: /home/biocbuild/bbs-3.14-bioc/R/lib/libRblas.so
LAPACK: /home/biocbuild/bbs-3.14-bioc/R/lib/libRlapack.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_GB 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
attached base packages:
[1] stats4 stats graphics grDevices utils datasets methods
[8] base
other attached packages:
[1] ggplot2_3.3.5 BiocParallel_1.28.0
[3] NBAMSeq_1.10.0 SummarizedExperiment_1.24.0
[5] Biobase_2.54.0 GenomicRanges_1.46.0
[7] GenomeInfoDb_1.30.0 IRanges_2.28.0
[9] S4Vectors_0.32.0 BiocGenerics_0.40.0
[11] MatrixGenerics_1.6.0 matrixStats_0.61.0
loaded via a namespace (and not attached):
[1] httr_1.4.2 sass_0.4.0 bit64_4.0.5
[4] jsonlite_1.7.2 splines_4.1.1 bslib_0.3.1
[7] assertthat_0.2.1 highr_0.9 blob_1.2.2
[10] GenomeInfoDbData_1.2.7 yaml_2.2.1 pillar_1.6.4
[13] RSQLite_2.2.8 lattice_0.20-45 glue_1.4.2
[16] digest_0.6.28 RColorBrewer_1.1-2 XVector_0.34.0
[19] colorspace_2.0-2 htmltools_0.5.2 Matrix_1.3-4
[22] DESeq2_1.34.0 XML_3.99-0.8 pkgconfig_2.0.3
[25] genefilter_1.76.0 zlibbioc_1.40.0 purrr_0.3.4
[28] xtable_1.8-4 scales_1.1.1 tibble_3.1.5
[31] annotate_1.72.0 mgcv_1.8-38 KEGGREST_1.34.0
[34] farver_2.1.0 generics_0.1.1 ellipsis_0.3.2
[37] withr_2.4.2 cachem_1.0.6 survival_3.2-13
[40] magrittr_2.0.1 crayon_1.4.1 memoise_2.0.0
[43] evaluate_0.14 fansi_0.5.0 nlme_3.1-153
[46] tools_4.1.1 lifecycle_1.0.1 stringr_1.4.0
[49] locfit_1.5-9.4 munsell_0.5.0 DelayedArray_0.20.0
[52] AnnotationDbi_1.56.0 Biostrings_2.62.0 compiler_4.1.1
[55] jquerylib_0.1.4 rlang_0.4.12 grid_4.1.1
[58] RCurl_1.98-1.5 labeling_0.4.2 bitops_1.0-7
[61] rmarkdown_2.11 gtable_0.3.0 DBI_1.1.1
[64] R6_2.5.1 knitr_1.36 dplyr_1.0.7
[67] fastmap_1.1.0 bit_4.0.4 utf8_1.2.2
[70] stringi_1.7.5 parallel_4.1.1 Rcpp_1.0.7
[73] vctrs_0.3.8 geneplotter_1.72.0 png_0.1-7
[76] tidyselect_1.1.1 xfun_0.27
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.