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 143 301 5 17 16 89 12 111 190
gene2 2 84 63 13 2 8 11 2 208
gene3 587 58 1 6 73 16 105 196 79
gene4 1 82 34 2 98 2 10 110 242
gene5 218 54 25 5 4 1 81 41 16
gene6 57 413 74 36 34 4 49 62 107
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 75 4 321 2 488 1 526 166
gene2 321 149 1 1 7 221 53 451
gene3 3 107 2 6 13 292 48 184
gene4 2 200 303 11 116 39 24 10
gene5 54 56 43 12 235 23 1 269
gene6 20 826 445 11 1 92 6 3
sample18 sample19 sample20
gene1 13 57 70
gene2 192 2 43
gene3 537 111 79
gene4 44 147 4
gene5 18 1 8
gene6 18 126 1
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 24.84861 -0.4263403 -0.7057702 -0.7205817 1
sample2 53.86174 -0.3463108 1.2745028 0.6958805 1
sample3 42.57126 -0.3985668 -0.5206388 -0.7236395 0
sample4 24.29973 -2.5448176 -0.3661942 -0.4673764 1
sample5 41.38161 -0.5092604 0.5738038 1.3869155 0
sample6 43.10515 1.5722885 0.6024634 0.3095557 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.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 144.4547 1.00004 1.6949915 0.192963 0.480680 236.796 243.766
gene2 51.1028 1.00015 6.3047353 0.012046 0.150575 210.884 217.854
gene3 84.8827 1.00006 0.0040092 0.949882 0.969268 231.887 238.857
gene4 69.2996 1.00009 0.0402864 0.841164 0.876213 214.825 221.795
gene5 51.3963 1.00007 0.7415260 0.389192 0.689670 207.379 214.349
gene6 96.6872 1.00009 0.1686986 0.681359 0.857476 229.689 236.659
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 144.4547 1.0226938 0.392244 2.6072906 0.00912619 0.0912619 236.796
gene2 51.1028 -0.5052109 0.404008 -1.2504960 0.21111843 0.6043199 210.884
gene3 84.8827 0.2987359 0.372324 0.8023538 0.42234835 0.6812070 231.887
gene4 69.2996 0.0127559 0.389064 0.0327862 0.97384506 0.9863074 214.825
gene5 51.3963 1.0922873 0.396006 2.7582623 0.00581096 0.0885365 207.379
gene6 96.6872 0.2786991 0.424016 0.6572839 0.51099839 0.7553220 229.689
BIC
<numeric>
gene1 243.766
gene2 217.854
gene3 238.857
gene4 221.795
gene5 214.349
gene6 236.659
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 144.4547 -1.057721 0.929951 -1.137394 0.255373462 0.75074647 236.796
gene2 51.1028 0.714560 0.957753 0.746079 0.455619579 0.75074647 210.884
gene3 84.8827 -0.755398 0.884438 -0.854100 0.393049659 0.75074647 231.887
gene4 69.2996 -3.547979 0.940428 -3.772727 0.000161473 0.00807364 214.825
gene5 51.3963 -0.231601 0.933215 -0.248175 0.803998854 0.94071026 207.379
gene6 96.6872 -2.275806 1.010505 -2.252147 0.024312980 0.24312980 229.689
BIC
<numeric>
gene1 243.766
gene2 217.854
gene3 238.857
gene4 221.795
gene5 214.349
gene6 236.659
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>
gene23 169.7333 1.00062 13.60397 0.000227063 0.0113532 240.298 247.268
gene47 106.5887 1.00030 8.86047 0.002918731 0.0654872 222.258 229.228
gene19 188.8400 1.00006 8.31706 0.003929229 0.0654872 253.499 260.470
gene2 51.1028 1.00015 6.30474 0.012046021 0.1505753 210.884 217.854
gene22 89.9088 1.00012 5.59447 0.018029285 0.1802928 209.042 216.012
gene45 72.9956 1.00043 4.98812 0.025596210 0.1855279 218.886 225.856
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 Under development (unstable) (2020-10-17 r79346)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.1 LTS
Matrix products: default
BLAS: /home/biocbuild/bbs-3.13-bioc/R/lib/libRblas.so
LAPACK: /home/biocbuild/bbs-3.13-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.3.2 BiocParallel_1.25.0
[3] NBAMSeq_1.7.1 SummarizedExperiment_1.21.0
[5] Biobase_2.51.0 GenomicRanges_1.43.0
[7] GenomeInfoDb_1.27.0 IRanges_2.25.0
[9] S4Vectors_0.29.0 BiocGenerics_0.37.0
[11] MatrixGenerics_1.3.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.37.0 rlang_0.4.8 annotate_1.69.0
[13] blob_1.2.1 Matrix_1.2-18 rmarkdown_2.5
[16] labeling_0.4.2 splines_4.1.0 geneplotter_1.69.0
[19] stringr_1.4.0 RCurl_1.98-1.2 bit_4.0.4
[22] munsell_0.5.0 DelayedArray_0.17.0 compiler_4.1.0
[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.1.0 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.31.0 genefilter_1.73.0
[49] ellipsis_0.3.1 vctrs_0.3.4 generics_0.0.2
[52] RColorBrewer_1.1-2 tools_4.1.0 bit64_4.0.5
[55] glue_1.4.2 DESeq2_1.31.0 purrr_0.3.4
[58] survival_3.2-7 yaml_2.2.1 AnnotationDbi_1.53.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.