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
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
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
from matplotlib import cm 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}, "
from pyhdx.alignment import align_dataframes import numpy as np mock_alignment = { 'dimer': 'MSEQNNTEMTFQIQRIYTKDISFEAPNAPHVFQKDWQPEVKLDLDTASSQLADDVY--------------EVVLRVTVTASLGEETAFLCEVQQGGIFSIAGIEGTQMAHCLGA----YCPNILFPAARECIASMVARGTFPQLNLAPVNFDALFMNYLQQQAGEGTEEHQDA-----------------', 'apo': 'MSEQNNTEMTFQIQRIYTKDI------------SFEAPNAPHVFQKDWQPEVKLDLDTASSQLADDVYEVVLRVTVTASLG-------------------EETAFLCEVQQGGIFSIAGIEGTQMAHCLGAYCPNILFPYARECITSMVSRG----TFPQLNLAPVNFDALFMNYLQQQAGEGTEEHQDA', } current_dir = Path(__file__).parent data_dir = current_dir.parent / 'tests' / 'test_data' data = read_dynamx(data_dir / 'ecSecB_apo.csv', data_dir / 'ecSecB_dimer.csv') pmt = PeptideMasterTable(data) pmt.set_control(('Full deuteration control', 0.167)) 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) guess = csv_to_protein(data_dir / 'ecSecB_guess.txt') hdx_set = HDXMeasurementSet([st1, st2]) gibbs_guess = hdx_set.guess_deltaG([guess['rate'], guess['rate']]) hdx_set.add_alignment(list(mock_alignment.values())) result = fit_gibbs_global_batch_aligned(hdx_set,
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
import pandas as pd import numpy as np from pyhdx.panel.sources import DataFrameSource from pyhdx.panel.filters import MultiIndexSelectFilter from lumen.sources import DerivedSource directory = Path(__file__).parent data_dir = directory / 'test_data' data = read_dynamx(data_dir / 'ecSecB_apo.csv', data_dir / 'ecSecB_dimer.csv') pmt = PeptideMasterTable(data) pmt.set_control(('Full deuteration control', 0.167)) states = pmt.groupby_state() st1 = states['SecB his dimer apo'] st2 = states['SecB WT apo'] df1 = pd.DataFrame(st1.full_data) df2 = pd.DataFrame(st2.full_data) rates_df = pd.read_csv(data_dir / 'ecSecB_rates.txt', index_col=0, header=[0, 1]) class TestLumenSources(object): @classmethod def setup_class(cls):
from pyhdx.models import PeptideMasterTable, HDXMeasurement from pyhdx.fitting import BatchFitting, KineticsFitting, fit_rates_weighted_average, fit_rates from pyhdx.fileIO import csv_to_protein from pyhdx.local_cluster import default_client from dask.distributed import Client from dask import delayed, compute import numpy as np import asyncio current_dir = Path(__file__).parent np.random.seed(43) data_dir = current_dir.parent / 'tests' / 'test_data' data = read_dynamx(data_dir / 'ecSecB_apo.csv', data_dir / 'ecSecB_dimer.csv') pmt = PeptideMasterTable(data) pmt.set_control(('Full deuteration control', 0.167)) names = ['SecB his dimer apo', 'SecB WT apo'] data_objs = [] for name in names: data = pmt.get_state(name) bools = data['end'] < 40 selected_data = data[bools] st = HDXMeasurement(selected_data) #st = HDXMeasurement(data) data_objs.append(st) if __name__ == '__main__':
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
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
from pyhdx.fileIO import read_dynamx, txt_to_np, fmt_export from pyhdx.models import PeptideMasterTable from numpy.lib.recfunctions import append_fields import numpy as np array = txt_to_np('test_data/simulated_data.csv', delimiter=',') data = read_dynamx('test_data/simulated_data.csv') print(array.dtype.names) print(data.dtype.names) pmt = PeptideMasterTable(array, drop_first=0, ignore_prolines=False, remove_nan=False) print(pmt.data.dtype.names) uptake = pmt.data['ex_residues'] * pmt.data['scores'] / 100 for u, m in zip(uptake, pmt.data['ex_residues']): print(u, m) extended = append_fields(pmt.data, ['uptake'], [uptake], usemask=False) fields = ('start', 'end', 'exposure', 'state', 'sequence', 'ex_residues', 'uptake') dtype = [(name, extended[name].dtype) for name in fields] export = np.empty_like(uptake, dtype=dtype) for name in fields: export[name] = extended[name] fmt, hdr = fmt_export(export, delimiter=',', width=0) np.savetxt('test.txt', export, fmt=fmt, header=hdr) new_data = read_dynamx('test.txt')