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 460 3 112 7 169 193 1 48 113
gene2 20 87 18 116 104 1 97 131 2
gene3 145 1 308 1 2 11 518 13 167
gene4 6 41 3 64 7 443 97 1 81
gene5 87 78 101 265 226 48 294 3 17
gene6 17 2 278 44 7 1 1 190 16
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 1 96 107 313 3 4 164 52
gene2 6 1 2 30 18 823 4 91
gene3 68 1 501 30 4 1 29 203
gene4 47 7 1 123 53 1 301 1
gene5 6 361 4 3 22 17 12 120
gene6 35 11 24 7 2 66 31 3
sample18 sample19 sample20
gene1 2 3 1
gene2 12 2 1
gene3 97 10 1
gene4 139 99 459
gene5 1 192 30
gene6 36 375 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 43.55917 0.14441736 0.2170695 0.29533464 0
sample2 44.54108 0.04735797 -1.9367028 -1.03447526 0
sample3 50.55432 -0.15526302 -0.3529096 1.13821197 1
sample4 49.28225 0.63080142 -0.9912898 1.51487020 1
sample5 77.17409 -0.74588880 -0.9599302 0.04746719 1
sample6 58.17694 -0.60488350 0.4614916 0.30359403 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 74.4412 1.00035 0.194819 0.65946924 0.8761276 221.448 228.418
gene2 64.1794 1.00007 0.754757 0.38496256 0.7128936 195.705 202.675
gene3 117.5126 1.00007 7.751152 0.00537017 0.0615892 214.630 221.600
gene4 82.0960 1.00019 2.428189 0.11922995 0.4985112 220.822 227.793
gene5 81.6149 1.00006 0.577739 0.44721330 0.7453555 232.616 239.586
gene6 48.8434 1.00006 4.001230 0.04548056 0.2706168 193.350 200.320
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 74.4412 -0.0163594 0.514361 -0.0318054 0.97462728 0.9873423 221.448
gene2 64.1794 1.2048230 0.444033 2.7133627 0.00666042 0.0832552 195.705
gene3 117.5126 -0.6940381 0.514544 -1.3488408 0.17738811 0.4223526 214.630
gene4 82.0960 0.4720981 0.479506 0.9845514 0.32484448 0.6247009 220.822
gene5 81.6149 -0.3061529 0.477210 -0.6415472 0.52116724 0.6514591 232.616
gene6 48.8434 0.6037654 0.444066 1.3596302 0.17394699 0.4223526 193.350
BIC
<numeric>
gene1 228.418
gene2 202.675
gene3 221.600
gene4 227.793
gene5 239.586
gene6 200.320
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 74.4412 -1.146477 1.15186 -0.995326 0.31957781 0.5918108 221.448
gene2 64.1794 -2.736695 1.02555 -2.668511 0.00761882 0.0761882 195.705
gene3 117.5126 1.305511 1.15185 1.133400 0.25704614 0.5918108 214.630
gene4 82.0960 0.374798 1.07307 0.349276 0.72688195 0.8407138 220.822
gene5 81.6149 1.097137 1.06886 1.026458 0.30467555 0.5918108 232.616
gene6 48.8434 1.368122 1.01327 1.350207 0.17694946 0.5918108 193.350
BIC
<numeric>
gene1 228.418
gene2 202.675
gene3 221.600
gene4 227.793
gene5 239.586
gene6 200.320
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>
gene50 74.5009 1.00010 15.42810 0.00008659 0.0043295 211.289 218.259
gene38 124.4731 1.00007 10.59859 0.00113236 0.0192165 237.112 244.082
gene30 69.3954 1.00007 10.56512 0.00115299 0.0192165 211.730 218.700
gene3 117.5126 1.00007 7.75115 0.00537017 0.0615892 214.630 221.600
gene18 128.0077 1.00007 7.45053 0.00634483 0.0615892 221.461 228.431
gene25 91.5350 1.00009 7.17646 0.00739071 0.0615892 211.355 218.325
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 RC (2024-04-16 r86468 ucrt)
Platform: x86_64-w64-mingw32/x64
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.5.1 BiocParallel_1.39.0
[3] NBAMSeq_1.21.0 SummarizedExperiment_1.35.0
[5] Biobase_2.65.0 GenomicRanges_1.57.0
[7] GenomeInfoDb_1.41.0 IRanges_2.39.0
[9] S4Vectors_0.43.0 BiocGenerics_0.51.0
[11] MatrixGenerics_1.17.0 matrixStats_1.3.0
loaded via a namespace (and not attached):
[1] KEGGREST_1.45.0 gtable_0.3.5 xfun_0.44
[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.67.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.2 compiler_4.4.0
[22] Biostrings_2.73.0 munsell_0.5.1 DESeq2_1.45.0
[25] codetools_0.2-20 snow_0.4-4 htmltools_0.5.8.1
[28] sass_0.4.9 yaml_2.3.8 pillar_1.9.0
[31] crayon_1.5.2 jquerylib_0.1.4 DelayedArray_0.31.1
[34] cachem_1.0.8 abind_1.4-5 nlme_3.1-164
[37] genefilter_1.87.0 tidyselect_1.2.1 locfit_1.5-9.9
[40] digest_0.6.35 dplyr_1.1.4 labeling_0.4.3
[43] splines_4.4.0 fastmap_1.2.0 grid_4.4.0
[46] colorspace_2.1-0 cli_3.6.2 SparseArray_1.5.4
[49] magrittr_2.0.3 S4Arrays_1.5.0 survival_3.6-4
[52] XML_3.99-0.16.1 utf8_1.2.4 withr_3.0.0
[55] scales_1.3.0 UCSC.utils_1.1.0 bit64_4.0.5
[58] rmarkdown_2.26 XVector_0.45.0 httr_1.4.7
[61] bit_4.0.5 png_0.1-8 memoise_2.0.1
[64] evaluate_0.23 knitr_1.46 mgcv_1.9-1
[67] rlang_1.1.3 Rcpp_1.0.12 DBI_1.2.2
[70] xtable_1.8-4 glue_1.7.0 annotate_1.83.0
[73] jsonlite_1.8.8 R6_2.5.1 zlibbioc_1.51.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.