comparison scimap_spatial.py @ 3:472780baf150 draft default tip

planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
author goeckslab
date Wed, 26 Jun 2024 15:26:49 +0000
parents fd38e533a54b
children
comparison
equal deleted inserted replaced
2:d19c068c2490 3:472780baf150
1 import argparse 1 import argparse
2 import json 2 import json
3 import warnings 3 import warnings
4 4
5 import pandas as pd
5 import scimap as sm 6 import scimap as sm
6 from anndata import read_h5ad 7 from anndata import read_h5ad
7 8
8 9
9 def main(inputs, anndata, output): 10 def main(inputs, anndata, output):
26 27
27 tool = params['analyses']['selected_tool'] 28 tool = params['analyses']['selected_tool']
28 tool_func = getattr(sm.tl, tool) 29 tool_func = getattr(sm.tl, tool)
29 30
30 options = params['analyses']['options'] 31 options = params['analyses']['options']
32
33 # tool specific pre-processing
31 if tool == 'cluster': 34 if tool == 'cluster':
32 options['method'] = params['analyses']['method'] 35 options['method'] = params['analyses']['method']
33 subset_genes = options.pop('subset_genes') 36 subset_genes = options.pop('subset_genes')
34 if subset_genes: 37 if subset_genes:
35 options['subset_genes'] = \ 38 options['subset_genes'] = \
36 [x.strip() for x in subset_genes.split(',')] 39 [x.strip() for x in subset_genes.split(',')]
37 sub_cluster_group = options.pop('sub_cluster_group') 40 sub_cluster_group = options.pop('sub_cluster_group')
38 if sub_cluster_group: 41 if sub_cluster_group:
39 options['sub_cluster_group'] = \ 42 options['sub_cluster_group'] = \
40 [x.strip() for x in sub_cluster_group.split(',')] 43 [x.strip() for x in sub_cluster_group.split(',')]
44 elif tool == 'spatial_lda':
45 max_weight_assignment = options.pop('max_weight_assignment')
41 46
42 for k, v in options.items(): 47 for k, v in options.items():
43 if v == '': 48 if v == '':
44 options[k] = None 49 options[k] = None
45 50
51 # tool execution
46 tool_func(adata, **options) 52 tool_func(adata, **options)
47 53
54 # spatial LDA post-processing
48 if tool == 'spatial_lda': 55 if tool == 'spatial_lda':
49 adata.uns.pop('spatial_lda_model') 56
57 if max_weight_assignment:
58 # assign cell to a motif based on maximum weight
59 adata.uns['spatial_lda']['neighborhood_motif'] = \
60 adata.uns['spatial_lda'].idxmax(axis=1)
61
62 # merge motif assignment into adata.obs
63 adata.obs = pd.merge(
64 adata.obs,
65 adata.uns['spatial_lda']['neighborhood_motif'],
66 left_index=True,
67 right_index=True
68 )
69
70 # write out LDA results as tabular files
71 # so they're accessible to Galaxy users
72 adata.uns['spatial_lda'].reset_index().to_csv(
73 'lda_weights.txt', sep='\t', index=False)
74 adata.uns['spatial_lda_probability'].T.reset_index(
75 names='motif').to_csv(
76 'lda_probabilities.txt', sep='\t', index=False)
77
78 if 'spatial_lda_model' in adata.uns:
79 adata.uns.pop('spatial_lda_model')
50 80
51 adata.write(output) 81 adata.write(output)
52 82
53 83
54 if __name__ == '__main__': 84 if __name__ == '__main__':