1
|
|
|
from typing import Sequence |
|
|
|
|
2
|
|
|
|
3
|
|
|
import numpy as np |
|
|
|
|
4
|
|
|
import pandas as pd |
|
|
|
|
5
|
|
|
from pocketutils.core.exceptions import InjectionError |
|
|
|
|
6
|
|
|
from typeddfs import TypedDfs |
|
|
|
|
7
|
|
|
from typeddfs.abs_dfs import AbsDf |
|
|
|
|
8
|
|
|
|
9
|
|
|
from mandos.model.concrete_hits import HIT_CLASSES |
10
|
|
|
from mandos.model.hits import AbstractHit |
11
|
|
|
from mandos.model.utils.setup import logger |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
def _from_hits(cls, hits: Sequence[AbstractHit]) -> AbsDf: |
15
|
|
|
data = [] |
16
|
|
|
if len(hits) == 0: |
17
|
|
|
logger.debug(f"No hits") |
|
|
|
|
18
|
|
|
return cls.new_df() |
19
|
|
|
for hit in hits: |
20
|
|
|
x = {f: getattr(hit, f) for f in hit.__class__.fields()} |
|
|
|
|
21
|
|
|
x["universal_id"] = hit.universal_id |
22
|
|
|
x["hit_class"] = hit.hit_class |
23
|
|
|
data.append(x) |
24
|
|
|
return cls.of([pd.Series(x) for x in data]) |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
def _to_hits(self: AbsDf) -> Sequence[AbstractHit]: |
28
|
|
|
hits = [] |
29
|
|
|
for row in self.itertuples(): |
30
|
|
|
# noinspection PyUnresolvedReferences |
31
|
|
|
c = row.hit_class |
|
|
|
|
32
|
|
|
# TODO: remove |
|
|
|
|
33
|
|
|
if c == "_DrugbankInteractionHit" and row.data_source == "drugbank:target-functions": |
34
|
|
|
c = "DrugbankGeneralFunctionHit" |
|
|
|
|
35
|
|
|
elif c == "_DrugbankInteractionHit" and row.data_source == "drugbank:targets": |
36
|
|
|
c = "DrugbankTargetHit" |
|
|
|
|
37
|
|
|
try: |
38
|
|
|
clazz = HIT_CLASSES[c] |
39
|
|
|
except KeyError: |
40
|
|
|
raise InjectionError(f"No hit class {c}") from None |
41
|
|
|
# ignore extra columns |
42
|
|
|
# if cols are missing, let it fail on clazz.__init__ |
43
|
|
|
data = {f: getattr(row, f) for f in clazz.fields()} |
44
|
|
|
try: |
45
|
|
|
# noinspection PyArgumentList |
46
|
|
|
hit = clazz(**data) |
47
|
|
|
except ValueError: |
48
|
|
|
logger.debug(f"Data passed to {clazz}: {data}") |
49
|
|
|
raise InjectionError( |
50
|
|
|
f"Fields for {c} do not match:" |
51
|
|
|
+ f" expected {', '.join(clazz.fields())};" |
52
|
|
|
+ f" got {', '.join(data.keys())}" |
53
|
|
|
) |
54
|
|
|
hits.append(hit) |
55
|
|
|
return hits |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
HitDf = ( |
59
|
|
|
TypedDfs.typed("HitDf") |
60
|
|
|
.require("record_id", dtype=str) |
61
|
|
|
.require("origin_inchikey", "matched_inchikey", dtype=str) |
62
|
|
|
.require("predicate", dtype=str) |
63
|
|
|
.require("object_id", "object_name", dtype=str) |
64
|
|
|
.require("search_key", "search_class", "data_source", dtype=str) |
65
|
|
|
.require("hit_class", dtype=str) |
66
|
|
|
.require("cache_date", "run_date") |
67
|
|
|
.reserve("inchi", "smiles", dtype=str) |
68
|
|
|
.reserve("compound_id", "compound_name", dtype=str) |
69
|
|
|
.reserve("chembl_id", "pubchem_id", dtype=str) |
70
|
|
|
.reserve("weight", dtype=np.float64) |
71
|
|
|
.add_classmethods(from_hits=_from_hits) |
72
|
|
|
.add_methods(to_hits=_to_hits) |
73
|
|
|
.strict(cols=False) |
74
|
|
|
.secure() |
75
|
|
|
).build() |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
__all__ = ["HitDf"] |
79
|
|
|
|