-
Notifications
You must be signed in to change notification settings - Fork 6
/
__main__.py
159 lines (120 loc) · 3.65 KB
/
__main__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"Enable python -m genome_grist.click"
import sys
import os
import subprocess
import click
def get_snakefile_path(name):
thisdir = os.path.dirname(__file__)
snakefile = os.path.join(thisdir, "conf", name)
return snakefile
def get_package_configfile(filename):
thisdir = os.path.dirname(__file__)
configfile = os.path.join(thisdir, "conf", filename)
return configfile
def run_snakemake(
configfile,
no_use_conda=False,
verbose=False,
snakefile_name="Snakefile",
outdir=None,
extra_args=[],
):
# find the Snakefile relative to package path
snakefile = get_snakefile_path(snakefile_name)
# basic command
cmd = ["snakemake", "-s", snakefile]
# add --use-conda
if not no_use_conda:
cmd += ["--use-conda"]
# add --outdir
if outdir:
cmd += ["--config", f"outdir={outdir}"]
# snakemake sometimes seems to want a default -j; set it to 1 for now.
# can overridden later on command line.
cmd += ["-j", "1"]
# add rest of snakemake arguments
cmd += list(extra_args)
# add configfile - try looking for it a few different ways.
configfiles = []
if os.path.isfile(configfile):
configfiles = [configfile]
elif os.path.isfile(get_package_configfile(configfile)):
configfiles = [get_package_configfile(configfile)]
else:
for suffix in ".yaml", ".conf":
tryfile = configfile + suffix
if os.path.isfile(tryfile):
configfiles = [tryfile]
break
tryfile = get_package_configfile(tryfile)
if os.path.isfile(tryfile):
configfiles = [tryfile]
break
if not configfiles:
raise ValueError(f"cannot find config file '{configfile}'")
cmd += ["--configfile"] + configfiles
if verbose:
print("final command:", cmd)
# runme
try:
subprocess.check_call(cmd)
except subprocess.CalledProcessError as e:
print(f"Error in snakemake invocation: {e}", file=sys.stderr)
return e.returncode
return 0
#
# actual command line functions
#
@click.group()
def cli():
pass
# create a run subcommand that by default passes all of its arguments
# on to snakemake (after setting Snakefile and config)
@click.command(context_settings={"ignore_unknown_options": True})
@click.argument("configfile")
@click.option("--no-use-conda", is_flag=True, default=False)
@click.option("--verbose", is_flag=True)
@click.option("--outdir", nargs=1)
@click.argument("snakemake_args", nargs=-1)
def run(configfile, snakemake_args, no_use_conda, verbose, outdir):
"execute genome-grist workflow (using snakemake underneath)"
run_snakemake(
configfile,
snakefile_name="Snakefile",
no_use_conda=no_use_conda,
verbose=verbose,
extra_args=snakemake_args,
outdir=outdir,
)
# 'check' command
@click.command()
@click.argument("configfile")
def check(configfile):
"check configuration"
run_snakemake(configfile, extra_args=["check"])
# 'showconf' command
@click.command()
@click.argument("configfile")
def showconf(configfile):
"show full configuration"
run_snakemake(configfile, extra_args=["showconf"])
# 'info' command
@click.command()
def info():
"provide basic install/config file info"
from .version import version
print(
f"""
This is genome-grist version v{version}
Package install path: {os.path.dirname(__file__)}
snakemake Snakefile: {get_snakefile_path('Snakefile')}
"""
)
cli.add_command(run)
cli.add_command(check)
cli.add_command(showconf)
cli.add_command(info)
def main():
cli()
if __name__ == "__main__":
main()