Add load_seurat(), fix spatial-pattern NA handling, and rename - #106
Open
orian116 wants to merge 6 commits into
Open
Add load_seurat(), fix spatial-pattern NA handling, and rename#106orian116 wants to merge 6 commits into
orian116 wants to merge 6 commits into
Conversation
- 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
…attern NA handling
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.#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add load_seurat(), fix spatial-pattern NA handling, and rename
directed overlap_scores columns
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.
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.
pattern/influence to target/source, and update plot_overlap_scores()
to match.
undirected_scores via get_im_scores(), matching the directed
workflow's single-call behavior.