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     165     158       3       1      47       5      47      43      11
gene2      50      15     168      42      28       2       1       1      27
gene3      38     485     945     130     128     471      46     160       3
gene4      85     152      41     152     136     295       2     209       4
gene5     107      32       1       3     320       1     508     325       1
gene6       2     738      37       3       1     434       1      18     518
      sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1      235      107      336        2      146        6        2        7
gene2       27       83      182       22       89        4     1067       10
gene3      277       14       10       63        1      312      118        1
gene4      180        1        5      103        1       17       93       39
gene5        1       19        5       24      264       51       15        2
gene6        3       40       36       67       30        5       21        1
      sample18 sample19 sample20
gene1       31        4      163
gene2       38        1      140
gene3      413        3      134
gene4       41       17        1
gene5        1      101        1
gene6        6        8        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 33.60814 -0.1611642 -0.80979948 -2.0400091    0
sample2 25.90787  0.2359096  0.23376247 -0.5595276    1
sample3 21.03667 -0.1910845 -0.45812649 -0.6949608    1
sample4 26.26290  0.3897112 -1.03781937 -0.4809042    2
sample5 63.07196  0.2442023  0.91481319  0.3887636    0
sample6 30.85611  0.4390571  0.05824031  0.1351116    2

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   63.3195   1.00008  1.0092614 0.315121873 0.53568148   213.047   220.017
gene2   73.4118   1.00003  0.0636229 0.800923392 0.91014022   214.527   221.497
gene3  150.6450   1.00006 14.4048867 0.000147803 0.00739015   236.446   243.416
gene4   62.5279   1.00012  1.4009098 0.236629326 0.50060858   222.969   229.939
gene5   78.0680   1.00024  1.2599253 0.261704291 0.50060858   203.980   210.951
gene6   88.8574   1.00013 10.0229349 0.001547982 0.02779178   194.182   201.153

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   63.3195  0.687066  0.518838  1.324239 0.1854238  0.461410   213.047
gene2   73.4118  0.332254  0.527712  0.629613 0.5289478  0.678138   214.527
gene3  150.6450  1.121019  0.478497  2.342793 0.0191400  0.206668   236.446
gene4   62.5279  0.267556  0.550919  0.485653 0.6272132  0.729585   222.969
gene5   78.0680 -1.142323  0.622864 -1.833984 0.0666564  0.333282   203.980
gene6   88.8574 -0.539335  0.539076 -1.000481 0.3170780  0.609765   194.182
            BIC
      <numeric>
gene1   220.017
gene2   221.497
gene3   243.416
gene4   229.939
gene5   210.951
gene6   201.153

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   63.3195 -1.074082  0.839010 -1.280179 0.20048233 0.5941217   213.047
gene2   73.4118  0.927933  0.857791  1.081771 0.27935419 0.6124693   214.527
gene3  150.6450 -1.579379  0.758888 -2.081175 0.03741790 0.2672707   236.446
gene4   62.5279 -0.480400  0.890642 -0.539386 0.58962045 0.7967844   222.969
gene5   78.0680 -2.839493  0.975634 -2.910407 0.00360958 0.0451198   203.980
gene6   88.8574  2.911130  0.886285  3.284642 0.00102112 0.0203060   194.182
            BIC
      <numeric>
gene1   220.017
gene2   221.497
gene3   243.416
gene4   229.939
gene5   210.951
gene6   201.153

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>
gene3   150.6450   1.00006  14.40489 0.000147803 0.00739015   236.446   243.416
gene6    88.8574   1.00013  10.02293 0.001547982 0.02779178   194.182   201.153
gene16   59.6816   1.00003   9.88442 0.001667507 0.02779178   204.873   211.843
gene13   86.9408   1.00008   7.44741 0.006355485 0.06755652   227.454   234.424
gene35   45.3740   1.00005   7.33760 0.006755652 0.06755652   199.444   206.414
gene48   76.6157   1.00007   6.72508 0.009511253 0.06931729   224.710   231.680
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.3.0 RC (2023-04-13 r84257)
Platform: x86_64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.6.4

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.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.4.2               BiocParallel_1.34.1        
 [3] NBAMSeq_1.16.0              SummarizedExperiment_1.30.1
 [5] Biobase_2.60.0              GenomicRanges_1.52.0       
 [7] GenomeInfoDb_1.36.0         IRanges_2.34.0             
 [9] S4Vectors_0.38.1            BiocGenerics_0.46.0        
[11] MatrixGenerics_1.12.0       matrixStats_0.63.0         

loaded via a namespace (and not attached):
 [1] KEGGREST_1.40.0         gtable_0.3.3            xfun_0.39              
 [4] bslib_0.4.2             lattice_0.21-8          vctrs_0.6.2            
 [7] tools_4.3.0             bitops_1.0-7            generics_0.1.3         
[10] parallel_4.3.0          RSQLite_2.3.1           AnnotationDbi_1.62.1   
[13] tibble_3.2.1            fansi_1.0.4             highr_0.10             
[16] blob_1.2.4              pkgconfig_2.0.3         Matrix_1.5-4           
[19] lifecycle_1.0.3         GenomeInfoDbData_1.2.10 farver_2.1.1           
[22] compiler_4.3.0          Biostrings_2.68.0       munsell_0.5.0          
[25] DESeq2_1.40.1           codetools_0.2-19        htmltools_0.5.5        
[28] sass_0.4.6              RCurl_1.98-1.12         yaml_2.3.7             
[31] pillar_1.9.0            crayon_1.5.2            jquerylib_0.1.4        
[34] DelayedArray_0.26.2     cachem_1.0.8            nlme_3.1-162           
[37] genefilter_1.82.1       tidyselect_1.2.0        locfit_1.5-9.7         
[40] digest_0.6.31           dplyr_1.1.2             labeling_0.4.2         
[43] splines_4.3.0           fastmap_1.1.1           grid_4.3.0             
[46] colorspace_2.1-0        cli_3.6.1               magrittr_2.0.3         
[49] S4Arrays_1.0.1          survival_3.5-5          XML_3.99-0.14          
[52] utf8_1.2.3              withr_2.5.0             scales_1.2.1           
[55] bit64_4.0.5             rmarkdown_2.21          XVector_0.40.0         
[58] httr_1.4.6              bit_4.0.5               png_0.1-8              
[61] memoise_2.0.1           evaluate_0.21           knitr_1.42             
[64] mgcv_1.8-42             rlang_1.1.1             Rcpp_1.0.10            
[67] DBI_1.1.3               xtable_1.8-4            glue_1.6.2             
[70] annotate_1.78.0         jsonlite_1.8.4          R6_2.5.1               
[73] zlibbioc_1.46.0        

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.