annotate squidpy_scatter.py @ 1:b84c324b58bd draft default tip

planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit ed118f79326f83eff1315f735b4e0e1a45a5e02c
author goeckslab
date Thu, 31 Oct 2024 17:55:17 +0000
parents 6fe0d4f464f4
children
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
1
b84c324b58bd planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit ed118f79326f83eff1315f735b4e0e1a45a5e02c
goeckslab
parents: 0
diff changeset
25 main_params = ['anndata', 'x_coord', 'y_coord', 'color']
0
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
26 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
27 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
28 aes_opts = params.pop('aesthetic_opts')
1
b84c324b58bd planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit ed118f79326f83eff1315f735b4e0e1a45a5e02c
goeckslab
parents: 0
diff changeset
29 options = {
b84c324b58bd planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit ed118f79326f83eff1315f735b4e0e1a45a5e02c
goeckslab
parents: 0
diff changeset
30 **{k: params[k] for k in main_params},
b84c324b58bd planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit ed118f79326f83eff1315f735b4e0e1a45a5e02c
goeckslab
parents: 0
diff changeset
31 **plot_opts,
b84c324b58bd planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit ed118f79326f83eff1315f735b4e0e1a45a5e02c
goeckslab
parents: 0
diff changeset
32 **legend_opts,
b84c324b58bd planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit ed118f79326f83eff1315f735b4e0e1a45a5e02c
goeckslab
parents: 0
diff changeset
33 **aes_opts
b84c324b58bd planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit ed118f79326f83eff1315f735b4e0e1a45a5e02c
goeckslab
parents: 0
diff changeset
34 }
0
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
35
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
36 # 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
37 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
38 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
39
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
40 # 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
41 # 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
42 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
43 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
44 try:
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
45 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
46 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
47 print(e)
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 # 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
50 # 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
51 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
52 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
53 continue
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
54
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
55 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
56 options[k] = None
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
57 continue
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
58
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
59 if k == 'groups':
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
60 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
61
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
62 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
63 # 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
64 # 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
65 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
66
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
67 elif k == 'figsize':
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
68 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
69
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
70 # 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
71 # 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
72 # 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
73 options['shape'] = None
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 # 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
76 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
77 adata=adata,
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
78 save='image.png',
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
79 **options
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
80 )
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
81
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
82
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
83 if __name__ == '__main__':
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 aparser = argparse.ArgumentParser()
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
86 aparser.add_argument(
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
87 "-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
88 aparser.add_argument(
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
89 "-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
90
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
91 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
92
6fe0d4f464f4 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
93 main(args.inputs, args.output_plot)