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