Frequently Asked Questions¶
General¶
What is MetaGraph?¶
MetaGraph is a framework for scalable construction and querying of very large annotated de Bruijn graphs. Its succinct DNA graph representation uses 2-4 bits per k-mer; annotations require additional space.
Key capabilities:
Index trillions of k-mers with minimal memory
Query sequences with exact matching or alignment
Annotate k-mers with labels, counts, or coordinates
Support DNA, RNA, and protein sequences
When should I use MetaGraph?¶
Good for:
Large-scale sequence search (millions of samples)
Metagenomic classification
Pangenome analysis
Multi-sample comparison
Not ideal for:
Single small datasets (MetaGraph might be overkill – use BLAST)
Full alignment profile or exact alignment (use dedicated aligners)
Installation¶
Which installation method?¶
Conda (easiest):
conda install -c conda-forge -c bioconda --strict-channel-priority metagraphDocker (isolated):
docker run ghcr.io/ratschlab/metagraph:masterSource (for custom alphabets or latest features)
See Installation.
Which alphabet?¶
DNA (default): DNA/RNA sequences (A,C,G,T)
DNA5: With ambiguous bases (A,C,G,T,N)
DNA_CASE_SENSITIVE: Preserves uppercase and lowercase bases as distinct symbols (A,C,G,T,N,a,c,g,t), including lowercase bases used for soft masking. Reverse complements preserve case:
ACGbecomesCGT, andacgbecomescgt. Lowercase bases are still indexed and matched; they are not skipped.Protein: Amino acid sequences (A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,Y,Z,X)
Graph Construction¶
What k-mer size?¶
General rules:
Longer k = more specific, less sensitive
Must be ≤ sequence/read length
The build default is the longest k-mer fitting the alphabet’s packed 64-bit
encoding, rounded down to an odd length for nucleotide alphabets: 31 for DNA/RNA,
21 for DNA5, 15 for case-sensitive DNA, and 12 for protein. This is a storage-based
starting point; choose -k for your search’s sensitivity and specificity.
The workflow CLI uses the selected MetaGraph executable’s build default and records
the resolved value in config.yaml. Pass -k explicitly for reproducible builds.
How many threads to use?¶
Use -p to set the thread count within your CPU allocation. Scaling depends on the operation and available I/O.
What buffer size?¶
When constructing very large graphs, it is recommended to use disk swap to limit the RAM usage:
metagraph build -k 31 --disk-swap /tmp --mem-cap-gb 10 -o graph input.fa
For succinct construction, --mem-cap-gb sets the preallocated buffer size
(default 1 GB). Allow additional RAM for the rest of the process and enough free
space in the swap directory.
Canonical vs Primary graph?¶
Workflow:
Build canonical from reads (contains k-mer + reverse complement)
Extract primary contigs:
metagraph transform --to-fasta --primary-kmersBuild primary graph (half the size, same information)
Important: Use primary graphs with RowDiff annotations for best compression.
How to handle large datasets?¶
Use disk swap:
metagraph build -k 31 --disk-swap /tmp --mem-cap-gb 50 -o graph input.fa
Build sample graphs separately:
# Per sample
for sample in samples/*.fasta.gz; do
metagraph build -k 31 --mode basic -o $sample $sample
metagraph transform --to-fasta --primary-kmers -o ${sample}.contigs ${sample}.dbg
done
# Joint graph from all contigs
ls samples/*.contigs.fasta.gz | metagraph build -k 31 --mode canonical -o joint
# Extract primary contigs from the joint graph
metagraph transform --to-fasta --primary-kmers -o joint_contigs_primary joint.dbg
# Joint primary graph from all contigs
metagraph build -k 31 --mode primary -o joint_primary joint_contigs_primary.fasta.gz
Extremely large graphs:
Extremely large succinct graphs can be constructed by building their parts separately
and writing them to disk on the fly. This is the default succinct build path;
pass --in-ram to construct the graph in RAM instead (requires more RAM, normally not needed).
Annotation¶
Which annotation type?¶
By scale:
In most scenarios, start with:
column(default)Fast and easy to construct:
flatFast queries and small:
row_diff_flatVery large scale:
row_diff_brwt(best compression)
By feature:
k-mer counts:
int_brwtorrow_diff_int_brwtCoordinates:
brwt_coordorrow_diff_brwt_coord
How to annotate with counts?¶
Recommended (for large data):
# Build weighted sample graph
metagraph build -k 31 --count-kmers -o sample.graph sample.fasta.gz
# Extract contigs with counts
metagraph transform --to-fasta -o contigs sample.graph.dbg
# Creates: contigs.fasta.gz + contigs.kmer_counts.gz
# Annotate joint graph
metagraph annotate -i joint.dbg --count-kmers \
--anno-filename -o annotation contigs.fasta.gz
Simple (not recommended for large data):
metagraph annotate -i graph.dbg --count-kmers \
--anno-filename -o annotation input.fa
What if my k-mer counts are very large?
Pass the --count-width flag to specify the number of bits used to represent the counts.
Default is 8 bits (max 255). E.g., with 12 bits, the max count is 4095:
metagraph build -k 31 --count-kmers --count-width 12 -o sample.graph sample.fasta.gz
metagraph transform --to-fasta -o contigs sample.graph.dbg
metagraph annotate -i joint.dbg --count-kmers --count-width 12 \
--anno-filename -o annotation contigs.fasta.gz
Querying¶
How to query?¶
Command line:
# Presence/absence
metagraph query -i graph.dbg -a annotation.column.annodbg query.fa
# Require at least 80% of the query k-mers to match each reported label
metagraph query --min-kmers-fraction-label 0.8 -i graph.dbg -a annotation.column.annodbg query.fa
# K-mer counts
metagraph query --query-mode counts -a annotation.int_brwt.annodbg ...
# Coordinates
metagraph query --query-mode coords -a annotation.brwt_coord.annodbg ...
Python API:
Start metagraph in the server mode:
metagraph server_query -i graph.dbg -a annotation.column.annodbg --port 5555 -p 10 --mmap
Query the index:
from metagraph.client import GraphClient
client = GraphClient('localhost', 5555, api_path='')
# Use a sequence from your input that is at least k bases long.
sequence = 'ACGTTAGCTTCGATGCTAGCTACGGCTTAGATCGACGTACGTAC'
results = client.search(sequence, discovery_fraction=0.8)
What is discovery_fraction?¶
Minimum fraction of k-mers that must match:
0.0: Any match (at least 1 k-mer must match)
0.8: At least 80% of query k-mers match the label
1.0: All k-mers match (strict)
How to align?¶
Exact k-mer matching:
metagraph align --map -i graph.dbg reads.fa
Sequence-to-graph alignment:
metagraph align -i graph.dbg reads.fa
# With annotation (label-consistent paths)
metagraph align -i graph.dbg -a annotation.column.annodbg reads.fa
# Adjust sensitivity
metagraph align --align-min-seed-length 15 --align-min-exact-match 0.7 ...
Performance¶
Memory requirements?¶
Graph:
Default construction: for k=31, at least 16 bytes per k-mer plus overhead (1B k-mers ≈ 18 GB)
Construction with disk swap: buffer size plus output graph size
Succinct (stat): ~4 bits/k-mer (10B k-mers ≈ 5 GB)
Succinct (small): ~2 bits/k-mer (10B k-mers ≈ 2.5 GB)
Large-scale indexing:
When indexing large-scale datasets, everything depends on the chosen buffer sizes. With carefully selected parameters, one can build and annotate graphs with trillion of k-mers. For real examples, see https://github.com/ratschlab/metagraph/blob/master/metagraph/experiments/large_index_scripts.md.
How to speed up construction?¶
Use available threads: Set
-pwithin your CPU allocation.Pre-process the input sequences with KMC:
kmc -k31 -m40 -sm -fm input.fasta.gz output /tmp/Parallelize samples: Build sample graphs in parallel, with 4-8 threads (
-p ...) on each.
How to reduce size?¶
Use primary graph: 50% smaller than canonical (only applies to indexing reads)
Use RowDiff<Multi-BRWT> for annotation (10-20% of Column)
Relax BRWT:
metagraph relax_brwt --relax-arity 32 ...(5-20% smaller)Transform graph:
metagraph transform --state small ...Filter out low-abundance k-mers:
kmc -ci2 ...before building to remove singleton k-mersUse other graph cleaning techniques:
metagraph clean ...to remove sequencing errors
How to reduce RAM usage while querying?¶
Use memory mapping (–mmap): see Memory mapping
Reduce the batch size: see
./metagraph query --batch-size ...Alternative: Use disk-backed formats:
row_diff_disk
When to use memory mapping?¶
When the graphs are extremely large
When the available RAM is limited
When not to use memory mapping?¶
Never use it with slow (e.g., spinning) disks, unless it’s for a single stats check
Troubleshooting¶
Top issues and solutions:
Installation fails¶
Conda: Try installing in a fresh environment
Docker: Pull latest: docker pull ghcr.io/ratschlab/metagraph:master
Source: Check compiler version and the dependencies
See Installation and Troubleshooting.
Out of memory¶
Use disk swap and choose a buffer below your available RAM, leaving room for the graph and other allocations. For complete examples and diagnosis, see Troubleshooting.
RowDiff transform generates no output¶
For annotations with coordinates or counts, look for *.column.annodbg output rather than
*.row_diff.annodbg. Write it to a separate directory: these names match the input filenames,
and using the input directory can overwrite the original columns.
See Convert count-aware annotations and Convert coordinate-aware annotations.