Skip to content

Commit 182f3e1

Browse files
Merge pull request #354 from gustaveroussy/no_owerwrite_prior
Do not overwrite the prior shape key in the transcript dataframe
2 parents 5768b55 + 4f0220e commit 182f3e1

File tree

7 files changed

+17
-18
lines changed

7 files changed

+17
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
### Added
1313
- Setting `sopa.settings.simplification_tolerance` to change the default shapely tolerance. For instance, set it to `0.1` for low simplification, or `0` for no simplification (#340)
1414
- Add an argument to load cells_boundaries and cells_table in `sopa.io.merscope` (`False` by default) (#346)
15+
- Do not overwrite the prior shape key in the transcript dataframe (useful if running multiple times `make_transcript_patches`)
1516

1617
## [2.1.8] - 2025-10-04
1718

sopa/_constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class SopaKeys:
1515
PATCHES_ILOCS = "ilocs"
1616
ROI = "region_of_interest"
1717
PRIOR_SHAPES_KEY = "prior_shapes_key"
18+
SOPA_PRIOR = "sopa_prior"
1819
POINTS_KEY = "points_key"
1920

2021
# Other SpatialData keys

sopa/patches/_transcripts.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ def __init__(
4949

5050
self.prior_shapes_key = prior_shapes_key
5151
self.unassigned_value = unassigned_value
52+
5253
self.min_points_per_patch = min_points_per_patch
5354
self.min_cells_per_patch = min_cells_per_patch
55+
5456
self.csv_name = csv_name
5557
self.centroids_csv_name = centroids_csv_name
5658
self.write_cells_centroids = write_cells_centroids
@@ -83,15 +85,16 @@ def assign_prior_segmentation(self) -> None:
8385
"Unassigned value is not needed when using a prior segmentation based on existing shapes"
8486
)
8587

86-
return assign_transcript_to_cell(
87-
self.sdata, self.points_key, self.prior_shapes_key, self.prior_shapes_key, unassigned_value=0
88+
assign_transcript_to_cell(
89+
self.sdata, self.points_key, self.prior_shapes_key, SopaKeys.SOPA_PRIOR, unassigned_value=0
8890
)
91+
return
8992

9093
assert self.prior_shapes_key in self.points.columns, (
9194
f"Prior-segmentation column {self.prior_shapes_key} not found in sdata['{self.points_key}']"
9295
)
9396

94-
self.points[self.prior_shapes_key] = _unassigned_to_zero(
97+
self.points[SopaKeys.SOPA_PRIOR] = _unassigned_to_zero(
9598
self.points[self.prior_shapes_key], self.unassigned_value
9699
)
97100

@@ -109,7 +112,7 @@ def get_prior_centroids(self) -> gpd.GeoDataFrame:
109112
"x": centroids.geometry.x,
110113
"y": centroids.geometry.y,
111114
"z": 0,
112-
self.prior_shapes_key: range(1, len(centroids) + 1),
115+
SopaKeys.SOPA_PRIOR: range(1, len(centroids) + 1),
113116
},
114117
geometry=centroids,
115118
)

sopa/segmentation/_transcripts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,6 @@ def _check_transcript_patches(sdata: SpatialData, with_prior: bool = False):
196196

197197
if with_prior:
198198
assert SopaKeys.PRIOR_SHAPES_KEY in sdata[SopaKeys.TRANSCRIPTS_PATCHES].columns, (
199-
"You need to create the transcript patches with a `prior_shapes_key`. "
200-
"For that, you can run cellpose first, and then run again `sopa.make_transcript_patches` with `prior_shapes_key='cellpose_boundaries'`"
199+
"You need to run `sopa.make_transcript_patches` with a `prior_shapes_key`. "
200+
"You can provide `prior_shapes_key='auto'` if your technology has a prior segmentation, or `prior_shapes_key='cellpose_boundaries'` if you ran cellpose segmentation first."
201201
)

sopa/segmentation/methods/_baysor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ def baysor(
4949
"""
5050
_check_transcript_patches(sdata)
5151

52-
prior_shapes_key = None
53-
if SopaKeys.PRIOR_SHAPES_KEY in sdata.shapes[SopaKeys.TRANSCRIPTS_PATCHES]:
54-
prior_shapes_key = sdata.shapes[SopaKeys.TRANSCRIPTS_PATCHES][SopaKeys.PRIOR_SHAPES_KEY].iloc[0]
52+
prior_shapes_key = (
53+
SopaKeys.SOPA_PRIOR if SopaKeys.PRIOR_SHAPES_KEY in sdata.shapes[SopaKeys.TRANSCRIPTS_PATCHES] else None
54+
)
5555

5656
if config is None or not len(config):
5757
config = _get_default_config(sdata, prior_shapes_key, scale)

sopa/segmentation/methods/_comseg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def comseg(
5858

5959
assert "gene_column" in config, "'gene_column' not found in config"
6060

61-
config["prior_name"] = sdata[SopaKeys.TRANSCRIPTS_PATCHES][SopaKeys.PRIOR_SHAPES_KEY].iloc[0]
61+
config["prior_name"] = SopaKeys.SOPA_PRIOR
6262

6363
if patch_index is not None:
6464
patch_dir = get_transcripts_patches_dirs(sdata, patch_index)

sopa/segmentation/methods/_proseg.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def proseg(
4444
infer_presets: Whether to infer the proseg presets based on the columns of the transcripts dataframe.
4545
key_added: Name of the shapes element to be added to `sdata.shapes`.
4646
"""
47-
_check_transcript_patches(sdata)
47+
_check_transcript_patches(sdata, with_prior=True)
4848

4949
points_key = sdata[SopaKeys.TRANSCRIPTS_PATCHES][SopaKeys.POINTS_KEY].iloc[0]
5050

@@ -98,20 +98,14 @@ def _get_proseg_command(
9898
) -> str:
9999
proseg_executable = _get_executable_path("proseg", ".cargo")
100100

101-
assert SopaKeys.PRIOR_SHAPES_KEY in sdata.shapes[SopaKeys.TRANSCRIPTS_PATCHES], (
102-
"Proseg requires a prior. Re-run `sopa.make_transcript_patches` with a `prior_shapes_key`."
103-
)
104-
105-
prior_shapes_key = sdata.shapes[SopaKeys.TRANSCRIPTS_PATCHES][SopaKeys.PRIOR_SHAPES_KEY].iloc[0]
106-
107101
feature_key = get_feature_key(sdata[points_key], raise_error=True)
108102

109103
use_zarr = _use_zarr_output(proseg_executable)
110104

111105
if infer_presets:
112106
command_line_suffix = _add_presets(command_line_suffix, sdata[points_key].columns)
113107

114-
return f"{proseg_executable} transcripts.csv -x x -y y -z z --gene-column {feature_key} --cell-id-column {prior_shapes_key} --cell-id-unassigned 0 {'--exclude-spatialdata-transcripts' if use_zarr else ''} {command_line_suffix}"
108+
return f"{proseg_executable} transcripts.csv -x x -y y -z z --gene-column {feature_key} --cell-id-column {SopaKeys.SOPA_PRIOR} --cell-id-unassigned 0 {'--exclude-spatialdata-transcripts' if use_zarr else ''} {command_line_suffix}"
115109

116110

117111
def _add_presets(command_line_suffix: str, columns: list[str]) -> str:

0 commit comments

Comments
 (0)