annotate scimap_spatial.py @ 4:2983eb1d7362 draft default tip

planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit c39cea5ae2ebf61b3d51d687fd9d2930c907b72d
author goeckslab
date Tue, 30 Jul 2024 18:20:55 +0000
parents 472780baf150
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
1 import argparse
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
2 import json
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
3 import warnings
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
4
3
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
5 import pandas as pd
0
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
6 import scimap as sm
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
7 from anndata import read_h5ad
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
8
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
9
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
10 def main(inputs, anndata, output):
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
11 """
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
12 Parameter
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
13 ---------
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
14 inputs : str
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
15 File path to galaxy tool parameter.
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
16 anndata : str
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
17 File path to anndata containing phenotyping info.
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
18 output : str
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
19 File path to output.
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
20 """
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
21 warnings.simplefilter('ignore')
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
22
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
23 with open(inputs, 'r') as param_handler:
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
24 params = json.load(param_handler)
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
25
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
26 adata = read_h5ad(anndata)
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
27
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
28 tool = params['analyses']['selected_tool']
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
29 tool_func = getattr(sm.tl, tool)
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
30
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
31 options = params['analyses']['options']
3
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
32
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
33 # tool specific pre-processing
0
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
34 if tool == 'cluster':
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
35 options['method'] = params['analyses']['method']
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
36 subset_genes = options.pop('subset_genes')
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
37 if subset_genes:
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
38 options['subset_genes'] = \
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
39 [x.strip() for x in subset_genes.split(',')]
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
40 sub_cluster_group = options.pop('sub_cluster_group')
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
41 if sub_cluster_group:
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
42 options['sub_cluster_group'] = \
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
43 [x.strip() for x in sub_cluster_group.split(',')]
3
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
44 elif tool == 'spatial_lda':
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
45 max_weight_assignment = options.pop('max_weight_assignment')
0
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
46
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
47 for k, v in options.items():
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
48 if v == '':
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
49 options[k] = None
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
50
3
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
51 # tool execution
0
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
52 tool_func(adata, **options)
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
53
3
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
54 # spatial LDA post-processing
1
fd38e533a54b planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit d20d6f5d08b43de71182459f8a6452c564a5becc
goeckslab
parents: 0
diff changeset
55 if tool == 'spatial_lda':
3
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
56
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
57 if max_weight_assignment:
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
58 # assign cell to a motif based on maximum weight
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
59 adata.uns['spatial_lda']['neighborhood_motif'] = \
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
60 adata.uns['spatial_lda'].idxmax(axis=1)
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
61
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
62 # merge motif assignment into adata.obs
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
63 adata.obs = pd.merge(
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
64 adata.obs,
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
65 adata.uns['spatial_lda']['neighborhood_motif'],
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
66 left_index=True,
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
67 right_index=True
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
68 )
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
69
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
70 # write out LDA results as tabular files
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
71 # so they're accessible to Galaxy users
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
72 adata.uns['spatial_lda'].reset_index().to_csv(
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
73 'lda_weights.txt', sep='\t', index=False)
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
74 adata.uns['spatial_lda_probability'].T.reset_index(
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
75 names='motif').to_csv(
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
76 'lda_probabilities.txt', sep='\t', index=False)
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
77
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
78 if 'spatial_lda_model' in adata.uns:
472780baf150 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents: 1
diff changeset
79 adata.uns.pop('spatial_lda_model')
1
fd38e533a54b planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit d20d6f5d08b43de71182459f8a6452c564a5becc
goeckslab
parents: 0
diff changeset
80
0
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
81 adata.write(output)
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
82
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
83
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
84 if __name__ == '__main__':
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
85 aparser = argparse.ArgumentParser()
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
86 aparser.add_argument("-i", "--inputs", dest="inputs", required=True)
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
87 aparser.add_argument("-e", "--output", dest="output", required=True)
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
88 aparser.add_argument("-a", "--anndata", dest="anndata", required=True)
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
89
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
90 args = aparser.parse_args()
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
91
42e6c251bfd0 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff changeset
92 main(args.inputs, args.anndata, args.output)