|
1
|
|
|
from pathlib import Path |
|
|
|
|
|
|
2
|
|
|
from typing import Set, Sequence, Optional, Union |
|
3
|
|
|
|
|
4
|
|
|
from mandos.model.utils.setup import logger |
|
5
|
|
|
from mandos.model.settings import MANDOS_SETTINGS |
|
6
|
|
|
from mandos.model.apis.chembl_support.chembl_activity import DataValidityComment |
|
7
|
|
|
from mandos.model.apis.chembl_support.chembl_targets import TargetType |
|
8
|
|
|
from mandos.model.apis.pubchem_support.pubchem_models import ClinicalTrialsGovUtils |
|
9
|
|
|
from mandos.model.taxonomy import Taxonomy |
|
10
|
|
|
from mandos.model.taxonomy_caches import TaxonomyFactories |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class EntryUtils: |
|
|
|
|
|
|
14
|
|
|
""" """ |
|
15
|
|
|
|
|
16
|
|
|
@classmethod |
|
17
|
|
|
def adjust_filename(cls, to: Optional[Path], default: Union[str, Path], replace: bool) -> Path: |
|
|
|
|
|
|
18
|
|
|
if to is None: |
|
19
|
|
|
path = Path(default) |
|
20
|
|
|
elif str(to).startswith("."): |
|
21
|
|
|
path = Path(default).with_suffix(str(to)) |
|
22
|
|
|
elif str(to).startswith("*."): |
|
23
|
|
|
path = Path(default).with_suffix(str(to)[1:]) |
|
24
|
|
|
elif to.is_dir() or to.suffix == "": |
|
25
|
|
|
path = to / default |
|
26
|
|
|
else: |
|
27
|
|
|
path = Path(to) |
|
28
|
|
|
if path.exists() and not replace: |
|
|
|
|
|
|
29
|
|
|
raise FileExistsError(f"File {path} already exists") |
|
30
|
|
|
elif replace: |
|
31
|
|
|
logger.info(f"Overwriting existing file {path}.") |
|
32
|
|
|
return path |
|
33
|
|
|
|
|
34
|
|
|
@staticmethod |
|
35
|
|
|
def split(st: str) -> Set[str]: |
|
|
|
|
|
|
36
|
|
|
return {s.strip() for s in st.split(",")} |
|
37
|
|
|
|
|
38
|
|
|
@staticmethod |
|
39
|
|
|
def get_taxa(taxa: str) -> Sequence[Taxonomy]: |
|
|
|
|
|
|
40
|
|
|
factory = TaxonomyFactories.from_uniprot(MANDOS_SETTINGS.taxonomy_cache_path) |
|
41
|
|
|
return [factory.load(str(taxon).strip()) for taxon in taxa.split(",")] |
|
42
|
|
|
|
|
43
|
|
|
@staticmethod |
|
44
|
|
|
def get_trial_statuses(st: str) -> Set[str]: |
|
|
|
|
|
|
45
|
|
|
return ClinicalTrialsGovUtils.resolve_statuses(st) |
|
46
|
|
|
|
|
47
|
|
|
@staticmethod |
|
48
|
|
|
def get_target_types(st: str) -> Set[str]: |
|
|
|
|
|
|
49
|
|
|
return {s.name for s in TargetType.resolve(st)} |
|
50
|
|
|
|
|
51
|
|
|
@staticmethod |
|
52
|
|
|
def get_flags(st: str) -> Set[str]: |
|
|
|
|
|
|
53
|
|
|
return {s.name for s in DataValidityComment.resolve(st)} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
__all__ = ["EntryUtils"] |
|
57
|
|
|
|