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