|
1
|
|
|
""" |
|
2
|
|
|
Command-line interface for mandos. |
|
3
|
|
|
""" |
|
4
|
|
|
|
|
5
|
|
|
from __future__ import annotations |
|
6
|
|
|
|
|
7
|
|
|
import logging |
|
8
|
|
|
from pathlib import Path |
|
9
|
|
|
from typing import Optional |
|
10
|
|
|
import typer |
|
|
|
|
|
|
11
|
|
|
from mandos.model.settings import MANDOS_SETTINGS |
|
12
|
|
|
from mandos.model.taxonomy_caches import TaxonomyFactories |
|
13
|
|
|
from mandos.search.entries import Entries |
|
14
|
|
|
|
|
15
|
|
|
from mandos.search.api_singletons import Apis |
|
16
|
|
|
from mandos.search.multi_searches import MultiSearch |
|
17
|
|
|
from mandos.search.searcher import Searcher, SearcherUtils |
|
18
|
|
|
|
|
19
|
|
|
logger = logging.getLogger(__package__) |
|
20
|
|
|
# IMPORTANT! |
|
21
|
|
|
Apis.set_default() |
|
22
|
|
|
cli = typer.Typer() |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
class Commands: |
|
26
|
|
|
""" |
|
27
|
|
|
Entry points for mandos. |
|
28
|
|
|
""" |
|
29
|
|
|
|
|
30
|
|
|
@staticmethod |
|
31
|
|
|
def search( |
|
32
|
|
|
path: Path, |
|
|
|
|
|
|
33
|
|
|
config: Optional[Path] = None, |
|
|
|
|
|
|
34
|
|
|
) -> None: |
|
35
|
|
|
""" |
|
36
|
|
|
Runs multiple searches. |
|
37
|
|
|
|
|
38
|
|
|
Args: |
|
39
|
|
|
path: |
|
40
|
|
|
config: |
|
41
|
|
|
""" |
|
42
|
|
|
MultiSearch(path, config).search() |
|
43
|
|
|
|
|
44
|
|
|
@staticmethod |
|
45
|
|
|
def find( |
|
46
|
|
|
path: Path, |
|
|
|
|
|
|
47
|
|
|
pubchem: bool = True, |
|
|
|
|
|
|
48
|
|
|
chembl: bool = True, |
|
|
|
|
|
|
49
|
|
|
) -> None: |
|
50
|
|
|
""" |
|
51
|
|
|
Fetches and caches compound data. |
|
52
|
|
|
Useful to check what you can see before running a search. |
|
53
|
|
|
""" |
|
54
|
|
|
out_path = path.with_suffix(".ids.csv") |
|
55
|
|
|
if out_path.exists(): |
|
56
|
|
|
raise FileExistsError(out_path) |
|
57
|
|
|
inchikeys = SearcherUtils.read(path) |
|
58
|
|
|
df = SearcherUtils.dl(inchikeys, pubchem=pubchem, chembl=chembl) |
|
|
|
|
|
|
59
|
|
|
df.to_csv(out_path) |
|
60
|
|
|
typer.echo(f"Wrote to {out_path}") |
|
61
|
|
|
|
|
62
|
|
|
@staticmethod |
|
63
|
|
|
def dl_tax( |
|
64
|
|
|
taxon: int, |
|
|
|
|
|
|
65
|
|
|
) -> None: |
|
66
|
|
|
""" |
|
67
|
|
|
Preps a new taxonomy file for use in mandos. |
|
68
|
|
|
Just returns if a corresponding file already exists in the resources dir or mandos cache (``~/.mandos``). |
|
|
|
|
|
|
69
|
|
|
Otherwise, downloads a tab-separated file from UniProt. |
|
70
|
|
|
(To find manually, follow the ``All lower taxonomy nodes`` link and click ``Download``.) |
|
71
|
|
|
Then applies fixes and reduces the file size, creating a new file alongside. |
|
72
|
|
|
Puts both the raw data and fixed data in the cache under ``~/.mandos/taxonomy/``. |
|
73
|
|
|
|
|
74
|
|
|
Args: |
|
75
|
|
|
taxon: The **ID** of the UniProt taxon |
|
76
|
|
|
""" |
|
77
|
|
|
TaxonomyFactories.from_uniprot(MANDOS_SETTINGS.taxonomy_cache_path).load(taxon) |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
# Oh dear this is a nightmare |
|
81
|
|
|
# it's really hard to create typer commands with dynamically configured params -- |
|
82
|
|
|
# we really need to rely on its inferring of params |
|
83
|
|
|
# that makes this really hard to do well |
|
84
|
|
|
for entry in Entries: |
|
85
|
|
|
from typer.models import CommandInfo |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
info = CommandInfo(entry.cmd(), callback=entry.run) |
|
88
|
|
|
cli.registered_commands.append(info) |
|
89
|
|
|
setattr(Commands, entry.cmd(), entry.run) |
|
90
|
|
|
|
|
91
|
|
|
cli.registered_commands.extend( |
|
92
|
|
|
[ |
|
93
|
|
|
CommandInfo("search", callback=Commands.search), |
|
|
|
|
|
|
94
|
|
|
CommandInfo("dl_tax", callback=Commands.dl_tax, hidden=True), |
|
95
|
|
|
] |
|
96
|
|
|
) |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
if __name__ == "__main__": |
|
100
|
|
|
cli() |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
__all__ = ["Commands", "Searcher"] |
|
104
|
|
|
|