comparison squidpy_spatial.py @ 0:be0e7952e229 draft

planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit ee16860018eba110ff845d62b18396db22abd91e
author goeckslab
date Mon, 29 Aug 2022 23:20:54 +0000
parents
children d30ef0613122
comparison
equal deleted inserted replaced
-1:000000000000 0:be0e7952e229
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 == 'numba_parallel': # for nhood_enrichment and ligrec
55 if v == 'false':
56 options[k] = False
57 elif v == 'true':
58 options[k] = True
59 elif k == 'interactions': # for ligrec
60 options[k] = pd.read_csv(v, sep="\t")
61 elif k == 'max_neighs':
62 options[k] = int(v) # for sepal
63
64 cluster_key = params['analyses'].get('cluster_key')
65 if cluster_key:
66 tool_func(adata, cluster_key, **options)
67 else:
68 tool_func(adata, **options)
69
70 if output_plot:
71 plotting_options = params['analyses']['plotting_options']
72 for k, v in plotting_options.items():
73 if not isinstance(v, str):
74 continue
75
76 if v in ('', 'none'):
77 plotting_options[k] = None
78 continue
79
80 if k == 'figsize':
81 options[k] = ast.literal_eval(v)
82 elif k in ('palette', 'score', 'source_groups', 'target_groups'):
83 options[k] = [e.strip() for e in v.split(',')]
84 elif k == 'means_range': # ligrec
85 v = v.strip()
86 if v[0] == '(':
87 v = v[1:]
88 if v[-1] == ')':
89 v = v[:-1]
90 options[k] = tuple([float(e.strip()) for e in v.split(',', 1)])
91
92 plotting_func = getattr(sq.pl, tool)
93 if cluster_key:
94 plotting_func(adata, cluster_key, save=output_plot, **plotting_options)
95 else: # TODO Remove this, since all plottings need cluster key
96 plotting_func(adata, save=output_plot, **plotting_options)
97
98 adata.write(output)
99
100
101 if __name__ == '__main__':
102 aparser = argparse.ArgumentParser()
103 aparser.add_argument("-i", "--inputs", dest="inputs", required=True)
104 aparser.add_argument("-e", "--output", dest="output", required=True)
105 aparser.add_argument("-a", "--anndata", dest="anndata", required=True)
106 aparser.add_argument("-p", "--output_plot", dest="output_plot", required=False)
107
108 args = aparser.parse_args()
109
110 main(args.inputs, args.anndata, args.output, args.output_plot)