88from spatialdata import SpatialData
99
1010from . import (
11+ utils ,
1112 write_cell_categories ,
1213 write_gene_counts ,
1314 write_image ,
1415 write_polygons ,
1516 write_transcripts ,
1617)
1718from ._constants import FileNames , experiment_dict
18- from .utils import explorer_file_path , get_element , get_spatial_image , to_intrinsic
1919
2020log = logging .getLogger (__name__ )
2121
@@ -28,6 +28,7 @@ def write(
2828 points_key : str | None = None ,
2929 gene_column : str | None = None ,
3030 pixelsize : float = 0.2125 ,
31+ spot : bool = False ,
3132 layer : str | None = None ,
3233 polygon_max_vertices : int = 13 ,
3334 lazy : bool = True ,
@@ -64,6 +65,7 @@ def write(
6465 points_key: Name of the transcripts (key of `sdata.points`). This argument doesn't need to be provided if there is only one points key.
6566 gene_column: Column name of the points dataframe containing the gene names.
6667 pixelsize: Number of microns in a pixel. Invalid value can lead to inconsistent scales in the Explorer.
68+ spot: Whether the technology is based on spots
6769 layer: Layer of `sdata.table` where the gene counts are saved. If `None`, uses `sdata.table.X`.
6870 polygon_max_vertices: Maximum number of vertices for the cell polygons. A higher value will display smoother cells.
6971 lazy: If `True`, will not load the full images in memory (except if the image memory is below `ram_threshold_gb`).
@@ -73,7 +75,7 @@ def write(
7375 path : Path = Path (path )
7476 _check_explorer_directory (path )
7577
76- image_key , image = get_spatial_image (sdata , image_key , return_key = True )
78+ image_key , image = utils . get_spatial_image (sdata , image_key , return_key = True )
7779
7880 ### Saving cell categories and gene counts
7981 if sdata .table is not None :
@@ -94,22 +96,27 @@ def write(
9496 write_cell_categories (path , adata )
9597
9698 ### Saving cell boundaries
97- shapes_key , geo_df = get_element (sdata , "shapes" , shapes_key , return_key = True )
99+ shapes_key , geo_df = utils . get_element (sdata , "shapes" , shapes_key , return_key = True )
98100
99101 if _should_save (mode , "b" ) and geo_df is not None :
100- geo_df = to_intrinsic (sdata , geo_df , image_key )
102+ geo_df = utils . to_intrinsic (sdata , geo_df , image_key )
101103
102104 if sdata .table is not None :
103105 geo_df = geo_df .loc [adata .obs [adata .uns ["spatialdata_attrs" ]["instance_key" ]]]
104106
107+ geo_df = utils ._standardize_shapes (geo_df )
108+
105109 write_polygons (path , geo_df .geometry , polygon_max_vertices , pixelsize = pixelsize )
106110
107111 ### Saving transcripts
108- df = get_element (sdata , "points" , points_key )
112+ if spot and sdata .table is not None :
113+ df , gene_column = utils ._spot_transcripts_origin (adata )
114+ else :
115+ df = utils .get_element (sdata , "points" , points_key )
116+ df = utils .to_intrinsic (sdata , df , image_key )
109117
110118 if _should_save (mode , "t" ) and df is not None :
111119 if gene_column is not None :
112- df = to_intrinsic (sdata , df , image_key )
113120 write_transcripts (path , df , gene_column , pixelsize = pixelsize )
114121 else :
115122 log .warn ("The argument 'gene_column' has to be provided to save the transcripts" )
@@ -172,7 +179,7 @@ def write_metadata(
172179 is_dir: If `False`, then `path` is a path to a single file, not to the Xenium Explorer directory.
173180 pixelsize: Number of microns in a pixel. Invalid value can lead to inconsistent scales in the Explorer.
174181 """
175- path = explorer_file_path (path , FileNames .METADATA , is_dir )
182+ path = utils . explorer_file_path (path , FileNames .METADATA , is_dir )
176183
177184 with open (path , "w" ) as f :
178185 metadata = experiment_dict (image_key , shapes_key , n_obs , pixelsize )
0 commit comments