Skip to content

Commit 657cbda

Browse files
Bug fixes, code updates/extensions and Spectrum Support
1 parent cfa846b commit 657cbda

22 files changed

Lines changed: 427 additions & 196 deletions

.github/workflows/test.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,3 @@ jobs:
4141
run: |
4242
cd tests
4343
pytest
44-
45-
# token: ${{ secrets.CODECOV_TOKEN }}
46-
# fail_ci_if_error: false
47-
# files: coverage.xml

configs/downstream/lm_char.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
seed: 42
2+
root_dir: /scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/data
3+
logs_dir: /scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/logs
4+
datasets:
5+
# - name: Immunogenicity
6+
# task: classification
7+
# - name: Glycosylation
8+
# task: classification
9+
# - name: Tissue
10+
# task: multilabel
11+
- name: Taxonomy_Domain
12+
task: multilabel
13+
model:
14+
- name: glylm
15+
pre_tokenizer: char
16+
token_file: none
17+
model_dir: /scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/unique/GlyLM/none_char_0_t6/checkpoint-5922440
18+
hidden_dim: 320
19+
epochs: 100
20+
learning_rate: 0.001
21+
batch_size: 128
22+
optimizer: Adam
23+
suffix: _none_char_t6_20

configs/downstream/lm_none.yaml

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,32 @@ seed: 42
22
root_dir: /scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/data
33
logs_dir: /scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/logs
44
datasets:
5-
- name: Immunogenicity
6-
task: classification
7-
- name: Glycosylation
8-
task: classification
9-
- name: Tissue
10-
task: multilabel
5+
#- name: Immunogenicity
6+
# task: classification
7+
#- name: Glycosylation
8+
# task: classification
9+
#- name: Tissue
10+
# task: multilabel
1111
- name: Taxonomy_Domain
1212
task: multilabel
1313
model:
14-
- name: glylm
15-
pre_tokenizer: char
16-
token_file: none
17-
model_dir: /scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/unique/GlyLM/none_char_0_t6/checkpoint-5922440
18-
hidden_dim: 320
19-
epochs: 100
20-
learning_rate: 0.001
21-
batch_size: 256
22-
optimizer: Adam
23-
suffix: _none_char_t6_20
2414
- name: glylm
2515
pre_tokenizer: glyles
2616
token_file: none
2717
model_dir: /scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/unique/GlyLM/none_glyles_263_t6/checkpoint-5923620
2818
hidden_dim: 320
2919
epochs: 100
3020
learning_rate: 0.001
31-
batch_size: 256
21+
batch_size: 128
3222
optimizer: Adam
3323
suffix: _none_glyles_t6_20
3424
- name: glylm
3525
pre_tokenizer: lib
3626
token_file: none
37-
model_dir: /scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/unique/GlyLM/none_lib_2_t6/checkpoint-5927260
27+
model_dir: /scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/unique/GlyLM/none_lib_2373_t6/checkpoint-5927260
3828
hidden_dim: 320
3929
epochs: 100
4030
learning_rate: 0.001
41-
batch_size: 256
31+
batch_size: 128
4232
optimizer: Adam
4333
suffix: _none_lib_t6_20

configs/downstream/spec.yaml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
seed: 42
2+
root_dir: /scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/data
3+
logs_dir: /scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/logs_spec_mse
4+
datasets:
5+
- name: Spectrum
6+
task: spectrum
7+
pre-transforms:
8+
GIFFLARTransform:
9+
GNNGLYTransform:
10+
ECFPTransform:
11+
SweetNetTransform:
12+
RGCNTransform:
13+
model:
14+
# - name: rf
15+
# n_estimators: 500
16+
# n_jobs: -1
17+
# random_state: 42
18+
# - name: svm
19+
# random_state: 42
20+
# - name: xgb
21+
# random_state: 42
22+
- name: mlp
23+
feat_dim: 1024
24+
hidden_dim: 1024
25+
batch_size: 256
26+
num_layers: 3
27+
epochs: 3
28+
patience: 30
29+
learning_rate: 0.001
30+
optimizer: Adam
31+
# - name: sweetnet
32+
# feat_dim: 128
33+
# hidden_dim: 1024
34+
# batch_size: 512
35+
# num_layers: 16
36+
# epochs: 100
37+
# patience: 30
38+
# learning_rate: 0.001
39+
# optimizer: Adam
40+
# suffix:
41+
# - name: gnngly
42+
# feat_dim: 128
43+
# hidden_dim: 1024
44+
# batch_size: 512
45+
# num_layers: 8
46+
# epochs: 100
47+
# patience: 30
48+
# learning_rate: 0.001
49+
# optimizer: Adam
50+
# suffix:
51+
# - name: rgcn
52+
# feat_dim: 128
53+
# hidden_dim: 1024
54+
# batch_size: 256
55+
# num_layers: 8
56+
# epochs: 100
57+
# learning_rate: 0.001
58+
# optimizer: Adam
59+
# suffix:
60+
# - name: gifflar
61+
# feat_dim: 128
62+
# hidden_dim: 1024
63+
# batch_size: 256
64+
# num_layers: 8
65+
# epochs: 100
66+
# learning_rate: 0.001
67+
# optimizer: Adam
68+
# pooling: global_mean
69+
# suffix: _128_8_gp
70+
# - name: glylm
71+
# token_file: /scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/unique/GlyLM/bpe_lib_2500.pkl
72+
# model_dir: /scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/unique/GlyLM/bpe_lib_2500_t6/checkpoint-5927260
73+
# pre_tokenizer: lib
74+
# hidden_dim: 320
75+
# epochs: 100
76+
# learning_rate: 0.001
77+
# batch_size: 256
78+
# optimizer: Adam
79+
# suffix: _bpe_lib_25_t6_20

configs/lgi/lgi_lm.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
seed: 42
2+
root_dir: /scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/lgi_data
3+
logs_dir: /scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/lgi_logs
4+
origin: /home/rjo21/Desktop/GIFFLAR/lgi_data_full.pkl
5+
add_valid:
6+
- name: UniLectin
7+
path: /home/rjo21/Desktop/GIFFLAR/experiments/results/unilectin.tsv
8+
task: classification
9+
- name: Canada
10+
path: /home/rjo21/Desktop/GIFFLAR/experiments/results/CanadaData.csv
11+
task: regression
12+
model:
13+
glycan_encoder:
14+
- name: glylm
15+
token_file: /scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/unique/GlyLM/bpe_lib_2500.pkl
16+
model_dir: /scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/unique/GlyLM/bpe_lib_2500_t6/checkpoint-5927260
17+
pre_tokenizer: lib
18+
hidden_dim: 320
19+
lectin_encoder:
20+
- name: ESM
21+
layer_num: 33
22+
batch_size: 256
23+
epochs: 10
24+
learning_rate: 0.001
25+
optimizer: Adam

experiments/aquire_lgi_dataset.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from pathlib import Path
12
import pickle
23
import random
4+
import pandas as pd
35

46
from tqdm import tqdm
57
import numpy as np
@@ -67,6 +69,21 @@ def contrastive_data(decoy_threshold: float = 0, max_num_decoys: int = 4, norm_p
6769
pickle.dump(triplets, f)
6870

6971

72+
def clean_unilectin():
73+
BASE = Path("results")
74+
full = pd.read_csv(BASE / "unilectin.tsv", sep="\t")
75+
clean = pd.read_csv(BASE / "unilectin_glycans.csv")
76+
mapping = dict(full[["PDB_ID", "seq"]].values)
77+
clean["seq"] = clean["pdb_id"].map(mapping)
78+
clean = clean[["supposed_iupac", "seq", "pdb_id"]]
79+
clean.dropna(inplace=True)
80+
clean.rename(columns={"supposed_iupac": "IUPAC", "pdb_id": "PDB_ID"}, inplace=True)
81+
clean["split"] = np.random.choice(["val", "test"], len(clean), p=[2/3, 1/3])
82+
clean["y"] = 1
83+
clean.to_csv(BASE / "unilectin_clean.tsv", sep="\t", index=False)
84+
85+
7086
if __name__ == '__main__':
7187
# classical_data()
72-
contrastive_data()
88+
# contrastive_data()
89+
clean_unilectin()

experiments/contrastive_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def shared_step(self, batch: tuple[HeteroDataBatch, HeteroDataBatch | None] | He
110110
fwd_dict["loss"] = nn.BCEWithLogitsLoss()(fwd_dict["preds"], fwd_dict["labels"].float())
111111
self.add_metrics[dataloader_idx - 1][stage].update(fwd_dict["preds"], fwd_dict["labels"])
112112
self.log(f"{stage}/{name}/loss", fwd_dict["loss"], batch_size=len(fwd_dict["preds"]), add_dataloader_idx=False)
113-
elif task == "regression":
113+
elif task in {"regression", "spectrum"}:
114114
pass
115115
else:
116116
raise ValueError(f"Task {task} is not supported")

experiments/lectinoracle.py

Lines changed: 69 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88

99
from gifflar.data.modules import LGI_GDM
1010
from gifflar.data.datasets import GlycanOnDiskDataset
11-
from experiments.lgi_model import LectinStorage
11+
from experiments.protein_encoding import ENCODER_MAP
12+
from gifflar.model.utils import LectinStorage
1213

13-
le = LectinStorage("ESM", 33)
14+
15+
le = LectinStorage(ENCODER_MAP["ESM"](33), "ESM", 33)
1416

1517
class LGI_OnDiskDataset(GlycanOnDiskDataset):
1618
@property
@@ -39,69 +41,82 @@ def get_ds(dl, split_idx: int):
3941

4042

4143
def collate_lgi(data):
42-
for d in data:
43-
d["train_idx"] = le.query(d["aa_seq"])
44-
44+
print(len(data))
4545
offset = 0
4646
labels, edges, y, train_idx, batch = [], [], [], [], []
47-
for i, d in enumerate(data):
48-
labels.append(d["labels"])
47+
for i, x in enumerate(data):
48+
labels.append(x["sweetnet_x"])
49+
edge_index = x["sweetnet_edge_index"]
4950
edges.append(torch.stack([
50-
d["edge_index"][0] + offset,
51-
d["edge_index"][1] + offset,
51+
edge_index[0] + offset,
52+
edge_index[1] + offset,
5253
]))
53-
offset += len(d["labels"])
54-
y.append(d["y"])
55-
train_idx.append(le.query(d["aa_seq"]))
56-
batch += [i for _ in range(len(d["labels"]))]
54+
offset += len(x["sweetnet_x"])
55+
y.append(x["y"])
56+
train_idx.append(a := le.query(x["aa_seq"]))
57+
if a is None:
58+
print(x["aa_seq"])
59+
batch += [i for _ in range(len(x["labels"]))]
5760

5861
labels = torch.cat(labels, dim=0)
5962
edges = torch.cat(edges, dim=1)
6063
y = torch.stack(y)
6164
train_idx = torch.stack(train_idx)
6265
batch = torch.tensor(batch)
63-
66+
6467
return Batch(
65-
labels=labels,
66-
edge_index=edges,
67-
y=y,
68-
train_idx=train_idx,
69-
batch=batch,
68+
labels=labels.to("cuda"),
69+
edge_index=edges.to("cuda"),
70+
y=y.to("cuda"),
71+
train_idx=train_idx.to("cuda"),
72+
batch=batch.to("cuda"),
7073
)
7174

72-
datamodule = LGI_GDM(
73-
root="/scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/lgi_data", filename="/home/rjo21/Desktop/GIFFLAR/lgi_data_full.pkl", hash_code="8b34af2a",
74-
batch_size=1, transform=None, pre_transform={"GIFFLARTransform": "", "SweetNetTransform": ""},
75-
)
76-
77-
get_ds(datamodule.train_dataloader(), 0)
78-
get_ds(datamodule.val_dataloader(), 1)
79-
get_ds(datamodule.test_dataloader(), 2)
80-
81-
train_set = LGI_OnDiskDataset("/scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/glycowork_full", path_idx=0)
82-
val_set = LGI_OnDiskDataset("/scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/glycowork_full", path_idx=1)
83-
84-
model = prep_model("LectinOracle", num_classes=1)
85-
optimizer = torch.optim.Adam(model.parameters())
86-
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer)
87-
88-
m, met = train_model(
89-
model=model,
90-
dataloaders={"train": torch.utils.data.DataLoader(train_set, batch_size=128, collate_fn=collate_lgi),
91-
"val": torch.utils.data.DataLoader(val_set, batch_size=128, collate_fn=collate_lgi)},
92-
criterion=torch.nn.MSELoss(),
93-
optimizer=optimizer,
94-
scheduler=scheduler,
95-
return_metrics=True,
96-
mode="regression",
97-
num_epochs=100,
98-
patience=100,
99-
)
100-
101-
import pickle
102-
103-
with open("lectinoracle_full_model.pkl", "wb") as f:
104-
pickle.dump(m, f)
105-
with open("lectinoracle_full_metrics.pkl", "wb") as f:
106-
pickle.dump(met, f)
10775

76+
def train():
77+
datamodule = LGI_GDM(
78+
root="/scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/lgi_data", filename="/home/rjo21/Desktop/GIFFLAR/lgi_data_full.pkl", hash_code="8b34af2a",
79+
batch_size=1, transform=None, pre_transform={"GIFFLARTransform": "", "SweetNetTransform": ""}, force_reload=True,
80+
)
81+
82+
model = prep_model("LectinOracle", num_classes=1)
83+
optimizer = torch.optim.Adam(model.parameters())
84+
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer)
85+
86+
m, met = train_model(
87+
model=model,
88+
dataloaders={"train": torch.utils.data.DataLoader(datamodule.train, batch_size=128, collate_fn=collate_lgi),
89+
"val": torch.utils.data.DataLoader(datamodule.val, batch_size=128, collate_fn=collate_lgi)},
90+
criterion=torch.nn.MSELoss(),
91+
optimizer=optimizer,
92+
scheduler=scheduler,
93+
return_metrics=True,
94+
mode="regression",
95+
num_epochs=100,
96+
patience=100,
97+
)
98+
99+
import pickle
100+
101+
with open("lectinoracle_full_model_2.pkl", "wb") as f:
102+
pickle.dump(m, f)
103+
with open("lectinoracle_full_metrics_2.pkl", "wb") as f:
104+
pickle.dump(met, f)
105+
106+
107+
def test():
108+
datamodule = LGI_GDM(
109+
root="/scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/lgi_data", filename="/home/rjo21/Desktop/GIFFLAR/experiments/results/unilectin.tsv",
110+
hash_code="8b34af2a", batch_size=1, transform=None, pre_transform={"GIFFLARTransform": "", "SweetNetTransform": ""}#, force_reload=True,
111+
)
112+
113+
get_ds(datamodule.train_dataloader(), 0)
114+
get_ds(datamodule.val_dataloader(), 1)
115+
get_ds(datamodule.test_dataloader(), 2)
116+
117+
train_set = LGI_OnDiskDataset("/scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/glycowork_full", path_idx=0)
118+
val_set = LGI_OnDiskDataset("/scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/glycowork_full", path_idx=1)
119+
120+
121+
if __name__ == "__main__":
122+
train()

0 commit comments

Comments
 (0)