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     127     194       5      28       1       1      67       3     104
gene2      88      27       3     218      57     313     260     245      74
gene3     487       1       6       9       3      15     273     351       1
gene4       4     152      99      50       2     192      42      18      94
gene5       2       3      62      23       2      70       8       8      40
gene6     130      67      63     231       1       9       1      60      23
      sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1      153      355      286        2        8       33        9        1
gene2      218        2       75        1       57      275      611        1
gene3      131        4      599      238        1       34      118        2
gene4      167        1       32       82      106        1       45      859
gene5        1        9       15        5       22      147       68        2
gene6        2      114       38        1        1       39       55       32
      sample18 sample19 sample20
gene1        1       69      315
gene2        1      152      511
gene3        2        2        1
gene4      171      285        1
gene5      395        2       22
gene6        1        7        2

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 74.43691 -2.0664729  0.3459202  0.38267508    2
sample2 57.07690  1.1849616 -1.1680420  0.03050886    2
sample3 74.84902 -0.2730559 -2.3506204  0.41613973    2
sample4 77.33160  1.5292606  0.4613487  0.78793133    1
sample5 38.59632  0.6056182 -0.2478403 -1.11412336    1
sample6 64.14295 -2.4979012  1.3757171 -1.12581218    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  102.1013   1.00010  0.117591 0.73179661  0.906542   216.603   223.574
gene2  130.5310   1.00007  0.249228 0.61774020  0.886985   246.592   253.562
gene3   97.7669   1.00012  0.220796 0.63862926  0.886985   203.560   210.530
gene4   92.8447   1.00010  0.114916 0.73480135  0.906542   230.388   237.358
gene5   34.3963   1.00003  1.223099 0.26876759  0.707283   185.346   192.316
gene6   47.1066   1.00005 13.491192 0.00023996  0.011998   186.724   193.694

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  102.1013  0.404007  0.348586  1.158988 0.24646128  0.674899   216.603
gene2  130.5310 -0.213359  0.329413 -0.647694 0.51718285  0.842527   246.592
gene3   97.7669 -0.171627  0.341140 -0.503099 0.61489483  0.853110   203.560
gene4   92.8447  0.478281  0.309465  1.545509 0.12222320  0.509263   230.388
gene5   34.3963 -0.761312  0.289517 -2.629597 0.00854862  0.182771   185.346
gene6   47.1066  0.391189  0.294242  1.329481 0.18368942  0.574029   186.724
            BIC
      <numeric>
gene1   223.574
gene2   253.562
gene3   210.530
gene4   237.358
gene5   192.316
gene6   193.694

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  102.1013  0.709039  0.964176  0.735383  0.462106  0.703679   216.603
gene2  130.5310 -1.125379  0.920608 -1.222430  0.221545  0.503511   246.592
gene3   97.7669 -1.220955  0.949343 -1.286105  0.198406  0.500981   203.560
gene4   92.8447 -1.246086  0.867321 -1.436707  0.150801  0.500981   230.388
gene5   34.3963 -0.236561  0.812697 -0.291082  0.770989  0.867003   185.346
gene6   47.1066 -0.228530  0.819325 -0.278925  0.780303  0.867003   186.724
            BIC
      <numeric>
gene1   223.574
gene2   253.562
gene3   210.530
gene4   237.358
gene5   192.316
gene6   193.694

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>
gene6    47.1066   1.00005  13.49119 0.00023996 0.0119980   186.724   193.694
gene44   94.9789   1.00011   8.76214 0.00307964 0.0769909   213.780   220.750
gene22   56.2109   1.00007   7.59087 0.00586668 0.0977781   208.841   215.811
gene49  120.9403   1.00008   6.22650 0.01259093 0.1358925   205.983   212.954
gene30   63.6383   1.00006   5.68901 0.01707422 0.1358925   200.091   207.062
gene13   69.6486   1.00020   5.57992 0.01819384 0.1358925   212.045   219.016
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.4.1 (2024-06-14 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows Server 2022 x64 (build 20348)

Matrix products: default


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.39.0        
 [3] NBAMSeq_1.21.0              SummarizedExperiment_1.35.4
 [5] Biobase_2.65.1              GenomicRanges_1.57.2       
 [7] GenomeInfoDb_1.41.2         IRanges_2.39.2             
 [9] S4Vectors_0.43.2            BiocGenerics_0.51.3        
[11] MatrixGenerics_1.17.0       matrixStats_1.4.1          

loaded via a namespace (and not attached):
 [1] KEGGREST_1.45.1         gtable_0.3.5            xfun_0.48              
 [4] bslib_0.8.0             lattice_0.22-6          vctrs_0.6.5            
 [7] tools_4.4.1             generics_0.1.3          parallel_4.4.1         
[10] RSQLite_2.3.7           tibble_3.2.1            fansi_1.0.6            
[13] AnnotationDbi_1.67.0    highr_0.11              blob_1.2.4             
[16] pkgconfig_2.0.3         Matrix_1.7-1            lifecycle_1.0.4        
[19] GenomeInfoDbData_1.2.13 farver_2.1.2            compiler_4.4.1         
[22] Biostrings_2.73.2       munsell_0.5.1           DESeq2_1.45.3          
[25] codetools_0.2-20        snow_0.4-4              htmltools_0.5.8.1      
[28] sass_0.4.9              yaml_2.3.10             pillar_1.9.0           
[31] crayon_1.5.3            jquerylib_0.1.4         DelayedArray_0.31.14   
[34] cachem_1.1.0            abind_1.4-8             nlme_3.1-166           
[37] genefilter_1.87.0       tidyselect_1.2.1        locfit_1.5-9.10        
[40] digest_0.6.37           dplyr_1.1.4             labeling_0.4.3         
[43] splines_4.4.1           fastmap_1.2.0           grid_4.4.1             
[46] colorspace_2.1-1        cli_3.6.3               SparseArray_1.5.45     
[49] magrittr_2.0.3          S4Arrays_1.5.11         survival_3.7-0         
[52] XML_3.99-0.17           utf8_1.2.4              withr_3.0.1            
[55] scales_1.3.0            UCSC.utils_1.1.0        bit64_4.5.2            
[58] rmarkdown_2.28          XVector_0.45.0          httr_1.4.7             
[61] bit_4.5.0               png_0.1-8               memoise_2.0.1          
[64] evaluate_1.0.1          knitr_1.48              mgcv_1.9-1             
[67] rlang_1.1.4             Rcpp_1.0.13             DBI_1.2.3              
[70] xtable_1.8-4            glue_1.8.0              annotate_1.83.0        
[73] jsonlite_1.8.9          R6_2.5.1                zlibbioc_1.51.2        

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.