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 446 18 5 114 1 17 268 77 4
gene2 2 1041 65 69 1 30 107 5 93
gene3 149 33 41 1 596 28 4 277 4
gene4 4 25 1 117 350 1 45 1 7
gene5 1 1 183 3 40 121 54 104 7
gene6 55 311 46 185 80 8 7 314 388
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 1 526 8 2 75 1 83 63
gene2 68 123 187 374 270 1 7 93
gene3 23 902 190 19 2 404 208 416
gene4 1 71 277 9 1 19 144 256
gene5 219 19 1 39 357 98 3 205
gene6 1 288 3 5 29 304 250 170
sample18 sample19 sample20
gene1 84 12 33
gene2 114 1 24
gene3 291 10 22
gene4 396 138 45
gene5 3 411 1
gene6 4 1 1248
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 78.10198 -0.81127774 0.53652198 0.06041413 2
sample2 79.68947 -0.93061246 -0.49188994 0.22510731 2
sample3 55.57142 -1.61657423 -0.00211368 -1.99680180 2
sample4 31.35824 -1.64441510 -0.64734406 -0.51095132 0
sample5 64.71446 0.93059106 -0.51393640 0.53077575 0
sample6 31.29346 -0.06354946 2.43150745 1.00077676 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 47.6045 1.00007 0.1075551 0.7429988 0.807607 215.264 222.234
gene2 112.3051 1.00012 0.7550910 0.3849241 0.620845 238.963 245.933
gene3 131.2139 1.00006 0.3414903 0.5589805 0.716642 236.529 243.499
gene4 86.2382 1.00009 1.7460638 0.1863789 0.438074 220.545 227.515
gene5 79.9994 1.00060 5.3609924 0.0206822 0.139526 220.120 227.090
gene6 128.6477 1.00010 0.0921174 0.7616830 0.810301 246.775 253.745
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 47.6045 -0.1444526 0.377166 -0.3829949 0.701723518 0.8996455 215.264
gene2 112.3051 -0.0408527 0.408829 -0.0999261 0.920402997 0.9204030 238.963
gene3 131.2139 1.1712584 0.328651 3.5638376 0.000365472 0.0060912 236.529
gene4 86.2382 0.7297367 0.411517 1.7732823 0.076181944 0.1914186 220.545
gene5 79.9994 -0.4627939 0.408238 -1.1336370 0.256946852 0.4430118 220.120
gene6 128.6477 0.2677766 0.395393 0.6772414 0.498252789 0.7312441 246.775
BIC
<numeric>
gene1 222.234
gene2 245.933
gene3 243.499
gene4 227.515
gene5 227.090
gene6 253.745
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 47.6045 1.300840 0.902112 1.441994 0.149304 0.533229 215.264
gene2 112.3051 0.695493 0.973787 0.714215 0.475095 0.712387 238.963
gene3 131.2139 0.687488 0.778478 0.883118 0.377173 0.712387 236.529
gene4 86.2382 -1.115324 0.975726 -1.143071 0.253009 0.712387 220.545
gene5 79.9994 -1.513357 0.969929 -1.560276 0.118695 0.494562 220.120
gene6 128.6477 0.795117 0.942173 0.843919 0.398715 0.712387 246.775
BIC
<numeric>
gene1 222.234
gene2 245.933
gene3 243.499
gene4 227.515
gene5 227.090
gene6 253.745
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>
gene21 87.5658 1.00010 7.86279 0.00505009 0.0926099 214.611 221.581
gene20 33.9079 1.00005 7.18447 0.00735607 0.0926099 202.114 209.084
gene11 88.5419 1.00011 7.10733 0.00767938 0.0926099 223.661 230.632
gene25 63.1237 1.00026 6.87110 0.00876659 0.0926099 206.596 213.566
gene22 58.8182 1.00010 6.77236 0.00926099 0.0926099 207.030 214.000
gene44 48.9418 1.00007 5.71943 0.01678114 0.1395264 183.342 190.312
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.0.3 (2020-10-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Mojave 10.14.6
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/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.3.2 BiocParallel_1.24.0
[3] NBAMSeq_1.6.1 SummarizedExperiment_1.20.0
[5] Biobase_2.50.0 GenomicRanges_1.42.0
[7] GenomeInfoDb_1.26.0 IRanges_2.24.0
[9] S4Vectors_0.28.0 BiocGenerics_0.36.0
[11] MatrixGenerics_1.2.0 matrixStats_0.57.0
loaded via a namespace (and not attached):
[1] Rcpp_1.0.5 locfit_1.5-9.4 lattice_0.20-41
[4] digest_0.6.27 R6_2.5.0 RSQLite_2.2.1
[7] evaluate_0.14 httr_1.4.2 pillar_1.4.6
[10] zlibbioc_1.36.0 rlang_0.4.8 annotate_1.68.0
[13] blob_1.2.1 Matrix_1.2-18 rmarkdown_2.5
[16] labeling_0.4.2 splines_4.0.3 geneplotter_1.68.0
[19] stringr_1.4.0 RCurl_1.98-1.2 bit_4.0.4
[22] munsell_0.5.0 DelayedArray_0.16.0 compiler_4.0.3
[25] xfun_0.18 pkgconfig_2.0.3 mgcv_1.8-33
[28] htmltools_0.5.0 tidyselect_1.1.0 tibble_3.0.4
[31] GenomeInfoDbData_1.2.4 XML_3.99-0.5 withr_2.3.0
[34] crayon_1.3.4 dplyr_1.0.2 bitops_1.0-6
[37] grid_4.0.3 nlme_3.1-150 xtable_1.8-4
[40] gtable_0.3.0 lifecycle_0.2.0 DBI_1.1.0
[43] magrittr_1.5 scales_1.1.1 stringi_1.5.3
[46] farver_2.0.3 XVector_0.30.0 genefilter_1.72.0
[49] ellipsis_0.3.1 vctrs_0.3.4 generics_0.0.2
[52] RColorBrewer_1.1-2 tools_4.0.3 bit64_4.0.5
[55] glue_1.4.2 DESeq2_1.30.0 purrr_0.3.4
[58] survival_3.2-7 yaml_2.2.1 AnnotationDbi_1.52.0
[61] colorspace_1.4-1 memoise_1.1.0 knitr_1.30
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.