Skip to content

feat: port create_lr_dataframe() into the package - #97

Merged
atuldeshpande merged 1 commit into
mainfrom
feat/create-lr-dataframe
Apr 29, 2026
Merged

feat: port create_lr_dataframe() into the package#97
atuldeshpande merged 1 commit into
mainfrom
feat/create-lr-dataframe

Conversation

@atuldeshpande

Copy link
Copy Markdown
Member

Summary

Promotes `create_lr_dataframe()` from the protocol-helpers vendor file into the package proper, per Orian's request. It's the natural tidy view of `calculate_lr_scores` / `calculate_gene_set_score` / `calculate_gene_set_specificity` output — those are already in the package, so the consumer should be too.

Two cleanups vs. the original

  • Removed the silent NA→0 coercion. The original first line was `lrscores[is.na(lrscores)] <- 0`, which is what masked the all-NaN LR matrix during the protocol render (the `ligand.symbol` column-name mismatch produced NaN, then this line zero-coerced everything). NAs now propagate; callers can `filter(!is.na(score))` or impute explicitly.
  • Doc/code mismatch on `@return` — original listed 8 columns, function returned 9. Updated to list all 9 (the missing one was `source_to_target`).

Constraint flagged via `@note`

The function reconstructs `source_cell_type` / `target_cell_type` from a joined string key (`andto`), so cell-type names must not contain `to` or `and` substrings. Documented at the top of the manpage.

Tests

  • Round-trip on a small 2-pair × 2-cell-type fixture
  • Explicit NA-preservation check (the regression we just fixed)

`devtools::test(filter = "create_lr_dataframe")` → 6 PASS, 0 FAIL.

🤖 Generated with Claude Code

Long-format reshape of the three matrices produced by
calculate_lr_scores() / calculate_gene_set_score() /
calculate_gene_set_specificity() — pivots them into a tidy
data frame keyed by (interaction, source_cell_type,
target_cell_type) with merged ligand/receptor gene names from
the lrpairs reference.

The function previously lived in the SpaceMarkers_Protocol repo's
R/protocol-helpers.R; Orian asked that we promote it to the
package proper since (a) it's the natural tidy view of the
package's own LR-scoring outputs, (b) all the dependencies
(reshape2, dplyr) are already in Imports, and (c) it's per-sample,
not multi-sample orchestration.

Two cleanups vs. the protocol-helpers version:

- Drop the unconditional `lrscores[is.na(lrscores)] <- 0` line.
  That was the silent-zero footgun that masked the all-NaN LR
  matrix bug we hit during the protocol render. NAs now propagate
  to the score column; callers can filter or impute explicitly.

- Doc/code mismatch on @return columns: original docstring listed
  8 columns; function returned 9 (source_to_target was tacked on
  at the end). Updated docstring to list all 9.

Also added a @note flagging the parsing constraint: cell-type
names must not contain "_to_" or "_and_" (the function uses
those as delimiters when reconstructing source/target from the
joined-key string).

Tests cover the round-trip on a small fixture plus an explicit
NA-preservation check (the regression we just fixed).
@atuldeshpande
atuldeshpande merged commit 60ed128 into main Apr 29, 2026
4 checks passed
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