1
|
|
|
import abc |
|
|
|
|
2
|
|
|
from dataclasses import dataclass |
3
|
|
|
from typing import Sequence, TypeVar |
4
|
|
|
|
5
|
|
|
from mandos.model.pubchem_api import PubchemApi |
6
|
|
|
from mandos.model.pubchem_support.pubchem_data import PubchemData |
7
|
|
|
from mandos.model.pubchem_support.pubchem_models import CoOccurrenceType |
8
|
|
|
from mandos.search.pubchem import PubchemHit, PubchemSearch |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
@dataclass(frozen=True, order=True, repr=True) |
|
|
|
|
12
|
|
|
class CoOccurrenceHit(PubchemHit, metaclass=abc.ABCMeta): |
13
|
|
|
score: int |
14
|
|
|
intersect_count: int |
15
|
|
|
query_count: int |
16
|
|
|
neighbor_count: int |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
@dataclass(frozen=True, order=True, repr=True) |
|
|
|
|
20
|
|
|
class DiseaseCoOccurrenceHit(CoOccurrenceHit): |
21
|
|
|
"""""" |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
@dataclass(frozen=True, order=True, repr=True) |
|
|
|
|
25
|
|
|
class GeneCoOccurrenceHit(CoOccurrenceHit): |
26
|
|
|
"""""" |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
@dataclass(frozen=True, order=True, repr=True) |
|
|
|
|
30
|
|
|
class ChemicalCoOccurrenceHit(CoOccurrenceHit): |
31
|
|
|
"""""" |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
H = TypeVar("H", bound=CoOccurrenceHit, covariant=True) |
|
|
|
|
35
|
|
|
|
36
|
|
|
|
37
|
|
|
class CoOccurrenceSearch(PubchemSearch[H], metaclass=abc.ABCMeta): |
|
|
|
|
38
|
|
|
"""""" |
39
|
|
|
|
40
|
|
|
def __init__(self, key: str, api: PubchemApi, min_score: int, min_articles: int): |
41
|
|
|
super().__init__(key, api) |
42
|
|
|
self.min_score = min_score |
43
|
|
|
self.min_articles = min_articles |
44
|
|
|
|
45
|
|
|
@classmethod |
46
|
|
|
def cooccurrence_type(cls) -> CoOccurrenceType: |
|
|
|
|
47
|
|
|
raise NotImplementedError() |
48
|
|
|
|
49
|
|
|
@property |
50
|
|
|
def data_source(self) -> str: |
|
|
|
|
51
|
|
|
return "PubChem" |
52
|
|
|
|
53
|
|
|
def _predicate(self) -> str: |
54
|
|
|
raise NotImplementedError() |
55
|
|
|
|
56
|
|
|
def _query(self, data: PubchemData): |
57
|
|
|
raise NotImplementedError() |
58
|
|
|
|
59
|
|
|
def find(self, inchikey: str) -> Sequence[H]: |
|
|
|
|
60
|
|
|
data = self.api.fetch_data(inchikey) |
61
|
|
|
return [ |
62
|
|
|
self.__class__.get_h()( |
63
|
|
|
record_id=None, |
64
|
|
|
compound_id=str(data.cid), |
65
|
|
|
inchikey=data.names_and_identifiers.inchikey, |
66
|
|
|
compound_name=data.name, |
67
|
|
|
predicate=self._predicate(), |
68
|
|
|
object_id=dd.neighbor_id, |
69
|
|
|
object_name=dd.neighbor_name, |
70
|
|
|
score=dd.score, |
71
|
|
|
intersect_count=dd.article_count, |
72
|
|
|
query_count=dd.query_article_count, |
73
|
|
|
neighbor_count=dd.neighbor_article_count, |
74
|
|
|
search_key=self.key, |
75
|
|
|
search_class=self.search_class, |
76
|
|
|
data_source=self.data_source, |
77
|
|
|
) |
78
|
|
|
for dd in data.literature.gene_cooccurrences |
79
|
|
|
if ( |
80
|
|
|
dd.score >= self.min_score |
81
|
|
|
and dd.neighbor_article_count >= self.min_articles |
82
|
|
|
and dd.query_article_count >= self.min_articles |
83
|
|
|
) |
84
|
|
|
] |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
class GeneCoOccurrenceSearch(CoOccurrenceSearch[GeneCoOccurrenceHit]): |
|
|
|
|
88
|
|
|
@classmethod |
89
|
|
|
def cooccurrence_type(cls) -> CoOccurrenceType: |
|
|
|
|
90
|
|
|
return CoOccurrenceType.gene |
91
|
|
|
|
92
|
|
|
def _predicate(self) -> str: |
|
|
|
|
93
|
|
|
return "gene co-occurrence" |
94
|
|
|
|
95
|
|
|
def _query(self, data: PubchemData): |
|
|
|
|
96
|
|
|
return data.literature.gene_cooccurrences |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
class ChemicalCoOccurrenceSearch(CoOccurrenceSearch[ChemicalCoOccurrenceHit]): |
|
|
|
|
100
|
|
|
@classmethod |
101
|
|
|
def cooccurrence_type(cls) -> CoOccurrenceType: |
|
|
|
|
102
|
|
|
return CoOccurrenceType.chemical |
103
|
|
|
|
104
|
|
|
def _predicate(self) -> str: |
|
|
|
|
105
|
|
|
return "chemical co-occurrence" |
106
|
|
|
|
107
|
|
|
def _query(self, data: PubchemData): |
|
|
|
|
108
|
|
|
return data.literature.chemical_cooccurrences |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
class DiseaseCoOccurrenceSearch(CoOccurrenceSearch[DiseaseCoOccurrenceHit]): |
|
|
|
|
112
|
|
|
@classmethod |
113
|
|
|
def cooccurrence_type(cls) -> CoOccurrenceType: |
|
|
|
|
114
|
|
|
return CoOccurrenceType.disease |
115
|
|
|
|
116
|
|
|
def _predicate(self) -> str: |
|
|
|
|
117
|
|
|
return "disease co-occurrence" |
118
|
|
|
|
119
|
|
|
def _query(self, data: PubchemData): |
|
|
|
|
120
|
|
|
return data.literature.disease_cooccurrences |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
__all__ = [ |
124
|
|
|
"GeneCoOccurrenceHit", |
125
|
|
|
"GeneCoOccurrenceSearch", |
126
|
|
|
"ChemicalCoOccurrenceSearch", |
127
|
|
|
"ChemicalCoOccurrenceHit", |
128
|
|
|
"DiseaseCoOccurrenceSearch", |
129
|
|
|
"DiseaseCoOccurrenceHit", |
130
|
|
|
"CoOccurrenceSearch", |
131
|
|
|
] |
132
|
|
|
|