Library for the Development and Use of Phylogenetic Network Methods
The GTR module implements the Generalized Time Reversible (GTR) substitution model and its special-case subclasses (JC, K80, F81, HKY, K81, SYM, TN93) for phylogenetic analysis.
Raised when there is an error in the formulation of a substitution model, whether from invalid inputs or computation issues.
Generalized Time Reversible model. Superclass for all time-reversible substitution models. Implements eigenvalue decomposition for computing matrix exponential e^(Q*t).
Create a GTR substitution model.
| Parameter | Type | Description |
|---|---|---|
| base_freqs | list[float] | Array of state frequencies (must sum to 1) |
| transitions | list[float] | Array of transition rates ((states^2 - states) / 2 long) |
| states | int | Number of states (default 4 for DNA) |
Get the Q (instantaneous rate) matrix.
Populate and normalize the Q matrix.
Compute matrix exponential e^(Q*t) using eigenvalue decomposition.
| Parameter | Type | Description |
|---|---|---|
| t | float | Time (typically in coalescent units) |
Change parameters and recompute Q. Keys: "states", "base frequencies", "transitions".
Get (base_freqs, transitions) tuple.
Get number of states.
Simplest model. All base frequencies and transition rates are equal.
No arguments needed - Q matrix is fixed.
Two-parameter model. Equal base frequencies, distinct transition and transversion rates.
Initialize with transversion (alpha) and transition (beta) parameters. Must sum to 1. Has closed-form solution for e^(Q*t).
Free base frequencies, equal transition rates.
Initialize with 4 base frequencies summing to 1.
Free base frequencies. Transversions equal, transitions equal (pattern [a, b, a, a, b, a]).
Equal base frequencies. Transition rates follow pattern [a, b, c, c, b, a].
Equal base frequencies, all 6 transition rates free.
Free base frequencies. Two different transition parameters (pattern [a, b, a, a, c, a]).
| Model | Free Base Freqs | Transition Pattern | Parameters |
|---|---|---|---|
| JC | No (0.25 each) | All equal | 0 |
| K80 | No (0.25 each) | Ts = Tv | 1 |
| F81 | Yes (4) | All equal | 3 |
| HKY | Yes (4) | Ts = Tv | 4 |
| K81 | No (0.25 each) | [a,b,c,c,b,a] | 2 |
| SYM | No (0.25 each) | All free | 5 |
| TN93 | Yes (4) | [a,b,a,a,c,a] | 5 |
| GTR | Yes (4) | All free | 8 |
from PhyNetPy.GTR import GTR, JC, K80, HKY, TN93
import numpy as np
# Jukes-Cantor (simplest)
jc = JC()
P = jc.expt(0.1) # Transition prob matrix at t=0.1
# Kimura 2-parameter
k80 = K80(alpha=0.7, beta=0.3)
P = k80.expt(0.5)
# HKY model
freqs = [0.3, 0.2, 0.2, 0.3] # Must sum to 1
trans = [1.0, 2.0, 1.0, 1.0, 2.0, 1.0] # [a,b,a,a,b,a] pattern
hky = HKY(freqs, trans)
# Get the Q matrix
Q = hky.getQ()
print(Q)
# Update parameters
hky.set_hyperparams({"base frequencies": [0.25, 0.25, 0.25, 0.25]})
# Full GTR
gtr = GTR(
base_freqs=[0.25, 0.25, 0.25, 0.25],
transitions=[1.0, 2.0, 1.0, 1.0, 2.0, 1.0]
)
P = gtr.expt(1.0)