mkt.databases.utils

General-purpose string, dataframe, and structure utility functions.

Collects string-splitting/matching helpers, dataframe aggregation and one-hot encoding utilities, and conversions between mmCIF dictionaries, Biopython structures, and serialized strings.

Functions

add_one_hot_encoding_to_dataframe(df, col_name)

Add one-hot encoding for one or more specified columns in a DataFrame.

aggregate_df_by_col_set(df_in, col_group[, ...])

Aggregate DataFrame by column and convert to set.

convert_input2list(obj_in[, bool_empty])

Convert input to a list if it is not already a list.

convert_mmcifdict2structure(dict_cif[, ...])

Convert an MMCIF2Dict dictionary into a Bio.PDB Structure.

convert_structure2string(structure)

Serialize a Bio.PDB Structure to a PDB-format string.

create_strsplit_list(list_in[, idx_start, ...])

Split list or Series of strings on delim with exception handling.

flatten_iterables_in_iterable(data)

Flatten nested lists or tuples into a single list.

load_kinase_object(str_in)

Load a kinase object from the kinase dictionary based on the given gene name.

replace_string_using_dict(str_in, dict_in)

Replace any partial matches in a string using a dictionary of {string match : string replace}.

return_bool_at_index(list_in, list_bool[, ...])

Return list of elements from list_in where corresponding element in list_bool is bool_return.

return_list_match_indices(str_in, list_in)

Return list of indices where str_in matches entry or entries in list_in.

return_list_out(list_kinhub_uniprot, ...)

Return list of indices where str_in matches entry or entries in list_in.

split_on_first_only(str_in, delim)

Split string on first occurrence of delim.

try_except_convert_str2int(str_in)

Convert string to int with exception handling.

try_except_match_str2dict(str_in, dict_in[, ...])

Dictionary match with exception handling.

try_except_split_concat_str(str_in, idx1, idx2)

Split str_in on delim with exception handling.

try_except_substraction(a, b)

Subtract two values with exception handling.

mkt.databases.utils.add_one_hot_encoding_to_dataframe(df: DataFrame, col_name: str | list[str], prefix: str | list[str] | None = None, bool_drop: bool = True, col_drop: str | list[str] | None = None) DataFrame[source]

Add one-hot encoding for one or more specified columns in a DataFrame.

Parameters:
  • df (pd.DataFrame) – Input DataFrame.

  • col_name (str | list[str]) – Column name(s) to apply one-hot encoding.

  • prefix (str | list[str] | None, optional) – Prefix(es) for the new columns. If None, uses column names as prefixes. If list, must match length of col_name, by default None.

  • bool_drop (bool, optional) – If True, drop the original column(s) after encoding, by default True.

  • col_drop (str | list[str] | None, optional) – If specified, drop these column(s) after encoding (i.e., to avoid multicollinearity).

Returns:

DataFrame with one-hot encoded columns added.

Return type:

pd.DataFrame

mkt.databases.utils.aggregate_df_by_col_set(df_in: DataFrame, col_group: str, bool_str: bool = True) DataFrame[source]

Aggregate DataFrame by column and convert to set.

Parameters:
  • df_in (pd.DataFrame) – Input DataFrame to aggregate

  • col_group (str) – Column to group by

  • bool_str (bool, optional) – If True, convert set to string, by default True

Returns:

Aggregated DataFrame with set values

Return type:

pd.DataFrame

mkt.databases.utils.convert_input2list(obj_in: Any, bool_empty: bool = False) list | None[source]

Convert input to a list if it is not already a list.

Parameters:
  • obj_in (Any) – Input object to convert to a list.

  • bool_empty (bool, optional) – If True, return an empty list if obj_in is None, by default False

Returns:

List containing the input if it is not already a list, otherwise returns the input as is.

Return type:

list

mkt.databases.utils.convert_mmcifdict2structure(dict_cif: dict[str, str | list[str]], structure_id: str = 'kinase') Structure[source]

Convert an MMCIF2Dict dictionary into a Bio.PDB Structure.

The CIF data stored on KinaseInfo.kincore.cif.cif is an MMCIF2Dict object; Bio.PDB cannot build a structure from it directly, so it is round tripped through a temporary CIF file via MMCIFIO/MMCIFParser.

Parameters:
  • dict_cif (dict[str, str | list[str]]) – CIF dictionary (MMCIF2Dict), e.g. KinaseInfo.kincore.cif.cif.

  • structure_id (str) – Identifier assigned to the returned structure, by default “kinase”.

Returns:

Bio.PDB Structure object parsed from the CIF dictionary. Residues are numbered by auth_seq_id, which for KinCore CIFs corresponds to the UniProt sequence position.

Return type:

Structure

mkt.databases.utils.convert_structure2string(structure: Structure) str[source]

Serialize a Bio.PDB Structure to a PDB-format string.

Parameters:

structure (Structure) – Bio.PDB Structure object.

Returns:

Structure in PDB string format; residue numbering is preserved from auth_seq_id (UniProt position for KinCore CIFs).

Return type:

str

mkt.databases.utils.create_strsplit_list(list_in: list[str], idx_start: int = 0, idx_end: int = 2) list[str][source]

Split list or Series of strings on delim with exception handling.

Parameters:
  • list_in (list[str]) – List of strings to split

  • idx_start (int) – Starting index

  • idx_end (int) – Ending index

Returns:

List of concatenated strings split on delim from idx_start:idx_end

Return type:

list[str]

mkt.databases.utils.flatten_iterables_in_iterable(data)[source]

Flatten nested lists or tuples into a single list.

Parameters:

data (list or tuple) – Input data to flatten

Returns:

Flattened list containing all elements from the input data

Return type:

list

mkt.databases.utils.load_kinase_object(str_in: str)[source]

Load a kinase object from the kinase dictionary based on the given gene name.

Parameters:

str_in (str) – Gene name of the kinase to load.

Returns:

KinaseInfo object corresponding to the given gene name.

Return type:

KinaseInfo

Raises:

ValueError – If the given gene name is not found in the kinase dictionary.

mkt.databases.utils.replace_string_using_dict(str_in: str, dict_in: dict[str, str]) str[source]

Replace any partial matches in a string using a dictionary of {string match : string replace}.

Parameters:
  • str_in (str) – Input string to replace partial matches

  • dict_in (dict[str, str]) – Dictionary of {string match : string replace}

Returns:

str_out – String with any partial matches replaced

Return type:

str

mkt.databases.utils.return_bool_at_index(list_in: list, list_bool: list, bool_return: bool = True)[source]

Return list of elements from list_in where corresponding element in list_bool is bool_return.

Parameters:
  • list_in (list) – List of elements to filter.

  • list_bool (list) – List of boolean values to filter by.

  • bool_return (bool, optional) – Boolean value to filter by, by default True

Returns:

List of elements from list_in where corresponding element in list_bool is bool_return.

Return type:

list

mkt.databases.utils.return_list_match_indices(str_in: str, list_in: list[str | list[str]]) list[int] | list | None[source]

Return list of indices where str_in matches entry or entries in list_in.

Parameters:
  • str_in (str) – Input string to check for matches in list_in

  • list_in (list[str | list[str]]) – List of string or list of list of strings to check for str_in match

Returns:

Returns index of matching entries in list

Return type:

list[int] | None

mkt.databases.utils.return_list_out(list_kinhub_uniprot: list[str | float], list_assay_name: list[str])[source]

Return list of indices where str_in matches entry or entries in list_in.

Parameters:
  • list_kinhub_uniprot (list[str | float]) – List of string or list of list of strings to check for str_in match

  • list_assay_name (list[str]) – List of string to check for matches in list_in

Returns:

list_out – Returns index of matching entries in list

Return type:

list[int] | None

mkt.databases.utils.split_on_first_only(str_in, delim)[source]

Split string on first occurrence of delim.

Parameters:
  • str_in (str) – Input string to split

  • delim (str) – Delimiter to split on

Returns:

Tuple containing two strings: str1 and str2

Return type:

tuple

mkt.databases.utils.try_except_convert_str2int(str_in: str)[source]

Convert string to int with exception handling.

Parameters:

str_in (str) – Input string to convert to int

Returns:

Returns int if conversion successful, otherwise returns str_in

Return type:

int | str

mkt.databases.utils.try_except_match_str2dict(str_in: str, dict_in: dict[str, Any], bool_keyout: bool = True) Any[source]

Dictionary match with exception handling.

Parameters:
  • str_in (str) – Input string

  • dict_in (dict[str, Any]) – Dictionary where keys are strings to match with str_in

  • bool_keyout (bool) – If true and no match, return string

Returns:

Returns either dictionary value if match, str_in if no match and bool_keyout = True else None

Return type:

Any

mkt.databases.utils.try_except_split_concat_str(str_in: str, idx1: int, idx2: int, delim: str = '-') str[source]

Split str_in on delim with exception handling.

Parameters:
  • str_in (str) – Input string

  • idx1 (int) – Starting index

  • idx2 (int) – Ending index

  • delim (str) – Delimiter to split on

Returns:

Concatenated string containing the strings split on delim from idx1:idx2

Return type:

str

mkt.databases.utils.try_except_substraction(a, b)[source]

Subtract two values with exception handling.

Parameters:
  • a (Any) – First value to subtract from

  • b (Any) – Second value to subtract

Returns:

Returns difference if subtraction successful, otherwise returns None

Return type:

Any