phenocor.analysis package

phenocor.analysis.hpo_correlation_analyzer module

class CorrelationResult(correlation_results: DataFrame, coef_matrix: DataFrame, pval_matrix: DataFrame, hpo_labels: dict[str, str] | None = None)

Bases: object

Store and summarize pairwise HPO correlation results.

This class contains the long-format pairwise association results, the symmetric Phi coefficient matrix, the corresponding raw p-value matrix, and optional HPO term labels.

Statistical calculations are performed by HPOCorrelationAnalyzer. This class provides utilities for filtering, exporting, and visualizing the resulting associations.

Parameters:
  • correlation_results (pd.DataFrame) – Long-format table containing pairwise HPO association results.

  • coef_matrix (pd.DataFrame) – Symmetric matrix of Phi coefficients.

  • pval_matrix (pd.DataFrame) – Symmetric matrix of raw Fisher’s exact test p-values.

  • hpo_labels (dict[str, str] | None, optional) – Mapping from HPO term IDs to human-readable labels.

get_significant_correlations(adj_pval_threshold: float = 0.05, *, min_abs_correlation: float | None = None) → DataFrame

Return statistically significant pairwise correlations.

Parameters:
  • adj_pval_threshold (float, default=0.05) – Adjusted p-value significance threshold.

  • min_abs_correlation (float | None, optional) – Minimum absolute Phi coefficient.

Returns:

Significant pairwise correlations satisfying the specified filters.

Return type:

pd.DataFrame

plot_correlation_heatmap_with_significance(*, adj_pval_threshold: float | None = 0.05, min_abs_correlation: float | None = None, title: str | None = None) → Figure

Create an interactive heatmap of pairwise HPO correlations.

By default, only correlations with adjusted p-values below 0.05 are displayed. Correlations can additionally be filtered by absolute Phi coefficient.

Parameters:
  • adj_pval_threshold (float | None, default=0.05) – Adjusted p-value threshold. If None, no significance filtering is applied.

  • min_abs_correlation (float | None, optional) – Minimum absolute Phi coefficient. If None, no correlation-strength filtering is applied.

  • title (str | None, optional) – Optional title displayed for the heatmap.

Returns:

Interactive correlation heatmap.

Return type:

plotly.graph_objects.Figure

Raises:

ValueError – If no correlations remain after filtering.

save_correlation_heatmap(output_file: str | Path = 'correlation_heatmap.html', *, adj_pval_threshold: float | None = 0.05, min_abs_correlation: float | None = None, title: str | None = None, width: int | None = None, height: int | None = None, scale: float = 2.0) → None

Save the correlation heatmap as HTML or a static image.

Parameters:
  • output_file (str | Path, default="correlation_heatmap.html") – Output path. Supported extensions are .html, .png, .svg, .pdf, and .jpg.

  • adj_pval_threshold (float | None, default=0.05) – Adjusted p-value threshold. If None, no significance filtering is applied.

  • min_abs_correlation (float | None, optional) – Minimum absolute Phi coefficient.

  • title (str | None, optional) – Optional subtitle displayed below the main heatmap title.

  • width (int | None, optional) – Export width in pixels for static image formats.

  • height (int | None, optional) – Export height in pixels for static image formats.

  • scale (float, default=2.0) – Resolution multiplier for raster image formats.

Raises:

ValueError – If the output format is unsupported.

save_correlation_results(output_file: str | Path = 'correlation_results.csv', *, adj_pval_threshold: float | None = 0.05, min_abs_correlation: float | None = None) → None

Save pairwise correlation results.

By default, only correlations with adjusted p-values below 0.05 are saved. If adj_pval_threshold is None, no significance filtering is applied.

Parameters:
  • output_file (str | Path, default="correlation_results.csv") – Output file path. Supported formats are .csv and .xlsx.

  • adj_pval_threshold (float | None, default=0.05) – Adjusted p-value threshold. If None, no significance filtering is applied.

  • min_abs_correlation (float | None, optional) – Minimum absolute Phi coefficient. If None, no correlation-strength filtering is applied.

Raises:

ValueError – If the output format is unsupported.

class HPOCorrelationAnalyzer(dataset: PhenotypeDataset)

Bases: object

Analyze pairwise associations between HPO terms.

Pairwise associations are quantified using the Phi coefficient. Statistical significance is assessed using Fisher’s exact test, followed by Benjamini-Hochberg correction for multiple testing.

Only ontology-unrelated HPO term pairs with sufficient pairwise non-missing observations are evaluated.

compute_correlation_matrix(min_individuals: int = 20, n_jobs: int = -1, include_pmids: bool = True) → CorrelationResult

Calculate pairwise associations between HPO terms.

Self-pairs and ancestor-descendant pairs are excluded using the ontology relationship mask. A remaining pair is tested only if at least min_individuals individuals have non-missing values for both HPO terms.

Parameters:
  • min_individuals (int, default=20) – Minimum number of individuals with non-missing values for both HPO terms required to test a pairwise association. This is a pair-specific effective sample size threshold, not a minimum cohort size.

  • n_jobs (int, default=-1) – Number of parallel jobs. -1 uses all available CPUs.

  • include_pmids (bool, default=True) – If True, aggregate unique PubMed IDs from individuals contributing to each tested pair.

Returns:

Pairwise association results together with symmetric Phi coefficient and raw p-value matrices.

Return type:

CorrelationResult

Raises:
  • TypeError – If min_individuals is not an integer.

  • ValueError – If min_individuals is less than 1.