Skip to content

Convert a Snakemake rule graph into a metro map

nf-metro converts the rule graph or job DAG that Snakemake prints into a metro map.

Snakemake prints its workflow graph without running any jobs:

Terminal window
snakemake --rulegraph > rules.dot

nf-metro accepts all four forms of this output:

  • --rulegraph (one node per rule) or --dag (one node per job). A rule that runs many jobs becomes one station.
  • DOT (the default) or --rulegraph mermaid-js.

Convert and render in one step with the --from snakemake flag:

Terminal window
nf-metro render rules.dot -o workflow.svg --from snakemake

A five-rule workflow, plus the usual all target rule:

Snakefile
rule all:
input: "multiqc/report.html"
rule fastqc: # reads/{sample}.fq.gz -> fastqc/{sample}.zip
rule trim_reads: # reads/{sample}.fq.gz -> trimmed/{sample}.fq.gz
rule align: # trimmed reads + genome.fa -> aligned/{sample}.bam
rule sort_bam: # aligned/{sample}.bam -> sorted/{sample}.bam
rule multiqc: # every fastqc zip and sorted BAM -> multiqc/report.html
Rendered map
Pipeline 1 Trim Reads Fastqc Align Sort Bam Multiqc Main Fastqc created with nf-metro v3.2.0+dev

Each rule becomes a station, and each dependency between two rules becomes a connection. The converter drops the all rule, because it only collects the final outputs. It drops all only when no other rule uses its outputs.

Two variant callers read the same alignment and meet again at BCFtools Stats:

Snakefile
rule bwa_mem: # reads + genome.fa -> aligned/{sample}.bam
rule samtools_index: # aligned/{sample}.bam -> aligned/{sample}.bam.bai
rule gatk_haplotypecaller: # BAM + BAI + genome.fa -> calls/gatk/{sample}.vcf
rule deepvariant: # BAM + BAI + genome.fa -> calls/deepvariant/{sample}.vcf
rule bcftools_stats: # both VCFs -> stats/{sample}.txt
Rendered map
Pipeline 1 Fastqc Bwa Mem Samtools Index Deepvariant Gatk Haplotypecaller Bcftools Stats Multiqc Main Gatk Haplotypecaller Fastqc created with nf-metro v3.2.0+dev

The main line runs from BWA through DeepVariant to MultiQC, the final rule with the most rules upstream. The GATK branch and the FastQC route each get their own line, so the legend tells the three routes apart.

To put every rule on a single main line instead, convert with --lines sections:

Terminal window
nf-metro convert rules.dot -o workflow.mmd --lines sections
nf-metro render workflow.mmd -o workflow.svg
Rendered map
Pipeline 1 Fastqc Bwa Mem Samtools Index Gatk Haplotypecaller Deepvariant Bcftools Stats Multiqc Main created with nf-metro v3.2.0+dev

Snakemake’s graph output contains rules and dependencies only:

  • One section. The output has no grouping (job group: directives do not appear in it), so every rule goes into one section. nf-metro folds a long section onto more rows.
  • Lines from the graph’s shape. The main line is the longest chain of rules into the main final rule: the one with the most rules upstream. Ties go to the rule with more direct inputs, then to the first by name. Each other chain of rules, longest first, becomes its own line, named after its first rule not already on a line, or after its last rule when every rule on it already is. Lines carry no color, so the theme’s palette colors them. Rename lines or regroup them by hand if your workflow’s routes differ, or convert with --lines sections for a single main line.
  • Rule names as labels. The converter turns bwa_mem into Bwa Mem.
  • No shortcut connections. When a rule reads from both an earlier rule and a rule in between, only the path through the rule in between is drawn. The longer path already shows that order, so the direct dependency would only duplicate it.

Convert to a .mmd file, edit it, then render it:

Terminal window
nf-metro convert rules.dot -o workflow.mmd --title "My Workflow"
# edit workflow.mmd: add sections, lines, and better labels
nf-metro render workflow.mmd -o workflow.svg

nf-metro convert detects Snakemake input automatically. To set the format yourself, use --from snakemake. See the guide for the directives you can add.