1
|
|
|
""" |
2
|
|
|
Command-line interface for mandos. |
3
|
|
|
""" |
4
|
|
|
|
5
|
|
|
from __future__ import annotations |
6
|
|
|
|
7
|
|
|
import os |
8
|
|
|
from pathlib import Path |
9
|
|
|
from typing import Optional |
10
|
|
|
|
11
|
|
|
import typer |
|
|
|
|
12
|
|
|
from pocketutils.core.exceptions import XValueError |
|
|
|
|
13
|
|
|
from typeddfs import FileFormat |
|
|
|
|
14
|
|
|
from typeddfs.cli_help import DfCliHelp |
|
|
|
|
15
|
|
|
from typeddfs.utils import Utils as TypedDfsUtils |
|
|
|
|
16
|
|
|
|
17
|
|
|
from mandos.analysis.filtration import Filtration |
18
|
|
|
from mandos.analysis.reification import Reifier |
19
|
|
|
from mandos.entry._arg_utils import Arg, ArgUtils, EntryUtils, Opt |
20
|
|
|
from mandos.entry._common_args import CommonArgs |
21
|
|
|
from mandos.entry._common_args import CommonArgs as Ca |
|
|
|
|
22
|
|
|
from mandos.entry.docs import Documenter |
23
|
|
|
from mandos.entry.fillers import CompoundIdFiller, IdMatchDf |
24
|
|
|
from mandos.entry.multi_searches import MultiSearch, SearchConfigDf |
25
|
|
|
from mandos.entry.searchers import InputCompoundsDf |
26
|
|
|
from mandos.model.apis.g2p_api import CachingG2pApi |
27
|
|
|
from mandos.model.hit_dfs import HitDf |
28
|
|
|
from mandos.model.settings import SETTINGS, Globals |
29
|
|
|
from mandos.model.taxonomy import TaxonomyDf |
30
|
|
|
from mandos.model.taxonomy_caches import TaxonomyFactories |
31
|
|
|
from mandos.model.utils.setup import MANDOS_SETUP, logger |
32
|
|
|
|
33
|
|
|
DEF_SUFFIX = SETTINGS.table_suffix |
34
|
|
|
nl = "\n\n" |
|
|
|
|
35
|
|
|
|
36
|
|
|
|
37
|
|
|
class _InsertedCommandListSingleton: |
38
|
|
|
commands = None |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
class MiscCommands: |
|
|
|
|
42
|
|
|
@staticmethod |
43
|
|
|
def init( |
44
|
|
|
log: Optional[Path] = CommonArgs.log, |
|
|
|
|
45
|
|
|
stderr: str = CommonArgs.stderr, |
|
|
|
|
46
|
|
|
): |
47
|
|
|
""" |
48
|
|
|
Initializes mandos, creating directories, etc. |
49
|
|
|
""" |
50
|
|
|
MANDOS_SETUP(log, stderr) |
51
|
|
|
Globals.mandos_path.mkdir(exist_ok=True, parents=True) |
52
|
|
|
typer.echo(f"Mandos home dir is {Globals.mandos_path}") |
53
|
|
|
if Globals.settings_path.exists(): |
54
|
|
|
typer.echo(f"Settings found at {Globals.settings_path}") |
55
|
|
|
else: |
56
|
|
|
typer.echo("No settings file found") |
57
|
|
|
typer.echo(f"Log level for stderr is level {logger.current_stderr_log_level}") |
58
|
|
|
|
59
|
|
|
@staticmethod |
60
|
|
|
def list_settings( |
61
|
|
|
log: Optional[Path] = CommonArgs.log, |
|
|
|
|
62
|
|
|
stderr: str = CommonArgs.stderr, |
|
|
|
|
63
|
|
|
): |
64
|
|
|
r""" |
65
|
|
|
Write the settings to stdout. |
66
|
|
|
""" |
67
|
|
|
MANDOS_SETUP(log, stderr) |
68
|
|
|
defaults = SETTINGS.defaults() |
69
|
|
|
width = max((len(k) + 2 + len(v) + 1 for k, v in SETTINGS.items())) |
|
|
|
|
70
|
|
|
for k, v in SETTINGS.as_dict(): |
|
|
|
|
71
|
|
|
msg = f"{k} = {v}".ljust(width) |
72
|
|
|
if v != defaults[k]: |
73
|
|
|
msg += f" (default: {defaults[k]})" |
74
|
|
|
typer.echo(msg) |
75
|
|
|
|
76
|
|
|
@staticmethod |
77
|
|
|
def document( |
|
|
|
|
78
|
|
|
to: Path = Opt.out_file( |
|
|
|
|
79
|
|
|
r""" |
80
|
|
|
The path to write command documentation to. |
81
|
|
|
|
82
|
|
|
The filename can end with .txt with optional compression (e.g. .txt.gz) |
83
|
|
|
for formatted text output alongside --format. |
84
|
|
|
|
85
|
|
|
Can can also use any table format: .feather; .snappy/.parquet; |
86
|
|
|
or .csv, .tsv, .tab, .json, .flexwf (with optional .gz/.bz2/.zip/.xz). |
87
|
|
|
|
88
|
|
|
[default: "commands-level<level>.txt"] |
89
|
|
|
""" |
90
|
|
|
), |
91
|
|
|
style: str = Opt.val( |
|
|
|
|
92
|
|
|
rf""" |
93
|
|
|
The format for formatted text output. |
94
|
|
|
|
95
|
|
|
This is ignored if --to is not a .txt file (or .txt.gz, etc.). |
96
|
|
|
The choices are: "table", "document", {", ".join(TypedDfsUtils.table_formats())}. |
97
|
|
|
|
98
|
|
|
"table" is a special style that saves in any machine-readable table format, such |
99
|
|
|
as Feather or Parquet (determined by --to). |
100
|
|
|
|
101
|
|
|
"document" is a special style that emits non-table-like flat text. |
102
|
|
|
""", |
103
|
|
|
"--style", |
104
|
|
|
default="table", |
105
|
|
|
), |
106
|
|
|
width: int = Opt.val( |
|
|
|
|
107
|
|
|
r""" |
108
|
|
|
Max number of characters for a cell. |
109
|
|
|
|
110
|
|
|
After that, the text is wrapped. |
111
|
|
|
Only applies when writing formatted text (.txt, etc.). |
112
|
|
|
|
113
|
|
|
[default: 40 if level > 1; 100 otherwise] |
114
|
|
|
""", |
115
|
|
|
default=None, |
116
|
|
|
show_default=False, |
117
|
|
|
), |
118
|
|
|
level: int = Opt.val( |
|
|
|
|
119
|
|
|
r""" |
120
|
|
|
The amount of detail to output. |
121
|
|
|
|
122
|
|
|
- 1 : show a 1-line description |
123
|
|
|
|
124
|
|
|
- 2 : Show a 1-line description, plus parameter names |
125
|
|
|
|
126
|
|
|
- 3 : Show the full description, plus parameter names, types, and 1-line descriptions |
127
|
|
|
|
128
|
|
|
- 4 : Show the full description, plus parameter names types, and full descriptions |
129
|
|
|
|
130
|
|
|
- 5 : Same as 4, but enable --hidden and --common |
131
|
|
|
""", |
132
|
|
|
default=4, |
133
|
|
|
min=1, |
134
|
|
|
max=5, |
135
|
|
|
), |
136
|
|
|
main_only: bool = Opt.flag(r"Only include main commands."), |
|
|
|
|
137
|
|
|
search_only: bool = Opt.flag(r"Only include search commands."), |
|
|
|
|
138
|
|
|
hidden: bool = Opt.flag(r"Show hidden commands."), |
|
|
|
|
139
|
|
|
common: bool = Opt.flag( |
|
|
|
|
140
|
|
|
r""" |
141
|
|
|
Show common arguments and options. |
142
|
|
|
|
143
|
|
|
Normally --log, --quiet, and --verbose are excluded, |
144
|
|
|
along with path, --key, --to, --as-of for searches, |
145
|
|
|
and the hidden flags for searches --check and --no-setup. |
146
|
|
|
""" |
147
|
|
|
), |
148
|
|
|
replace: bool = Ca.replace, |
|
|
|
|
149
|
|
|
log: Optional[Path] = Ca.log, |
|
|
|
|
150
|
|
|
stderr: str = CommonArgs.stderr, |
|
|
|
|
151
|
|
|
): |
152
|
|
|
r""" |
153
|
|
|
Write documentation on commands to a file. |
154
|
|
|
""" |
155
|
|
|
MANDOS_SETUP(log, stderr) |
156
|
|
|
if level == 5: |
157
|
|
|
hidden = common = True |
158
|
|
|
if width is None and level == 1: |
159
|
|
|
width = 100 |
160
|
|
|
elif width is None: |
161
|
|
|
width = 40 |
162
|
|
|
if width == 0: |
163
|
|
|
width = 9223372036854775807 |
164
|
|
|
default = f"commands-level{level}.txt" |
165
|
|
|
to = EntryUtils.adjust_filename(to, default, replace=replace) |
166
|
|
|
doc = Documenter( |
167
|
|
|
level=level, |
168
|
|
|
main=main_only, |
169
|
|
|
search=search_only, |
170
|
|
|
hidden=hidden, |
171
|
|
|
common=common, |
172
|
|
|
width=width, |
173
|
|
|
) |
174
|
|
|
doc.document(_InsertedCommandListSingleton.commands, to, style) |
175
|
|
|
|
176
|
|
|
@staticmethod |
177
|
|
|
def search( |
|
|
|
|
178
|
|
|
path: Path = Ca.in_compound_table, |
|
|
|
|
179
|
|
|
config: Path = Opt.in_file( |
|
|
|
|
180
|
|
|
r""" |
181
|
|
|
TOML config file. See the docs. |
182
|
|
|
""", |
183
|
|
|
default=..., |
184
|
|
|
), |
185
|
|
|
to: Path = Ca.out_wildcard, |
|
|
|
|
186
|
|
|
log: Optional[Path] = Ca.log, |
|
|
|
|
187
|
|
|
stderr: str = CommonArgs.stderr, |
|
|
|
|
188
|
|
|
replace: bool = Opt.flag(r"""Overwrite files if they exist."""), |
|
|
|
|
189
|
|
|
check: bool = Opt.flag("Check and write docs file only; do not run"), |
|
|
|
|
190
|
|
|
) -> None: |
191
|
|
|
r""" |
192
|
|
|
Run multiple searches. |
193
|
|
|
""" |
194
|
|
|
MANDOS_SETUP(log, stderr) |
195
|
|
|
default = "search-" + Globals.start_timestamp_filesys |
196
|
|
|
# TODO: , suffixes=FileFormat.from_path |
|
|
|
|
197
|
|
|
out_dir, suffix = EntryUtils.adjust_dir_name(to, default) |
198
|
|
|
logger.notice(f"Will write {suffix} to {out_dir}{os.sep}") |
199
|
|
|
config_fmt = FileFormat.from_path(config) |
200
|
|
|
if config_fmt is not FileFormat.toml: |
201
|
|
|
logger.caution(f"Config format is {config_fmt}, not toml; trying anyway") |
202
|
|
|
config = SearchConfigDf.read_file(config) |
203
|
|
|
search = MultiSearch(config, path, out_dir, suffix, replace, log) |
204
|
|
|
if not check: |
205
|
|
|
search.run() |
206
|
|
|
|
207
|
|
|
@staticmethod |
208
|
|
|
def serve( |
|
|
|
|
209
|
|
|
port: int = Opt.val(r"Port to serve on", default=1540), |
|
|
|
|
210
|
|
|
db: str = Opt.val("Name of the MySQL database", default="mandos"), |
|
|
|
|
211
|
|
|
log: Optional[Path] = Ca.log, |
|
|
|
|
212
|
|
|
stderr: str = CommonArgs.stderr, |
|
|
|
|
213
|
|
|
) -> None: |
214
|
|
|
r""" |
215
|
|
|
Start a REST server. |
216
|
|
|
|
217
|
|
|
The connection information is stored in your global settings file. |
218
|
|
|
""" |
219
|
|
|
MANDOS_SETUP(log, stderr) |
220
|
|
|
|
221
|
|
|
@staticmethod |
222
|
|
|
def export_db( |
|
|
|
|
223
|
|
|
path: Path = Ca.in_annotations_file, |
|
|
|
|
224
|
|
|
db: str = Opt.val(r"Name of the MySQL database", default="mandos"), |
|
|
|
|
225
|
|
|
host: str = Opt.val( |
|
|
|
|
226
|
|
|
r"Database hostname (ignored if ``--socket`` is passed", default="127.0.0.1" |
227
|
|
|
), |
228
|
|
|
socket: Optional[str] = Opt.val("Path to a Unix socket (if set, ``--host`` is ignored)"), |
|
|
|
|
229
|
|
|
user: Optional[str] = Opt.val("Database username (empty if not set)"), |
|
|
|
|
230
|
|
|
password: Optional[str] = Opt.val("Database password (empty if not set)"), |
|
|
|
|
231
|
|
|
as_of: Optional[str] = CommonArgs.as_of, |
|
|
|
|
232
|
|
|
log: Optional[Path] = CommonArgs.log, |
|
|
|
|
233
|
|
|
stderr: str = CommonArgs.stderr, |
|
|
|
|
234
|
|
|
) -> None: |
235
|
|
|
r""" |
236
|
|
|
Export to a relational database. |
237
|
|
|
|
238
|
|
|
Saves data from Mandos search commands to a database for serving via REST. |
239
|
|
|
|
240
|
|
|
See also: ``:serve``. |
241
|
|
|
""" |
242
|
|
|
MANDOS_SETUP(log, stderr) |
243
|
|
|
|
244
|
|
|
@staticmethod |
245
|
|
|
def init_db( |
|
|
|
|
246
|
|
|
db: str = Opt.val(r"Name of the MySQL database", default="mandos"), |
|
|
|
|
247
|
|
|
host: str = Opt.val( |
|
|
|
|
248
|
|
|
r"Database hostname (ignored if ``--socket`` is passed", default="127.0.0.1" |
249
|
|
|
), |
250
|
|
|
socket: Optional[str] = Opt.val("Path to a Unix socket (if set, ``--host`` is ignored)"), |
|
|
|
|
251
|
|
|
user: Optional[str] = Opt.val("Database username (empty if not set)"), |
|
|
|
|
252
|
|
|
password: Optional[str] = Opt.val("Database password (empty if not set)"), |
|
|
|
|
253
|
|
|
overwrite: bool = Opt.flag(r"Delete the database if it exists"), |
|
|
|
|
254
|
|
|
yes: bool = Ca.yes, |
|
|
|
|
255
|
|
|
log: Optional[Path] = CommonArgs.log, |
|
|
|
|
256
|
|
|
stderr: str = CommonArgs.stderr, |
|
|
|
|
257
|
|
|
) -> None: |
258
|
|
|
r""" |
259
|
|
|
Initialize an empty database. |
260
|
|
|
""" |
261
|
|
|
MANDOS_SETUP(log, stderr) |
262
|
|
|
|
263
|
|
|
@staticmethod |
264
|
|
|
def fill( |
|
|
|
|
265
|
|
|
path: Path = Arg.in_file( |
|
|
|
|
266
|
|
|
rf""" |
267
|
|
|
{DfCliHelp.help(InputCompoundsDf).get_short_text(nl=nl)} |
268
|
|
|
""", |
269
|
|
|
), |
270
|
|
|
to: Path = Opt.out_path( |
|
|
|
|
271
|
|
|
rf""" |
272
|
|
|
{DfCliHelp.help(IdMatchDf).get_short_text(nl=nl)} |
273
|
|
|
|
274
|
|
|
[default: <path>-ids-<start-time>{DEF_SUFFIX}] |
275
|
|
|
""" |
276
|
|
|
), |
277
|
|
|
no_pubchem: bool = Opt.flag("Do not use PubChem.", "--no-pubchem"), |
|
|
|
|
278
|
|
|
no_chembl: bool = Opt.flag("Do not use ChEMBL.", "--no-chembl"), |
|
|
|
|
279
|
|
|
replace: bool = Ca.replace, |
|
|
|
|
280
|
|
|
log: Optional[Path] = Ca.log, |
|
|
|
|
281
|
|
|
stderr: str = CommonArgs.stderr, |
|
|
|
|
282
|
|
|
) -> None: |
283
|
|
|
r""" |
284
|
|
|
Fill in missing IDs from existing compound data. |
285
|
|
|
|
286
|
|
|
The idea is to find a ChEMBL ID, a PubChem ID, and parent-compound InChI/InChI Key. |
287
|
|
|
Useful to check compound/ID associations before running a search. |
288
|
|
|
|
289
|
|
|
To be filled, each row must should have a non-null value for |
290
|
|
|
"inchikey", "chembl_id", and/or "pubchem_id". |
291
|
|
|
"inchi" will be used but not to match to PubChem and ChEMBL. |
292
|
|
|
|
293
|
|
|
No existing columns will be dropped or modified. |
294
|
|
|
Any conflicting column will be renamed to 'origin_<column>'. |
295
|
|
|
E.g. 'inchikey' will be renamed to 'origin_inchikey'. |
296
|
|
|
(Do not include a column beginning with 'origin_'). |
297
|
|
|
|
298
|
|
|
Final columns (assuming --no-chembl and --no-pubchem) will include: |
299
|
|
|
inchikey, inchi, pubchem_id, chembl_id, pubchem_inch, chembl_inchi, |
300
|
|
|
pubchem_inchikey, and chembl_inchikey. |
301
|
|
|
The "inchikey" and "inchikey" columns will be the "best" available: |
302
|
|
|
chembl (preferred), then pubchem, then your source inchikey column. |
303
|
|
|
In cases where PubChem and ChEMBL differ, an error will be logged. |
304
|
|
|
You can always check the columns "origin_inchikey" (yours), |
305
|
|
|
chembl_inchikey, and pubchem_inchikey. |
306
|
|
|
|
307
|
|
|
The steps are: |
308
|
|
|
|
309
|
|
|
- If "chembl_id" or "pubchem_id" is non-null, uses that to find an InChI Key (for each). |
310
|
|
|
|
311
|
|
|
- Otherwise, if only "inchikey" is non-null, uses it to find ChEMBL and PubChem records. |
312
|
|
|
|
313
|
|
|
- Log an error if the inchikeys or inchis differ between PubChem and ChEMBL. |
314
|
|
|
|
315
|
|
|
- Set the final "inchi" and "inchikey" to the best choice, |
316
|
|
|
falling back to the input inchi and inchikey if they are missing. |
317
|
|
|
""" |
318
|
|
|
MANDOS_SETUP(log, stderr) |
319
|
|
|
default = str(Path(path).with_suffix("")) + "-filled" + "".join(path.suffixes) |
320
|
|
|
to = EntryUtils.adjust_filename(to, default, replace=replace) |
321
|
|
|
df = IdMatchDf.read_file(path) |
|
|
|
|
322
|
|
|
df = CompoundIdFiller(chembl=not no_chembl, pubchem=not no_pubchem).fill(df) |
|
|
|
|
323
|
|
|
df.write_file(to) |
324
|
|
|
|
325
|
|
|
@staticmethod |
326
|
|
|
def cache_data( |
327
|
|
|
path: Path = Ca.in_compound_table, |
|
|
|
|
328
|
|
|
no_pubchem: bool = Opt.flag(r"Do not download data from PubChem", "--no-pubchem"), |
|
|
|
|
329
|
|
|
no_chembl: bool = Opt.flag(r"Do not fetch IDs from ChEMBL", "--no_chembl"), |
|
|
|
|
330
|
|
|
log: Optional[Path] = CommonArgs.log, |
|
|
|
|
331
|
|
|
stderr: str = CommonArgs.stderr, |
|
|
|
|
332
|
|
|
) -> None: |
333
|
|
|
r""" |
334
|
|
|
Fetch and cache compound data. |
335
|
|
|
|
336
|
|
|
Useful to freeze data before running a search. |
337
|
|
|
""" |
338
|
|
|
MANDOS_SETUP(log, stderr) |
339
|
|
|
logger.error(f"Not implemented fully yet.") |
|
|
|
|
340
|
|
|
df = IdMatchDf.read_file(path) |
|
|
|
|
341
|
|
|
df = CompoundIdFiller(chembl=not no_chembl, pubchem=not no_pubchem).fill(df) |
|
|
|
|
342
|
|
|
logger.notice(f"Done caching.") |
|
|
|
|
343
|
|
|
|
344
|
|
|
@staticmethod |
345
|
|
|
def export_taxa( |
|
|
|
|
346
|
|
|
taxa: str = Ca.taxa, |
|
|
|
|
347
|
|
|
forbid: str = Ca.ban_taxa, |
|
|
|
|
348
|
|
|
to: Path = Opt.out_path( |
|
|
|
|
349
|
|
|
rf""" |
350
|
|
|
{DfCliHelp.help(TaxonomyDf).get_short_text(nl=nl)} |
351
|
|
|
|
352
|
|
|
[default: ./<taxa>-<datetime>{DEF_SUFFIX}] |
353
|
|
|
""" |
354
|
|
|
), |
355
|
|
|
replace: bool = Ca.replace, |
|
|
|
|
356
|
|
|
in_cache: bool = CommonArgs.in_cache, |
|
|
|
|
357
|
|
|
log: Optional[Path] = CommonArgs.log, |
|
|
|
|
358
|
|
|
stderr: str = CommonArgs.stderr, |
|
|
|
|
359
|
|
|
): |
360
|
|
|
""" |
361
|
|
|
Export a taxonomic tree to a table. |
362
|
|
|
|
363
|
|
|
Writes a taxonomy of given taxa and their descendants to a table. |
364
|
|
|
""" |
365
|
|
|
MANDOS_SETUP(log, stderr) |
366
|
|
|
concat = taxa + "-" + forbid |
367
|
|
|
taxa = ArgUtils.parse_taxa(taxa) |
368
|
|
|
ancestors = ArgUtils.parse_taxa_ids(ancestors) |
|
|
|
|
369
|
|
|
forbid = ArgUtils.parse_taxa(forbid) |
370
|
|
|
default = concat + "-" + Globals.start_timestamp_filesys + DEF_SUFFIX |
371
|
|
|
to = EntryUtils.adjust_filename(to, default, replace=replace) |
372
|
|
|
my_tax = TaxonomyFactories.get_smart_taxonomy( |
373
|
|
|
allow=taxa, forbid=forbid, ancestors=ancestors, local_only=in_cache |
374
|
|
|
) |
375
|
|
|
my_tax = my_tax.to_df() |
376
|
|
|
my_tax.write_file(to, mkdirs=True) |
377
|
|
|
|
378
|
|
|
@staticmethod |
379
|
|
|
def cache_taxa( |
380
|
|
|
taxa: str = Opt.val( |
|
|
|
|
381
|
|
|
r""" |
382
|
|
|
Either "all" or a comma-separated list of UniProt taxon IDs. |
383
|
|
|
|
384
|
|
|
Can also be "vertebrata", "cellular", or "viral" for those specific taxa. |
385
|
|
|
"all" is only valid when --replace is passed; |
386
|
|
|
this will regenerate all taxonomy files that are found in the cache. |
387
|
|
|
""", |
388
|
|
|
default="", |
389
|
|
|
), |
390
|
|
|
replace: bool = Ca.replace, |
|
|
|
|
391
|
|
|
log: Optional[Path] = CommonArgs.log, |
|
|
|
|
392
|
|
|
stderr: str = CommonArgs.stderr, |
|
|
|
|
393
|
|
|
) -> None: |
394
|
|
|
""" |
395
|
|
|
Prep a new taxonomy file for use in mandos. |
396
|
|
|
|
397
|
|
|
With --replace set, will delete any existing file. |
398
|
|
|
This can be useful to make sure your cached taxonomy is up-to-date before running. |
399
|
|
|
|
400
|
|
|
Downloads and converts a tab-separated file from UniProt. |
401
|
|
|
(To find manually, follow the ``All lower taxonomy nodes`` link and click ``Download``.) |
402
|
|
|
Then applies fixes and reduces the file size, creating a new file alongside. |
403
|
|
|
Puts both the raw data and fixed data in the cache under ``~/.mandos/taxonomy/``. |
404
|
|
|
""" |
405
|
|
|
MANDOS_SETUP(log, stderr) |
406
|
|
|
if taxa == "all" and not replace: |
407
|
|
|
raise XValueError(f"Use --replace with 'all'") |
|
|
|
|
408
|
|
|
# we're good to go: |
409
|
|
|
factory = TaxonomyFactories.main() |
410
|
|
|
if taxa == "all": |
411
|
|
|
taxa = TaxonomyFactories.list_cached_files().keys() |
412
|
|
|
else: |
413
|
|
|
taxa = ArgUtils.parse_taxa_ids(taxa) |
414
|
|
|
factory.rebuild(taxa, replace=replace) |
415
|
|
|
|
416
|
|
|
@staticmethod |
417
|
|
|
def cache_g2p( |
418
|
|
|
replace: bool = Ca.replace, |
|
|
|
|
419
|
|
|
log: Optional[Path] = CommonArgs.log, |
|
|
|
|
420
|
|
|
stderr: str = CommonArgs.stderr, |
|
|
|
|
421
|
|
|
) -> None: |
422
|
|
|
""" |
423
|
|
|
Caches GuideToPharmacology data. |
424
|
|
|
|
425
|
|
|
With --replace set, will overwrite existing cached data. |
426
|
|
|
Data will generally be stored under``~/.mandos/g2p/``. |
427
|
|
|
""" |
428
|
|
|
MANDOS_SETUP(log, stderr) |
429
|
|
|
api = CachingG2pApi(SETTINGS.g2p_cache_path) |
430
|
|
|
api.download(force=replace) |
431
|
|
|
|
432
|
|
|
@staticmethod |
433
|
|
|
def cache_clear( |
434
|
|
|
log: Optional[Path] = CommonArgs.log, |
|
|
|
|
435
|
|
|
stderr: str = CommonArgs.stderr, |
|
|
|
|
436
|
|
|
yes: bool = CommonArgs.yes, |
|
|
|
|
437
|
|
|
) -> None: |
438
|
|
|
""" |
439
|
|
|
Deletes all cached data. |
440
|
|
|
""" |
441
|
|
|
MANDOS_SETUP(log, stderr) |
442
|
|
|
typer.echo(f"Will recursively delete all of these paths:") |
|
|
|
|
443
|
|
|
for p in SETTINGS.all_cache_paths: |
|
|
|
|
444
|
|
|
typer.echo(f" {p}") |
445
|
|
|
if not yes: |
446
|
|
|
typer.confirm("Delete?", abort=True) |
447
|
|
|
for p in SETTINGS.all_cache_paths: |
|
|
|
|
448
|
|
|
p.unlink(missing_ok=True) |
449
|
|
|
logger.notice("Deleted all cached data") |
450
|
|
|
|
451
|
|
|
@staticmethod |
452
|
|
|
def concat( |
|
|
|
|
453
|
|
|
path: Path = Arg.in_dir( |
|
|
|
|
454
|
|
|
rf""" |
455
|
|
|
Directory containing results from a mandos search. |
456
|
|
|
|
457
|
|
|
{DfCliHelp.list_formats().get_short_text()} |
458
|
|
|
""" |
459
|
|
|
), |
460
|
|
|
to: Optional[Path] = Ca.out_annotations_file, |
|
|
|
|
461
|
|
|
replace: bool = Ca.replace, |
|
|
|
|
462
|
|
|
log: Optional[Path] = CommonArgs.log, |
|
|
|
|
463
|
|
|
stderr: str = CommonArgs.stderr, |
|
|
|
|
464
|
|
|
) -> None: |
465
|
|
|
r""" |
466
|
|
|
Concatenate Mandos annotation files into one. |
467
|
|
|
|
468
|
|
|
Note that ``:search`` automatically performs this; |
469
|
|
|
this is needed only if you want to combine results from multiple independent searches. |
470
|
|
|
""" |
471
|
|
|
MANDOS_SETUP(log, stderr) |
472
|
|
|
default = path / ("concat" + DEF_SUFFIX) |
473
|
|
|
to = EntryUtils.adjust_filename(to, default, replace) |
474
|
|
|
for found in path.iterdir(): |
|
|
|
|
475
|
|
|
pass |
476
|
|
|
|
477
|
|
|
@staticmethod |
478
|
|
|
def filter( |
|
|
|
|
479
|
|
|
path: Path = Ca.out_annotations_file, |
|
|
|
|
480
|
|
|
by: Optional[Path] = Arg.in_file( |
|
|
|
|
481
|
|
|
r""" |
482
|
|
|
Path to a file containing filters. |
483
|
|
|
|
484
|
|
|
See the docs for more info. |
485
|
|
|
""" |
486
|
|
|
), |
487
|
|
|
to: Optional[Path] = Ca.out_annotations_file, |
|
|
|
|
488
|
|
|
replace: bool = Ca.replace, |
|
|
|
|
489
|
|
|
log: Optional[Path] = CommonArgs.log, |
|
|
|
|
490
|
|
|
stderr: str = CommonArgs.stderr, |
|
|
|
|
491
|
|
|
) -> None: |
492
|
|
|
""" |
493
|
|
|
Filters by simple expressions. |
494
|
|
|
""" |
495
|
|
|
MANDOS_SETUP(log, stderr) |
496
|
|
|
default = str(path) + "-filter-" + by.stem + DEF_SUFFIX |
497
|
|
|
to = EntryUtils.adjust_filename(to, default, replace) |
498
|
|
|
df = HitDf.read_file(path) |
|
|
|
|
499
|
|
|
Filtration.from_file(by).apply(df).write_file(to) |
500
|
|
|
|
501
|
|
|
@staticmethod |
502
|
|
|
def export_state( |
|
|
|
|
503
|
|
|
path: Path = Ca.in_annotations_file, |
|
|
|
|
504
|
|
|
to: Optional[Path] = Opt.out_path( |
|
|
|
|
505
|
|
|
""" |
506
|
|
|
Path to the output file. |
507
|
|
|
|
508
|
|
|
Valid formats and filename suffixes are .nt and .txt with an optional .gz, .zip, or .xz. |
509
|
|
|
If only a filename suffix is provided, will use that suffix with the default directory. |
510
|
|
|
If no suffix is provided, will interpret the path as a directory and use the default filename. |
|
|
|
|
511
|
|
|
Will fail if the file exists and ``--replace`` is not set. |
512
|
|
|
|
513
|
|
|
[default: <path>-statements.nt] |
514
|
|
|
""" |
515
|
|
|
), |
516
|
|
|
replace: bool = Ca.replace, |
|
|
|
|
517
|
|
|
log: Optional[Path] = CommonArgs.log, |
|
|
|
|
518
|
|
|
stderr: str = CommonArgs.stderr, |
|
|
|
|
519
|
|
|
) -> None: |
520
|
|
|
""" |
521
|
|
|
Output simple N-triples statements. |
522
|
|
|
|
523
|
|
|
Each statement is of this form, where the InChI Key refers to the input data: |
524
|
|
|
|
525
|
|
|
`"InChI Key" "predicate" "object" .` |
526
|
|
|
""" |
527
|
|
|
MANDOS_SETUP(log, stderr) |
528
|
|
|
default = f"{path}-statements.nt" |
529
|
|
|
to = EntryUtils.adjust_filename(to, default, replace) |
530
|
|
|
hits = HitDf.read_file(path).to_hits() |
531
|
|
|
with to.open() as f: |
|
|
|
|
532
|
|
|
for hit in hits: |
533
|
|
|
f.write(hit.to_triple.n_triples) |
534
|
|
|
|
535
|
|
|
@staticmethod |
536
|
|
|
def export_reify( |
|
|
|
|
537
|
|
|
path: Path = Ca.in_annotations_file, |
|
|
|
|
538
|
|
|
to: Optional[Path] = Opt.out_path( |
|
|
|
|
539
|
|
|
r""" |
540
|
|
|
Path to the output file. |
541
|
|
|
|
542
|
|
|
The filename suffix should be either .nt (N-triples) or .ttl (Turtle), |
543
|
|
|
with an optional .gz, .zip, or .xz. |
544
|
|
|
If only a filename suffix is provided, will use that suffix with the default directory. |
545
|
|
|
If no suffix is provided, will interpret the path as a directory but use the default filename. |
|
|
|
|
546
|
|
|
Will fail if the file exists and ``--replace`` is not set. |
547
|
|
|
|
548
|
|
|
[default: <path>-reified.nt] |
549
|
|
|
""" |
550
|
|
|
), |
551
|
|
|
replace: bool = Ca.replace, |
|
|
|
|
552
|
|
|
log: Optional[Path] = CommonArgs.log, |
|
|
|
|
553
|
|
|
stderr: str = CommonArgs.stderr, |
|
|
|
|
554
|
|
|
) -> None: |
555
|
|
|
""" |
556
|
|
|
Outputs reified semantic triples. |
557
|
|
|
""" |
558
|
|
|
MANDOS_SETUP(log, stderr) |
559
|
|
|
default = f"{path}-reified.nt" |
560
|
|
|
to = EntryUtils.adjust_filename(to, default, replace) |
561
|
|
|
hits = HitDf.read_file(path).to_hits() |
562
|
|
|
with to.open() as f: |
|
|
|
|
563
|
|
|
for triple in Reifier().reify(hits): |
564
|
|
|
f.write(triple.n_triples) |
565
|
|
|
|
566
|
|
|
@staticmethod |
567
|
|
|
def export_copy( |
|
|
|
|
568
|
|
|
path: Path = Ca.in_annotations_file, |
|
|
|
|
569
|
|
|
to: Optional[Path] = Opt.out_path( |
|
|
|
|
570
|
|
|
rf""" |
571
|
|
|
Path to the output file. |
572
|
|
|
|
573
|
|
|
{DfCliHelp.list_formats().get_short_text()} |
574
|
|
|
|
575
|
|
|
[default: <path.parent>/export{DEF_SUFFIX}] |
576
|
|
|
""" |
577
|
|
|
), |
578
|
|
|
replace: bool = Ca.replace, |
|
|
|
|
579
|
|
|
log: Optional[Path] = CommonArgs.log, |
|
|
|
|
580
|
|
|
stderr: str = CommonArgs.stderr, |
|
|
|
|
581
|
|
|
) -> None: |
582
|
|
|
""" |
583
|
|
|
Copies and/or converts annotation files. |
584
|
|
|
|
585
|
|
|
Example: ``:export:copy --to .snappy`` to highly compress a data set. |
586
|
|
|
""" |
587
|
|
|
MANDOS_SETUP(log, stderr) |
588
|
|
|
default = path.parent / DEF_SUFFIX |
589
|
|
|
to = EntryUtils.adjust_filename(to, default, replace) |
590
|
|
|
df = HitDf.read_file(path) |
|
|
|
|
591
|
|
|
df.write_file(to) |
592
|
|
|
|
593
|
|
|
|
594
|
|
|
__all__ = ["MiscCommands"] |
595
|
|
|
|