-
Notifications
You must be signed in to change notification settings - Fork 18
/
create-analysis-module.py
executable file
·312 lines (272 loc) · 10.4 KB
/
create-analysis-module.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env python3
"""Script to create a new analysis module for OpenScPCA"""
import argparse
import pathlib
import re
import shutil
import subprocess
import sys
from typing import Union
R_VERSION = "4.4.0"
BIOC_VERSION = "3.19"
def copy_file_with_tag_replacement(
src: Union[pathlib.Path, str],
dest: Union[pathlib.Path, str],
tag: str,
replacement: str,
) -> None:
"""
Copy a file from src to dest, replacing all occurrences of tag with replacement.
The tag in the file must be enclosed in `{{` and `}}` and may contain whitespace.
"""
tag = tag.strip(
" {}"
) # make sure the incoming tag does not have enclosing {} or whitespace
tag_re = re.compile(r"{{\s*" + tag + r"\s*}}")
with open(src, "r") as f:
content = f.readlines()
# replace the module tag with the module name
output = (tag_re.sub(replacement, line) for line in content)
with open(dest, "w") as f:
f.writelines(output)
def main() -> None:
parser = argparse.ArgumentParser(
description="Create a new analysis module for OpenScPCA.",
usage="./%(prog)s MODULE_NAME [options]",
)
parser.add_argument(
"name",
metavar="MODULE_NAME",
help=(
"Name of the analysis module to create."
" This will be used for the directory name and must not already exist within the `analyses` directory."
),
)
parser.add_argument(
"-R",
"--use-r",
"--use-R",
dest="use_r",
action="store_true",
default=False,
help="Set up for R analysis, including an R Markdown notebook template.",
)
parser.add_argument(
"--use-renv",
action="store_true",
default=False,
help="Initialize a new renv environment for the module. Implies `--use-r`.",
)
parser.add_argument(
"--use-conda",
action="store_true",
default=False,
help="Initialize a new conda environment for the module.",
)
parser.add_argument(
"--use-python",
action="store_true",
default=False,
help="Set up for Python analysis, including a Python script template. Implies `--use-conda`.",
)
parser.add_argument(
"--use-jupyter",
action="store_true",
default=False,
help="Set up for analysis with Jupyter, including a Jupyter Notebook template. Implies `--use-conda`.",
)
parser.add_argument(
"--conda-file-only",
action="store_true",
help=(
"Only make the conda `environment.yml` file."
" Creating a new conda environment is skipped."
),
)
args = parser.parse_args()
if args.use_renv:
args.use_r = True
if args.use_jupyter or args.use_python:
args.use_conda = True
# get the paths relative to this script file
base_dir = pathlib.Path(__file__).parent
template_dir = base_dir / "templates" / "analysis-module"
module_dir = base_dir / "analyses" / args.name
# fail if the module name is not a simple directory name
if not re.search(r"^[A-Za-z0-9_\-]+$", args.name):
sys.exit(
"Module name should not contain spaces or special characters.\nExiting."
)
# exit if the module directory already exists
if module_dir.exists():
sys.exit(
f"Analysis module `{args.name}` already exists at `{module_dir}`."
"\nExiting."
)
# set the conda environment name
env_name = f"openscpca-{args.name}"
# if use_conda is requested, check that conda is available
if args.use_conda and not args.conda_file_only:
if not shutil.which("conda"):
sys.exit(
"Setup with conda was requested, but conda is not available on the system."
"\nPlease install conda (Miniconda) and try again."
)
# check for existing environment name
existing_envs = subprocess.run(
["conda", "env", "list"],
capture_output=True,
text=True,
).stdout
existing_envs = [env.split()[0] for env in existing_envs.splitlines() if env]
if env_name in existing_envs:
sys.exit(
f"\nConda environment `{env_name}` already exists."
"\nYou may use the `--conda_file_only` option to create the environment file only and manually create your environment with:"
"\n`conda env create --f analyses/<module>/environment.yml --name <env_name>`"
"\n\nAlternatively, you can remove the existing environment with:"
f"\n`conda env remove --name {env_name}`"
"\nor rename the existing environment:"
f"\n\n`conda rename --name {env_name} <new_name>`"
"\n\nExiting."
)
# if use_renv is requested, check that R is available
if args.use_renv and not shutil.which("Rscript"):
sys.exit(
"Setup with renv was requested, but Rscript is not available on the system."
"\nPlease install R and try again."
)
# set up final status messages
final_messages = [f"Creating analysis module `{args.name}` complete:\n"]
# create the new module directory from the template
try:
shutil.copytree(template_dir, module_dir)
final_messages.append(
f"- Created new analysis module directory: `{module_dir}`."
)
except FileNotFoundError:
# just in case the template directory is missing
if not template_dir.exists():
sys.exit(
"Expected template directory does not exist at `{template_dir}`."
"\nExiting."
)
else:
raise
if args.use_conda or args.conda_file_only:
# add a template environment.yml file
if args.use_jupyter:
env_template = base_dir / "templates" / "jupyter" / "environment.yml"
else:
env_template = base_dir / "templates" / "python" / "environment.yml"
module_env = module_dir / "environment.yml"
copy_file_with_tag_replacement(
src=env_template,
dest=module_env,
tag="openscpca_module",
replacement=args.name,
)
final_messages.append(f"- Created conda environment file: `{module_env}`.")
# create the conda environment
if not args.conda_file_only:
subprocess.run(
["conda", "env", "create", "-f", "environment.yml"],
cwd=module_dir,
)
final_messages.append(f"- Created conda environment: `{env_name}`.")
if args.use_renv:
# initialize a new renv environment
renv_script = f"""
if (!requireNamespace("renv", quietly = TRUE))
install.packages("renv")
renv::scaffold(
repos = list(CRAN = "https://p3m.dev/cran/latest"),
settings = list(
ppm.enabled = TRUE,
r.version = "{R_VERSION}",
bioconductor.version = "{BIOC_VERSION}"
)
)
"""
subprocess.run(
["Rscript", "-e", renv_script],
cwd=module_dir,
)
# Set .Rprofile to not activate renv in an OpenScPCA Docker image
(module_dir / ".Rprofile").write_text(
"# Don't activate renv in an OpenScPCA docker image\n"
"if (Sys.getenv('OPENSCPCA_DOCKER') != 'TRUE') {\n"
" source('renv/activate.R')\n"
"}\n"
)
# make the components directory and add a dependencies.R file
component_dir = module_dir / "components"
component_dir.mkdir(exist_ok=True)
(component_dir / "dependencies.R").write_text(
"# R dependencies not captured by `renv`\n" '# library("missing_package")\n'
)
final_messages.append(f"- Initialized new renv environment in `{module_dir}`.")
# Add template files
if args.use_r:
template_rmd = base_dir / "templates" / "rmarkdown" / "notebook-template.Rmd"
module_rmd = module_dir / "notebook-template.Rmd"
copy_file_with_tag_replacement(
src=template_rmd,
dest=module_dir / "notebook-template.Rmd",
tag="openscpca_module",
replacement=args.name,
)
final_messages.append(f"- Added R Markdown notebook template: `{module_rmd}`.")
if args.use_python:
template_py = base_dir / "templates" / "python" / "script-template.py"
module_py = module_dir / "script-template.py"
copy_file_with_tag_replacement(
src=template_py,
dest=module_py,
tag="openscpca_module",
replacement=args.name,
)
if args.use_jupyter:
template_ipynb = base_dir / "templates" / "jupyter" / "notebook-template.ipynb"
module_ipynb = module_dir / "notebook-template.ipynb"
copy_file_with_tag_replacement(
src=template_ipynb,
dest=module_ipynb,
tag="openscpca_module",
replacement=args.name,
)
final_messages.append(f"- Added Jupyter Notebook template: `{module_ipynb}`.")
# Add GHA workflows
workflow_template_dir = base_dir / "templates" / "workflows"
workflows_dir = base_dir / ".github" / "workflows"
# run module template (default to conda)
if args.use_r:
gha_template_source = "run_renv-module.yml"
else:
gha_template_source = "run_conda-module.yml"
gha_template = workflows_dir / f"run_{args.name}.yml"
copy_file_with_tag_replacement(
src=workflow_template_dir / gha_template_source,
dest=gha_template,
tag="openscpca_module",
replacement=args.name,
)
final_messages.append(
f"- Added a GitHub Action template to run the module: `{gha_template}`."
)
# docker template
docker_template = workflows_dir / f"docker_{args.name}.yml"
copy_file_with_tag_replacement(
src=workflow_template_dir / "docker_module.yml",
dest=docker_template,
tag="openscpca_module",
replacement=args.name,
)
final_messages.append(
f"- Added a GitHub Action template for Docker builds: `{docker_template}`."
)
# print final status messages
print() # add a newline before the final messages
print("\n".join(final_messages))
if __name__ == "__main__":
main()