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 14 125 83 106 3 1 71 74
gene2 18 1 2 334 8 4 1 2 191
gene3 398 4 3 46 96 5 2 3 36
gene4 1 46 20 295 54 2 2 3 1078
gene5 112 307 41 1 32 6 1 50 10
gene6 125 2 1 64 7 86 16 97 56
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 1 1 113 60 18 5 1 21
gene2 1 25 1 3 122 375 568 185
gene3 4 36 2 344 808 339 44 1
gene4 37 8 1 1 102 230 1 249
gene5 1 36 2 136 36 1 103 4
gene6 61 1 4 269 41 122 154 4
sample18 sample19 sample20
gene1 417 165 2
gene2 6 84 14
gene3 256 315 59
gene4 81 589 328
gene5 92 342 10
gene6 35 129 146
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 61.62516 1.2517649 -0.3978103 -0.3072051 1
sample2 63.07291 0.9409884 0.9133252 -1.4889276 2
sample3 63.12365 0.4793448 0.9962835 -0.3014775 0
sample4 63.07130 0.6407252 -0.8100224 -0.1664305 1
sample5 34.41759 0.9836666 0.4597228 -0.3196812 1
sample6 51.76991 1.1872650 -0.8454540 -0.3558961 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 65.9010 1.00009 0.0151460 0.902357 0.938403 198.691 205.661
gene2 87.5544 1.00006 0.7902230 0.374080 0.688780 204.613 211.583
gene3 101.4641 1.00005 1.9208547 0.165787 0.406354 227.696 234.666
gene4 107.4342 1.00019 0.0686016 0.793696 0.938403 227.746 234.716
gene5 51.7667 1.00012 0.0273460 0.868969 0.938403 210.255 217.225
gene6 56.5872 1.15916 0.3205784 0.813923 0.938403 216.946 224.075
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 65.9010 0.233524 0.454366 0.5139564 0.6072825 0.820652 198.691
gene2 87.5544 -1.135195 0.501517 -2.2635232 0.0236035 0.214104 204.613
gene3 101.4641 -0.408630 0.487602 -0.8380405 0.4020079 0.758445 227.696
gene4 107.4342 -0.036771 0.527643 -0.0696891 0.9444411 0.962649 227.746
gene5 51.7667 0.670380 0.495330 1.3534004 0.1759278 0.487766 210.255
gene6 56.5872 -0.393224 0.421550 -0.9328036 0.3509214 0.758445 216.946
BIC
<numeric>
gene1 205.661
gene2 211.583
gene3 234.666
gene4 234.716
gene5 217.225
gene6 224.075
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 65.9010 -3.000087 1.071483 -2.799938 0.00511125 0.0718983 198.691
gene2 87.5544 2.390498 1.208596 1.977913 0.04793849 0.2438920 204.613
gene3 101.4641 1.977628 1.144369 1.728139 0.08396335 0.2998691 227.696
gene4 107.4342 1.866692 1.226441 1.522040 0.12799912 0.3345118 227.746
gene5 51.7667 -0.433153 1.146271 -0.377880 0.70551964 0.9045124 210.255
gene6 56.5872 0.525025 0.972699 0.539762 0.58936137 0.8159530 216.946
BIC
<numeric>
gene1 205.661
gene2 211.583
gene3 234.666
gene4 234.716
gene5 217.225
gene6 224.075
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>
gene22 149.8081 1.00084 14.28091 0.000158832 0.00794161 220.149 227.120
gene10 132.2014 1.00004 10.72449 0.001057650 0.02644125 237.050 244.020
gene33 44.3178 1.00036 9.50189 0.002055727 0.03426212 191.913 198.883
gene46 76.9610 1.00004 8.50563 0.003541675 0.04399800 213.156 220.127
gene27 92.8809 1.00003 8.11131 0.004399800 0.04399800 198.918 205.889
gene13 25.7498 1.00018 7.44596 0.006359302 0.05299418 157.055 164.025
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.1.1 (2021-08-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server x64 (build 17763)
Matrix products: default
locale:
[1] LC_COLLATE=C
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats4 stats graphics grDevices utils datasets methods
[8] base
other attached packages:
[1] ggplot2_3.3.5 BiocParallel_1.28.0
[3] NBAMSeq_1.10.0 SummarizedExperiment_1.24.0
[5] Biobase_2.54.0 GenomicRanges_1.46.0
[7] GenomeInfoDb_1.30.0 IRanges_2.28.0
[9] S4Vectors_0.32.0 BiocGenerics_0.40.0
[11] MatrixGenerics_1.6.0 matrixStats_0.61.0
loaded via a namespace (and not attached):
[1] httr_1.4.2 sass_0.4.0 bit64_4.0.5
[4] jsonlite_1.7.2 splines_4.1.1 bslib_0.3.1
[7] assertthat_0.2.1 highr_0.9 blob_1.2.2
[10] GenomeInfoDbData_1.2.7 yaml_2.2.1 pillar_1.6.4
[13] RSQLite_2.2.8 lattice_0.20-45 glue_1.4.2
[16] digest_0.6.28 RColorBrewer_1.1-2 XVector_0.34.0
[19] colorspace_2.0-2 htmltools_0.5.2 Matrix_1.3-4
[22] DESeq2_1.34.0 XML_3.99-0.8 pkgconfig_2.0.3
[25] genefilter_1.76.0 zlibbioc_1.40.0 purrr_0.3.4
[28] xtable_1.8-4 snow_0.4-3 scales_1.1.1
[31] tibble_3.1.5 annotate_1.72.0 mgcv_1.8-38
[34] KEGGREST_1.34.0 farver_2.1.0 generics_0.1.1
[37] ellipsis_0.3.2 withr_2.4.2 cachem_1.0.6
[40] survival_3.2-13 magrittr_2.0.1 crayon_1.4.1
[43] memoise_2.0.0 evaluate_0.14 fansi_0.5.0
[46] nlme_3.1-153 tools_4.1.1 lifecycle_1.0.1
[49] stringr_1.4.0 locfit_1.5-9.4 munsell_0.5.0
[52] DelayedArray_0.20.0 AnnotationDbi_1.56.0 Biostrings_2.62.0
[55] compiler_4.1.1 jquerylib_0.1.4 rlang_0.4.12
[58] grid_4.1.1 RCurl_1.98-1.5 labeling_0.4.2
[61] bitops_1.0-7 rmarkdown_2.11 gtable_0.3.0
[64] DBI_1.1.1 R6_2.5.1 knitr_1.36
[67] dplyr_1.0.7 fastmap_1.1.0 bit_4.0.4
[70] utf8_1.2.2 stringi_1.7.5 parallel_4.1.1
[73] Rcpp_1.0.7 vctrs_0.3.8 geneplotter_1.72.0
[76] png_0.1-7 tidyselect_1.1.1 xfun_0.27
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.