Read Phasing
Within a cluster the phase of unphased heterozygous variants (e.g. 0/1) is ambiguous: it is not clear which haplotype each mutation sits on. When that happens, the cluster cannot be realigned and is not further considered. If you have the sequencing reads the variant calls were derived from, Twitcher can try to resolve this ambiguity by local read-based phasing, determining which mutations co-occur on the same reads, which then allows realignments.
Provide an indexed BAM file:
twitcher vcf input.vcf reference.fa --phasing-bam reads.bam -o output.vcfLogic
Phasing runs only for clusters that contain at least one unphased heterozygous record.
Also fetched is the reference sequence around the cluster, which is what each record’s alleles are compared against.
Twitcher implicitly considers all possible haplotypes. Every heterozygous record (phased anchors and unphased records alike) is a site with two choices, its two GT alleles. A homozygous record is a site with a single choice, and homozygous-reference records are ignored. For each record Twitcher also works out a comparison span, a stretch of reference. How wide the span is depends on the record.
Each read is then walked through the graph. The read’s anchor is its first base aligned (by the CIGAR) to a window position that lies outside the record’s span; the aligner’s placement is trusted. The anchor can sit anywhere in the window, so reads lying wholly inside a long cluster count. From the anchor the read is followed in both directions, ignoring the CIGAR, as far as the window and the read extend, so soft-clipped bases are compared too. At each site the walk tries every choice and keeps only those that spell the read’s bases; a base below
--phasing-min-base-qualmatches anything.A read gives no evidence when it has no anchor, when it carries the reference allele at a
1/2site, when no path through the graph matches, or when walking it takes more than 4096 choice expansions or a path would have to decide more than 512 sites. There is no fallback: such a read does not serve as evidence at all.A cluster is unsupported when the graph cannot be built: a record has a missing allele or is haploid, a genotype uses a symbolic allele (e.g.
<DEL>; a symbolic ALT the genotype does not use is fine), an indel sits in a repeat too long to bound, or the reference does not cover the window. No reads are scanned for it and no evidence is collected.Each read says which GT slot it carries at each record it decided. The two mates of a read pair are deduplicated.
Variants that are already phased in the input act as symmetry-breakers: their haplotype is known. When there are none, an arbitrary choice is made. Then, the unphased variants are phased one at a time.
A variant is only phased when the read evidence is strong enough (see the parameters below). Variants that stay unresolved remain unphased and continue to block their cluster, which is then output unchanged.
Records phased this way are written with an invented phase set. They carry the TSPHASE INFO flag, so they can be told apart from phasing inherited from the input VCF (see Genotype changes).
Per-haplotype realignment
Once a multi-record cluster is unambiguous, it is decomposed into per-haplotype sub-clusters that are each realigned on their own. Every sub-cluster has to stand on its own: --cluster-max-gap and the validity gates of the active strategy are applied again to it, and sub-clusters that no longer pass are not realigned. Removing the other haplotype’s variants can open a gap or drop a sub-cluster below --cluster-min-records or --cluster-min-mass, so a split can leave a haplotype with nothing to realign. The records of such a sub-cluster are still written, per haplotype, like those of a sub-cluster without a template switch. A phased heterozygous variant contributes to whichever haplotype carries its alternate allele. A homozygous-alternate variant (e.g. 1/1) sits on both haplotypes, so how it is handled depends on the rest of the cluster:
- No phased heterozygous variants: the two haplotypes are identical across the cluster, so each homozygous-alternate stretch is realigned once and reported as homozygous (
1/1). - Phased heterozygous variants present: the haplotypes genuinely differ, so every homozygous-alternate variant is included on each haplotype’s reconstruction. This keeps each haplotype’s realignment input complete (a mutation that is physically present on a haplotype is never omitted) and prevents an unrelated homozygous variant from opening a spurious gap that would split a real sub-cluster.
Variants near a cluster
A sub-cluster’s alt sequence spans --padding bases on either side of the cluster, and a template switch may copy from anywhere in it. Other input records in that window (flanks) are applied to the alt wherever the allele they put on the sub-cluster’s haplotype is certain:
- A homozygous record (
1/1,1|1, …) is applied to every sub-cluster, haploid ones included (e.g. across a PAR boundary). - A phased heterozygous record is applied to a haplotype sub-cluster in the same phase set, with the allele in that haplotype’s slot. The sub-cluster’s phase set must come from the input: the invented phase sets of local phasing never match, but a record phased against an input anchor keeps the anchor’s. Phased genotypes without
PSform one block per contig, as the VCF specification defines. - A haploid record is applied to a haploid sub-cluster.
Any other flank is left as reference, because its allele on the haplotype is unknown: unphased heterozygous records, records of another phase set, missing alleles (the . of .|1), heterozygous records next to a homozygous sub-cluster (whose two haplotypes may differ there), haploid records next to a diploid sub-cluster, symbolic alleles (<DEL>, breakends), and records that reach beyond the window, overlap a flank applied before, or overlap or touch the alignment range (aln_region, cluster ± --range-extension). A * allele changes nothing; the deletion spanning it is a record of its own.
Only records that could join a cluster can be flanks; those that fail FILTER or carry no alternate allele are ignored. Records of the proximity cluster are flanks of every sub-cluster they are not part of, e.g. one split off a sub-cluster on its haplotype by a gap; for them, the genotype resolved by local phasing counts. Records outside --regions or --targets do count: the input is read --padding bases beyond each region, and records outside the regions or targets that are not part of a cluster reaching into them serve as flanks only and are not written to the output. A record belongs to a region or target if it overlaps it.
A template switch whose template lies on the alt (RQ*, QQ*) and overlaps or touches the reference span of an unknown flank may have copied a sequence this haplotype does not carry. Its record carries the TSUNKNOWNFLANK INFO flag, and its CSV row true in ts_template_unknown_flank. Touching counts because an insertion at the template’s edge would extend the copied sequence. Switches copying from the reference are never flagged.
Phasing Parameters
--phasing-bam <FILE>: the indexed BAM/CRAM file with the reads. Without it, no local phasing is performed.--phasing-min-reads(default: 3): minimum number of fragments (reads, with mates counted once) that must support the winning haplotype assignment.--phasing-confidence(default: 0.8): minimum fraction of informative reads that must agree on the assignment, in [0.5, 1.0]. A read is informative for a variant when it covers the position and carries a recognisable allele. Higher values reject conflicting or noisy evidence; lower values phase more aggressively.--phasing-min-mapq(default: 20): minimum read mapping quality (MAPQ). Reads below this are skipped entirely, so mis-mapped reads cannot contribute spurious allele calls.--phasing-min-base-qual(default: 20): minimum base quality (Phred) for a read base to be compared. A base below this matches any allele, so likely sequencing errors neither decide which allele a read carries nor make it match nothing.
If the anchors within a cluster belong to more than one phase set, the cluster is considered too ambiguous and phasing is skipped.
Unresolvable clusters
A cluster that cannot be decomposed into haplotypes is not realigned; its records are passed through to the output VCF unchanged. To find out which clusters these were, and why, pass --output-unresolvable <FILE>:
twitcher vcf input.vcf reference.fa --phasing-bam reads.bam --output-unresolvable unresolvable.tsv -o output.vcfThe file is a headerless TSV with one line per unresolvable cluster and three columns:
| # | Column | Content |
|---|---|---|
| 1 | Region | The cluster’s span as contig:start-end, 1-based and inclusive. |
| 2 | Phasing status | What local phasing did with the cluster (see below). |
| 3 | Reason | Why the cluster is unresolvable: MoreThanOneUnphasedHet (more than one heterozygous record stayed unphased, so the haplotype assignment is ambiguous) MissingAllele (a record’s genotype does not name an alternate allele that can be placed on a haplotype), MixedPloidy (the cluster has both haploid and diploid records) or HomAcrossPhaseSets (a homozygous alternate record sits next to heterozygous records of more than one phase set). |
The phasing status is one of:
trivial: the cluster needed no phasing (nothing was ambiguous to begin with).no reads: no--phasing-bamwas given, so no phasing was attempted.error(region resolution): no region could be derived from the cluster’s records, so no reads could be fetched.error(multiple phasesets),error(bam error),error(other): phasing ran but failed; the parenthesised text names the cause.phased[D,D,…]: phasing ran, with one decisionDper record it attempted, in cluster order:OK_DIRECT/OK_FLIP: resolved; the record keeps the slot order, or has it swapped.NO_EVIDENCE: no informative read covers the record.MIN_READS: too few fragments support the winning assignment (--phasing-min-reads).MIN_CONFIDENCE: the reads disagree too much (--phasing-confidence).
phased[]) means every record was already phased in the input.
Example:
chr1:1000523-1000547 phased[OK_DIRECT,NO_EVIDENCE] MoreThanOneUnphasedHet
chr7:88214-88231 no reads MoreThanOneUnphasedHet
chr7:4410902-4410915 trivial MissingAllele