Curation (Python)#
raven-toolbox objects in raven_toolbox.curation.
Functions#
Function |
Summary |
|---|---|
Add or update metabolites / reactions / 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 |
|
Outcome 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_')
Add or update metabolites / reactions / 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 freshly-generated metabolite / reaction ids (e.g.
s_andr_for yeast-GEM,M_andR_for the cobrapy / BiGG default). New entity ids are formed by finding the largest existing zero-padded suffix matching the prefix and incrementing from there.
Returns
A :class:CurationResult summarising 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_')
File-path convenience wrapper for batch_curate.
Each path is optional. TSVs are read with pandas' default
read_csv(sep='\t'); empty cells become NaN (not the
empty string), which the engine treats as "skip this field".
CurationResult#
Record of what batch_curate added / 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. Files
already present in output_dir are not re-fetched; delete them
manually to force a refresh.
Parameters
accession:NCBI genome assembly accession, e.g.
"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
accessiondoesn't start withGCF_/GCA_.RuntimeErrorIf the download, extraction, or the archive's contents are invalid.
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 automatically via
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, if
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 + 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.
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, matchinggetGeneData.m's own dedup key exactly.
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:Either a DataFrame, or a path to a tab-delimited file to load one from (e.g. from
raven_toolbox.curation.get_gene_data). Must contain at least the columns"GenBank_protein"andheader_col.header_col:Column whose values replace each FASTA header (e.g.
"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_filedoesn't exist.ValueErrorIf
gene_tableis missing"GenBank_protein"orheader_col.
Notes
A row is skipped only if its GenBank_protein is empty — not if
header_col is, matching processProteinFastaFile.m exactly (an
accession mapped to an empty header_col value still renames that
sequence's header to an empty string).
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:Either a DataFrame, or a path to a tab-delimited file to load one from.
from_col:Column whose values match the ids currently in
model.genes.to_col:Column whose values will replace them.
Returns
RenameGenesResult
:
Raises
ValueErrorIf
from_colorto_colis not a column ofgene_table.
RenameGenesResult#
Outcome 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.