mkt.schema.io_utils

Serialize and deserialize KinaseInfo objects to/from json/yaml/toml and tar archives.

Provides serialize_kinase_dict() and deserialize_kinase_dict() for round-tripping per-kinase files, plus helpers for reading packaged .tar.gz data in memory. TOML serialization is skipped on Windows.

Module Attributes

DICT_FUNCS

Dictionary of serialization and deserialization functions supported.

STR_CONSERVATION_FILENAME

Basename (no suffix) of the persisted KLIFS conservation-data artifact.

Functions

clean_files_and_delete_directory(list_files)

Remove unzipped files.

deserialize_kinase_dict([suffix, ...])

Deserialize KinaseInfo object from files.

extract_tarfiles(path_from, path_to)

Extract tar.gz files.

get_repo_root()

Get the root of the git repository.

load_conservation_data([suffix, str_path, ...])

Load a KLIFSConservationData artifact from a single file.

return_filenotfound_error_if_empty_or_missing(...)

Return FileNotFoundError for the given path.

return_str_path_from_pkg_data([str_path, ...])

Return the path to the package data directory or of a user-provided directory.

save_plot(fig, output_filename[, plot_type, ...])

Save the current matplotlib figure in the requested vector/raster formats.

serialize_conservation_data(conservation_data)

Serialize a KLIFSConservationData artifact to a single file.

serialize_kinase_dict(kinase_dict[, suffix, ...])

Serialize KinaseInfo object to files.

untar_files_in_memory(str_path[, ...])

Untar files exclusively in memory.

untar_if_neeeded(str_filename)

Unzip the file if it is a zip file.

mkt.schema.io_utils.DICT_FUNCS = {'json': {'deserialize_file': <function load>, 'deserialize_str': <function loads>, 'kwargs_deserialize': {}, 'kwargs_serialize': {'default': <class 'list'>, 'indent': 4}, 'serialize': <function dumps>}, 'toml': {'deserialize_file': <function load>, 'deserialize_str': <function loads>, 'kwargs_deserialize': {}, 'kwargs_serialize': {}, 'serialize': <function dumps>}, 'yaml': {'deserialize_file': <function safe_load>, 'deserialize_str': <function safe_load>, 'kwargs_deserialize': {}, 'kwargs_serialize': {'sort_keys': False}, 'serialize': <function safe_dump>}}

Dictionary of serialization and deserialization functions supported.

Type:

dict[str, dict[str, Callable]]

mkt.schema.io_utils.STR_CONSERVATION_FILENAME = 'KLIFSConservationData'

Basename (no suffix) of the persisted KLIFS conservation-data artifact.

Type:

str

mkt.schema.io_utils._conservation_cache = {}

Module-level cache of loaded KLIFSConservationData objects keyed by str_name (mirrors _deserialization_cache).

Type:

dict

mkt.schema.io_utils.clean_files_and_delete_directory(list_files: list[str]) None[source]

Remove unzipped files.

Parameters:

list_files (list[str]) – List of files to remove.

Returns:

None

Return type:

None

mkt.schema.io_utils.deserialize_kinase_dict(suffix: str = 'json', deserialization_kwargs: dict[str, Any] | None = None, str_path: str | None = None, bool_remove: bool = True, list_ids: list[str] | None = None, str_name: str | None = None, bool_verbose: bool = True) dict[str, BaseModel][source]

Deserialize KinaseInfo object from files.

Parameters:
  • suffix (str) – Deserialization types supported: json, yaml, toml.

  • deserialization_kwargs (dict[str, Any], optional) – Additional keyword arguments for deserialization function, by default None.

  • str_path (str | None, optional) – Path from which to load files, by default None.

  • bool_remove (bool, optional) – If True, remove the files after deserialization, by default True.

  • list_ids (list[str] | None, optional) – List of IDs to filter the files if reading from memory, by default None.

  • str_name (str | None, optional) – Name of a variable in the global scope that contains the kinase dictionary to prevent reloading, by default None.

Returns:

Dictionary of KinaseInfo objects.

Return type:

dict[str, KinaseInfo]

mkt.schema.io_utils.extract_tarfiles(path_from, path_to)[source]

Extract tar.gz files.

Parameters:
  • path_from (str) – Path to the tar.gz file

  • path_to (str) – Pth to extract the files to

Returns:

None

Return type:

None

mkt.schema.io_utils.get_repo_root()[source]

Get the root of the git repository.

Returns:

Path to the root of the git repository; if not found, return current directory

Return type:

str

mkt.schema.io_utils.load_conservation_data(suffix: str = 'json', str_path: str | None = None, str_name: str | None = None)[source]

Load a KLIFSConservationData artifact from a single file.

Parameters:
  • suffix (str) – Deserialization type supported: json, yaml, toml.

  • str_path (str | None) – Path to the artifact file, by default None (the packaged mkt/schema/KLIFSConservationData.json).

  • str_name (str | None) – If provided, cache the loaded object under this name in _conservation_cache to prevent reloading.

Returns:

The deserialized conservation-data object.

Return type:

KLIFSConservationData

mkt.schema.io_utils.return_filenotfound_error_if_empty_or_missing(str_path_in: str) FileNotFoundError | None[source]

Return FileNotFoundError for the given path.

Parameters:

str_path_in (str) – Path that was not found.

Returns:

FileNotFoundError for the given path. If the path is not empty, return None.

Return type:

FileNotFoundError | None

mkt.schema.io_utils.return_str_path_from_pkg_data(str_path: str | None = None, pkg_name: str | None = None, pkg_resource: str | None = None) str[source]

Return the path to the package data directory or of a user-provided directory.

Parameters:
  • str_path (str | None, optional) – Path to the KinaseInfo directory, by default None.

  • pkg_name (str | None, optional) – Package name, by default None and will use mkt.schema.

  • pkg_resource (str | None, optional) – Package resource, by default None and will use KinaseInfo.

Returns:

Path to the package data or user-provided directory.

Return type:

str

mkt.schema.io_utils.save_plot(fig, output_filename: str, plot_type: str = 'Plot', bool_force_local: bool = True, bool_image_subdir=True, output_path: str | None = None, bool_svg: bool = True, bool_png: bool = True, bool_pdf: bool = False, **kwargs) None[source]

Save the current matplotlib figure in the requested vector/raster formats.

Parameters:

figmatplotlib.figure.Figure

The figure object to save.

output_filenamestr

Name of the output file to save the plot. Any extension is stripped; the format suffixes are appended per the bool_svg/bool_png/bool_pdf flags.

plot_typestr

Description of the plot type for logging purposes (e.g., “Dynamic range plot”)

bool_force_localbool

If True, forces saving to the local dir (repo root or cwd) regardless of env var; default is True.

bool_image_subdirbool

If True, saves images to a subdirectory named “images” within the output path; default is True.

output_pathstr | None

Optional path to save the plot. If None, saves to the current working directory.

bool_svgbool

If True, write an SVG copy; default is True.

bool_pngbool

If True, write a PNG copy; default is True.

bool_pdfbool

If True, write a PDF copy; default is False.

**kwargs

Additional keyword arguments to pass to plt.savefig (e.g., {“dpi”: 300}). Default is empty dict.

mkt.schema.io_utils.serialize_conservation_data(conservation_data: BaseModel, suffix: str = 'json', str_path: str | None = None) str | None[source]

Serialize a KLIFSConservationData artifact to a single file.

Unlike serialize_kinase_dict() (one file per kinase), this writes the whole single-object artifact to {str_path}/KLIFSConservationData.{suffix}, reusing the DICT_FUNCS serialization registry.

Parameters:
  • conservation_data (BaseModel) – The mkt.schema.conservation_schema.KLIFSConservationData object.

  • suffix (str) – Serialization type supported: json, yaml, toml.

  • str_path (str | None) – Directory to write into, by default None (the mkt.schema package directory).

Returns:

Path written, or None if the suffix is unsupported.

Return type:

str | None

mkt.schema.io_utils.serialize_kinase_dict(kinase_dict: dict[str, BaseModel], suffix: str = 'json', serialization_kwargs: dict[str, Any] | None = None, str_path: str | None = None) None[source]

Serialize KinaseInfo object to files.

Parameters:
  • kinase_dict (dict[str, BaseModel]) – Dictionary of KinaseInfo objects.

  • suffix (str) – Serialization types supported: json, yaml, toml.

  • serialization_kwargs (dict[str, Any], optional) –

    Additional keyword arguments for serialization function, by default None;

    (e.g., {“indent”: 2} for json.dumps, {“sort_keys”: False} for yaml.safe_dump).

  • str_path (str | None = None) – Path to save the serialized file, by default None will use package data or Github repo data.

mkt.schema.io_utils.untar_files_in_memory(str_path: str, bool_extract: bool = True, list_ids: list[str] | None = None) dict[str, str][source]

Untar files exclusively in memory.

Parameters:
  • str_path (str) – Path to the tar.gz file.

  • bool_extract (bool, optional) – If True, extract the files to memory, by default True.

  • list_ids (list[str] | None, optional) – List of IDs to filter the files if reading from memory, by default None. If None, all files will be extracted.

Returns:

Dictionary of file names and their contents as strings.

Return type:

dict[str, str]

mkt.schema.io_utils.untar_if_neeeded(str_filename: str) str[source]

Unzip the file if it is a zip file.

Parameters:

str_filename (str) – Path to the file.

Returns:

Path to the unzipped file or original file if not .tar.gz.

Return type:

str