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 15 1 9 8 1 1 1 399 52
gene2 1 471 220 33 177 401 195 157 174
gene3 2 325 3 59 48 625 57 22 91
gene4 6 316 20 251 3 38 2 12 339
gene5 213 95 4 90 9 30 1 10 45
gene6 1 2 25 6 133 9 47 1 1
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 1 1 16 28 26 17 1 88
gene2 53 51 6 114 67 146 94 40
gene3 78 6 1 131 145 57 327 5
gene4 371 65 1 106 88 34 60 264
gene5 25 72 1 157 168 1 1405 98
gene6 8 1 49 1 37 24 87 150
sample18 sample19 sample20
gene1 242 13 297
gene2 433 36 153
gene3 1 2 1
gene4 165 30 1
gene5 1 4 122
gene6 31 80 144
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 63.34326 -0.15090964 0.2767432 0.7221222 0
sample2 79.90305 -0.80168936 0.6762192 0.7857936 0
sample3 78.68660 -0.90957285 0.5818459 0.1615234 0
sample4 37.97143 1.85343795 -1.1533996 -2.0012874 0
sample5 64.18945 0.06243127 0.1321777 -0.2098076 1
sample6 72.96444 -0.07292561 0.1194145 -0.7813484 1
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 39.1911 1.00014 1.328524 0.2491012 0.637745 190.457 197.427
gene2 137.6169 1.00007 3.679696 0.0550815 0.324363 251.397 258.367
gene3 86.8449 1.00011 3.552051 0.0594872 0.324363 219.372 226.343
gene4 87.2146 1.00007 0.757062 0.3842582 0.738958 232.980 239.950
gene5 81.5867 1.00005 1.973751 0.1600585 0.597271 219.215 226.185
gene6 32.2989 1.00014 4.482746 0.0342553 0.324363 192.059 199.029
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 39.1911 0.2896794 0.529627 0.546950 0.584413 0.942601 190.457
gene2 137.6169 -0.2366866 0.407039 -0.581484 0.560914 0.934857 251.397
gene3 86.8449 -0.5305983 0.533731 -0.994130 0.320160 0.800399 219.372
gene4 87.2146 -0.4482322 0.504428 -0.888595 0.374221 0.825994 232.980
gene5 81.5867 -0.0724965 0.507823 -0.142759 0.886480 0.996901 219.215
gene6 32.2989 -0.4074043 0.490605 -0.830412 0.406306 0.825994 192.059
BIC
<numeric>
gene1 197.427
gene2 258.367
gene3 226.343
gene4 239.950
gene5 226.185
gene6 199.029
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 39.1911 -0.401565 0.890752 -0.450815 0.6521227 0.807380 190.457
gene2 137.6169 0.214571 0.685084 0.313204 0.7541254 0.852702 251.397
gene3 86.8449 0.652247 0.897167 0.727007 0.4672214 0.720540 219.372
gene4 87.2146 -0.486055 0.849679 -0.572045 0.5672914 0.746436 232.980
gene5 81.5867 -1.757396 0.876637 -2.004702 0.0449949 0.404715 219.215
gene6 32.2989 0.971748 0.805770 1.205987 0.2278225 0.517778 192.059
BIC
<numeric>
gene1 197.427
gene2 258.367
gene3 226.343
gene4 239.950
gene5 226.185
gene6 199.029
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>
gene32 80.3734 1.00004 8.58306 0.00339379 0.169690 217.458 224.428
gene10 74.7391 1.00014 5.39587 0.02019746 0.324363 199.958 206.929
gene24 85.0140 1.00008 5.37053 0.02048636 0.324363 211.929 218.899
gene6 32.2989 1.00014 4.48275 0.03425527 0.324363 192.059 199.029
gene23 67.1587 1.00018 4.08097 0.04339076 0.324363 203.180 210.150
gene7 26.4674 1.00006 3.81945 0.05066994 0.324363 185.580 192.550
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 (2023-06-16 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.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 snow_0.4-4
[28] htmltools_0.5.6.1 sass_0.4.7 RCurl_1.98-1.12
[31] yaml_2.3.7 pillar_1.9.0 crayon_1.5.2
[34] jquerylib_0.1.4 DelayedArray_0.28.0 cachem_1.0.8
[37] abind_1.4-5 nlme_3.1-163 genefilter_1.84.0
[40] tidyselect_1.2.0 locfit_1.5-9.8 digest_0.6.33
[43] dplyr_1.1.3 labeling_0.4.3 splines_4.3.1
[46] fastmap_1.1.1 grid_4.3.1 colorspace_2.1-0
[49] cli_3.6.1 SparseArray_1.2.0 magrittr_2.0.3
[52] S4Arrays_1.2.0 survival_3.5-7 XML_3.99-0.14
[55] utf8_1.2.4 withr_2.5.2 scales_1.2.1
[58] bit64_4.0.5 rmarkdown_2.25 XVector_0.42.0
[61] httr_1.4.7 bit_4.0.5 png_0.1-8
[64] memoise_2.0.1 evaluate_0.23 knitr_1.45
[67] mgcv_1.9-0 rlang_1.1.1 Rcpp_1.0.11
[70] DBI_1.1.3 xtable_1.8-4 glue_1.6.2
[73] annotate_1.80.0 jsonlite_1.8.7 R6_2.5.1
[76] 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.