isabl-cli 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,87 @@
1
+ """Tests for pipelines package utils module."""
2
+
3
+ from os import path
4
+ from os.path import join
5
+ import os
6
+ import shutil
7
+ import pytest
8
+
9
+ from isabl_cli.batch_systems import lsf
10
+ from isabl_cli.batch_systems import sge
11
+ from isabl_cli.batch_systems import slurm
12
+
13
+
14
+ @pytest.mark.skipif(not shutil.which("bsub"), reason="bsub not found")
15
+ def test_submit_array_lsf(tmpdir):
16
+ _test_submit_commands(tmpdir, "lsf", lsf.submit_lsf_array)
17
+
18
+
19
+ @pytest.mark.skipif(not shutil.which("qsub"), reason="qsub not found")
20
+ def test_submit_array_sge(tmpdir):
21
+ _test_submit_commands(tmpdir, "sge", sge.submit_sge_array)
22
+
23
+
24
+ @pytest.mark.skipif(not shutil.which("sbatch"), reason="sbatch not found")
25
+ def test_submit_array_slurm(tmpdir):
26
+ _test_submit_commands(tmpdir, "slurm", slurm.submit_slurm_array)
27
+
28
+
29
+ def _test_submit_commands(tmpdir, scheduler, submit_array):
30
+ test_dir = tmpdir.strpath
31
+ commands = []
32
+ total_jobs = 10
33
+
34
+ # kill SGE with time limit instead of failing directly
35
+ # because SGE's implementation uses SIGUSR2 signal to catch
36
+ # the event when the scheduler kills the job.
37
+ # The other schedulers allow for direct job dependencies.
38
+ extra_args = "" if scheduler != "sge" else "-l h_rt=00:00:10"
39
+ exit_cmd = "exit 1" if scheduler != "sge" else "sleep 20"
40
+
41
+ for i in range(total_jobs):
42
+ rundir = path.join(test_dir, str(i))
43
+ os.makedirs(rundir, exist_ok=True)
44
+
45
+ # the head job script echoes to stdout and stderr, both will be stored in
46
+ # head_job.log and head_job.err respectively
47
+ cmd_content = "echo 'FERMINA' && (>&2 echo 'VALENTINO') "
48
+ command_path = join(rundir, "head_job.sh")
49
+
50
+ if i % 2 == 0:
51
+ cmd_content += f"&& {exit_cmd}"
52
+
53
+ with open(command_path, "+w") as f:
54
+ f.write(cmd_content)
55
+
56
+ # the exit command echoes both stderr and stdout, however
57
+ # both are meant to be stored in the same file, head_job.exit
58
+ commands.append((command_path, "echo 'LORD' && (>&2 echo 'HENRY')"))
59
+
60
+ submit_array(
61
+ commands=commands,
62
+ requirements="",
63
+ extra_args=extra_args,
64
+ throttle_by=total_jobs,
65
+ jobname="Test Job Array",
66
+ wait=True,
67
+ )
68
+
69
+ for i in range(total_jobs):
70
+ rundir = path.join(test_dir, str(i))
71
+
72
+ with open(join(rundir, "head_job.log")) as f:
73
+ assert "FERMINA" in f.read()
74
+
75
+ with open(join(rundir, "head_job.err")) as f:
76
+ assert "VALENTINO" in f.read()
77
+
78
+ # chech both stderr and stdout are redirected to head_job.exit
79
+ with open(join(rundir, "head_job.exit")) as f:
80
+ content = f.read()
81
+
82
+ if i % 2 == 0:
83
+ assert "LORD" in content
84
+ assert "HENRY" in content
85
+
86
+ if i % 2 != 0:
87
+ assert not content
tests/test_cli.py ADDED
@@ -0,0 +1,34 @@
1
+ from click.testing import CliRunner
2
+ import pytest
3
+
4
+ from isabl_cli import cli
5
+ from isabl_cli import settings
6
+
7
+ from .test_app import MockApplication
8
+
9
+
10
+ def test_main(capsys):
11
+ settings._DEFAULTS["INSTALLED_APPLICATIONS"] = [
12
+ # valid app
13
+ "tests.test_app.MockApplication",
14
+ # invalid app
15
+ "tests.test_app.ImaginaryApplication",
16
+ # used to increase coverage
17
+ "isabl_cli.api.IsablDict",
18
+ ]
19
+ # Prints error trying to import invalid app
20
+ cli.add_apps_groups(settings.system_settings.INSTALLED_APPLICATIONS)
21
+ captured = capsys.readouterr()
22
+ assert "Could not import 'tests.test_app.ImaginaryApplication'" in captured.err
23
+
24
+ runner = CliRunner()
25
+ result = runner.invoke(cli.main)
26
+ assert "process-finished" in result.output
27
+ assert "import-data" in result.output
28
+ assert "import-bedfiles" in result.output
29
+ assert "import-reference-data" in result.output
30
+
31
+ # Still imports valid app
32
+ assert f"{MockApplication.ASSEMBLY} applications." in result.output
33
+
34
+ settings._DEFAULTS["INSTALLED_APPLICATIONS"] = []
tests/test_commands.py ADDED
@@ -0,0 +1,341 @@
1
+ from click.testing import CliRunner
2
+
3
+ from isabl_cli import api
4
+ from isabl_cli import commands
5
+ from isabl_cli import data
6
+ from isabl_cli import factories
7
+
8
+ from .test_app import MockApplication
9
+ from isabl_cli.test import utils
10
+
11
+
12
+ def test_commands(tmpdir):
13
+ analysis = api.create_instance(
14
+ "analyses",
15
+ project_level_analysis=factories.ProjectFactory(),
16
+ storage_url=tmpdir.strpath,
17
+ status="FINISHED",
18
+ **factories.AnalysisFactory(ran_by=None),
19
+ )
20
+
21
+ path = tmpdir.join("test.path")
22
+ path.write("not empty")
23
+
24
+ runner = CliRunner()
25
+ args = ["-fi", "pk", analysis["pk"]]
26
+ runner.invoke(commands.process_finished, args, catch_exceptions=False)
27
+ analysis = api.get_instance("analyses", analysis["pk"])
28
+
29
+ assert analysis["status"] == "SUCCEEDED"
30
+ assert analysis["storage_usage"]
31
+
32
+ args = ["--key", analysis["pk"], "--status", "STAGED"]
33
+ runner.invoke(commands.patch_status, args, catch_exceptions=False)
34
+ analysis = api.get_instance("analyses", analysis["pk"])
35
+ assert analysis["status"] == "STAGED"
36
+
37
+ args = [
38
+ "analyses",
39
+ "-fi",
40
+ "pk",
41
+ analysis["pk"],
42
+ "-f",
43
+ "pk",
44
+ "-f",
45
+ "application.name",
46
+ "-f",
47
+ "application",
48
+ "-f",
49
+ "carlos",
50
+ "-f",
51
+ "invalid.nested_attr",
52
+ ]
53
+
54
+ result = runner.invoke(commands.get_metadata, args, catch_exceptions=False)
55
+ assert analysis["application"]["name"] in result.output
56
+ assert "application.name" in result.output
57
+ assert "INVALID KEY (carlos)" in result.output
58
+ assert "INVALID KEY (nested_attr)" in result.output
59
+ result = runner.invoke(
60
+ commands.get_metadata, args + ["--json"], catch_exceptions=False
61
+ )
62
+
63
+ args = ["analyses", "-fi", "pk", analysis["pk"], "--pattern", "*.path"]
64
+ result = runner.invoke(commands.get_paths, args, catch_exceptions=False)
65
+ assert tmpdir.strpath in result.output
66
+ assert "test.path" in result.output
67
+
68
+ args = ["analyses", "-fi", "pk", analysis["pk"]]
69
+ result = runner.invoke(commands.get_paths, args, catch_exceptions=False)
70
+ assert tmpdir.strpath in result.output
71
+
72
+ args = ["analyses", "-fi", "pk", analysis["pk"]]
73
+ result = runner.invoke(commands.get_count, args, catch_exceptions=False)
74
+ assert "1" in result.output
75
+
76
+ args = ["-fi", "pk", analysis["pk"]]
77
+ result = runner.invoke(commands.get_outdirs, args, catch_exceptions=False)
78
+ assert tmpdir.strpath in result.output
79
+ result = runner.invoke(
80
+ commands.get_outdirs, args + ["--pattern", "*.path"], catch_exceptions=False
81
+ )
82
+ assert "test.path" in result.output
83
+
84
+ # use two experiments to increase coverage with project_results=
85
+ project = api.create_instance("projects", **factories.ProjectFactory())
86
+ experiment = factories.ExperimentFactory(projects=[project])
87
+ experiment["sample"]["individual"]["species"] = "HUMAN"
88
+ experiment_b = factories.ExperimentFactory(projects=[project])
89
+ experiment_b["sample"] = experiment["sample"]
90
+ analysis = utils.assert_run(
91
+ application=MockApplication(),
92
+ tuples=[
93
+ ([api.create_instance("experiments", **experiment)], []),
94
+ ([api.create_instance("experiments", **experiment_b)], []),
95
+ ],
96
+ commit=True,
97
+ project_results=["project_result_file"],
98
+ )[0]
99
+
100
+ args = ["--app-results", analysis.application.pk]
101
+ result = runner.invoke(commands.get_results, args, catch_exceptions=False)
102
+ assert "command_script" in result.output
103
+ args = ["-fi", "pk", analysis.pk, "-r", "command_script"]
104
+ result = runner.invoke(commands.get_results, args, catch_exceptions=False)
105
+ assert "head_job.sh" in result.output
106
+
107
+ args = ["-fi", "pk", analysis.pk, "--force"]
108
+ result = runner.invoke(commands.patch_results, args, catch_exceptions=False)
109
+ assert "Retrieving 1 from analyses API endpoint" in result.output
110
+
111
+
112
+ def test_get_bed():
113
+ runner = CliRunner()
114
+ technique = api.create_instance("techniques", **factories.TechniqueFactory())
115
+ args = [str(technique.pk)]
116
+ result = runner.invoke(commands.get_bed, args, catch_exceptions=False)
117
+ assert "No BED files" in result.output
118
+
119
+ api.patch_instance(
120
+ "techniques",
121
+ technique.pk,
122
+ reference_data={"test_targets_bedfile": {"url": "/hello/world"}},
123
+ )
124
+
125
+ result = runner.invoke(commands.get_bed, args, catch_exceptions=False)
126
+ assert "/hello/world" in result.output
127
+
128
+ api.patch_instance(
129
+ "techniques",
130
+ technique.pk,
131
+ reference_data={
132
+ "test_targets_bedfile": {"url": "/hello/world"},
133
+ "another_targets_bedfile": {"url": "/hello/world"},
134
+ },
135
+ )
136
+
137
+ result = runner.invoke(commands.get_bed, args, catch_exceptions=False)
138
+ assert "Multiple BEDs" in result.output
139
+
140
+
141
+ def test_get_bams():
142
+ runner = CliRunner()
143
+ experiment = api.create_instance("experiments", **factories.ExperimentFactory())
144
+ args = [str(experiment.pk)]
145
+ result = runner.invoke(commands.get_bams, args, catch_exceptions=False)
146
+ assert "No bams for" in result.output
147
+
148
+ result = runner.invoke(
149
+ commands.get_bams, args + ["--verbose"], catch_exceptions=False
150
+ )
151
+ assert experiment.system_id in result.output
152
+ assert "None" in result.output
153
+
154
+ api.patch_instance(
155
+ "experiments",
156
+ experiment.pk,
157
+ bam_files={"grch": {"url": "/hello/world", "analysis": 1}},
158
+ )
159
+
160
+ result = runner.invoke(commands.get_bams, args, catch_exceptions=False)
161
+ assert "/hello/world" in result.output
162
+
163
+ api.patch_instance(
164
+ "experiments",
165
+ experiment.pk,
166
+ bam_files={
167
+ "a1": {"url": "/hello/world", "analysis": 1},
168
+ "a2": {"url": "/hello/mars", "analysis": 2},
169
+ },
170
+ )
171
+
172
+ result = runner.invoke(commands.get_bams, args, catch_exceptions=False)
173
+ assert "Multiple bams" in result.output
174
+
175
+ result = runner.invoke(
176
+ commands.get_bams, args + ["--assembly", "a2"], catch_exceptions=False
177
+ )
178
+ assert "/hello/mars" in result.output
179
+
180
+
181
+ def test_get_data():
182
+ runner = CliRunner()
183
+ experiment = api.create_instance("experiments", **factories.ExperimentFactory())
184
+ experiment = data.update_storage_url("experiments", experiment.pk)
185
+ args = [str(experiment.pk)]
186
+ result = runner.invoke(commands.get_data, args, catch_exceptions=False)
187
+ assert "No data for" in result.output
188
+
189
+ result = runner.invoke(
190
+ commands.get_bams, args + ["--verbose"], catch_exceptions=False
191
+ )
192
+ assert experiment.system_id in result.output
193
+ assert "None" in result.output
194
+
195
+ api.patch_instance(
196
+ "experiments",
197
+ experiment.pk,
198
+ raw_data=[
199
+ {"file_url": "/hello/world", "file_type": "TXT"},
200
+ {"file_url": "/hello/mars", "file_type": "PNG"},
201
+ ],
202
+ )
203
+
204
+ result = runner.invoke(commands.get_data, args, catch_exceptions=False)
205
+ assert "/hello/world" in result.output
206
+ assert "/hello/mars" in result.output
207
+
208
+ result = runner.invoke(
209
+ commands.get_data, args + ["--dtypes", "TXT"], catch_exceptions=False
210
+ )
211
+ assert "/hello/mars" not in result.output
212
+
213
+
214
+ def test_login():
215
+ runner = CliRunner()
216
+ result = runner.invoke(commands.login, input="admin\nadmin", catch_exceptions=False)
217
+ assert "Successful authorization! Token stored" in result.output
218
+
219
+
220
+ def test_run_signals(tmpdir):
221
+ signal = "isabl_cli.data.symlink_analysis_to_targets"
222
+ runner = CliRunner()
223
+ experiment_dir = tmpdir.mkdir("experiment")
224
+ analysis = api.create_instance(
225
+ "analyses",
226
+ **factories.AnalysisFactory(
227
+ storage_url=str(tmpdir.mkdir("analysis")),
228
+ targets=[factories.ExperimentFactory(storage_url=str(experiment_dir))],
229
+ ),
230
+ )
231
+
232
+ api.patch_instance("analyses", analysis.pk, status="CREATED")
233
+ args = ["analyses", "-fi", "pk", analysis.pk, "-s", signal]
234
+ result = runner.invoke(commands.run_signals, args, catch_exceptions=False)
235
+ assert "analyses" not in str(experiment_dir.listdir())
236
+
237
+ api.patch_instance("analyses", analysis.pk, status="SUCCEEDED")
238
+ args = ["analyses", "-fi", "pk", analysis.pk, "-s", signal]
239
+ result = runner.invoke(commands.run_signals, args, catch_exceptions=False)
240
+ assert analysis.application.name.lower() in str(
241
+ experiment_dir.join("analyses").listdir()
242
+ )
243
+
244
+
245
+ def test_run_web_signals():
246
+ application = MockApplication().application
247
+ analysis = api.create_instance(
248
+ "analyses",
249
+ **factories.AnalysisFactory(
250
+ targets=[factories.ExperimentFactory()], application=application
251
+ ),
252
+ )
253
+
254
+ api.create_instance(
255
+ "signals",
256
+ import_string="isabl_cli.signals.resume_analysis_signal",
257
+ target_endpoint="analyses",
258
+ target_id=analysis.pk,
259
+ )
260
+
261
+ runner = CliRunner()
262
+ args = ["-fi", "target_id", analysis.pk, "-fi", "target_endpoint", "analyses"]
263
+ result = runner.invoke(commands.run_web_signals, args, catch_exceptions=False)
264
+ assert str(analysis.pk) in result.output
265
+ assert "SUCCEEDED" in result.output
266
+
267
+ api.create_instance(
268
+ "signals",
269
+ import_string="isabl_cli.signals.force_analysis_signal",
270
+ target_endpoint="analyses",
271
+ target_id=analysis.pk,
272
+ )
273
+
274
+ result = runner.invoke(commands.run_web_signals, args, catch_exceptions=False)
275
+ assert str(analysis.pk) in result.output
276
+ assert "SUCCEEDED" in result.output
277
+
278
+ # increase coverage on get_result
279
+ assert MockApplication().get_result(
280
+ experiment=api.get_experiments([analysis.targets[0].pk])[0],
281
+ application_key=application.pk,
282
+ result_key="analysis_result_key",
283
+ application_name=str(MockApplication),
284
+ )
285
+
286
+
287
+ def test_process_finished_tags(tmpdir):
288
+ """Check that a tagged analysis does NOT get processed to FINISHED."""
289
+ analysis = api.create_instance(
290
+ "analyses",
291
+ project_level_analysis=factories.ProjectFactory(),
292
+ storage_url=tmpdir.strpath,
293
+ status="FINISHED",
294
+ **factories.AnalysisFactory(ran_by=None, tags=[{"name":"PROCESSING FINISHED"}]),
295
+ )
296
+ runner = CliRunner()
297
+ args = ["-fi", "pk", analysis["pk"]]
298
+ runner.invoke(commands.process_finished, args, catch_exceptions=False)
299
+ analysis = api.get_instance("analyses", analysis["pk"])
300
+ assert analysis["status"] == "FINISHED"
301
+
302
+ # strip the tags, check that is DOES get processed
303
+ api.patch_instance("analyses", analysis.pk,
304
+ **factories.AnalysisFactory(ran_by=None, tags=[]))
305
+ runner.invoke(commands.process_finished, args, catch_exceptions=False)
306
+ analysis = api.get_instance("analyses", analysis["pk"])
307
+ assert analysis["status"] == "SUCCEEDED"
308
+
309
+
310
+ def test_force_process_finished_tags(tmpdir):
311
+ """Check that a tagged analysis does get processed to FINISHED when forced."""
312
+ analysis = api.create_instance(
313
+ "analyses",
314
+ project_level_analysis=factories.ProjectFactory(),
315
+ storage_url=tmpdir.strpath,
316
+ status="FINISHED",
317
+ **factories.AnalysisFactory(ran_by=None, tags=[{"name":"PROCESSING FINISHED"}]),
318
+ )
319
+ runner = CliRunner()
320
+ args = ["-fi", "pk", analysis["pk"], "--force"]
321
+ result = runner.invoke(commands.process_finished, args, catch_exceptions=True)
322
+ print(result.output)
323
+ analysis = api.get_instance("analyses", analysis["pk"])
324
+ assert analysis["status"] == "SUCCEEDED"
325
+
326
+
327
+ def test_rejected_analysis(tmpdir):
328
+ """Check an analysis can be rejected using the command cli."""
329
+ analysis = api.create_instance(
330
+ "analyses",
331
+ status="FINISHED",
332
+ storage_url=tmpdir.strpath,
333
+ **factories.AnalysisFactory(ran_by=None),
334
+ )
335
+ runner = CliRunner()
336
+ rejection_reason = "This analysis is FAKE."
337
+ args = ["--key", analysis["pk"], "--reason", rejection_reason]
338
+ runner.invoke(commands.reject_analysis, args, catch_exceptions=False)
339
+ analysis = api.get_instance("analyses", analysis["pk"])
340
+ assert analysis["status"] == "REJECTED"
341
+ assert analysis["notes"] == rejection_reason