Skip to content

Order scdesign2 input by spatial cluster - #22

Merged
rcannood merged 5 commits into
mainfrom
fix/scdesign2-ordering
Jul 28, 2026
Merged

Order scdesign2 input by spatial cluster#22
rcannood merged 5 commits into
mainfrom
fix/scdesign2-ordering

Conversation

@rcannood

Copy link
Copy Markdown
Member

simulate_count_scDesign2() writes its output one cell type at a time, in contiguous blocks (new_count[, llim:ulim] <- ... in the package source), but the script attached coordinates from the input in its original order. The datasets are not sorted by spatial_cluster -- MOBNEW starts 1,2,3,3,4,2,4,1 -- so every spot received the counts of an unrelated spot, and the spatial signal was destroyed before the metrics saw it.

It shows in the results: scdesign2 came last on ks_statistic_morans_I (25.7, against 1.8 for scdesign3_nb) and last on svg_precision (0.442). Every other method already does input[order(input$obs$spatial_cluster)] first; this one now does too.

While in there: cell_type_sel and cell_type_prop are matched to each other by position, but were built with unique() and table() respectively. Those only coincide when the labels happen to sort the same way -- with 10 or more clusters table() gives 1,10,11,12,2,... and the proportions get attached to the wrong clusters. Both now derive from unique() on the sorted data, which is also exactly the block order:

cell_types       : 1,2,3,4,5,6,7,8,9,10,11,12
blocks in data   : 1,2,3,4,5,6,7,8,9,10,11,12
order matches    : TRUE
block sizes exact: TRUE
sort(unique())   : 1,10,11,12,2,3,4,5,6,7,8,9   <- what it would have been

Block sizes come out exact, so coordinates line up spot for spot rather than approximately.

Checklist before requesting a review

  • I have performed a self-review of my code

  • Check the correct box. Does this PR contain:

    • Breaking changes
    • New functionality
    • Major changes
    • Minor changes
    • Bug fixes
  • Proposed changes are described in the CHANGELOG.md

  • CI Tests succeed and look good!

rcannood added 2 commits July 28, 2026 14:20
`simulate_count_scDesign2()` writes its output one cell type at a time, in
contiguous blocks (`new_count[, llim:ulim] <- ...`), but scdesign2 was pulling
the coordinates from the input in its original order. The datasets are not
sorted by spatial_cluster -- MOBNEW starts 1,2,3,3,4,2,4,1 -- so every spot got
the counts of an unrelated spot and the spatial signal was destroyed before
the metrics ever saw it. It shows: scdesign2 came last on morans_I (25.7,
against 1.8 for scdesign3_nb) and on svg_precision.

Every other method already does `input[order(input$obs$spatial_cluster)]`
first; this one now does too.

Also make cell_type_sel and cell_type_prop agree: they are matched by
position, but were built with `unique()` and `table()` respectively, which
only coincide when the labels happen to sort the same way. Both now come from
`unique()` on the sorted data, which also matches the block order for 10 or
more clusters, where numeric and character sorting disagree.
rcannood added 3 commits July 28, 2026 15:00
Sorting for the simulation is only half of it: the result has to be sorted
back, or scdesign2 joins the group of methods whose output is in a different
spot order than the real dataset and whose clustering_ari is therefore scored
against mismatched spots. See #31.
@rcannood
rcannood merged commit 69843d6 into main Jul 28, 2026
2 checks passed
@rcannood
rcannood deleted the fix/scdesign2-ordering branch July 28, 2026 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant