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       2      74      13      46      72       7     105      45     459
gene2     189     215      17      33     724       1     642     278       3
gene3     226      44       1      52     202     267       1     130      64
gene4     728     498      22       1     177      15       3       1      17
gene5     339      55      25     198       3       2      76      43     145
gene6     589       1      22     175       1       4      12      79     147
      sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1      707        6        1        2       38        1       67      114
gene2       40       56        7        1        1      697       43      544
gene3      103       21      548       13      360      444        1       85
gene4      116      194        1      135        8      355        2      111
gene5        9       29      257        1        1      246       12      541
gene6      231       17       56      170      138        6     1081        1
      sample18 sample19 sample20
gene1       48        1        5
gene2       44       48        8
gene3      179       25      968
gene4       11        8      105
gene5       67        1       25
gene6        4       22      136

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 63.13188  0.4477818 -0.27312540  0.4139081    0
sample2 78.81099 -1.6380218 -0.31010279 -1.5696608    2
sample3 34.90837  0.2603444  0.63223992 -1.3771187    2
sample4 64.28135  0.1963218  0.61517247 -0.2202156    1
sample5 30.71241 -1.9626649 -0.03298638 -0.6162461    0
sample6 21.87581 -1.1658689  1.31906734 -0.1253764    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   73.2747   1.00003 0.0768108  0.781749  0.897143   218.471   225.441
gene2  128.2247   1.00003 0.0128199  0.909956  0.961245   242.323   249.293
gene3  148.9888   1.00016 0.3797029  0.537802  0.790886   254.258   261.228
gene4   91.7294   1.00006 1.8070581  0.178875  0.508607   223.599   230.569
gene5   70.8992   1.00006 2.0057640  0.156713  0.508607   225.042   232.012
gene6  117.7662   1.00008 0.6081517  0.435491  0.702405   227.222   234.192

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   73.2747 -0.2741298  0.343910 -0.797098 0.42539433 0.6861199   218.471
gene2  128.2247 -0.6776574  0.326704 -2.074223 0.03805858 0.2619053   242.323
gene3  148.9888  0.0868157  0.305268  0.284392 0.77611022 0.8418301   254.258
gene4   91.7294 -0.3800270  0.316189 -1.201897 0.22940359 0.5403049   223.599
gene5   70.8992  0.0987354  0.315541  0.312908 0.75435041 0.8418301   225.042
gene6  117.7662  0.8401965  0.302848  2.774314 0.00553183 0.0612428   227.222
            BIC
      <numeric>
gene1   225.441
gene2   249.293
gene3   261.228
gene4   230.569
gene5   232.012
gene6   234.192

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   73.2747 -0.723407  1.069139 -0.676626  0.498643  0.709515   218.471
gene2  128.2247 -0.203194  1.015663 -0.200060  0.841433  0.960453   242.323
gene3  148.9888 -0.469363  0.951647 -0.493211  0.621863  0.840356   254.258
gene4   91.7294 -1.000913  0.977330 -1.024129  0.305774  0.709515   223.599
gene5   70.8992  0.450717  0.981468  0.459228  0.646071  0.849384   225.042
gene6  117.7662 -0.953761  0.944179 -1.010148  0.312424  0.709515   227.222
            BIC
      <numeric>
gene1   225.441
gene2   249.293
gene3   261.228
gene4   230.569
gene5   232.012
gene6   234.192

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>
gene25  150.6752   1.00010  15.94996 6.51374e-05 0.00325687   229.445   236.415
gene47   94.4289   1.00012   8.40062 3.75267e-03 0.09381664   185.108   192.078
gene38   86.3693   1.00016   6.73955 9.43768e-03 0.15729460   220.363   227.333
gene8   127.3620   1.00017   5.65637 1.74025e-02 0.21753169   243.979   250.949
gene35   54.1477   1.00005   3.13197 7.67799e-02 0.50860712   209.227   216.197
gene22   97.5441   1.00014   2.92327 8.73657e-02 0.50860712   219.205   226.175
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.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server 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    

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

other attached packages:
 [1] ggplot2_3.3.6               BiocParallel_1.32.0        
 [3] NBAMSeq_1.14.0              SummarizedExperiment_1.28.0
 [5] Biobase_2.58.0              GenomicRanges_1.50.0       
 [7] GenomeInfoDb_1.34.0         IRanges_2.32.0             
 [9] S4Vectors_0.36.0            BiocGenerics_0.44.0        
[11] MatrixGenerics_1.10.0       matrixStats_0.62.0         

loaded via a namespace (and not attached):
 [1] httr_1.4.4             sass_0.4.2             bit64_4.0.5           
 [4] jsonlite_1.8.3         splines_4.2.1          bslib_0.4.0           
 [7] assertthat_0.2.1       highr_0.9              blob_1.2.3            
[10] GenomeInfoDbData_1.2.9 yaml_2.3.6             pillar_1.8.1          
[13] RSQLite_2.2.18         lattice_0.20-45        glue_1.6.2            
[16] digest_0.6.30          RColorBrewer_1.1-3     XVector_0.38.0        
[19] colorspace_2.0-3       htmltools_0.5.3        Matrix_1.5-1          
[22] DESeq2_1.38.0          XML_3.99-0.12          pkgconfig_2.0.3       
[25] genefilter_1.80.0      zlibbioc_1.44.0        xtable_1.8-4          
[28] snow_0.4-4             scales_1.2.1           tibble_3.1.8          
[31] annotate_1.76.0        mgcv_1.8-41            KEGGREST_1.38.0       
[34] farver_2.1.1           generics_0.1.3         withr_2.5.0           
[37] cachem_1.0.6           cli_3.4.1              survival_3.4-0        
[40] magrittr_2.0.3         crayon_1.5.2           memoise_2.0.1         
[43] evaluate_0.17          fansi_1.0.3            nlme_3.1-160          
[46] tools_4.2.1            lifecycle_1.0.3        stringr_1.4.1         
[49] locfit_1.5-9.6         munsell_0.5.0          DelayedArray_0.24.0   
[52] AnnotationDbi_1.60.0   Biostrings_2.66.0      compiler_4.2.1        
[55] jquerylib_0.1.4        rlang_1.0.6            grid_4.2.1            
[58] RCurl_1.98-1.9         labeling_0.4.2         bitops_1.0-7          
[61] rmarkdown_2.17         gtable_0.3.1           codetools_0.2-18      
[64] DBI_1.1.3              R6_2.5.1               knitr_1.40            
[67] dplyr_1.0.10           fastmap_1.1.0          bit_4.0.4             
[70] utf8_1.2.2             stringi_1.7.8          parallel_4.2.1        
[73] Rcpp_1.0.9             vctrs_0.5.0            geneplotter_1.76.0    
[76] png_0.1-7              tidyselect_1.2.0       xfun_0.34             

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.