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 1 1 375 102 1 332 20 141
gene2 71 12 7 3 8 189 1 38 100
gene3 76 16 1 20 46 176 60 176 5
gene4 125 55 324 1 42 2 129 183 2
gene5 20 4 118 19 103 11 46 356 60
gene6 4 2 123 2 23 1 24 199 99
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 19 399 11 7 5 125 51 26
gene2 5 1 1 1 53 4 30 163
gene3 1 19 3 40 1 1 2 1
gene4 419 2 14 10 60 55 509 167
gene5 211 2 83 1 470 98 125 126
gene6 273 5 25 2 1 297 70 155
sample18 sample19 sample20
gene1 1 149 5
gene2 81 276 1010
gene3 403 1 2
gene4 88 1 61
gene5 9 7 46
gene6 85 267 111
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 66.85296 0.1977064 0.91432247 1.3359741 0
sample2 21.12231 0.7733010 -0.61751034 -0.7682266 0
sample3 38.41918 -1.1293677 0.03642817 1.6802529 2
sample4 33.41475 -1.0303210 -1.30283166 0.3928843 2
sample5 63.25405 -1.0039299 -1.53395832 0.3294808 2
sample6 34.52833 1.6164039 -0.21915569 -0.6614028 0
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 82.8226 1.00005 0.05828777 0.809261 0.963406 216.531 223.501
gene2 73.7929 1.00007 2.46759252 0.116220 0.605413 213.431 220.401
gene3 54.2551 1.00011 0.47114460 0.492641 0.746426 189.861 196.831
gene4 104.5287 1.00004 1.38887620 0.238601 0.605413 232.412 239.382
gene5 77.1968 1.00004 0.90356660 0.341837 0.681236 227.526 234.496
gene6 83.4661 1.00006 0.00185077 0.965698 0.969067 224.066 231.036
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 82.8226 -0.0560974 0.391026 -0.143462 0.88592532 0.9928059 216.531
gene2 73.7929 0.3003064 0.377588 0.795329 0.42642236 0.8195422 213.431
gene3 54.2551 1.0990539 0.384804 2.856139 0.00428827 0.0357356 189.861
gene4 104.5287 -0.2338392 0.358076 -0.653043 0.51372853 0.8195422 232.412
gene5 77.1968 0.0900283 0.314360 0.286386 0.77458216 0.9682277 227.526
gene6 83.4661 0.2247063 0.362293 0.620233 0.53510408 0.8195422 224.066
BIC
<numeric>
gene1 223.501
gene2 220.401
gene3 196.831
gene4 239.382
gene5 234.496
gene6 231.036
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 82.8226 -0.649093 0.955453 -0.679356 0.4969122 0.803186 216.531
gene2 73.7929 -1.066900 0.923389 -1.155417 0.2479196 0.692521 213.431
gene3 54.2551 0.313046 0.934980 0.334815 0.7377645 0.878291 189.861
gene4 104.5287 1.839674 0.875391 2.101546 0.0355931 0.361181 232.412
gene5 77.1968 1.483102 0.769045 1.928498 0.0537932 0.361181 227.526
gene6 83.4661 1.755147 0.885899 1.981205 0.0475683 0.361181 224.066
BIC
<numeric>
gene1 223.501
gene2 220.401
gene3 196.831
gene4 239.382
gene5 234.496
gene6 231.036
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>
gene21 76.6865 1.00024 9.04208 0.00263894 0.131947 206.300 213.270
gene46 96.8958 1.00006 5.87668 0.01534436 0.243961 229.545 236.515
gene13 89.0793 1.00006 5.72448 0.01673363 0.243961 215.708 222.678
gene7 87.8366 1.00016 5.45510 0.01951692 0.243961 223.516 230.486
gene32 67.7956 1.00007 3.96552 0.04644752 0.464475 218.653 225.623
gene40 86.6503 1.00009 3.46852 0.06256151 0.521346 187.709 194.679
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.0 (2020-04-24)
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.0/Resources/lib/libRblas.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/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.0 BiocParallel_1.22.0
[3] NBAMSeq_1.4.0 SummarizedExperiment_1.18.0
[5] DelayedArray_0.14.0 matrixStats_0.56.0
[7] Biobase_2.48.0 GenomicRanges_1.40.0
[9] GenomeInfoDb_1.24.0 IRanges_2.22.0
[11] S4Vectors_0.26.0 BiocGenerics_0.34.0
loaded via a namespace (and not attached):
[1] Rcpp_1.0.4.6 locfit_1.5-9.4 lattice_0.20-41
[4] assertthat_0.2.1 digest_0.6.25 R6_2.4.1
[7] RSQLite_2.2.0 evaluate_0.14 pillar_1.4.3
[10] zlibbioc_1.34.0 rlang_0.4.5 annotate_1.66.0
[13] blob_1.2.1 Matrix_1.2-18 rmarkdown_2.1
[16] labeling_0.3 splines_4.0.0 geneplotter_1.66.0
[19] stringr_1.4.0 RCurl_1.98-1.2 bit_1.1-15.2
[22] munsell_0.5.0 compiler_4.0.0 xfun_0.13
[25] pkgconfig_2.0.3 mgcv_1.8-31 htmltools_0.4.0
[28] tidyselect_1.0.0 tibble_3.0.1 GenomeInfoDbData_1.2.3
[31] XML_3.99-0.3 withr_2.2.0 crayon_1.3.4
[34] dplyr_0.8.5 bitops_1.0-6 grid_4.0.0
[37] nlme_3.1-147 xtable_1.8-4 gtable_0.3.0
[40] lifecycle_0.2.0 DBI_1.1.0 magrittr_1.5
[43] scales_1.1.0 stringi_1.4.6 farver_2.0.3
[46] XVector_0.28.0 genefilter_1.70.0 ellipsis_0.3.0
[49] vctrs_0.2.4 RColorBrewer_1.1-2 tools_4.0.0
[52] bit64_0.9-7 glue_1.4.0 DESeq2_1.28.0
[55] purrr_0.3.4 survival_3.1-12 yaml_2.2.1
[58] AnnotationDbi_1.50.0 colorspace_1.4-1 memoise_1.1.0
[61] knitr_1.28
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.