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 362 22 2 102 2 25 18 32 3
gene2 23 111 35 255 3 3 292 47 245
gene3 11 43 938 1 7 1 19 637 395
gene4 1 16 6 3 7 2 263 7 1
gene5 2 34 62 1 24 9 4 1 2
gene6 1 413 129 190 179 3 4 1 1
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 6 63 196 1 280 2 1 75
gene2 46 7 126 120 83 2 1 1
gene3 23 446 61 187 1 703 2 11
gene4 89 29 3 1 67 73 158 97
gene5 125 1 52 55 1 230 60 42
gene6 69 1 1 431 7 88 383 19
sample18 sample19 sample20
gene1 193 519 53
gene2 40 78 109
gene3 33 58 83
gene4 133 90 160
gene5 176 7 604
gene6 4 265 2
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 22.53383 -1.13496381 -1.3265657 -0.8699502 0
sample2 41.17227 0.24465137 1.7666600 0.2145658 1
sample3 47.41689 -1.95575612 -0.8706489 -0.9562638 2
sample4 73.81356 -0.94301605 1.3900854 1.5009264 1
sample5 56.76245 0.07314511 0.4986531 0.8311680 1
sample6 44.41126 -0.48479663 -1.3003933 -0.5657210 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 72.9835 1.00006 0.00929685 0.92346851 0.9368683 215.815 222.785
gene2 74.4635 1.00085 0.39278767 0.53081425 0.8561520 222.171 229.142
gene3 130.6823 1.00008 2.83444991 0.09229638 0.3549861 229.096 236.066
gene4 57.2213 1.00009 5.81672777 0.01588883 0.1324069 206.776 213.746
gene5 53.5951 1.00013 3.59743618 0.05792601 0.2633000 207.822 214.792
gene6 87.3457 1.00010 8.64469375 0.00328379 0.0820947 210.879 217.849
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 72.9835 -0.6508942 0.373364 -1.743325 0.0812769 0.457957 215.815
gene2 74.4635 0.4362013 0.360640 1.209519 0.2264635 0.598371 222.171
gene3 130.6823 0.1487532 0.364913 0.407640 0.6835378 0.840860 229.096
gene4 57.2213 0.3962301 0.383663 1.032755 0.3017187 0.647779 206.776
gene5 53.5951 0.2219396 0.409085 0.542527 0.5874557 0.793859 207.822
gene6 87.3457 -0.0688273 0.418123 -0.164610 0.8692511 0.912010 210.879
BIC
<numeric>
gene1 222.785
gene2 229.142
gene3 236.066
gene4 213.746
gene5 214.792
gene6 217.849
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 72.9835 -1.80469224 1.19684 -1.50787938 0.1315854 0.442904 215.815
gene2 74.4635 0.12352581 1.16460 0.10606677 0.9155294 0.973967 222.171
gene3 130.6823 2.50858362 1.17397 2.13684051 0.0326110 0.375964 229.096
gene4 57.2213 0.00246681 1.24221 0.00198583 0.9984155 0.998416 206.776
gene5 53.5951 1.54516743 1.33611 1.15646996 0.2474890 0.591590 207.822
gene6 87.3457 2.75056692 1.48706 1.84966890 0.0643613 0.375964 210.879
BIC
<numeric>
gene1 222.785
gene2 229.142
gene3 236.066
gene4 213.746
gene5 214.792
gene6 217.849
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>
gene30 73.6596 1.00007 8.79316 0.00302563 0.0820947 208.532 215.502
gene6 87.3457 1.00010 8.64469 0.00328379 0.0820947 210.879 217.849
gene41 52.3455 1.00007 7.64398 0.00569988 0.0949980 213.637 220.608
gene39 90.6803 1.00011 6.99513 0.00818142 0.1022678 224.591 231.562
gene45 59.4118 1.00014 6.08315 0.01366168 0.1324069 210.065 217.035
gene4 57.2213 1.00009 5.81673 0.01588883 0.1324069 206.776 213.746
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.0.3 (2020-10-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.5 LTS
Matrix products: default
BLAS: /home/biocbuild/bbs-3.12-bioc/R/lib/libRblas.so
LAPACK: /home/biocbuild/bbs-3.12-bioc/R/lib/libRlapack.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=C
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] parallel stats4 stats graphics grDevices utils datasets
[8] methods base
other attached packages:
[1] ggplot2_3.3.2 BiocParallel_1.24.0
[3] NBAMSeq_1.6.0 SummarizedExperiment_1.20.0
[5] Biobase_2.50.0 GenomicRanges_1.42.0
[7] GenomeInfoDb_1.26.0 IRanges_2.24.0
[9] S4Vectors_0.28.0 BiocGenerics_0.36.0
[11] MatrixGenerics_1.2.0 matrixStats_0.57.0
loaded via a namespace (and not attached):
[1] Rcpp_1.0.5 locfit_1.5-9.4 lattice_0.20-41
[4] digest_0.6.27 R6_2.4.1 RSQLite_2.2.1
[7] evaluate_0.14 httr_1.4.2 pillar_1.4.6
[10] zlibbioc_1.36.0 rlang_0.4.8 annotate_1.68.0
[13] blob_1.2.1 Matrix_1.2-18 rmarkdown_2.5
[16] labeling_0.4.2 splines_4.0.3 geneplotter_1.68.0
[19] stringr_1.4.0 RCurl_1.98-1.2 bit_4.0.4
[22] munsell_0.5.0 DelayedArray_0.16.0 compiler_4.0.3
[25] xfun_0.18 pkgconfig_2.0.3 mgcv_1.8-33
[28] htmltools_0.5.0 tidyselect_1.1.0 tibble_3.0.4
[31] GenomeInfoDbData_1.2.4 XML_3.99-0.5 withr_2.3.0
[34] crayon_1.3.4 dplyr_1.0.2 bitops_1.0-6
[37] grid_4.0.3 nlme_3.1-150 xtable_1.8-4
[40] gtable_0.3.0 lifecycle_0.2.0 DBI_1.1.0
[43] magrittr_1.5 scales_1.1.1 stringi_1.5.3
[46] farver_2.0.3 XVector_0.30.0 genefilter_1.72.0
[49] ellipsis_0.3.1 vctrs_0.3.4 generics_0.0.2
[52] RColorBrewer_1.1-2 tools_4.0.3 bit64_4.0.5
[55] glue_1.4.2 DESeq2_1.30.0 purrr_0.3.4
[58] survival_3.2-7 yaml_2.2.1 AnnotationDbi_1.52.0
[61] colorspace_1.4-1 memoise_1.1.0 knitr_1.30
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). BioMed Central: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). Oxford University Press: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). Oxford University Press:2672–8.