comparison squidpy_scatter.py @ 0:5b17a47d1ade draft default tip

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