Simple example

This notebook shows how to

  • load mass photometry count data

  • create and plot the mass histogram distribution

  • fit the distribution with a multi-gaussian

  • plot the fittings

[1]:
from pyphotomol import (
    MPAnalyzer,
    plot_histogram,
    plot_histograms_and_fits,
    LayoutConfig,
    LegendConfig
)

from scripts import display_figure_static # Only for static display, to be shown in GitHub
[2]:
file = '../test_files/demo.h5'
mp = MPAnalyzer()

files = [file] * 10
names = [f'demo{i+1}' for i in range(10)]

mp.import_files(files, names=names)
mp.apply_to_all('count_binding_events')
# Create the histogram - same window and bin width for all files
mp.apply_to_all('create_histogram',use_masses=True, window=[0, 800], bin_width=10)
[3]:
# Plot the mass distribution
colors = ['gray'] * len(files)

# Create configuration objects for customized plotting
layout_config = LayoutConfig(
    stacked=True,  # One plot per file
    show_subplot_titles=False,  # Hide subplot titles
    vertical_spacing=0.01,  # Vertical spacing between subplots
    extra_padding_y_label=0.02  # Extra padding for y-axis label to avoid overlap with axis ticks
)

fig = plot_histogram(mp,  # PhotoMol analyzer instance
                     colors,  # colors for each histogram
                     layout_config=layout_config)

display_figure_static(fig)
../_images/notebooks_1_simple_example_4_0.png

We can also view the unbinding events

[4]:
mp.apply_to_all('create_histogram',use_masses=True,
                window=[-800, 800], # Important negative window to include unbinding events
                bin_width=10)

fig = plot_histogram(mp,  # PhotoMol analyzer instance
                     colors,  # colors for each histogram
                     layout_config=layout_config)

display_figure_static(fig)

../_images/notebooks_1_simple_example_6_0.png
[5]:
# Re do the histogram with a positive window
mp.apply_to_all('create_histogram',use_masses=True,
                window=[0, 800], # Important negative window to include unbinding events
                bin_width=10)

# Estimate the peak positions - only to use later as a guess for the fit
# It uses scipy's find_peaks function under the hood
mp.apply_to_all('guess_peaks',min_height=10, min_distance=4, prominence=4)

# Extract the peaks positions
mp.get_properties('peaks_guess')
[5]:
[array([ 65., 145., 465.]),
 array([ 65., 145., 465.]),
 array([ 65., 145., 465.]),
 array([ 65., 145., 465.]),
 array([ 65., 145., 465.]),
 array([ 65., 145., 465.]),
 array([ 65., 145., 465.]),
 array([ 65., 145., 465.]),
 array([ 65., 145., 465.]),
 array([ 65., 145., 465.])]
[6]:
# Fit the mass distribution using a multi gaussian
mp.apply_to_all(method_name='fit_histogram',
                peaks_guess=[65,145,465], # Initial peak guess, same for both files
                threshold=40, # Minimum observed value for the massses
                fit_baseline=False) # Decide if baseline correction should be applied
[7]:
# Plot the mass distribution with the fit
legends_df, hist_df = mp.create_plotting_config()

colors_hist = hist_df['color'].tolist()

# The following column controls which legends are shown in the plot
# Set the first four legends to True to show them, others to False
legends_df['show_legend'] = False
legends_df.loc[:3, 'show_legend'] = True

# Create configuration objects for customized plotting
layout_config = LayoutConfig(
    stacked=True,
    vertical_spacing=0.04,
    extra_padding_y_label=0.02
)

fig = plot_histograms_and_fits(mp,
                               legends_df=legends_df,
                               colors_hist=colors_hist,
                               layout_config=layout_config)

display_figure_static(fig, height=800)
../_images/notebooks_1_simple_example_9_0.png
[8]:
# Print the fitted params of the first model
first_model = list(mp.models.keys())[0]
print(mp.models[first_model].fit_table)
   Position / kDa  Sigma / kDa      Counts  Counts / %  Amplitudes  \
0       65.272253    15.861073  870.825870        61.0  232.080891
1      145.751043    20.221630  293.735298        21.0   57.949551
2      480.554337    29.687422  171.772627        12.0   23.082962

   Position Error / %  Sigma Error / %  Amplitude Error / %
0                0.70             3.24                 2.43
1                1.35            10.00                 8.40
2                1.23            19.95                17.28
[9]:
# Print the logbook
logbook = mp.get_batch_logbook()
for i, row in logbook.iterrows():
    print(f"Method: {row['method']}")
    # Print the parameters for each method
    for param, value in row['parameters'].items():
        print(f"  {param}: {value}")
Method: import_files
  files: ['../test_files/demo.h5', '../test_files/demo.h5', '../test_files/demo.h5', '../test_files/demo.h5', '../test_files/demo.h5', '../test_files/demo.h5', '../test_files/demo.h5', '../test_files/demo.h5', '../test_files/demo.h5', '../test_files/demo.h5']
  names: ['demo1', 'demo2', 'demo3', 'demo4', 'demo5', 'demo6', 'demo7', 'demo8', 'demo9', 'demo10']
  n_files: 10
Method: apply_to_all
  method_name: count_binding_events
  target_models: ['demo1', 'demo2', 'demo3', 'demo4', 'demo5', 'demo6', 'demo7', 'demo8', 'demo9', 'demo10']
  args: ()
  kwargs: {}
Method: apply_to_all
  method_name: create_histogram
  target_models: ['demo1', 'demo2', 'demo3', 'demo4', 'demo5', 'demo6', 'demo7', 'demo8', 'demo9', 'demo10']
  args: ()
  kwargs: {'use_masses': True, 'window': [0, 800], 'bin_width': 10}
Method: apply_to_all
  method_name: create_histogram
  target_models: ['demo1', 'demo2', 'demo3', 'demo4', 'demo5', 'demo6', 'demo7', 'demo8', 'demo9', 'demo10']
  args: ()
  kwargs: {'use_masses': True, 'window': [-800, 800], 'bin_width': 10}
Method: apply_to_all
  method_name: create_histogram
  target_models: ['demo1', 'demo2', 'demo3', 'demo4', 'demo5', 'demo6', 'demo7', 'demo8', 'demo9', 'demo10']
  args: ()
  kwargs: {'use_masses': True, 'window': [0, 800], 'bin_width': 10}
Method: apply_to_all
  method_name: guess_peaks
  target_models: ['demo1', 'demo2', 'demo3', 'demo4', 'demo5', 'demo6', 'demo7', 'demo8', 'demo9', 'demo10']
  args: ()
  kwargs: {'min_height': 10, 'min_distance': 4, 'prominence': 4}
Method: apply_to_all
  method_name: fit_histogram
  target_models: ['demo1', 'demo2', 'demo3', 'demo4', 'demo5', 'demo6', 'demo7', 'demo8', 'demo9', 'demo10']
  args: ()
  kwargs: {'peaks_guess': [65, 145, 465], 'threshold': 40, 'fit_baseline': False}