Mercurial > repos > goeckslab > vitessce_spatial
comparison vitessce_spatial.py @ 0:9f60ef2d586e draft
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/vitessce commit 9b2dc921e692af8045773013d9f87d4d790e2ea1
| author | goeckslab |
|---|---|
| date | Thu, 08 Sep 2022 17:23:33 +0000 |
| parents | |
| children | 068da7f7cd83 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:9f60ef2d586e |
|---|---|
| 1 import argparse | |
| 2 import json | |
| 3 import warnings | |
| 4 from pathlib import Path | |
| 5 | |
| 6 import scanpy as sc | |
| 7 from anndata import read_h5ad | |
| 8 from vitessce import ( | |
| 9 AnnDataWrapper, | |
| 10 Component as cm, | |
| 11 MultiImageWrapper, | |
| 12 OmeTiffWrapper, | |
| 13 VitessceConfig, | |
| 14 ) | |
| 15 | |
| 16 | |
| 17 def main(inputs, output, image, anndata=None, masks=None): | |
| 18 """ | |
| 19 Parameter | |
| 20 --------- | |
| 21 inputs : str | |
| 22 File path to galaxy tool parameter. | |
| 23 output : str | |
| 24 Output folder for saving web content. | |
| 25 image : str | |
| 26 File path to the OME Tiff image. | |
| 27 anndata : str | |
| 28 File path to anndata containing phenotyping info. | |
| 29 masks : str | |
| 30 File path to the image masks. | |
| 31 """ | |
| 32 warnings.simplefilter('ignore') | |
| 33 | |
| 34 with open(inputs, 'r') as param_handler: | |
| 35 params = json.load(param_handler) | |
| 36 | |
| 37 vc = VitessceConfig(name=None, description=None) | |
| 38 dataset = vc.add_dataset() | |
| 39 image_wrappers = [OmeTiffWrapper(img_path=image, name='OMETIFF')] | |
| 40 if masks: | |
| 41 image_wrappers.append( | |
| 42 OmeTiffWrapper(img_path=masks, name='MASKS', is_bitmask=True) | |
| 43 ) | |
| 44 dataset.add_object(MultiImageWrapper(image_wrappers)) | |
| 45 | |
| 46 status = vc.add_view(dataset, cm.STATUS) | |
| 47 spatial = vc.add_view(dataset, cm.SPATIAL) | |
| 48 lc = vc.add_view(dataset, cm.LAYER_CONTROLLER) | |
| 49 | |
| 50 if not anndata: | |
| 51 vc.layout(status / lc | spatial) | |
| 52 config_dict = vc.export(to='files', base_url='http://localhost', out_dir=output) | |
| 53 with open(Path(output).joinpath('config.json'), 'w') as f: | |
| 54 json.dump(config_dict, f, indent=4) | |
| 55 return | |
| 56 | |
| 57 adata = read_h5ad(anndata) | |
| 58 | |
| 59 params = params['do_phenotyping'] | |
| 60 embedding = params['scatterplot_embeddings']['embedding'] | |
| 61 embedding_options = params['scatterplot_embeddings']['options'] | |
| 62 if embedding == 'umap': | |
| 63 sc.pp.neighbors(adata, **embedding_options) | |
| 64 sc.tl.umap(adata) | |
| 65 mappings_obsm = 'X_umap' | |
| 66 mappings_obsm_name = "UMAP" | |
| 67 elif embedding == 'tsne': | |
| 68 sc.tl.tsne(adata, **embedding_options) | |
| 69 mappings_obsm = 'X_tsne' | |
| 70 mappings_obsm_name = "tSNE" | |
| 71 else: # pca | |
| 72 sc.tl.pca(adata, **embedding_options) | |
| 73 mappings_obsm = 'X_pca' | |
| 74 mappings_obsm_name = "PCA" | |
| 75 | |
| 76 adata.obsm['XY_centroid'] = adata.obs[['X_centroid', 'Y_centroid']].values | |
| 77 | |
| 78 cell_set_obs = params['phenotype_factory']['phenotypes'] | |
| 79 if not isinstance(cell_set_obs, list): | |
| 80 cell_set_obs = [x.strip() for x in cell_set_obs.split(',')] | |
| 81 cell_set_obs_names = [obj[0].upper() + obj[1:] for obj in cell_set_obs] | |
| 82 dataset.add_object( | |
| 83 AnnDataWrapper( | |
| 84 adata, | |
| 85 mappings_obsm=[mappings_obsm], | |
| 86 mappings_obsm_names=[mappings_obsm_name], | |
| 87 spatial_centroid_obsm='XY_centroid', | |
| 88 cell_set_obs=cell_set_obs, | |
| 89 cell_set_obs_names=cell_set_obs_names, | |
| 90 expression_matrix="X" | |
| 91 ) | |
| 92 ) | |
| 93 | |
| 94 cellsets = vc.add_view(dataset, cm.CELL_SETS) | |
| 95 scattorplot = vc.add_view(dataset, cm.SCATTERPLOT, mapping=mappings_obsm_name) | |
| 96 heatmap = vc.add_view(dataset, cm.HEATMAP) | |
| 97 genes = vc.add_view(dataset, cm.GENES) | |
| 98 cell_set_sizes = vc.add_view(dataset, cm.CELL_SET_SIZES) | |
| 99 cell_set_expression = vc.add_view(dataset, cm.CELL_SET_EXPRESSION) | |
| 100 vc.layout( | |
| 101 (status / genes / cell_set_expression) | |
| 102 | (cellsets / lc / scattorplot) | |
| 103 | (cell_set_sizes / heatmap / spatial) | |
| 104 ) | |
| 105 config_dict = vc.export(to='files', base_url='http://localhost', out_dir=output) | |
| 106 | |
| 107 with open(Path(output).joinpath('config.json'), 'w') as f: | |
| 108 json.dump(config_dict, f, indent=4) | |
| 109 | |
| 110 | |
| 111 if __name__ == '__main__': | |
| 112 aparser = argparse.ArgumentParser() | |
| 113 aparser.add_argument("-i", "--inputs", dest="inputs", required=True) | |
| 114 aparser.add_argument("-e", "--output", dest="output", required=True) | |
| 115 aparser.add_argument("-g", "--image", dest="image", required=True) | |
| 116 aparser.add_argument("-a", "--anndata", dest="anndata", required=False) | |
| 117 aparser.add_argument("-m", "--masks", dest="masks", required=False) | |
| 118 | |
| 119 args = aparser.parse_args() | |
| 120 | |
| 121 main(args.inputs, args.output, args.image, args.anndata, args.masks) |
