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 383 105 1 13 2 269 30 2
gene2 21 1 107 42 62 590 33 6 3
gene3 5 1 224 54 348 93 48 3 106
gene4 40 3 88 2 84 1 10 43 1
gene5 7 1 1 32 264 495 1 313 25
gene6 18 187 429 54 113 10 71 259 329
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 1 829 90 25 26 190 1 29
gene2 14 1 4 81 5 11 7 240
gene3 1 18 113 116 6 24 47 519
gene4 142 1 89 254 18 54 51 165
gene5 38 244 2 22 584 38 19 11
gene6 166 20 8 18 409 1 333 1
sample18 sample19 sample20
gene1 362 555 87
gene2 1 19 71
gene3 11 32 1
gene4 1 237 15
gene5 129 54 88
gene6 57 256 4
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 52.55769 0.4057805 0.6013479 0.2075753 2
sample2 23.30403 -0.9993752 -1.3777764 -0.5602620 0
sample3 51.68711 0.2635050 1.1771741 0.1922988 1
sample4 61.19599 -0.2998339 -0.1286972 0.8450080 1
sample5 29.01202 -0.1791292 0.6729535 1.2452066 0
sample6 28.43087 -0.1542338 0.4686567 0.3787563 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 139.5799 1.00011 1.91730054 0.16617764 0.3381527 235.366 242.337
gene2 48.4910 1.00007 4.30997779 0.03790077 0.1579199 202.808 209.778
gene3 71.3969 1.00005 1.46381633 0.22635648 0.4191787 222.196 229.166
gene4 48.3398 1.00003 0.00192697 0.96532379 0.9653238 210.856 217.826
gene5 110.4328 1.00045 1.16933654 0.27941004 0.4989465 227.762 234.732
gene6 135.3996 1.00005 7.01952648 0.00806541 0.0685061 239.698 246.669
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 139.5799 -1.009102 0.581583 -1.735095 0.0827240 0.335890 235.366
gene2 48.4910 -0.914815 0.532511 -1.717927 0.0858098 0.335890 202.808
gene3 71.3969 0.229234 0.542713 0.422385 0.6727439 0.814590 222.196
gene4 48.3398 0.209226 0.525609 0.398065 0.6905823 0.814590 210.856
gene5 110.4328 0.558515 0.533348 1.047186 0.2950140 0.702414 227.762
gene6 135.3996 0.877554 0.495197 1.772130 0.0763731 0.335890 239.698
BIC
<numeric>
gene1 242.337
gene2 209.778
gene3 229.166
gene4 217.826
gene5 234.732
gene6 246.669
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 139.5799 0.00271884 0.939435 0.00289412 0.997691 0.997691 235.366
gene2 48.4910 0.26728609 0.854884 0.31265789 0.754541 0.852870 202.808
gene3 71.3969 -0.15873737 0.877887 -0.18081751 0.856511 0.892199 222.196
gene4 48.3398 0.40327761 0.847841 0.47565231 0.634322 0.834929 210.856
gene5 110.4328 -0.66411027 0.865337 -0.76745872 0.442809 0.763463 227.762
gene6 135.3996 -0.82391355 0.802511 -1.02666949 0.304576 0.722251 239.698
BIC
<numeric>
gene1 242.337
gene2 209.778
gene3 229.166
gene4 217.826
gene5 234.732
gene6 246.669
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>
gene48 124.6309 1.00015 11.80001 0.000592563 0.0296282 208.393 215.364
gene42 81.2981 1.00016 10.10974 0.001475949 0.0368987 205.221 212.191
gene40 142.2276 1.00006 8.97884 0.002732339 0.0455390 232.839 239.809
gene44 97.6084 1.00004 7.17318 0.007401462 0.0685061 214.788 221.759
gene6 135.3996 1.00005 7.01953 0.008065414 0.0685061 239.698 246.669
gene12 149.3512 1.00018 6.94023 0.008432862 0.0685061 232.933 239.903
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.0 RC (2021-05-10 r80283)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Mojave 10.14.6
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRblas.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib
locale:
[1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] parallel stats4 stats graphics grDevices utils datasets
[8] methods base
other attached packages:
[1] ggplot2_3.3.3 BiocParallel_1.26.0
[3] NBAMSeq_1.8.0 SummarizedExperiment_1.22.0
[5] Biobase_2.52.0 GenomicRanges_1.44.0
[7] GenomeInfoDb_1.28.0 IRanges_2.26.0
[9] S4Vectors_0.30.0 BiocGenerics_0.38.0
[11] MatrixGenerics_1.4.0 matrixStats_0.58.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.0 bslib_0.2.5.1
[7] assertthat_0.2.1 highr_0.9 blob_1.2.1
[10] GenomeInfoDbData_1.2.6 yaml_2.2.1 pillar_1.6.1
[13] RSQLite_2.2.7 lattice_0.20-44 glue_1.4.2
[16] digest_0.6.27 RColorBrewer_1.1-2 XVector_0.32.0
[19] colorspace_2.0-1 htmltools_0.5.1.1 Matrix_1.3-3
[22] DESeq2_1.32.0 XML_3.99-0.6 pkgconfig_2.0.3
[25] genefilter_1.74.0 zlibbioc_1.38.0 purrr_0.3.4
[28] xtable_1.8-4 scales_1.1.1 tibble_3.1.2
[31] annotate_1.70.0 mgcv_1.8-35 KEGGREST_1.32.0
[34] farver_2.1.0 generics_0.1.0 ellipsis_0.3.2
[37] withr_2.4.2 cachem_1.0.5 survival_3.2-11
[40] magrittr_2.0.1 crayon_1.4.1 memoise_2.0.0
[43] evaluate_0.14 fansi_0.4.2 nlme_3.1-152
[46] tools_4.1.0 lifecycle_1.0.0 stringr_1.4.0
[49] locfit_1.5-9.4 munsell_0.5.0 DelayedArray_0.18.0
[52] AnnotationDbi_1.54.0 Biostrings_2.60.0 compiler_4.1.0
[55] jquerylib_0.1.4 rlang_0.4.11 grid_4.1.0
[58] RCurl_1.98-1.3 labeling_0.4.2 bitops_1.0-7
[61] rmarkdown_2.8 gtable_0.3.0 DBI_1.1.1
[64] R6_2.5.0 knitr_1.33 dplyr_1.0.6
[67] fastmap_1.1.0 bit_4.0.4 utf8_1.2.1
[70] stringi_1.6.2 Rcpp_1.0.6 vctrs_0.3.8
[73] geneplotter_1.70.0 png_0.1-7 tidyselect_1.1.1
[76] xfun_0.23
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.