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     150       4       9     219     180       2      14      13     118
gene2    1091     486    1008      35      97      27     246       1       1
gene3     219      62      11       5       1       1     424     245      73
gene4       7       3       1       1     189       2      23       9      66
gene5       6      73       2     468     275      60      74     491     123
gene6       7    1084      97       4      35      39       8     282       5
      sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1        6        5      219       33       21      135       19        2
gene2       44       28        1      244       47        1       52      278
gene3      275        3      650       13        1        2      188        7
gene4        1        6        8       25        5      121       55       13
gene5       75        1      130        4      204        2       24      127
gene6        1      398        2      107        1        1       70       22
      sample18 sample19 sample20
gene1        3        3        5
gene2       37       78      348
gene3       17        1       22
gene4        3       68       64
gene5       21      666       17
gene6      106      238       70

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 26.31145  0.5619716  0.7018006 -1.0332727    0
sample2 32.65385 -1.5831876 -1.3020107 -0.4120632    0
sample3 36.35482  0.3354885  1.5923540  0.7890664    0
sample4 63.09763  1.8712693 -0.4193318  0.1665577    0
sample5 52.04506  0.4440052  0.4032179 -2.5101022    0
sample6 79.47380 -0.6617745 -0.7963773 -0.1440922    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   42.1177   1.00007  0.704822 0.4011572  0.866849   204.918   211.888
gene2  190.3664   1.00006  0.167499 0.6824645  0.866849   250.985   257.955
gene3   86.3021   1.00003  5.736975 0.0166168  0.166168   213.627   220.597
gene4   24.9000   1.00006  0.577917 0.4471980  0.866849   173.331   180.301
gene5  116.8376   1.00007  0.223324 0.6365618  0.866849   244.170   251.140
gene6  101.9065   1.00022  0.296017 0.5864264  0.866849   227.354   234.325

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   42.1177  0.682894  0.347605  1.964571 0.04946395 0.2248361   204.918
gene2  190.3664  0.186876  0.387345  0.482454 0.62948331 0.7726939   250.985
gene3   86.3021 -0.541995  0.378282 -1.432782 0.15192026 0.3798006   213.627
gene4   24.9000  0.405423  0.280748  1.444085 0.14871508 0.3798006   173.331
gene5  116.8376  0.176978  0.371293  0.476653 0.63360896 0.7726939   244.170
gene6  101.9065 -1.198137  0.390713 -3.066541 0.00216551 0.0360919   227.354
            BIC
      <numeric>
gene1   211.888
gene2   257.955
gene3   220.597
gene4   180.301
gene5   251.140
gene6   234.325

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   42.1177  2.472889  1.063383  2.325493 0.02004561 0.1670467   204.918
gene2  190.3664 -2.333386  1.184841 -1.969367 0.04891100 0.2717278   250.985
gene3   86.3021  0.928979  1.156933  0.802967 0.42199406 0.7903945   213.627
gene4   24.9000  2.603689  0.865194  3.009369 0.00261791 0.0357933   173.331
gene5  116.8376 -0.334079  1.134194 -0.294552 0.76833633 0.9870746   244.170
gene6  101.9065  0.952467  1.190479  0.800071 0.42366991 0.7903945   227.354
            BIC
      <numeric>
gene1   211.888
gene2   257.955
gene3   220.597
gene4   180.301
gene5   251.140
gene6   234.325

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
       <numeric> <numeric> <numeric>   <numeric>   <numeric> <numeric>
gene47  151.7339   1.00009  21.13499 4.69291e-06 0.000234645   225.501
gene24   64.1522   1.00005   7.34558 6.72497e-03 0.142823093   211.686
gene28   76.2668   1.00004   6.74958 9.37973e-03 0.142823093   209.340
gene21   68.8090   1.00007   6.39823 1.14258e-02 0.142823093   217.279
gene3    86.3021   1.00003   5.73698 1.66168e-02 0.166168096   213.627
gene26   41.6453   1.00011   4.70073 3.01494e-02 0.232673590   186.669
             BIC
       <numeric>
gene47   232.471
gene24   218.656
gene28   216.310
gene21   224.249
gene3    220.597
gene26   193.639
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.1 Patched (2022-07-09 r82577)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur ... 10.16

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_GB/en_US.UTF-8

attached base packages:
[1] stats4    stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
 [1] ggplot2_3.3.6               BiocParallel_1.32.0        
 [3] NBAMSeq_1.14.0              SummarizedExperiment_1.28.0
 [5] Biobase_2.58.0              GenomicRanges_1.50.0       
 [7] GenomeInfoDb_1.34.0         IRanges_2.32.0             
 [9] S4Vectors_0.36.0            BiocGenerics_0.44.0        
[11] MatrixGenerics_1.10.0       matrixStats_0.62.0         

loaded via a namespace (and not attached):
 [1] httr_1.4.4             sass_0.4.2             bit64_4.0.5           
 [4] jsonlite_1.8.3         splines_4.2.1          bslib_0.4.0           
 [7] assertthat_0.2.1       highr_0.9              blob_1.2.3            
[10] GenomeInfoDbData_1.2.9 yaml_2.3.6             pillar_1.8.1          
[13] RSQLite_2.2.18         lattice_0.20-45        glue_1.6.2            
[16] digest_0.6.30          RColorBrewer_1.1-3     XVector_0.38.0        
[19] colorspace_2.0-3       htmltools_0.5.3        Matrix_1.5-1          
[22] DESeq2_1.38.0          XML_3.99-0.12          pkgconfig_2.0.3       
[25] genefilter_1.80.0      zlibbioc_1.44.0        xtable_1.8-4          
[28] scales_1.2.1           tibble_3.1.8           annotate_1.76.0       
[31] mgcv_1.8-41            KEGGREST_1.38.0        farver_2.1.1          
[34] generics_0.1.3         withr_2.5.0            cachem_1.0.6          
[37] cli_3.4.1              survival_3.4-0         magrittr_2.0.3        
[40] crayon_1.5.2           memoise_2.0.1          evaluate_0.17         
[43] fansi_1.0.3            nlme_3.1-160           tools_4.2.1           
[46] lifecycle_1.0.3        stringr_1.4.1          locfit_1.5-9.6        
[49] munsell_0.5.0          DelayedArray_0.24.0    AnnotationDbi_1.60.0  
[52] Biostrings_2.66.0      compiler_4.2.1         jquerylib_0.1.4       
[55] rlang_1.0.6            grid_4.2.1             RCurl_1.98-1.9        
[58] labeling_0.4.2         bitops_1.0-7           rmarkdown_2.17        
[61] gtable_0.3.1           codetools_0.2-18       DBI_1.1.3             
[64] R6_2.5.1               knitr_1.40             dplyr_1.0.10          
[67] fastmap_1.1.0          bit_4.0.4              utf8_1.2.2            
[70] stringi_1.7.8          parallel_4.2.1         Rcpp_1.0.9            
[73] vctrs_0.5.0            geneplotter_1.76.0     png_0.1-7             
[76] tidyselect_1.2.0       xfun_0.34             

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–78.