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 86 376 6 132 2 196 73 16 1
gene2 91 343 10 140 366 123 2 232 1
gene3 1 7 20 51 1 13 92 1 2
gene4 22 76 15 19 4 165 36 1 34
gene5 303 21 11 2 4 9 21 19 2
gene6 171 43 70 10 1 646 60 5 179
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 120 70 30 1 4 434 14 9
gene2 13 10 3 1 2 3 1 1
gene3 29 100 156 6 47 51 1 90
gene4 4 11 42 33 4 143 45 1
gene5 23 164 35 68 371 8 36 4
gene6 75 48 4 1 1 2 5 1
sample18 sample19 sample20
gene1 33 1 771
gene2 270 16 201
gene3 1 48 5
gene4 1 171 259
gene5 14 7 45
gene6 390 71 253
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 63.29676 -0.8041195 -0.42622390 -0.5262095 0
sample2 55.69575 0.7617944 -1.67210790 -0.2016715 2
sample3 70.10650 2.2472788 0.11908446 -0.9896059 1
sample4 70.63567 -0.6641999 0.04521703 2.2840708 2
sample5 77.96669 -1.3180549 1.02961283 0.5713979 2
sample6 38.88339 0.9885427 -0.40173411 -1.2102923 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
<numeric> <numeric> <numeric> <numeric>
gene1 81.4253796400377 1.00007419295041 0.572934259894597 0.449120409510763
gene2 64.2089409567406 1.0001805798025 0.0060870922093718 0.937812323068874
gene3 25.4176877255583 1.00016353565239 0.0360828830129869 0.849293862751811
gene4 49.414986734078 1.00009983657056 1.58905492703792 0.207502782756702
gene5 52.6868952663877 1.0000903343974 0.000201044287467747 0.988808979717613
gene6 101.964447252056 1.00012758158107 4.43364325483717 0.0352504020178051
padj AIC BIC
<numeric> <numeric> <numeric>
gene1 0.76740404962448 221.237092511362 228.207292302555
gene2 0.98315823005564 210.027976254288 216.998281978303
gene3 0.98315823005564 186.150599623867 193.120888376472
gene4 0.518756956891755 212.087923725399 219.058149050772
gene5 0.988808979717613 208.985693252506 215.955909116259
gene6 0.220315012611282 213.205039928405 220.17529288038
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
<numeric> <numeric> <numeric> <numeric>
gene1 81.4253796400377 0.0349902301863601 0.353519641254002 0.0989767642393025
gene2 64.2089409567406 -1.10324626082546 0.382295850906086 -2.88584419164015
gene3 25.4176877255583 0.986836362736786 0.335410495603989 2.9421749637254
gene4 49.414986734078 0.0728591057203441 0.366217723435414 0.198950244780257
gene5 52.6868952663877 0.191059797207517 0.346928651164302 0.550717839435618
gene6 101.964447252056 -0.419486934543395 0.356564642620362 -1.17646811938677
pvalue padj AIC BIC
<numeric> <numeric> <numeric> <numeric>
gene1 0.921156718950734 0.99197975902208 221.237092511362 228.207292302555
gene2 0.0039036535851509 0.0650608930858484 210.027976254288 216.998281978303
gene3 0.00325915727736301 0.0650608930858484 186.150599623867 193.120888376472
gene4 0.842301665380016 0.99197975902208 212.087923725399 219.058149050772
gene5 0.581827113553702 0.80809321326903 208.985693252506 215.955909116259
gene6 0.239407864815875 0.492632321705494 213.205039928405 220.17529288038
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
<numeric> <numeric> <numeric> <numeric>
gene1 81.4253796400377 -2.7131639288422 1.05078662267793 -2.58203128045891
gene2 64.2089409567406 1.16127575308661 1.1236278425628 1.03350567607682
gene3 25.4176877255583 0.0467667599049966 0.972884479289927 0.0480702086429933
gene4 49.414986734078 -0.504745493271176 1.08044349495277 -0.467165099914122
gene5 52.6868952663877 -0.728095802725808 1.01311664517777 -0.718669272873364
gene6 101.964447252056 -0.384564998847924 1.05291218612657 -0.365239384551768
pvalue padj AIC BIC
<numeric> <numeric> <numeric> <numeric>
gene1 0.00982206819568156 0.13767978454464 221.237092511362 228.207292302555
gene2 0.301367315625772 0.717256325091551 210.027976254288 216.998281978303
gene3 0.961660288806558 0.976095822927323 186.150599623867 193.120888376472
gene4 0.640381760981334 0.878857924247414 212.087923725399 219.058149050772
gene5 0.472344719999109 0.878857924247414 208.985693252506 215.955909116259
gene6 0.714932721383029 0.893665901728786 213.205039928405 220.17529288038
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
<numeric> <numeric> <numeric> <numeric>
gene40 88.2535569044162 1.00013418959521 19.9134522390307 8.11913321784354e-06
gene16 63.7942069042264 1.00005861188695 10.1529904506291 0.00144128421239361
gene50 27.9997131432142 1.00003850939091 8.24635950147757 0.00408436534757521
gene21 56.8866459752251 1.00007860425559 8.19655243786665 0.00419853838989665
gene49 91.1287231731843 1.00004624043672 5.58321538643428 0.0181379955833232
gene36 71.2859287192644 1.00005179073371 5.54907912291258 0.0184957946586043
padj AIC BIC
<numeric> <numeric> <numeric>
gene40 0.000405956660892177 205.261742169082 212.232001700871
gene16 0.0360321053098403 194.405860677708 201.376044954333
gene50 0.0524817298737081 178.954856401689 185.92502066161
gene21 0.0524817298737081 190.606598193669 197.576802377341
gene49 0.154131622155036 227.980918816647 234.95109077462
gene36 0.154131622155036 221.544745003359 228.514922487942
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.2 (2019-12-12)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server 2012 R2 x64 (build 9600)
Matrix products: default
locale:
[1] LC_COLLATE=C
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] parallel stats4 stats graphics grDevices utils datasets
[8] methods base
other attached packages:
[1] ggplot2_3.2.1 NBAMSeq_1.2.1
[3] SummarizedExperiment_1.16.1 DelayedArray_0.12.2
[5] BiocParallel_1.20.1 matrixStats_0.55.0
[7] Biobase_2.46.0 GenomicRanges_1.38.0
[9] GenomeInfoDb_1.22.0 IRanges_2.20.2
[11] S4Vectors_0.24.3 BiocGenerics_0.32.0
loaded via a namespace (and not attached):
[1] bit64_0.9-7 splines_3.6.2 Formula_1.2-3
[4] assertthat_0.2.1 latticeExtra_0.6-29 blob_1.2.1
[7] GenomeInfoDbData_1.2.2 yaml_2.2.1 RSQLite_2.2.0
[10] pillar_1.4.3 backports_1.1.5 lattice_0.20-38
[13] glue_1.3.1 digest_0.6.24 RColorBrewer_1.1-2
[16] XVector_0.26.0 checkmate_2.0.0 colorspace_1.4-1
[19] htmltools_0.4.0 Matrix_1.2-18 DESeq2_1.26.0
[22] XML_3.99-0.3 pkgconfig_2.0.3 genefilter_1.68.0
[25] zlibbioc_1.32.0 purrr_0.3.3 xtable_1.8-4
[28] snow_0.4-3 scales_1.1.0 jpeg_0.1-8.1
[31] htmlTable_1.13.3 tibble_2.1.3 annotate_1.64.0
[34] mgcv_1.8-31 farver_2.0.3 withr_2.1.2
[37] nnet_7.3-12 lazyeval_0.2.2 survival_3.1-8
[40] magrittr_1.5 crayon_1.3.4 memoise_1.1.0
[43] evaluate_0.14 nlme_3.1-144 foreign_0.8-75
[46] tools_3.6.2 data.table_1.12.8 lifecycle_0.1.0
[49] stringr_1.4.0 locfit_1.5-9.1 munsell_0.5.0
[52] cluster_2.1.0 AnnotationDbi_1.48.0 compiler_3.6.2
[55] rlang_0.4.4 grid_3.6.2 RCurl_1.98-1.1
[58] rstudioapi_0.11 htmlwidgets_1.5.1 labeling_0.3
[61] bitops_1.0-6 base64enc_0.1-3 rmarkdown_2.1
[64] gtable_0.3.0 DBI_1.1.0 R6_2.4.1
[67] gridExtra_2.3 knitr_1.28 dplyr_0.8.4
[70] bit_1.1-15.2 Hmisc_4.3-1 stringi_1.4.6
[73] Rcpp_1.0.3 geneplotter_1.64.0 vctrs_0.2.2
[76] rpart_4.1-15 acepack_1.4.1 png_0.1-7
[79] tidyselect_1.0.0 xfun_0.12
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.