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
gene1 1 50 3 563 3 71 112 2
gene2 55 1 169 1 1 299 269 4
gene3 23 1 154 5 7 10 102 16
gene4 404 39 71 13 21 23 2 22
gene5 230 1 19 13 48 1 125 74
gene6 270 14 1 177 21 48 3 1
sample9 sample10 sample11 sample12 sample13 sample14 sample15
gene1 1 18 98 7 3 1 438
gene2 3 9 39 151 21 7 62
gene3 6 14 143 2 117 413 106
gene4 98 173 967 3 120 47 7
gene5 142 3 123 49 3 1 2
gene6 1 1 9 10 16 278 7
sample16 sample17 sample18 sample19 sample20
gene1 2 237 30 22 2
gene2 1 1 19 760 36
gene3 235 2 170 72 3
gene4 32 87 10 511 1
gene5 90 1 26 3 149
gene6 1 7 71 35 11
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 77.02145 0.8156103 1.561581548 -0.05595064 0
sample2 36.58047 -0.1097310 0.184164397 0.25414512 2
sample3 33.86539 -0.2290377 -0.303039992 -0.74715349 2
sample4 28.25202 -0.7088702 -0.006176627 -0.58145020 1
sample5 55.77676 -0.3473787 -1.200653724 -0.68435843 0
sample6 71.10518 1.3035122 0.164027048 1.06821733 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;
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 5 columns
baseMean edf stat
<numeric> <numeric> <numeric>
gene1 84.3793635956969 1.56458369392198 1.454161269207
gene2 125.712982177134 1.0000664853489 0.00309807435471525
gene3 67.8296059621983 1.00007361338238 0.943981471369953
gene4 142.565920085671 1.00010058154233 9.86702360970191
gene5 46.0320106206954 1.00003866817429 0.00379664341305267
gene6 47.319595974352 1.00009079629979 1.19787126289115
pvalue padj
<numeric> <numeric>
gene1 0.382379528944865 0.651958637969764
gene2 0.95563245805288 0.993275710791537
gene3 0.331278787433253 0.613479235987505
gene4 0.00168378113226422 0.0210472641533027
gene5 0.950862530761611 0.993275710791537
gene6 0.273793488290287 0.526525939019783
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 6 columns
baseMean coef SE
<numeric> <numeric> <numeric>
gene1 84.3793635956969 -0.614471903113667 0.588123965828201
gene2 125.712982177134 0.916323387834464 0.653966689853516
gene3 67.8296059621983 0.658655206830593 0.524796195764176
gene4 142.565920085671 0.00559297766071627 0.513730046174928
gene5 46.0320106206954 0.306725978366412 0.548064609888456
gene6 47.319595974352 0.160064939169069 0.596759112854084
stat pvalue padj
<numeric> <numeric> <numeric>
gene1 -1.04479997214254 0.296115427782895 0.616907141214364
gene2 1.40117746370188 0.161161011696745 0.470473941573914
gene3 1.25506856213296 0.209453871772981 0.523634679432451
gene4 0.0108869973682868 0.991313604481664 0.995761083058439
gene5 0.559652954838369 0.575716177278846 0.799692096991928
gene6 0.268223703201675 0.788527130743636 0.876141256381817
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 6 columns
baseMean coef SE
<numeric> <numeric> <numeric>
gene1 84.3793635956969 2.02247199479628 1.158119774842
gene2 125.712982177134 1.16552268718307 1.24628032988706
gene3 67.8296059621983 2.38518636812942 1.00598658329837
gene4 142.565920085671 0.673220009897608 0.979550200085572
gene5 46.0320106206954 -1.81242984695074 1.0422072821929
gene6 47.319595974352 -0.972145102211274 1.13422434568402
stat pvalue padj
<numeric> <numeric> <numeric>
gene1 1.74634095603125 0.0807517231221085 0.292962379929045
gene2 0.935201061296297 0.349684691694034 0.672470560950065
gene3 2.37099222567065 0.0177404042367063 0.118447935160696
gene4 0.68727463874623 0.491909677419372 0.758912283047259
gene5 -1.7390301122606 0.0820294663801326 0.292962379929045
gene6 -0.857101248011919 0.391388931522674 0.720994775360697
To explore the nonlinear association of covariates, it is 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 5 columns
baseMean edf stat
<numeric> <numeric> <numeric>
gene44 56.0103890794657 1.00005834573686 12.300196701442
gene36 148.329376050838 1.00004186571313 11.1225278358626
gene33 57.0669598777245 1.00004581390391 9.87045146379159
gene4 142.565920085671 1.00010058154233 9.86702360970191
gene27 70.6105463985253 2.24116670150349 16.4434539348552
gene12 59.9954012172501 1.00005338059828 7.05661777320937
pvalue padj
<numeric> <numeric>
gene44 0.000453197644706901 0.0210472641533027
gene36 0.000853042669024154 0.0210472641533027
gene33 0.00167997166253272 0.0210472641533027
gene4 0.00168378113226422 0.0210472641533027
gene27 0.00317699935552308 0.0317699935552308
gene12 0.00790016582582732 0.0658347152152277
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 3.6.0 (2019-04-26)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.2 LTS
Matrix products: default
BLAS: /home/biocbuild/bbs-3.9-bioc/R/lib/libRblas.so
LAPACK: /home/biocbuild/bbs-3.9-bioc/R/lib/libRlapack.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 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] parallel stats4 stats graphics grDevices utils datasets
[8] methods base
other attached packages:
[1] ggplot2_3.1.1 NBAMSeq_1.0.0
[3] SummarizedExperiment_1.14.0 DelayedArray_0.10.0
[5] BiocParallel_1.18.0 matrixStats_0.54.0
[7] Biobase_2.44.0 GenomicRanges_1.36.0
[9] GenomeInfoDb_1.20.0 IRanges_2.18.0
[11] S4Vectors_0.22.0 BiocGenerics_0.30.0
loaded via a namespace (and not attached):
[1] bit64_0.9-7 splines_3.6.0 Formula_1.2-3
[4] assertthat_0.2.1 latticeExtra_0.6-28 blob_1.1.1
[7] GenomeInfoDbData_1.2.1 yaml_2.2.0 pillar_1.3.1
[10] RSQLite_2.1.1 backports_1.1.4 lattice_0.20-38
[13] glue_1.3.1 digest_0.6.18 RColorBrewer_1.1-2
[16] XVector_0.24.0 checkmate_1.9.1 colorspace_1.4-1
[19] htmltools_0.3.6 Matrix_1.2-17 plyr_1.8.4
[22] DESeq2_1.24.0 XML_3.98-1.19 pkgconfig_2.0.2
[25] genefilter_1.66.0 zlibbioc_1.30.0 purrr_0.3.2
[28] xtable_1.8-4 scales_1.0.0 htmlTable_1.13.1
[31] tibble_2.1.1 annotate_1.62.0 mgcv_1.8-28
[34] withr_2.1.2 nnet_7.3-12 lazyeval_0.2.2
[37] survival_2.44-1.1 magrittr_1.5 crayon_1.3.4
[40] memoise_1.1.0 evaluate_0.13 nlme_3.1-139
[43] foreign_0.8-71 tools_3.6.0 data.table_1.12.2
[46] stringr_1.4.0 locfit_1.5-9.1 munsell_0.5.0
[49] cluster_2.0.9 AnnotationDbi_1.46.0 compiler_3.6.0
[52] rlang_0.3.4 grid_3.6.0 RCurl_1.95-4.12
[55] rstudioapi_0.10 htmlwidgets_1.3 labeling_0.3
[58] bitops_1.0-6 base64enc_0.1-3 rmarkdown_1.12
[61] gtable_0.3.0 DBI_1.0.0 R6_2.4.0
[64] gridExtra_2.3 knitr_1.22 dplyr_0.8.0.1
[67] bit_1.1-14 Hmisc_4.2-0 stringi_1.4.3
[70] Rcpp_1.0.1 geneplotter_1.62.0 rpart_4.1-15
[73] acepack_1.4.1 tidyselect_0.2.5 xfun_0.6
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). BioMed Central: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). Oxford University Press: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). Oxford University Press:2672–8.