Epiformer is a PyTorch-based deep learning framework for epistasis detection and phenotype prediction. It builds an end-to-end regression model (Epiformer) by combining:
- Multi-scale CNN + self-attention (
CNN_self_attention) - SNP interaction Transformer (
SNPInteractionAttention) - Cross-Gated MLP fusion (
CrossGatedMLP)
-
Epiformer.py
Main training script. Runs 5-fold cross-validation, selects and saves the best model by PCC (Pearson correlation coefficient). -
singleloci_ importance.py
Extracts single-locus (per-SNP) importance scores (from theCNN_self_attentionbranch) and exports averaged feature weights. -
attention_weight_epi.py
Extracts SNP–SNP interaction attention matrices (from the Transformer branch) and exports an averaged attention matrix.
We recommend using conda (the project root provides environment.yml):
# In the project root
conda env create -f environment.yml
conda activate EpiformerMinimal dependencies (for reference): Python 3.9+ and
pip install torch numpy pandas scikit-learnBy default, scripts read data from the following directory (relative to the project root):
demo_data/data_zeamap/<phe>/
where <phe> is the phenotype name (e.g., KW). The directory should contain at least:
genotype_encoded.npyembeddings.pklSNP_annotation.csvyData.csv
Dataset download link / DOI: (https://doi.org/10.6084/m9.figshare.30448166)
- Training and interpretation outputs are written to:
result/<phe>/
python Epiformer.py --phe KWEpiformer.py supports CLI arguments. If --data_dir/--result_dir are not provided, it uses:
- Data dir:
<project_root>/demo_data/data_zeamap/<phe>/ - Result dir:
<project_root>/result/<phe>/
- Load and concatenate multi-source features (embedding + genotype + SNP annotation).
- Reduce feature dimension with a small MLP and apply standardization.
- Train
Epiformerwith 5-fold cross-validation. - For each epoch, compute:
Train Loss(MSE)Test Loss(MSE)PCCMAE
- If PCC improves for a fold, save the best model:
all_best_model_fold_<k>.pth
- Use Early Stopping (default patience=30) based on PCC.
- Prints best
PCC / MAEfor each fold - Prints the final 5-fold average:
Average PCC ± stdandAverage MAE ± std - Saves best models (per fold) to:
result/<phe>/all_best_model_fold_<k>.pth
python "singleloci_ importance.py"- Loads a trained model checkpoint (default: fold 1)
- Extracts single-locus (per-SNP) importance scores from the local
CNN_self_attentionbranch - Averages scores across samples and exports one importance value per SNP to CSV
Important: singleloci_ importance.py currently hard-codes the following in main:
args.phe = "KW/"args.result_dir = "result/KW"model_path = "result/KW/all_best_model_fold_1.pth"
Default output file:
result/KW/attention_weights_test_3.csv
python attention_weight_epi.py- Loads a trained model checkpoint
- Extracts the attention matrix from the
SNPInteractionAttentionbranch - Averages over samples and exports an SNP×SNP attention matrix
Important: attention_weight_epi.py currently hard-codes the following in main:
args.phe = "KW/"args.result_dir = "result/"model_path = "result/KW/all_best_model_fold_1.pth"
Default output file:
result/KW/attention_weights_matrix_test.csv
-
CUDA / CPU
The code automatically selectscuda:0if available; otherwise it falls back to CPU (slower). -
Reproducibility
A fixed seed is set (set_seed(42)), but small variations may still occur across hardware / library versions. -
SNP contextual embeddings (Evo 2)
Due to differences across experimental environments, we recommend that users use Evo 2 to re-embed the contextual sequence around each SNP (typically 10 bp upstream and 10 bp downstream, plus the SNP itself), then save the resulting embeddings asembeddings.pklunderdemo_data/data_zeamap/<phe>/before training or inference.
- Email:
<zhangxiaowei2118@gmail.com>
