Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,120 @@
|
|
| 1 |
---
|
| 2 |
license: cc-by-nc-4.0
|
| 3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
license: cc-by-nc-4.0
|
| 3 |
---
|
| 4 |
+
|
| 5 |
+
# BioXMol — molecular encoder
|
| 6 |
+
|
| 7 |
+
BioXMol is a graph neural network that turns a molecule (SMILES) into a fixed-length
|
| 8 |
+
embedding. Unlike a structure-only encoder, it was trained with **soft contrastive
|
| 9 |
+
learning** to align molecular structure with biological readouts (Cell Painting
|
| 10 |
+
morphology from JUMP-CP and L1000 transcriptomics), using a momentum (EMA) teacher.
|
| 11 |
+
The result is a structure encoder whose embeddings carry biological signal, intended
|
| 12 |
+
for downstream tasks such as drug-induced liver injury (DILI) prediction.
|
| 13 |
+
|
| 14 |
+
This repository ships the **molecule branch only**. The biological encoders are used
|
| 15 |
+
during pretraining; at inference you provide only SMILES.
|
| 16 |
+
|
| 17 |
+
> **Note on usage.** This is a custom Gated-GNN, not a `transformers` model, so it is
|
| 18 |
+
> **not** loadable with `AutoModel.from_pretrained`. Use the lightweight code below,
|
| 19 |
+
> which depends only on `torch` and `rdkit`.
|
| 20 |
+
|
| 21 |
+
## Files
|
| 22 |
+
|
| 23 |
+
- `bioxmol_soft_seed{0,1,2}.safetensors` — encoder weights, one per training seed
|
| 24 |
+
- `modeling_bioxmol.py` — self-contained model definition
|
| 25 |
+
- `featurizer.py` — SMILES → graph featurizer (`smiles2graph`)
|
| 26 |
+
- `requirements.txt`
|
| 27 |
+
|
| 28 |
+
## Installation
|
| 29 |
+
|
| 30 |
+
```bash
|
| 31 |
+
pip install torch rdkit safetensors huggingface_hub numpy
|
| 32 |
+
```
|
| 33 |
+
|
| 34 |
+
## Usage
|
| 35 |
+
|
| 36 |
+
```python
|
| 37 |
+
import torch
|
| 38 |
+
from huggingface_hub import hf_hub_download
|
| 39 |
+
from safetensors.torch import load_file
|
| 40 |
+
from modeling_bioxmol import GatedGraphNeuralNetwork
|
| 41 |
+
from featurizer import smiles2graph
|
| 42 |
+
|
| 43 |
+
REPO = "arslanmasood/BioXMol"
|
| 44 |
+
|
| 45 |
+
def load_encoder(seed: int = 0):
|
| 46 |
+
model = GatedGraphNeuralNetwork(n_edge=1, in_dim=75, n_conv=6, fc_dims=[1024, 128])
|
| 47 |
+
weights = hf_hub_download(REPO, f"bioxmol_soft_seed{seed}.safetensors")
|
| 48 |
+
model.load_state_dict(load_file(weights))
|
| 49 |
+
model.eval()
|
| 50 |
+
return model
|
| 51 |
+
|
| 52 |
+
def featurize_batch(smiles_list):
|
| 53 |
+
graphs = [smiles2graph(s) for s in smiles_list] # each -> (adj, node_feat)
|
| 54 |
+
n = max(nf.shape[0] for _, nf in graphs) # pad to largest molecule
|
| 55 |
+
adjs, feats, masks = [], [], []
|
| 56 |
+
for adj, nf in graphs:
|
| 57 |
+
adj = torch.as_tensor(adj, dtype=torch.float)
|
| 58 |
+
nf = torch.as_tensor(nf, dtype=torch.float)
|
| 59 |
+
a = torch.zeros(n, n); a[:adj.shape[0], :adj.shape[1]] = adj
|
| 60 |
+
f = torch.zeros(n, nf.shape[1]); f[:nf.shape[0]] = nf
|
| 61 |
+
m = torch.zeros(n, 1); m[:nf.shape[0]] = 1.0 # 1 = real atom
|
| 62 |
+
adjs.append(a); feats.append(f); masks.append(m)
|
| 63 |
+
return torch.stack(adjs), torch.stack(feats), torch.stack(masks)
|
| 64 |
+
|
| 65 |
+
@torch.no_grad()
|
| 66 |
+
def encode(smiles_list, layer="first_fc", seed=0):
|
| 67 |
+
"""layer in {'GNN' (75-d), 'first_fc' (1024-d), 'second_fc' (128-d)}."""
|
| 68 |
+
model = load_encoder(seed)
|
| 69 |
+
adj, node_feat, atom_vec = featurize_batch(smiles_list)
|
| 70 |
+
return model.embed(adj, node_feat, atom_vec, layer=layer)
|
| 71 |
+
|
| 72 |
+
emb = encode(["CC(=O)Oc1ccccc1C(=O)O", "CN1C=NC2=C1C(=O)N(C(=O)N2C)C"])
|
| 73 |
+
print(emb.shape) # (2, 1024) for first_fc
|
| 74 |
+
```
|
| 75 |
+
|
| 76 |
+
## Which embedding to use
|
| 77 |
+
|
| 78 |
+
| `layer` | Dim | What it is |
|
| 79 |
+
|---|---|---|
|
| 80 |
+
| `GNN` | 75 | sum-pooled node features |
|
| 81 |
+
| `first_fc` | 1024 | hidden representation (recommended default for general reuse) |
|
| 82 |
+
| `second_fc` | 128 | contrastive projection head |
|
| 83 |
+
|
| 84 |
+
Standardize SMILES the same way the model was pretrained before featurizing, otherwise
|
| 85 |
+
embeddings will be off-distribution.
|
| 86 |
+
|
| 87 |
+
## Training data
|
| 88 |
+
|
| 89 |
+
- **JUMP-CP** (Cell Painting morphological profiles) and **LINCS L1000** (transcriptomic
|
| 90 |
+
signatures) — used for soft-contrastive pretraining.
|
| 91 |
+
- **DILIRank 2.0** — used for downstream evaluation.
|
| 92 |
+
|
| 93 |
+
Each dataset is governed by its own terms; consult them before redistributing
|
| 94 |
+
derived artifacts.
|
| 95 |
+
|
| 96 |
+
## Evaluation
|
| 97 |
+
|
| 98 |
+
Evaluated on DILIRank 2.0, with emphasis on **activity-cliff** ranking (structurally
|
| 99 |
+
similar drug pairs with divergent toxicity), where representation quality is most
|
| 100 |
+
visible. Results vary across seeds; report the per-seed range rather than a single
|
| 101 |
+
peak when citing this model. Standard cross-validation metrics do not separate the
|
| 102 |
+
methods, which is the expected behaviour for this benchmark.
|
| 103 |
+
|
| 104 |
+
## License
|
| 105 |
+
|
| 106 |
+
Released under **CC-BY-NC-4.0** (attribution, non-commercial). The associated training
|
| 107 |
+
code inherits **GPLv3** from the upstream framework and is distributed separately.
|
| 108 |
+
A patent application covering the method has been filed; non-commercial research use
|
| 109 |
+
is permitted under this license.
|
| 110 |
+
|
| 111 |
+
## Citation
|
| 112 |
+
|
| 113 |
+
```bibtex
|
| 114 |
+
@misc{bioxmol,
|
| 115 |
+
title = {BioXMol: multimodal soft-contrastive molecular representations},
|
| 116 |
+
author = {Masood, Arslan and others},
|
| 117 |
+
year = {2026},
|
| 118 |
+
note = {Manuscript in preparation}
|
| 119 |
+
}
|
| 120 |
+
```
|