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       9     209     166     309       1      64      63       1      30
gene2       6       1     329     319       9      17     256     722     126
gene3      15     274       7      21       3       2      62      57      12
gene4      11      28     912      98       2      32     295       7     211
gene5     176      13     172     328     359       1     216      23     364
gene6       7     645       9       5      43       3      34      36       1
      sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1      847      130       79      136        9       42        3       19
gene2       99       67        1      108      141        9      166       48
gene3       19      174        1        5       23      115        2       22
gene4        4      244        1       15      386       84       12       80
gene5      188        2        6      153      173        6      368       15
gene6      459        2       13      509        1      100        6        4
      sample18 sample19 sample20
gene1        4       33      308
gene2       92       11      176
gene3      876        1      488
gene4        1       65        3
gene5      119      406       10
gene6       49       37      108

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 52.83477 -0.80213662  2.0417279  0.78715949    2
sample2 77.28252 -0.05196036 -0.1352802 -0.01054929    1
sample3 41.54699 -0.31173715 -0.9224491  1.24921362    1
sample4 53.17015  0.57616514 -0.3935760  0.38090169    1
sample5 55.28492 -1.12731817  1.0873633 -0.37820532    0
sample6 37.35198 -0.64955508  0.3338783 -0.75570594    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   89.6698   1.00005 0.5237387 0.4692780 0.7305072   231.385   238.355
gene2  105.0001   1.00006 0.8779267 0.3488040 0.6551071   240.505   247.475
gene3  104.2485   1.00016 0.1126638 0.7371822 0.8824308   211.976   218.947
gene4  124.9324   1.00005 0.0613833 0.8044041 0.8824308   232.449   239.419
gene5  167.9805   1.00022 2.8759754 0.0900224 0.3462400   256.617   263.588
gene6   70.6001   1.00014 6.5745285 0.0103518 0.0862646   214.617   221.587

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   89.6698  0.06561384  0.382124  0.171708  0.863667  0.912787   231.385
gene2  105.0001  0.21138565  0.371712  0.568682  0.569572  0.808937   240.505
gene3  104.2485  0.25375663  0.400859  0.633033  0.526712  0.808937   211.976
gene4  124.9324  0.54125412  0.442660  1.222730  0.221432  0.607415   232.449
gene5  167.9805 -0.00629937  0.425663 -0.014799  0.988193  0.988193   256.617
gene6   70.6001 -0.15226341  0.421126 -0.361563  0.717679  0.875218   214.617
            BIC
      <numeric>
gene1   238.355
gene2   247.475
gene3   218.947
gene4   239.419
gene5   263.588
gene6   221.587

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   89.6698 -0.9924955  0.878626 -1.1295995  0.258645  0.553176   231.385
gene2  105.0001  0.2065561  0.853790  0.2419286  0.808836  0.941782   240.505
gene3  104.2485  1.3301748  0.924014  1.4395616  0.149991  0.543406   211.976
gene4  124.9324 -0.4007816  1.016548 -0.3942575  0.693391  0.912357   232.449
gene5  167.9805 -0.7609564  0.977115 -0.7787787  0.436110  0.736491   256.617
gene6   70.6001  0.0403743  0.968328  0.0416949  0.966742  0.966742   214.617
            BIC
      <numeric>
gene1   238.355
gene2   247.475
gene3   218.947
gene4   239.419
gene5   263.588
gene6   221.587

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>
gene16   97.8351   1.00007  16.29587 5.46549e-05 0.00273275   205.016   211.987
gene27  104.8707   1.00005   8.67384 3.22863e-03 0.08071563   222.246   229.216
gene33   35.9451   1.00004   7.36471 6.65304e-03 0.08626461   176.881   183.851
gene48   39.2942   1.00005   7.01054 8.10622e-03 0.08626461   175.587   182.557
gene12  187.7381   1.00008   6.77530 9.24514e-03 0.08626461   223.632   230.602
gene6    70.6001   1.00014   6.57453 1.03518e-02 0.08626461   214.617   221.587
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-01 r87860 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows Server 2022 x64 (build 20348)

Matrix products: default
  LAPACK version 3.12.0

locale:
[1] LC_COLLATE=C                          
[2] LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

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        snow_0.4-4              htmltools_0.5.8.1      
[25] sass_0.4.9              yaml_2.3.10             pillar_1.10.1          
[28] crayon_1.5.3            jquerylib_0.1.4         DelayedArray_0.33.6    
[31] cachem_1.1.0            abind_1.4-8             nlme_3.1-167           
[34] genefilter_1.89.0       tidyselect_1.2.1        locfit_1.5-9.12        
[37] digest_0.6.37           dplyr_1.1.4             labeling_0.4.3         
[40] splines_4.5.0           fastmap_1.2.0           grid_4.5.0             
[43] colorspace_2.1-1        cli_3.6.4               SparseArray_1.7.6      
[46] magrittr_2.0.3          S4Arrays_1.7.3          survival_3.8-3         
[49] XML_3.99-0.18           withr_3.0.2             scales_1.3.0           
[52] UCSC.utils_1.3.1        bit64_4.6.0-1           rmarkdown_2.29         
[55] XVector_0.47.2          httr_1.4.7              bit_4.5.0.1            
[58] png_0.1-8               memoise_2.0.1           evaluate_1.0.3         
[61] knitr_1.49              mgcv_1.9-1              rlang_1.1.5            
[64] Rcpp_1.0.14             xtable_1.8-4            glue_1.8.0             
[67] DBI_1.2.3               annotate_1.85.0         jsonlite_1.9.1         
[70] 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.