Skip to content

[Bug] KeyError in score_interactions when some interactions missing from scoring matrix #225

Description

@cafferychen777

Bug Description

When running cpdb_statistical_analysis_method.call() with score_interactions=True, a KeyError is raised because some interaction pairs in interaction_scores['interacting_pair'] are not found in the interacting_pair2score dictionary.

Error Location

File: cellphonedb/utils/scoring_utils.py, line 293:

interaction_scores[ct_pair] = [interacting_pair2score[id] for id in interaction_scores['interacting_pair']]

Error Message

KeyError: 'BMP8A_ACVR_1A2B_receptor'

Steps to Reproduce

  1. Load a human spatial transcriptomics dataset (e.g., 10x Visium human lymph node)
  2. Run CellPhoneDB v5 statistical analysis with score_interactions=True
from cellphonedb.src.core.methods import cpdb_statistical_analysis_method

result = cpdb_statistical_analysis_method.call(
    cpdb_file_path=db_path,
    meta_file_path=meta_file,
    counts_file_path=counts_file,
    counts_data='hgnc_symbol',
    threshold=0.1,
    iterations=100,
    output_path=output_dir,
    score_interactions=True,  # This causes the error
)

Expected Behavior

The scoring should handle missing interactions gracefully, either by:

  1. Skipping interactions not found in the scoring matrix
  2. Assigning a default score (e.g., 0 or NaN)

Workaround

Setting score_interactions=False avoids the error, but disables the interaction scoring feature.

Environment

  • CellPhoneDB version: 5.0.0
  • Python version: 3.10
  • OS: macOS

Additional Context

This bug was discovered while integrating CellPhoneDB into ChatSpatial, an MCP server for spatial transcriptomics analysis.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions