1
|
|
|
from __future__ import annotations |
|
|
|
|
2
|
|
|
import enum |
3
|
|
|
from typing import Set, Mapping, Optional, Union, List, Dict |
|
|
|
|
4
|
|
|
|
5
|
|
|
from loguru import logger |
|
|
|
|
6
|
|
|
from mandos.model import CleverEnum |
7
|
|
|
|
8
|
|
|
from mandos.entries.api_singletons import Apis |
9
|
|
|
from mandos.entries.searcher import IdMatchFrame, ChemFinder |
10
|
|
|
from mandos.model.apis.chembl_support.chembl_utils import ChemblUtils |
11
|
|
|
from mandos.model.apis.pubchem_api import PubchemCompoundLookupError |
12
|
|
|
from mandos.model.apis.pubchem_support.pubchem_data import PubchemData |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
PUT_FIRST = [ |
16
|
|
|
"compound_id", |
17
|
|
|
"library", |
18
|
|
|
"inchikey", |
19
|
|
|
"chembl_id", |
20
|
|
|
"pubchem_id", |
21
|
|
|
"g2p_id", |
22
|
|
|
"common_name", |
23
|
|
|
] |
24
|
|
|
PUT_LAST = ["inchi", "smiles" "iupac", "origin_inchikey", "origin_inchi", "origin_smiles"] |
|
|
|
|
25
|
|
|
|
26
|
|
|
|
27
|
|
|
class IdType(CleverEnum): |
|
|
|
|
28
|
|
|
inchikey = enum.auto() |
29
|
|
|
chembl_id = enum.auto() |
30
|
|
|
pubchem_id = enum.auto() |
31
|
|
|
# g2p_id = enum.auto() |
32
|
|
|
common_name = enum.auto() |
33
|
|
|
iupac = enum.auto() |
34
|
|
|
inchi = enum.auto() |
35
|
|
|
smiles = enum.auto() |
36
|
|
|
|
37
|
|
|
@classmethod |
38
|
|
|
def parse(cls, fill: str) -> Set[IdType]: |
|
|
|
|
39
|
|
|
if fill == "@all": |
|
|
|
|
40
|
|
|
return set(IdType) |
41
|
|
|
elif fill == "@primary": |
42
|
|
|
return IdType.primary() |
43
|
|
|
else: |
44
|
|
|
return {IdType.of(s.strip().lower()) for s in fill.split(",")} |
45
|
|
|
|
46
|
|
|
@property |
47
|
|
|
def is_primary(self) -> bool: |
|
|
|
|
48
|
|
|
return self in self.__class__.primary() |
49
|
|
|
|
50
|
|
|
@classmethod |
51
|
|
|
def primary(cls) -> Set[IdType]: |
|
|
|
|
52
|
|
|
# in order from best to worst |
53
|
|
|
return {IdType.inchikey, IdType.chembl_id, IdType.pubchem_id} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
class CompoundIdFiller: |
|
|
|
|
57
|
|
|
def __init__(self, wanted: Set[Union[str, IdType]] = None, replace: bool = False): |
58
|
|
|
self.wanted = [IdType.of(s) for s in wanted] |
59
|
|
|
self.replace = replace |
60
|
|
|
|
61
|
|
|
def fill( |
|
|
|
|
62
|
|
|
self, |
|
|
|
|
63
|
|
|
df: IdMatchFrame, |
|
|
|
|
64
|
|
|
) -> IdMatchFrame: |
65
|
|
|
df = df.copy() |
66
|
|
|
df = df.dropna(how="all", axis=1) |
67
|
|
|
sources: Set[IdType] = {s for s in IdType.primary() if s.name in df.columns} |
68
|
|
|
targets: Set[IdType] = {s for s in self.wanted if s.name not in df.columns or self.replace} |
69
|
|
|
if len(sources) == 0: |
70
|
|
|
raise ValueError(f"No valid sources in list {df.columns.values}") |
71
|
|
|
source = next(iter(sources)) |
72
|
|
|
# noinspection PyUnresolvedReferences |
73
|
|
|
logger.notice(f"Getting {', '.join([s.name for s in targets])} from {source.name}") |
74
|
|
|
# watch out! these are simply in order, nothing more |
75
|
|
|
remapped: Dict[IdType, List[str]] = {t: [] for t in IdType} |
76
|
|
|
for i, source_val in enumerate(df[source.name].values): |
77
|
|
|
if source_val is None: |
78
|
|
|
raise AssertionError() |
79
|
|
|
matches: Dict[IdType, str] = self._matches(source, source_val, targets) |
80
|
|
|
for target, target_val in matches.items(): |
81
|
|
|
remapped[target].append(target_val) |
82
|
|
|
logger.info(f"Processed {source_val} ({i} of {len(df)}") |
83
|
|
|
if i % 20 == 0 and i > 0: |
84
|
|
|
logger.notice(f"Processed {i} / {len(df)}") |
85
|
|
|
for target in targets: |
86
|
|
|
rx = remapped[target] |
|
|
|
|
87
|
|
|
df[target.name] = rx |
88
|
|
|
order = [o for o in PUT_FIRST if o in df.columns] |
89
|
|
|
order += [c for c in df.columns if c not in PUT_FIRST and c not in PUT_LAST] |
90
|
|
|
order += [o for o in PUT_LAST if o in df.columns] |
91
|
|
|
df = df.cfirst(order) |
92
|
|
|
return df |
93
|
|
|
|
94
|
|
|
def _matches(self, source: IdType, source_val: str, targets: Set[IdType]) -> Dict[IdType, str]: |
95
|
|
|
if source is IdType.pubchem_id: |
96
|
|
|
inchikey = Apis.Pubchem.find_inchikey(int(source_val)) |
97
|
|
|
elif source is IdType.chembl_id: |
98
|
|
|
# TODO |
|
|
|
|
99
|
|
|
# get_compound wants an inchikey, |
100
|
|
|
# but we're secretly passing a CHEMBLxxxx ID instead |
101
|
|
|
# we just know that that works |
102
|
|
|
inchikey = ChemblUtils(Apis.Chembl).get_compound(source_val).inchikey |
103
|
|
|
elif source is IdType.inchikey: |
104
|
|
|
inchikey = source |
105
|
|
|
else: |
106
|
|
|
raise AssertionError(source.name) |
107
|
|
|
matched: Dict[IdType, str] = {k: None for k in self.wanted} |
108
|
|
|
matched[IdType.inchikey] = inchikey |
109
|
|
|
if IdType.pubchem_id in targets: |
110
|
|
|
try: |
111
|
|
|
pubchem_data: Optional[PubchemData] = Apis.Pubchem.fetch_data(inchikey) |
112
|
|
|
except PubchemCompoundLookupError: |
113
|
|
|
pubchem_data = None |
114
|
|
|
if pubchem_data is not None: |
115
|
|
|
matched[IdType.pubchem_id] = str(pubchem_data.cid) |
116
|
|
|
if IdType.common_name in targets: |
117
|
|
|
matched[IdType.common_name] = pubchem_data.name |
118
|
|
|
if IdType.iupac in targets: |
119
|
|
|
matched[IdType.iupac] = pubchem_data.names_and_identifiers.iupac |
120
|
|
|
if IdType.smiles in targets: |
121
|
|
|
matched[IdType.smiles] = pubchem_data.names_and_identifiers.isomeric_smiles |
122
|
|
|
if IdType.inchi in targets: |
123
|
|
|
matched[IdType.inchi] = pubchem_data.names_and_identifiers.inchi |
124
|
|
|
if IdType.chembl_id in targets: |
125
|
|
|
chembl_id = ChemFinder.chembl().find(inchikey) |
126
|
|
|
if chembl_id is not None: |
127
|
|
|
matched[IdType.chembl_id] = chembl_id |
128
|
|
|
return matched |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
__all__ = ["IdType", "CompoundIdFiller"] |
132
|
|
|
|