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 195 19 2 61 8 4 804 329 1
gene2 77 122 4 87 10 9 3 458 31
gene3 199 96 146 155 43 49 424 257 10
gene4 411 20 72 380 218 28 389 67 331
gene5 1 2 10 60 11 4 163 1 588
gene6 42 284 35 1 2 70 1 2 745
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 105 115 15 21 53 77 13 22
gene2 188 2 687 292 51 10 21 44
gene3 36 65 1 22 180 142 8 148
gene4 6 1 21 1 2 118 912 2
gene5 34 59 68 163 49 16 1 21
gene6 1 1 105 197 1091 45 44 60
sample18 sample19 sample20
gene1 75 28 1
gene2 167 3 1
gene3 1 60 46
gene4 6 27 6
gene5 46 1 63
gene6 182 1 244
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 60.03657 0.1735732 1.952116e-01 -1.62958401 2
sample2 75.54840 -0.3448225 1.358539e-07 -0.90582229 2
sample3 43.67510 0.5759888 -2.417837e-01 0.36415755 0
sample4 54.91128 0.2746436 -1.425638e-01 -0.05653739 0
sample5 77.04134 0.1616370 1.235222e+00 0.15572026 2
sample6 73.97851 1.8351914 -4.850546e-02 1.11533887 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 88.7336 1.00007 0.147293 0.70124657 0.8551787 220.328 227.299
gene2 118.1393 1.00007 1.747603 0.18618820 0.4822348 218.672 225.642
gene3 91.6939 1.00009 0.341405 0.55911736 0.7765519 236.531 243.501
gene4 114.8791 1.00006 1.497269 0.22109540 0.5024895 232.374 239.344
gene5 61.6824 1.00006 8.406867 0.00373971 0.0389551 200.715 207.685
gene6 127.5728 1.00097 1.713240 0.19131154 0.4822348 228.915 235.886
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 88.7336 -0.630657 0.473103 -1.333022 0.18252468 0.5350987 220.328
gene2 118.1393 1.285774 0.451041 2.850682 0.00436256 0.0436256 218.672
gene3 91.6939 -0.576917 0.431077 -1.338316 0.18079358 0.5350987 236.531
gene4 114.8791 -0.151883 0.518596 -0.292874 0.76961867 0.8700008 232.374
gene5 61.6824 -1.036167 0.460892 -2.248180 0.02456472 0.1649465 200.715
gene6 127.5728 -0.473169 0.549416 -0.861221 0.38911610 0.5895698 228.915
BIC
<numeric>
gene1 227.299
gene2 225.642
gene3 243.501
gene4 239.344
gene5 207.685
gene6 235.886
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 88.7336 1.252631 0.871536 1.437268 0.1506417 0.538006 220.328
gene2 118.1393 0.660543 0.826117 0.799575 0.4239571 0.860247 218.672
gene3 91.6939 -0.196345 0.790023 -0.248531 0.8037234 0.999692 236.531
gene4 114.8791 -0.373967 0.948155 -0.394415 0.6932746 0.999692 232.374
gene5 61.6824 1.456765 0.846881 1.720154 0.0854045 0.427022 200.715
gene6 127.5728 0.737587 1.008007 0.731729 0.4643342 0.860247 228.915
BIC
<numeric>
gene1 227.299
gene2 225.642
gene3 243.501
gene4 239.344
gene5 207.685
gene6 235.886
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 101.2455 1.00009 11.95961 0.000544027 0.0272014 224.142 231.112
gene33 52.2875 1.00009 8.88980 0.002868356 0.0389551 205.592 212.563
gene46 40.8649 1.00004 8.73020 0.003130496 0.0389551 189.330 196.300
gene5 61.6824 1.00006 8.40687 0.003739709 0.0389551 200.715 207.685
gene49 98.5282 1.00007 8.33279 0.003895513 0.0389551 218.656 225.626
gene47 68.7150 1.00050 6.77471 0.009290820 0.0774235 209.791 216.762
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-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.3.0 BiocParallel_1.22.0
[3] NBAMSeq_1.4.1 SummarizedExperiment_1.18.1
[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.1
[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] snow_0.4-3 assertthat_0.2.1 digest_0.6.25
[7] R6_2.4.1 RSQLite_2.2.0 evaluate_0.14
[10] pillar_1.4.4 zlibbioc_1.34.0 rlang_0.4.6
[13] annotate_1.66.0 blob_1.2.1 Matrix_1.2-18
[16] rmarkdown_2.1 labeling_0.3 splines_4.0.0
[19] geneplotter_1.66.0 stringr_1.4.0 RCurl_1.98-1.2
[22] bit_1.1-15.2 munsell_0.5.0 compiler_4.0.0
[25] xfun_0.13 pkgconfig_2.0.3 mgcv_1.8-31
[28] htmltools_0.4.0 tidyselect_1.0.0 tibble_3.0.1
[31] GenomeInfoDbData_1.2.3 XML_3.99-0.3 withr_2.2.0
[34] crayon_1.3.4 dplyr_0.8.5 bitops_1.0-6
[37] grid_4.0.0 nlme_3.1-147 xtable_1.8-4
[40] gtable_0.3.0 lifecycle_0.2.0 DBI_1.1.0
[43] magrittr_1.5 scales_1.1.0 stringi_1.4.6
[46] farver_2.0.3 XVector_0.28.0 genefilter_1.70.0
[49] ellipsis_0.3.0 vctrs_0.2.4 RColorBrewer_1.1-2
[52] tools_4.0.0 bit64_0.9-7 glue_1.4.0
[55] DESeq2_1.28.0 purrr_0.3.4 survival_3.1-12
[58] yaml_2.2.1 AnnotationDbi_1.50.0 colorspace_1.4-1
[61] memoise_1.1.0 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): 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.