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 179 235 1 14 18 69 64 355 1
gene2 1 3 21 6 1 1 4 26 71
gene3 148 44 3 273 5 1 1 8 115
gene4 113 2 29 7 2 53 2 193 24
gene5 6 17 21 29 366 13 1 64 195
gene6 5 5 135 130 1 5 127 216 34
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 10 3 1848 187 1 37 35 14
gene2 10 4 7 127 42 46 271 2
gene3 11 7 168 3 12 36 22 1
gene4 1 1 6 5 121 71 52 171
gene5 184 2 4 1210 54 56 201 3
gene6 22 224 2 1 8 198 9 382
sample18 sample19 sample20
gene1 516 72 22
gene2 24 117 23
gene3 72 3 1
gene4 46 221 11
gene5 14 1 359
gene6 548 148 109
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 45.80454 -0.67696702 0.4757105 1.3805930 1
sample2 71.56170 1.48223037 0.2018922 1.7711097 2
sample3 49.11499 1.50332834 0.4788836 1.9331688 2
sample4 31.76184 0.16693000 1.1395186 -0.4399929 2
sample5 44.76679 -0.45921695 -0.7049107 0.8112379 1
sample6 47.78797 -0.03322722 1.0789435 0.2366181 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 188.6805 1.00008 1.5950891 0.206655 0.555178 238.635 245.605
gene2 30.8274 1.00023 1.8920351 0.169150 0.555178 189.735 196.705
gene3 41.4709 1.00007 1.0808716 0.298541 0.670205 186.922 193.893
gene4 39.0263 1.00003 0.0937343 0.759489 0.914050 204.336 211.306
gene5 127.8197 1.00021 0.5871080 0.443764 0.700151 231.732 238.703
gene6 79.1836 1.00004 0.7559089 0.384640 0.670205 225.594 232.564
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 188.6805 -1.507719 0.531022 -2.839278 0.00452158 0.0781025 238.635
gene2 30.8274 -0.175435 0.463920 -0.378157 0.70531365 0.9320979 189.735
gene3 41.4709 -0.586886 0.458774 -1.279248 0.20080959 0.4943794 186.922
gene4 39.0263 -0.130285 0.457165 -0.284984 0.77565675 0.9320979 204.336
gene5 127.8197 -0.727705 0.528057 -1.378080 0.16817846 0.4943794 231.732
gene6 79.1836 0.332173 0.459880 0.722304 0.47010737 0.7345428 225.594
BIC
<numeric>
gene1 245.605
gene2 196.705
gene3 193.893
gene4 211.306
gene5 238.703
gene6 232.564
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 188.6805 0.666759 0.937727 0.711037 0.4770613 0.713606 238.635
gene2 30.8274 -0.380198 0.822878 -0.462035 0.6440563 0.806486 189.735
gene3 41.4709 1.966042 0.825753 2.380906 0.0172701 0.123358 186.922
gene4 39.0263 -0.786089 0.814745 -0.964829 0.3346307 0.713606 204.336
gene5 127.8197 -1.390521 0.941052 -1.477624 0.1395084 0.435964 231.732
gene6 79.1836 -0.454915 0.810959 -0.560959 0.5748252 0.792756 225.594
BIC
<numeric>
gene1 245.605
gene2 196.705
gene3 193.893
gene4 211.306
gene5 238.703
gene6 232.564
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>
gene17 57.6754 1.00006 9.41217 0.00215656 0.107828 198.040 205.010
gene20 56.0031 1.00003 5.69467 0.01701744 0.425436 208.624 215.594
gene27 118.0769 1.00016 3.77179 0.05214581 0.555178 219.430 226.400
gene41 82.9474 1.00005 3.46694 0.06261088 0.555178 220.155 227.125
gene21 80.1475 1.00017 3.37522 0.06620346 0.555178 212.547 219.518
gene32 93.0541 1.00005 3.30090 0.06924209 0.555178 221.615 228.585
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.0 (2020-04-24)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.4 LTS
Matrix products: default
BLAS: /home/biocbuild/bbs-3.11-bioc/R/lib/libRblas.so
LAPACK: /home/biocbuild/bbs-3.11-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.0 BiocParallel_1.22.0
[3] NBAMSeq_1.4.0 SummarizedExperiment_1.18.0
[5] DelayedArray_0.14.0 matrixStats_0.56.0
[7] Biobase_2.48.0 GenomicRanges_1.40.0
[9] GenomeInfoDb_1.24.0 IRanges_2.22.0
[11] S4Vectors_0.26.0 BiocGenerics_0.34.0
loaded via a namespace (and not attached):
[1] Rcpp_1.0.4.6 locfit_1.5-9.4 lattice_0.20-41
[4] assertthat_0.2.1 digest_0.6.25 R6_2.4.1
[7] RSQLite_2.2.0 evaluate_0.14 pillar_1.4.3
[10] zlibbioc_1.34.0 rlang_0.4.5 annotate_1.66.0
[13] blob_1.2.1 Matrix_1.2-18 rmarkdown_2.1
[16] labeling_0.3 splines_4.0.0 geneplotter_1.66.0
[19] stringr_1.4.0 RCurl_1.98-1.2 bit_1.1-15.2
[22] munsell_0.5.0 compiler_4.0.0 xfun_0.13
[25] pkgconfig_2.0.3 mgcv_1.8-31 htmltools_0.4.0
[28] tidyselect_1.0.0 tibble_3.0.1 GenomeInfoDbData_1.2.3
[31] XML_3.99-0.3 withr_2.2.0 crayon_1.3.4
[34] dplyr_0.8.5 bitops_1.0-6 grid_4.0.0
[37] nlme_3.1-147 xtable_1.8-4 gtable_0.3.0
[40] lifecycle_0.2.0 DBI_1.1.0 magrittr_1.5
[43] scales_1.1.0 stringi_1.4.6 farver_2.0.3
[46] XVector_0.28.0 genefilter_1.70.0 ellipsis_0.3.0
[49] vctrs_0.2.4 RColorBrewer_1.1-2 tools_4.0.0
[52] bit64_0.9-7 glue_1.4.0 DESeq2_1.28.0
[55] purrr_0.3.4 survival_3.1-12 yaml_2.2.1
[58] AnnotationDbi_1.50.0 colorspace_1.4-1 memoise_1.1.0
[61] knitr_1.28
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.