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 87 199 1 4 22 1400 450 53
gene2 11 387 210 323 163 682 139 20 4
gene3 4 47 25 53 23 15 611 3 27
gene4 150 334 113 478 19 27 3 10 164
gene5 4 385 80 1 3 4 218 2 505
gene6 209 294 3 75 408 1 37 32 65
sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1 72 18 2 166 215 15 2 101
gene2 5 502 313 23 374 1 73 106
gene3 160 46 166 10 17 970 920 53
gene4 12 275 1 2 189 286 21 220
gene5 73 23 22 27 15 25 189 34
gene6 1 3 129 311 54 2 195 5
sample18 sample19 sample20
gene1 255 1 66
gene2 2 300 684
gene3 109 134 800
gene4 24 69 28
gene5 18 36 108
gene6 2 317 3
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 53.34750 -0.2247691 0.06446786 -0.3313058 0
sample2 72.48195 0.6733607 0.79475848 -1.0492868 2
sample3 43.61273 -0.2916392 0.60957580 0.7930560 2
sample4 75.09485 0.8864967 0.80274262 0.1688156 2
sample5 23.16734 0.7224085 1.14767780 0.1862156 1
sample6 44.15943 0.2235846 -1.33699687 1.1669175 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 158.0845 1.00009 1.07928e+01 0.00101972 0.0509859 229.764 236.734
gene2 204.5902 1.00007 1.84654e-02 0.89208222 0.9632527 257.366 264.336
gene3 173.8494 1.00006 8.98823e-01 0.34310322 0.6483236 254.144 261.114
gene4 113.2686 1.00008 5.77412e+00 0.01627357 0.1356131 231.971 238.941
gene5 64.7502 1.00006 2.00604e-01 0.65425958 0.8593302 218.827 225.797
gene6 75.9488 1.00010 1.23881e-05 0.99940303 0.9994030 220.336 227.306
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 158.0845 0.345618 0.442422 0.781194 0.4346883 0.658619 229.764
gene2 204.5902 0.682681 0.400622 1.704051 0.0883716 0.381821 257.366
gene3 173.8494 -0.582570 0.418137 -1.393250 0.1635443 0.469899 254.144
gene4 113.2686 0.150845 0.368883 0.408924 0.6825956 0.812614 231.971
gene5 64.7502 -0.494415 0.399450 -1.237740 0.2158127 0.469899 218.827
gene6 75.9488 0.572533 0.422086 1.356438 0.1749598 0.469899 220.336
BIC
<numeric>
gene1 236.734
gene2 264.336
gene3 261.114
gene4 238.941
gene5 225.797
gene6 227.306
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 158.0845 -0.623765 0.900235 -0.692891 0.488378 0.775429 229.764
gene2 204.5902 0.424675 0.815115 0.521000 0.602367 0.775429 257.366
gene3 173.8494 -1.162471 0.849030 -1.369176 0.170944 0.470373 254.144
gene4 113.2686 -0.389122 0.751983 -0.517461 0.604834 0.775429 231.971
gene5 64.7502 0.802061 0.809797 0.990447 0.321956 0.703024 218.827
gene6 75.9488 -0.550028 0.861496 -0.638456 0.523177 0.775429 220.336
BIC
<numeric>
gene1 236.734
gene2 264.336
gene3 261.114
gene4 238.941
gene5 225.797
gene6 227.306
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>
gene1 158.0845 1.00009 10.79280 0.00101972 0.0509859 229.764 236.734
gene44 195.7867 1.00007 8.43072 0.00369108 0.0922769 251.798 258.769
gene38 94.7503 1.00007 7.22037 0.00721015 0.1201691 229.573 236.543
gene25 135.2984 1.00011 6.61899 0.01009145 0.1249981 227.938 234.908
gene26 27.3826 1.00009 6.23869 0.01249981 0.1249981 184.299 191.269
gene4 113.2686 1.00008 5.77412 0.01627357 0.1356131 231.971 238.941
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-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.3.2 BiocParallel_1.24.0
[3] NBAMSeq_1.6.1 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] snow_0.4-3 digest_0.6.27 R6_2.5.0
[7] RSQLite_2.2.1 evaluate_0.14 httr_1.4.2
[10] pillar_1.4.6 zlibbioc_1.36.0 rlang_0.4.8
[13] annotate_1.68.0 blob_1.2.1 Matrix_1.2-18
[16] rmarkdown_2.5 labeling_0.4.2 splines_4.0.3
[19] geneplotter_1.68.0 stringr_1.4.0 RCurl_1.98-1.2
[22] bit_4.0.4 munsell_0.5.0 DelayedArray_0.16.0
[25] compiler_4.0.3 xfun_0.18 pkgconfig_2.0.3
[28] mgcv_1.8-33 htmltools_0.5.0 tidyselect_1.1.0
[31] tibble_3.0.4 GenomeInfoDbData_1.2.4 XML_3.99-0.5
[34] withr_2.3.0 crayon_1.3.4 dplyr_1.0.2
[37] bitops_1.0-6 grid_4.0.3 nlme_3.1-150
[40] xtable_1.8-4 gtable_0.3.0 lifecycle_0.2.0
[43] DBI_1.1.0 magrittr_1.5 scales_1.1.1
[46] stringi_1.5.3 farver_2.0.3 XVector_0.30.0
[49] genefilter_1.72.0 ellipsis_0.3.1 vctrs_0.3.4
[52] generics_0.0.2 RColorBrewer_1.1-2 tools_4.0.3
[55] bit64_4.0.5 glue_1.4.2 DESeq2_1.30.0
[58] purrr_0.3.4 survival_3.2-7 yaml_2.2.1
[61] AnnotationDbi_1.52.0 colorspace_1.4-1 memoise_1.1.0
[64] 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.