Install GAMclust package:

devtools::install_github("alserglab/GAMclust")
library(GAMclust)
library(gatom)
library(mwcsr)
library(fgsea)
library(data.table)
library(Seurat)
library(futile.logger)

set.seed(42)

Preparing working environment

First, please load and initialize all objects required for GAM-clustering analysis:

  1. load metabolic network and its metabolites annotation. We provide two networks: KEGG and combined network that includes KEGG, Rhea and transport reactions:

(1.1.) load KEGG metabolic network network.kegg.rds and its metabolites annotation met.kegg.db.rds

# KEGG network:
network <- readRDS(url("http://artyomovlab.wustl.edu/publications/supp_materials/GATOM/network.kegg.rds"))
metabolites.annotation <- readRDS(url("http://artyomovlab.wustl.edu/publications/supp_materials/GATOM/met.kegg.db.rds"))

(1.2.) or load combined metabolic network network.combined.rds, its metabolites annotation met.combined.db.rds and species-specific list of genes that either come from proteome or are not linked to a specific enzyme gene2reaction.combined.mmu.eg.tsv for mouse and gene2reaction.combined.hsa.eg.tsv for human data;

# combined network (KEGG+Rhea+transport reactions):
network <- readRDS(url("http://artyomovlab.wustl.edu/publications/supp_materials/GATOM/network.combined.rds"))
metabolites.annotation <- readRDS(url("http://artyomovlab.wustl.edu/publications/supp_materials/GATOM/met.combined.db.rds"))
gene2reaction.extra <- data.table::fread("http://artyomovlab.wustl.edu/publications/supp_materials/GATOM/gene2reaction.combined.mmu.eg.tsv", colClasses="character")
  1. load species-specific network annotation: org.Hs.eg.gatom.anno for human data or org.Mm.eg.gatom.anno for mouse data;
network.annotation <- readRDS(url("http://artyomovlab.wustl.edu/publications/supp_materials/GATOM/org.Mm.eg.gatom.anno.rds"))
  1. load provided list of metabolites that should not be considered during the analysis as connections between reactions (e.g., CO2, HCO3-, etc);
met.to.filter <- data.table::fread(system.file("mets2mask.lst", package="GAMclust"))$ID
  1. initialize SMGWCS solver:

(4.1.) we recommend to use here either heuristic relax-and-cut solver rnc_solver from mwcsr package,

solver <- mwcsr::rnc_solver()

(4.2.) either proprietary CPLEX solver (free for academy);

cplex.dir <- "/opt/ibm/ILOG/CPLEX_Studio1271"
solver <- mwcsr::virgo_solver(cplex_dir = cplex.dir)
  1. set working directory where the results will be saved to.
work.dir <- "results"
dir.create(work.dir, showWarnings = F, recursive = T)
  1. TEMPORARY: collecting logs while developing the tool.
stats.dir <- file.path(work.dir, "stats")
dir.create(stats.dir, showWarnings = F, recursive = T)

setup_logger <- function(log.file.path, logger.name = "stats.logger") {
  file.appender <- appender.file(log.file.path)
  console.appender <- appender.console()
  combined.appender <- function(line) {
    file.appender(line)
    console.appender(line)
  }
  flog.appender(combined.appender, name = logger.name)
  flog.threshold(TRACE, name = logger.name)
}

log.file <- file.path(stats.dir, "log.txt")
setup_logger(log.file.path = log.file, logger.name = "stats.logger")

Preparing objects for the analysis

Preparing data

GAMclust works with bulk, single cell and spatial RNA-seq data.

This vignette shows how to process single cell RNA-seq data on the example of Tabula Muris Senis data reanalysis.

For single cell data, take 6,000-12,000 genes for the GAM-clustering analysis. To do this while preprocessing data with Seurat pipeline, set variable.features.n = 12000 in SCTransform() function. In case of preprocessing multi-sample data, set nfeatures=12000 in SelectIntegrationFeatures()).

Let’s load already preprocessed data.

seurat_object <- readRDS(url("http://artyomovlab.wustl.edu/publications/supp_materials/GAMclust/tms12k.rds"))

E <- as.matrix(Seurat::GetAssayData(object = seurat_object,
                                    assay = "SCT",
                                    slot = "scale.data"))

nrow(E) # ! make sure this value is in range from 6,000 to 12,000
# [1] 12000
E[1:3, 1:3]
#        AACCATGAGATCCCAT-1-0-0-0 AACCATGTCAGAGGTG-1-0-0-0
# Sox17                -0.1865187               -0.2478802
# Mrpl15               -0.4440852                0.3323707
# Lypla1               -0.4182570               -0.5818529
#        AAGGTTCTCCTAGAAC-1-0-0-0
# Sox17                -0.2390427
# Mrpl15               -0.5901840
# Lypla1               -0.5602515

Genes in your dataset may be named as Symbol, Entrez, Ensembl or RefSeq IDs. One of these names should be specified as value of gene.id.type parameter in prepareData().

If you analyse singe cell or spatial RNA-seq data, please set use.PCA=TRUE in prepareData().

E.prep <- prepareData(E = E,
                      gene.id.type = "Symbol",
                      use.PCA = TRUE,
                      use.PCA.n = 50,
                      network.annotation = network.annotation)

E.prep[1:3, 1:3]
#                PC1        PC2         PC3
# 16176   0.07415203 2.20087518 -0.85846106
# 278180  1.21894952 2.84602959  1.64928876
# 17394  -5.31802334 0.07568864  0.08151284

Preparing network

The prepareNetwork() function defines the structure of the final metabolic modules.

network.prep <- prepareNetwork(E = E.prep,
                               network = network,
                               topology = "metabolites",
                               met.to.filter = met.to.filter,
                               network.annotation = network.annotation,
                               gene2reaction.extra = gene2reaction.extra) # for combined network
# INFO [2026-01-24 15:19:34] Global metabolite network contains 6754 edges.
# INFO [2026-01-24 15:19:34] Largest connected component of this global network contains 1397 nodes and 5139 edges.

Preclustering

The preClustering() function defines initial patterns using k-medoids clustering on gene expression matrix. It is strongly recommended to do initial clustering with no less than 32 clusters (initial.number.of.clusters = 32).

You can visualize the initial heatmap as shown below.

cur.centers <- preClustering(E.prep = E.prep,
                             network.prep = network.prep,
                             initial.number.of.clusters = 32,
                             network.annotation = network.annotation)
# INFO [2026-01-24 15:19:34] 1123 metabolic genes from the analysed dataset mapped to this component.
cur.centers[1:3, 1:3]
#         PC1         PC2       PC3
# 1  4.777154 -0.04738601  1.088851
# 2  2.852732  0.71344306 -2.404469
# 3 -1.788987  0.91377131 -1.362991
pheatmap::pheatmap(
      GAMclust:::normalize.rows(cur.centers),
      cluster_rows=F, cluster_cols=F,
      show_rownames=F, show_colnames=T)

GAM-clustering analysis

Now you have everything prepared for the GAM-clustering analysis.

Initial patterns will be now refined in an iterative process. The output of gamClustering() function presents a set of specific subnetworks (also called metabolic modules) that reflect metabolic variability within a given transcriptional dataset.

Note, that it may take a long time to derive metabolic modules by gamClustering() function (tens of minutes).

There is a set of parameters which determine the size and number of your final modules. We recommend you to start with the default settings, however you can adjust them based on your own preferences:

  1. If you consider final modules to bee too small or too big and it complicates interpretation for you, you can either increase or reduce by 10 units the max.module.size parameter.

  2. If among final modules you consider presence of any modules with too similar patterns, you can reduce by 0.1 units the cor.threshold parameter.

  3. If among final modules you consider presence of any uninformative modules, you can reduce by 10 times the p.adj.val.threshold parameter.

results <- gamClustering(E.prep = E.prep,
                         network.prep = network.prep,
                         cur.centers = cur.centers,
                         
                         start.base = 0.5,
                         base.dec = 0.05,
                         max.module.size = 50,

                         cor.threshold = 0.8,
                         p.adj.val.threshold = 1e-5,

                         batch.solver = seq_batch_solver(solver),
                         work.dir = work.dir,
                         
                         show.intermediate.clustering = FALSE,
                         verbose = FALSE,
                         collect.stats = TRUE)
# INFO [2026-01-24 15:19:35] GAM-CLUSTERING starts here.
# INFO [2026-01-24 15:19:35] [*] Iteration 1
# INFO [2026-01-24 15:20:57] [*] Iteration 2
# INFO [2026-01-24 15:22:23] >> max cor exceeded 0.8: 0.86
# INFO [2026-01-24 15:22:23] [*] Iteration 3
# INFO [2026-01-24 15:23:47] [*] Iteration 4
# INFO [2026-01-24 15:25:08] >> max cor exceeded 0.8: 0.82
# INFO [2026-01-24 15:25:08] [*] Iteration 5
# INFO [2026-01-24 15:26:27] [*] Iteration 6
# INFO [2026-01-24 15:27:47] >> max cor exceeded 0.8: 0.82
# INFO [2026-01-24 15:27:47] [*] Iteration 7
# INFO [2026-01-24 15:29:02] [*] Iteration 8
# INFO [2026-01-24 15:30:16] >> max cor exceeded 0.8: 0.83
# INFO [2026-01-24 15:30:16] [*] Iteration 9
# INFO [2026-01-24 15:31:26] [*] Iteration 10
# INFO [2026-01-24 15:32:35] >> max cor exceeded 0.8: 0.85
# INFO [2026-01-24 15:32:35] [*] Iteration 11
# INFO [2026-01-24 15:33:44] [*] Iteration 12
# INFO [2026-01-24 15:34:54] >> max cor exceeded 0.8: 0.82
# INFO [2026-01-24 15:34:54] [*] Iteration 13
# INFO [2026-01-24 15:35:57] [*] Iteration 14
# INFO [2026-01-24 15:37:01] >> max cor exceeded 0.8: 0.82
# INFO [2026-01-24 15:37:01] [*] Iteration 15
# INFO [2026-01-24 15:38:01] [*] Iteration 16
# INFO [2026-01-24 15:39:01] >> max cor exceeded 0.8: 0.84
# INFO [2026-01-24 15:39:01] [*] Iteration 17
# INFO [2026-01-24 15:39:58] [*] Iteration 18
# INFO [2026-01-24 15:40:56] >> max cor exceeded 0.8: 0.82
# INFO [2026-01-24 15:40:56] [*] Iteration 19
# INFO [2026-01-24 15:41:51] [*] Iteration 20
# INFO [2026-01-24 15:42:44] >> max cor exceeded 0.8: 0.84
# INFO [2026-01-24 15:42:44] [*] Iteration 21
# INFO [2026-01-24 15:43:35] [*] Iteration 22
# INFO [2026-01-24 15:44:33] [*] Iteration 23
# INFO [2026-01-24 15:45:22] [*] Iteration 24
# INFO [2026-01-24 15:46:15] [*] Iteration 25
# INFO [2026-01-24 15:47:02] [*] Iteration 26
# INFO [2026-01-24 15:47:53] [*] Iteration 27
# INFO [2026-01-24 15:48:38] [*] Iteration 28
# INFO [2026-01-24 15:49:27] [*] Iteration 29
# INFO [2026-01-24 15:50:14] [*] Iteration 30
# INFO [2026-01-24 15:50:54] [*] Iteration 31
# INFO [2026-01-24 15:51:36] [*] Iteration 32
# INFO [2026-01-24 15:52:17] [*] Iteration 33
# INFO [2026-01-24 15:52:53] [*] Iteration 34
# INFO [2026-01-24 15:53:31] [*] Iteration 35
# INFO [2026-01-24 15:54:05] [*] Iteration 36
# INFO [2026-01-24 15:54:41] [*] Iteration 37
# INFO [2026-01-24 15:55:12] [*] Iteration 38
# INFO [2026-01-24 15:55:47] [*] Iteration 39
# INFO [2026-01-24 15:56:16] [*] Iteration 40
# INFO [2026-01-24 15:56:47] [*] Iteration 41
# INFO [2026-01-24 15:57:14] [*] Iteration 42
# INFO [2026-01-24 15:57:43] [*] Iteration 43
# INFO [2026-01-24 15:58:08] [*] Iteration 44
# INFO [2026-01-24 15:58:35] [*] Iteration 45
# INFO [2026-01-24 15:59:00] [*] Iteration 46
# INFO [2026-01-24 15:59:19] [*] Iteration 47
# INFO [2026-01-24 15:59:41] [*] Iteration 48
# INFO [2026-01-24 15:59:58] [*] Iteration 49
# INFO [2026-01-24 16:00:17] [*] Iteration 50
# INFO [2026-01-24 16:00:32] [*] Iteration 51
# INFO [2026-01-24 16:00:49] [*] Iteration 52
# INFO [2026-01-24 16:01:01] [*] Iteration 53
# INFO [2026-01-24 16:01:21] GAM-CLUSTERING ends here.

Visualizing and exploring the GAM-clustering results

Each metabolic module is a connected piece of metabolic network whose genes expression is correlated across all dataset.

The following functions will help you to visualize and explore the obtained results.

Get graphs of modules:
getGraphs(modules = results$modules,
          network.annotation = network.annotation,
          metabolites.annotation = metabolites.annotation,
          seed.for.layout = 42,
          work.dir = work.dir)
# Graphs for module 1 are built
# Graphs for module 2 are built
# Graphs for module 3 are built
# Graphs for module 4 are built
# Graphs for module 5 are built

Example of the graph of the third module:

m.3 n1--n9 Msra n1--n8 Tst n2--n13 Elovl7 n2--n5 Acsl1 n2--n15 Zdhhc3 n3--n12 Elovl7 n3--n6 Dgat1 n4--n13 Elovl7 n4--n12 Elovl7 n5--n5 Slc27a4 n6--n11 Dgat1 n7--n14 Acpp n7--n10 Abhd5 n8--n15 Zdhhc3 n10--n11 Abhd5 n1 Thioredoxin n9 Thioredoxin disulfide n2 Palmitoyl-CoA n13 3-Oxostearoyl-CoA n3 Acyl-CoA n12 3-Oxoacyl-CoA n4 Malonyl-CoA n5 Hexadecanoic acid n6 Triacylglycerol n11 2,3-Dehydroacyl-CoA n7 1-Acyl-sn-glycerol 3-phosphate n14 1-Acylglycerol n8 [Protein]-L-cysteine n15 S-Palmitoylprotein n10 Phosphatidate

Get gene tables:

The table contains gene list. Each gene has two descriptive values: i) gene’s correlation value with the modules pattern and ii) gene’s score. High score means that this gene’s expression is similar to the module’s pattern and not similar to other modules’ patterns.

m.gene.list <- getGeneTables(modules = results$modules,
                             nets = results$nets,
                             patterns = results$patterns.pos,
                             gene.exprs = E.prep,
                             network.annotation = network.annotation,
                             work.dir = work.dir)
# Gene tables for module 1 are produced
# Gene tables for module 2 are produced
# Gene tables for module 3 are produced
# Gene tables for module 4 are produced
# Gene tables for module 5 are produced

Example of the gene table of the third module:

head(GAMclust:::read.tsv(file.path(work.dir, "m.3.genes.tsv"))) |>
  kableExtra::kable() |>
  kableExtra::kable_styling()
symbol Entrez score cor
Acsl1 14081 -0.9897756 0.1297181
Abhd5 67469 -0.5499853 0.1240232
Tst 22117 1.7352249 0.0769055
Acpp 56318 1.8039528 0.0395354
Msra 110265 2.8572537 0.0343768
Elovl7 74559 2.3507946 0.0203915
Get plots of patterns:
for(i in 1:length(m.gene.list)){
  
  print(fgsea::plotCoregulationProfileReduction(m.gene.list[[i]], 
                                               seurat_object, 
                                               title = paste0("module ", i),
                                               raster = TRUE,
                                               reduction = "pumap"))
}

Get plots of individual genes expression (example for the third module):
Seurat::DefaultAssay(seurat_object) <- "SCT"

i <- 3

Seurat::FeaturePlot(seurat_object, 
                    slot = "data",
                    reduction = "pumap",
                    order = T,
                    features = m.gene.list[[i]],
                    ncol = 6,
                    raster = TRUE,
                    combine = TRUE)

Get tables and plots with annotation of modules:

Functional annotation of obtained modules is done based on KEGG and Reactome canonical metabolic pathways.

getAnnotationTables(network.annotation = network.annotation,
                    nets = results$nets,
                    work.dir = work.dir)
# Pathway annotation for module 1 is produced
# Pathway annotation for module 2 is produced
# Pathway annotation for module 3 is produced
# Pathway annotation for module 4 is produced
# Pathway annotation for module 5 is produced

Example of the annotation table of the third module:

head(GAMclust:::read.tsv(file.path(work.dir, "m.3.pathways.tsv"))) |>
  kableExtra::kable() |>
  kableExtra::kable_styling(font_size = 8) |>
  kableExtra::column_spec(1, width = "1.6in") |>
  kableExtra::column_spec(2:6, width = "0.5in") |>
  kableExtra::column_spec(7, width = "1.2in")
pathway pval padj foldEnrichment overlap size overlapGenes
mmu_M00086: beta-Oxidation, acyl-CoA synthesis 0.0065147 1 153.50000 1 1 Acsl1
mmu_M00089: Triacylglycerol biosynthesis 0.0194484 1 51.16667 1 3 Dgat1

Annotation heatmap for all modules:

getAnnotationHeatmap(work.dir = work.dir)
# Processing module 1
# Module size: 14
# Processing module 2
# Module size: 15
# Processing module 3
# Module size: 9
# Processing module 4
# Module size: 8
# Processing module 5
# Module size: 18

Compare modules obtained in different runs:

You may also compare two results of running GAM-clustering on the same dataset (e.g. runs with different parameters) or compare two results of running GAM-clustering on different datasets (then set same.data=FALSE).

modulesSimilarity(dir1 = work.dir,
                  dir2 = "old.dir",
                  name1 = "new",
                  name2 = "old",
                  same.data = TRUE,
                  use.genes.with.pos.score = TRUE,
                  work.dir = work.dir,
                  file.name = "comparison.png")