General utilities module

read_mgf

Read an MGF file into a pandas DataFrame

write_mgf

Writes a pandas DataFrame to an MGF file

is_shifted

Determine if two values differ by more than a specified tolerance.

get_formula_mass

Return the mass of a chemical formula.

get_adduct_mass

Get the mass of an adduct.

General utility functions

modifinder.utilities.general_utils.add_adduct_to_formula(formula, adduct)[source]
modifinder.utilities.general_utils.convert_to_formula(item)[source]
modifinder.utilities.general_utils.convert_to_universal_key(key: str) str[source]

Convert different types of keys to universal keys. This function standardizes various key names to a universal format.

:param : key (str): The key to be converted.

Returns:

str: The converted key.

modifinder.utilities.general_utils.entropy(probabilities)[source]
modifinder.utilities.general_utils.get_adduct_mass(adduct)[source]

Get the mass of an adduct.

Parameters:

adduct (str) –

The adduct to get the mass of.

The supported format is [aM±bA]c± where a and b and c are numbers, M is the molecule, A is the adduct, and c is the charge.

Returns:

The mass of the adduct.

Return type:

float

Raises:

ValueError – If the adduct format is not accepted

modifinder.utilities.general_utils.get_diff(formula1, formula2)[source]
modifinder.utilities.general_utils.get_elements(formula)[source]
modifinder.utilities.general_utils.get_formula_mass(formula)[source]

Return the mass of a chemical formula.

Parameters:

formula (str) – The chemical formula.

Returns:

The mass of the chemical formula.

Return type:

float

modifinder.utilities.general_utils.is_shifted(val1: float, val2: float, ppm: float | None = None, mz_tol: float | None = None) bool[source]

Determine if two values differ by more than a specified tolerance.

The function checks if the absolute difference between two values exceeds either a given parts per million (ppm) value or a mass/charge (m/z) tolerance. If only one of ppm or mz_tol is provided, the function uses that value for comparison. If both are provided, the function checks both conditions and returns True if either condition is satisfied.

:param : val1 (float): The first value to compare. :param : val2 (float): The second value to compare. :param : ppm (float, optional): The parts per million tolerance. Default is None. :param : mz_tol (float, optional): The m/z tolerance. Default is None.

Returns:

bool: True if the values differ by more than the specified tolerance, False otherwise.

Raises:

ValueError: If neither ppm nor mz_tol is provided.

modifinder.utilities.general_utils.is_submolecule(sub_formula, target_formula, ignore_H=False)[source]
modifinder.utilities.general_utils.parse_data_to_universal(data)[source]

Parse the data to a universal format.

This function takes a dictionary of data and converts it into a universal format. It processes specific keys like “peaks_json” and “Charge” differently, and attempts to convert other values to floats. If the conversion to float is successful and the key is “Charge”, it further converts the value to an integer.

:param : data (dict): The input data dictionary to be parsed.

Returns:

dict: A dictionary with keys converted to a universal format and values processed

accordingly.

modifinder.utilities.general_utils.parse_molecular_formula(formula)[source]
modifinder.utilities.general_utils.power_prob(probabilities)[source]
modifinder.utilities.general_utils.read_mgf(mgf_path: str) DataFrame[source]

Read an MGF file into a pandas DataFrame

Parameters:

mgf_path (str) – The path to the MGF file to be read

Returns:

pandas DataFrame with columns as metadata and ‘spectrum’ as the m/z and intensity values

Return type:

pd.DataFrame

modifinder.utilities.general_utils.remove_adduct_from_formula(formula, adduct)[source]
modifinder.utilities.general_utils.write_mgf(msms_df: DataFrame, mgf_path: str)[source]

Writes a pandas DataFrame to an MGF file

Parameters:
  • msms_df (pd.DataFrame) – pandas DataFrame with column ‘spectrum’ and other columns as metadata

  • mgf_path (str) – Path to write the MGF file