Skip to content

Commit 9f13498

Browse files
committed
Update xenium morton code nb
1 parent 3051c3f commit 9f13498

1 file changed

Lines changed: 39 additions & 20 deletions

File tree

docs/notebooks/spatial_data_xenium_morton.ipynb

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
"from vitessce.data_utils import (\n",
4848
" sdata_morton_sort_points,\n",
4949
" sdata_points_process_columns,\n",
50-
" sdata_points_write_bounding_box_attrs,\n",
5150
" sdata_points_modify_row_group_size,\n",
5251
" sdata_morton_query_rect,\n",
5352
")"
@@ -59,7 +58,8 @@
5958
"metadata": {},
6059
"outputs": [],
6160
"source": [
62-
"from spatialdata import read_zarr"
61+
"from spatialdata import read_zarr\n",
62+
"from spatialdata.models import PointsModel"
6363
]
6464
},
6565
{
@@ -70,7 +70,7 @@
7070
"source": [
7171
"data_dir = \"data\"\n",
7272
"zip_filepath = join(data_dir, \"xenium_rep1_io.spatialdata.zarr.zip\")\n",
73-
"spatialdata_filepath = join(data_dir, \"xenium_rep1_io.spatialdata.zarr\")"
73+
"spatialdata_filepath = join(data_dir, \"xenium_rep1_io2.spatialdata.zarr\")"
7474
]
7575
},
7676
{
@@ -82,7 +82,7 @@
8282
"if not isdir(spatialdata_filepath):\n",
8383
" if not isfile(zip_filepath):\n",
8484
" os.makedirs(data_dir, exist_ok=True)\n",
85-
" urlretrieve('https://s3.embl.de/spatialdata/spatialdata-sandbox/xenium_rep1_io.zip', zip_filepath)\n",
85+
" urlretrieve('https://data-2.vitessce.io/sdata-datasets/xenium_rep1_io.spatialdata.zarr.zip', zip_filepath)\n",
8686
" with zipfile.ZipFile(zip_filepath,\"r\") as zip_ref:\n",
8787
" zip_ref.extractall(data_dir)\n",
8888
" os.rename(join(data_dir, \"data.zarr\"), spatialdata_filepath)\n",
@@ -161,6 +161,15 @@
161161
"sdata.points['transcripts'].head()"
162162
]
163163
},
164+
{
165+
"cell_type": "code",
166+
"execution_count": null,
167+
"metadata": {},
168+
"outputs": [],
169+
"source": [
170+
"transformations = sdata['transcripts'].attrs['transform']"
171+
]
172+
},
164173
{
165174
"cell_type": "markdown",
166175
"metadata": {},
@@ -223,15 +232,7 @@
223232
"metadata": {},
224233
"outputs": [],
225234
"source": [
226-
"sdata[\"transcripts_with_morton_codes\"] = ddf\n",
227-
"sdata.write_element(\"transcripts_with_morton_codes\")"
228-
]
229-
},
230-
{
231-
"cell_type": "markdown",
232-
"metadata": {},
233-
"source": [
234-
"### Step 4. Write bounding box metadata with `sdata_points_write_bounding_box_attrs`"
235+
"del ddf.attrs['transform']"
235236
]
236237
},
237238
{
@@ -240,14 +241,15 @@
240241
"metadata": {},
241242
"outputs": [],
242243
"source": [
243-
"sdata_points_write_bounding_box_attrs(sdata, \"transcripts_with_morton_codes\")"
244+
"sdata[\"transcripts_with_morton_codes\"] = PointsModel.parse(ddf, feature_key=\"feature_name\", instance_key=\"cell_id\", transformations=transformations)\n",
245+
"sdata.write_element(\"transcripts_with_morton_codes\")"
244246
]
245247
},
246248
{
247249
"cell_type": "markdown",
248250
"metadata": {},
249251
"source": [
250-
"### Step 5. Modify the row group sizes of the Parquet files with `sdata_points_modify_row_group_size`"
252+
"### Step 4. Modify the row group sizes of the Parquet files with `sdata_points_modify_row_group_size`"
251253
]
252254
},
253255
{
@@ -278,7 +280,7 @@
278280
"import pyarrow.parquet as pq\n",
279281
"from os.path import join\n",
280282
"\n",
281-
"parquet_file = pq.ParquetFile(join(sdata.path, \"points\", \"transcripts_with_morton_codes\", \"points.parquet\", \"part.0.parquet\"))\n",
283+
"parquet_file = pq.ParquetFile(join(sdata.path, \"points\", \"transcripts_with_morton_codes\", \"points.parquet\", \"part.1.parquet\"))\n",
282284
"\n",
283285
"# Get the number of row groups in this part-0 file.\n",
284286
"num_groups = parquet_file.num_row_groups\n",
@@ -308,17 +310,28 @@
308310
"wrapper = SpatialDataWrapper(\n",
309311
" sdata_path=spatialdata_filepath,\n",
310312
" # The following paths are relative to the root of the SpatialData zarr store on-disk.\n",
311-
" image_path=\"images/rasterized\",\n",
313+
" image_path=\"images/morphology_focus\",\n",
312314
" table_path=\"tables/table\",\n",
313315
" obs_feature_matrix_path=\"tables/table/X\",\n",
314-
" obs_spots_path=\"shapes/cells\",\n",
316+
" obs_spots_path=\"shapes/cell_circles\",\n",
315317
" coordinate_system=\"global\",\n",
316318
" coordination_values={\n",
317319
" # The following tells Vitessce to consider each observation as a \"spot\"\n",
318320
" \"obsType\": \"cell\",\n",
319321
" }\n",
320322
")\n",
321-
"dataset = vc.add_dataset(name='MERFISH').add_object(wrapper)\n",
323+
"points_wrapper = SpatialDataWrapper(\n",
324+
" sdata_path=spatialdata_filepath,\n",
325+
" # The following paths are relative to the root of the SpatialData zarr store on-disk.\n",
326+
" obs_points_path=\"points/transcripts_with_morton_codes\",\n",
327+
" obs_feature_matrix_path=\"tables/dense_table/X\",\n",
328+
" coordinate_system=\"global\",\n",
329+
" coordination_values={\n",
330+
" \"obsType\": \"point\",\n",
331+
" \"featureType\": \"gene\",\n",
332+
" }\n",
333+
")\n",
334+
"dataset = vc.add_dataset(name='MERFISH').add_object(wrapper).add_object(points_wrapper)\n",
322335
"\n",
323336
"# Add views (visualizations) to the configuration:\n",
324337
"spatial = vc.add_view(\"spatialBeta\", dataset=dataset)\n",
@@ -332,6 +345,12 @@
332345
" }]),\n",
333346
"}, scope_prefix=get_initial_coordination_scope_prefix(\"A\", \"obsSpots\"))\n",
334347
"\n",
348+
"vc.link_views_by_dict([spatial, layer_controller], {\n",
349+
" 'pointLayer': CL([{\n",
350+
" 'obsType': 'point',\n",
351+
" }]),\n",
352+
"}, scope_prefix=get_initial_coordination_scope_prefix(\"A\", \"obsPoints\"))\n",
353+
"\n",
335354
"vc.link_views([spatial, layer_controller, feature_list, obs_sets], ['obsType'], [wrapper.obs_type_label])\n",
336355
"\n",
337356
"# Layout the views\n",
@@ -379,7 +398,7 @@
379398
"name": "python",
380399
"nbconvert_exporter": "python",
381400
"pygments_lexer": "ipython3",
382-
"version": "3.10.14"
401+
"version": "3.12.5"
383402
}
384403
},
385404
"nbformat": 4,

0 commit comments

Comments
 (0)