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 181 124 26 56 1 150 51 41 26
gene2 14 264 104 4 47 30 809 782 5
gene3 130 46 76 2 26 5 1 3 70
gene4 14 112 86 329 1 24 2 243 17
gene5 1 58 1 1 4 23 2 20 31
gene6 62 89 66 215 3 250 1 24 1
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 418 49 2 1 64 37 115 1
gene2 26 172 253 135 17 1 110 1
gene3 39 2 233 5 1 24 5 1
gene4 4 37 251 1 2 3 1 57
gene5 236 21 31 1 373 25 12 140
gene6 45 113 133 1 3 3 1 198
sample18 sample19 sample20
gene1 2 227 10
gene2 12 1 157
gene3 1 63 6
gene4 661 188 2
gene5 5 6 4
gene6 2 2 19
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 32.59259 -0.2657889 0.87630545 -0.2490041 0
sample2 52.46539 -0.1991162 -0.78586552 -1.5671406 1
sample3 59.18598 -0.9658973 0.65457936 -2.1245159 0
sample4 58.86643 1.2905880 0.06208294 0.3086465 1
sample5 55.62493 0.3164586 -0.02959217 0.1727345 0
sample6 50.27485 -0.6189322 0.67204385 -1.4086403 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 64.0070 1.00015 5.79062e-01 0.4468797 0.709019 220.943 227.913
gene2 135.9113 1.00006 4.91849e+00 0.0265755 0.147642 225.824 232.794
gene3 30.2640 1.00009 1.31078e+00 0.2522620 0.504524 186.494 193.465
gene4 95.3365 1.00032 3.88297e+00 0.0488054 0.187713 215.976 222.946
gene5 34.5089 1.00002 6.80666e-05 0.9934255 0.993425 187.292 194.262
gene6 47.5108 1.00041 5.83530e-03 0.9397012 0.980188 206.009 212.980
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 64.0070 0.928766 0.683896 1.3580521 0.1744471 0.3465837 220.943
gene2 135.9113 -0.404072 0.624429 -0.6471058 0.5175635 0.7321400 225.824
gene3 30.2640 1.763427 0.715015 2.4662782 0.0136525 0.0687635 186.494
gene4 95.3365 -0.056104 0.751092 -0.0746966 0.9404561 0.9992212 215.976
gene5 34.5089 -1.527640 0.665217 -2.2964537 0.0216500 0.0984089 187.292
gene6 47.5108 -0.307646 0.746958 -0.4118655 0.6804380 0.8723564 206.009
BIC
<numeric>
gene1 227.913
gene2 232.794
gene3 193.465
gene4 222.946
gene5 194.262
gene6 212.980
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 64.0070 1.989243 1.45286 1.369189 0.1709402 0.427350 220.943
gene2 135.9113 3.246345 1.32419 2.451570 0.0142234 0.101596 225.824
gene3 30.2640 2.238391 1.52067 1.471979 0.1410266 0.419170 186.494
gene4 95.3365 0.677263 1.59065 0.425776 0.6702710 0.823535 215.976
gene5 34.5089 -1.691962 1.39937 -1.209093 0.2266273 0.494493 187.292
gene6 47.5108 -0.468518 1.58324 -0.295923 0.7672892 0.856055 206.009
BIC
<numeric>
gene1 227.913
gene2 232.794
gene3 193.465
gene4 222.946
gene5 194.262
gene6 212.980
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>
gene8 80.6806 1.00011 14.17418 0.000166812 0.00834058 200.360 207.331
gene16 97.9762 1.00007 9.62293 0.001922368 0.04011843 211.787 218.757
gene18 132.4689 1.00010 9.21091 0.002407106 0.04011843 247.725 254.695
gene41 48.6366 1.00010 7.50130 0.006167421 0.07709277 187.263 194.234
gene11 86.2609 1.00007 6.81324 0.009050762 0.09050762 216.143 223.113
gene26 60.1304 1.00004 6.27541 0.012244079 0.10203399 193.808 200.779
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 alpha (2020-04-05 r78150)
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.21.2
[3] NBAMSeq_1.3.1 SummarizedExperiment_1.17.5
[5] DelayedArray_0.13.10 matrixStats_0.56.0
[7] Biobase_2.47.3 GenomicRanges_1.39.3
[9] GenomeInfoDb_1.23.16 IRanges_2.21.8
[11] S4Vectors_0.25.15 BiocGenerics_0.33.3
loaded via a namespace (and not attached):
[1] locfit_1.5-9.4 Rcpp_1.0.4.6 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.33.1 rlang_0.4.5 annotate_1.65.1
[13] blob_1.2.1 Matrix_1.2-18 rmarkdown_2.1
[16] labeling_0.3 splines_4.0.0 geneplotter_1.65.0
[19] stringr_1.4.0 RCurl_1.98-1.1 bit_1.1-15.2
[22] munsell_0.5.0 compiler_4.0.0 xfun_0.12
[25] pkgconfig_2.0.3 mgcv_1.8-31 htmltools_0.4.0
[28] tidyselect_1.0.0 tibble_3.0.0 GenomeInfoDbData_1.2.2
[31] XML_3.99-0.3 fansi_0.4.1 withr_2.1.2
[34] crayon_1.3.4 dplyr_0.8.5 bitops_1.0-6
[37] grid_4.0.0 nlme_3.1-145 xtable_1.8-4
[40] gtable_0.3.0 lifecycle_0.2.0 DBI_1.1.0
[43] magrittr_1.5 scales_1.1.0 cli_2.0.2
[46] stringi_1.4.6 farver_2.0.3 XVector_0.27.2
[49] genefilter_1.69.0 ellipsis_0.3.0 vctrs_0.2.4
[52] RColorBrewer_1.1-2 tools_4.0.0 bit64_0.9-7
[55] glue_1.4.0 DESeq2_1.27.29 purrr_0.3.3
[58] survival_3.1-11 yaml_2.2.1 AnnotationDbi_1.49.1
[61] colorspace_1.4-1 memoise_1.1.0 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.