comparison scimap_plotting.py @ 2:ce22e846c5e4 draft

planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 9fb5578191db8a559191e45156cfb95350f01aea
author goeckslab
date Mon, 10 Jun 2024 18:44:25 +0000
parents
children 88fca6e905be
comparison
equal deleted inserted replaced
1:dcfcad35e847 2:ce22e846c5e4
1 import argparse
2 import json
3 import os
4 import warnings
5
6 import matplotlib.pylab as plt
7 import numpy as np
8 import scimap as sm
9 import seaborn as sns
10 from anndata import read_h5ad
11
12 sns.set(color_codes=True)
13
14
15 def main(inputs, anndata, output):
16 """
17 Parameter
18 ---------
19 inputs : str
20 File path to galaxy tool parameter.
21 anndata : str
22 File path to anndata containing phenotyping info.
23 output : str
24 File path to output.
25 """
26 warnings.simplefilter('ignore')
27
28 with open(inputs, 'r') as param_handler:
29 params = json.load(param_handler)
30
31 adata = read_h5ad(anndata)
32
33 tool = params['analyses']['selected_tool']
34 options = params['analyses']['options']
35
36 if tool == 'stacked_barplot':
37
38 # parse list text arguments
39 for o in options.copy():
40 opt_list = options.pop(o)
41 if opt_list:
42 options[o] = [x.strip() for x in opt_list.split(',')]
43
44 # add base args into options dict to pass to tool
45 options['x_axis'] = params['analyses']['x_axis']
46 options['y_axis'] = params['analyses']['y_axis']
47 options['method'] = params['analyses']['method']
48
49 options['return_data'] = True
50
51 df = sm.pl.stacked_barplot(adata, **options)
52
53 # Pick cmap to use
54 num_phenotypes = len(df.columns) - 1
55 if num_phenotypes <= 9:
56 matplotlib_cmap = "Set1"
57 elif num_phenotypes > 9 and num_phenotypes <= 20:
58 matplotlib_cmap = plt.cm.tab20
59 else:
60 matplotlib_cmap = plt.cm.gist_ncar
61
62 # Plotting
63 sns.set_theme(style="white")
64 ax = df.plot.bar(stacked=True, cmap=matplotlib_cmap)
65 fig = ax.get_figure()
66 handles, labels = ax.get_legend_handles_labels()
67 ax.legend(
68 reversed(handles),
69 reversed(labels),
70 bbox_to_anchor=(1, 1),
71 loc='upper left'
72 )
73 plt.tight_layout()
74
75 # # save and close
76 fig.savefig('out.png', dpi=300)
77 plt.close(fig)
78
79 if tool == 'voronoi':
80
81 plt.style.use('fast')
82
83 tool_func = getattr(sm.pl, tool)
84
85 # x_lim/y_lim need to be parsed from comma-sep str to integer tuples
86 for lim in ['x_lim', 'y_lim']:
87 opt_list = options.pop(lim)
88 if opt_list:
89 options[lim] = [int(x.strip()) for x in opt_list.split(',')]
90
91 # parse list text arguments
92 for cat in ['overlay_points_categories', 'overlay_drop_categories']:
93 opt_list = options.pop(cat)
94 if opt_list:
95 options[cat] = [x.strip() for x in opt_list.split(',')]
96
97 # add base args into options dict to pass to tool
98 options['color_by'] = params['analyses']['color_by']
99 options['x_coordinate'] = params['analyses']['x_coordinate']
100 options['y_coordinate'] = params['analyses']['y_coordinate']
101
102 # fill any missing params with None as tool expects
103 for k, v in options.items():
104 if v == '':
105 options[k] = None
106
107 options['saveDir'] = os.getcwd()
108 options['fileName'] = 'out.png'
109
110 if options['size_max'] is None:
111 options['size_max'] = np.inf
112
113 # call the tool and unpack all options
114 tool_func(adata, **options)
115
116
117 if __name__ == '__main__':
118 aparser = argparse.ArgumentParser()
119 aparser.add_argument("-i", "--inputs", dest="inputs", required=True)
120 aparser.add_argument("-a", "--anndata", dest="anndata", required=True)
121 aparser.add_argument("-e", "--output", dest="output", required=True)
122
123 args = aparser.parse_args()
124
125 main(args.inputs, args.anndata, args.output)