Curation (Python)#
raven-toolbox objects in raven_toolbox.curation.
Functions#
Function |
Summary |
|---|---|
Add or update metabolites, reactions and genes from data tables. |
|
File-path convenience wrapper for |
|
Record of what |
|
Download the GFF3 annotation and protein FASTA for an NCBI genome assembly. |
|
Build a gene-mapping table from NCBI genome annotation files. |
|
Parse a GFF3 file into a gene-protein mapping table. |
|
Rename a protein FASTA's headers using a gene mapping table. |
|
Rename |
|
Result of a gene-renaming pass. |
Reference#
batch_curate#
batch_curate(model: cobra.Model, *, mets_df: pd.DataFrame | None=None, genes_df: pd.DataFrame | None=None, rxns_df: pd.DataFrame | None=None, rxns_coeffs_df: pd.DataFrame | None=None, met_id_prefix: str='M_', rxn_id_prefix: str='R_', overwrite_with_empty: bool=True)
Add or update metabolites, reactions and genes from data tables.
Each table is optional. rxns_df and rxns_coeffs_df must be
provided together (one describes the per-reaction attributes, the
other carries the stoichiometric coefficients).
Parameters
modelModel to curate in place.
mets_df, genes_df, rxns_df, rxns_coeffs_dfTables matching the schema described in
raven_toolbox.curation.met_id_prefix, rxn_id_prefixPrefixes for new metabolite and reaction ids (for example
s_andr_for yeast-GEM,M_andR_for the cobrapy and BiGG default). New entity ids are formed by finding the largest existing zero-padded suffix matching the prefix and incrementing from there.overwrite_with_emptyHow an empty cell is applied to an entity that already exists in the model, as
curateModelFromTables'soverwriteWithEmpty. With True (default) a column present in the table overwrites the stored value, so an empty cell clears it: formula, gene short name, reaction name, subsystem, EC code, notes and InChI become empty. For charge and confidence score an empty cell becomes 0 when at least one cell of the column has a value, and a column without any value is not written. For a MIRIAM column an empty cell removes the namespace from the existing entity when at least one existing entity of the table has a value in that column. With False an empty cell leaves the stored value as it is. Independent of this option, an empty cell never changes a gene rule or a lower or upper bound (no bound given). A new entity takes the cells that have a value; an empty charge or confidence score is 0 when the column has a value in some row of the table. Must be aboolornumpy.bool_, otherwiseTypeErroris raised.
`Numeric cells (charge, confidence score, lower and upper bound, reversibility, ``rxnIdx``` :
and coefficient) are read as RAVEN reads them: an empty cell or ``NaN`` (any case, with an
:
optional sign) is not given, and other text must be a plain decimal number or ``Inf``. A
:
comma, a hexadecimal number or any expression raises :class:ValueError, and nothing is
:
evaluated. ``true`` and ``false`` in any case are 1 and 0 in the lower bound, upper bound and
:
```rev`` cells and are not numbers in the other columns. A cell is trimmed as MATLAB's` :
```strtrimtrims it, after dropping the backspaces at its start astextscan`` does; the`
:
non-breaking space, NUL and the next line character stay in the text.
:
A charge must be a finite whole number (``2``, ``-3``, ``2.0`` and ``2e0`` are accepted,
:
```0.5, 2.5andInfraise :class:`ValueError`; nothing is rounded). Arev`` cell`
:
must be 0, 1, ``true`` or ``false`` (any other number raises, although the value is not
:
otherwise used). A ``rxnIdx`` cell must be a finite whole number of at least 1; an index
:
appears in at most one row of the reaction table (the coefficient table repeats it, one row
:
per metabolite). As in RAVEN, the two reaction tables are matched on index and name, and an
:
index used by two rows of ``rxns_df`` raises :class:ValueError, which names the index and
:
the data rows. The index is compared by value (``1``, ``1.0`` and ``1e0`` are the same).
:
A coefficient must be a finite number other than zero; two rows of one reaction for the
:
same metabolite add their coefficients. A row of a reaction table without any content is
:
ignored (in the coefficient table the content is the index, reaction name, metabolite name
:
and compartment); a row with content needs a ``rxnIdx`` when the table has that column,
:
and a coefficient row needs a coefficient. A metabolite row needs a compartment. A
:
reaction of one table without a row in the other raises :class:ValueError.
:
An empty MIRIAM column removes no annotation.
:
A lower bound above the upper bound (also against the stored bound of an existing
:
reaction, since both bounds are set together) raises :class:ValueError. RAVEN's
:
```curateModelFromTableswrites such bounds as given andsolveLP`` rejects the model when`
:
it is run ("Invalid set of bounds for reaction(s): ..."); a cobra reaction cannot hold them.
:
The following checks run before the model is touched, so a table that fails leaves the
:
model unchanged:
:
* the required columns;
:
* the metabolites of the coefficient rows;
:
* the bounds (several rows that update one reaction are checked one after the other);
:
* a gene listed twice in ``genes_df``;
:
* the gene rules;
:
* the numeric cells, the charges, the ``rev`` cells and the ``rxnIdx`` cells describedabove.
Errors that cobra raises while the tables are applied are not checked beforehand, and the
:
model may then be partly changed.
:
A ``grRules`` cell must be a well-formed gene rule, in the grammar of RAVEN's
:
```parseGrRule: genes joined by andororin any case or by&, &&, |`` and`
:
```||``, with balanced parentheses. A rule with an operator that lacks an operand, empty` :
parentheses or two genes without an operator between them raises :class:ValueError, as it
:
raises ``RAVEN:badInput`` there. The rule of a new reaction that names a gene which neither
:
the model nor ``genes_df`` has also raises :class:ValueError; RAVEN's ``addRxns`` is
:
called without permission to add genes and stops with an error of its own. The rule of an
:
`existing reaction may name a gene the model does not have. A gene id that ``genes_df``` :
lists more than once raises :class:ValueError, whether the model has the gene or not.
:
The reaction gets the rule as this grammar reads it, so ``G1&G2`` is one gene, and it stays
:
one gene through :func:~raven_toolbox.io.write_yaml_model and
:
:func:~raven_toolbox.io.read_yaml_model and through cobra's SBML files. cobra's JSON
:
reader reads the rule with cobra's own grammar and splits ``G1&G2`` into ``G1`` and ``G2``.
:
A reaction table with a header and no row is empty: when both reaction tables are empty
:
nothing happens, and a coefficient row without a reaction row (or the reverse) raises
:
:class:ValueError.
:
Returns
CurationResultA summary of the changes.
batch_curate_from_tsv#
batch_curate_from_tsv(model: cobra.Model, *, mets_tsv: str | Path | None=None, genes_tsv: str | Path | None=None, rxns_tsv: str | Path | None=None, rxns_coeffs_tsv: str | Path | None=None, met_id_prefix: str='M_', rxn_id_prefix: str='R_', overwrite_with_empty: bool=True)
File-path convenience wrapper for batch_curate.
Each path is optional. A table file is read as RAVEN's curateModelFromTables reads it:
the first line is the header (also when it is blank, which leaves no usable column) and
its names are trimmed and lose one pair of surrounding double quotes; a column with an empty
header is ignored; when two columns match the same core name the first one is used and the
others are ignored, and a repeated name of another column gets .1, .2 ... appended
(RAVEN uses both columns). The rest is tab-delimited text with every cell kept as text
(NA, null or None stay text and 22 stays 22), a double quote around a
cell, spaces before a cell dropped, a trailing tab on a row ignored, and a row with more
non-empty cells than the header has columns refused with ValueError (RAVEN wraps
the extra cell into a new row). A line without any text is skipped; RAVEN makes an empty row
from a line that holds only "" or from a row that ends in two or more tabs.
Parameters
model: cobra.ModelModel to curate in place.
mets_tsv: str or Path or NonePath of the metabolite table. Default
None, which skips the metabolite table.genes_tsv: str or Path or NonePath of the gene table. Default
None, which skips the gene table.rxns_tsv: str or Path or NonePath of the reaction table. It must be given together with
rxns_coeffs_tsv, otherwisebatch_curateraisesValueError. DefaultNone.rxns_coeffs_tsv: str or Path or NonePath of the table with the stoichiometric coefficients of the reactions. It must be given together with
rxns_tsv. DefaultNone.met_id_prefix: strPrefix of the ids of new metabolites, as in
batch_curate. Default"M_".rxn_id_prefix: strPrefix of the ids of new reactions, as in
batch_curate. Default"R_".overwrite_with_empty: boolHow an empty cell is applied to an entity that already exists in the model, as in
batch_curate. DefaultTrue, which lets an empty cell clear the stored value. WithFalsean empty cell leaves the stored value as it is. A value that is not aboolornumpy.bool_raisesTypeError.
CurationResult#
Record of what batch_curate added and updated.
Each list holds the cobra ids of the affected entities, in the order they were processed.
download_genome_data#
download_genome_data(accession: str, *, output_dir: str | Path='.', verbose: bool=True)
Download the GFF3 annotation and protein FASTA for an NCBI genome assembly.
Retrieves both files in one archive via the NCBI Datasets v2 API. When both files
are already present in output_dir, nothing is fetched; deleting them forces a
new download.
Parameters
accession:NCBI genome assembly accession, for example
"GCF_000002595.2". Both RefSeq (GCF_) and GenBank (GCA_) prefixes are accepted (case-insensitively).output_dir:Directory to save the downloaded files in (default: current directory).
verbose:Print download progress.
Returns
(gff_file, faa_file):Paths to the downloaded GFF3 annotation and protein FASTA files.
Raises
ValueErrorIf
accessiondoes not start withGCF_orGCA_.RuntimeErrorIf the download or the extraction fails, or the archive lacks the GFF3 or the protein FASTA file.
get_gene_data#
get_gene_data(accession_or_path: str | Path, *, output_file: str | Path | None=None, download_dir: str | Path='.')
Build a gene-mapping table from NCBI genome annotation files.
Parameters
accession_or_path:An NCBI assembly accession (downloaded with
download_genome_dataif not already present indownload_dir), or a path to a local GFF3 file.output_file:If given, also save the table as a tab-delimited file.
download_dir:Directory to download to when
accession_or_pathis an accession.
Returns
pandas.DataFrameSee
parse_gff_gene_table.
Raises
ValueErrorIf
accession_or_pathis neither an existing file nor a recognised NCBI assembly accession.
parse_gff_gene_table#
parse_gff_gene_table(gff_path: str | Path)
Parse a GFF3 file into a gene-protein mapping table.
Extracts gene and CDS pairs. The protein accession is taken from each
CDS's protein_id (or, failing that, a Dbxref=GenBank:... entry),
and the owning gene is resolved through the Parent chain
(CDS -> mRNA -> gene for eukaryotes, CDS -> gene for prokaryotes), so
the GenBank_protein column matches a protein FASTA's headers.
Parameters
gff_path: str or PathPath of an uncompressed GFF3 file, read as UTF-8. Comment lines (starting with
#) and lines with fewer than 9 tab-separated fields are skipped.
Returns
pandas.DataFrameColumns
locus_tag,old_locus_tag,GeneID,gene_name,GenBank_protein,UniProt. Deduplicated (a CDS repeats once per exon) on every column exceptUniProt, the same key asgetGeneData.m.
process_protein_fasta_file#
process_protein_fasta_file(faa_file: str | Path, gene_table: pd.DataFrame | str | Path, header_col: str, *, output_dir: str | Path='.')
Rename a protein FASTA's headers using a gene mapping table.
Replaces each sequence header with the value from header_col,
matched via the GenBank_protein accession present in the original
FASTA header (its first whitespace-delimited token). Sequences whose
accession is not found in gene_table keep their original header.
Parameters
faa_file:Path to the protein FASTA file (
.faa) to process.gene_table:A DataFrame, or the path of a tab-delimited file that holds one (for example from
raven_toolbox.curation.get_gene_data). It needs the columns"GenBank_protein"andheader_col.header_col:Column whose values replace each FASTA header (for example
"locus_tag").output_dir:Directory the processed FASTA is saved in. The output file name is the original base name with
"_processed"appended before the extension.
Returns
PathPath to the written processed FASTA file.
Raises
FileNotFoundErrorIf
faa_filedoes not exist.ValueErrorIf
gene_tableis missing"GenBank_protein"orheader_col.
Notes
A row is skipped when its GenBank_protein is empty or missing. An empty or
missing header_col value does not skip the row: the header of the matching
sequence becomes an empty string, as in processProteinFastaFile.m.
rename_model_genes#
rename_model_genes(model: cobra.Model, gene_table: pd.DataFrame | str | Path, from_col: str, to_col: str)
Rename model's genes in place using a mapping table.
Parameters
model:Model to rename genes in.
gene_table:A DataFrame, or the path of a tab-delimited file that holds one.
from_col:Column whose values match the ids currently in
model.genes.to_col:Column whose values replace them.
Returns
RenameGenesResult
:
Raises
ValueErrorIf
from_colorto_colis not a column ofgene_table.
RenameGenesResult#
Result of a gene-renaming pass.
Parameters
renamed:Number of model genes for which a mapping was found and applied.
unmapped:Model gene ids with no entry in
from_col(or an emptyto_col), left unchanged. Sorted, unique.