MAGIBU (Methylation-based Guided Builder) is a modular R framework for building methylation-based classification models. It is platform-agnostic, supporting both Illumina methylation arrays (450K, EPIC, EPICv2) and Oxford Nanopore sequencing data.
Download, unzip and double click MAGIBU.exe (no installation needed) | Express tutorial: classify samples in 6 steps | Full tutorial: every option | All versions
The MAGIBU App is a Windows program with a graphical interface: Illumina arrays and nanopore BAM files are classified with a few clicks. Details in App/README.md.
The rest of this page describes the R functions.
This tutorial covers:
- How to install MAGIBU. Spoiler: you don't need
- Building a reference model from a training dataset
- Projecting new samples onto the reference model for classification
To load the functions into your R environment, navigate to the directory where you saved the files and run the following command:
sapply(list.files(), source)To perform predictions, you first need to create a reference model from a training dataset. In this tutorial, we use the Capper et al. dataset (PMID: 29539639), available from GEO accessions GSE90496 (reference set, 2801 samples) and GSE109379 (validation set, 1104 samples), with sample annotations provided in this repository at CapperAnnotations/samples.clinics.3905.tsv.
After downloading the IDAT files, process them to extract beta values:
process_methylation_data( path.to.idat = "path/to/idats", array.type = "Infinium", # Capper et al. are 450K arrays annot.file.path = "App/app/resources/annot_450k_hg38_from_epicv2.tsv", save.path = "path/to/tsv/reference_samples", n.cores = 60 )
array.type is one of "EpicV2", "EpicV1", "Infinium" (450K) or "Nanopore" (exact spelling).
annot.file.path (columns Name, chr, pos, hg38) is required for "EpicV1" and "Infinium"; for "EpicV2"
leave it NULL.
⚠️ Important: Ensure the output directory (path/to/tsv/reference_samples) contains only the methylation TSV files generated by this function.
umaps <- generate_umap_training_model( methylation.path = "path/to/tsv/reference_samples", metadata_file = "CapperAnnotations/samples.clinics.3905.tsv", save.path = "path/to/reference", name = "capper.umap", n_top_vars = 50000, pca_components = 50, n_neighbors = 15, n_cores = 60 )
The save.path parameter saves the model components for future use. Pre-built model files based on the Capper et al. dataset are available in the Models/ directory.
For this example, we use samples from Mattei et al. (GSE319846):
process_methylation_data( path.to.idat = "path/to/new_idats", array.type = "EpicV1", # GSE319846 samples are EPIC v1 arrays annot.file.path = "Annotations/epic_v1_manifest_hg38.csv", save.path = "path/to/tsv/new_samples", n.cores = 60 )
Nanopore BAM files (aligned to hg38, with MM/ML methylation tags, sorted and indexed) can be converted
into the TSV files used by project_new_samples in two equivalent ways.
Option 1 - MAGIBU-meth (faster). MAGIBU-meth (C, htslib; binary in App/app/bin/) reads the
methylation tags directly from the BAM and writes the same entropy file as modkit + ModkitResorter.sh
(identical row by row on a 30x whole-genome BAM, in 6.5 minutes with 16 threads):
REF=hg38.fasta # your fasta reference. It must match the one used for the BAM alignment.
THN=16
MAGIBU-meth --ref ${REF} -t ${THN} -o sample_sorted.entropy.file.tsv dorado.output.bam
mv sample_sorted.entropy.file.tsv /path/to/tsv/new_samples
Output: one row per CpG, tab separated, no header.
| Column | Content |
|---|---|
| 1 | chromosome |
| 2 | position (0-based C of the CpG, both strands combined) |
| 3 | entropy of the CpG (window of 3 consecutive CpGs) |
| 4 | reads carrying the full 3-CpG pattern |
| 5 | beta |
| 6 | coverage (confident calls) |
A call is methylated if p5mC > 0.85 and unmethylated if p5mC + p5hmC < 0.3; the others are discarded (same rules as ParseModkit.pl of PoreMeth2).
5mC and 5hmC. With a BAM basecalled with a dorado 5mC_5hmC model, MAGIBU-meth can keep the two
modifications apart or add them up:
| Option | Column 5 (beta) | Extra columns |
|---|---|---|
| none (default) | 5mC (original rule) | none |
--sum-5hmc |
5mC + 5hmC (methylated if p5mC + p5hmC > 0.85) | none |
--split-5hmc |
as above, unchanged | 7 beta_5mC, 8 beta_5hmC, 9 n |
With --split-5hmc each call is 5mC (p5mC > 0.85), 5hmC (p5hmC > 0.85), unmodified C
(p5mC + p5hmC < 0.3) or ambiguous (discarded), and beta_5mC = n5mC / n, beta_5hmC = n5hmC / n,
n = n5mC + n5hmC + nC. Columns 1-6 do not change, so the file stays compatible with every script that
reads column 5. beta_5hmC is NA when the BAM has no 5hmC calls (5mC-only basecalling model).
--split-5hmc can be combined with --sum-5hmc.
# entropy + 5mC and 5hmC kept apart, in one run
MAGIBU-meth --ref ${REF} -t ${THN} --split-5hmc -o sample_sorted.entropy.file.tsv dorado.output.bam
Then choose which beta goes into the classification in Step 1-B (nanopore.settings):
# 5mC and 5hmC separated: beta_5mC (column 7, needs --split-5hmc) nanopore.settings = c(chr = "V1", pos = "V2", beta = "V7") # 5mC only (original rule) or 5mC + 5hmC summed (file made with --sum-5hmc): column 5 nanopore.settings = c(chr = "V1", pos = "V2", beta = "V5")
Other options:
| Option | Meaning |
|---|---|
-r regions.bed |
read only these regions (much faster); without it, the whole genome |
--ref hg38.fasta |
CpG sites from the reference (needs the .fai). Without it they are taken from the reads |
--no-entropy |
beta only: no 3-CpG window, columns 3-4 written as NA |
--min-cov N |
confident calls needed to write a CpG (default 3) |
--full-precision |
beta and entropy with full precision instead of 4 decimals |
-t N |
threads |
Option 2 - modkit + ModkitResorter.sh. Run modkit with the following command:
REF=hg38.fasta # your fasta reference. It must match the one used for the BAM alignment.
THN=10
modkit extract full --mapped-only --threads ${THN} --cpg --reference ${REF} dorado.output.bam modkit.output.tsv
Then run the PoreMeth2 script ModkitResorter.sh. You can retrieve ModkitResorter.sh and ParseModkit.pl (required) from PoreMeth2.
sh ModkitResorter.sh modkit.output.tsv
mv *sorted.entropy* /path/to/tsv/new_samples
It generates the TSV files used by the project_new_samples function. This option gives column 5 only
(5mC): to keep 5mC and 5hmC apart use Option 1 with --split-5hmc.
⚠️ Important: Again, ensure the output directory contains only methylation TSV files.
projected <- project_new_samples( training.metadata = "path/to/reference/capper.umap_metadata_result.tsv", model.components = "path/to/reference/capper.umap_model.components.rds", uwot.model = "path/to/reference/capper.umap_umap_model.uwot", path.new.tsv = "path/to/tsv/new_samples", save.path = "path/to/new_projection" ) # Alternatively, you can pass the umaps object directly if still in memory
Create a sample ID vector from your processed files:
my_samples <- gsub("\\.tsv$", "", list.files("path/to/tsv/new_samples"))
Note: The order of sample names in this vector determines the order of results.
plot_umap_zoom_combined( metadata.in = projected$metadata, output.path = "path/to/plots", name = "tutorial_files", sample_id = my_samples )
get_top_3_distances( projected$metadata, sample_ids = my_samples )
This function returns the three nearest reference classes for each sample, along with their distances, useful for assessing classification confidence.
If you use MAGIBU in your research, please cite:
Mattei G, Giunti L, Scagnet M, Agushi R, Mussa F, Caporalini C, Sardi I, Civale VY, Magi A, Genitori L, Buccoliero AM. A MAGIBU-based model for pediatric and juvenile CNS tumors: an in-house epigenetic decision-support framework compared with online DNA methylation classifiers. Free Neuropathol. 2026 Aug 24;7:21. doi: 10.17879/freeneuropathology-2026-9648. PMID: 42683363; PMCID: PMC13532279.
