Mercurial > repos > goeckslab > rename_tiff_channels
annotate scale_cell_coordinates.py @ 6:4763362914f4 draft default tip
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit 5c324d06ad54dca76aa2b4c8118df5654f49da66
author | goeckslab |
---|---|
date | Mon, 15 Jul 2024 21:37:39 +0000 |
parents | 61fd94c7ce42 |
children |
rev | line source |
---|---|
5
61fd94c7ce42
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
1 import argparse |
61fd94c7ce42
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
2 import json |
61fd94c7ce42
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
3 import warnings |
61fd94c7ce42
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
4 |
61fd94c7ce42
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 |
61fd94c7ce42
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
6 |
61fd94c7ce42
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
7 |
61fd94c7ce42
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): |
61fd94c7ce42
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
9 |
61fd94c7ce42
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
10 """ |
61fd94c7ce42
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
11 inputs : str |
61fd94c7ce42
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 |
61fd94c7ce42
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
13 output: str |
61fd94c7ce42
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 |
61fd94c7ce42
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
15 """ |
61fd94c7ce42
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') |
61fd94c7ce42
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
17 |
61fd94c7ce42
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 |
61fd94c7ce42
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: |
61fd94c7ce42
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) |
61fd94c7ce42
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
21 |
61fd94c7ce42
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 |
61fd94c7ce42
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']) |
61fd94c7ce42
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
24 |
61fd94c7ce42
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
25 # scale coords |
61fd94c7ce42
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'] |
61fd94c7ce42
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 = [] |
61fd94c7ce42
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']]: |
61fd94c7ce42
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}' |
61fd94c7ce42
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'] |
61fd94c7ce42
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) |
61fd94c7ce42
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
32 |
61fd94c7ce42
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 |
61fd94c7ce42
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 |
61fd94c7ce42
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
35 |
61fd94c7ce42
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 |
61fd94c7ce42
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) |
61fd94c7ce42
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
38 |
61fd94c7ce42
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
39 |
61fd94c7ce42
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__': |
61fd94c7ce42
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
41 |
61fd94c7ce42
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() |
61fd94c7ce42
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( |
61fd94c7ce42
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) |
61fd94c7ce42
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( |
61fd94c7ce42
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) |
61fd94c7ce42
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
47 |
61fd94c7ce42
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() |
61fd94c7ce42
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff
changeset
|
49 |
61fd94c7ce42
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) |