annotate scale_cell_coordinates.py @ 1:815efbdbb7b2 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:44 +0000
parents 8969e16f3701
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8969e16f3701 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff changeset
1 import argparse
8969e16f3701 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff changeset
2 import json
8969e16f3701 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff changeset
3 import warnings
8969e16f3701 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff changeset
4
8969e16f3701 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
8969e16f3701 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff changeset
6
8969e16f3701 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff changeset
7
8969e16f3701 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):
8969e16f3701 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff changeset
9
8969e16f3701 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff changeset
10 """
8969e16f3701 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff changeset
11 inputs : str
8969e16f3701 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
8969e16f3701 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff changeset
13 output: str
8969e16f3701 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
8969e16f3701 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff changeset
15 """
8969e16f3701 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')
8969e16f3701 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff changeset
17
8969e16f3701 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
8969e16f3701 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:
8969e16f3701 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)
8969e16f3701 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff changeset
21
8969e16f3701 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
8969e16f3701 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'])
8969e16f3701 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff changeset
24
8969e16f3701 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff changeset
25 # scale coords
8969e16f3701 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']
8969e16f3701 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 = []
8969e16f3701 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']]:
8969e16f3701 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}'
8969e16f3701 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']
8969e16f3701 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)
8969e16f3701 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff changeset
32
8969e16f3701 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
8969e16f3701 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
8969e16f3701 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff changeset
35
8969e16f3701 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
8969e16f3701 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)
8969e16f3701 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff changeset
38
8969e16f3701 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff changeset
39
8969e16f3701 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__':
8969e16f3701 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff changeset
41
8969e16f3701 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()
8969e16f3701 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(
8969e16f3701 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)
8969e16f3701 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(
8969e16f3701 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)
8969e16f3701 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff changeset
47
8969e16f3701 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()
8969e16f3701 planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/mti-utils commit bc438db690e41823909b32b693f297d942433a43
goeckslab
parents:
diff changeset
49
8969e16f3701 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)