1 Introduction

Functional enrichment analysis methods such as gene set enrichment analysis (GSEA) have been widely used for analyzing gene expression data. GSEA is a powerful method to infer results of gene expression data at a level of gene sets by calculating enrichment scores for predefined sets of genes. GSEA depends on the availability and accuracy of gene sets. There are overlaps between terms of gene sets or categories because multiple terms may exist for a single biological process, and it can thus lead to redundancy within enriched terms. In other words, the sets of related terms are overlapping. Using deep learning, this pakage is aimed to predict enrichment scores for unique tokens or words from text in names of gene sets to resolve this overlapping set issue. Furthermore, we can coin a new term by combining tokens and find its enrichment score by predicting such a combined tokens.

Text can be seen as sequential data, either as a sequence of characters or as a sequence of words. Recurrent Neural Network (RNN) operating on sequential data is a type of the neural network. RNN has been applied to a variety of tasks including natural language processing such as machine translation. However, RNN suffers from the problem of long-term dependencies which means that RNN struggles to remember information for long periods of time. An Long Short-Term Memory (LSTM) network is a special kind of RNN that is designed to solve the long-term dependency problem. The bidirectional LSTM network consists of two distinct LSTM networks, termed the forward LSTM and the backward LSTM, which process the sequences in opposite directions. Gated Recurrent Unit (GRU) is a simplified version of LSTM with less number of parameters, and thus, the total number of parameters can be greatly reduced for a large neural network. LSTM and GRU are known to be successful remedies to the long-term dependency problem. The above models take terms of gene sets as input and enrichment scores as output to predict enrichment scores of new terms.



2 Example

2.1 Terms of gene sets

2.1.1 GSEA

Consider a simple example. Once GSEA is performed, the result calculated from GSEA is fed into the algorithm to train the deep learning models.

library(ttgsea)
library(fgsea)
data(examplePathways)
data(exampleRanks)
names(examplePathways) <- gsub("_", " ", substr(names(examplePathways), 9, 1000))

set.seed(1)
fgseaRes <- fgseaSimple(examplePathways, exampleRanks, nperm = 10000)
data.table::data.table(fgseaRes[order(fgseaRes$NES, decreasing = TRUE),])
##                                                        pathway         pval
##                                                         <char>        <num>
##    1:                                     Mitotic Prometaphase 0.0001520219
##    2:                  Resolution of Sister Chromatid Cohesion 0.0001537043
##    3:                                      Cell Cycle, Mitotic 0.0001255020
##    4:                             RHO GTPases Activate Formins 0.0001534684
##    5:                                               Cell Cycle 0.0001227446
##   ---                                                                      
## 1416: Downregulation of SMAD2 3:SMAD4 transcriptional activity 0.0022655188
## 1417:                                  HATs acetylate histones 0.0002779322
## 1418:                   TRAF3-dependent IRF activation pathway 0.0010962508
## 1419:                                     Nephrin interactions 0.0013498313
## 1420:                                  Interleukin-6 signaling 0.0004174494
##              padj         ES       NES nMoreExtreme  size
##             <num>      <num>     <num>        <num> <int>
##    1: 0.004481064  0.7253270  2.963541            0    82
##    2: 0.004481064  0.7347987  2.954314            0    74
##    3: 0.004481064  0.5594755  2.751403            0   317
##    4: 0.004481064  0.6705979  2.717798            0    78
##    5: 0.004481064  0.5388497  2.688064            0   369
##   ---                                                    
## 1416: 0.028982313 -0.6457899 -1.984552            9    16
## 1417: 0.006365544 -0.4535612 -1.994238            0    68
## 1418: 0.017020529 -0.7176839 -2.022102            4    12
## 1419: 0.019558780 -0.6880106 -2.025979            5    14
## 1420: 0.008590987 -0.8311374 -2.079276            1     8
##                                      leadingEdge
##                                           <list>
##    1:   66336,66977,12442,107995,66442,52276,...
##    2:   66336,66977,12442,107995,66442,52276,...
##    3:   66336,66977,12442,107995,66442,12571,...
##    4:   66336,66977,107995,66442,52276,67629,...
##    5:   66336,66977,12442,107995,66442,19361,...
##   ---                                           
## 1416:    66313,20482,20481,17127,17128,83814,...
## 1417: 74026,319190,244349,75560,13831,246103,...
## 1418:   56489,12914,54131,54123,56480,217069,...
## 1419:   109711,14360,20742,17973,18708,12488,...
## 1420:        16194,16195,16451,12402,16452,20848
### convert from gene set defined by BiocSet::BiocSet to list
#library(BiocSet)
#genesets <- BiocSet(examplePathways)
#gsc_list <- as(genesets, "list")

# convert from gene set defined by GSEABase::GeneSetCollection to list
#library(GSEABase)
#genesets <- BiocSet(examplePathways)
#gsc <- as(genesets, "GeneSetCollection")
#gsc_list <- list()
#for (i in 1:length(gsc)) {
#  gsc_list[[setName(gsc[[i]])]] <- geneIds(gsc[[i]])
#}

#set.seed(1)
#fgseaRes <- fgseaSimple(gsc_list, exampleRanks, nperm = 10000)


2.1.2 deep learning and embedding

Since deep learning architectures are incapable of processing characters or words in their raw form, the text needs to be converted to numbers as inputs. Word embeddings are the texts converted into numbers. For tokenization, unigram and bigram sequences are used as default. An integer is assigned to each token, and then each term is converted to a sequence of integers. The sequences that are longer than the given maximum length are truncated, whereas shorter sequences are padded with zeros. Keras is a higher-level library built on top of TensorFlow. It is available in R through the keras package. The input to the Keras embedding are integers. These integers are of the tokens. This representation is passed to the embedding layer. The embedding layer acts as the first hidden layer of the neural network.

if (keras::is_keras_available() & reticulate::py_available()) {
  # model parameters
  num_tokens <- 1000
  length_seq <- 30
  batch_size <- 32
  embedding_dim <- 50
  num_units <- 32
  epochs <- 10
  
  # algorithm
  ttgseaRes <- fit_model(fgseaRes, "pathway", "NES",
                         model = bi_lstm(num_tokens, embedding_dim,
                                         length_seq, num_units),
                         num_tokens = num_tokens,
                         length_seq = length_seq,
                         epochs = epochs,
                         batch_size = batch_size,
                         use_generator = FALSE,
                         callbacks = keras::callback_early_stopping(
                            monitor = "loss",
                            patience = 10,
                            restore_best_weights = TRUE))
  
  # prediction for every token
  ttgseaRes$token_pred
  ttgseaRes$token_gsea[["TGF beta"]][,1:5]
}
## Epoch 1/10
## 45/45 - 5s - loss: 1.5457 - pearson_correlation: 0.1306 - 5s/epoch - 121ms/step
## Epoch 2/10
## 45/45 - 2s - loss: 1.3272 - pearson_correlation: 0.4064 - 2s/epoch - 46ms/step
## Epoch 3/10
## 45/45 - 2s - loss: 0.9995 - pearson_correlation: 0.6110 - 2s/epoch - 40ms/step
## Epoch 4/10
## 45/45 - 2s - loss: 0.8047 - pearson_correlation: 0.6908 - 2s/epoch - 38ms/step
## Epoch 5/10
## 45/45 - 2s - loss: 0.7130 - pearson_correlation: 0.7291 - 2s/epoch - 44ms/step
## Epoch 6/10
## 45/45 - 2s - loss: 0.6468 - pearson_correlation: 0.7555 - 2s/epoch - 46ms/step
## Epoch 7/10
## 45/45 - 2s - loss: 0.6055 - pearson_correlation: 0.7748 - 2s/epoch - 42ms/step
## Epoch 8/10
## 45/45 - 2s - loss: 0.5696 - pearson_correlation: 0.7905 - 2s/epoch - 44ms/step
## Epoch 9/10
## 45/45 - 2s - loss: 0.5538 - pearson_correlation: 0.7972 - 2s/epoch - 44ms/step
## Epoch 10/10
## 45/45 - 2s - loss: 0.5341 - pearson_correlation: 0.8031 - 2s/epoch - 43ms/step
## 32/32 - 1s - 690ms/epoch - 22ms/step
##                                                                       pathway
## 614                               TGF-beta receptor signaling activates SMADs
## 615                                    Signaling by TGF-beta Receptor Complex
## 624                             Downregulation of TGF-beta receptor signaling
## 1267 TGF-beta receptor signaling in EMT epithelial to mesenchymal transition 
##             pval       padj         ES       NES
## 614  0.042648445 0.21175102 -0.4407843 -1.551889
## 615  0.004676539 0.04614047 -0.4244875 -1.763638
## 624  0.033777574 0.18377071 -0.4868887 -1.591561
## 1267 0.726840855 0.88913295 -0.2872284 -0.788917


2.1.3 Monte Carlo p-value

Deep learning models predict only enrichment scores. The p-values of the scores are not provided by the model. So, the Monte Carlo p-value method is used within the algorithm. Computing the p-value for a statistical test can be easily accomplished via Monte Carlo. The ordinary Monte Carlo is a simulation technique for approximating the expectation of a function for a general random variable, when the exact expectation cannot be found analytically. The Monte Carlo p-value method simply simulates a lot of datasets under the null, computes a statistic for each generated dataset, and then computes the percentile rank of observed value among these sets of simulated values. The number of tokens used for each simulation is the same to the length of the sequence of the corresponding term. If a new text does not have any tokens, its p-value is not available.

if (exists("ttgseaRes")) {
  # prediction with MC p-value
  set.seed(1)
  new_text <- c("Cell Cycle DNA Replication",
                "Cell Cycle",
                "DNA Replication",
                "Cycle Cell",
                "Replication DNA",
                "TGF-beta receptor")
  print(predict_model(ttgseaRes, new_text))
  print(predict_model(ttgseaRes, "data science"))
}
## 1/1 - 0s - 26ms/epoch - 26ms/step
## 32/32 - 0s - 331ms/epoch - 10ms/step
## 32/32 - 0s - 310ms/epoch - 10ms/step
## 32/32 - 0s - 312ms/epoch - 10ms/step
## 32/32 - 0s - 311ms/epoch - 10ms/step
## 32/32 - 0s - 325ms/epoch - 10ms/step
## 32/32 - 0s - 307ms/epoch - 10ms/step
## 32/32 - 0s - 328ms/epoch - 10ms/step
## 32/32 - 0s - 310ms/epoch - 10ms/step
## 32/32 - 0s - 318ms/epoch - 10ms/step
## 32/32 - 0s - 304ms/epoch - 9ms/step
## 32/32 - 0s - 310ms/epoch - 10ms/step
## 32/32 - 0s - 305ms/epoch - 10ms/step
##                     new_text test_value MC_p_value adj_p_value
## 1 Cell Cycle DNA Replication  3.3670430      0.000      0.0000
## 2                 Cell Cycle  2.0140176      0.010      0.0150
## 3            DNA Replication  2.6307242      0.002      0.0060
## 4                 Cycle Cell  0.7734405      0.256      0.2560
## 5            Replication DNA  1.9659741      0.004      0.0080
## 6        TGF - beta receptor -1.5675679      0.056      0.0672
## 1/1 - 0s - 26ms/epoch - 26ms/step
##        new_text test_value MC_p_value adj_p_value
## 1 datum science 0.06104184         NA          NA


2.1.4 visualization

You are allowed to create a visualization of your model architecture.

if (exists("ttgseaRes")) {
  summary(ttgseaRes$model)
  plot_model(ttgseaRes$model)
}
## Model: "model"
## ________________________________________________________________________________
##  Layer (type)                       Output Shape                    Param #     
## ================================================================================
##  input_1 (InputLayer)               [(None, 30)]                    0           
##  embedding (Embedding)              (None, 30, 50)                  50050       
##  bidirectional (Bidirectional)      (None, 64)                      21248       
##  dense (Dense)                      (None, 1)                       65          
## ================================================================================
## Total params: 71,363
## Trainable params: 71,363
## Non-trainable params: 0
## ________________________________________________________________________________


2.2 Leading edge genes

Take another exmaple. A set of names of ranked genes can be seen as sequential data. In the result of GSEA, names of leading edge genes for each gene set are given. The leading edge subset contains genes which contribute most to the enrichment score. Thus the scores of one or more genes of the leading edge subset can be predicted.

if (keras::is_keras_available() & reticulate::py_available()) {
  # leading edge
  LE <- unlist(lapply(fgseaRes$leadingEdge, function(x) gsub(",", "", toString(x))))
  fgseaRes <- cbind(fgseaRes, LE)
  
  # model parameters
  num_tokens <- 1000
  length_seq <- 30
  batch_size <- 32
  embedding_dim <- 50
  num_units <- 32
  epochs <- 10
  
  # algorithm
  ttgseaRes <- fit_model(fgseaRes, "LE", "NES",
                         model = bi_lstm(num_tokens, embedding_dim,
                                         length_seq, num_units),
                         num_tokens = num_tokens,
                         length_seq = length_seq,
                         epochs = epochs,
                         batch_size = batch_size,
                         verbose = 0,
                         callbacks = callback_early_stopping(
                            monitor = "loss",
                            patience = 5,
                            restore_best_weights = TRUE))
  
  # prediction for every token
  ttgseaRes$token_pred
  
  # prediction with MC p-value
  set.seed(1)
  new_text <- c("107995 56150", "16952")
  predict_model(ttgseaRes, new_text)
}



3 Case Study

The “airway” dataset has four cell lines with two conditions, control and treatment with dexamethasone. By using the package “DESeq2”, differntially expressed genes between controls and treated samples are identified from the gene expression data. Then the log2FC is used as a score for GSEA. For GSEA, GOBP for human is obtained from the package “org.Hs.eg.db”, by using the package “BiocSet”. GSEA is performed by the package “fgsea”. Since “fgsea” can accept a list, the type of gene set is converted to a list. Finally, the result of GSEA is fitted to a deep learning model, and then enrichment scores of new terms can be predicted.

if (keras::is_keras_available() & reticulate::py_available()) {
  ## data preparation
  library(airway)
  data(airway)
  
  ## differentially expressed genes
  library(DESeq2)
  des <- DESeqDataSet(airway, design = ~ dex)
  des <- DESeq(des)
  res <- results(des)
  head(res)
  # log2FC used for GSEA
  statistic <- res$"log2FoldChange"
  names(statistic) <- rownames(res)
  statistic <- na.omit(statistic)
  head(statistic)
  
  ## gene set
  library(org.Hs.eg.db)
  library(BiocSet)
  go <- go_sets(org.Hs.eg.db, "ENSEMBL", ontology = "BP")
  go <- as(go, "list")
  # convert GO id to term name
  library(GO.db)
  names(go) <- Term(GOTERM)[names(go)]
  
  ## GSEA
  library(fgsea)
  set.seed(1)
  fgseaRes <- fgsea(go, statistic)
  head(fgseaRes)
  
  ## tokenizing text of GSEA
  # model parameters
  num_tokens <- 5000
  length_seq <- 30
  batch_size <- 64
  embedding_dim <- 128
  num_units <- 32
  epochs <- 20
  # algorithm
  ttgseaRes <- fit_model(fgseaRes, "pathway", "NES",
                         model = bi_lstm(num_tokens, embedding_dim,
                                         length_seq, num_units),
                         num_tokens = num_tokens,
                         length_seq = length_seq,
                         epochs = epochs,
                         batch_size = batch_size,
                         callbacks = keras::callback_early_stopping(
                           monitor = "loss",
                           patience = 5,
                           restore_best_weights = TRUE))
  # prediction
  ttgseaRes$token_pred
  set.seed(1)
  predict_model(ttgseaRes, c("translation response",
                             "cytokine activity",
                             "rhodopsin mediate",
                             "granzyme",
                             "histone deacetylation",
                             "helper T cell",
                             "Wnt"))
}



4 Session information

sessionInfo()
## R version 4.4.0 beta (2024-04-15 r86425)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 22.04.4 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.19-bioc/R/lib/libRblas.so 
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## time zone: America/New_York
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] fgsea_1.30.0  ttgsea_1.12.0 keras_2.15.0 
## 
## loaded via a namespace (and not attached):
##  [1] tidyselect_1.2.1     dplyr_1.1.4          tensorflow_2.16.0   
##  [4] fastmap_1.1.1        textshape_1.7.5      digest_0.6.35       
##  [7] lifecycle_1.0.4      koRpus_0.13-8        tokenizers_0.3.0    
## [10] NLP_0.2-1            magrittr_2.0.3       compiler_4.4.0      
## [13] rlang_1.1.3          sass_0.4.9           tools_4.4.0         
## [16] utf8_1.2.4           yaml_2.3.8           qdapRegex_0.7.8     
## [19] data.table_1.15.4    knitr_1.46           stopwords_2.3       
## [22] htmlwidgets_1.6.4    reticulate_1.36.1    xml2_1.3.6          
## [25] textclean_0.9.3      RColorBrewer_1.1-3   BiocParallel_1.38.0 
## [28] purrr_1.0.2          grid_4.4.0           fansi_1.0.6         
## [31] tm_0.7-13            colorspace_2.1-0     ggplot2_3.5.1       
## [34] scales_1.3.0         zeallot_0.1.0        cli_3.6.2           
## [37] rmarkdown_2.26       DiagrammeR_1.0.11    crayon_1.5.2        
## [40] generics_0.1.3       rstudioapi_0.16.0    tfruns_1.5.3        
## [43] visNetwork_2.1.2     cachem_1.0.8         sylly.en_0.1-3      
## [46] parallel_4.4.0       textstem_0.1.4       base64enc_0.1-3     
## [49] vctrs_0.6.5          Matrix_1.7-0         jsonlite_1.8.8      
## [52] slam_0.1-50          koRpus.lang.en_0.1-4 lgr_0.4.4           
## [55] jquerylib_0.1.4      glue_1.7.0           codetools_0.2-20    
## [58] cowplot_1.1.3        sylly_0.1-6          stringi_1.8.3       
## [61] gtable_0.3.5         munsell_0.5.1        mlapi_0.1.1         
## [64] tibble_3.2.1         pillar_1.9.0         htmltools_0.5.8.1   
## [67] float_0.3-2          rsparse_0.5.1        R6_2.5.1            
## [70] evaluate_0.23        lattice_0.22-6       lexicon_1.2.1       
## [73] png_0.1-8            SnowballC_0.7.1      syuzhet_1.0.7       
## [76] RhpcBLASctl_0.23-42  bslib_0.7.0          text2vec_0.6.4      
## [79] Rcpp_1.0.12          fastmatch_1.1-4      whisker_0.4.1       
## [82] xfun_0.43            pkgconfig_2.0.3



5 References

Alterovitz, G., & Ramoni, M. (Eds.). (2011). Knowledge-Based Bioinformatics: from Analysis to Interpretation. John Wiley & Sons.

Consoli, S., Recupero, D. R., & Petkovic, M. (2019). Data Science for Healthcare: Methodologies and Applications. Springer.

DasGupta, A. (2011). Probability for Statistics and Machine Learning: Fundamentals and Advanced Topics. Springer.

Ghatak, A. (2019). Deep Learning with R. Springer.

Hassanien, A. E., & Elhoseny, M. (2019). Cybersecurity and Secure Information Systems: Challenges and Solutions and Smart Environments. Springer.

Leong, H. S., & Kipling, D. (2009). Text-based over-representation analysis of microarray gene lists with annotation bias. Nucleic acids research, 37(11), e79.

Micheas, A. C. (2018). Theory of Stochastic Objects: Probability, Stochastic Processes and Inference. CRC Press.

Shaalan, K., Hassanien, A. E., & Tolba, F. (Eds.). (2017). Intelligent Natural Language Processing: Trends and Applications (Vol. 740). Springer.