Mercurial > repos > goeckslab > squidpy_scatter
comparison squidpy_spatial.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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:6fe0d4f464f4 |
---|---|
1 import argparse | |
2 import ast | |
3 import json | |
4 import warnings | |
5 | |
6 import pandas as pd | |
7 import squidpy as sq | |
8 from anndata import read_h5ad | |
9 | |
10 | |
11 def main(inputs, anndata, output, output_plot): | |
12 """ | |
13 Parameter | |
14 --------- | |
15 inputs : str | |
16 File path to galaxy tool parameter. | |
17 anndata : str | |
18 File path to anndata containing phenotyping info. | |
19 output : str | |
20 File path to output. | |
21 output_plot: str or None | |
22 File path to save the plotting image. | |
23 """ | |
24 warnings.simplefilter('ignore') | |
25 | |
26 with open(inputs, 'r') as param_handler: | |
27 params = json.load(param_handler) | |
28 | |
29 adata = read_h5ad(anndata) | |
30 | |
31 if 'spatial' not in adata.obsm: | |
32 try: | |
33 adata.obsm['spatial'] = adata.obs[['X_centroid', 'Y_centroid']].values | |
34 except Exception as e: | |
35 print(e) | |
36 | |
37 tool = params['analyses']['selected_tool'] | |
38 tool_func = getattr(sq.gr, tool) | |
39 | |
40 options = params['analyses']['options'] | |
41 | |
42 for k, v in options.items(): | |
43 if not isinstance(v, str): | |
44 continue | |
45 | |
46 if v in ('', 'none'): | |
47 options[k] = None | |
48 continue | |
49 | |
50 if k == 'genes': # for spatial_autocorr and sepal | |
51 options[k] = [e.strip() for e in v.split(',')] | |
52 elif k == 'radius': # for spatial_neighbors | |
53 options[k] = ast.literal_eval(v) | |
54 elif k == 'interactions': # for ligrec | |
55 options[k] = pd.read_csv(v, sep="\t") | |
56 elif k == 'max_neighs': | |
57 options[k] = int(v) # for sepal | |
58 | |
59 cluster_key = params['analyses'].get('cluster_key') | |
60 if cluster_key: | |
61 tool_func(adata, cluster_key, **options) | |
62 else: | |
63 tool_func(adata, **options) | |
64 | |
65 if output_plot: | |
66 plotting_options = params['analyses']['plotting_options'] | |
67 for k, v in plotting_options.items(): | |
68 if not isinstance(v, str): | |
69 continue | |
70 | |
71 if v in ('', 'none'): | |
72 plotting_options[k] = None | |
73 continue | |
74 | |
75 if k == 'figsize': | |
76 options[k] = ast.literal_eval(v) | |
77 elif k in ('palette', 'score', 'source_groups', 'target_groups'): | |
78 options[k] = [e.strip() for e in v.split(',')] | |
79 elif k == 'means_range': # ligrec | |
80 v = v.strip() | |
81 if v[0] == '(': | |
82 v = v[1:] | |
83 if v[-1] == ')': | |
84 v = v[:-1] | |
85 options[k] = tuple([float(e.strip()) for e in v.split(',', 1)]) | |
86 | |
87 plotting_func = getattr(sq.pl, tool) | |
88 if cluster_key: | |
89 plotting_func(adata, cluster_key, save=output_plot, **plotting_options) | |
90 else: # TODO Remove this, since all plottings need cluster key | |
91 plotting_func(adata, save=output_plot, **plotting_options) | |
92 | |
93 adata.write(output) | |
94 | |
95 | |
96 if __name__ == '__main__': | |
97 aparser = argparse.ArgumentParser() | |
98 aparser.add_argument("-i", "--inputs", dest="inputs", required=True) | |
99 aparser.add_argument("-e", "--output", dest="output", required=True) | |
100 aparser.add_argument("-a", "--anndata", dest="anndata", required=True) | |
101 aparser.add_argument("-p", "--output_plot", dest="output_plot", required=False) | |
102 | |
103 args = aparser.parse_args() | |
104 | |
105 main(args.inputs, args.anndata, args.output, args.output_plot) |