Parsers
Database-specific parsers that produce SSSOM-compliant MappingSets.
All parsers inherit from BaseParser
and return BaseMappingSet objects.
Anybody can add a new database by adding the necessary config yaml,
parser class, and a DataSourceConfig entry in constants.py.
If possible, you can also add automated download in src/pysec2pri/download.py.
Database |
Input |
Methods |
|---|---|---|
ChEBI |
TSV directory (≥ release 245) or SDF file (< 245) |
|
Ensembl |
|
|
HMDB |
|
|
HGNC |
|
|
NCBI Gene |
|
|
UniProt |
|
|
VGNC |
|
|
Wikidata |
SPARQL endpoint (live) or pre-fetched JSON |
|
Module Reference
- class ChEBIParser(version: str | None = None, show_progress: bool = True, subset: str | None = None)[source]
Parser for ChEBI data files.
Supports both TSV flat files (>= release 245) and (legacy) SDF files. Extracts secondary-to-primary ChEBI identifier mappings and name-to-synonym relationships.
Returns an IdMappingSet for ID mappings (cardinality computed on IDs) and can optionally include synonym mappings via LabelMappingSet.
Initialize the ChEBI parser.
- Parameters:
version – Version/release identifier for the datasource.
show_progress – Whether to show progress bars during parsing.
subset – Which compound subset to use, e.g.
"3star"or"complete"(seechebi.yaml’ssubsetblock). For TSV format, filters by stars in compounds.tsv; for SDF format, determines which file to download. Defaults to the config’ssubset.default.
- parse(input_path: Path | str | None = None, *, secondary_ids_path: Path | str | None = None, compounds_path: Path | str | None = None) BaseMappingSet[source]
Parse ChEBI data into an IdMappingSet.
Accepts three calling conventions:
input_pathis a directory: expectssecondary_ids.tsv(and optionallycompounds.tsv) inside it (TSV format >= 245).input_pathis an SDF file: legacy format (< 245).Keyword args
secondary_ids_path/compounds_path: explicit TSV paths.
- Parameters:
input_path – Path to an SDF file, or a directory of TSV files.
secondary_ids_path – Explicit path to secondary_ids.tsv (TSV format).
compounds_path – Explicit path to compounds.tsv for 3-star filtering.
- Returns:
IdMappingSet with computed cardinalities.
- parse_synonyms(input_path: Path | str | None = None, *, names_path: Path | str | None = None, compounds_path: Path | str | None = None) BaseMappingSet[source]
Parse ChEBI data into a LabelMappingSet for synonyms.
Accepts three calling conventions:
input_pathis a directory: expectsnames.tsv(and optionallycompounds.tsv) inside it (TSV format >= 245).input_pathis an SDF file: legacy format (< 245).Keyword args
names_path/compounds_path: explicit TSV paths (kept for backwards compatibility).
- Parameters:
input_path – Path to an SDF file, or a directory of TSV files.
names_path – Explicit path to names.tsv (TSV format).
compounds_path – Explicit path to compounds.tsv for 3-star filtering.
- Returns:
LabelMappingSet with computed cardinalities based on labels.
- parse_primary_ids(input_path: Path | str | None = None, *, compounds_path: Path | str | None = None) BaseMappingSet[source]
Return a mapping set containing the full list of current ChEBI primary IDs.
Reads
compounds.tsv(TSV releases >= 245) to extract every current ChEBI compound ID. The returned mapping set has an emptymappingslist; its_primary_idsstore is populated with every current ChEBI ID (CHEBI: prefixed) so thatto_pri_ids()produces the complete list.- Parameters:
input_path – Path to a directory containing
compounds.tsv, or directly tocompounds.tsvitself.compounds_path – Explicit path to
compounds.tsv(overridesinput_path).
- Returns:
IdMappingSetwith no mappings and_primary_idspopulated with all current ChEBI IDs.
- parse_primary_labels(input_path: Path | str | None = None, *, compounds_path: Path | str | None = None) BaseMappingSet[source]
Return a mapping set containing the full list of current ChEBI compound names.
Reads
compounds.tsvto extract every current compound’s canonical name. The returned mapping set has an emptymappingslist; its_primary_labelsstore is populated.- Parameters:
input_path – Path to a directory containing
compounds.tsv, or directly tocompounds.tsvitself.compounds_path – Explicit path to
compounds.tsv.
- Returns:
LabelMappingSetwith no mappings and_primary_labelspopulated.
- class EnsemblParser(version: str | None = None, show_progress: bool = True, species: str | int = 9606)[source]
Parser for Ensembl core flat-file dumps using Polars.
Each release’s
stable_id_eventtable is cumulative, so a single parse describes the whole state of Ensembl gene IDs at that release (no chain-walking across releases).Returns: - IdMappingSet for ID-to-ID mappings (retired Ensembl gene IDs) - LabelMappingSet for symbol mappings (external gene synonyms)
Initialize the Ensembl parser.
- Parameters:
version – Ensembl release number (e.g.
"115").show_progress – Whether to show progress bars during parsing.
species – Canonical NCBI taxon ID of the species being parsed. Unlike NCBI, this never filters rows (the input files are already species-specific downloads) – it only labels the output (see
_product_slug()).
- parse(stable_id_event_path: Path | str | None = None, mapping_session_path: Path | str | None = None, gene_path: Path | str | None = None) BaseMappingSet[source]
Parse
stable_id_event(+mapping_session) into an IdMappingSet.- Parameters:
stable_id_event_path – Path to
stable_id_event.txt(can be.gzcompressed).mapping_session_path – Optional path to
mapping_session.txt, used to resolve each row’smapping_date. When omitted, rows fall back to the set-level release date.gene_path – Optional path to
gene.txt. When supplied,_primary_idsis populated with every current gene ID.
- Returns:
IdMappingSet with computed cardinalities based on IDs.
- parse_labels(gene_path: Path | str | None = None, xref_path: Path | str | None = None, external_synonym_path: Path | str | None = None) BaseMappingSet[source]
Parse external gene synonyms into a LabelMappingSet.
A release’s files state a gene’s current symbol and its synonyms, but not the symbols it previously had, so a single release yields alias mappings only. Renames are recovered by consolidating across releases.
- Parameters:
gene_path – Path to
gene.txt.xref_path – Path to
xref.txt.external_synonym_path – Optional path to
external_synonym.txt. When omitted, the returned set carries only the full primary label set (no synonym mappings).
- Returns:
LabelMappingSet with computed cardinalities based on labels.
- parse_primary_ids(gene_path: Path | str | None = None) BaseMappingSet[source]
Return a mapping set containing the full list of current Ensembl gene IDs.
- Parameters:
gene_path – Path to
gene.txt(can be.gzcompressed).- Returns:
IdMappingSetwith_primary_idspopulated.
- parse_primary_labels(gene_path: Path | str | None = None, xref_path: Path | str | None = None) BaseMappingSet[source]
Return a mapping set containing the full list of current Ensembl gene labels.
- Parameters:
gene_path – Path to
gene.txt.xref_path – Path to
xref.txt.
- Returns:
LabelMappingSetwith_primary_labelspopulated.
- parse_all(stable_id_event_path: Path | str | None, mapping_session_path: Path | str | None, gene_path: Path | str | None, xref_path: Path | str | None, external_synonym_path: Path | str | None) tuple[BaseMappingSet, BaseMappingSet][source]
Parse the full set of Ensembl core flat files.
- Parameters:
stable_id_event_path – Path to
stable_id_event.txt.mapping_session_path – Path to
mapping_session.txt.gene_path – Path to
gene.txt.xref_path – Path to
xref.txt.external_synonym_path – Path to
external_synonym.txt.
- Returns:
Tuple of (IdMappingSet, LabelMappingSet).
- current_label_snapshot(gene_path: Path | str, xref_path: Path | str) dict[str, str][source]
Return
{bare stable_id -> current display label}for every current gene.Unlike
_extract_primary_labels()(keyed by label, for ambiguity detection andto_pri_labels()), this is keyed by gene – the natural shape for diffing one release’s label snapshot against the next (seepysec2pri.consolidate.build_label_history()).- Parameters:
gene_path – Path to
gene.txt.xref_path – Path to
xref.txt.
- Returns:
dict[stable_id, label](bare stable IDs, noENSEMBL:prefix).
- parse_label_history(transitions: Iterable[tuple[str, str, str, str | None]]) BaseMappingSet[source]
Build a LabelMappingSet from precomputed previous->current label transitions.
- Parameters:
transitions – Iterable of
(stable_id, prev_label, curr_label, mapping_date)tuples, one per gene whose display label changed between two releases (seepysec2pri.consolidate.build_label_history()).- Returns:
LabelMappingSet with
IAO:0100001(“term replaced by”) mappings.
- class HMDBParser(version: str | None = None, show_progress: bool = True, config_name: str | None = None)[source]
Shared XML-parser for HMDB metabolite and protein files.
Use
HMDBMetaboliteParserorHMDBProteinParser.Initialize the parser.
- Parameters:
version – Version/release identifier for the datasource.
show_progress – Whether to show progress bars during parsing.
config_name – Name of config file to load (defaults to class name).
- parse_primary_ids(metabolites_path: Path | str | None = None, proteins_path: Path | str | None = None) BaseMappingSet[source]
Return a mapping set containing the full list of current HMDB primary IDs.
Reads one or both of
hmdb_metabolites.xmlandhmdb_proteins.xmland collects all primary accession numbers. The returned mapping set has an emptymappingslist;_primary_idsis populated with every currentHMDB:<acc>CURIE.- Parameters:
metabolites_path – Path to
hmdb_metabolites.xml(or zip/gz).proteins_path – Path to
hmdb_proteins.xml(or zip/gz).
- Returns:
IdMappingSetwith_primary_idspopulated. At least one of the two path arguments must be supplied.
- class HGNCParser(version: str | None = None, show_progress: bool = True)[source]
Parser for HGNC TSV files using Polars for memory efficiency.
Extracts secondary-to-primary HGNC identifier mappings and symbol mappings from HGNC withdrawn and complete set files.
Returns: - IdMappingSet for ID-to-ID mappings (withdrawn/merged IDs) - LabelMappingSet for symbol mappings (alias/previous symbols)
Initialize the HGNC parser.
- Parameters:
version – Version/release identifier for the datasource.
show_progress – Whether to show progress bars during parsing.
- merged_patterns: ClassVar[list[str]] = ['merged_into_report(i.e. hgnc_id/symbol/status)', 'merged_into_report(i.e hgnc_id/symbol/status)', 'merged_into_report(s) (i.e hgnc_id|symbol|status)']
Naming variants of the merged-info column across file versions.
- parse(input_path: Path | str | None, complete_set_path: Path | str | None = None) BaseMappingSet[source]
Parse HGNC withdrawn TSV file into an IdMappingSet.
- Parameters:
input_path – Path to the withdrawn HGNC TSV file.
complete_set_path – Optional path to the HGNC complete set TSV. When supplied,
all_primary_ids|labelson the returned mapping set is populated with every current HGNC ID, not just those that appear asobject_idin a withdrawn to primary mapping.
- Returns:
IdMappingSet with computed cardinalities based on IDs.
- parse_primary_labels(complete_set_path: Path | str | None) BaseMappingSet[source]
Return a mapping set whose only content is the full primary Symbol list.
Reads the HGNC complete set to extract every current HGNC Symbol and stores it in
_primary_labels. Themappingslist is intentionally left empty, this mapping set exists only to driveto_pri_labels().- Parameters:
complete_set_path – Path to the HGNC complete set TSV file.
- Returns:
LabelMappingSetwith no mappings and_primary_labelspopulated with all current HGNC labels.
- parse_primary_ids(complete_set_path: Path | str | None) BaseMappingSet[source]
Return a mapping set whose only content is the full primary ID list.
Reads the HGNC complete set to extract every current HGNC ID and stores it in
_primary_ids. Themappingslist is intentionally left empty, this mapping set exists only to driveto_pri_ids().- Parameters:
complete_set_path – Path to the HGNC complete set TSV file.
- Returns:
IdMappingSetwith no mappings and_primary_idspopulated with all current HGNC IDs.
- parse_labels(complete_set_path: Path | str | None, statuses: list[str] | None = None) LabelMappingSet[source]
Parse HGNC complete set for symbol (label) mappings.
- Parameters:
complete_set_path – Path to the complete HGNC set TSV file.
statuses – Entry statuses to include (e.g.
["Approved"]). IfNone(default), all entries are included.
- Returns:
LabelMappingSet with computed cardinalities based on labels.
- parse_all(withdrawn_path: Path | str | None, complete_set_path: Path | str | None) tuple[BaseMappingSet, BaseMappingSet][source]
Parse both withdrawn and complete set files.
- Parameters:
withdrawn_path – Path to the withdrawn HGNC TSV file.
complete_set_path – Path to the complete HGNC set TSV file.
- Returns:
Tuple of (IdMappingSet, LabelMappingSet).
- class NCBIParser(version: str | None = None, show_progress: bool = True, config_name: str | None = None)[source]
Parser for NCBI Gene TSV files using Polars.
Extracts secondary-to-primary NCBI Gene identifier mappings including gene symbols from gene_history and gene_info files.
Returns: - IdMappingSet for ID-to-ID mappings (discontinued Gene IDs) - LabelMappingSet for symbol mappings (gene synonyms)
Initialize the parser.
- Parameters:
version – Version/release identifier for the datasource.
show_progress – Whether to show progress bars during parsing.
config_name – Name of config file to load (defaults to class name).
- parse(input_path: Path | str | None = None, species: str = '9606', gene_info_path: Path | str | None = None) BaseMappingSet[source]
Parse NCBI gene_history file into an IdMappingSet.
- Parameters:
input_path – Path to gene_history file (can be .gz compressed).
species – NCBI taxon ID to filter by, or “all” to skip filtering entirely (default: “9606” for human).
gene_info_path – Optional path to the gene_info file. When supplied,
_primary_idson the returned mapping set is populated with every currentNCBIGene:<id>CURIE for the given taxonomy, not just those that appear asobject_idin a discontinued-to-primary mapping.
- Returns:
IdMappingSet with computed cardinalities based on IDs.
- parse_labels(gene_info_path: Path | str | None, species: str = '9606') BaseMappingSet[source]
Parse NCBI gene_info file for label (label) mappings.
- Parameters:
gene_info_path – Path to gene_info file.
species – NCBI taxon ID to filter by, or “all” to skip filtering entirely (default: “9606” for human).
- Returns:
LabelMappingSet with computed cardinalities based on labels.
- parse_primary_ids(gene_info_path: Path | str | None, species: str = '9606') BaseMappingSet[source]
Return a mapping set containing the full list of current NCBI Gene primary IDs.
Reads
gene_infoto extract every current Gene ID for the given taxonomy. The returned mapping set has an emptymappingslist;_primary_idsis populated with every currentNCBIGene:<id>CURIE.- Parameters:
gene_info_path – Path to the gene_info file (can be .gz compressed).
species – NCBI taxon ID to filter by, or
"all"to skip filtering entirely (default:"9606"for human).
- Returns:
IdMappingSetwith_primary_idspopulated.
- parse_primary_labels(gene_info_path: Path | str | None, species: str = '9606') BaseMappingSet[source]
Return a mapping set containing the full list of current NCBI Gene labels.
Reads
gene_infoto extract every current gene label for the given taxonomy. The returned mapping set has an emptymappingslist;_primary_labelsis populated.- Parameters:
gene_info_path – Path to the gene_info file (can be .gz compressed).
species – NCBI taxon ID to filter by, or
"all"to skip filtering entirely (default:"9606"for human).
- Returns:
LabelMappingSetwith_primary_labelspopulated.
- parse_all(gene_history_path: Path | str | None, gene_info_path: Path | str | None, species: str = '9606') tuple[BaseMappingSet, BaseMappingSet][source]
Parse both gene_history and gene_info files.
- Parameters:
gene_history_path – Path to gene_history file.
gene_info_path – Path to gene_info file.
species – NCBI taxon ID to filter by, or
"all"to process every organism in the file (seeALL_SPECIES).
- Returns:
Tuple of (IdMappingSet, LabelMappingSet).
- class UniProtParser(version: str | None = None, show_progress: bool = True, config_name: str | None = None)[source]
Parser for UniProt files using Polars.
Extracts secondary-to-primary UniProt accession mappings from sec_ac.txt (secondary accessions) and delac_sp.txt (deleted accessions).
Returns IdMappingSet for all mappings (UniProt only has ID mappings).
Initialize the parser.
- Parameters:
version – Version/release identifier for the datasource.
show_progress – Whether to show progress bars during parsing.
config_name – Name of config file to load (defaults to class name).
- parse(input_path: Path | str | None = None, delac_path: Path | str | None = None) BaseMappingSet[source]
Parse UniProt mapping files into an IdMappingSet.
- Parameters:
input_path – Path to sec_ac.txt (secondary accessions file).
delac_path – Path to delac_sp.txt (deleted accessions file).
- Returns:
IdMappingSet with computed cardinalities based on IDs.
- parse_primary_ids(acindex_path: Path | str | None = None) BaseMappingSet[source]
Return a mapping set containing the full list of current UniProt primary ACs.
Parses
acindex.txt(or a gzip-compressed variant) to extract every accession number that currently appears in UniProtKB/Swiss-Prot. The file lists one AC per row (after the__________separator line); only the first whitespace-delimited token of each data line is taken.For versioned (legacy) releases the file can be found at:
https://ftp.uniprot.org/pub/databases/uniprot/previous_releases/ release-{version}/knowledgebase/docs/acindex.txt.gz
- Parameters:
acindex_path – Local path to
acindex.txt(plain or.gz). Auto-downloaded from the current release whenNone.- Returns:
IdMappingSetwith no mappings and_primary_idspopulated with all currentUniProtKB:<AC>CURIEs.
- class VGNCParser(version: str | None = None, show_progress: bool = True)[source]
Parser for VGNC TSV files using Polars for memory efficiency.
Extracts secondary-to-primary VGNC identifier mappings and symbol mappings from the VGNC withdrawn and gene-set files.
Produces two mapping-set kinds:
IdMappingSet for ID-to-ID mappings (withdrawn/merged IDs).
LabelMappingSet for symbol mappings (alias/previous symbols), scoped to one species.
Initialize the VGNC parser.
- Parameters:
version – Version/release identifier for the datasource.
show_progress – Whether to show progress bars during parsing.
- withdrawn_label_column: ClassVar[str] = 'withdrawn_symbol'
Column holding a withdrawn entry symbol.
- parse(input_path: Path | str | None, complete_set_path: Path | str | None = None, species: str | None = None) BaseMappingSet[source]
Parse the VGNC withdrawn TSV file into an IdMappingSet.
Always parses the full, unfiltered withdrawn file first (see module docstring); when species is given (and isn’t
ALL_SPECIES), the result is then subset by resolving each mapping’s primary VGNC ID against the gene-set file’staxon_idcolumn – this requires complete_set_path.- Parameters:
input_path – Path to the VGNC withdrawn TSV file.
complete_set_path – Path to the VGNC gene-set file. Required when species is given (used to resolve taxon IDs). When supplied,
_primary_idson the returned mapping set is populated with every current VGNC ID for species (or across all species, when species isNone/"all").species – NCBI taxon ID to subset the output to, or
ALL_SPECIES.None(default) returns the full, unfiltered set across every species.
- Returns:
IdMappingSet with computed cardinalities based on IDs.
- Raises:
ValueError – If species is given without complete_set_path (there is no other way to resolve taxon IDs).
- parse_primary_ids(complete_set_path: Path | str | None, species: str | None = None) BaseMappingSet[source]
Return a mapping set whose only content is the full primary ID list.
Reads the VGNC gene-set file to extract every current VGNC ID, optionally subset to species.
- Parameters:
complete_set_path – Path to the VGNC gene-set TSV file.
species – NCBI taxon ID to subset the result to, or
ALL_SPECIES.None(default) returns the full, unfiltered set across every species.
- Returns:
IdMappingSetwith no mappings and_primary_idspopulated.
- parse_labels(complete_set_path: Path | str | None, species: str, statuses: list[str] | None = None) LabelMappingSet[source]
Parse the VGNC gene-set file for symbol (label) mappings, scoped to one species.
- Parameters:
complete_set_path – Path to the VGNC gene-set TSV file.
species – NCBI taxon ID to filter by, or
ALL_SPECIESto process every species together (see module docstring for why that changes ambiguity detection). Required at this layer – callers needing config’sspecies.defaultfallback (seeconfig/vgnc.yaml) should resolve it themselves, aspysec2pri.api’sgenerate_vgnc_labelsdoes.statuses – Entry statuses to include (e.g.
["Approved"]). IfNone(default), all entries are included.
- Returns:
LabelMappingSet with computed cardinalities based on labels.
- parse_primary_labels(complete_set_path: Path | str | None, species: str) BaseMappingSet[source]
Return a mapping set whose only content is the full primary Symbol list.
Reads the VGNC gene-set file to extract every current approved symbol for species, storing it in
_primary_labels. Themappingslist is intentionally left empty; this mapping set exists only to driveto_pri_labels().- Parameters:
complete_set_path – Path to the VGNC gene-set TSV file.
species – NCBI taxon ID to filter by, or
ALL_SPECIES.
- Returns:
LabelMappingSetwith no mappings and_primary_labelspopulated.
- parse_all(withdrawn_path: Path | str | None, complete_set_path: Path | str | None, species: str) tuple[BaseMappingSet, BaseMappingSet][source]
Parse both the withdrawn and gene-set files.
- Parameters:
withdrawn_path – Path to the VGNC withdrawn TSV file.
complete_set_path – Path to the VGNC gene-set TSV file.
species – NCBI taxon ID to filter the label mappings by.
- Returns:
Tuple of (IdMappingSet, LabelMappingSet).
- class WikidataParser(version: str | None = None, show_progress: bool = True, entity_type: str | None = None, endpoint: str | None = None, test_subset: bool = False)[source]
Parser for Wikidata redirect mappings via SPARQL.
Queries the QLever Wikidata endpoint to find redirect mappings for chemicals, genes, and proteins.
Returns IdMappingSet for all mappings.
Initialize the Wikidata parser.
- Parameters:
version – Version/date string for the mappings.
show_progress – Whether to show progress.
entity_type – Entity type to query; one of
entity_types(). Defaults to the first one the config declares.endpoint – Optional custom SPARQL endpoint.
test_subset – Whether to use test queries (LIMIT 10).
- classmethod entity_types() list[str][source]
Return the entity types declared by
wikidata.yaml’squeriesblock.Each key names both a
--entity-typechoice and its redirect query, so the config is the only place they are listed.
- parse(input_path: Path | str | None = None) BaseMappingSet[source]
Query Wikidata and return a MappingSet.
- Parameters:
input_path – Ignored for Wikidata (queries endpoint directly).
- Returns:
IdMappingSet containing Wikidata redirect mappings.
- parse_all() BaseMappingSet[source]
Query all entity types from config and return combined MappingSet.
Runs all SPARQL queries defined in the config file’s ‘queries’ section (e.g., chemical_redirects, gene_redirects, protein_redirects) and combines the results into a single MappingSet.
- Returns:
IdMappingSet containing all Wikidata redirect mappings.
- parse_from_file(input_path: Path | str) BaseMappingSet[source]
Parse Wikidata redirects from a pre-downloaded TSV file.
- Parameters:
input_path – Path to TSV file with SPARQL results.
- Returns:
IdMappingSet with computed cardinalities.
- parse_labels(input_path: Path | str | None = None) LabelMappingSet[source]
Return a LabelMappingSet of previous-label to current-label mappings.
Queries the SPARQL endpoint (or reads input_path) exactly like
parse(), but wraps the result in aLabelMappingSetso label-specific exports (label_sec2pri,pri_labels) work.- Parameters:
input_path – Pre-downloaded TSV file. Queries SPARQL if
None.- Returns:
LabelMappingSetwith label-based mappings.
Adding a New Parser
See Adding a source.