Skip to content

Add load_seurat(), fix spatial-pattern NA handling, and rename - #106

Open
orian116 wants to merge 6 commits into
mainfrom
issues_07092026
Open

Add load_seurat(), fix spatial-pattern NA handling, and rename#106
orian116 wants to merge 6 commits into
mainfrom
issues_07092026

Conversation

@orian116

@orian116 orian116 commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

Add load_seurat(), fix spatial-pattern NA handling, and rename
directed overlap_scores columns

  • load_seurat(): load a Seurat object into a SpaceMarkersExperiment,
    mirroring load_anndata() — coerces via as.SingleCellExperiment(),
    attaches spatial patterns from a configurable deconv assay (skippable
    via deconv_assay = NULL), and derives spatial_params from the Seurat
    image's scale factors.
  • Drop NA-containing spots from spatial patterns (e.g. RCTD leaving
    unestimated barcodes as NA) in both get_spatial_features() and the
    spatial_patterns<- setter, with a message reporting rows removed,
    instead of erroring downstream in the mixture-model tests.
  • Rename calculate_overlap_directed()'s overlap_scores columns from
    pattern/influence to target/source, and update plot_overlap_scores()
    to match.
  • get_pairwise_interacting_genes() (SME method) now also populates
    undirected_scores via get_im_scores(), matching the directed
    workflow's single-call behavior.

claude and others added 6 commits July 9, 2026 20:10
- Add load_seurat() (R/preprocessing.R), mirroring load_anndata(): reads a
  Seurat .rds/object, coerces via as.SingleCellExperiment() into a
  SpaceMarkersExperiment, attaches spatial patterns from a configurable
  deconv assay (deconv_assay = NULL skips patterns with a pointer to
  get_spatial_features() instead of failing), and derives spatial_params
  from the named image's scale factors.
- Drop NA-containing spot rows from spatial patterns (e.g. RCTD leaving
  NAs for unestimated barcodes) via a shared .complete_pattern_rows()
  helper, wired into both get_spatial_features() and the
  spatial_patterns<- setter, with a message reporting how many rows were
  removed.
- Rename calculate_overlap_directed()'s overlap_scores columns from
  pattern/influence to target/source, and update plot_overlap_scores()'s
  shape detection to match.
- get_pairwise_interacting_genes() (SME method) now also populates
  undirected_scores via get_im_scores(), so a single call leaves the
  undirected workflow in the same "final result populated" state that
  calculate_gene_scores_directed() already gives the directed workflow.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018YdxsdbnYWrywAYxKMq9MK
…es-6j3ru2

Add load_seurat(), fix NA handling and overlap-score naming
Fix parallel-processing crashes and a cell-type-name parsing bug in
the ligand-receptor scoring pipeline

Three separate, unrelated bugs were blocking calculate_gene_set_specificity()
and calculate_lr_scores() from completing on real deconvolution data. All
three are now fixed; calculate_gene_scores_directed() was investigated but
left unchanged, since it was confirmed not to be the source of the
parallel-processing crash below.

1. .calculate_all_fc_scores() (used by calculate_gene_set_specificity())

Symptom: an intermittent crash ("Stop worker failed... wrong args for
   environment subassignment" / "Error: BiocParallel errors"), sometimes
appearing even after the real computation had already finished
correctly.

Cause: this function split its work across multiple CPU cores using
the BiocParallel package. BiocParallel has its own internal bug in the
code that collects results back from those parallel workers, and it can
surface at unpredictable times -- including after the actual work is
done -- in a way calling code can't reliably catch or prevent.

   Fix: switrenamed the parallel step to use R's built-in parallel package
(mclapply()) instead of BiocParallel. This still spreads the work across
multiple cores for speed, just through simpler, more stable code that
doesn't hit this bug. If parallel execution fails for any reason, it
   now falls back to processing cell types one at a time instead of
   crashing. Also removed a line that was changing global parallel-
   processing settings as a side effect, which wasn't needed and could
affect unrelated code later in the same session.

2. .calculate_fc_score()

Symptom: "not enough 'y' observations" error from R's wilcox.test().

   Cause: this function splits spots into a "high" and "low" group based
   on how much of a given cell type is present, then statistically
   compares gene expression between the two groups. With sparse,
   tie-heavy deconvolution values (common in RCTD output), one of those
   groups can end up completely empty, and the statistical test errors
   instead of handling that gracefully.

   Fix: added a check for an empty group before running the test; if
   either group has no spots, that gene/cell-type combination is now
   skipped (recorded as not significant) instead of crashing -- the same
   way the function already handled the separate all-zero-expression case.

3. calculate_lr_scores()

   Symptom: "subscript out of bounds" while matching up ligand and
   receptor scores.

   Cause: to match a ligand score column like
   "ACINAR_near_FIBROBLASTS_myCAF" to its receptor column, the function
   extracted the target cell-type name by cutting off everythi up to
   the *last* underscore. That works for simple names, but silently
   breaks for cell-type/subtype names that contain their own underscores
   (e.g. "FIBROBLASTS_myCAF"), yielding "myCAF" instead of
   "FIBROBLASTS_myCAF" and then failing to find that column.

   Fix: changed the parsing to split on the "_near_" separator
   specifically, instead of the last underscore, so cell-type names
   containing underscores are handled correctly.

Also included: .get_csv_features() now recognizes a barcode column
anywhere in a CSV's columns (not only one named exactly "barcode"), and
drops rows with a missing barcode value before they can cause a later
error.#
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.

2 participants