Mercurial > repos > goeckslab > scimap_phenotyping
annotate scimap_spatial.py @ 3:3a441d347a46 draft
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
author | goeckslab |
---|---|
date | Wed, 26 Jun 2024 15:27:07 +0000 |
parents | dcfcad35e847 |
children |
rev | line source |
---|---|
0
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
1 import argparse |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
2 import json |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
3 import warnings |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
4 |
3
3a441d347a46
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
7ed4b55b11f7
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 |
7ed4b55b11f7
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 |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
8 |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
9 |
7ed4b55b11f7
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): |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
11 """ |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
12 Parameter |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
13 --------- |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
14 inputs : str |
7ed4b55b11f7
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. |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
16 anndata : str |
7ed4b55b11f7
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. |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
18 output : str |
7ed4b55b11f7
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. |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
20 """ |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
21 warnings.simplefilter('ignore') |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
22 |
7ed4b55b11f7
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: |
7ed4b55b11f7
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) |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
25 |
7ed4b55b11f7
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) |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
27 |
7ed4b55b11f7
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'] |
7ed4b55b11f7
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) |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
30 |
7ed4b55b11f7
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
3a441d347a46
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents:
1
diff
changeset
|
32 |
3a441d347a46
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
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
34 if tool == 'cluster': |
7ed4b55b11f7
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'] |
7ed4b55b11f7
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') |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
37 if subset_genes: |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
38 options['subset_genes'] = \ |
7ed4b55b11f7
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(',')] |
7ed4b55b11f7
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') |
7ed4b55b11f7
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: |
7ed4b55b11f7
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'] = \ |
7ed4b55b11f7
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
3a441d347a46
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': |
3a441d347a46
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
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
46 |
7ed4b55b11f7
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(): |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
48 if v == '': |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
49 options[k] = None |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
50 |
3
3a441d347a46
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
7ed4b55b11f7
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) |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
53 |
3
3a441d347a46
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
dcfcad35e847
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
3a441d347a46
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents:
1
diff
changeset
|
56 |
3a441d347a46
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: |
3a441d347a46
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 |
3a441d347a46
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'] = \ |
3a441d347a46
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) |
3a441d347a46
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents:
1
diff
changeset
|
61 |
3a441d347a46
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 |
3a441d347a46
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( |
3a441d347a46
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents:
1
diff
changeset
|
64 adata.obs, |
3a441d347a46
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'], |
3a441d347a46
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, |
3a441d347a46
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 |
3a441d347a46
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents:
1
diff
changeset
|
68 ) |
3a441d347a46
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents:
1
diff
changeset
|
69 |
3a441d347a46
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 |
3a441d347a46
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 |
3a441d347a46
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( |
3a441d347a46
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) |
3a441d347a46
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( |
3a441d347a46
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( |
3a441d347a46
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) |
3a441d347a46
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit 49210b00535415865694ddbec16238d8cf5e6bb0
goeckslab
parents:
1
diff
changeset
|
77 |
3a441d347a46
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: |
3a441d347a46
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
dcfcad35e847
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit d20d6f5d08b43de71182459f8a6452c564a5becc
goeckslab
parents:
0
diff
changeset
|
80 |
0
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
81 adata.write(output) |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
82 |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
83 |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
84 if __name__ == '__main__': |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
85 aparser = argparse.ArgumentParser() |
7ed4b55b11f7
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) |
7ed4b55b11f7
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) |
7ed4b55b11f7
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) |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
89 |
7ed4b55b11f7
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() |
7ed4b55b11f7
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/scimap commit b19cb55dfb751cccc857b95a432890299bfeebb5
goeckslab
parents:
diff
changeset
|
91 |
7ed4b55b11f7
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) |