# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda env:py38_pyhdx_pn011]
#     language: python
#     name: conda-env-py38_pyhdx_pn011-py
# ---

# + [markdown] pycharm={"name": "#%% md\n"}
# # PyHDX basics

# + pycharm={"name": "#%%\n"}
from pyhdx import PeptideMasterTable, read_dynamx, HDXMeasurement
from pyhdx.plot import plot_peptides
import matplotlib.pyplot as plt
from pathlib import Path

# + [markdown] pycharm={"name": "#%% md\n"}
# We can use the ``read_dynamx`` function to read the file. This function returns a ``numpy`` structured array where each
# entry corresponds to one peptide, in this example 567 peptides. 

# + pycharm={"name": "#%%\n"}
fpath = Path() / '..' / '..' / 'tests' / 'test_data' / 'ecSecB_apo.csv'
data = read_dynamx(fpath)
data.size

# + [markdown] pycharm={"name": "#%% md\n"}
# This array is loaded into the ``PeptideMasterTable`` class, which is the main data entry class. The parameter ``drop_first``
# determines how many N-terminal residues are considered to be fully back-exchanged, and therefore is subtracted from the
# total amount of exchangable D per peptide. The parameter ``ignore_prolines`` is controls whether the number of Prolines
# residues in the peptide should be subtracted from the total amount of exchangable and should generally be set to ``True``.
#
# The final number of exchangable residues is found in the 'ex_residues' field.

# + pycharm={"name": "#%%\n"}
master_table = PeptideMasterTable(data, drop_first=1, ignore_prolines=True)
master_table.data['ex_residues'][:50]

# + [markdown] pycharm={"name": "#%% md\n"}
# This master table allows us to control how the deuterium uptake content is determined. The method ``set_control`` can be
# used to choose which set of peptides is used as the fully deuterated (FD) control. This adds a new field called 'uptake'
# which is the normalized (to 100%) deuterium uptake of each peptide, with respect to the total amount of exchanging residues.

# + pycharm={"name": "#%%\n"}
master_table.set_control(('Full deuteration control', 0.167))
master_table.data['uptake'][:50]

# + [markdown] pycharm={"name": "#%% md\n"}
# Next we'll select our state of interest from the master Table. The available states are listed in `master_table.states`.
# Using `get_state` allows us to select all entries which belong to this state.

# + pycharm={"name": "#%%\n"}
master_table.states
state_data = master_table.get_state('SecB WT apo')
state_data.size

# -

# This `data` array can now be used to create an ``HDXMeasurement`` object, the main data object in PyHDX.
# Experimental metadata such as labelling pH and temperature can be specified. These quantities are required for calculating
# intrinsic exchange rates and ΔG values. The pH values are uncorrected values are measured by the pH meter (ie p(H, D)
# values)

# + pycharm={"name": "#%%\n"}
hdxm = HDXMeasurement(state_data, temperature=303.15, pH=8., name='My HDX measurement')
type(hdxm), len(hdxm), hdxm.timepoints, hdxm.name, hdxm.state

# + [markdown] pycharm={"name": "#%% md\n"}
# Iterating over a ``HDXMeasurement`` object returns a set of ``PeptideMeasurements`` each with their own attributes describing
# the topology of the coverage. When creating the object, peptides which are not present in all timepoints are removed, such
# that all timepoints and ``PeptideMeasurements`` have identical coverage.
#
# Note that exposure time units is assumed to be minutes.

# + pycharm={"name": "#%%\n"}
fig, ax = plt.subplots(figsize=(14, 5))
i = 0
plot_peptides(hdxm[i], ax, 20, cbar=True)
t = ax.set_title(f'Peptides t = {hdxm.timepoints[i]}')
l = ax.set_xlabel('Residue number')

# + pycharm={"name": "#%%\n"}
fig, ax = plt.subplots(figsize=(14, 5))
i = 3
plot_peptides(hdxm[i], ax, 20, cbar=True)
t = ax.set_title(f'Peptides t = {hdxm.timepoints[i]}')
l = ax.set_xlabel('Residue number')

# + pycharm={"name": "#%%\n"}


StarcoderdataJupyterScriptsDedupFiltered