Skip to content

Commit dc8ba1d

Browse files
authored
Merge pull request #37 from Adamtaranto/copilot/update-dash-app-network-plots
[WIP] Update Dash app with haplotype mapping and features
2 parents 07b417e + 1d34ac7 commit dc8ba1d

6 files changed

Lines changed: 757 additions & 2 deletions

File tree

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
## Features
99

10-
- **Multiple Network Algorithms**: MST, MSN, TCS (Statistical Parsimony), and Median-Joining
10+
- **Multiple Network Algorithms**: MST, MSN, TCS (Statistical Parsimony), Median-Joining, and TSW (Parsimony Network)
1111
- **Distance Metrics**: Hamming, Jukes-Cantor, Kimura 2-parameter, Tamura-Nei
1212
- **Comprehensive Analysis**: Network statistics, topology analysis, population genetics measures
1313
- **Rich Visualization**: Static (matplotlib) and interactive (Plotly) network plots
@@ -188,6 +188,16 @@ pypopart network sequences.fasta -a mjn -e 0 -o network.graphml
188188

189189
**Use when**: You want to infer ancestral haplotypes and show complex evolutionary relationships. The epsilon parameter controls network complexity (0 = maximum simplification).
190190

191+
### Tight Span Walker (TSW) - Parsimony Network
192+
193+
Constructs networks using the tight span of the distance matrix, preserving all metric properties.
194+
195+
```bash
196+
pypopart network sequences.fasta -a tsw -o network.graphml
197+
```
198+
199+
**Use when**: You need accurate metric-preserving networks for complex evolutionary relationships with reticulation. Best for small to medium datasets (n < 100). Automatically infers ancestral/median sequences.
200+
191201
## Distance Metrics
192202

193203
- **hamming**: Simple count of differences (fastest)

docs/algorithms/tsw.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Tight Span Walker (TSW) - Parsimony Network Algorithm
2+
3+
## Overview
4+
5+
The Tight Span Walker (TSW) algorithm constructs haplotype networks using parsimony principles by computing the **tight span** of a distance matrix. The tight span is the smallest metric space that contains all optimal paths between sequences, making it ideal for representing complex evolutionary relationships with reticulation events.
6+
7+
## Algorithm Details
8+
9+
### How It Works
10+
11+
1. **Compute dT Distances (Tree Metric)**
12+
- For each pair of sequences (i, j), calculate: `dT(i,j) = max over all k of |d(i,k) - d(j,k)|`
13+
- This represents the minimum distance if sequences were constrained to a tree structure
14+
15+
2. **Build Geodesic Paths**
16+
- For each pair of haplotypes, construct the geodesic (shortest) path
17+
- Compare the actual distance with the dT distance
18+
- If `actual_distance - dT_distance > epsilon`, infer intermediate median vertices
19+
20+
3. **Infer Median Vertices**
21+
- When needed, create intermediate (ancestral) nodes
22+
- Medians represent hypothetical ancestral or intermediate sequences
23+
24+
4. **Construct Network**
25+
- Connect all haplotypes through geodesic paths
26+
- Include inferred median vertices to maintain metric properties
27+
28+
### Key Features
29+
30+
- **Metric Preservation**: Maintains all distance relationships from original data
31+
- **Reticulate Networks**: Can represent complex relationships
32+
- **Ancestral Inference**: Automatically infers hypothetical ancestral sequences
33+
- **Parsimony-Based**: Uses parsimony principles
34+
35+
## Parameters
36+
37+
- **epsilon** (float, default=1e-6): Tolerance for metric comparisons
38+
- **distance_method** (str, default='hamming'): Distance calculation method
39+
40+
## When to Use TSW
41+
42+
✓ Complex evolutionary relationships with reticulation
43+
✓ Small to medium datasets (n < 100)
44+
✓ Accurate metric representation needed
45+
✓ Ancestral sequence inference desired
46+
47+
## Usage Example
48+
49+
```python
50+
from pypopart.algorithms import TightSpanWalker
51+
from pypopart.io import load_alignment
52+
53+
alignment = load_alignment('sequences.fasta')
54+
tsw = TightSpanWalker(distance_method='hamming')
55+
network = tsw.construct_network(alignment)
56+
```
57+
58+
## References
59+
60+
1. Dress, A. W., & Huson, D. H. (2004). Constructing splits graphs. IEEE/ACM Transactions on Computational Biology and Bioinformatics, 1(3), 109-115.
61+
2. Bryant, D., & Moulton, V. (2004). Neighbor-Net. Molecular Biology and Evolution, 21(2), 255-265.

src/pypopart/algorithms/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,25 @@
1010
from .msn import MinimumSpanningNetwork
1111
from .mst import MinimumSpanningTree
1212
from .tcs import TCS
13+
from .tsw import TightSpanWalker
1314

1415
# Convenient aliases
1516
MSTAlgorithm = MinimumSpanningTree
1617
MSNAlgorithm = MinimumSpanningNetwork
1718
TCSAlgorithm = TCS
1819
MJNAlgorithm = MedianJoiningNetwork
20+
TSWAlgorithm = TightSpanWalker
1921

2022
__all__ = [
2123
'NetworkAlgorithm',
2224
'MinimumSpanningTree',
2325
'MinimumSpanningNetwork',
2426
'TCS',
2527
'MedianJoiningNetwork',
28+
'TightSpanWalker',
2629
'MSTAlgorithm',
2730
'MSNAlgorithm',
2831
'TCSAlgorithm',
2932
'MJNAlgorithm',
33+
'TSWAlgorithm',
3034
]

0 commit comments

Comments
 (0)