Skip to main content
Gene sets over M-Optimus prediction panels. Provides 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 a GeneSet.

GeneSet

A named, immutable group of gene identifiers. Duplicate genes are removed on construction while preserving first-seen order, so the resolved column order is deterministic. A set must contain at least one gene.
Human-readable set name, for example "hypoxia".
The gene identifiers in the set. Duplicates are removed.
The identifier space of genes — "symbol" (default) or "ensembl".
Optional free-text description of the set.
Attributes:
  • name - The set name.
  • genes - The de-duplicated gene identifiers in first-seen order.
  • id_type - The identifier space of genes.
  • description - Optional description.

__len__

Returns the number of unique genes in the set.

from_gene_list

Builds a set from a flat single-column gene-list file. Reads a CSV/TSV holding one gene identifier per row under a single column (the layout of predefined panel/gene-list files). The column is matched tolerantly against common spellings (for example "Ensembl ID") when the exact name is absent.
Path to the CSV/TSV file.
The name to give the resulting set.
Column holding the gene identifiers.
The identifier space of the genes. Defaults to "ensembl" since flat gene lists are typically Ensembl ids.
Optional free-text description of the set.
Returns: A 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, tagged BIO-CFG-001.

to_spec

Returns a JSON/YAML-safe dict describing this gene set. Stores the full (de-duplicated) gene list plus id space and description, so 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

Rebuilds a GeneSet from a to_spec dict.
A mapping with name and genes and optional id_type / description (as produced by to_spec).
Returns: The reconstructed gene set. Raises:
  • BioptimusValueError - If name or genes is missing, or id_type is not a supported identifier space, tagged BIO-CFG-001.

coverage

Reports how many of the set’s genes are present in a panel. This is a pure query with no side effects (it does not warn or raise), so callers can inspect coverage before deciding how to proceed.
The prediction panel gene identifiers, in panel order.
Optional symbol-to-panel-id mapping applied to each gene before lookup.
Returns: A (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

Resolves the set’s genes to column indices into a prediction panel. Genes absent from the panel are dropped with a structured warning. A set that resolves to no genes at all raises, since the caller almost certainly has an id-space mismatch (for example a symbol set against an Ensembl panel without a mapping).
The prediction panel gene identifiers, in panel order.
Optional symbol-to-panel-id mapping applied to each gene before lookup.
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, tagged BIO-GENE-001.

resolve_with_mask

Resolves genes to panel indices and reports which genes survived. Like 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.
The prediction panel gene identifiers, in panel order.
Optional symbol-to-panel-id mapping applied to each gene before lookup.
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, tagged BIO-GENE-001.

select_genes

Subsets a prediction matrix to a gene set’s resolved genes.
An (N, G) array of predictions, where G matches the length of gene_names.
The prediction panel gene identifiers, in panel order.
The gene set to select.
Optional symbol-to-panel-id mapping forwarded to GeneSet.resolve.
Returns: A (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, tagged BIO-CFG-001, or if none of the set’s genes resolve, tagged BIO-GENE-001.

GeneSetCollection

A named group of 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.
A mapping of set name to either a GeneSet or a plain iterable of gene identifiers. Plain iterables are wrapped in a GeneSet using the mapping key as the name.
The identifier space used for members supplied as plain iterables. Ignored for members that are already GeneSet objects.
Raises:
  • BioptimusValueError - If members is empty, tagged BIO-CFG-001.

from_sets

Groups existing gene sets into a collection keyed by their names.
The gene sets to group. Names must be unique.
Returns: A collection containing the given sets. Raises:
  • BioptimusValueError - If two sets share the same name, tagged BIO-CFG-001.

to_spec

Returns a JSON/YAML-safe dict describing this collection. Each member is stored with its mapping key alongside the member spec, so a collection whose key differs from the member’s name (or that holds same-named members under distinct keys) rebuilds faithfully. Returns: A dict with a sets list of {"key", "spec"} entries (see GeneSet.to_spec), in member order.

from_spec

Rebuilds a GeneSetCollection from a to_spec dict.
A mapping with a sets list of {"key", "spec"} entries.
Returns: The reconstructed collection, preserving member keys and order. Raises:
  • BioptimusValueError - If sets is missing or an entry lacks key or spec, tagged BIO-CFG-001.

from_gmt

Builds a collection from a single GMT file (for example MSigDB).
Path to the .gmt file. Each line is <set name>\t<description>\t<gene>\t<gene>....
The identifier space of the genes in the file.
Returns: A collection with one GeneSet per GMT line. Raises:
  • BioptimusValueError - If two GMT lines share a set name, tagged BIO-CFG-001.

from_records

Builds a collection from a long-form (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.
A path to a CSV/TSV file (separator inferred from the extension) or an in-memory pandas.DataFrame.
Column holding the set name.
Column holding the gene identifier.
The identifier space of the genes.
Returns: A collection with one GeneSet per distinct set_col value. Raises:
  • BioptimusValueError - If a required column is missing, tagged BIO-CFG-001.

__getitem__

Returns the member set with the given name.

__iter__

Iterates over member set names.

__len__

Returns the number of member sets.

sets

Returns the member sets as a list of GeneSet objects.

names

Returns the member set names in insertion order.

coverage

Reports panel coverage for every member set in one table. The panel lookup is built once and shared across all member sets, so this is cheaper than calling GeneSet.coverage per set.
The prediction panel gene identifiers, in panel order.
Optional symbol-to-panel-id mapping applied to each gene.
Returns: A DataFrame indexed by set name with hit, total, and missing (a list of the unresolved genes) columns.

resolve_columns

Resolves the union of every member set’s genes to panel columns. Returns the sorted, de-duplicated column indices spanning all member sets. A caller that reads predictions lazily (for example 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.
The prediction panel gene identifiers, in panel order.
Optional symbol-to-panel-id mapping applied to each gene.
Returns: The sorted, unique panel column indices covered by any member set. Raises:
  • BioptimusValueError - If any member set resolves to no genes, tagged BIO-GENE-001.

select

Subsets a prediction matrix to each member set’s resolved genes. The panel lookup is built once and shared across all member sets.
An (N, G) array of predictions, where G matches the length of gene_names.
The prediction panel gene identifiers, in panel order.
Optional symbol-to-panel-id mapping applied to each gene.
Returns: A mapping of set name to a (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, tagged BIO-CFG-001, or if any member set resolves to no genes, tagged BIO-GENE-001.