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 6 41 2 84 135 1 164 64 9
gene2 31 1 3 245 2 210 51 182 62
gene3 147 33 27 7 81 236 17 18 558
gene4 10 41 26 2 59 48 52 1 63
gene5 8 156 1 31 8 4 177 79 276
gene6 1 133 70 47 602 48 1 2 143
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 110 67 2 85 117 28 35 10
gene2 114 13 417 25 153 220 64 46
gene3 157 4 1 17 20 61 90 248
gene4 121 2 285 175 8 146 122 130
gene5 115 21 233 49 1 360 1 114
gene6 177 9 127 5 2 6 24 23
sample18 sample19 sample20
gene1 19 51 3
gene2 440 2 370
gene3 1 484 30
gene4 126 1 37
gene5 430 199 13
gene6 30 46 2
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 42.23140 -1.06854777 -1.9148149 2.1622543 1
sample2 41.05865 0.06969265 -0.7117269 -0.6220956 0
sample3 55.72230 -0.18563410 2.7621480 -0.2850841 1
sample4 46.27921 0.72806825 0.9189249 -0.3763677 1
sample5 33.57518 1.05426767 1.0148440 -0.4556817 2
sample6 54.57660 0.56532508 1.4340208 0.6938135 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 37.3776 1.00007 1.0835860 0.2979282 0.677110 207.192 214.162
gene2 116.3574 1.00005 1.4734503 0.2248219 0.672821 244.886 251.856
gene3 85.9155 1.00007 0.0101426 0.9200585 0.959026 234.749 241.719
gene4 61.8265 1.00008 1.1750474 0.2784035 0.675593 220.312 227.283
gene5 95.9509 1.00022 3.0271702 0.0819371 0.533332 231.698 238.669
gene6 65.7465 1.00006 3.3887829 0.0656474 0.533332 205.461 212.431
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 37.3776 -0.451541 0.341495 -1.322248 0.18608557 0.706027 207.192
gene2 116.3574 0.454610 0.384284 1.183004 0.23680745 0.726373 244.886
gene3 85.9155 0.111858 0.383582 0.291613 0.77058225 0.957654 234.749
gene4 61.8265 0.692502 0.356575 1.942097 0.05212536 0.436703 220.312
gene5 95.9509 1.022439 0.384319 2.660390 0.00780503 0.130084 231.698
gene6 65.7465 1.028353 0.364206 2.823548 0.00474953 0.118738 205.461
BIC
<numeric>
gene1 214.162
gene2 251.856
gene3 241.719
gene4 227.283
gene5 238.669
gene6 212.431
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 37.3776 0.9647052 0.907750 1.0627436 0.287898 0.712387 207.192
gene2 116.3574 0.0936038 1.022689 0.0915272 0.927074 0.965702 244.886
gene3 85.9155 -1.0997060 1.021815 -1.0762276 0.281825 0.712387 234.749
gene4 61.8265 0.9835362 0.943638 1.0422808 0.297282 0.712387 220.312
gene5 95.9509 0.3212644 1.005216 0.3195975 0.749273 0.936592 231.698
gene6 65.7465 -1.5280669 0.961094 -1.5899237 0.111852 0.632278 205.461
BIC
<numeric>
gene1 214.162
gene2 251.856
gene3 241.719
gene4 227.283
gene5 238.669
gene6 212.431
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>
gene37 51.7417 1.00011 4.44925 0.0349317 0.533332 206.912 213.882
gene40 47.3886 1.00017 4.43987 0.0351192 0.533332 191.171 198.141
gene9 55.5620 1.00006 4.17005 0.0411564 0.533332 202.374 209.344
gene50 77.1378 1.00009 3.71160 0.0540510 0.533332 220.668 227.639
gene6 65.7465 1.00006 3.38878 0.0656474 0.533332 205.461 212.431
gene20 79.3008 1.00100 3.13058 0.0767897 0.533332 234.070 241.041
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.1 Patched (2023-06-17 r84564)
Platform: x86_64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.6.5
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0
locale:
[1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
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.4 BiocParallel_1.36.0
[3] NBAMSeq_1.18.0 SummarizedExperiment_1.32.0
[5] Biobase_2.62.0 GenomicRanges_1.54.1
[7] GenomeInfoDb_1.38.0 IRanges_2.36.0
[9] S4Vectors_0.40.1 BiocGenerics_0.48.1
[11] MatrixGenerics_1.14.0 matrixStats_1.0.0
loaded via a namespace (and not attached):
[1] KEGGREST_1.42.0 gtable_0.3.4 xfun_0.41
[4] bslib_0.5.1 lattice_0.22-5 vctrs_0.6.4
[7] tools_4.3.1 bitops_1.0-7 generics_0.1.3
[10] parallel_4.3.1 RSQLite_2.3.2 AnnotationDbi_1.64.0
[13] tibble_3.2.1 fansi_1.0.5 highr_0.10
[16] blob_1.2.4 pkgconfig_2.0.3 Matrix_1.6-1.1
[19] lifecycle_1.0.3 GenomeInfoDbData_1.2.11 farver_2.1.1
[22] compiler_4.3.1 Biostrings_2.70.1 munsell_0.5.0
[25] DESeq2_1.42.0 codetools_0.2-19 htmltools_0.5.6.1
[28] sass_0.4.7 RCurl_1.98-1.12 yaml_2.3.7
[31] pillar_1.9.0 crayon_1.5.2 jquerylib_0.1.4
[34] DelayedArray_0.28.0 cachem_1.0.8 abind_1.4-5
[37] nlme_3.1-163 genefilter_1.84.0 tidyselect_1.2.0
[40] locfit_1.5-9.8 digest_0.6.33 dplyr_1.1.3
[43] labeling_0.4.3 splines_4.3.1 fastmap_1.1.1
[46] grid_4.3.1 colorspace_2.1-0 cli_3.6.1
[49] SparseArray_1.2.0 magrittr_2.0.3 S4Arrays_1.2.0
[52] survival_3.5-7 XML_3.99-0.14 utf8_1.2.4
[55] withr_2.5.2 scales_1.2.1 bit64_4.0.5
[58] rmarkdown_2.25 XVector_0.42.0 httr_1.4.7
[61] bit_4.0.5 png_0.1-8 memoise_2.0.1
[64] evaluate_0.23 knitr_1.45 mgcv_1.9-0
[67] rlang_1.1.1 Rcpp_1.0.11 DBI_1.1.3
[70] xtable_1.8-4 glue_1.6.2 annotate_1.80.0
[73] jsonlite_1.8.7 R6_2.5.1 zlibbioc_1.48.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.