1
|
|
|
from typing import Sequence |
|
|
|
|
2
|
|
|
|
3
|
|
|
import numpy as np |
|
|
|
|
4
|
|
|
import pandas as pd |
|
|
|
|
5
|
|
|
from typeddfs import TypedDfs |
|
|
|
|
6
|
|
|
from typeddfs.abs_dfs import AbsDf |
|
|
|
|
7
|
|
|
|
8
|
|
|
from mandos.model.concrete_hits import HIT_CLASSES |
9
|
|
|
from mandos.model.hits import AbstractHit |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
def _from_hits(cls, hits: Sequence[AbstractHit]) -> AbsDf: |
13
|
|
|
data = [] |
14
|
|
|
for hit in hits: |
15
|
|
|
x = {f: getattr(hit, f) for f in hit.__class__.fields()} |
|
|
|
|
16
|
|
|
x["universal_id"] = hit.universal_id |
17
|
|
|
x["hit_class"] = hit.hit_class |
18
|
|
|
data.append(x) |
19
|
|
|
return cls([pd.Series(x) for x in data]) |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
def _to_hits(self: AbsDf) -> Sequence[AbstractHit]: |
23
|
|
|
hits = [] |
24
|
|
|
for row in self.itertuples(): |
25
|
|
|
clazz = HIT_CLASSES[row.hit_class] |
26
|
|
|
# ignore extra columns |
27
|
|
|
# if cols are missing, let it fail on clazz.__init__ |
28
|
|
|
data = {f: getattr(row, f) for f in clazz.fields()} |
29
|
|
|
# noinspection PyArgumentList |
30
|
|
|
hit = clazz(**data) |
31
|
|
|
hits.append(hit) |
32
|
|
|
return hits |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
HitDf = ( |
36
|
|
|
TypedDfs.typed("HitDf") |
37
|
|
|
.require("record_id", dtype=str) |
38
|
|
|
.require("origin_inchikey", "matched_inchikey", dtype=str) |
39
|
|
|
.require("predicate", dtype=str) |
40
|
|
|
.require("object_id", "object_name", dtype=str) |
41
|
|
|
.require("search_key", "search_class", "data_source", dtype=str) |
42
|
|
|
.require("hit_class", dtype=str) |
43
|
|
|
.require("cache_date", "run_date") |
44
|
|
|
.reserve("inchi", "smiles", dtype=str) |
45
|
|
|
.reserve("compound_id", "compound_name", dtype=str) |
46
|
|
|
.reserve("chembl_id", "pubchem_id", dtype=str) |
47
|
|
|
.reserve("weight", dtype=np.float64) |
48
|
|
|
.add_classmethods(from_hits=_from_hits) |
49
|
|
|
.add_methods(to_hits=_to_hits) |
50
|
|
|
.strict(cols=False) |
51
|
|
|
.secure() |
52
|
|
|
).build() |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
__all__ = ["HitDf"] |
56
|
|
|
|