An interactive educational tool for understanding the mathematical principles of DNA sequence evolution and likelihood calculations in phylogenetics.
This program provides a step-by-step, interactive tutorial that guides users through the process of calculating the likelihood of DNA sequence evolution. It's designed for students and researchers learning phylogenetic methods, offering both detailed educational mode and quick calculation options.
- Interactive Tutorial Mode: Step-by-step guidance through the entire likelihood calculation process
- Quick Calculation Mode: Streamlined interface for experienced users
- Multiple Evolutionary Models: Supports Jukes-Cantor, Kimura 2-parameter, HKY85, and GTR models
- Visual Learning: Matplotlib visualizations of matrices and likelihood curves
- Custom Parameters: Option to input custom rate matrices and base compositions
- Real-time Calculations: See how changing parameters affects results
- Python 3.6 or higher
- NumPy
- SciPy
- Matplotlib
- Clone or download this repository:
git clone https://github.com/mol-evol/Maximum-Likelihood-calculations
cd Maximum-Likelihood-calculations- Install required packages:
pip install -r requirements.txtOr install individually:
pip install numpy scipy matplotlibRun the main program:
python showLikelihoodCalcs.pyYou'll see two main options:
This mode walks you through seven comprehensive steps:
Step 1: Choose Rate Matrix
- Select from predefined models (JC, K2P, HKY85, GTR)
- Option to input custom rate matrix
- Visual representation of your selected matrix
Step 2: Choose Base Composition
- Select equilibrium nucleotide frequencies
- Options: equal, GC-rich, AT-rich, or custom
- Bar chart visualization of composition
Step 3: Calculate Q Matrix
- Detailed substep-by-substep calculation
- Shows unscaled Q, row sum adjustment, and scaling
- Verifies that rows sum to zero
Step 4: Calculate P Matrix
- Explore probability matrices at different time points
- Interactive selection of branch lengths
- See how probabilities change over evolutionary time
Step 5: Input DNA Sequences
- Use example sequences
- Input your own aligned sequences
- Generate random sequences with specified differences
Step 6: Calculate Likelihood
- Position-by-position likelihood calculation
- Shows contribution of each site
- Converts to log-likelihood for interpretation
Step 7: Explore Branch Lengths
- Calculate likelihood for specific branch lengths
- Plot likelihood curves
- Find optimal branch length automatically
- Compare multiple branch lengths
For experienced users:
- Rapid model and parameter selection
- Direct sequence input
- Automatic optimal branch length calculation
- Results with visualization
The program implements the standard likelihood calculation for DNA sequence evolution:
- Rate Matrix (R): Defines relative substitution rates between nucleotides
- Composition Vector (π): Equilibrium base frequencies
- Q Matrix: Instantaneous rate matrix calculated as Q = R × π, scaled for unit substitution rate
- P Matrix: Transition probability matrix P(t) = e^(Qt)
- Likelihood: L = ∏ π(i) × P(i→j) over all sites
- Simplest model with equal substitution rates
- 1 parameter (branch length only)
- Distinguishes transitions from transversions
- 2 parameters
- Unequal base frequencies + transition/transversion bias
- 5 parameters
- Most general model with all rates different
- 9 parameters
# From the tutorial, you'll see calculations like:
Rate Matrix R:
--------------------------------------------------
A C G T
---------------------------------------------
A | --- 1.000 2.000 1.000
C | 1.000 --- 1.000 2.000
G | 2.000 1.000 --- 1.000
T | 1.000 2.000 1.000 ---
Composition vector π:
A: 0.250
C: 0.250
G: 0.250
T: 0.250
# The program then shows Q matrix calculation, P matrix for chosen branch length,
# and step-by-step likelihood calculation for your sequencesThis tool is designed for:
- Learning: Understand each step of likelihood calculation
- Teaching: Demonstrate phylogenetic concepts interactively
- Experimentation: See how different models and parameters affect results
- Verification: Check hand calculations against computed results
- Start with the Guided Tutorial to understand the process
- Try different evolutionary models on the same sequences
- Experiment with branch lengths to see likelihood curves
- Use custom matrices to explore parameter effects
- Compare results with published phylogenetic software
- Designed for educational purposes, not large-scale analyses
- Single site model (no rate heterogeneity across sites)
- No tree topology optimization (single branch only)
- For production analyses, use IQ-TREE, RAxML, or similar
Import errors: Ensure all dependencies are installed
pip install -r requirements.txtDisplay issues: If plots don't appear or you get a "no display" error, you may need to configure matplotlib's backend. Add these lines at the very beginning of the Python file, before any other imports:
import matplotlib
matplotlib.use('TkAgg') # or 'Qt5Agg'This tells matplotlib which graphics system to use. Common backends:
TkAgg: Works on most systems (requires tkinter)Qt5Agg: Alternative if you have PyQt5 installedAgg: For saving plots to files without displaying them
If you're using SSH or a remote server without display capabilities, use:
import matplotlib
matplotlib.use('Agg') # For non-interactive environmentsMemory errors: This tool is for small educational examples. Large alignments need specialized software.
Contributions welcome! Areas for improvement:
- Additional evolutionary models
- Rate heterogeneity across sites
- Tree visualization
- More statistical tests
- Performance optimizations
MIT License - see LICENSE file for details
If you use this tool in teaching or research:
DNA Likelihood Calculator: An Interactive Educational Tool for Phylogenetics
https://github.com/mol-evol/Maximum-Likelihood-calculations
For questions or suggestions, please open an issue on GitHub.
Remember: This is an educational tool. For research-grade phylogenetic analyses, use established software packages like IQ-TREE, RAxML, MEGA, or PAML.