Analysis of the DeepFM Architecture in Pharmacogenomics

Design decisions, results and lessons learned building Pharmagen’s predictive model

Deep Learning
Pharmacogenomics
PyTorch
Fecha de publicación

15 de marzo de 2026

AdvertenciaUpdate — honesty note

This article documents v1 of Pharmagen. A later review found assumptions in data preparation and splitting that likely introduced information leakage and inflated the metrics shown below: I treat them as preliminary and optimistic, not a final result. v2 rebuilds the evaluation with leakage-free splits and metrics reported alongside their methodology. Full context in the post From prototype to production and on the project page.

Why DeepFM and not a classic tabular model

Pharmacogenomics poses a problem that is unusual in machine learning: the interactions between genetic variants and drugs are combinatorial and non-linear. A model like XGBoost captures first-order relationships well, but epistasis (the interaction between multiple genetic loci) requires explicitly modeling second-order and higher interactions.

The DeepFM architecture combines the best of two worlds:

  • Factorization Machines (FM): capture all second-order interactions between features efficiently, without manual feature engineering.
  • Deep Neural Network (DNN): learns higher-order patterns that FMs cannot reach.

\[\hat{y} = \sigma \left( w_0 + \sum_{i=1}^n w_i x_i + \sum_{i=1}^n \sum_{j=i+1}^n \langle v_i, v_j \rangle x_i x_j + y_{DNN} \right)\]


Feature engineering: the decisions that matter

PubChem CID instead of ATC

ATC classification groups drugs by therapeutic use, not by molecular structure. Two drugs in the same ATC group can have completely different genomic-interaction profiles. Using PubChem CID, the model works with real chemical identities.

The synthetic “Genalle” variable

Fusing the rsID with HGVS notation into a single identifier (Genalle) normalizes the allelic representation and avoids ambiguities when the same variant has multiple nomenclatures.

Handling imbalance with Focal Loss

In pharmacogenomics, rare phenotypes (ultrarapid metabolizers, severe adverse reactions) are the most clinically critical yet least represented in the data. Adaptive Focal Loss penalizes errors on these minority classes more heavily, prioritizing patient safety over global accuracy.


Preliminary results (v1) (see the honesty note above)

These figures come from v1’s internal splits and are likely inflated by data leakage. I keep them for transparency and traceability, not as a validated result:

Task Metric (preliminary)
Phenotypic Category 86.89% Precision
Dosage Adjustment 0.8328 Balanced Accuracy
Pharmacokinetics 0.8037 Balanced Accuracy
Effect Direction 66.07% Precision

The effect-direction task (increased/decreased) is the hardest because it depends on clinical contexts that are not always encoded in pure genomic data. It is an area where future GNNs could bring significant improvements.


Lessons learned

  1. Feature engineering matters more than architecture. Moving from ATC to PubChem CID improved performance more than any hyperparameter tuning.
  2. Class imbalance in clinical domains is not a technical problem, it is an ethical one. Optimizing for global accuracy can mask a model that fails systematically on the most dangerous cases.
  3. Optuna + stratified cross-validation is the right combination for small datasets with many classes. 40 trials were enough to converge.
  4. Evaluation matters as much as the model. A high metric is worthless if the protocol allows data leakage. Catching this myself and rebuilding the validation in v2 has been the most valuable lesson of the whole project: it is what separates an experiment from a reliable tool.