💻 Command line
Every command is run as krsim <command>, and krsim <command> --help lists all of its options. The examples use seed 42 and write to results/.
Main commands
The simulator itself: run these four in order to go from a transmission genealogy to host VCFs.
🦟 Stage 1 — Simulate genealogy
krsim simulate \
--Nh 100 --T 50 --K 2 --chi 0.18 --C 1 \
--recombination_rate 7.4e-7 \
--random_seed 42 \
--output_dir results/ \
--save_pairsAlways writes the tree sequence ts_{seed}.trees, which every later stage starts from, and the individual–node mapping sim_ind_node_map_{seed}.csv (which strains each host carries, with each node’s time in generations before present; 0 = most recent/current generation), needed by vcf-host-depth and relate-host. The --save_* flags below add extra outputs.
| Flag | Description |
|---|---|
--Nh |
Number of human hosts |
--T |
Transmission generations |
--K |
Parasites transmitted per mosquito bite |
--chi |
Superinfection probability (0–1) |
--C |
Oocysts per mosquito |
--recombination_rate |
Per-bp recombination rate — uses 7.4e-7 (P. falciparum) if not given |
--random_seed |
Seed for reproducibility |
--output_dir |
Output directory |
--save_pairs |
Save recombinant node pairs to node_pairs_{seed}.csv |
--save_lineage |
Save per-generation child→parent strain lineage to lineage_{seed}.csv, plus a node_id_map_{seed}.csv pre/post-simplify() ID mapping — turn this on if you want true generation-by-generation parentage (for krsim strain-transmission-map, krsim pairwise-strain-pedigree-map, or your own pedigree analysis), since the final tree sequence alone can skip generations |
⚡ Stage 2 — Add mutations
krsim mutate \
--ts results/ts_42.trees \
--mutation_rate 1e-6 \
--random_seed 42 \
--output_dir results/If --mutation_rate is not given, 1e-6 per bp is used. Outputs ts_mut_{seed}.trees. All stages from here onwards use the mutated tree sequence.
🧬 Stage 3 — Strain-level VCF
krsim vcf-strain \
--ts results/ts_mut_42.trees \
--random_seed 42 \
--output_dir results/Outputs output_{seed}_strain.vcf — one sample column per parasite strain (node), genotypes 0/1.
🧍 Stage 4 — Host-level VCF
# depth from real depth files
krsim vcf-host-depth \
--strain_vcf results/output_42_strain.vcf \
--ind_node_map_csv results/sim_ind_node_map_42.csv \
--depth_zip my_depth.zip \
--random_seed 42 \
--output_dir results/
# or modelled depth, no data needed
krsim vcf-host-depth \
--strain_vcf results/output_42_strain.vcf \
--ind_node_map_csv results/sim_ind_node_map_42.csv \
--depth_model sWGA \
--random_seed 42 \
--output_dir results/Mixes each host’s strains the way sequencing a blood sample would, with realistic read depth that varies along the genome as in real data (see realistic read depth). Give one depth source:
--depth_zip FILE: a zip of<sample>.window_stats.tsvdepth files (mean and SD of depth in every 500 bp window), made from your own samples withkrsim depth-zip, or an example zip of 100 sWGA or 100 gDNA Pf8 samples. The files must come from reads mapped to the 3D7 reference used by MalariaGEN Pf8 (PlasmoDB-54); krsim checks every file it uses and stops if one doesn’t match. The zip needs at least one file per host. If it has more, files are picked at random, each used at most once, set by--random_seed.--depth_model sWGA|gDNA: depth simulated from a model fitted to Pf8: AR(8) for sWGA, ARMA(2,2) for gDNA, both with Student t kicks. Optional:--depth_mean X: centre of the hosts’ typical depths--depth_spread S: how much hosts’ typical depths differ (SD of log depth);0gives every host--depth_mean, though depth still varies along each host’s genome--depth_params FILE: a JSON file changing any other model value, e.g.{"nu": 3.0, "cap": 1000}; values not listed keep their defaults. The settings arear,ma,mu,scale,nu,offset,cap,end_width,end_factor,depth_meananddepth_spread;krsim vcf-host-depth --helplists the defaults
For an idealised control with no depth variation, use --depth_spread 0 (every host the same typical depth) with --depth_params set to {"scale": 0, "end_width": 0} (no variation along the genome, and no low-depth sWGA chromosome ends). Each site’s read count is still a Poisson draw, so a site can occasionally get 0 reads. This isn’t realistic, but it lets you test a method without the effect of uneven depth.
Sample-level error (optional, either depth source; both 0 by default, so no error):
--error_wt_mut P: the chance that a strain’s WT allele is read as MUT in its host. At a site with several ALT alleles, it becomes one of them, each equally likely--error_mut_wt P: the chance that a strain’s MUT allele is read as WT in its host. This is usually the larger of the two: strains carrying MUT are more often mis-called as WT than the other way round
At each site, in each host, each strain’s allele is read wrong with that chance, and all that strain’s reads go to the wrong allele, so AD, DP and GT follow from them. The error applies to the host VCF only: the strain VCF is not changed, so it still holds the true alleles to compare against. The rates are written to the VCF header (##error_rates), and the number of flipped alleles to the screen and the log. For example, --error_wt_mut 0.005 --error_mut_wt 0.01.
The model:
- Within-host strain proportions sampled once per host from Dirichlet(α=1 per strain)
- Each host’s depth along the genome:
- with
--depth_zip: its depth file’s mean m and SD s in each window - with
--depth_model: a typical depth drawn once per host from a log-normal, times the host’s own relative depth track from the model, capped atcaptimes the typical depth and, for sWGA, lowered near chromosome ends
- with
- At each variant site, the host’s depth is drawn for the site’s window:
- with
--depth_zip: a negative binomial with mean m and SD s (r = m² / (s² − m), p = r / (r + m)), since real read depth varies more than a Poisson; Poisson(m) if s² ≤ m; 0 if m = 0 - with
--depth_model: Poisson around the window’s depth
- with
- With sample-level error: each strain’s allele is read wrong with chance
--error_wt_mut(WT read as MUT) or--error_mut_wt(MUT read as WT) - The depth is split between the host’s strains via Multinomial from the proportions
- Reads are counted by allele and written as
GT:AD:DP:0/0when all reads carry the ref allele,1/1when all carry the alt,0/1when both are seen (a mixed infection), and./.where the depth is 0. At a multi-allelic site (more than one ALT allele, from recurrent mutation),ADgives the reads for every allele andGTlists the two alleles with the most reads (e.g.1/2)
Outputs output_{seed}_host_depth.vcf (its header records the depth source, for --depth_model every value used, and the error rates), host_depth_profiles_{seed}.csv (each host’s depth file, or its typical depth) and host_depth_proportions_{seed}.csv.
Helper commands
Optional: none of these are needed to make the VCFs. They prepare input (depth-zip), give the true coalescence times and relatedness to check results against (coal-mut, relate-strain, relate-host), or draw the transmission pedigree (the two maps). In krsim --help they are listed in pink under Helper commands.
🧰 Zip of depth files
krsim depth-zip \
--bed sample1.per-base.bed.gz sample2.per-base.bed.gz \
--output my_depth.zipNot a pipeline step: it prepares the input for vcf-host-depth --depth_zip. It turns each sample’s mosdepth per-base output (<sample>.per-base.bed.gz) into a depth file (<sample>.window_stats.tsv: mean and SD of depth in every 500 bp window) and puts them all in one zip. Each file is checked against the 3D7 reference used by Pf8. It takes a few seconds per sample.
⏳ Coalescence & mutation times
krsim coal-mut \
--ts results/ts_mut_42.trees \
--random_seed 42 \
--output_dir results/Outputs coal_T_{seed}.csv (pairwise MRCA times per tree interval) and mut_T_{seed}.csv (position and age of every mutation). Both tables include a chromosome column (1-indexed, from krsim/sim/params.py’s COMP_BREAKPOINTS), with position/left/right given as chromosome-local coordinates rather than genome-wide ones. coal_T only contains rows where the sample pair has a common ancestor in that tree interval — pairs with no shared ancestry anywhere in the ARG are simply absent from the table.
coal_T has one row for every pair of strains in every tree, so it is easy to read for a small run like the quick start, but very computationally expensive for larger populations. See Running at scale.
Pedigree visualizations
Both maps need Stage 1 run with --save_lineage; the transmission map also needs the mutated tree sequence from Stage 2. See them in action in the tutorial.
Strain transmission map — visualize the pedigree
krsim strain-transmission-map \
--lineage_csv results/lineage_42.csv \
--node_id_map_csv results/node_id_map_42.csv \
--ts results/ts_mut_42.trees \
--random_seed 42 \
--output_dir results/Needs --save_lineage from Stage 1 and ts_mut_{seed}.trees from Stage 2. Renders an interactive, self-contained strain_transmission_map_{seed}.html — a generation-by-generation pedigree diagram (founders at top, current samples at bottom, one lane per host) built from the pre-simplify lineage_{seed}.csv, so it shows the true parentage that simplify() compresses away. Each strain is labeled with its pre-simplify ID (with its post-simplify ID as a small corner badge, where one exists); clicking a strain traces and highlights its full ancestry and descendants. Every mutation is drawn as a × on the pedigree link, and at the time, where it happened. Pick a chromosome and a tree to fade out everything except the genealogy of that stretch of genome and its mutations. Open the file directly in a browser — no server or dependencies needed. Intended for small debug runs (a handful of hosts, a few dozen generations); it warns in-page and degrades gracefully past that, but isn’t meant for production-scale runs (--Nh 100, etc.).