gain.gene_sets package

Subpackages

Submodules

gain.gene_sets.gene_set module

Classes for handling of gene sets and gene set collections.

class BaseGeneSetCollection(collection_id: str)[source]

Bases: ABC

Base class for gene set collections.

abstractmethod get_all_gene_sets() → list[GeneSet][source]

Return list of all gene sets in the collection.

abstractmethod get_gene_set(gene_set_id: str) → GeneSet | None[source]

Return the gene set if found; returns None if not found.

abstractmethod load() → BaseGeneSetCollection[source]

Load the gene sets from the resource.

class BaseResourceSchema(*, type: str | None = None, meta: MetaSchema | None = None)[source]

Bases: BaseModel

meta: MetaSchema | None
model_config = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

type: str | None
class CategoricalHistogramSchema(*, type: Literal['categorical'], displayed_values_count: int | None = None, displayed_values_percent: float | None = None, value_order: list[str | int] | None = None, y_log_scale: bool | None = None, label_rotation: int | None = None, plot_function: str | None = None, enforce_type: bool | None = None, natural_order: bool | None = None)[source]

Bases: BaseModel

displayed_values_count: int | None
displayed_values_percent: float | None
enforce_type: bool | None
label_rotation: int | None
model_config = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

natural_order: bool | None
plot_function: str | None
type: Literal['categorical']
value_order: list[str | int] | None
y_log_scale: bool | None
class GeneSet(name: str, desc: str, syms: list[str])[source]

Bases: object

A named set of genes.

Variables:
  • name (str) – The name of the gene set, unique in its collection.

  • desc (str) – The description of the gene set. It can be empty.

  • count (int) – The number of gene symbols in syms.

  • syms (list[str]) – The gene symbols of the set.

count: int
desc: str
name: str
syms: list[str]
class GeneSetCollection(resource: GenomicResource)[source]

Bases: BaseGeneSetCollection

The gene sets of a gene_set_collection resource.

Build a collection with build_gene_set_collection_from_resource_id or build_gene_set_collection_from_resource. The built collection may not be loaded: call load() before the first read.

Variables:
  • resource (GenomicResource) – The resource that holds the gene sets.

  • config (GeneSetResourceSchema) – The validated config of the resource.

  • collection_id (str) – The id: key of the resource config.

  • web_label (str) – The label that a web interface shows for the collection. Empty when the config sets no web_label.

  • web_format_str (str) – The format string that a web interface uses to show a gene set. Empty when the config sets no web_format_str.

  • gene_sets (dict[str, GeneSet]) – The loaded gene sets by name. The dictionary is empty until load() runs.

property files: set[str]

Return the names of the resource files that hold the gene sets.

gene_sets: dict[str, GeneSet]
get_all_gene_sets() → list[GeneSet][source]

Return all the gene sets of the collection.

Returns:

The gene sets in the order of the resource. The list is empty when the collection is not loaded.

Return type:

list[GeneSet]

get_gene_collection_count_statistics() → dict | None[source]

Read the stored counts of the collection.

Returns:

A dictionary with the keys number_of_gene_sets and number_of_unique_genes. None when the resource has no stored counts because no statistics build ran.

Return type:

dict | None

get_gene_set(gene_set_id: str) → GeneSet | None[source]

Return one gene set by its name.

Parameters:

gene_set_id (str) – The name of the gene set.

Returns:

The gene set, or None when the collection has no set with this name. The collection logs a warning in that case. An unloaded collection has no sets, so it always returns None.

Return type:

GeneSet | None

get_gene_sets_list_statistics() → list[dict] | None[source]

Read the stored list of gene sets with their sizes.

Returns:

One dictionary for each gene set, with the keys name, count and desc, ordered by falling count. None when the resource has no stored list because no statistics build ran.

Return type:

list[dict] | None

get_gene_sets_per_gene_hist() → NullHistogram | CategoricalHistogram | NumberHistogram | None[source]

Read the stored histogram of the gene sets that contain a gene.

Returns:

The histogram, or None when the resource has no stored histogram because no statistics build ran.

Return type:

Histogram | None

get_genes_per_gene_set_hist() → NullHistogram | CategoricalHistogram | NumberHistogram | None[source]

Read the stored histogram of the number of genes in a gene set.

Returns:

The histogram, or None when the resource has no stored histogram because no statistics build ran.

Return type:

Histogram | None

is_loaded() → bool[source]

Return whether the collection holds any loaded gene set.

Returns:

True after load() read at least one gene set. A resource with no gene sets always reports False.

Return type:

bool

load() → GeneSetCollection[source]

Read the gene sets from the resource.

A call on a loaded collection does nothing.

Returns:

This collection, so the call chains.

Return type:

GeneSetCollection

Raises:

ValueError – If the format: key of the resource is not directory, gmt or map.

class GeneSetResourceSchema(*, id: str, filename: str | None = None, directory: str | None = None, format: str | None, web_label: str | None = None, web_format_str: str | None = None, histograms: dict[Literal['genes_per_gene_set', 'gene_sets_per_gene'], Annotated[NumericHistogramSchema | CategoricalHistogramSchema, FieldInfo(annotation=NoneType, required=True, discriminator='type')]] | None = None)[source]

Bases: BaseModel

directory: str | None
filename: str | None
histograms: dict[Literal['genes_per_gene_set', 'gene_sets_per_gene'], HistogramConfig] | None
model_config = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

resource_format: str | None
resource_id: str
web_format_str: str | None
web_label: str | None
class MetaSchema(*, description: str | None = None, labels: dict[str, Any] | None = None)[source]

Bases: BaseModel

description: str | None
labels: dict[str, Any] | None
model_config = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class NumericHistogramSchema(*, type: Literal['number'], plot_function: str | None = None, number_of_bins: int | None = None, view_range: ViewRangeSchema | None = None, x_log_scale: bool | None = None, y_log_scale: bool | None = None, x_min_log: float | None = None, value_order: list[str | int] | None = None, displayed_values_count: int | None = None)[source]

Bases: BaseModel

displayed_values_count: int | None
model_config = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

number_of_bins: int | None
plot_function: str | None
type: Literal['number']
value_order: list[str | int] | None
view_range: ViewRangeSchema | None
x_log_scale: bool | None
x_min_log: float | None
y_log_scale: bool | None
class ViewRangeSchema(*, min: float | None = None, max: float | None = None)[source]

Bases: BaseModel

max: float | None
min: float | None
model_config = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

build_gene_set_collection_from_file(filename: str, collection_id: str | None = None, collection_format: str | None = None, web_label: str | None = None, web_format_str: str | None = None) → GeneSetCollection[source]

Return a Gene Set Collection by adapting a file to a local resource.

build_gene_set_collection_from_resource(resource: GenomicResource) → GeneSetCollection[source]

Build the gene set collection of a genomic resource.

The returned collection may not be loaded. Calls with the same resource return the same object, so an earlier caller may have loaded it.

Parameters:

resource (GenomicResource) – A resource of type gene_set_collection.

Returns:

The collection of the resource. Check is_loaded() or call load() before any read.

Return type:

GeneSetCollection

Raises:

ValueError – If the resource is not of type gene_set_collection.

build_gene_set_collection_from_resource_id(resource_id: str, grr: GenomicResourceRepo | None = None) → GeneSetCollection[source]

Build the gene set collection of the resource with a given id.

The returned collection may not be loaded. Calls that find the same resource return the same object, so an earlier caller may have loaded it.

Parameters:
  • resource_id (str) – The id of a resource of type gene_set_collection in the repository.

  • grr (GenomicResourceRepo | None) – The repository that resolves resource_id. When it is None, the function builds the default repository with build_genomic_resource_repository().

Returns:

The collection of the resource. Check is_loaded() or call load() before any read.

Return type:

GeneSetCollection

Raises:
  • FileNotFoundError – If a protocol-backed repository has no resource with this id.

  • ValueError – If a group repository has no resource with this id, or if the resource is not of type gene_set_collection.

gain.gene_sets.gene_term module

class GeneInfo(gene_id: str, gene_sym: str, synonyms: set[str], description: str)[source]

Bases: object

description: str
gene_id: str
gene_sym: str
synonyms: set[str]
class GeneTerms[source]

Bases: object

Class representing gene terms.

filter_genes(filter_fun: Callable[[list[str]], list[str]]) → None[source]

Filter the genes.

g2t: dict[str, Any]
gene_ns: str | None
rename_genes(gene_ns: str | None, rename_fn: Callable[[str], str | None]) → None[source]

Rename genese.

save(fname: str) → None[source]

Save to fname.

t2g: dict[str, Any]
t_desc: dict[str, Any]
class NCBIGeneInfo(genes: dict[str, gain.gene_sets.gene_term.GeneInfo], ns_tokens: dict[str, dict[str, list[gain.gene_sets.gene_term.GeneInfo]]])[source]

Bases: object

genes: dict[str, GeneInfo]
ns_tokens: dict[str, dict[str, list[GeneInfo]]]
dd() → dict[str, int][source]
get_clean_gene_id(ncbi_gene_info: NCBIGeneInfo, ns: str, term: str) → str | None[source]

Gene gene ID from NCBI gene info data.

load_gene_terms(path: str) → GeneTerms | None[source]

Load gene terms from a file.

load_ncbi_gene_info(gene_info_file: str) → NCBIGeneInfo[source]
read_ewa_set_file(set_files: list[IO]) → GeneTerms[source]

Read a set of ewa files.

read_gmt_file(input_file: IO) → GeneTerms[source]

Read a gmt file.

read_mapping_file(input_file: IO, names_file: IO | None) → GeneTerms[source]

Read a mapping file.

rename_gene_terms(gene_terms: GeneTerms, gene_ns: str, ncbi_gene_info: NCBIGeneInfo) → GeneTerms[source]

Rename gene terms using NCBI gene info data.

Module contents