-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathchebi.py
More file actions
1721 lines (1507 loc) · 49.7 KB
/
chebi.py
File metadata and controls
1721 lines (1507 loc) · 49.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
__all__ = [
"JCIData",
"JCIExtendedTokenData",
"JCIExtendedBPEData",
"JCIExtSelfies",
"JCITokenData",
"ChEBIOver100",
"JCI_500_COLUMNS",
"JCI_500_COLUMNS_INT",
]
import os
import pickle
import random
from abc import ABC
from collections import OrderedDict
from itertools import cycle, permutations, product
from typing import TYPE_CHECKING, Any, Dict, Generator, List, Literal, Optional, Union
import numpy as np
import pandas as pd
import torch
from rdkit import Chem
from chebai.preprocessing import reader as dr
from chebai.preprocessing.datasets.base import XYBaseDataModule, _DynamicDataset
if TYPE_CHECKING:
import fastobo
import networkx as nx
# exclude some entities from the dataset because the violate disjointness axioms
CHEBI_BLACKLIST = [
194026,
144321,
156504,
167175,
167174,
167178,
183506,
74635,
3311,
190439,
92386,
]
class JCIBase(XYBaseDataModule):
LABEL_INDEX = 2
SMILES_INDEX = 1
@property
def _name(self):
return "JCI"
@property
def processed_file_names(self):
return ["test.pt", "train.pt", "validation.pt"]
def download(self):
pass
@property
def raw_file_names(self):
return ["test.pkl", "train.pkl", "validation.pkl"]
def _perform_data_preparation(self, *args, **kwargs):
print("Check for raw data in", self.raw_dir)
if any(
not os.path.isfile(os.path.join(self.raw_dir, f))
for f in self.raw_file_names
):
raise ValueError("Raw data is missing")
@staticmethod
def _load_tuples(input_file_path):
with open(input_file_path, "rb") as input_file:
for row in pickle.load(input_file).values:
yield row[1], row[2:].astype(bool), row[0]
@staticmethod
def _get_data_size(input_file_path):
with open(input_file_path, "rb") as f:
return len(pickle.load(f))
def setup_processed(self):
print("Transform splits")
os.makedirs(self.processed_dir, exist_ok=True)
for k in ["test", "train", "validation"]:
print("transform", k)
torch.save(
self._load_data_from_file(os.path.join(self.raw_dir, f"{k}.pkl")),
os.path.join(self.processed_dir, f"{k}.pt"),
)
class JCIData(JCIBase):
READER = dr.OrdReader
class JCISelfies(JCIBase):
READER = dr.SelfiesReader
class JCITokenData(JCIBase):
READER = dr.ChemDataReader
class _ChEBIDataExtractor(_DynamicDataset, ABC):
"""
A class for extracting and processing data from the ChEBI dataset.
Args:
chebi_version_train (int, optional): The version of ChEBI to use for training and validation. If not set,
chebi_version will be used for training, validation and test. Defaults to None.
single_class (int, optional): The ID of the single class to predict. If not set, all available labels will be
predicted. Defaults to None.
subset (Literal["2_STAR", "3_STAR"], optional): If set, only use entities that are part of the given subset.
**kwargs: Additional keyword arguments (passed to XYBaseDataModule).
Attributes:
single_class (Optional[int]): The ID of the single class to predict.
chebi_version_train (Optional[int]): The version of ChEBI to use for training and validation.
subset (Optional[Literal["2_STAR", "3_STAR"]]): If set, only use entities that are part of the given subset.
"""
# ---- Index for columns of processed `data.pkl` (derived from `_graph_to_raw_dataset` method) ------
# "id" at row index 0
# "name" at row index 1
# "SMILES" at row index 2
# labels starting from row index 3
_ID_IDX: int = 0
_DATA_REPRESENTATION_IDX: int = 2
_LABELS_START_IDX: int = 3
def __init__(
self,
chebi_version_train: Optional[int] = None,
single_class: Optional[int] = None,
subset: Optional[Literal["2_STAR", "3_STAR"]] = None,
augment_smiles: bool = False,
aug_smiles_variations: Optional[int] = None,
**kwargs,
):
if bool(augment_smiles):
assert (
int(aug_smiles_variations) > 0
), "Number of variations must be greater than 0"
aug_smiles_variations = int(aug_smiles_variations)
if not kwargs.get("splits_file_path", None):
raise ValueError(
"When using SMILES augmentation, a splits_file_path must be provided to ensure consistent splits."
)
reader_kwargs = kwargs.get("reader_kwargs", {})
reader_kwargs["canonicalize_smiles"] = False
kwargs["reader_kwargs"] = reader_kwargs
self.augment_smiles = bool(augment_smiles)
self.aug_smiles_variations = aug_smiles_variations
# predict only single class (given as id of one of the classes present in the raw data set)
self.single_class = single_class
self.subset = subset
super(_ChEBIDataExtractor, self).__init__(**kwargs)
# use different version of chebi for training and validation (if not None)
# (still uses self.chebi_version for test set)
self.chebi_version_train = chebi_version_train
if self.chebi_version_train is not None:
# Instantiate another same class with "chebi_version" as "chebi_version_train", if train_version is given
# This is to get the data from respective directory related to "chebi_version_train"
_init_kwargs = kwargs
_init_kwargs["chebi_version"] = self.chebi_version_train
self._chebi_version_train_obj = self.__class__(
single_class=self.single_class,
augment_smiles=self.augment_smiles,
aug_smiles_variations=self.aug_smiles_variations,
**_init_kwargs,
)
# ------------------------------ Phase: Prepare data -----------------------------------
def _perform_data_preparation(self, *args: Any, **kwargs: Any) -> None:
"""
Prepares the data for the Chebi dataset.
This method checks for the presence of raw data in the specified directory.
If the raw data is missing, it fetches the ontology and creates a dataframe & saves it as data.pkl pickle file.
The resulting dataframe/pickle file is expected to contain columns with the following structure:
- Column at index `self._ID_IDX`: ID of chebi data instance
- Column at index `self._DATA_REPRESENTATION_IDX`: SMILES representation of the chemical
- Column from index `self._LABELS_START_IDX` onwards: Labels
It will pre-process the data related to `chebi_version_train`, if specified.
Args:
*args: Variable length argument list.
**kwargs: Arbitrary keyword arguments.
Returns:
None
"""
super()._perform_data_preparation(args, kwargs)
if self.chebi_version_train is not None:
if not os.path.isfile(
os.path.join(
self._chebi_version_train_obj.processed_dir_main,
self._chebi_version_train_obj.processed_main_file_names_dict[
"data"
],
)
):
print(
f"Missing processed data related to train version: {self.chebi_version_train}"
)
print("Calling the prepare_data method related to it")
# Generate the "chebi_version_train" data if it doesn't exist
self._chebi_version_train_obj.prepare_data(*args, **kwargs)
def _download_required_data(self) -> str:
"""
Downloads the required raw data related to chebi.
Returns:
str: Path to the downloaded data.
"""
return self._load_chebi(self.chebi_version)
def _load_chebi(self, version: int) -> str:
"""
Load the ChEBI ontology file.
Args:
version (int): The version of the ChEBI ontology to load.
Returns:
str: The file path of the loaded ChEBI ontology.
"""
import requests
chebi_name = self.raw_file_names_dict["chebi"]
chebi_path = os.path.join(self.raw_dir, chebi_name)
if not os.path.isfile(chebi_path):
print(
f"Missing raw chebi data related to version: v_{version}, Downloading..."
)
url = f"http://purl.obolibrary.org/obo/chebi/{version}/chebi.obo"
r = requests.get(url, allow_redirects=True)
open(chebi_path, "wb").write(r.content)
return chebi_path
def _extract_class_hierarchy(self, data_path: str) -> "nx.DiGraph":
"""
Extracts the class hierarchy from the ChEBI ontology.
Constructs a directed graph (DiGraph) using NetworkX, where nodes are annotated with fields/terms from
the chebi term documents from `.obo` file.
Args:
data_path (str): The path to the ChEBI ontology.
Returns:
nx.DiGraph: The class hierarchy.
"""
import fastobo
import networkx as nx
with open(data_path, encoding="utf-8") as chebi:
chebi = "\n".join(line for line in chebi if not line.startswith("xref:"))
elements = []
for term_doc in fastobo.loads(chebi):
if (
term_doc
and isinstance(term_doc.id, fastobo.id.PrefixedIdent)
and term_doc.id.prefix == "CHEBI"
):
term_dict = term_callback(term_doc)
if term_dict and (
not self.subset or term_dict["subset"] == self.subset
):
elements.append(term_dict)
g = nx.DiGraph()
for n in elements:
g.add_node(n["id"], **n)
# Only take the edges which connects the existing nodes, to avoid internal creation of obsolete nodes
# https://github.com/ChEB-AI/python-chebai/pull/55#issuecomment-2386654142
g.add_edges_from(
[(p, q["id"]) for q in elements for p in q["parents"] if g.has_node(p)]
)
print("Compute transitive closure")
return nx.transitive_closure_dag(g)
def _graph_to_raw_dataset(self, g: "nx.DiGraph") -> pd.DataFrame:
"""
Converts the graph to a raw dataset.
Uses the graph created by `_extract_class_hierarchy` method to extract the
raw data in Dataframe format with additional columns corresponding to each multi-label class.
Args:
g (nx.DiGraph): The class hierarchy graph.
Returns:
pd.DataFrame: The raw dataset created from the graph.
"""
import networkx as nx
smiles = nx.get_node_attributes(g, "smiles")
names = nx.get_node_attributes(g, "name")
print("Process graph")
molecules, smiles_list = zip(
*(
(n, smiles)
for n, smiles in ((n, smiles.get(n)) for n in smiles.keys())
if smiles
)
)
data = OrderedDict(id=molecules) # `id` column at index 0
data["name"] = [
names.get(node) for node in molecules
] # `name` column at index 1
data["SMILES"] = smiles_list # `SMILES` (data representation) column at index 2
# Labels columns from index 3 onwards
for n in self.select_classes(g):
data[n] = [
((n in g.predecessors(node)) or (n == node)) for node in molecules
]
data = pd.DataFrame(data)
data = data[~data["SMILES"].isnull()]
data = data[~data["name"].isin(CHEBI_BLACKLIST)]
return data
def _after_prepare_data(self, *args, **kwargs) -> None:
self._perform_smiles_augmentation()
def _perform_smiles_augmentation(self) -> None:
if not self.augment_smiles:
return
aug_pkl_file_name = self.processed_main_file_names_dict["aug_data"]
aug_data_df = self.get_processed_pickled_df_file(aug_pkl_file_name)
if aug_data_df is not None:
self._data_pkl_filename = aug_pkl_file_name
return
data_df = self.get_processed_pickled_df_file(
self.processed_main_file_names_dict["data"]
)
# +1 to account for if original SMILES is generated by random chance
AUG_SMILES_VARIATIONS = self.aug_smiles_variations + 1
def generate_augmented_smiles(smiles: str) -> list[str]:
mol: Chem.Mol = Chem.MolFromSmiles(smiles)
if mol is None:
return [smiles] # if mol is None, return original SMILES
# sanitization set to False, as it can alter the fragment representation in ways you might not want.
# As we don’t want RDKit to "fix" fragments, only need the fragments as-is, to generate SMILES strings.
frags = Chem.GetMolFrags(mol, asMols=True, sanitizeFrags=False)
augmented = set()
frag_smiles: list[set] = []
for frag in frags:
atom_ids = [atom.GetIdx() for atom in frag.GetAtoms()]
random.shuffle(atom_ids) # seed set by lightning
atom_id_iter = cycle(atom_ids)
frag_smiles.append(
{
Chem.MolToSmiles(
frag, rootedAtAtom=next(atom_id_iter), doRandom=True
)
for _ in range(AUG_SMILES_VARIATIONS)
}
)
if len(frags) > 1:
# all permutations (ignoring the set order, meaning mixing sets in every order),
aug_counter: int = 0
for perm in permutations(frag_smiles):
for combo in product(*perm):
augmented.add(".".join(combo))
aug_counter += 1
if aug_counter >= AUG_SMILES_VARIATIONS:
break
if aug_counter >= AUG_SMILES_VARIATIONS:
break
else:
augmented = frag_smiles[0]
if smiles in augmented:
augmented.remove(smiles)
if len(augmented) > AUG_SMILES_VARIATIONS - 1:
# AUG_SMILES_VARIATIONS = number of new smiles needed to generate + original smiles
# if 3 smiles variations are needed, and 4 are generated because none
# correponds to original smiles and no-one is elimnated in previous if condition
augmented = random.sample(augmented, AUG_SMILES_VARIATIONS - 1)
# original smiles always first in the list
return [smiles] + list(augmented)
data_df["SMILES"] = data_df["SMILES"].apply(generate_augmented_smiles)
# Explode the list of augmented smiles into multiple rows
# augmented smiles will have same ident, as of the original, but does it matter ?
# instead its helpful to group augmented smiles generated from the same original SMILES
exploded_df = data_df.explode("SMILES").reset_index(drop=True)
self.save_processed(
exploded_df, self.processed_main_file_names_dict["aug_data"]
)
self._data_pkl_filename = aug_pkl_file_name
# ------------------------------ Phase: Setup data -----------------------------------
def setup_processed(self) -> None:
"""
Transform and prepare processed data for the ChEBI dataset.
Main function of this method is to transform `data.pkl` into a model input data format (`data.pt`),
ensuring that the data is in a format compatible for input to the model.
The transformed data must contain the following keys: `ident`, `features`, `labels`, and `group`.
This method uses a subclass of Data Reader to perform the transformation.
It will transform the data related to `chebi_version_train`, if specified.
"""
super().setup_processed()
# Transform the data related to "chebi_version_train" to encoded data, if it doesn't exist
if self.chebi_version_train is not None and not os.path.isfile(
os.path.join(
self._chebi_version_train_obj.processed_dir,
self._chebi_version_train_obj.processed_file_names_dict["data"],
)
):
print(
f"Missing encoded data related to train version: {self.chebi_version_train}"
)
print("Calling the setup method related to it")
self._chebi_version_train_obj.setup()
def _load_dict(self, input_file_path: str) -> Generator[dict[str, Any], None, None]:
"""
Loads a dictionary from a pickled file, yielding individual dictionaries for each row.
This method reads data from a specified pickled file, processes each row to extract relevant
information, and yields dictionaries containing the keys `features`, `labels`, and `ident`.
If `single_class` is specified, it only includes the label for that specific class; otherwise,
it includes labels for all classes starting from the fourth column.
The pickled file is expected to contain rows with the following structure:
- Data at row index `self._ID_IDX`: ID of the chebi data instance
- Data at row index `self._DATA_REPRESENTATION_IDX` : SMILES representation for the chemical
- Data from row index `self._LABELS_START_IDX` onwards: Labels
This method is used in `_load_data_from_file` to process each row of data and convert it
into the desired dictionary format before loading it into the model.
Args:
input_file_path (str): The path to the input pickled file.
Yields:
Dict[str, Any]: A dictionary with keys `features`, `labels`, and `ident`.
`features` contains the sequence, `labels` contains the labels as boolean values,
and `ident` contains the identifier.
"""
with open(input_file_path, "rb") as input_file:
df = pd.read_pickle(input_file)
if self.single_class is None:
all_labels = df.iloc[:, self._LABELS_START_IDX :].to_numpy(dtype=bool)
else:
single_cls_index = df.columns.get_loc(int(self.single_class))
all_labels = df.iloc[:, [single_cls_index]].to_numpy(dtype=bool)
features = df.iloc[:, self._DATA_REPRESENTATION_IDX].to_numpy()
idents = df.iloc[:, self._ID_IDX].to_numpy()
for feat, labels, ident in zip(features, all_labels, idents):
yield dict(features=feat, labels=labels, ident=ident)
# ------------------------------ Phase: Dynamic Splits -----------------------------------
def _get_data_splits(self) -> tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame]:
"""
Loads encoded/transformed data and generates training, validation, and test splits.
This method first loads encoded data from a file named `data.pt`, which is derived from either
`chebi_version` or `chebi_version_train`. It then splits the data into training, validation, and test sets.
If `chebi_version_train` is provided:
- Loads additional encoded data from `chebi_version_train`.
- Splits this data into training and validation sets, while using the test set from `chebi_version`.
- Prunes the test set from `chebi_version` to include only labels that exist in `chebi_version_train`.
If `chebi_version_train` is not provided:
- Splits the data from `chebi_version` into training, validation, and test sets without modification.
Raises:
FileNotFoundError: If the required `data.pt` file(s) do not exist. Ensure that `prepare_data`
and/or `setup` methods have been called to generate the dataset files.
Returns:
Tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame]: A tuple containing three DataFrames:
- Training set
- Validation set
- Test set
"""
try:
filename = self.processed_file_names_dict["data"]
data_chebi_version = self.load_processed_data_from_file(filename)
except FileNotFoundError:
raise FileNotFoundError(
"File data.pt doesn't exists. "
"Please call 'prepare_data' and/or 'setup' methods to generate the dataset files"
)
df_chebi_version = pd.DataFrame(data_chebi_version)
train_df_chebi_ver, df_test_chebi_ver = self.get_test_split(
df_chebi_version, seed=self.dynamic_data_split_seed
)
if self.chebi_version_train is not None:
# Load encoded data derived from "chebi_version_train"
try:
filename_train = (
self._chebi_version_train_obj.processed_file_names_dict["data"]
)
data_chebi_train_version = torch.load(
os.path.join(
self._chebi_version_train_obj.processed_dir, filename_train
),
weights_only=False,
)
except FileNotFoundError:
raise FileNotFoundError(
f"File data.pt doesn't exists related to chebi_version_train {self.chebi_version_train}."
f"Please call 'prepare_data' and/or 'setup' methods to generate the dataset files"
)
df_chebi_train_version = pd.DataFrame(data_chebi_train_version)
# Get train/val split of data based on "chebi_version_train", but
# using test set from "chebi_version"
df_train, df_val = self.get_train_val_splits_given_test(
df_chebi_train_version,
df_test_chebi_ver,
seed=self.dynamic_data_split_seed,
)
# Modify test set from "chebi_version" to only include the labels that
# exists in "chebi_version_train", all other entries remains same.
df_test = self._setup_pruned_test_set(df_test_chebi_ver)
else:
# Get all splits based on "chebi_version"
df_train, df_val = self.get_train_val_splits_given_test(
train_df_chebi_ver,
df_test_chebi_ver,
seed=self.dynamic_data_split_seed,
)
df_test = df_test_chebi_ver
return df_train, df_val, df_test
def _setup_pruned_test_set(
self, df_test_chebi_version: pd.DataFrame
) -> pd.DataFrame:
"""
Create a test set with the same leaf nodes, but use only classes that appear in the training set.
Args:
df_test_chebi_version (pd.DataFrame): The test dataset.
Returns:
pd.DataFrame: The pruned test dataset.
"""
classes_file_name = "classes.txt"
# Load original and new classes
with open(os.path.join(self.processed_dir_main, classes_file_name), "r") as f:
orig_classes = f.readlines()
with open(
os.path.join(
self._chebi_version_train_obj.processed_dir_main, classes_file_name
),
"r",
) as f:
new_classes = f.readlines()
# Mapping array (-1 means no match in new classes)
mapping_array = np.array(
[
-1 if oc not in new_classes else new_classes.index(oc)
for oc in orig_classes
],
dtype=int,
)
# Convert labels column to 2D NumPy array
labels_matrix = np.array(df_test_chebi_version["labels"].tolist(), dtype=bool)
# Allocate new labels matrix
num_new_classes = len(new_classes)
new_labels_matrix = np.zeros(
(labels_matrix.shape[0], num_new_classes), dtype=bool
)
# Copy only valid columns
valid_mask = mapping_array != -1
new_labels_matrix[:, mapping_array[valid_mask]] = labels_matrix[:, valid_mask]
# Assign back
df_test_chebi_version["labels"] = new_labels_matrix.tolist()
return df_test_chebi_version
# ------------------------------ Phase: Raw Properties -----------------------------------
@property
def base_dir(self) -> str:
"""
Return the base directory path for data.
Returns:
str: The base directory path for data.
"""
return os.path.join("data", f"chebi_v{self.chebi_version}")
@property
def processed_dir_main(self) -> str:
"""
Returns the main directory path where processed data is stored.
Returns:
str: The path to the main processed data directory, based on the base directory and the instance's name.
"""
return os.path.join(
self.base_dir,
self._name if self.subset is None else f"{self._name}_{self.subset}",
"processed",
)
@property
def processed_dir(self) -> str:
"""
Return the directory path for processed data.
Returns:
str: The path to the processed data directory.
"""
res = os.path.join(
self.processed_dir_main,
*self.identifier,
)
if self.single_class is None:
return res
else:
return os.path.join(res, f"single_{self.single_class}")
@property
def raw_file_names_dict(self) -> dict:
return {"chebi": "chebi.obo"}
@property
def processed_main_file_names_dict(self) -> dict:
"""
Returns a dictionary mapping processed data file names.
Returns:
dict: A dictionary mapping dataset key to their respective file names.
For example, {"data": "data.pkl"}.
"""
p_dict = super().processed_main_file_names_dict
if self.augment_smiles:
p_dict["aug_data"] = f"aug_data_var{self.aug_smiles_variations}.pkl"
return p_dict
@property
def processed_file_names_dict(self) -> dict:
"""
Returns a dictionary for the processed and tokenized data files.
Returns:
dict: A dictionary mapping dataset keys to their respective file names.
For example, {"data": "data.pt"}.
"""
if not self.augment_smiles:
return super().processed_file_names_dict
if self.n_token_limit is not None:
return {
"data": f"aug_data_var{self.aug_smiles_variations}_maxlen{self.n_token_limit}.pt"
}
return {"data": f"aug_data_var{self.aug_smiles_variations}.pt"}
class JCIExtendedBase(_ChEBIDataExtractor):
@property
def _name(self):
return "JCI_extended"
def select_classes(self, g, *args, **kwargs):
return JCI_500_COLUMNS_INT
class ChEBIOverX(_ChEBIDataExtractor):
"""
A class for extracting data from the ChEBI dataset with a threshold for selecting classes.
This class is designed to filter Chebi classes based on a specified threshold, selecting only those classes
which have a certain number of subclasses in the hierarchy.
Attributes:
LABEL_INDEX (int): The index of the label in the dataset.
SMILES_INDEX (int): The index of the SMILES string in the dataset.
READER (ChemDataReader): The reader used for reading the dataset.
THRESHOLD (None): The threshold for selecting classes.
"""
READER: dr.ChemDataReader = dr.ChemDataReader
THRESHOLD: int = None
@property
def _name(self) -> str:
"""
Returns the name of the dataset.
Returns:
str: The dataset name.
"""
return f"ChEBI{self.THRESHOLD}"
def select_classes(self, g: "nx.DiGraph", *args, **kwargs) -> List:
"""
Selects classes from the ChEBI dataset based on the number of successors meeting a specified threshold.
This method iterates over the nodes in the graph, counting the number of successors for each node.
Nodes with a number of successors greater than or equal to the defined threshold are selected.
Note:
The input graph must be transitive closure of a directed acyclic graph.
Args:
g (nx.Graph): The graph representing the dataset.
*args: Additional positional arguments (not used).
**kwargs: Additional keyword arguments (not used).
Returns:
List: A sorted list of node IDs that meet the successor threshold criteria.
Side Effects:
Writes the list of selected nodes to a file named "classes.txt" in the specified processed directory.
Notes:
- The `THRESHOLD` attribute should be defined in the subclass of this class.
- Nodes without a 'smiles' attribute are ignored in the successor count.
"""
import networkx as nx
smiles = nx.get_node_attributes(g, "smiles")
nodes = list(
sorted(
{
node
for node in g.nodes
if sum(
1 if smiles[s] is not None else 0 for s in g.successors(node)
)
>= self.THRESHOLD
}
)
)
filename = "classes.txt"
with open(os.path.join(self.processed_dir_main, filename), "wt") as fout:
fout.writelines(str(node) + "\n" for node in nodes)
return nodes
class ChEBIOverXDeepSMILES(ChEBIOverX):
"""
A class for extracting data from the ChEBI dataset with DeepChem SMILES reader.
Inherits from ChEBIOverX.
Attributes:
READER (DeepChemDataReader): The reader used for reading the dataset (DeepChem SMILES).
"""
READER: dr.DeepChemDataReader = dr.DeepChemDataReader
class ChEBIOverXSELFIES(ChEBIOverX):
"""
A class for extracting data from the ChEBI dataset with SELFIES reader.
Inherits from ChEBIOverX.
Attributes:
READER (SelfiesReader): The reader used for reading the dataset (SELFIES).
"""
READER: dr.SelfiesReader = dr.SelfiesReader
class ChEBIOver100(ChEBIOverX):
"""
A class for extracting data from the ChEBI dataset with a threshold of 100 for selecting classes.
Inherits from ChEBIOverX.
Attributes:
THRESHOLD (int): The threshold for selecting classes (100).
"""
THRESHOLD: int = 100
class ChEBIOver50(ChEBIOverX):
"""
A class for extracting data from the ChEBI dataset with a threshold of 50 for selecting classes.
Inherits from ChEBIOverX.
Attributes:
THRESHOLD (int): The threshold for selecting classes (50).
"""
THRESHOLD: int = 50
class ChEBIOver100DeepSMILES(ChEBIOverXDeepSMILES, ChEBIOver100):
"""
A class for extracting data from the ChEBI dataset with DeepChem SMILES reader and a threshold of 100.
Inherits from ChEBIOverXDeepSMILES and ChEBIOver100.
"""
pass
class ChEBIOver100SELFIES(ChEBIOverXSELFIES, ChEBIOver100):
"""
A class for extracting data from the ChEBI dataset with SELFIES reader and a threshold of 100.
Inherits from ChEBIOverXSELFIES and ChEBIOver100.
"""
pass
class ChEBIOver50SELFIES(ChEBIOverXSELFIES, ChEBIOver50):
pass
class ChEBIOverXPartial(ChEBIOverX):
"""
Dataset that doesn't use the full ChEBI, but extracts a part of ChEBI (subclasses of a given top class)
Attributes:
top_class_id (int): The ID of the top class from which to extract subclasses.
"""
def __init__(self, top_class_id: int, external_data_ratio: float, **kwargs):
"""
Initializes the ChEBIOverXPartial dataset.
Args:
top_class_id (int): The ID of the top class from which to extract subclasses.
**kwargs: Additional keyword arguments passed to the superclass initializer.
external_data_ratio (float): How much external data (i.e., samples where top_class_id
is no positive label) to include in the dataset. 0 means no external data, 1 means
the maximum amount (i.e., the complete ChEBI dataset).
"""
if "top_class_id" not in kwargs:
kwargs["top_class_id"] = top_class_id
if "external_data_ratio" not in kwargs:
kwargs["external_data_ratio"] = external_data_ratio
self.top_class_id: int = top_class_id
self.external_data_ratio: float = external_data_ratio
super().__init__(**kwargs)
@property
def processed_dir_main(self) -> str:
"""
Returns the main processed data directory specific to the top class.
Returns:
str: The processed data directory path.
"""
return os.path.join(
self.base_dir,
self._name,
f"partial_{self.top_class_id}_ext_ratio_{self.external_data_ratio:.2f}",
"processed",
)
def _extract_class_hierarchy(self, chebi_path: str) -> "nx.DiGraph":
"""
Extracts a subset of ChEBI based on subclasses of the top class ID.
This method calls the superclass method to extract the full class hierarchy,
then extracts the subgraph containing only the descendants of the top class ID, including itself.
Args:
chebi_path (str): The file path to the ChEBI ontology file.
Returns:
nx.DiGraph: The extracted class hierarchy as a directed graph, limited to the
descendants of the top class ID.
"""
g = super()._extract_class_hierarchy(chebi_path)
top_class_successors = list(g.successors(self.top_class_id)) + [
self.top_class_id
]
external_nodes = list(set(n for n in g.nodes if n not in top_class_successors))
if 0 < self.external_data_ratio < 1:
n_external_nodes = int(
len(top_class_successors)
* self.external_data_ratio
/ (1 - self.external_data_ratio)
)
print(
f"Extracting {n_external_nodes} external nodes from the ChEBI dataset (ratio: {self.external_data_ratio:.2f})"
)
external_nodes = external_nodes[: int(n_external_nodes)]
elif self.external_data_ratio == 0:
external_nodes = []
g = g.subgraph(top_class_successors + external_nodes)
print(
f"Subgraph contains {len(g.nodes)} nodes, of which {len(top_class_successors)} are subclasses of the top class ID {self.top_class_id}."
)
return g
def select_classes(self, g: "nx.DiGraph", *args, **kwargs) -> List:
"""Only selects classes that meet the threshold AND are subclasses of the top class ID (including itself)."""
import networkx as nx
smiles = nx.get_node_attributes(g, "smiles")
nodes = list(
sorted(
{
node
for node in g.nodes
if sum(
1 if smiles[s] is not None else 0 for s in g.successors(node)
)
>= self.THRESHOLD
and (
self.top_class_id in g.predecessors(node)
or node == self.top_class_id
)
}
)
)
filename = "classes.txt"
with open(os.path.join(self.processed_dir_main, filename), "wt") as fout:
fout.writelines(str(node) + "\n" for node in nodes)
return nodes
class ChEBIOver50Partial(ChEBIOverXPartial, ChEBIOver50):
"""
Dataset that extracts a part of ChEBI based on subclasses of a given top class,
with a threshold of 50 for selecting classes.
Inherits from ChEBIOverXPartial and ChEBIOver50.
"""
pass
class ChEBIOverXFingerprints(ChEBIOverX):
"""A class that uses Fingerprints for the processed data (used for fixed-length ML models)."""
READER = dr.FingerprintReader
class ChEBIOver100Fingerprints(ChEBIOverXFingerprints, ChEBIOver100):
"""
A class for extracting data from the ChEBI dataset with Fingerprints reader and a threshold of 100.
Inherits from ChEBIOverXFingerprints and ChEBIOver100.
"""
pass
class JCIExtendedBPEData(JCIExtendedBase):
READER = dr.ChemBPEReader
class JCIExtendedTokenData(JCIExtendedBase):
READER = dr.ChemDataReader
class JCIExtSelfies(JCIExtendedBase):