-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlumoscli.py
544 lines (476 loc) · 17.4 KB
/
lumoscli.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
'''
Lumos command line interface module
'''
import os
import sys
import tempfile
from pathlib import Path
from pkg_resources import resource_string
import click
from art import text2art
from lumos.config import (
load_config_string,
load_config_file
)
from lumos import logger
from lumos.generator import (
render_single_channel_plateview,
render_single_plate_plateview,
render_single_run_plateview,
render_single_plate_plateview_parallelism,
)
from lumos.picasso import picasso_generate_plate_image
# Find the OS temporary directory location
default_temp_directory = tempfile.gettempdir()
# Define general constant parameters
output_file_format_list = ['jpg', 'jpeg', 'png']
# Define global variable for config in this file
config = None
# Setup command group
@click.group(invoke_without_command=True)
@click.pass_context
@click.option(
'-cf',
"--config-file",
type=click.Path(exists=True),
help="Specify a custom configuration file to be used by Lumos.",
)
@click.option(
'-gcf',
"--generate-config-file",
type=click.Path(),
help='''Create a template for a Lumos configuration file that can
then be customized.''',
)
def cli(ctx, config_file, generate_config_file):
'''
\b
Welcome to Lumos, a plate image visualization tool!
Please choose an operation mode (i.e. command) from the list below
and type 'lumos <mode> --help' to get started.
Alternatively, to use a custom configuration file, type 'lumos -cf <path_to_config> <mode> --help'.
\b
'''
# Print lumos header
header_ascii_art = text2art("Lumos", font="big")
click.echo(header_ascii_art)
if generate_config_file is not None:
# If the given path is a folder create a new file, otherwise overwrite the existing file
if os.path.isdir(generate_config_file):
generate_config_file = generate_config_file + '/lumos_config.yaml'
# Copy content of default config file
with open(generate_config_file, 'wb') as f:
default_config_bytes = resource_string(
__name__, 'lumos/default_lumos_config.yaml')
f.write(default_config_bytes)
click.echo(
"The template for the configuration file has been created in "+generate_config_file)
sys.exit(0)
global config
if config_file is not None:
# Load the specified config file
config = load_config_file(config_file)
else:
# Load the default config bundled with lumos
default_config_string = resource_string(
__name__, 'lumos/default_lumos_config.yaml').decode('utf-8')
config = load_config_string(default_config_string)
if ctx.invoked_subcommand is None:
click.secho("Type 'lumos --help' to get started!", fg='bright_blue')
# ----------------------------- QUALITY CONTROL ----------------------------- #
# Setup QC mode
@cli.command(
name="qc",
help="Quality Control mode",
)
@click.option(
'-s',
"--scope",
type=click.Choice(["run", "plate", "channel"], case_sensitive=False),
required=True,
help="Choose if you want to generate a plateview for a single channel, a plate or a whole run",
)
@click.option(
'-c',
"--channel",
type=click.STRING,
help="If scope is 'channel', choose which single channel to render.",
)
@click.option(
'-sp',
"--source-path",
type=click.Path(exists=True),
required=True,
help="Folder of your run or single plate.",
)
@click.option(
'-op',
"--output-path",
type=click.Path(exists=True),
required=True,
help="Folder where images will be outputted.",
)
@click.option(
'-tp',
"--temp-path",
type=click.Path(exists=True),
default=default_temp_directory,
show_default=True,
help="Path to the temporary working folder.",
)
@click.option(
'-f',
"--output-format",
type=click.Choice(output_file_format_list,
case_sensitive=False),
help="File format of the generated images. You can choose the default one in the configuration file.",
)
@click.option(
'-b',
"--brightfield",
type=click.STRING,
help="Choose a brightfield channel to include in the plate/run render. To render all of them, type 'all'.",
)
@click.option(
'-p',
"--parallelism",
type=click.INT,
default=1,
show_default=True,
help="Choose the number of CPU cores on which to perform parallel computation of different channels.",
)
@click.option(
'-k',
"--keep-temp-files",
is_flag=True,
help="(dev only) Choose if temporary files should be kept instead of copying again the source files.",
hidden=True,
# Note: This option is hidden from the --help menu as it is only for debugging purposes.
)
@click.option(
"--disable-logs",
is_flag=True,
help="(dev only) Disable the logger and some visual elements in the console (e.g. progress bars). Useful when running tests with pytest as the logger gets printed to the console.",
hidden=True,
# Note: This option is hidden from the --help menu as it is only for debugging purposes.
)
def quality_control(scope, channel, source_path, output_path, temp_path, output_format, brightfield, parallelism, keep_temp_files, disable_logs):
'''
Quality Control operation mode - CLI entry
'''
if config is None:
click.secho(
"CONFIG ERROR: A configuration could not be resolved. This should never happen. Please seek assistance.", fg='bright_red', bold=True)
sys.exit(1)
is_in_parallel = (parallelism != 1)
# create logger
logger.setup(temp_path, enabled=not disable_logs,
parallelism=is_in_parallel)
# announce startup to logger
logger.info("Started - Quality Control")
# decode arguments
if is_in_parallel:
if parallelism < 1:
click.secho(
"CLI ERROR: '--parallelism' argument cannot be less than 1. Please remove it or change its value.", fg='bright_red', bold=True)
sys.exit(1)
click.secho(
"CLI WARNING: When using parallelism, pressing [CTRL+C] might not terminate the program. To halt the execution of the program before it finishes, you have to close your terminal.", fg='bright_yellow')
click.secho(
"CLI INFO: When using parallelism, less progress information will be printed in the terminal.", fg='bright_magenta')
click.echo()
if keep_temp_files:
click.secho("CLI Development Note: Keeping previously downloaded temporary files (remove '--keep-temp-files' to regenerate them every time)", fg='bright_blue')
logger.info(
"Argument '--keep-temp-files' used from CLI: keeping previously downloaded temporary files")
if output_format is None:
output_format = config['default_output_format']
# decode scope
if scope == "channel":
if not channel:
click.secho(
"CLI ERROR: Missing channel. Please define a channel using '--channel' <channel_name>.", fg='bright_red', bold=True)
sys.exit(1)
if channel not in list(config['channel_info'].keys()):
click.secho(
f"CLI ERROR: Wrong channel chosen. Please choose one amongst {list(config['channel_info'].keys())}.", fg='bright_red', bold=True)
sys.exit(1)
if is_in_parallel:
click.secho(
"CLI WARNING: The '--parallelism' argument has no effect on performance when scope is 'channel'. Consider removing it.", fg='bright_yellow')
if brightfield is not None:
click.secho("CLI WARNING: The '--brightfield' argument has no effect when scope is 'channel' and should not be specified. Consider removing it.", fg='bright_yellow')
click.echo()
# get platename
plate_name = Path(source_path).name
click.echo(
"Process plate: "
+ plate_name,
)
# get channel name
channel_name = config['channel_info'][channel]['name']
click.echo(
"Render channel: "
+ channel
+ " - "
+ channel_name,
)
# render the channel for the plate
render_single_channel_plateview(
config,
source_path,
plate_name,
channel,
channel_name,
output_path,
temp_path,
output_format,
keep_temp_files,
)
else:
if channel:
click.secho(
"CLI ERROR: '--channel' argument must not be used for run/plate scope. Please remove it.", fg='bright_red', bold=True)
sys.exit(1)
channels_to_render = config['default_channels_to_render'].copy()
if brightfield is None:
click.secho(
"CLI Note: Generating render for NONE of the brightfield channels.", fg='bright_blue')
elif brightfield == "all":
channels_to_render = channels_to_render + \
config['brightfield_channels']
click.secho(
"CLI Note: Generating renders for ALL brightfield channels.", fg='bright_blue')
elif brightfield not in config['brightfield_channels']:
click.secho(
f"CLI ERROR: Wrong brightfield channel chosen. Please choose one amongst {config['brightfield_channels']}.", fg='bright_red', bold=True)
sys.exit(1)
else:
channels_to_render.append(brightfield)
click.secho(
f"CLI Note: Generating render ONLY for brightfield channel {brightfield}.",
fg='bright_blue')
click.echo(os.linesep)
# execute image generation according to the scope
if scope == "run":
# get run name
run_name = Path(source_path)
# get plates and their path, only if files in it
plate_list = list(Path(source_path).glob(
f"./{config['path_from_run_folder_to_plates']}/*"))
# create a dict with plate name as key and plate folder path as value
# only folders with tif images are eligible
plate_dict = {
x.name: x
for x in plate_list
if (x.is_dir() and len(list(x.glob(f"./{config['path_from_plate_folder_to_images']}/*.tif*"))))
}
if len(plate_dict) == 0:
click.secho("ERROR: No valid plates found in run folder.",
color='bright_red')
click.secho(" Make sure that your folder structure matches your configuration. "
+ f"The current configuration is: <run_folder>/{config['path_from_run_folder_to_plates']}"
+ f"<plate_folders>/{config['path_from_plate_folder_to_images']}<images>",
color='bright_red')
logger.error("No valid plates found in run folder")
sys.exit(1)
click.echo(
"Lumos will process "
+ str(len(plate_dict))
+ " plate folders from run: "
+ str(run_name)
)
click.echo("Plates: " + str(list(plate_dict.keys())))
click.echo(
"Channels being rendered: "
+ str(channels_to_render)
+ os.linesep
+ os.linesep
)
# render all the plates of the run
render_single_run_plateview(
config,
plate_dict,
channels_to_render,
output_path,
temp_path,
output_format,
parallelism,
keep_temp_files,
)
elif scope == "plate":
# get platename
plate_name = Path(source_path).name
click.echo(
"Process plate '"
+ plate_name
+ "' and render channels: "
+ str(channels_to_render),
)
# render all the channels of the plate
if not is_in_parallel:
render_single_plate_plateview(
config,
source_path,
plate_name,
channels_to_render,
output_path,
temp_path,
output_format,
keep_temp_files,
)
else:
render_single_plate_plateview_parallelism(
config,
source_path,
plate_name,
channels_to_render,
output_path,
temp_path,
output_format,
parallelism,
keep_temp_files,
)
# announce stop to logger
logger.info("Stopped - Quality Control")
# Successfully terminate
sys.exit(0)
# ----------------------------- CELL PAINTING ----------------------------- #
# define cellpainter command
@cli.command(
name="cp",
help="Cell Painting mode",
)
@click.option(
'-s',
"--scope",
type=click.Choice(["plate", "wells", "sites"], case_sensitive=False),
required=True,
help="Choose if you want to generate a cellpainted image for a whole plate, each well, or each individual site.",
)
@click.option(
'-w',
"--single-well",
type=click.STRING,
help="If scope is 'wells' or 'plate', this allows you to generate the image for only one well.",
)
@click.option(
'-sp',
"--source-path",
type=click.Path(exists=True),
required=True,
help="Folder of your plate images.",
)
@click.option(
'-op',
"--output-path",
type=click.Path(exists=True),
required=True,
help="Folder where images will be outputted.",
)
@click.option(
'-tp',
"--temp-path",
type=click.Path(exists=True),
default=default_temp_directory,
show_default=True,
help="Path to the temporary working folder.",
)
@click.option(
'-pp',
"--platemap-path",
type=click.Path(exists=True),
help='''Path to the platemap file of your plate.
This allows the identifier of each compound to be displayed on the well images.
You can choose which columns to parse in the configuration file.''',
)
@click.option(
'-f',
"--output-format",
type=click.Choice(output_file_format_list,
case_sensitive=False),
help="File format of the generated images. You can choose the default one in the configuration file.",
)
@click.option(
"--style",
type=click.STRING,
default='classic',
show_default=True,
help='''Rendering style of the output image.
You can see and customize the available styles in the configuration file.''',
)
@click.option(
"--disable-logs",
is_flag=True,
help="(dev only) Disable the logger. Useful when running tests with pytest as the logger gets printed to the console.",
hidden=True,
# Note: This option is hidden from the --help menu as it is only for debugging purposes.
)
def cell_painting(scope, single_well, source_path, output_path, temp_path, platemap_path, output_format, style, disable_logs):
'''
Cell Painting operation mode - CLI entry
'''
# create logger
logger.setup(temp_path, enabled=not disable_logs, parallelism=False)
# announce startup to logger
logger.info("Started - Cell Painting")
# get platename
plate_name = Path(source_path).name
if style not in list(config['fingerprint_style_dict'].keys()):
click.secho(
f"CLI ERROR: The chosen style does not exist. Please choose one amongst {list(config['fingerprint_style_dict'].keys())}.",
fg='bright_red', bold=True
)
sys.exit(1)
if output_format is None:
output_format = config['default_output_format']
if platemap_path is not None and scope == 'sites':
click.secho("CLI WARNING: The '--platemap-path' argument has no effect when scope is 'sites' and should not be specified. Consider removing it.", fg='bright_yellow')
click.echo()
if not single_well:
click.echo(
"Process wells of plate: "
+ plate_name
+ " and multiplex cell painting channels "
+ str(config['cp_channels_to_use'] if style ==
'classic' else list(config['channel_info'].keys())[:5])
)
elif scope != 'plate':
click.echo(
"Process well "
+ single_well
+ " of plate: "
+ plate_name
+ " and multiplex cell painting channels "
+ str(config['cp_channels_to_use'] if style ==
'classic' else list(config['channel_info'].keys())[:5])
)
else:
logger.error(
"'-w/--single-well' argument used while in scope 'plate'.")
click.secho(
"CLI ERROR: Used '-w/--single-well' argument while using scope 'plate'. Those two arguments are incompatible.",
fg='bright_red', bold=True
)
sys.exit(1)
# multiplex the channels of the plate (not brightfield) into a single RGB image
picasso_generate_plate_image(
config,
source_path,
plate_name,
output_path,
temp_path,
platemap_path,
output_format,
style,
scope,
single_well,
True,
)
# announce stop to logger
logger.info("Stopped - Cell Painting")
# Successfully terminate
sys.exit(0)
if __name__ == "__main__":
# use Click
cli()