Installation

To install and load NBAMSeq

if (!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
BiocManager::install("NBAMSeq")
library(NBAMSeq)

Introduction

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:

Here we illustrate each of these steps respectively.

Data input

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      41     557      18      14       6      34       1     993     169
gene2       1     119      59      55       1      16       1       2      17
gene3       9       3       7       3     104       6       6     226     496
gene4     306      16      51       5       2     132       2       7       2
gene5      23     343       1     130      37     450       1      86      67
gene6       3     144     263      34     103       1      45     408       3
      sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1       15      262      142       79       96        9       92       53
gene2      177       43        9        1       38       14        1        6
gene3        3      240       24       63      380       65      286        1
gene4      502        1       75       63       80        1      528      108
gene5     1560       38      454        2        2       11        1      221
gene6      287        2        1        9      455       73        1      184
      sample18 sample19 sample20
gene1       30      204       19
gene2        1       95       30
gene3        1       20      264
gene4      212       54      302
gene5        3        1       44
gene6        3        9       35

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 79.71063  1.0323115  0.09477607  0.2343479    0
sample2 77.36460  1.3468202 -0.03951843 -1.5117014    1
sample3 45.87374  0.5597939  1.50724898  2.4349669    0
sample4 73.89417 -0.3049792 -0.66059218  0.9008444    0
sample5 61.70722  0.3883174 -0.45387197  0.7146592    1
sample6 22.78496 -0.3740537 -0.91291343 -1.5444831    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:

design = ~ s(pheno) + var1 + var2 + var3 + var4

Several notes should be made regarding the design formula:

We then construct the NBAMSeqDataSet using countData, colData, and design:

gsd = NBAMSeqDataSet(countData = countData, colData = colData, design = design)
gsd
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

Differential expression analysis can be performed by NBAMSeq function:

gsd = NBAMSeq(gsd)

Several other arguments in NBAMSeq function are available for users to customize the analysis.

library(BiocParallel)
gsd = NBAMSeq(gsd, parallel = TRUE)

Pulling out DE results

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.

res1 = results(gsd, name = "pheno")
head(res1)
DataFrame with 6 rows and 7 columns
       baseMean       edf      stat    pvalue      padj       AIC       BIC
      <numeric> <numeric> <numeric> <numeric> <numeric> <numeric> <numeric>
gene1  110.2078   1.00006  0.315578 0.5743190  0.766513   236.828   243.798
gene2   23.2179   1.00012  0.991417 0.3193967  0.591475   184.448   191.419
gene3   79.2017   1.00004  2.367909 0.1238594  0.329530   215.541   222.512
gene4   90.8124   1.00004  1.864554 0.1721250  0.409822   224.525   231.495
gene5  102.3511   1.00027  2.792619 0.0947501  0.329530   229.662   236.633
gene6   74.0875   1.00009  0.183001 0.6688220  0.807061   211.684   218.654

For linear continuous covariates, base mean, estimated coefficient, standard error, test statistics, p-value, and adjusted p-value will be returned.

res2 = results(gsd, name = "var1")
head(res2)
DataFrame with 6 rows and 8 columns
       baseMean       coef        SE      stat    pvalue      padj       AIC
      <numeric>  <numeric> <numeric> <numeric> <numeric> <numeric> <numeric>
gene1  110.2078  0.4414852  0.460024  0.959701 0.3372057  0.565192   236.828
gene2   23.2179 -0.6490583  0.512037 -1.267600 0.2049408  0.565192   184.448
gene3   79.2017 -0.0655163  0.499119 -0.131264 0.8955666  0.920619   215.541
gene4   90.8124  0.4859443  0.501346  0.969278 0.3324063  0.565192   224.525
gene5  102.3511  1.0214008  0.603638  1.692076 0.0906315  0.453158   229.662
gene6   74.0875  0.0613949  0.497726  0.123351 0.9018293  0.920619   211.684
            BIC
      <numeric>
gene1   243.798
gene2   191.419
gene3   222.512
gene4   231.495
gene5   236.633
gene6   218.654

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.

res3 = results(gsd, contrast = c("var4", "2", "0"))
head(res3)
DataFrame with 6 rows and 8 columns
       baseMean      coef        SE      stat    pvalue      padj       AIC
      <numeric> <numeric> <numeric> <numeric> <numeric> <numeric> <numeric>
gene1  110.2078  0.520359   0.99603  0.522433 0.6013691  0.887700   236.828
gene2   23.2179 -1.015113   1.12031 -0.906100 0.3648827  0.887700   184.448
gene3   79.2017 -2.447532   1.13399 -2.158342 0.0309012  0.220723   215.541
gene4   90.8124 -0.562141   1.07859 -0.521182 0.6022401  0.887700   224.525
gene5  102.3511 -0.439348   1.30342 -0.337074 0.7360608  0.898613   229.662
gene6   74.0875  0.975552   1.07511  0.907397 0.3641969  0.887700   211.684
            BIC
      <numeric>
gene1   243.798
gene2   191.419
gene3   222.512
gene4   231.495
gene5   236.633
gene6   218.654

Visualization

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>
gene22   70.5105   1.00007  11.33913 0.000758956 0.0379478   210.709   217.679
gene32   53.0644   1.00005   7.43202 0.006410054 0.1602513   212.283   219.253
gene48   88.3198   1.00007   6.19187 0.012835336 0.1773691   219.424   226.394
gene45  138.1574   1.00006   6.01448 0.014189526 0.1773691   241.017   247.987
gene50   75.0694   1.00007   5.35981 0.020609638 0.1983969   207.493   214.463
gene21   54.9851   1.00005   5.10890 0.023807629 0.1983969   216.168   223.138
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))

Session info

sessionInfo()
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.1               SummarizedExperiment_1.18.1
 [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.1             
[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.4          
[10] zlibbioc_1.34.0        rlang_0.4.6            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            

References

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.