-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy path__main__.py
More file actions
964 lines (905 loc) · 30.9 KB
/
Copy path__main__.py
File metadata and controls
964 lines (905 loc) · 30.9 KB
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
import json
from collections.abc import Callable
from pathlib import Path
from typing import Any, Literal
import click
from spatialdata_io._constants._constants import VisiumKeys
from spatialdata_io.readers.generic import VALID_IMAGE_TYPES, VALID_SHAPE_TYPES
@click.group()
def cli() -> None:
"""Convert standard technology data formats to SpatialData object.
Usage:
python -m spatialdata_io <Command> -i <input> -o <output>
For help on how to use a specific command, run:
python -m spatialdata_io <Command> --help
"""
def _input_output_click_options(func: Callable[..., None]) -> Callable[..., None]:
"""Decorator to add input and output options to a Click command."""
func = click.option(
"--input",
"-i",
type=click.Path(exists=True, file_okay=False, dir_okay=True),
help="Path to the input file.",
required=True,
)(func)
func = click.option(
"--output", "-o", type=click.Path(exists=False), help="Path to the output file.", required=True
)(func)
return func
def _parse_json_param(value: str, param_name: str) -> dict[str, Any]:
try:
result: dict[str, Any] = json.loads(value)
return result
except json.JSONDecodeError as e:
raise click.BadParameter(f"Invalid JSON for {param_name!r}: {e}") from e
@cli.command(name="codex")
@_input_output_click_options
@click.option(
"--fcs",
type=bool,
default=True,
help="Whether the .fcs file is provided if False a .csv file is expected. [default: True]",
)
@click.option(
"--imread-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to imread. [default: {}]",
)
def codex_wrapper(
input: str,
output: str,
fcs: bool = True,
imread_kwargs: str = "{}",
) -> None:
"""Codex conversion to SpatialData."""
from spatialdata_io.readers.codex import codex
sdata = codex(input, fcs=fcs, imread_kwargs=_parse_json_param(imread_kwargs, "imread_kwargs"))
sdata.write(output)
@cli.command(name="cosmx")
@_input_output_click_options
@click.option("--dataset-id", type=str, default=None, help="Name of the dataset [default: None]")
@click.option("--transcripts", type=bool, default=True, help="Whether to load transcript information. [default: True]")
@click.option(
"--imread-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to imread. [default: {}]",
)
@click.option(
"--image-models-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to Image2DModel. [default: {}]",
)
def cosmx_wrapper(
input: str,
output: str,
dataset_id: str | None = None,
transcripts: bool = True,
imread_kwargs: str = "{}",
image_models_kwargs: str = "{}",
) -> None:
"""Cosmic conversion to SpatialData."""
from spatialdata_io.readers.cosmx import cosmx
sdata = cosmx(
input,
dataset_id=dataset_id,
transcripts=transcripts,
imread_kwargs=_parse_json_param(imread_kwargs, "imread_kwargs"),
image_models_kwargs=_parse_json_param(image_models_kwargs, "image_models_kwargs"),
)
sdata.write(output)
@cli.command(name="curio")
@_input_output_click_options
def curio_wrapper(input: str, output: str) -> None:
"""Curio conversion to SpatialData."""
from spatialdata_io.readers.curio import curio
sdata = curio(input)
sdata.write(output)
@cli.command(name="dbit")
@_input_output_click_options
@click.option(
"--anndata-path",
type=click.Path(exists=True),
default=None,
help="Path to the counts and metadata file. [default: None]",
)
@click.option(
"--barcode-position",
type=click.Path(exists=True),
default=None,
help="Path to the barcode coordinates file. [default: None]",
)
@click.option("--image-path", type=str, default=None, help="Path to the low resolution image file. [default: None]")
@click.option("--dataset-id", type=str, default=None, help="Dataset ID. [default: None]")
@click.option("--border", type=bool, default=True, help="Value pass internally to _xy2edges. [default: True]")
@click.option("--border-scale", type=float, default=1, help="The factor by which the border is scaled. [default: 1]")
def dbit_wrapper(
input: str,
output: str,
anndata_path: str | None = None,
barcode_position: str | None = None,
image_path: str | None = None,
dataset_id: str | None = None,
border: bool = True,
border_scale: float = 1,
) -> None:
"""Conversion of DBit-seq to SpatialData."""
from spatialdata_io.readers.dbit import dbit
sdata = dbit(
input,
anndata_path=anndata_path,
barcode_position=barcode_position,
image_path=image_path,
dataset_id=dataset_id,
border=border,
border_scale=border_scale,
)
sdata.write(output)
@cli.command(name="iss")
@_input_output_click_options
@click.option(
"--raw-relative-path", type=click.Path(exists=True), required=True, help="Relative path to raw raster image file."
)
@click.option(
"--labels-relative-path", type=click.Path(exists=True), required=True, help="Relative path to label image file."
)
@click.option(
"--h5ad-relative-path",
type=click.Path(exists=True),
required=True,
help="Relative path to counts and metadata file.",
)
@click.option(
"--instance-key",
type=str,
default=None,
help="Which column of the AnnData table contains the CellID. [default: None]",
)
@click.option("--dataset-id", type=str, default="region", help="Dataset ID [default: region]")
@click.option(
"--multiscale-image",
type=bool,
default=True,
help="Whether to process the image into a multiscale image [default: True]",
)
@click.option(
"--multiscale-labels",
type=bool,
default=True,
help="Whether to process the label image into a multiscale image [default: True]",
)
@click.option(
"--imread-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to imread. [default: {}]",
)
@click.option(
"--image-models-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to Image2DModel. [default: {}]",
)
@click.option(
"--labels-models-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to Labels2DModel. [default: {}]",
)
def iss_wrapper(
input: str,
output: str,
raw_relative_path: Path,
labels_relative_path: Path,
h5ad_relative_path: Path,
instance_key: str | None = None,
dataset_id: str = "region",
multiscale_image: bool = True,
multiscale_labels: bool = True,
imread_kwargs: str = "{}",
image_models_kwargs: str = "{}",
labels_models_kwargs: str = "{}",
) -> None:
"""ISS conversion to SpatialData."""
from spatialdata_io.readers.iss import iss
sdata = iss(
input,
raw_relative_path,
labels_relative_path,
h5ad_relative_path,
instance_key=instance_key,
dataset_id=dataset_id,
multiscale_image=multiscale_image,
multiscale_labels=multiscale_labels,
imread_kwargs=_parse_json_param(imread_kwargs, "imread_kwargs"),
image_models_kwargs=_parse_json_param(image_models_kwargs, "image_models_kwargs"),
labels_models_kwargs=_parse_json_param(labels_models_kwargs, "labels_models_kwargs"),
)
sdata.write(output)
@cli.command(name="mcmicro")
@click.option(
"--input", "-i", type=click.Path(exists=True), help="Path to the mcmicro project directory.", required=True
)
@click.option("--output", "-o", type=click.Path(), help="Path to the output.zarr file.", required=True)
@click.option(
"--imread-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to imread. [default: {}]",
)
@click.option(
"--image-models-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to Image2DModel. [default: {}]",
)
@click.option(
"--labels-models-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to Labels2DModel. [default: {}]",
)
def mcmicro_wrapper(
input: str,
output: str,
imread_kwargs: str = "{}",
image_models_kwargs: str = "{}",
labels_models_kwargs: str = "{}",
) -> None:
"""Conversion of MCMicro to SpatialData."""
from spatialdata_io.readers.mcmicro import mcmicro
sdata = mcmicro(
input,
imread_kwargs=_parse_json_param(imread_kwargs, "imread_kwargs"),
image_models_kwargs=_parse_json_param(image_models_kwargs, "image_models_kwargs"),
labels_models_kwargs=_parse_json_param(labels_models_kwargs, "labels_models_kwargs"),
)
sdata.write(output)
@cli.command(name="merscope")
@_input_output_click_options
@click.option(
"--vpt-outputs",
type=click.Path(exists=True),
default=None,
help="Optional argument to specify the path to the Vizgen postprocessing tool. [default: None]",
)
@click.option("--z-layers", type=int, default=3, help="Indices of the z-layers to consider. [default: 3]")
@click.option("--region-name", type=str, default=None, help="Name of the ROI. [default: None]")
@click.option("--slide-name", type=str, default=None, help="Name of the slide/run [default: None]")
@click.option(
"--backend",
type=click.Choice(["dask_image", "rioxarray"]),
default=None,
help="Either 'dask_image' or 'rioxarray'. [default: None]",
)
@click.option("--transcripts", type=bool, default=True, help="Whether to read transcripts. [default: True]")
@click.option("--cells-boundaries", type=bool, default=True, help="Whether to read cells boundaries. [default: True]")
@click.option("--cells-table", type=bool, default=True, help="Whether to read cells table. [default: True]")
@click.option("--mosaic-images", type=bool, default=True, help="Whether to read the mosaic images. [default: True]")
@click.option(
"--imread-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to imread. [default: {}]",
)
@click.option(
"--image-models-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to Image2DModel. [default: {}]",
)
def merscope_wrapper(
input: str,
output: str,
vpt_outputs: Path | str | dict[str, Any] | None = None,
z_layers: int | list[int] | None = 3,
region_name: str | None = None,
slide_name: str | None = None,
backend: Literal["dask_image", "rioxarray"] | None = None,
transcripts: bool = True,
cells_boundaries: bool = True,
cells_table: bool = True,
mosaic_images: bool = True,
imread_kwargs: str = "{}",
image_models_kwargs: str = "{}",
) -> None:
"""Merscope conversion to SpatialData."""
from spatialdata_io.readers.merscope import merscope
sdata = merscope(
input,
vpt_outputs=vpt_outputs,
z_layers=z_layers,
region_name=region_name,
slide_name=slide_name,
backend=backend,
transcripts=transcripts,
cells_boundaries=cells_boundaries,
cells_table=cells_table,
mosaic_images=mosaic_images,
imread_kwargs=_parse_json_param(imread_kwargs, "imread_kwargs"),
image_models_kwargs=_parse_json_param(image_models_kwargs, "image_models_kwargs"),
)
sdata.write(output)
@cli.command(name="seqfish")
@_input_output_click_options
@click.option("--load-images", type=bool, default=True, help="Whether to load images. [default: True]")
@click.option("--load-labels", type=bool, default=True, help="Whether to load labels. [default: True]")
@click.option("--load-points", type=bool, default=True, help="Whether to load points. [default: True]")
@click.option("--load-shapes", type=bool, default=True, help="Whether to load shapes. [default: True]")
@click.option("--cells-as-circles", type=bool, default=False, help="Whether to read cells as circles. [default: False]")
@click.option(
"--rois",
type=str,
multiple=True,
default=None,
help="Which sections to load. Provide one or more ROI identifiers. [default: All sections are loaded]",
)
@click.option(
"--raster-models-scale-factors",
type=int,
multiple=True,
default=None,
help="Scale factors for raster models. [default: None]",
)
@click.option(
"--imread-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to imread. [default: {}]",
)
def seqfish_wrapper(
input: str,
output: str,
load_images: bool = True,
load_labels: bool = True,
load_points: bool = True,
load_shapes: bool = True,
cells_as_circles: bool = False,
rois: list[str] | None = None,
raster_models_scale_factors: list[int] | None = None,
imread_kwargs: str = "{}",
) -> None:
"""Seqfish conversion to SpatialData."""
from spatialdata_io.readers.seqfish import seqfish
sdata = seqfish(
input,
load_images=load_images,
load_labels=load_labels,
load_points=load_points,
load_shapes=load_shapes,
cells_as_circles=cells_as_circles,
rois=list(rois) if rois else None,
raster_models_scale_factors=list(raster_models_scale_factors) if raster_models_scale_factors else None,
imread_kwargs=_parse_json_param(imread_kwargs, "imread_kwargs"),
)
sdata.write(output)
@cli.command(name="steinbock")
@_input_output_click_options
@click.option(
"--labels-kind",
type=click.Choice(["deepcell", "ilastik"]),
default="deepcell",
help="What kind of labels to use. [default: 'deepcell']",
)
@click.option(
"--imread-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to imread. [default: {}]",
)
@click.option(
"--image-models-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to Image2DModel. [default: {}]",
)
def steinbock_wrapper(
input: str,
output: str,
labels_kind: Literal["deepcell", "ilastik"] = "deepcell",
imread_kwargs: str = "{}",
image_models_kwargs: str = "{}",
) -> None:
"""Steinbock conversion to SpatialData."""
from spatialdata_io.readers.steinbock import steinbock
sdata = steinbock(
input,
labels_kind=labels_kind,
imread_kwargs=_parse_json_param(imread_kwargs, "imread_kwargs"),
image_models_kwargs=_parse_json_param(image_models_kwargs, "image_models_kwargs"),
)
sdata.write(output)
@cli.command(name="stereoseq")
@_input_output_click_options
@click.option("--dataset-id", type=str, default=None, help="Dataset ID. [default: None]")
@click.option(
"--read-square-bin",
type=bool,
default=True,
help="If True, will read the square bin ``{xx.GEF_FILE!r}`` file and build corresponding points element. [default: True]",
)
@click.option(
"--optional-tif", type=bool, default=False, help="If True, will read ``{xx.TISSUE_TIF!r}`` files. [default: False]"
)
@click.option(
"--imread-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to imread. [default: {}]",
)
@click.option(
"--image-models-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to Image2DModel. [default: {}]",
)
def stereoseq_wrapper(
input: str,
output: str,
dataset_id: str | None = None,
read_square_bin: bool = True,
optional_tif: bool = False,
imread_kwargs: str = "{}",
image_models_kwargs: str = "{}",
) -> None:
"""Stereoseq conversion to SpatialData."""
from spatialdata_io.readers.stereoseq import stereoseq
sdata = stereoseq(
input,
dataset_id=dataset_id,
read_square_bin=read_square_bin,
optional_tif=optional_tif,
imread_kwargs=_parse_json_param(imread_kwargs, "imread_kwargs"),
image_models_kwargs=_parse_json_param(image_models_kwargs, "image_models_kwargs"),
)
sdata.write(output)
@cli.command(name="visium")
@_input_output_click_options
@click.option("--dataset-id", type=str, default=None, help="Dataset ID. [default: None]")
@click.option(
"--counts-file",
type=str,
default=VisiumKeys.FILTERED_COUNTS_FILE,
help="Name of the counts file, defaults to ``{vx.FILTERED_COUNTS_FILE!r}``. [default: None]",
)
@click.option(
"--fullres-image-file",
type=click.Path(exists=True),
default=None,
help="Path to the full resolution image. [default: None]",
)
@click.option(
"--tissue-positions-file",
type=click.Path(exists=True),
default=None,
help="Path to the tissue positions file. [default: None]",
)
@click.option(
"--scalefactors-file",
type=click.Path(exists=True),
default=None,
help="Path to the scalefactors file. [default: None]",
)
@click.option(
"--var-names-make-unique",
type=bool,
default=True,
help="Whether to make variable names unique. [default: True]",
)
@click.option(
"--imread-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to imread. [default: {}]",
)
@click.option(
"--image-models-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to Image2DModel. [default: {}]",
)
def visium_wrapper(
input: str,
output: str,
dataset_id: str | None = None,
counts_file: str = VisiumKeys.FILTERED_COUNTS_FILE,
fullres_image_file: str | Path | None = None,
tissue_positions_file: str | Path | None = None,
scalefactors_file: str | Path | None = None,
var_names_make_unique: bool = True,
imread_kwargs: str = "{}",
image_models_kwargs: str = "{}",
) -> None:
"""Visium conversion to SpatialData."""
from spatialdata_io.readers.visium import visium
sdata = visium(
input,
dataset_id=dataset_id,
counts_file=counts_file,
fullres_image_file=fullres_image_file,
tissue_positions_file=tissue_positions_file,
scalefactors_file=scalefactors_file,
var_names_make_unique=var_names_make_unique,
imread_kwargs=_parse_json_param(imread_kwargs, "imread_kwargs"),
image_models_kwargs=_parse_json_param(image_models_kwargs, "image_models_kwargs"),
)
sdata.write(output)
@cli.command(name="visium-hd")
@_input_output_click_options
@click.option("--dataset-id", type=str, default=None, help="Dataset ID. [default: None]")
@click.option(
"--filtered-counts-file",
type=bool,
default=True,
help="It sets the value of `counts_file` to ``{vx.FILTERED_COUNTS_FILE!r}`` (when `True`) or to``{vx.RAW_COUNTS_FILE!r}`` (when `False`). [default: True]",
)
@click.option(
"--bin-size",
type=int,
multiple=True,
default=None,
help="When specified, load the data of a specific bin size, or a list of bin sizes. By default, it loads all the available bin sizes. [default: None]",
)
@click.option(
"--bins-as-squares",
type=bool,
default=True,
help="If true, bins are represented as squares otherwise as circles. [default: True]",
)
@click.option(
"--fullres-image-file",
type=click.Path(exists=True),
default=None,
help="Path to the full resolution image. [default: None]",
)
@click.option(
"--load-all-images",
type=bool,
default=False,
help="If `False`, load only the full resolution, high resolution, and low resolution images. If `True`, also the following images: ``{vx.IMAGE_CYTASSIST!r}``. [default: False]",
)
@click.option(
"--annotate-table-by-labels",
type=bool,
default=False,
help="If true, annotates the table by labels. [default: False]",
)
@click.option(
"--load-segmentations-only",
type=bool,
default=None,
help="If `True`, only the segmented cell boundaries and their associated counts will be loaded. All binned data will be skipped. [default: None, which will fall back to `False` with a deprecation warning]",
)
@click.option(
"--load-nucleus-segmentations",
type=bool,
default=False,
help="If `True` and nucleus segmentation files are present, load nucleus segmentation polygons and the corresponding nucleus-filtered count table. [default: False]",
)
@click.option(
"--var-names-make-unique",
type=bool,
default=True,
help="Whether to make variable names unique. [default: True]",
)
@click.option(
"--gex-only",
type=bool,
default=False,
help="If `True`, only load gene expression features. [default: False]",
)
@click.option(
"--imread-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to imread. [default: {}]",
)
@click.option(
"--image-models-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to Image2DModel. [default: {}]",
)
@click.option(
"--anndata-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to anndata. [default: {}]",
)
def visium_hd_wrapper(
input: str,
output: str,
dataset_id: str | None = None,
filtered_counts_file: bool = True,
load_segmentations_only: bool | None = None,
load_nucleus_segmentations: bool = False,
bin_size: int | list[int] | None = None,
bins_as_squares: bool = True,
fullres_image_file: str | Path | None = None,
load_all_images: bool = False,
annotate_table_by_labels: bool = False,
var_names_make_unique: bool = True,
gex_only: bool = False,
imread_kwargs: str = "{}",
image_models_kwargs: str = "{}",
anndata_kwargs: str = "{}",
) -> None:
"""Visium HD conversion to SpatialData."""
from spatialdata_io.readers.visium_hd import visium_hd
sdata = visium_hd(
path=input,
dataset_id=dataset_id,
filtered_counts_file=filtered_counts_file,
load_segmentations_only=load_segmentations_only,
load_nucleus_segmentations=load_nucleus_segmentations,
bin_size=bin_size,
bins_as_squares=bins_as_squares,
fullres_image_file=fullres_image_file,
load_all_images=load_all_images,
annotate_table_by_labels=annotate_table_by_labels,
var_names_make_unique=var_names_make_unique,
gex_only=gex_only,
imread_kwargs=_parse_json_param(imread_kwargs, "imread_kwargs"),
image_models_kwargs=_parse_json_param(image_models_kwargs, "image_models_kwargs"),
anndata_kwargs=_parse_json_param(anndata_kwargs, "anndata_kwargs"),
)
sdata.write(output)
@cli.command(name="xenium")
@_input_output_click_options
@click.option("--cells-boundaries", type=bool, default=True, help="Whether to read cells boundaries. [default: True]")
@click.option(
"--nucleus-boundaries", type=bool, default=True, help="Whether to read Nucleus boundaries. [default: True]"
)
@click.option("--cells-as-circles", type=bool, default=False, help="Whether to read cells as circles. [default: False]")
@click.option("--cells-labels", type=bool, default=True, help="Whether to read cells labels (raster). [default: True]")
@click.option(
"--nucleus-labels", type=bool, default=True, help="Whether to read nucleus labels (raster). [default: True]"
)
@click.option("--transcripts", type=bool, default=True, help="Whether to read transcripts. [default: True]")
@click.option("--morphology-mip", type=bool, default=True, help="Whether to read morphology mip image. [default: True]")
@click.option(
"--morphology-focus", type=bool, default=True, help="Whether to read morphology focus image. [default: True]"
)
@click.option(
"--aligned-images",
type=bool,
default=True,
help="Whether to parse additional H&E or IF aligned images. [default: True]",
)
@click.option(
"--cells-table",
type=bool,
default=True,
help="Whether to read cells annotations in the AnnData table. [default: True]",
)
@click.option(
"--cells-analysis",
type=bool,
default=True,
help="Whether to read the onboard secondary analysis (clustering/PCA/UMAP/diffexp) into the table. [default: True]",
)
@click.option(
"--gex-only",
type=bool,
default=True,
help="If `True`, only load gene expression features. [default: True]",
)
@click.option(
"--imread-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to imread. [default: {}]",
)
@click.option(
"--image-models-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to Image2DModel. [default: {}]",
)
@click.option(
"--labels-models-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to Labels2DModel. [default: {}]",
)
def xenium_wrapper(
input: str,
output: str,
*,
cells_boundaries: bool = True,
nucleus_boundaries: bool = True,
cells_as_circles: bool = False,
cells_labels: bool = True,
nucleus_labels: bool = True,
transcripts: bool = True,
morphology_mip: bool = True,
morphology_focus: bool = True,
aligned_images: bool = True,
cells_table: bool = True,
cells_analysis: bool = True,
gex_only: bool = True,
imread_kwargs: str = "{}",
image_models_kwargs: str = "{}",
labels_models_kwargs: str = "{}",
) -> None:
"""Xenium conversion to SpatialData."""
from spatialdata_io.readers.xenium import xenium
sdata = xenium(
input,
cells_boundaries=cells_boundaries,
nucleus_boundaries=nucleus_boundaries,
cells_as_circles=cells_as_circles,
cells_labels=cells_labels,
nucleus_labels=nucleus_labels,
transcripts=transcripts,
morphology_mip=morphology_mip,
morphology_focus=morphology_focus,
aligned_images=aligned_images,
cells_table=cells_table,
cells_analysis=cells_analysis,
gex_only=gex_only,
imread_kwargs=_parse_json_param(imread_kwargs, "imread_kwargs"),
image_models_kwargs=_parse_json_param(image_models_kwargs, "image_models_kwargs"),
labels_models_kwargs=_parse_json_param(labels_models_kwargs, "labels_models_kwargs"),
)
sdata.write(output)
@cli.command(name="macsima")
@_input_output_click_options
@click.option(
"--parsing-style",
type=click.Choice(["processed_single_folder", "processed_multiple_folders", "raw"]),
default="processed_single_folder",
help="Parsing style for MACSima data. [default: processed_single_folder]",
)
@click.option(
"--filter-folder-names",
type=str,
multiple=True,
default=None,
help="List of folder names to filter out when parsing multiple folders. [default: None]",
)
@click.option(
"--subset",
type=int,
default=None,
help="Subset the image to the first 'subset' pixels in x and y dimensions. [default: None]",
)
@click.option(
"--c-subset", type=int, default=None, help="Subset the image to the first 'c-subset' channels. [default: None]"
)
@click.option(
"--max-chunk-size", type=int, default=1024, help="Maximum chunk size for x and y dimensions. [default: 1024]"
)
@click.option("--c-chunks-size", type=int, default=1, help="Chunk size for c dimension. [default: 1]")
@click.option("--multiscale", type=bool, default=True, help="Whether to create a multiscale image. [default: True]")
@click.option(
"--transformations",
type=bool,
default=True,
help="Whether to add a transformation from pixels to microns to the image. [default: True]",
)
@click.option(
"--scale-factors",
type=int,
multiple=True,
default=None,
help="Scale factors to use for downsampling. If None, scale factors are calculated based on image size. [default: None]",
)
@click.option(
"--default-scale-factor", type=int, default=2, help="Default scale factor to use for downsampling. [default: 2]"
)
@click.option(
"--nuclei-channel-name",
type=str,
default="DAPI",
help="Common string of the nuclei channel to separate nuclei from other channels. [default: 'DAPI']",
)
@click.option(
"--split-threshold-nuclei-channel",
type=int,
default=2,
help="Threshold for splitting nuclei channels. [default: 2]",
)
@click.option(
"--skip-rounds",
type=int,
multiple=True,
default=None,
help="List of round numbers to skip when parsing the data. [default: None]",
)
@click.option(
"--include-cycle-in-channel-name",
type=bool,
default=False,
help="Whether to include the cycle number in the channel name. [default: False]",
)
@click.option(
"--imread-kwargs",
type=str,
default="{}",
help="JSON string of keyword arguments passed to imread. [default: {}]",
)
def macsima_wrapper(
input: str,
output: str,
*,
parsing_style: str = "processed_single_folder",
filter_folder_names: list[str] | None = None,
subset: int | None = None,
c_subset: int | None = None,
max_chunk_size: int = 1024,
c_chunks_size: int = 1,
multiscale: bool = True,
transformations: bool = True,
scale_factors: list[int] | None = None,
default_scale_factor: int = 2,
nuclei_channel_name: str = "DAPI",
split_threshold_nuclei_channel: int | None = 2,
skip_rounds: list[int] | None = None,
include_cycle_in_channel_name: bool = False,
imread_kwargs: str = "{}",
) -> None:
"""Read MACSima formatted dataset and convert to SpatialData."""
from spatialdata_io.readers.macsima import macsima
sdata = macsima(
path=input,
parsing_style=parsing_style,
filter_folder_names=filter_folder_names,
imread_kwargs=_parse_json_param(imread_kwargs, "imread_kwargs"),
subset=subset,
c_subset=c_subset,
max_chunk_size=max_chunk_size,
c_chunks_size=c_chunks_size,
multiscale=multiscale,
transformations=transformations,
scale_factors=scale_factors,
default_scale_factor=default_scale_factor,
nuclei_channel_name=nuclei_channel_name,
split_threshold_nuclei_channel=split_threshold_nuclei_channel,
skip_rounds=skip_rounds,
include_cycle_in_channel_name=include_cycle_in_channel_name,
)
sdata.write(output)
@cli.command(name="generic")
@click.option(
"--input",
"-i",
type=click.Path(exists=True, file_okay=True, dir_okay=False),
required=True,
help=f"Path to the image/shapes input file. Supported extensions: {VALID_IMAGE_TYPES + VALID_SHAPE_TYPES}",
)
@click.option(
"--output",
"-o",
type=click.Path(file_okay=False),
required=True,
help="Path to zarr store to write to. If it does not exist yet, create new zarr store from input",
)
@click.option("--name", "-n", type=str, help="name of the element to be stored")
@click.option(
"--data-axes",
type=str,
help="Axes of the data for image files. Valid values are permutations of 'cyx' and 'czyx'.",
)
@click.option(
"--coordinate-system",
"-c",
type=str,
help="Coordinate system in spatialdata object to which an element should belong",
)
def read_generic_wrapper(
input: str,
output: str,
name: str | None = None,
data_axes: str | None = None,
coordinate_system: str | None = None,
) -> None:
"""Read generic data to SpatialData."""
from spatialdata_io.converters.generic_to_zarr import generic_to_zarr
if data_axes is not None and "".join(sorted(data_axes)) not in ["cxy", "cxyz"]:
raise ValueError("data_axes must be a permutation of 'cyx' or 'czyx'.")
generic_to_zarr(input=input, output=output, name=name, data_axes=data_axes, coordinate_system=coordinate_system)
if __name__ == "__main__":
cli()