Mercurial > repos > goeckslab > cell_intensity_processing
annotate scale_cell_coordinates.py @ 5:afa3cb2110eb draft
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
author | goeckslab |
---|---|
date | Thu, 11 Jul 2024 22:41:26 +0000 |
parents | |
children |
rev | line source |
---|---|
5
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
1 import argparse |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
2 import json |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
3 import warnings |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
4 |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
5 import anndata as ad |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
6 |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
7 |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
8 def main(inputs, output): |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
9 |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
10 """ |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
11 inputs : str |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
12 File path to galaxy tool JSON inputs config file |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
13 output: str |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
14 File path to save the output h5ad file |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
15 """ |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
16 warnings.simplefilter('ignore') |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
17 |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
18 # read inputs JSON |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
19 with open(inputs, 'r') as param_handler: |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
20 params = json.load(param_handler) |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
21 |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
22 # read input anndata file |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
23 adata = ad.read_h5ad(params['anndata']) |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
24 |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
25 # scale coords |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
26 unit = params['unit'] |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
27 new_col_names = [] |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
28 for c in [params['x_coord'], params['y_coord']]: |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
29 scaled_col_name = f'{c}_{unit}' |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
30 adata.obs[scaled_col_name] = adata.obs[c] * params['resolution'] |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
31 new_col_names.append(scaled_col_name) |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
32 |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
33 # overwrite adata.obsm['spatial'] with scaled coordinates |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
34 adata.obsm['spatial'] = adata.obs[scaled_col_name].values |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
35 |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
36 # write out anndata to h5ad file |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
37 adata.write_h5ad(output) |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
38 |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
39 |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
40 if __name__ == '__main__': |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
41 |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
42 aparser = argparse.ArgumentParser() |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
43 aparser.add_argument( |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
44 "-i", "--inputs", dest="inputs", required=True) |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
45 aparser.add_argument( |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
46 "-o", "--output", dest="output", required=False) |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
47 |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
48 args = aparser.parse_args() |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
49 |
afa3cb2110eb
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
50 main(args.inputs, args.output) |