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 1 10 52 394 3 52 37 26 240
gene2 12 108 35 7 449 165 10 3 3
gene3 1 3 1 5 142 3 175 1 4
gene4 184 2 19 5 30 192 3 91 1
gene5 15 1 7 134 12 1 9 24 9
gene6 1 1 360 78 8 107 1 53 9
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 29 82 1 3 938 3 914 110
gene2 4 1 16 10 1 77 9 38
gene3 432 291 70 162 7 1 133 1
gene4 35 167 250 21 4 47 5 94
gene5 1 103 33 15 19 75 66 7
gene6 512 249 179 103 16 141 287 89
sample18 sample19 sample20
gene1 125 1 53
gene2 247 131 1
gene3 137 56 53
gene4 1 170 62
gene5 33 31 23
gene6 2 5 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 48.44995 1.3551550 -0.5319536 -0.1600502 1
sample2 45.76193 0.8388865 2.5783154 -1.3113475 0
sample3 63.59380 0.7627634 -0.7466934 0.3289092 0
sample4 68.08355 0.5425361 1.0341969 2.4307850 2
sample5 39.89988 0.1851019 0.3405028 2.0734438 1
sample6 74.29516 -0.5952411 0.4229664 1.3760040 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 116.5672 1.00016 0.849784 0.356635 0.715956 230.676 237.647
gene2 59.8064 1.00006 1.137879 0.286177 0.681375 207.195 214.165
gene3 77.1961 1.00009 0.753639 0.385381 0.715956 211.460 218.431
gene4 52.4473 1.00013 0.234743 0.628247 0.842867 216.667 223.638
gene5 30.6428 1.00007 2.679565 0.101679 0.423662 191.012 197.983
gene6 81.8056 1.00010 0.837376 0.360172 0.715956 223.706 230.676
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 116.5672 0.5566137 0.399071 1.3947730 0.1630843 0.543614 230.676
gene2 59.8064 0.5799572 0.387217 1.4977562 0.1341966 0.535146 207.195
gene3 77.1961 -0.7856393 0.406838 -1.9310869 0.0534723 0.381945 211.460
gene4 52.4473 -0.0068902 0.360818 -0.0190961 0.9847645 0.994448 216.667
gene5 30.6428 -0.0807033 0.318230 -0.2536008 0.7998039 0.979068 191.012
gene6 81.8056 -0.9607749 0.383385 -2.5060316 0.0122095 0.203491 223.706
BIC
<numeric>
gene1 237.647
gene2 214.165
gene3 218.431
gene4 223.638
gene5 197.983
gene6 230.676
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 116.5672 0.501069 1.119703 0.447502 0.6545130 0.884477 230.676
gene2 59.8064 -1.872925 1.097872 -1.705959 0.0880157 0.258026 207.195
gene3 77.1961 -0.837818 1.140761 -0.734438 0.4626818 0.722940 211.460
gene4 52.4473 0.269848 1.014955 0.265872 0.7903378 0.938244 216.667
gene5 30.6428 0.968101 0.892847 1.084285 0.2782382 0.535074 191.012
gene6 81.8056 -0.837772 1.069607 -0.783252 0.4334794 0.699160 223.706
BIC
<numeric>
gene1 237.647
gene2 214.165
gene3 218.431
gene4 223.638
gene5 197.983
gene6 230.676
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>
gene24 66.9698 1.00007 14.24479 0.000161433 0.00807163 197.279 204.250
gene45 88.3147 1.00006 12.31719 0.000448948 0.01122370 197.037 204.008
gene38 52.1314 1.00008 7.75358 0.005363761 0.08939601 196.536 203.507
gene33 62.0064 1.00003 4.86689 0.027380740 0.24763456 195.828 202.798
gene49 141.0853 1.00004 4.74832 0.029333213 0.24763456 232.136 239.106
gene9 55.4457 1.00008 4.72608 0.029716148 0.24763456 205.921 212.891
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.4.0 beta (2024-04-14 r86421)
Platform: x86_64-apple-darwin20
Running under: macOS Monterey 12.7.1
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0
locale:
[1] en_US.UTF-8/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.5.1 BiocParallel_1.38.0
[3] NBAMSeq_1.20.0 SummarizedExperiment_1.34.0
[5] Biobase_2.64.0 GenomicRanges_1.56.0
[7] GenomeInfoDb_1.40.0 IRanges_2.38.0
[9] S4Vectors_0.42.0 BiocGenerics_0.50.0
[11] MatrixGenerics_1.16.0 matrixStats_1.3.0
loaded via a namespace (and not attached):
[1] KEGGREST_1.44.0 gtable_0.3.5 xfun_0.43
[4] bslib_0.7.0 lattice_0.22-6 vctrs_0.6.5
[7] tools_4.4.0 generics_0.1.3 parallel_4.4.0
[10] RSQLite_2.3.6 tibble_3.2.1 fansi_1.0.6
[13] AnnotationDbi_1.66.0 highr_0.10 blob_1.2.4
[16] pkgconfig_2.0.3 Matrix_1.7-0 lifecycle_1.0.4
[19] GenomeInfoDbData_1.2.12 farver_2.1.1 compiler_4.4.0
[22] Biostrings_2.72.0 munsell_0.5.1 DESeq2_1.44.0
[25] codetools_0.2-20 htmltools_0.5.8.1 sass_0.4.9
[28] yaml_2.3.8 pillar_1.9.0 crayon_1.5.2
[31] jquerylib_0.1.4 DelayedArray_0.30.0 cachem_1.0.8
[34] abind_1.4-5 nlme_3.1-164 genefilter_1.86.0
[37] tidyselect_1.2.1 locfit_1.5-9.9 digest_0.6.35
[40] dplyr_1.1.4 labeling_0.4.3 splines_4.4.0
[43] fastmap_1.1.1 grid_4.4.0 colorspace_2.1-0
[46] cli_3.6.2 SparseArray_1.4.0 magrittr_2.0.3
[49] S4Arrays_1.4.0 survival_3.6-4 XML_3.99-0.16.1
[52] utf8_1.2.4 withr_3.0.0 scales_1.3.0
[55] UCSC.utils_1.0.0 bit64_4.0.5 rmarkdown_2.26
[58] XVector_0.44.0 httr_1.4.7 bit_4.0.5
[61] png_0.1-8 memoise_2.0.1 evaluate_0.23
[64] knitr_1.46 mgcv_1.9-1 rlang_1.1.3
[67] Rcpp_1.0.12 DBI_1.2.2 xtable_1.8-4
[70] glue_1.7.0 annotate_1.82.0 jsonlite_1.8.8
[73] R6_2.5.1 zlibbioc_1.50.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.