## Twitcher
Twitcher (**t**emplate s**witch** align**er**) is a tool for identification and annotation of short-range template switch events. It builds on the ideas introduced in [Löytynoja and Goldman (2017)](https://doi.org/10.1101/gr.214973.116) and [Löytynoja (2022)](https://doi.org/10.1101/gr.276478.121), but includes a novel algorithm for optimal alignment of two sequences with any number of template switches. Twitcher works on VCF- and BAM-formatted data and can be directly incorporated in genome data analysis pipelines.
Twitcher operates in two stages:
1. While reading the input (VCF or BAM; from stdin or a file), it extracts suspicious™ clusters of SNVs and indels, e.g. more than 2 base changes within 10 bp window.
2. It then reconstructs the alternative (mutated) haplotype for the mutation cluster and flanking sequence, and aligns that with the homologous regions in the reference genome. If the mutation cluster can be better explained with one or more template switches, it outputs the new, reannotated VCF record and/or description of the mutation as a line in a CSV file.
## Algorithm
Twitcher implements the original FPA algorithm (it can be selected with `--fpa`) but, by default, uses a [novel `tsalign` algorithm](https://github.com/algbio/tsalign). Conceptually, `tsalign` builds on the previous implementation and combines classical dynamic programming approaches with the possibility to jump between multiple matrices. However, the number of jumps (and independent template switch events) is not limited and also copy-events in forward orientation can be included. The flexibility comes with a cost, and `tsalign` solves a problem that is $O(n^6)$ in time and memory in the worst case.
### Computational speedups
To reduce the search space and time, we use the A* algorithm with a predictive heuristic to create a lower bound on the remaining alignment cost. In practice, the algorithm is sufficiently fast for analyses of complex sequences but it may fail on repetitive sequences with numerous equally good sub-solutions. Often, that is acceptable, and although low-complexity sequences may evolve through template switching, alternative mechanisms can also explain copy-number reductions and expansions. (In principle, polymerase "slippage" -- thought to be involved e.g. in microsatellite evolution -- could also be considered a template switch and be identified by Twitcher.)
Another option for speeding up the analysis is to disable some mutation types and parameters. By default, forward copy events are disabled in Twitcher and, similarly to the original FPA tool, it only searches for reverse-complement copy events. Twitcher allows setting different mutation costs for various classes of sites and, by default, only some of these are enabled. The full scale of mutations -- and the relative cost of events -- can be adjusted through the cost model file, as explained in [Twitcher Costs](docs/costs.md).
The greatest speed up for the `tsalign` algorithm is not to use it at all. Twitcher can build and utilise a database of known mutation clusters and retrieve the solution for a known pattern without the alignment step. This gives a massive acceleration in analyses of population data where individuals contain only few unique mutations and largely share the mutation patterns with others. A Twitcher DB can be considered a "panel" of known mutation clusters and be reutilised in production analyses.
### Description of TSM events
The inferred template switch events are described using a novel TSIGAR format, which can be understood as an extension to the CIGAR format. The format is explained in more detail in [TSIGAR Format](docs/output/tsigar.md).
## Installation
Twitcher (and `tsalign`) are written in Rust and available through `cargo install`. Those not familiar with Rust may prefer the prebuilt binary.
### Prebuilt binary (Linux, x86-64)
Every release ships a prebuilt binary; no Rust toolchain required:
```bash
curl -LO https://version.helsinki.fi/kraujasp/twitcher/-/releases/permalink/latest/downloads/binaries/twitcher-linux-x86_64
chmod +x twitcher-linux-x86_64
./twitcher-linux-x86_64 --version
```
The binary is built for `x86-64-v3` (AVX2), so it needs a reasonably modern CPU. More detailed instructions are available in [Twitcher Installation](docs/installation.md).
### From source
Twitcher is an open-source software and the code is available at [https://version.helsinki.fi/kraujasp/twitcher/](https://version.helsinki.fi/kraujasp/twitcher/).
It can be installed from source using `cargo`:
```bash
cargo install twitcher
```
`cargo` (version 1.95.0 or higher is required) is available e.g. via [rustup](https://rustup.rs/). The `clang` C compiler is required to compile the underlying `htslib` bindings.
Instructions for building Twitcher locally are available in [Twitcher Installation](docs/installation.md).
## Usage
Twitcher has two main commands:
- **`twitcher vcf`** — Re-annotate VCF files with template switch mutations
- **`twitcher reads`** — Find template switch mutations in sequencing reads
### Quick Start
```bash
twitcher vcf input.vcf reference.fa -o output.vcf
```
This realigns identified mutation clusters and outputs inferred TSM events as single complex variants.
Detailed instructions for using the program are available in [Twitcher Usage](docs/usage.md).
### Examples
```bash
# Realign with larger context window
twitcher vcf input.vcf reference.fa --padding 500 -o output.vcf
# Only output realigned variants
twitcher vcf input.vcf reference.fa --only-realigned -o output.vcf
# Use reads to (try to) phase ambiguous clusters
twitcher vcf input.vcf reference.fa --bam-file reads.bam -o output.vcf
# Export template switch regions
twitcher vcf input.vcf reference.fa -o output.vcf --output-regions ts_regions.bed
# Use a database to store known cases
twitcher vcf input.vcf reference.fa --db known.db -o output.vcf
```