SKILL.md
Version Compatibility
Reference examples tested with: BioPython 1.83+, TreeTime 0.11+, matplotlib 3.8+, numpy 1.26+, pandas 2.2+, scanpy 1.10+
Before using code patterns, verify installed versions match. If versions differ:
- Python:
pip show <package>thenhelp(module.function)to check signatures - R:
packageVersion('<pkg>')then?function_nameto verify parameters
If code throws ImportError, AttributeError, or TypeError, introspect the installed package and adapt the example to match the actual API rather than retrying.
Transmission Inference
"Infer who infected whom in my outbreak" → Reconstruct transmission networks from genomic and epidemiological data to identify transmission pairs, superspreaders, and unsampled cases.
- R:
TransPhylo::inferTTree()for Bayesian transmission tree inference
TransPhylo in R
library(TransPhylo)
library(ape)
# Load dated phylogeny (from BEAST/TreeTime)
tree <- read.nexus('dated_tree.nexus')
# Convert to TransPhylo format
ptree <- ptreeFromPhylo(tree, dateLastSample = 2020.5)
# Estimate transmission tree
# Uses MCMC to sample from posterior distribution
res <- inferTTree(
ptree,
mcmcIterations = 100000,
startNeg = 0.1, # Initial within-host effective population
startOff.r = 2, # Initial R0 estimate
startOff.p = 0.5, # Initial sampling probability
startPi = 0.9, # Initial probability of being sampled
dateT = 2020.6 # End of outbreak observation
)
# Extract consensus transmission tree
ttree <- extractTTree(res)
# Get transmission pairs
pairs <- ttree$ttree[, c('infector', 'infectee', 'time')]
Prepare Data
def prepare_for_transphylo(dated_tree_file, sample_dates, output_prefix):
'''Prepare inputs for TransPhylo analysis
Requirements:
- Time-scaled phylogeny (from TreeTime or BEAST)
- Sample collection dates
- Tips must have matching names
TransPhylo estimates:
- Who infected whom
- Unsampled cases in the transmission chain
- R0 and generation time
'''
from Bio import Phylo
import pandas as pd
tree = Phylo.read(dated_tree_file, 'nexus')
# Verify all tips have dates
dates_df = pd.read_csv(sample_dates, sep='\t')
tip_names = {clade.name for clade in tree.get_terminals()}
dated_names = set(dates_df['name'])
missing = tip_names - dated_names
if missing:
print(f'Warning: {len(missing)} tips without dates: {missing}')
return {'tree': dated_tree_file, 'dates': sample_dates}
