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      47       1     148       1     135       1     209      19      17
gene2     282      10     139     152       1      27       2      13      95
gene3      22       1      11       4       1       3     121       1    1102
gene4       1     311       4      73     327       1     113      32      39
gene5     396     333      57      78       1      17      32     865      37
gene6      34      89      78      14      11     158      79       2     225
      sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1       22      175      214       16        3       27      459      110
gene2       56      384        1       94       55       80       50      101
gene3        5        7        9       22        1        1        2       30
gene4      562       52        2        1        8        5       37        7
gene5        4        2      130      258        4      234        1       26
gene6       39       31        5       22      204       45       82        1
      sample18 sample19 sample20
gene1        3        4      128
gene2        7        9        1
gene3       80       13       21
gene4        9       55        3
gene5      517      355       90
gene6      125      131      355

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.92391  2.04501514  1.6546908 -0.7792506    1
sample2 22.39739 -0.09681323  0.9546643  1.9220361    0
sample3 39.76552 -0.37548621 -0.4799308 -0.2751021    2
sample4 32.18057 -1.78489510  0.2273332  0.1470594    0
sample5 41.09150 -0.50279625 -0.3881662  1.9397883    0
sample6 41.68730 -0.22177422  1.4803430  0.1878065    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   88.5310   1.00019  0.146018  0.702582  0.859019   224.454   231.424
gene2   52.0904   1.00005  0.654100  0.418686  0.664576   218.239   225.209
gene3   35.4802   1.00008  1.743778  0.186679  0.498393   172.397   179.368
gene4   76.2280   1.00005  1.456988  0.227434  0.568586   202.368   209.338
gene5  173.3946   1.00009  0.056540  0.812260  0.923023   243.054   250.024
gene6   93.2728   1.00008  0.706500  0.400642  0.664576   230.234   237.204

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   88.5310 -0.0278828  0.294768 -0.0945924 0.92463859 0.95610681   224.454
gene2   52.0904 -0.1049727  0.264142 -0.3974096 0.69106547 0.90079307   218.239
gene3   35.4802  0.1585331  0.254565  0.6227620 0.53344090 0.86038856   172.397
gene4   76.2280 -1.1073987  0.288193 -3.8425560 0.00012176 0.00608798   202.368
gene5  173.3946  0.4933707  0.275853  1.7885291 0.07369069 0.32027387   243.054
gene6   93.2728  0.0113177  0.244490  0.0462909 0.96307839 0.96307839   230.234
            BIC
      <numeric>
gene1   231.424
gene2   225.209
gene3   179.368
gene4   209.338
gene5   250.024
gene6   237.204

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   88.5310 -0.3128218  1.080442 -0.2895314 0.77217475 0.8393204   224.454
gene2   52.0904  0.0618674  0.969797  0.0637941 0.94913415 0.9705305   218.239
gene3   35.4802  2.8731033  0.951379  3.0199355 0.00252828 0.0566767   172.397
gene4   76.2280 -0.4980850  0.992571 -0.5018131 0.61579897 0.7319616   202.368
gene5  173.3946 -1.5371425  1.016846 -1.5116769 0.13061608 0.3531905   243.054
gene6   93.2728  2.1463205  0.898042  2.3900008 0.01684833 0.2094483   230.234
            BIC
      <numeric>
gene1   231.424
gene2   225.209
gene3   179.368
gene4   209.338
gene5   250.024
gene6   237.204

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>
gene35   45.4798   1.00006   8.70455 0.00317561  0.148373   199.762   206.732
gene48   64.7973   1.00007   7.20983 0.00725259  0.148373   196.500   203.470
gene36   41.5375   1.00006   6.58143 0.01030674  0.148373   196.214   203.184
gene23   82.6022   1.00008   6.20610 0.01273768  0.148373   200.631   207.602
gene26  108.8420   1.00009   5.93541 0.01483728  0.148373   236.383   243.354
gene11   95.9691   1.00005   3.78207 0.05181242  0.351321   226.123   233.093
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.5.1 Patched (2025-09-10 r88807)
Platform: x86_64-apple-darwin20
Running under: macOS Monterey 12.7.6

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

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_4.0.0               BiocParallel_1.44.0        
 [3] NBAMSeq_1.26.0              SummarizedExperiment_1.40.0
 [5] Biobase_2.70.0              GenomicRanges_1.62.0       
 [7] Seqinfo_1.0.0               IRanges_2.44.0             
 [9] S4Vectors_0.48.0            BiocGenerics_0.56.0        
[11] generics_0.1.4              MatrixGenerics_1.22.0      
[13] matrixStats_1.5.0          

loaded via a namespace (and not attached):
 [1] KEGGREST_1.50.0      gtable_0.3.6         xfun_0.53           
 [4] bslib_0.9.0          lattice_0.22-7       vctrs_0.6.5         
 [7] tools_4.5.1          parallel_4.5.1       tibble_3.3.0        
[10] AnnotationDbi_1.72.0 RSQLite_2.4.3        blob_1.2.4          
[13] pkgconfig_2.0.3      Matrix_1.7-4         RColorBrewer_1.1-3  
[16] S7_0.2.0             lifecycle_1.0.4      compiler_4.5.1      
[19] farver_2.1.2         Biostrings_2.78.0    DESeq2_1.50.0       
[22] codetools_0.2-20     htmltools_0.5.8.1    sass_0.4.10         
[25] yaml_2.3.10          crayon_1.5.3         pillar_1.11.1       
[28] jquerylib_0.1.4      DelayedArray_0.36.0  cachem_1.1.0        
[31] abind_1.4-8          nlme_3.1-168         genefilter_1.92.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.1       
[40] fastmap_1.2.0        grid_4.5.1           cli_3.6.5           
[43] SparseArray_1.10.0   magrittr_2.0.4       S4Arrays_1.10.0     
[46] survival_3.8-3       dichromat_2.0-0.1    XML_3.99-0.19       
[49] withr_3.0.2          scales_1.4.0         bit64_4.6.0-1       
[52] rmarkdown_2.30       XVector_0.50.0       httr_1.4.7          
[55] bit_4.6.0            png_0.1-8            memoise_2.0.1       
[58] evaluate_1.0.5       knitr_1.50           mgcv_1.9-3          
[61] rlang_1.1.6          Rcpp_1.1.0           xtable_1.8-4        
[64] glue_1.8.0           DBI_1.2.3            annotate_1.88.0     
[67] jsonlite_2.0.0       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–8.