Skip to contents

Applies semantic similarity reduction to GO enrichment results from a batch of .RData files (both UP and DOWN regulated genes). For each dataset, it invokes process_go to reduce redundancy, merge metadata, and generate visualizations.

Usage

reduce_go_terms(indir, outdir, figdir)

Arguments

indir

Character. Input directory containing GO enrichment .RData files. Expected filenames follow the pattern UP_GO_<name>.RData and DOWN_GO_<name>.RData.

outdir

Character. Output directory to save reduced GO term .RData files.

figdir

Character. Directory to save output plots (treemaps and bar plots).

Value

Invisibly, NULL. It generates and saves .RData and .png files in the specified output directories.

Details

This wrapper function automates analysis across multiple files following the naming pattern UP_GO_<name>.RData or DOWN_GO_<name>.RData, typically produced by GO enrichment pipelines.

For each file, the function:

  • Loads the GO enrichment result.

  • Filters invalid, duplicated, or missing GO IDs.

  • Skips datasets with fewer than 40 unique GO terms.

  • Computes semantic similarity with rrvgo::calculateSimMatrix() using the "Rel" method.

  • Reduces terms via rrvgo::reduceSimMatrix() with a similarity threshold of 0.7.

  • Saves both the reduced term object and merged gene-GO mapping.

  • Generates treemap and bar plots showing parent GO categories.

Internal errors in similarity computation are caught with tryCatch() to allow uninterrupted batch processing across datasets.

Requires the Bioconductor packages rrvgo and org.Hs.eg.db.

See also

process_go for analyzing individual files.

Examples

# \donttest{
if (all(vapply(c("rrvgo", "org.Hs.eg.db", "dplyr", "ggplot2"),
               requireNamespace, logical(1), quietly = TRUE))) {
  indir <- file.path(tempdir(), "reduce_go_terms_example")
  dir.create(indir, showWarnings = FALSE)

  # One file per enrichment result, named UP_GO_<name>.RData or DOWN_GO_<name>.RData
  for (q in c("SLE_UP_moDC", "SLE_DOWN_moDC")) {
    go_bp <- go_celltype_results[go_celltype_results$query == q &
                                   go_celltype_results$source == "GO:BP", ]
    go_bp <- head(go_bp[order(go_bp$p_value), ], 60)
    go_table <- data.frame(go_id = go_bp$term_id, term = go_bp$term_name,
                           p.adjust = go_bp$p_value)
    direction <- if (grepl("_UP_", q, fixed = TRUE)) "UP" else "DOWN"
    save(go_table, file = file.path(indir, paste0(direction, "_GO_moDC.RData")))
  }

  reduce_go_terms(indir = indir, outdir = tempdir(), figdir = tempdir())
}
#> 
#> ------------------------------------
#> Dataset: moDC
#> ------------------------------------
#> Create simMatrix
#> preparing gene to GO mapping data...
#> preparing IC data...
#> Reduce semantic similarity
#> 'select()' returned 1:many mapping between keys and columns
#> Merge metadata
#> Save treemap plot
#> 
#> ------------------------------------
#> Dataset: moDC
#> ------------------------------------
#> Create simMatrix
#> preparing gene to GO mapping data...
#> preparing IC data...
#> Warning: Removed 1 terms without IC information in orgdb for BP
#> Reduce semantic similarity
#> 'select()' returned 1:many mapping between keys and columns
#> Merge metadata
#> Save treemap plot
# }