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
gene1 1 68 10 6 1 40 1 97
gene2 41 95 143 32 143 57 40 1
gene3 5 582 9 25 169 534 201 4
gene4 13 7 3 98 2 9 128 21
gene5 865 86 194 20 15 12 79 48
gene6 435 9 68 4 58 55 123 8
sample9 sample10 sample11 sample12 sample13 sample14 sample15
gene1 60 1 499 13 368 28 3
gene2 1 35 1 10 151 80 574
gene3 26 367 129 13 1 192 46
gene4 10 179 12 4 2 12 1
gene5 1 22 52 553 38 416 2
gene6 134 115 247 43 30 103 501
sample16 sample17 sample18 sample19 sample20
gene1 6 487 1 14 14
gene2 9 1 9 76 112
gene3 4 7 8 8 13
gene4 141 2 1 11 390
gene5 234 403 44 252 67
gene6 79 31 14 40 143
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 55.65481 1.0657264 -0.62217573 1.3619716 1
sample2 63.89513 -1.3758422 -0.52953989 -0.7037095 2
sample3 63.83531 0.5011832 -1.81016743 -1.0769374 1
sample4 73.20474 1.0981266 -0.11230582 -0.8525421 2
sample5 24.72809 -2.8186670 0.09918202 0.4876742 1
sample6 65.51147 0.6251068 -0.39760542 1.7556906 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;
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 5 columns
baseMean edf stat
<numeric> <numeric> <numeric>
gene1 100.937580266488 1.00004934026771 0.00739771817458906
gene2 64.1566733017586 1.00013024983567 20.6606039556142
gene3 98.5440762816742 1.00011596573921 8.41353707770038
gene4 42.8141813606958 1.00003510580841 1.36409888427823
gene5 175.611056201745 1.00016073385872 2.29502462179236
gene6 94.4200060200211 1.00003675221347 1.1392203146464
pvalue padj
<numeric> <numeric>
gene1 0.931496982211732 0.931496982211732
gene2 5.49086104824696e-06 0.000274543052412348
gene3 0.00372833308506262 0.0266309506075901
gene4 0.242853541081588 0.510846422256105
gene5 0.129855187879848 0.381927023176022
gene6 0.285836478487016 0.510846422256105
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 6 columns
baseMean coef SE
<numeric> <numeric> <numeric>
gene1 100.937580266488 0.691131557509217 0.415793265615062
gene2 64.1566733017586 -0.796355771776422 0.345981106392014
gene3 98.5440762816742 -1.24207843429552 0.382782346954202
gene4 42.8141813606958 -0.215753187480105 0.399518956600663
gene5 175.611056201745 0.626248618609277 0.434026691631932
gene6 94.4200060200211 0.381498101589746 0.340468204185752
stat pvalue padj
<numeric> <numeric> <numeric>
gene1 1.66219997932593 0.0964726841805165 0.299659193166566
gene2 -2.30173196473368 0.0213502924093895 0.0996898699906995
gene3 -3.24486864187634 0.00117504814221757 0.0184090875614086
gene4 -0.540032416273454 0.589174677197809 0.74331882603971
gene5 1.44288042805523 0.149054157443745 0.368712915781896
gene6 1.12051021769307 0.262496400908593 0.46471975757829
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 6 columns
baseMean coef SE
<numeric> <numeric> <numeric>
gene1 100.937580266488 1.78966020892694 0.982971186883701
gene2 64.1566733017586 -1.75408035097624 0.888800424973438
gene3 98.5440762816742 0.0348497330412195 0.926950514479007
gene4 42.8141813606958 -0.572222219450487 0.984081448797224
gene5 175.611056201745 1.01270598805079 1.05472301348336
gene6 94.4200060200211 0.178773027907122 0.828194557609955
stat pvalue padj
<numeric> <numeric> <numeric>
gene1 1.82066395516706 0.068657954526038 0.264069055869377
gene2 -1.97353680499046 0.0484344355945416 0.264069055869377
gene3 0.0375961094976109 0.970009709901786 0.987761021554848
gene4 -0.581478514964259 0.56091799435778 0.846718160930295
gene5 0.960162976539396 0.336973197102616 0.707658431242869
gene6 0.215858733028908 0.82909786500273 0.948147521787377
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 5 columns
baseMean edf stat
<numeric> <numeric> <numeric>
gene2 64.1566733017586 1.00013024983567 20.6606039556142
gene10 84.8465952698202 1.00008243290987 13.6902855589529
gene8 55.9271170671289 1.00026287520925 13.3033984048181
gene30 15.7090102460532 1.0000651518018 9.82340324781178
gene22 151.701858867258 1.00006573042838 9.60046395262862
gene34 164.578239460982 1.00005879526844 9.4297628600664
pvalue padj
<numeric> <numeric>
gene2 5.49086104824696e-06 0.000274543052412348
gene10 0.000215712061941596 0.00443108302025615
gene8 0.000265864981215369 0.00443108302025615
gene30 0.00172418018146628 0.0177973244103175
gene22 0.00194666870785135 0.0177973244103175
gene34 0.00213567892923811 0.0177973244103175
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 Under development (unstable) (2019-11-03 r77362)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: OS X El Capitan 10.11.6
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.0.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.2.1 NBAMSeq_1.3.0
[3] SummarizedExperiment_1.17.0 DelayedArray_0.13.0
[5] BiocParallel_1.21.0 matrixStats_0.55.0
[7] Biobase_2.47.0 GenomicRanges_1.39.1
[9] GenomeInfoDb_1.23.0 IRanges_2.21.1
[11] S4Vectors_0.25.0 BiocGenerics_0.33.0
loaded via a namespace (and not attached):
[1] bit64_0.9-7 splines_4.0.0 Formula_1.2-3
[4] assertthat_0.2.1 latticeExtra_0.6-28 blob_1.2.0
[7] GenomeInfoDbData_1.2.2 yaml_2.2.0 pillar_1.4.2
[10] RSQLite_2.1.2 backports_1.1.5 lattice_0.20-38
[13] glue_1.3.1 digest_0.6.22 RColorBrewer_1.1-2
[16] XVector_0.27.0 checkmate_1.9.4 colorspace_1.4-1
[19] htmltools_0.4.0 Matrix_1.2-17 DESeq2_1.27.2
[22] XML_3.98-1.20 pkgconfig_2.0.3 genefilter_1.69.0
[25] zlibbioc_1.33.0 purrr_0.3.3 xtable_1.8-4
[28] scales_1.0.0 htmlTable_1.13.2 tibble_2.1.3
[31] annotate_1.65.0 mgcv_1.8-30 withr_2.1.2
[34] nnet_7.3-12 lazyeval_0.2.2 survival_3.1-6
[37] magrittr_1.5 crayon_1.3.4 memoise_1.1.0
[40] evaluate_0.14 nlme_3.1-141 foreign_0.8-72
[43] tools_4.0.0 data.table_1.12.6 stringr_1.4.0
[46] locfit_1.5-9.1 munsell_0.5.0 cluster_2.1.0
[49] AnnotationDbi_1.49.0 compiler_4.0.0 rlang_0.4.1
[52] grid_4.0.0 RCurl_1.95-4.12 rstudioapi_0.10
[55] htmlwidgets_1.5.1 labeling_0.3 bitops_1.0-6
[58] base64enc_0.1-3 rmarkdown_1.16 gtable_0.3.0
[61] DBI_1.0.0 R6_2.4.0 gridExtra_2.3
[64] knitr_1.25 dplyr_0.8.3 zeallot_0.1.0
[67] bit_1.1-14 Hmisc_4.2-0 stringi_1.4.3
[70] Rcpp_1.0.2 geneplotter_1.65.0 vctrs_0.2.0
[73] rpart_4.1-15 acepack_1.4.1 tidyselect_0.2.5
[76] xfun_0.10
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.