mandos.model.pubchem_support.Codes.value()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
from __future__ import annotations
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
import enum
3
import re
4
from dataclasses import dataclass
5
from datetime import date
6
from typing import Union, Optional, FrozenSet, Sequence
7
8
import pandas as pd
0 ignored issues
show
introduced by
Unable to import 'pandas'
Loading history...
9
10
from mandos import MandosResources
11
from mandos.model.query_utils import Fns
12
13
hazards = pd.read_csv(MandosResources.path("hazards.tab"), sep="\t")
14
hazards = dict(hazards.set_index("code").T.to_dict())
15
16
17
@dataclass(frozen=True, repr=True, eq=True, order=True)
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
18
class ComputedProperty:
19
    key: str
20
    value: Union[int, str, float, bool]
21
    unit: Optional[str]
22
    ref: str
23
24
    def req_is(self, type_) -> Union[int, str, float, bool]:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
25
        if not isinstance(self.value, type_):
26
            raise TypeError(f"{self.key}->{self.value} has {type(self.value)}, not {type_}")
27
        return self.value
28
29
    @property
30
    def as_str(self) -> str:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
31
        return f"{self.value} {self.unit}"
32
33
34
class Code(str):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
35
    @property
36
    def type_name(self) -> str:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
37
        return self.__class__.__name__.lower()
38
39
    @classmethod
40
    def of(cls, value: Union[str, int, float]):
0 ignored issues
show
Coding Style Naming introduced by
Method name "of" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
introduced by
Missing function or method docstring
Loading history...
41
        if isinstance(value, float):
42
            try:
43
                value = int(value)
44
            except ArithmeticError:
45
                value = str(value)
46
        value = str(value).strip()
47
        return cls(value)
48
49
50
class Codes:
51
    """
52
    These turn out to be extremely useful for documenting return types.
53
    For example, ``DrugbankInteraction`` might have a ``gene`` field,
54
    which can be described as a ``GenecardSymbol`` if known.
55
    """
56
57
    class EcNumber(Code):
58
        """
59
        e.g. 'EC:4.6.1.1'
60
        """
61
62
    class GeneId(Code):
63
        """
64
        GeneCard, UniProt gene name, etc.
65
        e.g. 'slc1a2'
66
        """
67
68
    class GenecardSymbol(GeneId):
0 ignored issues
show
Documentation introduced by
Empty class docstring
Loading history...
69
        """"""
70
71
    class UniprotId(GeneId):
0 ignored issues
show
Documentation introduced by
Empty class docstring
Loading history...
72
        """"""
73
74
    class PubchemCompoundId(Code):
75
        """
76
        e.g. 2352
77
        """
78
79
        @property
80
        def value(self) -> int:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
81
            return int(self)
82
83
    class AtcCode(Code):
0 ignored issues
show
Documentation introduced by
Empty class docstring
Loading history...
84
        """"""
85
86
    class PubmedId(Code):
0 ignored issues
show
Documentation introduced by
Empty class docstring
Loading history...
87
        """"""
88
89
    class Doi(Code):
0 ignored issues
show
Documentation introduced by
Empty class docstring
Loading history...
90
        """"""
91
92
    class MeshCode(Code):
0 ignored issues
show
Documentation introduced by
Empty class docstring
Loading history...
93
        """"""
94
95
    class PdbId(Code):
0 ignored issues
show
Documentation introduced by
Empty class docstring
Loading history...
96
        """"""
97
98
    class MeshHeading(Code):
0 ignored issues
show
Documentation introduced by
Empty class docstring
Loading history...
99
        """"""
100
101
    class MeshSubheading(Code):
0 ignored issues
show
Documentation introduced by
Empty class docstring
Loading history...
102
        """"""
103
104
    class DrugbankCompoundId(Code):
0 ignored issues
show
Documentation introduced by
Empty class docstring
Loading history...
105
        """"""
106
107
    class DeaSchedule(Code):
0 ignored issues
show
Documentation introduced by
Empty class docstring
Loading history...
108
        """"""
109
110
        @property
111
        def value(self) -> int:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
112
            return Fns.roman_to_arabic(1, 5)(self)
113
114
    class GhsCode(Code):
0 ignored issues
show
Documentation introduced by
Empty class docstring
Loading history...
115
        """"""
116
117
118
class CoOccurrenceType(enum.Enum):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
119
    chemical = enum.auto()
120
    gene = enum.auto()
121
    disease = enum.auto()
122
123
    @property
124
    def x_name(self) -> str:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
125
        if self is CoOccurrenceType.chemical:
0 ignored issues
show
unused-code introduced by
Unnecessary "elif" after "return"
Loading history...
126
            return "ChemicalNeighbor"
127
        elif self is CoOccurrenceType.gene:
128
            return "ChemicalGeneSymbolNeighbor"
129
        elif self is CoOccurrenceType.disease:
130
            return "ChemicalDiseaseNeighbor"
131
        raise AssertionError(f"{self} not found!!")
132
133
    @property
134
    def id_name(self) -> str:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
135
        if self is CoOccurrenceType.chemical:
0 ignored issues
show
unused-code introduced by
Unnecessary "elif" after "return"
Loading history...
136
            return "CID"
137
        elif self is CoOccurrenceType.gene:
138
            return "GeneSymbol"
139
        elif self is CoOccurrenceType.disease:
140
            return "MeSH"
141
        raise AssertionError(f"{self} not found!!")
142
143
144
@dataclass(frozen=True, repr=True, eq=True)
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
145
class ClinicalTrial:
146
    title: str
147
    conditions: FrozenSet[str]
148
    phase: str
149
    status: str
150
    interventions: FrozenSet[str]
151
    cids: FrozenSet[Codes.PubchemCompoundId]
152
    source: str
153
154
    @property
155
    def known_phase(self) -> int:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
156
        return {
157
            "Phase 4": 4,
158
            "Phase 3": 3,
159
            "Phase 2": 2,
160
            "Phase 1": 1,
161
            "Early Phase 1": 1,
162
            "N/A": 0,
163
        }.get(self.status, 0)
164
165
166
@dataclass(frozen=True, repr=True, eq=True)
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
167
class GhsCode:
168
    code: Codes.GhsCode
169
    statement: str
170
    clazz: str
171
    categories: FrozenSet[str]
172
    signal_word: str
173
    type: str
174
175
    @classmethod
176
    def find(cls, code: str) -> GhsCode:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
177
        h = hazards[code]
0 ignored issues
show
Coding Style Naming introduced by
Variable name "h" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
178
        cats = h["category"]  # TODO
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
179
        return GhsCode(
180
            code=Codes.GhsCode(code),
181
            statement=h["statement"],
182
            clazz=h["class"],
183
            categories=cats,
184
            signal_word=h["signal_word"],
185
            type=h["type"],
186
        )
187
188
    @property
189
    def level(self) -> int:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
190
        return int(self.code[1])
191
192
193
@dataclass(frozen=True, repr=True, eq=True)
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
194
class AssociatedDisorder:
195
    disease: str
196
    evidence_type: str
197
    n_refs: int
198
199
200
@dataclass(frozen=True, repr=True, eq=True)
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
201
class AtcCode:
202
    code: str
203
    name: str
204
205
    @property
206
    def level(self) -> int:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
207
        return len(self.parts)
208
209
    @property
210
    def parts(self) -> Sequence[str]:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
211
        pat = re.compile(r"([A-Z])([0-9]{2})?([A-Z])?([A-Z])?([A-Z])?")
212
        match = pat.fullmatch(self.code)
213
        return [g for g in match.groups() if g is not None]
214
215
216
@dataclass(frozen=True, repr=True, eq=True)
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
217
class DrugbankInteraction:
218
    gene_symbol: Codes.GeneId
219
    action: str
220
    target_name: str
221
    general_function: Sequence[str]
222
    specific_function: str
223
    pmids: FrozenSet[Codes.PubmedId]
224
    dois: FrozenSet[Codes.Doi]
225
226
227
@dataclass(frozen=True, repr=True, eq=True)
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
228
class DrugbankDdi:
229
    drug_drugbank_id: Codes.DrugbankCompoundId
230
    drug_pubchem_id: Codes.PubchemCompoundId
231
    drug_drugbank_name: str
232
    description: str
233
234
235
class AssayType(enum.Enum):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
236
    confirmatory = enum.auto()
237
    literature = enum.auto()
238
239
240
@dataclass(frozen=True, repr=True, eq=True)
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
241
class PubchemAssay:
242
    type: AssayType
243
    ref: str  # e.g. "ChEMBL"
244
    name: str  # e.g. "Binding affinity towards human monoclonal antibody 2E2 using [3H]cocaine"
245
    made_date: date
246
247
248
class Activity(enum.Enum):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
249
    active = enum.auto()
250
    inactive = enum.auto()
251
    unspecified = enum.auto()
252
253
254
@dataclass(frozen=True, repr=True, eq=True)
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
best-practice introduced by
Too many instance attributes (8/7)
Loading history...
255
class Bioactivity:
256
    assay: PubchemAssay
257
    gene_id: Optional[Codes.GeneId]
258
    tax_id: Optional[int]
259
    pmid: Optional[Codes.PubmedId]
260
    activity: Activity
261
    activity_name: str
262
    activity_value: str
263
    target_name: Optional[str]
264
265
266
@dataclass(frozen=True, repr=True, eq=True)
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
best-practice introduced by
Too many instance attributes (9/7)
Loading history...
267
class PdbEntry:
268
    pdbid: Codes.PdbId
269
    title: str
270
    exp_method: str
271
    resolution: float
272
    lig_names: FrozenSet[str]
273
    cids: FrozenSet[Codes.PubchemCompoundId]
274
    uniprot_ids: FrozenSet[Codes.UniprotId]
275
    pmids: FrozenSet[Codes.PubmedId]
276
    dois: FrozenSet[Codes.Doi]
277
278
279
@dataclass(frozen=True, repr=True, eq=True)
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
best-practice introduced by
Too many instance attributes (11/7)
Loading history...
280
class PubmedEntry:
281
    pmid: Codes.PubmedId
282
    article_type: str
283
    pmidsrcs: FrozenSet[str]
284
    mesh_headings: FrozenSet[Codes.MeshHeading]
285
    mesh_subheadings: FrozenSet[Codes.MeshSubheading]
286
    mesh_codes: FrozenSet[Codes.MeshCode]
287
    cids: FrozenSet[Codes.PubchemCompoundId]
288
    article_title: str
289
    article_abstract: str
290
    journal_name: str
291
    pub_date: date
292
293
294
@dataclass(frozen=True, repr=True, eq=True)
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
295
class Publication:
296
    pmid: Codes.PubmedId
297
    pub_date: date
298
    is_review: bool
299
    title: str
300
    journal: str
301
    relevance_score: int
302
303
304
@dataclass(frozen=True, repr=True, eq=True)
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
best-practice introduced by
Too many instance attributes (8/7)
Loading history...
305
class CoOccurrence:
306
    neighbor_id: str
307
    neighbor_name: str
308
    kind: CoOccurrenceType
309
    # https://pubchemdocs.ncbi.nlm.nih.gov/knowledge-panels
310
    article_count: int
311
    query_article_count: int
312
    neighbor_article_count: int
313
    score: int
314
    publications: FrozenSet[Publication]
315
316
    def strip_pubs(self) -> CoOccurrence:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
317
        return CoOccurrence(
318
            self.neighbor_id,
319
            self.neighbor_name,
320
            self.kind,
321
            self.article_count,
322
            self.query_article_count,
323
            self.neighbor_article_count,
324
            self.score,
325
            frozenset({}),
326
        )
327
328
329
@dataclass(frozen=True, repr=True, eq=True)
0 ignored issues
show
Documentation introduced by
Empty class docstring
Loading history...
330
class DrugGeneInteraction:
331
    """"""
332
333
    gene_name: Optional[str]
334
    gene_claim_id: Optional[str]
335
    source: str
336
    interactions: FrozenSet[str]
337
    pmids: FrozenSet[Codes.PubmedId]
338
    dois: FrozenSet[Codes.Doi]
339
340
341
@dataclass(frozen=True, repr=True, eq=True)
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
342
class CompoundGeneInteraction:
343
    gene_name: Optional[Codes.GeneId]
344
    interactions: FrozenSet[str]
345
    tax_name: Optional[str]
346
    pmids: FrozenSet[Codes.PubmedId]
347
348
349
__all__ = [
350
    "ClinicalTrial",
351
    "AssociatedDisorder",
352
    "AtcCode",
353
    "DrugbankInteraction",
354
    "DrugbankDdi",
355
    "Bioactivity",
356
    "PubchemAssay",
357
    "DrugGeneInteraction",
358
    "CompoundGeneInteraction",
359
    "GhsCode",
360
    "PubmedEntry",
361
    "Code",
362
    "Codes",
363
    "CoOccurrenceType",
364
    "CoOccurrence",
365
    "Publication",
366
    "ComputedProperty",
367
]
368