|
| 1 | +#!/usr/bin/env python |
| 2 | +# coding: utf-8 |
| 3 | + |
| 4 | +# # Vitessce Widget Tutorial |
| 5 | + |
| 6 | +# ## Import dependencies |
| 7 | +# |
| 8 | + |
| 9 | +# https://github.com/vitessce/vitessce/blob/main/examples/configs/src/view-configs/spatial-beta/kpmp.js |
| 10 | + |
| 11 | +# In[2]: |
| 12 | + |
| 13 | + |
| 14 | +from os.path import join |
| 15 | + |
| 16 | + |
| 17 | +# In[1]: |
| 18 | + |
| 19 | + |
| 20 | +import spatialdata |
| 21 | +from spatialdata import SpatialData |
| 22 | +from spatialdata.models import Image2DModel, Labels2DModel, TableModel |
| 23 | +from spatialdata.transformations import ( |
| 24 | + Affine, |
| 25 | + MapAxis, |
| 26 | + Scale, |
| 27 | + Sequence, |
| 28 | + Translation, |
| 29 | + get_transformation, |
| 30 | + set_transformation, |
| 31 | +) |
| 32 | +from anndata import read_zarr, AnnData |
| 33 | +import tifffile |
| 34 | +from tifffile import imread, TiffFile |
| 35 | +import xml.etree.ElementTree |
| 36 | +import io |
| 37 | +import zarr |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +# In[2]: |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +# In[3]: |
| 47 | + |
| 48 | + |
| 49 | +base_dir = join("..", "..", "..", "kpmp-f2f-march-2023", "S-1905-017737") |
| 50 | + |
| 51 | + |
| 52 | +# In[ ]: |
| 53 | + |
| 54 | + |
| 55 | +img_path = join(base_dir, "S-1905-017737_PAS_2of2_bf.ome.tif") |
| 56 | +seg_path = join(base_dir, "S-1905-017737_PAS_2of2.ome.tif") |
| 57 | + |
| 58 | +adata_paths = { |
| 59 | + "cortical_interstitia": join(base_dir, "Cortical Interstitium.adata.zarr"), |
| 60 | + "non_globally_sclerotic_glomeruli": join(base_dir, "Glomeruli.adata.zarr"), |
| 61 | + "globally_sclerotic_glomeruli": join(base_dir, "Globally Sclerotic Glomeruli.adata.zarr"), |
| 62 | + "tubules": join(base_dir, "Tubules with Area non infinity.adata.zarr"), |
| 63 | + "arteries_arterioles": None, |
| 64 | + "interstitialfibrosis_and_tubular_atrophy": join(base_dir, "IFTA.adata.zarr"), |
| 65 | + "peritubular_capillaries": join(base_dir, "Peritubular Capillaries renamed.adata.zarr"), |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +# In[ ]: |
| 70 | + |
| 71 | + |
| 72 | +# The shape of the data should be c(z)yx for 2D (3D) images |
| 73 | + |
| 74 | + |
| 75 | +# In[ ]: |
| 76 | + |
| 77 | + |
| 78 | +img_arr = imread(img_path) |
| 79 | + |
| 80 | + |
| 81 | +# In[ ]: |
| 82 | + |
| 83 | + |
| 84 | +seg_store = imread(seg_path, aszarr=True) |
| 85 | +seg_z = zarr.open(seg_store) |
| 86 | + |
| 87 | + |
| 88 | +# In[ ]: |
| 89 | + |
| 90 | + |
| 91 | +def clean_adata(adata): |
| 92 | + colnames = adata.obs.columns.tolist() |
| 93 | + adata.obs = adata.obs.rename(columns=dict([ (c, c.replace(" ", "_")) for c in colnames ])) |
| 94 | + return adata |
| 95 | + |
| 96 | + |
| 97 | +# In[ ]: |
| 98 | + |
| 99 | + |
| 100 | +sdata = SpatialData( |
| 101 | + images={ |
| 102 | + "image": Image2DModel.parse(img_arr, scale_factors=[2, 2, 2, 2, 2]), |
| 103 | + }, |
| 104 | + tables={ |
| 105 | + f"table_{obs_type}": TableModel.parse(clean_adata(read_zarr(adata_path))) |
| 106 | + for obs_type, adata_path in adata_paths.items() |
| 107 | + if adata_path is not None |
| 108 | + } |
| 109 | +) |
| 110 | + |
| 111 | +for i, obs_type in enumerate(adata_paths.keys()): |
| 112 | + sdata[f"labels_{obs_type}"] = Labels2DModel.parse(seg_z['1'][i, :, :], scale_factors=[2, 2, 2, 2]) |
| 113 | + # Scale by a factor of 2 since we are using the second resolution from the original file. |
| 114 | + scale = Scale([2.0, 2.0], axes=("y","x")) |
| 115 | + set_transformation(sdata[f"labels_{obs_type}"], scale, to_coordinate_system="global") |
| 116 | + |
| 117 | + |
| 118 | +# In[ ]: |
| 119 | + |
| 120 | + |
| 121 | +sdata.write(join(base_dir, "sdata.zarr"), overwrite=True) |
| 122 | +print(join(base_dir, "sdata.zarr")) |
| 123 | +print("Done") |
| 124 | + |
| 125 | +# In[ ]: |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | +# In[ ]: |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | + |
0 commit comments