11from __future__ import annotations
22from itertools import combinations
3- from pathlib import Path
4- from typing import TYPE_CHECKING , Optional
3+ from typing import TYPE_CHECKING , Literal , Optional
54
65import numpy as np
76import scanpy as sc
1211from scipy .spatial .distance import squareform
1312from scipy .stats import fisher_exact
1413
15- from .enrichment import calculate_cluster_enrichment
14+ from .enrichment import _load_gmt , calculate_cluster_enrichment
15+
16+ __all__ = [ # re-export private helper for callers/tests that imported it here
17+ "_load_gmt" ,
18+ "calculate_cluster_enrichment" ,
19+ "dendrogram_cherry_pairs" ,
20+ "merge_clusters_go" ,
21+ "merge_small_clusters" ,
22+ "paga_dendrogram" ,
23+ ]
1624
1725if TYPE_CHECKING :
1826 from anndata import AnnData
1927
2028
21- # ── Gene-set loading ──────────────────────────────────────────────────────────
22-
23-
24- def _load_gmt (path : str | dict [str , list [str ]] | None ) -> dict [str , list [str ]]:
25- """Parse a GMT file or gseapy library name into a gene-set dict.
26-
27- Parameters
28- ----------
29- path
30- A ``dict`` (returned as-is), a path to a ``.gmt`` file, a gseapy
31- library name (fetched via ``gseapy.get_library``), or ``None``
32- (returns the default UniProt subcellular compartment gene sets
33- bundled with grassp).
34-
35- Returns
36- -------
37- dict[str, list[str]]
38- Mapping of term name to list of gene symbols.
39- """
40- if path is None :
41- path = str (
42- Path (__file__ ).parent .parent
43- / 'datasets'
44- / 'external'
45- / 'custom_goterms_genes_reviewed.gmt'
46- )
47- if isinstance (path , dict ):
48- return path
49- import os
50-
51- if not os .path .exists (path ):
52- import gseapy as gp
53-
54- return gp .get_library (name = path )
55- gene_sets : dict [str , list [str ]] = {}
56- with open (path ) as f :
57- for line in f :
58- parts = line .strip ().split ('\t ' )
59- if len (parts ) < 3 :
60- continue
61- term = parts [0 ]
62- genes = [g for g in parts [2 :] if g ]
63- gene_sets [term ] = genes
64- return gene_sets
65-
66-
6729# ── Pair-testing helpers ──────────────────────────────────────────────────────
6830
6931
@@ -907,6 +869,7 @@ def merge_clusters_go(
907869 connectivity_lower : float = 0.5 ,
908870 cluster_col : str = 'leiden' ,
909871 gene_sets_path : Optional [str | dict ] = None ,
872+ species : Literal ['hsap' , 'mmus' , 'scer' ] = 'hsap' ,
910873 gene_name_key : str = 'Gene_name_canonical' ,
911874 compartment_col : str = 'Cell_compartment' ,
912875 key_added : str = 'leiden_merged' ,
@@ -951,8 +914,15 @@ def merge_clusters_go(
951914 ``adata.obs`` column with initial cluster labels.
952915 gene_sets_path
953916 Path to a GMT file, a gseapy library name, a pre-loaded
954- ``dict[str, list[str]]``, or ``None`` (uses the default UniProt
955- subcellular compartment gene sets bundled with grassp).
917+ ``dict[str, list[str]]``, or ``None`` (uses the consolidated UniProt
918+ subcellular compartment gene sets for the chosen ``species``).
919+ species
920+ Species code used to pick the default gene-set file when
921+ ``gene_sets_path`` is ``None``. One of ``"hsap"`` (human,
922+ ``consolidated_goterms_human.gmt``), ``"mmus"`` (mouse,
923+ ``consolidated_goterms_mouse.gmt``), or ``"scer"`` (yeast,
924+ ``consolidated_goterms_yeast.gmt``). Default ``"hsap"``. Ignored when
925+ an explicit ``gene_sets_path`` is provided.
956926 gene_name_key
957927 ``adata.obs`` column with gene/protein names used for enrichment.
958928 compartment_col
@@ -971,7 +941,7 @@ def merge_clusters_go(
971941 If True, plot the initial PAGA dendrogram after convergence with leaf
972942 lines colored by compartment term and merge nodes colored by p-value.
973943 """
974- gene_sets = _load_gmt (gene_sets_path )
944+ gene_sets = _load_gmt (gene_sets_path , species = species )
975945
976946 # Initialise working column from the original clustering
977947 adata .obs [key_added ] = adata .obs [cluster_col ].astype (str ).astype ('category' )
0 commit comments