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 765 40 15 986 114 1147 1 16 1
gene2 251 44 7 286 20 17 497 418 1
gene3 338 23 96 36 210 7 12 1 5
gene4 212 1 3 1 23 132 283 41 44
gene5 42 56 1 12 4 43 2 135 94
gene6 3 11 13 1 5 341 15 101 325
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 336 146 7 60 433 1 3 54
gene2 12 5 6 300 81 3 9 259
gene3 2 267 32 22 11 8 505 3
gene4 224 296 137 624 1 24 3 304
gene5 583 305 480 3 1 193 14 22
gene6 2 18 3 1 1 1 1 2
sample18 sample19 sample20
gene1 23 89 57
gene2 346 1 131
gene3 64 123 102
gene4 52 412 321
gene5 274 3 21
gene6 1 68 515
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 36.18591 0.9197948 -0.20331224 -0.2023843 1
sample2 30.74424 1.3928948 -0.05525458 -0.8436824 2
sample3 22.25703 -0.1176638 -1.19092080 -0.1301426 1
sample4 20.00382 -1.0658616 -1.15930373 -0.3211788 2
sample5 34.50094 -0.2304408 0.26387525 0.4077247 2
sample6 33.28691 0.3962444 0.82648425 0.2260761 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 227.7642 1.00010 12.0154057 0.00052819 0.0264095 241.186 248.156
gene2 123.7860 1.00008 1.4177199 0.23378673 0.4366429 226.543 233.513
gene3 86.8597 1.00004 4.0169596 0.04505660 0.3189645 217.325 224.296
gene4 129.0261 1.00008 0.0896479 0.76473130 0.8832302 243.918 250.888
gene5 122.9257 1.00012 1.8397527 0.17499925 0.4218347 224.568 231.538
gene6 59.2694 1.00004 1.4057926 0.23578714 0.4366429 183.191 190.162
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 227.7642 -1.397025 0.441781 -3.1622596 0.00156550 0.0391375 241.186
gene2 123.7860 -0.619471 0.395018 -1.5682113 0.11683182 0.3754070 226.543
gene3 86.8597 1.041145 0.387928 2.6838618 0.00727772 0.1193768 217.325
gene4 129.0261 -0.017156 0.431141 -0.0397921 0.96825890 0.9722415 243.918
gene5 122.9257 0.395598 0.438822 0.9014996 0.36732274 0.7063899 224.568
gene6 59.2694 1.143250 0.478241 2.3905293 0.01682410 0.1344662 183.191
BIC
<numeric>
gene1 248.156
gene2 233.513
gene3 224.296
gene4 250.888
gene5 231.538
gene6 190.162
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 227.7642 -0.390118 1.018353 -0.383087 0.7016550 0.838894 241.186
gene2 123.7860 1.266795 0.897341 1.411720 0.1580325 0.488588 226.543
gene3 86.8597 1.064741 0.874099 1.218101 0.2231855 0.488588 217.325
gene4 129.0261 0.625253 0.979423 0.638390 0.5232199 0.771551 243.918
gene5 122.9257 -0.920560 0.992547 -0.927472 0.3536813 0.609795 224.568
gene6 59.2694 -1.894104 1.077744 -1.757471 0.0788375 0.488588 183.191
BIC
<numeric>
gene1 248.156
gene2 233.513
gene3 224.296
gene4 250.888
gene5 231.538
gene6 190.162
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>
gene1 227.7642 1.00010 12.01541 0.00052819 0.0264095 241.186 248.156
gene44 172.5366 1.00014 8.04368 0.00457148 0.1142870 255.892 262.863
gene42 105.7388 1.21445 5.10552 0.02544364 0.3189645 228.121 235.304
gene35 81.6179 1.00207 4.65049 0.03108709 0.3189645 213.819 220.791
gene14 111.1849 1.00006 4.42233 0.03548435 0.3189645 228.734 235.704
gene3 86.8597 1.00004 4.01696 0.04505660 0.3189645 217.325 224.296
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.3.0 RC (2023-04-13 r84269 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server 2022 x64 (build 20348)
Matrix products: default
locale:
[1] LC_COLLATE=C
[2] LC_CTYPE=English_United States.utf8
[3] LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8
time zone: America/New_York
tzcode source: internal
attached base packages:
[1] stats4 stats graphics grDevices utils datasets methods
[8] base
other attached packages:
[1] ggplot2_3.4.2 BiocParallel_1.34.0
[3] NBAMSeq_1.16.0 SummarizedExperiment_1.30.0
[5] Biobase_2.60.0 GenomicRanges_1.52.0
[7] GenomeInfoDb_1.36.0 IRanges_2.34.0
[9] S4Vectors_0.38.0 BiocGenerics_0.46.0
[11] MatrixGenerics_1.12.0 matrixStats_0.63.0
loaded via a namespace (and not attached):
[1] KEGGREST_1.40.0 gtable_0.3.3 xfun_0.39
[4] bslib_0.4.2 lattice_0.21-8 vctrs_0.6.2
[7] tools_4.3.0 bitops_1.0-7 generics_0.1.3
[10] parallel_4.3.0 tibble_3.2.1 fansi_1.0.4
[13] AnnotationDbi_1.62.0 RSQLite_2.3.1 highr_0.10
[16] blob_1.2.4 pkgconfig_2.0.3 Matrix_1.5-4
[19] lifecycle_1.0.3 GenomeInfoDbData_1.2.10 farver_2.1.1
[22] compiler_4.3.0 Biostrings_2.68.0 munsell_0.5.0
[25] DESeq2_1.40.0 codetools_0.2-19 snow_0.4-4
[28] htmltools_0.5.5 sass_0.4.5 RCurl_1.98-1.12
[31] yaml_2.3.7 crayon_1.5.2 pillar_1.9.0
[34] jquerylib_0.1.4 DelayedArray_0.26.0 cachem_1.0.7
[37] nlme_3.1-162 genefilter_1.82.0 tidyselect_1.2.0
[40] locfit_1.5-9.7 digest_0.6.31 dplyr_1.1.2
[43] labeling_0.4.2 splines_4.3.0 fastmap_1.1.1
[46] grid_4.3.0 colorspace_2.1-0 cli_3.6.1
[49] magrittr_2.0.3 survival_3.5-5 XML_3.99-0.14
[52] utf8_1.2.3 withr_2.5.0 scales_1.2.1
[55] bit64_4.0.5 rmarkdown_2.21 XVector_0.40.0
[58] httr_1.4.5 bit_4.0.5 png_0.1-8
[61] memoise_2.0.1 evaluate_0.20 knitr_1.42
[64] mgcv_1.8-42 rlang_1.1.0 Rcpp_1.0.10
[67] xtable_1.8-4 glue_1.6.2 DBI_1.1.3
[70] annotate_1.78.0 jsonlite_1.8.4 R6_2.5.1
[73] zlibbioc_1.46.0
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.