GeneSet — a named, immutable group of gene identifiers — and the shared select_genes helper for subsetting a prediction matrix to a set’s genes.
M-Optimus emits one predicted expression value per gene of a fixed panel (gene_names). A GeneSet names a subset of those genes (for example a hypoxia signature) so callers can slice predictions down to a biologically meaningful group.
Gene sets are usually authored in gene symbols while prediction panels are often Ensembl-indexed. Pass a mapping (symbol -> Ensembl id) to GeneSet.resolve, GeneSet.coverage, or select_genes to bridge the two id spaces. Genes that do not resolve against the panel are dropped with a structured warning; a set that resolves to no genes at all raises BioptimusValueError tagged BIO-GENE-001.
Two kinds of identifier collision are handled keep-first + warn rather than silently: a many-to-one mapping (two set genes mapping to one panel column) drops the later gene so the column is not double-counted (gene_set_duplicate_columns), and a panel that lists an id more than once binds to its first column rather than the last (panel_duplicate_genes).
GeneIdType
Supported gene identifier spaces for aGeneSet.
GeneSet
name- The set name.genes- The de-duplicated gene identifiers in first-seen order.id_type- The identifier space ofgenes.description- Optional description.
__len__
from_gene_list
"Ensembl ID") when the exact name is absent.
The identifier space of the genes. Defaults to
"ensembl" since flat gene lists are typically Ensembl ids.GeneSet containing the file’s gene identifiers.
Raises:
BioptimusValueError- If the gene column is missing, the file is missing/unreadable/malformed, or the list is empty, taggedBIO-CFG-001.
to_spec
from_spec rebuilds an identical set. Used to persist gene-set definitions (for example in a cohort manifest).
Returns:
A dict with name, genes, id_type, and description.
from_spec
GeneSet from a to_spec dict.
A mapping with
name and genes and optional id_type / description (as produced by to_spec).BioptimusValueError- Ifnameorgenesis missing, orid_typeis not a supported identifier space, taggedBIO-CFG-001.
coverage
(hit, total, missing) tuple where hit is the number of set
genes found in the panel, total is the set size, and missing
lists the set genes (as authored) not found in the panel.
resolve
mapping).
Returns:
An int array of column indices into gene_names, in the set’s
gene order, for the genes that resolved.
Raises:
BioptimusValueError- If none of the set’s genes resolve against the panel, taggedBIO-GENE-001.
resolve_with_mask
resolve, but also returns a boolean mask over the set’s (de-duplicated) genes marking which ones resolved. Callers that carry per-gene data aligned to genes (for example signature weights) use the mask to drop the same positions the resolution dropped, keeping the two aligned.
Returns:
A (indices, kept_mask) tuple where indices are the resolved
column indices (in set order) and kept_mask is a boolean array
of length len(self) that is True at each gene that resolved.
Raises:
BioptimusValueError- If none of the set’s genes resolve against the panel, taggedBIO-GENE-001.
select_genes
Optional symbol-to-panel-id mapping forwarded to
GeneSet.resolve.(selected, names) tuple where selected is an (N, k) array
of the resolved columns (in the set’s gene order) and names are the
corresponding panel identifiers.
Raises:
BioptimusValueError- If values does not match the panel, taggedBIO-CFG-001, or if none of the set’s genes resolve, taggedBIO-GENE-001.
GeneSetCollection
GeneSet objects keyed by set name.
Behaves as a read-only Mapping[str, GeneSet] (supports coll["name"], iteration over names, len, in, .values(), .items()), and adds batch operations that resolve every member against a panel in one pass.
The identifier space used for members supplied as plain iterables. Ignored for members that are already
GeneSet objects.BioptimusValueError- Ifmembersis empty, taggedBIO-CFG-001.
from_sets
BioptimusValueError- If two sets share the same name, taggedBIO-CFG-001.
to_spec
sets list of {"key", "spec"} entries (see
GeneSet.to_spec), in member order.
from_spec
GeneSetCollection from a to_spec dict.
Returns:
The reconstructed collection, preserving member keys and order.
Raises:
BioptimusValueError- Ifsetsis missing or an entry lackskeyorspec, taggedBIO-CFG-001.
from_gmt
GeneSet per GMT line.
Raises:
BioptimusValueError- If two GMT lines share a set name, taggedBIO-CFG-001.
from_records
(set, gene) table.
Each row assigns one gene to one set, so a set spans multiple rows (the layout of MSigDB-style CSVs and the decoupler “net” format). Rows are grouped by set_col and, within a set, gene order follows first appearance.
Returns:
A collection with one GeneSet per distinct set_col
value.
Raises:
BioptimusValueError- If a required column is missing, taggedBIO-CFG-001.
__getitem__
__iter__
__len__
sets
GeneSet objects.
names
coverage
GeneSet.coverage per set.
Returns:
A DataFrame indexed by set name with hit, total, and
missing (a list of the unresolved genes) columns.
resolve_columns
bioptimus.data.cohort.Cohort.select_genes) uses this to read only the needed columns instead of the full panel, then re-selects per set with select on the reduced matrix.
Returns:
The sorted, unique panel column indices covered by any member set.
Raises:
BioptimusValueError- If any member set resolves to no genes, taggedBIO-GENE-001.
select
(selected, names) tuple, where
selected is an (N, k) array and names are the resolved
panel identifiers.
Raises:
BioptimusValueError- If values does not match the panel, taggedBIO-CFG-001, or if any member set resolves to no genes, taggedBIO-GENE-001.

