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      16      58     115     122     432       6       1       2       4
gene2      27      50       3      37       1       7      43       1      24
gene3      49       1       3      13     476      14     220      13      10
gene4      69     107     150      67       1      16       2      11      13
gene5     137      53     167       1      90       2     352     266      96
gene6      18     284       1       9       5      16       3       2      32
      sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1      145      613        4        1       42      245       12      665
gene2       32       34      174        1      343      223       86        1
gene3        1        3        5        2        2       28       63       58
gene4        1        9        1       13        2       28        1       60
gene5      150        1       63       31        9        3      206       62
gene6       41        1       44        2       21        1       55        1
      sample18 sample19 sample20
gene1       70        1       62
gene2        9       59       87
gene3        1        1        2
gene4      192       21       51
gene5      139       33      278
gene6      689       10        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 40.18741  0.78029020 -0.4724210 -1.3280741    0
sample2 75.21883 -0.06609071  1.2276652 -0.8041706    2
sample3 27.83243  0.36026605 -0.5542843 -0.4785402    2
sample4 77.10456  1.22152839 -1.1508265  0.3655641    1
sample5 79.95693 -0.54209307 -0.5712200  1.0772320    0
sample6 57.65014  2.17942698  1.2927173  1.1278387    1

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  114.9222   1.00004 0.9903158 0.31966946 0.5729797   215.937   222.907
gene2   63.4843   1.00007 0.3444146 0.55740969 0.7741801   215.361   222.331
gene3   47.0870   1.00007 8.2075232 0.00417425 0.0521782   173.986   180.956
gene4   28.7192   1.00007 1.8280106 0.17638705 0.4502377   186.326   193.296
gene5   92.1652   1.00011 0.8839676 0.34713092 0.5729797   234.659   241.629
gene6   38.1786   1.00015 0.0757679 0.78343228 0.8703051   182.747   189.717

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  114.9222 -0.250996  0.316560 -0.792887 0.4278435  0.855687   215.937
gene2   63.4843 -0.217248  0.370365 -0.586578 0.5574873  0.929900   215.361
gene3   47.0870 -0.343436  0.336354 -1.021055 0.3072284  0.768071   173.986
gene4   28.7192  0.635237  0.308994  2.055825 0.0397994  0.248746   186.326
gene5   92.1652 -0.384508  0.320251 -1.200643 0.2298896  0.718405   234.659
gene6   38.1786  0.319842  0.359878  0.888753 0.3741360  0.813339   182.747
            BIC
      <numeric>
gene1   222.907
gene2   222.331
gene3   180.956
gene4   193.296
gene5   241.629
gene6   189.717

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  114.9222  2.101909  0.797312  2.636246 0.00838290  0.139715   215.937
gene2   63.4843 -0.622199  0.938633 -0.662878 0.50740889  0.724870   215.361
gene3   47.0870 -1.982045  0.869206 -2.280294 0.02259026  0.176247   173.986
gene4   28.7192  1.354430  0.776355  1.744600 0.08105451  0.311748   186.326
gene5   92.1652 -1.665751  0.813784 -2.046921 0.04066584  0.203329   234.659
gene6   38.1786 -2.615329  0.930850 -2.809613 0.00496011  0.124003   182.747
            BIC
      <numeric>
gene1   222.907
gene2   222.331
gene3   180.956
gene4   193.296
gene5   241.629
gene6   189.717

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>
gene9    73.1253   1.00004  23.29041 1.98903e-06 9.94515e-05   201.674
gene35  160.0021   1.00003   9.70354 1.84003e-03 4.57038e-02   232.563
gene22   87.3737   1.00009   8.97231 2.74223e-03 4.57038e-02   214.226
gene3    47.0870   1.00007   8.20752 4.17425e-03 5.21782e-02   173.986
gene16  194.3723   1.00010   7.17012 7.41756e-03 7.41756e-02   236.700
gene24   60.4557   1.15528   8.45924 1.42475e-02 1.15041e-01   203.607
             BIC
       <numeric>
gene9    208.645
gene35   239.533
gene22   221.196
gene3    180.956
gene16   243.670
gene24   210.732
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 Under development (unstable) (2025-03-02 r87868)
Platform: aarch64-apple-darwin20
Running under: macOS Ventura 13.7.1

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0

locale:
[1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/New_York
tzcode source: internal

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

other attached packages:
 [1] ggplot2_3.5.1               BiocParallel_1.41.2        
 [3] NBAMSeq_1.23.0              SummarizedExperiment_1.37.0
 [5] Biobase_2.67.0              GenomicRanges_1.59.1       
 [7] GenomeInfoDb_1.43.4         IRanges_2.41.3             
 [9] S4Vectors_0.45.4            BiocGenerics_0.53.6        
[11] generics_0.1.3              MatrixGenerics_1.19.1      
[13] matrixStats_1.5.0          

loaded via a namespace (and not attached):
 [1] KEGGREST_1.47.0         gtable_0.3.6            xfun_0.51              
 [4] bslib_0.9.0             lattice_0.22-6          vctrs_0.6.5            
 [7] tools_4.5.0             parallel_4.5.0          tibble_3.2.1           
[10] AnnotationDbi_1.69.0    RSQLite_2.3.9           blob_1.2.4             
[13] pkgconfig_2.0.3         Matrix_1.7-2            lifecycle_1.0.4        
[16] GenomeInfoDbData_1.2.13 farver_2.1.2            compiler_4.5.0         
[19] Biostrings_2.75.4       munsell_0.5.1           DESeq2_1.47.5          
[22] codetools_0.2-20        htmltools_0.5.8.1       sass_0.4.9             
[25] yaml_2.3.10             pillar_1.10.1           crayon_1.5.3           
[28] jquerylib_0.1.4         DelayedArray_0.33.6     cachem_1.1.0           
[31] abind_1.4-8             nlme_3.1-167            genefilter_1.89.0      
[34] tidyselect_1.2.1        locfit_1.5-9.12         digest_0.6.37          
[37] dplyr_1.1.4             labeling_0.4.3          splines_4.5.0          
[40] fastmap_1.2.0           grid_4.5.0              colorspace_2.1-1       
[43] cli_3.6.4               SparseArray_1.7.6       magrittr_2.0.3         
[46] S4Arrays_1.7.3          survival_3.8-3          XML_3.99-0.18          
[49] withr_3.0.2             scales_1.3.0            UCSC.utils_1.3.1       
[52] bit64_4.6.0-1           rmarkdown_2.29          XVector_0.47.2         
[55] httr_1.4.7              bit_4.5.0.1             png_0.1-8              
[58] memoise_2.0.1           evaluate_1.0.3          knitr_1.49             
[61] mgcv_1.9-1              rlang_1.1.5             Rcpp_1.0.14            
[64] xtable_1.8-4            glue_1.8.0              DBI_1.2.3              
[67] annotate_1.85.0         jsonlite_1.9.1          R6_2.6.1               

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.