annotate squidpy_scatter.py @ 1:2a5036c29889 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:09 +0000
parents 5b17a47d1ade
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
1 import argparse
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
2 import ast
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
3 import json
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
4 import warnings
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
5
5b17a47d1ade 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
5b17a47d1ade 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
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
8
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
9
5b17a47d1ade 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):
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
11
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
12 """
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
13 inputs : str
5b17a47d1ade 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
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
15 output_plot: str
5b17a47d1ade 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
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
17 """
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
18 warnings.simplefilter('ignore')
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
19
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
20 # read inputs JSON
5b17a47d1ade 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:
5b17a47d1ade 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)
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
23
5b17a47d1ade 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
2a5036c29889 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
5b17a47d1ade 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')
5b17a47d1ade 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')
5b17a47d1ade 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
2a5036c29889 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit ed118f79326f83eff1315f735b4e0e1a45a5e02c
goeckslab
parents: 0
diff changeset
29 options = {
2a5036c29889 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},
2a5036c29889 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit ed118f79326f83eff1315f735b4e0e1a45a5e02c
goeckslab
parents: 0
diff changeset
31 **plot_opts,
2a5036c29889 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit ed118f79326f83eff1315f735b4e0e1a45a5e02c
goeckslab
parents: 0
diff changeset
32 **legend_opts,
2a5036c29889 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit ed118f79326f83eff1315f735b4e0e1a45a5e02c
goeckslab
parents: 0
diff changeset
33 **aes_opts
2a5036c29889 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit ed118f79326f83eff1315f735b4e0e1a45a5e02c
goeckslab
parents: 0
diff changeset
34 }
0
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
35
5b17a47d1ade 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
5b17a47d1ade 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')
5b17a47d1ade 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)
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
39
5b17a47d1ade 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']
5b17a47d1ade 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
5b17a47d1ade 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')
5b17a47d1ade 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:
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
44 try:
5b17a47d1ade 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
5b17a47d1ade 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:
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
47 print(e)
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
48
5b17a47d1ade 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,
5b17a47d1ade 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
5b17a47d1ade 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():
5b17a47d1ade 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):
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
53 continue
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
54
5b17a47d1ade 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'):
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
56 options[k] = None
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
57 continue
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
58
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
59 if k == 'groups':
5b17a47d1ade 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(',')]
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
61
5b17a47d1ade 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':
5b17a47d1ade 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
5b17a47d1ade 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)
5b17a47d1ade 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(',')])]
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
66
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
67 elif k == 'figsize':
5b17a47d1ade 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)
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
69
5b17a47d1ade 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
5b17a47d1ade 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.
5b17a47d1ade 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
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
73 options['shape'] = None
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
74
5b17a47d1ade 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
5b17a47d1ade 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(
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
77 adata=adata,
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
78 save='image.png',
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
79 **options
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
80 )
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
81
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
82
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
83 if __name__ == '__main__':
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
84
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
85 aparser = argparse.ArgumentParser()
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
86 aparser.add_argument(
5b17a47d1ade 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)
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
88 aparser.add_argument(
5b17a47d1ade 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)
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
90
5b17a47d1ade 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()
5b17a47d1ade planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
goeckslab
parents:
diff changeset
92
5b17a47d1ade 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)