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 3 1 83 212 27 186 23 123
gene2 1 1 1 1 49 40 199 159
gene3 1 25 1 1 12 45 47 2
gene4 22 520 38 224 630 108 1 1
gene5 2 13 534 90 13 45 80 27
gene6 7 51 2 77 36 4 2 12
sample9 sample10 sample11 sample12 sample13 sample14 sample15
gene1 1 228 107 2 190 99 5
gene2 12 34 230 239 22 36 50
gene3 93 1130 1 2 12 8 51
gene4 1 1 1 10 447 5 5
gene5 7 192 1 3 51 1 19
gene6 1 181 548 32 1 36 2
sample16 sample17 sample18 sample19 sample20
gene1 3 164 27 12 16
gene2 4 9 9 122 16
gene3 234 1 127 195 12
gene4 20 30 101 20 1
gene5 301 77 41 86 6
gene6 2 3 202 312 7
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 73.48350 1.8946133351 0.2329225 -2.39279372 0
sample2 43.78128 -0.0223295003 -0.5085798 -0.07212285 2
sample3 31.55925 -1.1120726498 -0.3234962 0.17000859 1
sample4 35.72086 -0.2277942437 -0.5378307 0.33124949 0
sample5 68.17951 -0.0005914057 1.5002634 -1.70024985 1
sample6 56.99346 -1.0167425600 -0.1612027 0.79429525 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;
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 60.1439359481175 1.00022272575496 0.316000194956739
gene2 53.7980655537633 1.00008144024126 4.62497481355467
gene3 81.9227093471244 1.00004309149727 4.81046585712738
gene4 76.7885705326382 1.0003016147766 9.04907194096506
gene5 60.6609249390823 1.40265671190118 2.51448966662445
gene6 60.2352020433357 1.00010385936164 2.47158637765086
pvalue padj
<numeric> <numeric>
gene1 0.574056880273728 0.718323722964759
gene2 0.0315157299791885 0.143253318087221
gene3 0.0282900627200019 0.142456266401811
gene4 0.00262877972770797 0.0328597465963496
gene5 0.348960070126902 0.545250109573284
gene6 0.11596835986316 0.305179894376736
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 60.1439359481175 -0.189515106223551 0.460435352306251
gene2 53.7980655537633 -0.0336790181847226 0.460417947364138
gene3 81.9227093471244 -0.602157329475525 0.518817249197619
gene4 76.7885705326382 -0.728019372159228 0.564805808621264
gene5 60.6609249390823 -1.08559336090974 0.456812920401545
gene6 60.2352020433357 -0.343217624369822 0.525486120346267
stat pvalue padj
<numeric> <numeric> <numeric>
gene1 -0.411599815857532 0.680632766606654 0.816930888560405
gene2 -0.0731487953011667 0.941687712594463 0.941687712594463
gene3 -1.16063475223076 0.245790466455402 0.56451767814743
gene4 -1.28897288421375 0.197407513614538 0.56451767814743
gene5 -2.37645064845252 0.0174800995466672 0.182027042712979
gene6 -0.653143082339948 0.513664040009421 0.755388294131502
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 60.1439359481175 -2.23976707879468 1.08066251417501
gene2 53.7980655537633 -0.698282501979672 1.08388368560424
gene3 81.9227093471244 2.71382962634417 1.23200298922903
gene4 76.7885705326382 0.484377525080048 1.33152463245247
gene5 60.6609249390823 -1.01926144013198 1.05397351329969
gene6 60.2352020433357 -0.132091887379934 1.23930964379266
stat pvalue padj
<numeric> <numeric> <numeric>
gene1 -2.07258700048882 0.0382107318702782 0.233567545770985
gene2 -0.644241177585762 0.519419052809666 0.763851548249509
gene3 2.20277844296665 0.0276103682684035 0.230086402236696
gene4 0.363776616124554 0.716024825646518 0.895031032058148
gene5 -0.967065516609582 0.333511282576141 0.575019452717484
gene6 -0.106585055673167 0.915118174886399 0.95462962703433
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 5 columns
baseMean edf stat
<numeric> <numeric> <numeric>
gene38 73.5382588037963 1.00003781947477 14.6170177924649
gene41 86.5366383281467 2.33001242679733 17.1745521094085
gene29 49.7074912046266 1.00100734284745 9.17861460612215
gene4 76.7885705326382 1.0003016147766 9.04907194096506
gene18 57.7161446692709 1.00005088552113 5.0257909260987
gene50 81.6143352130087 1.16532778742788 5.13351608623441
pvalue padj
<numeric> <numeric>
gene38 0.000131743079059047 0.00658715395295235
gene41 0.000635873096794522 0.0158968274198631
gene29 0.00245030144071076 0.0328597465963496
gene4 0.00262877972770797 0.0328597465963496
gene18 0.024977160124219 0.142456266401811
gene50 0.025497117586886 0.142456266401811
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.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.3 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.2.1 NBAMSeq_1.0.1
[3] SummarizedExperiment_1.14.1 DelayedArray_0.10.0
[5] BiocParallel_1.18.1 matrixStats_0.54.0
[7] Biobase_2.44.0 GenomicRanges_1.36.0
[9] GenomeInfoDb_1.20.0 IRanges_2.18.1
[11] S4Vectors_0.22.0 BiocGenerics_0.30.0
loaded via a namespace (and not attached):
[1] bit64_0.9-7 splines_3.6.1 Formula_1.2-3
[4] assertthat_0.2.1 latticeExtra_0.6-28 blob_1.2.0
[7] GenomeInfoDbData_1.2.1 yaml_2.2.0 pillar_1.4.2
[10] RSQLite_2.1.2 backports_1.1.4 lattice_0.20-38
[13] glue_1.3.1 digest_0.6.20 RColorBrewer_1.1-2
[16] XVector_0.24.0 checkmate_1.9.4 colorspace_1.4-1
[19] htmltools_0.3.6 Matrix_1.2-17 DESeq2_1.24.0
[22] XML_3.98-1.20 pkgconfig_2.0.2 genefilter_1.66.0
[25] zlibbioc_1.30.0 purrr_0.3.2 xtable_1.8-4
[28] scales_1.0.0 htmlTable_1.13.1 tibble_2.1.3
[31] annotate_1.62.0 mgcv_1.8-28 withr_2.1.2
[34] nnet_7.3-12 lazyeval_0.2.2 survival_2.44-1.1
[37] magrittr_1.5 crayon_1.3.4 memoise_1.1.0
[40] evaluate_0.14 nlme_3.1-141 foreign_0.8-72
[43] tools_3.6.1 data.table_1.12.2 stringr_1.4.0
[46] locfit_1.5-9.1 munsell_0.5.0 cluster_2.1.0
[49] AnnotationDbi_1.46.0 compiler_3.6.1 rlang_0.4.0
[52] grid_3.6.1 RCurl_1.95-4.12 rstudioapi_0.10
[55] htmlwidgets_1.3 labeling_0.3 bitops_1.0-6
[58] base64enc_0.1-3 rmarkdown_1.14 gtable_0.3.0
[61] DBI_1.0.0 R6_2.4.0 gridExtra_2.3
[64] knitr_1.24 dplyr_0.8.3 zeallot_0.1.0
[67] bit_1.1-14 Hmisc_4.2-0 stringi_1.4.3
[70] Rcpp_1.0.2 geneplotter_1.62.0 vctrs_0.2.0
[73] rpart_4.1-15 acepack_1.4.1 tidyselect_0.2.5
[76] xfun_0.8
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.