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 39 34 115 17 19 13 46 48
gene2 2 93 20 17 16 287 83 134
gene3 43 588 3 2 128 17 4 20
gene4 271 59 2 122 5 101 2 181
gene5 14 1 8 364 10 21 15 280
gene6 52 29 9 258 56 678 138 576
sample9 sample10 sample11 sample12 sample13 sample14 sample15
gene1 1 26 1 6 67 3 65
gene2 26 387 2 7 167 2 245
gene3 4 44 9 341 126 80 4
gene4 61 53 121 43 237 454 35
gene5 81 1 24 1 1 30 26
gene6 353 1 28 1 143 1 227
sample16 sample17 sample18 sample19 sample20
gene1 232 285 4 700 116
gene2 53 6 412 512 12
gene3 69 105 172 2 114
gene4 1 8 185 9 1
gene5 25 980 1 1 14
gene6 10 1185 57 30 25
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 45.68189 -0.56434193 -0.1173612 -0.2519788 2
sample2 51.33442 0.41000606 -0.1617071 -1.4036705 1
sample3 56.87139 -1.06008148 -0.7716984 0.7171770 0
sample4 48.95940 -1.10558773 1.3713330 1.4466994 2
sample5 70.56267 -0.07950669 -0.1453455 -0.3814853 0
sample6 23.68780 -0.13524617 -2.0316586 0.8921058 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 57.9095664566279 1.00013573983602 0.443762157866825
gene2 86.9893358937095 1.00015413972742 0.696341277787806
gene3 96.3182231397205 1.00008808109268 3.3451279268653
gene4 96.5334722249479 1.0001212353405 1.72679365866303
gene5 57.5197708541638 1.00004614055739 14.2256946779633
gene6 114.710057881245 1.00005900632081 2.11630088134053
pvalue padj
<numeric> <numeric>
gene1 0.505368526554384 0.721955037934834
gene2 0.404116474333547 0.645664960260843
gene3 0.0674377082015472 0.263946328639829
gene4 0.188830494803121 0.496922354745055
gene5 0.000162189177037631 0.00444919490901165
gene6 0.14576016275905 0.448691678733603
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 57.9095664566279 -0.401793435753404 0.347949493681804
gene2 86.9893358937095 0.493214148596027 0.329380396443183
gene3 96.3182231397205 0.62203804067925 0.31008405330605
gene4 96.5334722249479 0.244933096755823 0.335373492008499
gene5 57.5197708541638 -0.923276768185908 0.30269803549055
gene6 114.710057881245 0.0101278789668535 0.326515510276744
stat pvalue padj
<numeric> <numeric> <numeric>
gene1 -1.15474643029899 0.24819427997359 0.427921172368259
gene2 1.49739982683245 0.134289253323167 0.419653916634897
gene3 2.00603041029428 0.0448530035621007 0.196626801368276
gene4 0.730329327130052 0.465188906005355 0.684101332360817
gene5 -3.05015778080506 0.00228721173661384 0.0571802934153459
gene6 0.0310180639145424 0.97525513368319 0.97525513368319
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 57.9095664566279 -0.152465715341341 0.935714002037772
gene2 86.9893358937095 1.11573051233849 0.883856419286787
gene3 96.3182231397205 0.173412175199927 0.837043639096083
gene4 96.5334722249479 3.28186034225279 0.907408045493527
gene5 57.5197708541638 1.56117400974899 0.80186490354437
gene6 114.710057881245 0.345544858838881 0.874950313153578
stat pvalue padj
<numeric> <numeric> <numeric>
gene1 -0.162940508541397 0.870565275804782 0.94219887524746
gene2 1.26234362051566 0.206825166950517 0.537753664004689
gene3 0.207172203574946 0.835875374708727 0.94219887524746
gene4 3.61674150736434 0.000298334964479384 0.0149167482239692
gene5 1.94692896876813 0.0515432510641139 0.293875987158664
gene6 0.394930836236215 0.692893931915481 0.94219887524746
To explore the nonlinear association of covariates, it is 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>
gene5 57.5197708541638 1.00004614055739 14.2256946779633
gene25 57.7613282230133 1.00005291537191 14.0512063570746
gene18 64.1473384266057 1.00005356491994 12.1653257223555
gene8 82.8188382925501 1.00004202638168 9.24046180727509
gene29 79.9065394999251 1.00005026428115 8.86975210650455
gene45 69.256376139901 1.00015760899572 7.9436471763423
pvalue padj
<numeric> <numeric>
gene5 0.000162189177037631 0.00444919490901165
gene25 0.000177967796360466 0.00444919490901165
gene18 0.000486986742951584 0.00811644571585973
gene8 0.00236756958590585 0.0289967213068209
gene29 0.00289967213068209 0.0289967213068209
gene45 0.00482474647616751 0.0384570436076805
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 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server 2012 R2 x64 (build 9600)
Matrix products: default
locale:
[1] LC_COLLATE=C
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] parallel stats4 stats graphics grDevices utils datasets
[8] methods base
other attached packages:
[1] ggplot2_3.1.1 NBAMSeq_1.0.0
[3] SummarizedExperiment_1.14.0 DelayedArray_0.10.0
[5] BiocParallel_1.18.0 matrixStats_0.54.0
[7] Biobase_2.44.0 GenomicRanges_1.36.0
[9] GenomeInfoDb_1.20.0 IRanges_2.18.0
[11] S4Vectors_0.22.0 BiocGenerics_0.30.0
loaded via a namespace (and not attached):
[1] bit64_0.9-7 splines_3.6.0 Formula_1.2-3
[4] assertthat_0.2.1 latticeExtra_0.6-28 blob_1.1.1
[7] GenomeInfoDbData_1.2.1 yaml_2.2.0 pillar_1.3.1
[10] RSQLite_2.1.1 backports_1.1.4 lattice_0.20-38
[13] glue_1.3.1 digest_0.6.18 RColorBrewer_1.1-2
[16] XVector_0.24.0 checkmate_1.9.1 colorspace_1.4-1
[19] htmltools_0.3.6 Matrix_1.2-17 plyr_1.8.4
[22] DESeq2_1.24.0 XML_3.98-1.19 pkgconfig_2.0.2
[25] genefilter_1.66.0 zlibbioc_1.30.0 purrr_0.3.2
[28] xtable_1.8-4 snow_0.4-3 scales_1.0.0
[31] htmlTable_1.13.1 tibble_2.1.1 annotate_1.62.0
[34] mgcv_1.8-28 withr_2.1.2 nnet_7.3-12
[37] lazyeval_0.2.2 survival_2.44-1.1 magrittr_1.5
[40] crayon_1.3.4 memoise_1.1.0 evaluate_0.13
[43] nlme_3.1-139 foreign_0.8-71 tools_3.6.0
[46] data.table_1.12.2 stringr_1.4.0 locfit_1.5-9.1
[49] munsell_0.5.0 cluster_2.0.9 AnnotationDbi_1.46.0
[52] compiler_3.6.0 rlang_0.3.4 grid_3.6.0
[55] RCurl_1.95-4.12 rstudioapi_0.10 htmlwidgets_1.3
[58] labeling_0.3 bitops_1.0-6 base64enc_0.1-3
[61] rmarkdown_1.12 gtable_0.3.0 DBI_1.0.0
[64] R6_2.4.0 gridExtra_2.3 knitr_1.22
[67] dplyr_0.8.0.1 bit_1.1-14 Hmisc_4.2-0
[70] stringi_1.4.3 Rcpp_1.0.1 geneplotter_1.62.0
[73] rpart_4.1-15 acepack_1.4.1 tidyselect_0.2.5
[76] xfun_0.6
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.