def load_from_yaml(yaml_dict, data_dir=None):  #name: load what from yaml?
    """
    Creates a :class:`~pyhdx.fitting.KineticsFitting` object from dictionary input.

    Dictionary can be generated from .yaml format and should specifiy

    Parameters
    ----------
    yaml_dict : :obj:`dict`
        Input dictionary specifying metadata and file location to load
    data_dir : :obj:`str` or pathlib.Path object

    Returns
    -------
    kf : :class:`~pyhdx.fititng.KineticsFitting`
        :class:`~pyhdx.fititng.KineticsFitting` class as specified by input dict.
    """

    if data_dir is not None:
        input_files = [Path(data_dir) / fname for fname in yaml_dict['filenames']]
    else:
        input_files = yaml_dict['filenames']

    data = read_dynamx(*input_files)

    pmt = PeptideMasterTable(data, d_percentage=yaml_dict['d_percentage'])  #todo add proline, n_term options
    if 'control' in yaml_dict.keys():  # Use a FD control for back exchange correction
        pmt.set_control(tuple(yaml_dict['control']))
    elif 'be_percent' in yaml_dict.keys():  # Flat back exchange percentage for all peptides\
        pmt.set_backexchange(yaml_dict['be_percent'])
    else:
        raise ValueError('No valid back exchange control method specified')

    try:
        c_term = yaml_dict.get('c_term', 0) or len(yaml_dict['sequence']) + 1
    except KeyError:
        raise ValueError("Must specify either 'c_term' or 'sequence'")

    states = pmt.groupby_state(c_term=c_term)
    series = states[yaml_dict['series_name']]

    if yaml_dict['temperature_unit'].lower() == 'celsius':
        temperature = yaml_dict['temperature'] + 273.15
    elif yaml_dict['temperature_unit'].lower() == 'kelvin':
        temperature = yaml_dict['temperature']
    else:
        raise ValueError("Invalid option for 'temperature_unit', must be 'Celsius' or 'Kelvin'")

    kf = KineticsFitting(series, temperature=temperature, pH=yaml_dict['pH'])

    return kf
Exemple #2
0
    def setup_class(cls):
        cls.fpath = input_dir / 'ecSecB_apo.csv'
        data = read_dynamx(cls.fpath)
        control = ('Full deuteration control', 0.167*60)

        cls.temperature, cls.pH = 273.15 + 30, 8.

        pf = PeptideMasterTable(data, drop_first=1, ignore_prolines=True, remove_nan=False)
        pf.set_control(control)
        cls.hdxm = HDXMeasurement(pf.get_state('SecB WT apo'), temperature=cls.temperature, pH=cls.pH)

        initial_rates = csv_to_dataframe(output_dir / 'ecSecB_guess.csv')

        gibbs_guess = cls.hdxm.guess_deltaG(initial_rates['rate'])
        cls.fit_result = fit_gibbs_global(cls.hdxm, gibbs_guess, epochs=100, r1=2)
def load_folding_from_yaml(yaml_dict, data_dir=None):  #name: load what from yaml?
    """
    Creates a :class:`~pyhdx.fitting.KineticsFitting` object from dictionary input.

    Dictionary can be generated from .yaml format and should specifiy

    Parameters
    ----------
    yaml_dict : :obj:`dict`
        Input dictionary specifying metadata and file location to load
    data_dir : :obj:`str` or pathlib.Path object

    Returns
    -------
    kf : :class:`~pyhdx.fititng.KineticsFitting`
        :class:`~pyhdx.fititng.KineticsFitting` class as specified by input dict.
    """

    if data_dir is not None:
        input_files = [Path(data_dir) / fname for fname in yaml_dict['filenames']]
    else:
        input_files = yaml_dict['filenames']

    data = read_dynamx(*input_files)

    pmt = PeptideMasterTable(data, d_percentage=yaml_dict['d_percentage'])  #todo add proline, n_term options
    #todo merge this with the other func where it checks for control names to determine what to apply
    pmt.set_control(control_100=tuple(yaml_dict['control_100']), control_0=tuple(yaml_dict['control_0']))

    try:
        c_term = yaml_dict.get('c_term', 0) or len(yaml_dict['sequence']) + 1
    except KeyError:
        raise ValueError("Must specify either 'c_term' or 'sequence'")

    states = pmt.groupby_state(c_term=c_term)
    series = states[yaml_dict['series_name']]

    if yaml_dict['temperature_unit'].lower() == 'celsius':
        temperature = yaml_dict['temperature'] + 273.15
    elif yaml_dict['temperature_unit'].lower() == 'kelvin':
        temperature = yaml_dict['temperature']
    else:
        raise ValueError("Invalid option for 'temperature_unit', must be 'Celsius' or 'Kelvin'")

    kf = KineticsFitting(series, temperature=temperature, pH=yaml_dict['pH'])

    return kf
Exemple #4
0
def load_folding_from_yaml(yaml_dict,
                           data_dir=None):  #name: load what from yaml?
    """


    """

    raise NotImplementedError(
        'Loading folding data from yaml currently not implemented')

    if data_dir is not None:
        input_files = [
            Path(data_dir) / fname for fname in yaml_dict['filenames']
        ]
    else:
        input_files = yaml_dict['filenames']

    data = read_dynamx(*input_files)

    pmt = PeptideMasterTable(data, d_percentage=yaml_dict['d_percentage']
                             )  #todo add proline, n_term options
    #todo merge this with the other func where it checks for control names to determine what to apply
    pmt.set_control(control_1=tuple(yaml_dict['control_1']),
                    control_0=tuple(yaml_dict['control_0']))

    try:
        c_term = yaml_dict.get('c_term', 0) or len(yaml_dict['sequence']) + 1
    except KeyError:
        raise ValueError("Must specify either 'c_term' or 'sequence'")

    states = pmt.groupby_state(c_term=c_term)
    series = states[yaml_dict['series_name']]

    if yaml_dict['temperature_unit'].lower() == 'celsius':
        temperature = yaml_dict['temperature'] + 273.15
    elif yaml_dict['temperature_unit'].lower() == 'kelvin':
        temperature = yaml_dict['temperature']
    else:
        raise ValueError(
            "Invalid option for 'temperature_unit', must be 'Celsius' or 'Kelvin'"
        )

    kf = KineticsFitting(series, temperature=temperature, pH=yaml_dict['pH'])

    return kf
from pyhdx.fileIO import csv_to_protein, read_dynamx, dataframe_to_file, save_fitresult
from pyhdx.fitting import fit_gibbs_global_batch
from pyhdx.fitting_torch import CheckPoint
from pyhdx.models import PeptideMasterTable, HDXMeasurement, HDXMeasurementSet

current_dir = Path(__file__).parent
#current_dir = Path().cwd() / 'templates'  # pycharm scientific compat

output_dir = current_dir / 'output'
output_dir.mkdir(exist_ok=True)
data_dir = current_dir.parent / 'tests' / 'test_data'
data = read_dynamx(data_dir / 'input' / 'ecSecB_apo.csv', data_dir / 'input' / 'ecSecB_dimer.csv')

pmt = PeptideMasterTable(data)
pmt.set_control(('Full deuteration control', 0.167*60))

st1 = HDXMeasurement(pmt.get_state('SecB his dimer apo'), pH=8, temperature=273.15 + 30)
st2 = HDXMeasurement(pmt.get_state('SecB WT apo'), pH=8, temperature=273.15 + 30)

hdx_set = HDXMeasurementSet([st1, st2])
guess = csv_to_protein(data_dir / 'output' / 'ecSecB_guess.csv')
gibbs_guess = hdx_set[0].guess_deltaG(guess['rate'])


# Example fit with only 5000 epochs and high learning rate
# Checkpoint stores model history every `epoch_step` epochs
checkpoint = CheckPoint(epoch_step=250)
result = fit_gibbs_global_batch(hdx_set, gibbs_guess, r1=0.5, r2=0.1, epochs=5000, lr=1e5, callbacks=[checkpoint])
print(f"MSE loss: {result.mse_loss:.2f}, "
      f"Reg loss: {result.reg_loss:.2f}, "
Exemple #6
0
def load_from_yaml(yaml_dict,
                   data_dir=None,
                   **kwargs):  #name: load what from yaml?
    #todo perhas classmethod on HDXMeasurement object?
    """
    Creates a :class:`~pyhdx.models.HDXMeasurement` object from dictionary input.

    Dictionary can be generated from .yaml format and should specify

    Parameters
    ----------
    yaml_dict : :obj:`dict`
        Input dictionary specifying metadata and file location to load
    data_dir : :obj:`str` or pathlib.Path object

    Returns
    -------
    hdxm : :class:`~pyhdx.models.HDXMeasurement`
        Output data object as specified by `yaml_dict`.
    """

    if data_dir is not None:
        input_files = [
            Path(data_dir) / fname for fname in yaml_dict['filenames']
        ]
    else:
        input_files = yaml_dict['filenames']

    data = read_dynamx(*input_files)

    pmt = PeptideMasterTable(data, d_percentage=yaml_dict['d_percentage']
                             )  #todo add proline, n_term options
    if 'control' in yaml_dict.keys(
    ):  # Use a FD control for back exchange correction
        pmt.set_control(tuple(yaml_dict['control']))
    elif 'be_percent' in yaml_dict.keys(
    ):  # Flat back exchange percentage for all peptides\
        pmt.set_backexchange(yaml_dict['be_percent'])
    else:
        raise ValueError('No valid back exchange control method specified')

    if yaml_dict['temperature_unit'].lower() == 'celsius':
        temperature = yaml_dict['temperature'] + 273.15
    elif yaml_dict['temperature_unit'].lower() == 'kelvin':
        temperature = yaml_dict['temperature']
    else:
        raise ValueError(
            "Invalid option for 'temperature_unit', must be 'Celsius' or 'Kelvin'"
        )

    sequence = yaml_dict.get('sequence', '')
    c_term = yaml_dict.get('c_term', 0)
    n_term = yaml_dict.get('n_term', 1)

    if not (c_term or sequence):
        raise ValueError("Must specify either 'c_term' or 'sequence'")

    state_data = pmt.get_state([yaml_dict['series_name']])
    hdxm = HDXMeasurement(state_data,
                          temperature=temperature,
                          pH=yaml_dict['pH'],
                          sequence=sequence,
                          n_term=n_term,
                          c_term=c_term,
                          **kwargs)

    return hdxm
Exemple #7
0
def load_from_yaml_v040b2(yaml_dict,
                          data_dir=None,
                          **kwargs):  # pragma: no cover
    """
    This is the legacy version to load yaml files of PyHDX v0.4.0b2

    Creates a :class:`~pyhdx.models.HDXMeasurement` object from dictionary input.

    Dictionary can be generated from .yaml format. See templates/yaml_files/SecB.yaml for format specification.

    Parameters
    ----------
    yaml_dict : :obj:`dict`
        Input dictionary specifying experimental metadata and file location to load
    data_dir : :obj:`str` or pathlib.Path object

    Returns
    -------
    hdxm : :class:`~pyhdx.models.HDXMeasurement`
        Output data object as specified by `yaml_dict`.
    """

    if data_dir is not None:
        input_files = [
            Path(data_dir) / fname for fname in yaml_dict["filenames"]
        ]
    else:
        input_files = yaml_dict["filenames"]

    data = read_dynamx(*input_files)

    pmt = PeptideMasterTable(data, d_percentage=yaml_dict["d_percentage"]
                             )  # todo add proline, n_term options
    if "control" in yaml_dict.keys(
    ):  # Use a FD control for back exchange correction
        pmt.set_control(tuple(yaml_dict["control"]))
    elif ("be_percent" in yaml_dict.keys()
          ):  # Flat back exchange percentage for all peptides\
        pmt.set_backexchange(yaml_dict["be_percent"])
    else:
        raise ValueError("No valid back exchange control method specified")

    if yaml_dict["temperature_unit"].lower() == "celsius":
        temperature = yaml_dict["temperature"] + 273.15
    elif yaml_dict["temperature_unit"].lower() == "kelvin":
        temperature = yaml_dict["temperature"]
    else:
        raise ValueError(
            "Invalid option for 'temperature_unit', must be 'Celsius' or 'Kelvin'"
        )

    sequence = yaml_dict.get("sequence", "")
    c_term = yaml_dict.get("c_term", 0)
    n_term = yaml_dict.get("n_term", 1)

    if not (c_term or sequence):
        raise ValueError("Must specify either 'c_term' or 'sequence'")

    state_data = pmt.get_state([yaml_dict["series_name"]])
    hdxm = HDXMeasurement(state_data,
                          temperature=temperature,
                          pH=yaml_dict["pH"],
                          sequence=sequence,
                          n_term=n_term,
                          c_term=c_term,
                          **kwargs)

    return hdxm
Exemple #8
0
def yaml_to_hdxm(yaml_dict, data_dir=None, data_filters=None, **kwargs):
    # todo perhas classmethod on HDXMeasurement object?
    """
    Creates a :class:`~pyhdx.models.HDXMeasurement` object from dictionary input.

    Dictionary can be generated from .yaml format. See templates/yaml_files/SecB.yaml for format specification.

    Parameters
    ----------
    yaml_dict : :obj:`dict`
        Input dictionary specifying experimental metadata and file location to load
    data_dir : :obj:`str` or pathlib.Path object

    Returns
    -------
    hdxm : :class:`~pyhdx.models.HDXMeasurement`
        Output data object as specified by `yaml_dict`.
    """

    if data_dir is not None:
        input_files = [
            Path(data_dir) / fname for fname in yaml_dict["filenames"]
        ]
    else:
        input_files = yaml_dict["filenames"]

    data = read_dynamx(*input_files)

    pmt = PeptideMasterTable(data,
                             drop_first=yaml_dict.get('drop_first', 1),
                             d_percentage=yaml_dict['d_percentage']
                             )  #todo add proline, n_term options

    if 'control' in yaml_dict.keys(
    ):  # Use a FD control for back exchange correction
        # todo control should be set from an external file
        control_state = yaml_dict["control"]["state"]
        exposure_value = yaml_dict["control"]["exposure"]["value"]
        exposure_units = yaml_dict["control"]["exposure"]["unit"]
        control_exposure = exposure_value * time_factors[exposure_units]

        pmt.set_control((control_state, control_exposure))
    elif ("be_percent" in yaml_dict.keys()
          ):  # Flat back exchange percentage for all peptides\
        pmt.set_backexchange(yaml_dict["be_percent"])
    else:
        raise ValueError("No valid back exchange control method specified")

    temperature = yaml_dict["temperature"]["value"]
    try:
        t_offset = temperature_offsets[yaml_dict["temperature"]["unit"]]
    except KeyError:
        t_offset = temperature_offsets[yaml_dict["temperature"]
                                       ["unit"].lower()]

    temperature += t_offset

    sequence = yaml_dict.get("sequence", "")
    c_term = yaml_dict.get("c_term")
    n_term = yaml_dict.get("n_term") or 1

    if not (c_term or sequence):
        raise ValueError("Must specify either 'c_term' or 'sequence'")

    state_data = pmt.get_state(yaml_dict["state"])
    data_filters = data_filters or []
    for filter in data_filters:
        state_data = filter(state_data)

    hdxm = HDXMeasurement(state_data,
                          temperature=temperature,
                          pH=yaml_dict["pH"],
                          sequence=sequence,
                          n_term=n_term,
                          c_term=c_term,
                          **kwargs)

    return hdxm