annotate squidpy_scatter.py @ 0:6fe0d4f464f4 draft

planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
author goeckslab
date Thu, 11 Jul 2024 22:22:53 +0000
parents
children b84c324b58bd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
1 import argparse
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
2 import ast
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
3 import json
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
4 import warnings
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
5
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
6 import squidpy as sq
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
7 from anndata import read_h5ad
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
8
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
9
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
10 def main(inputs, output_plot):
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
11
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
12 """
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
13 inputs : str
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
14 File path to galaxy tool JSON inputs config file
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
15 output_plot: str
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
16 File path to save the plotting image
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
17 """
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
18 warnings.simplefilter('ignore')
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
19
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
20 # read inputs JSON
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
21 with open(inputs, 'r') as param_handler:
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
22 params = json.load(param_handler)
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
23
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
24 # collapse param dict hierarchy, parse inputs
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
25 plot_opts = params.pop('plot_opts')
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
26 legend_opts = params.pop('legend_opts')
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
27 aes_opts = params.pop('aesthetic_opts')
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
28 options = {**params, **plot_opts, **legend_opts, **aes_opts}
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
29
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
30 # read input anndata file
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
31 adata_fh = options.pop('anndata')
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
32 adata = read_h5ad(adata_fh)
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
33
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
34 # ensure spatial coords in anndata.obsm['spatial']
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
35 # if not, populate with user provided X/Y coord column names
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
36 x, y = options.pop('x_coord'), options.pop('y_coord')
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
37 if 'spatial' not in adata.obsm:
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
38 try:
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
39 adata.obsm['spatial'] = adata.obs[[x, y]].values
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
40 except Exception as e:
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
41 print(e)
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
42
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
43 # scan thru tool params,
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
44 # replace None values, and reformat specific parameters
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
45 for k, v in options.items():
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
46 if not isinstance(v, str):
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
47 continue
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
48
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
49 if v in ('', 'none'):
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
50 options[k] = None
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
51 continue
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
52
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
53 if k == 'groups':
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
54 options[k] = [e.strip() for e in v.split(',')]
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
55
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
56 elif k == 'crop_coord':
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
57 # split str on commas into tuple of coords
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
58 # then nest in list (expected by squidpy function)
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
59 options[k] = [tuple([int(e.strip()) for e in v.split(',')])]
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
60
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
61 elif k == 'figsize':
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
62 options[k] = ast.literal_eval(v)
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
63
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
64 # not exposing this parameter for now. Only useful to change for ST data
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
65 # and can otherwise just be problematic.
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
66 # Explicitly setting to None is necessary to avoid an error
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
67 options['shape'] = None
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
68
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
69 # call squidpy spatial scatter function, unpack tool params
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
70 sq.pl.spatial_scatter(
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
71 adata=adata,
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
72 save='image.png',
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
73 **options
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
74 )
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
75
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
76
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
77 if __name__ == '__main__':
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
78
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
79 aparser = argparse.ArgumentParser()
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
80 aparser.add_argument(
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
81 "-i", "--inputs", dest="inputs", required=True)
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
82 aparser.add_argument(
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
83 "-p", "--output_plot", dest="output_plot", required=False)
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
84
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
85 args = aparser.parse_args()
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
86
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
87 main(args.inputs, args.output_plot)