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      25      40     213      60       1     530      66     143     622
gene2      98      65      29       1       3     159      23      67     294
gene3      61      38     420      81     200       7     129       7       1
gene4      15       2      38       1      14       2       9     193     235
gene5     789     283       1      19       1     127      39      22      78
gene6      12     156      17      84     345     323     116     131       2
      sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1     1365        5        1      101       43        1      186      342
gene2       52        1       78       10      212        2       71       14
gene3      204        1      399       16        7        7       71      146
gene4      419       38      103        6        6      332       11        5
gene5        1        4      235       61        8        6       23        8
gene6      326      126       29       15       93        1        6        1
      sample18 sample19 sample20
gene1        1       14      457
gene2      393      580        2
gene3       18        7      113
gene4        4        1        1
gene5      183        6        1
gene6      443      154        1

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 62.38993 -0.179919647 -0.104971878  0.7457638    2
sample2 61.96337 -0.011706548  0.834748344 -0.3253055    0
sample3 70.70229  0.004914265  0.002343635  0.7985984    0
sample4 20.77724  2.849819120 -0.208892124  1.1063661    2
sample5 50.67062 -1.927186730 -0.761985950 -2.0688726    1
sample6 43.92942 -0.839516704  0.859811817 -1.5360931    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:

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  167.4766   1.00008  0.107563 0.7430686  0.906181   245.212   252.182
gene2   89.8683   1.00009  0.120741 0.7283758  0.906181   225.393   232.364
gene3   88.6469   1.00026  3.295547 0.0694973  0.231658   230.835   237.805
gene4   83.0558   1.00015  5.726640 0.0167304  0.104565   200.221   207.191
gene5   93.0420   1.55155  4.786661 0.0612337  0.231658   217.571   225.091
gene6   95.4173   1.00009  1.689893 0.1936469  0.420972   235.515   242.486

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  167.4766 -0.565053  0.331008 -1.707067 0.087809555 0.3652928   245.212
gene2   89.8683 -0.771655  0.313546 -2.461061 0.013852695 0.1439071   225.393
gene3   88.6469 -0.103488  0.323809 -0.319597 0.749274195 0.8325269   230.835
gene4   83.0558 -1.168333  0.343233 -3.403910 0.000664287 0.0332143   200.221
gene5   93.0420 -0.204255  0.366687 -0.557028 0.577508601 0.7804170   217.571
gene6   95.4173 -0.270921  0.326555 -0.829634 0.406745957 0.7229885   235.515
            BIC
      <numeric>
gene1   252.182
gene2   232.364
gene3   237.805
gene4   207.191
gene5   225.091
gene6   242.486

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  167.4766  1.0817632  0.879979  1.22930637  0.218957  0.633456   245.212
gene2   89.8683 -0.6840846  0.824085 -0.83011467  0.406474  0.787283   225.393
gene3   88.6469  0.9382077  0.862726  1.08749253  0.276819  0.725849   230.835
gene4   83.0558 -0.3755241  0.891596 -0.42118212  0.673622  0.872964   200.221
gene5   93.0420 -0.3995598  0.962022 -0.41533348  0.677898  0.872964   217.571
gene6   95.4173  0.0075898  0.868061  0.00874339  0.993024  0.993024   235.515
            BIC
      <numeric>
gene1   252.182
gene2   232.364
gene3   237.805
gene4   207.191
gene5   225.091
gene6   242.486

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>
gene19   76.4506   1.00007  11.21780 0.000810901 0.0234330   213.709   220.679
gene18   35.5800   1.00014  10.95019 0.000937322 0.0234330   170.105   177.075
gene26   85.4089   1.00006   7.67440 0.005604715 0.0827091   218.953   225.924
gene24   98.9852   1.00013   7.26666 0.007030268 0.0827091   206.003   212.973
gene49   56.0795   1.00004   6.85068 0.008862841 0.0827091   199.813   206.783
gene43   78.7780   1.00011   6.64949 0.009925093 0.0827091   219.029   225.999
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.2.0 RC (2022-04-19 r82224)
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.2/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/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] stats4    stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
 [1] ggplot2_3.3.5               BiocParallel_1.30.0        
 [3] NBAMSeq_1.12.0              SummarizedExperiment_1.26.0
 [5] Biobase_2.56.0              GenomicRanges_1.48.0       
 [7] GenomeInfoDb_1.32.0         IRanges_2.30.0             
 [9] S4Vectors_0.34.0            BiocGenerics_0.42.0        
[11] MatrixGenerics_1.8.0        matrixStats_0.62.0         

loaded via a namespace (and not attached):
 [1] httr_1.4.2             sass_0.4.1             bit64_4.0.5           
 [4] jsonlite_1.8.0         splines_4.2.0          bslib_0.3.1           
 [7] assertthat_0.2.1       highr_0.9              blob_1.2.3            
[10] GenomeInfoDbData_1.2.8 yaml_2.3.5             pillar_1.7.0          
[13] RSQLite_2.2.12         lattice_0.20-45        glue_1.6.2            
[16] digest_0.6.29          RColorBrewer_1.1-3     XVector_0.36.0        
[19] colorspace_2.0-3       htmltools_0.5.2        Matrix_1.4-1          
[22] DESeq2_1.36.0          XML_3.99-0.9           pkgconfig_2.0.3       
[25] genefilter_1.78.0      zlibbioc_1.42.0        purrr_0.3.4           
[28] xtable_1.8-4           scales_1.2.0           tibble_3.1.6          
[31] annotate_1.74.0        mgcv_1.8-40            KEGGREST_1.36.0       
[34] farver_2.1.0           generics_0.1.2         ellipsis_0.3.2        
[37] withr_2.5.0            cachem_1.0.6           cli_3.3.0             
[40] survival_3.3-1         magrittr_2.0.3         crayon_1.5.1          
[43] memoise_2.0.1          evaluate_0.15          fansi_1.0.3           
[46] nlme_3.1-157           tools_4.2.0            lifecycle_1.0.1       
[49] stringr_1.4.0          locfit_1.5-9.5         munsell_0.5.0         
[52] DelayedArray_0.22.0    AnnotationDbi_1.58.0   Biostrings_2.64.0     
[55] compiler_4.2.0         jquerylib_0.1.4        rlang_1.0.2           
[58] grid_4.2.0             RCurl_1.98-1.6         labeling_0.4.2        
[61] bitops_1.0-7           rmarkdown_2.14         gtable_0.3.0          
[64] DBI_1.1.2              R6_2.5.1               knitr_1.38            
[67] dplyr_1.0.8            fastmap_1.1.0          bit_4.0.4             
[70] utf8_1.2.2             stringi_1.7.6          parallel_4.2.0        
[73] Rcpp_1.0.8.3           vctrs_0.4.1            geneplotter_1.74.0    
[76] png_0.1-7              tidyselect_1.1.2       xfun_0.30             

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.