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 18 1 13 1 18 475 2 102 18
gene2 37 58 101 467 3 45 54 1 6
gene3 522 37 21 74 11 38 2 17 198
gene4 281 3 6 13 1 4 124 5 13
gene5 3 307 15 8 23 61 10 1 40
gene6 18 265 2 1 123 7 5 289 20
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 1 2 256 25 3 9 1 56
gene2 11 59 30 364 121 19 314 40
gene3 13 1 163 177 65 133 32 417
gene4 53 357 72 69 18 155 293 1
gene5 6 73 241 219 1 62 3 1
gene6 9 98 1 5 41 1 31 210
sample18 sample19 sample20
gene1 238 3 184
gene2 23 31 35
gene3 4 72 2
gene4 4 30 157
gene5 167 264 10
gene6 1 2 54
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 22.19687 1.1431272 -0.66359854 -0.41320575 1
sample2 47.69430 -1.3946527 -0.39552165 -0.56774226 2
sample3 69.76995 0.4516791 -0.01542598 -0.55365582 0
sample4 50.21560 1.2296205 -1.28367071 0.88849420 1
sample5 53.50617 -0.4670014 0.51301070 1.19413976 2
sample6 47.58961 -1.8169837 -0.35982451 0.08748007 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 51.045695589182 1.00006450151615 0.104445836559098 0.746613102632875
gene2 95.3550972863871 1.00004198966151 3.44936174547028 0.0632841352818891
gene3 104.501310464557 1.00002455408131 1.12346600123558 0.28918253640783
gene4 92.1535272878853 1.0000467117278 2.61267800598187 0.106039491082266
gene5 60.0009478301794 1.00009079082553 0.0715212011482138 0.789299912136194
gene6 56.3312659984156 1.00007448432004 2.27959113735728 0.131100045948362
padj AIC BIC
<numeric> <numeric> <numeric>
gene1 0.853458498643093 199.73292353767 206.70311367879
gene2 0.271224570489403 223.710484008042 230.680651733381
gene3 0.628657687843108 219.225217253192 226.195367617362
gene4 0.353464970274221 208.834154593052 215.804327020305
gene5 0.876999902373549 211.584794726392 218.555011044625
gene6 0.357286473860278 202.192903668927 209.163103750246
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 51.045695589182 -0.911212181214477 0.422586908610355 -2.15627167488678
gene2 95.3550972863871 0.494262438008996 0.334379752990782 1.47814702770781
gene3 104.501310464557 0.621092973004256 0.335476405114781 1.85137602387194
gene4 92.1535272878853 0.978755429672315 0.351939775821544 2.78103100846608
gene5 60.0009478301794 -0.133189065087478 0.391582178802769 -0.340130558276919
gene6 56.3312659984156 -0.756305347534548 0.429900825523777 -1.75925539713279
pvalue padj AIC BIC
<numeric> <numeric> <numeric> <numeric>
gene1 0.0310624576775764 0.146346831761031 199.73292353767 206.70311367879
gene2 0.139368428908827 0.358026746173071 223.710484008042 230.680651733381
gene3 0.0641154758984787 0.228983842494567 219.225217253192 226.195367617362
gene4 0.00541865611718853 0.0541865611718853 208.834154593052 215.804327020305
gene5 0.733758210024306 0.853207220958496 211.584794726392 218.555011044625
gene6 0.0785341387678532 0.261780462559511 202.192903668927 209.163103750246
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 51.045695589182 -1.66151724623395 1.03871222361578 -1.59959342776401
gene2 95.3550972863871 1.60563639836149 0.819693068620299 1.95882637029503
gene3 104.501310464557 2.64118401316982 0.831181064732713 3.17762774591017
gene4 92.1535272878853 1.2588809096047 0.86271983601392 1.45920014476679
gene5 60.0009478301794 -0.257988458463381 0.959425864744466 -0.268898794522382
gene6 56.3312659984156 -1.65712295336168 1.05661341703718 -1.56833419549827
pvalue padj AIC BIC
<numeric> <numeric> <numeric> <numeric>
gene1 0.109688807403057 0.389343864971967 199.73292353767 206.70311367879
gene2 0.0501331242164714 0.250665621082357 223.710484008042 230.680651733381
gene3 0.00148485260914764 0.024747543485794 219.225217253192 226.195367617362
gene4 0.144510028990001 0.401416747194448 208.834154593052 215.804327020305
gene5 0.788007564649338 0.84423621294963 211.584794726392 218.555011044625
gene6 0.11680315949159 0.389343864971967 202.192903668927 209.163103750246
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>
gene23 83.1156244896868 1.00013318853483 13.0250008426242 0.000307927213017055
gene10 50.6628926185658 1.00012873634973 9.05524444702964 0.00262111183778102
gene49 64.0226312346896 1.00006790229083 6.95278204959836 0.00837183349935941
gene24 76.7092912776474 1.00016158799762 6.26641004326099 0.0123159492024363
gene19 115.033847854373 1.00005565681613 5.42224746343257 0.0198848213501677
gene15 82.9402903171507 1.00010615053047 5.12801287058395 0.0235418219492056
padj AIC BIC
<numeric> <numeric> <numeric>
gene23 0.0153963606508527 227.90410898545 234.874367520451
gene10 0.0655277959445254 198.279799859541 205.250053961357
gene49 0.139530558322657 205.892358458237 212.862551985618
gene24 0.153949365030453 212.242105974748 219.21239278801
gene19 0.196181849576714 224.219009801239 231.189191135405
gene15 0.196181849576714 219.420741844205 226.390973456592
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-apple-darwin15.6.0 (64-bit)
Running under: OS X El Capitan 10.11.6
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/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] 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] scales_1.1.0 jpeg_0.1-8.1 htmlTable_1.13.3
[31] tibble_2.1.3 annotate_1.64.0 mgcv_1.8-31
[34] farver_2.0.3 withr_2.1.2 nnet_7.3-12
[37] lazyeval_0.2.2 survival_3.1-8 magrittr_1.5
[40] crayon_1.3.4 memoise_1.1.0 evaluate_0.14
[43] nlme_3.1-144 foreign_0.8-75 tools_3.6.2
[46] data.table_1.12.8 lifecycle_0.1.0 stringr_1.4.0
[49] locfit_1.5-9.1 munsell_0.5.0 cluster_2.1.0
[52] AnnotationDbi_1.48.0 compiler_3.6.2 rlang_0.4.4
[55] grid_3.6.2 RCurl_1.98-1.1 rstudioapi_0.11
[58] htmlwidgets_1.5.1 labeling_0.3 bitops_1.0-6
[61] base64enc_0.1-3 rmarkdown_2.1 gtable_0.3.0
[64] DBI_1.1.0 R6_2.4.1 gridExtra_2.3
[67] knitr_1.28 dplyr_0.8.4 bit_1.1-15.2
[70] Hmisc_4.3-1 stringi_1.4.6 Rcpp_1.0.3
[73] geneplotter_1.64.0 vctrs_0.2.2 rpart_4.1-15
[76] acepack_1.4.1 png_0.1-7 tidyselect_1.0.0
[79] 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.