1
|
|
|
import dataclasses |
|
|
|
|
2
|
|
|
from dataclasses import dataclass |
3
|
|
|
from typing import Optional, Sequence |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
@dataclass(frozen=True, repr=True, order=True) |
7
|
|
|
class Triple: |
8
|
|
|
""" |
9
|
|
|
Compound, predicate, object. |
10
|
|
|
""" |
11
|
|
|
|
12
|
|
|
compound_id: str |
13
|
|
|
compound_lookup: str |
14
|
|
|
compound_name: str |
15
|
|
|
predicate: str |
16
|
|
|
object_name: str |
17
|
|
|
object_id: str |
18
|
|
|
|
19
|
|
|
@classmethod |
20
|
|
|
def tab_header(cls) -> str: |
21
|
|
|
""" |
22
|
|
|
|
23
|
|
|
Returns: |
24
|
|
|
|
25
|
|
|
""" |
26
|
|
|
return "\t".join( |
27
|
|
|
[ |
28
|
|
|
"compound_id", |
29
|
|
|
"compound_lookup", |
30
|
|
|
"compound_name", |
31
|
|
|
"predicate", |
32
|
|
|
"object_name", |
33
|
|
|
"object_id", |
34
|
|
|
] |
35
|
|
|
) |
36
|
|
|
|
37
|
|
|
@property |
38
|
|
|
def tabs(self) -> str: |
|
|
|
|
39
|
|
|
items = [ |
40
|
|
|
self.compound_lookup, |
41
|
|
|
self.compound_id, |
42
|
|
|
self.compound_name, |
43
|
|
|
self.predicate, |
44
|
|
|
self.object_name, |
45
|
|
|
self.object_id, |
46
|
|
|
] |
47
|
|
|
return "\t".join(["-" if k is None else str(k) for k in items]) |
48
|
|
|
|
49
|
|
|
@property |
50
|
|
|
def statement(self) -> str: |
51
|
|
|
""" |
52
|
|
|
Returns a simple text statement with brackets. |
53
|
|
|
|
54
|
|
|
Returns: |
55
|
|
|
|
56
|
|
|
""" |
57
|
|
|
sub = f"{self.compound_lookup} [{self.compound_id}] [{self.compound_name}]>" |
58
|
|
|
pred = f"<{self.predicate}>" |
59
|
|
|
obj = f"<{self.object_name} [{self.object_id}]>" |
60
|
|
|
return "\t".join([sub, pred, obj]) |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
@dataclass(frozen=True, order=True, repr=True) |
|
|
|
|
64
|
|
|
class AbstractHit: |
65
|
|
|
"""""" |
66
|
|
|
|
67
|
|
|
record_id: Optional[str] |
68
|
|
|
compound_id: str |
69
|
|
|
inchikey: str |
70
|
|
|
compound_lookup: str |
71
|
|
|
compound_name: str |
72
|
|
|
object_id: str |
73
|
|
|
object_name: str |
74
|
|
|
|
75
|
|
|
def to_triple(self) -> Triple: |
|
|
|
|
76
|
|
|
return Triple( |
77
|
|
|
compound_lookup=self.compound_lookup, |
78
|
|
|
compound_id=self.compound_id, |
79
|
|
|
compound_name=self.compound_name, |
80
|
|
|
predicate=self.predicate, |
81
|
|
|
object_id=self.object_id, |
82
|
|
|
object_name=self.object_name, |
83
|
|
|
) |
84
|
|
|
|
85
|
|
|
@property |
86
|
|
|
def predicate(self) -> str: |
87
|
|
|
""" |
88
|
|
|
|
89
|
|
|
Returns: |
90
|
|
|
|
91
|
|
|
""" |
92
|
|
|
raise NotImplementedError() |
93
|
|
|
|
94
|
|
|
def __hash__(self): |
95
|
|
|
return hash(self.record_id) |
96
|
|
|
|
97
|
|
|
@classmethod |
98
|
|
|
def fields(cls) -> Sequence[str]: |
99
|
|
|
""" |
100
|
|
|
|
101
|
|
|
Returns: |
102
|
|
|
|
103
|
|
|
""" |
104
|
|
|
return [f.name for f in dataclasses.fields(cls)] |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
__all__ = ["AbstractHit", "Triple"] |
108
|
|
|
|