This notebook loads cluster FRAP data from the output of the "Extract_two_radii_TrackMate.ijm" ImageJ macro and FRAP data from unclustered proteins from the output of the "Manual_FRAP_ROI.ijm" ImageJ macro. Both datasets are then fit and compared.


In [ ]:
# load the built-in and custom modules

# uncomment for debugging
%load_ext autoreload 
%autoreload 2 

import os, sys, inspect
import matplotlib.pylab as plt
import numpy as np
from pprint import pprint
import glob
import pandas as pd
import seaborn as sns


# Add source code directory (src) to path to enable module import
curr_frame = inspect.getfile(inspect.currentframe())
curr_dir = os.path.dirname(os.path.abspath(curr_frame))
parent_dir = os.path.dirname(curr_dir)
module_dir = os.path.join(parent_dir, 'src')
os.sys.path.insert(0, module_dir)

# import fraptools
import fraptools as frap

In [ ]:
# Load FRAP data from clusters

# Path to cluster FRAP data (text files saved by "Extract_two_radii_TrackMate.ijm")
data_dir = '../data/processed/IRE1_foci_FRAP_2018-10-11/spot_radii'

# The excluded trace file allows you to manually remove bad traces from the analysis.
# It should be a simple csv with each line containing the file name followed by trace ID
excluded_trace_file = '../data/processed/IRE1_foci_FRAP_2018-10-11/2018-10-11_excluded_traces.csv'

# Populate excluded trace file
excluded_files_df = pd.read_csv(excluded_trace_file)
excluded_traces = [tuple(x) for x in excluded_files_df.values]

# Generate list of all valid cluster FRAP files
frap_files = sorted(glob.glob(os.path.join(data_dir,'*.txt')))

# list of all FRAP data:
frap_data_by_file = []
filenames_no_ext = []

# Go file by file and read data
for file in frap_files:
    
    # Read data from the provided source file
    data = pd.read_csv(file, delimiter='\t')
    filename_no_ext = os.path.split(os.path.splitext(file)[0])[1]
    frap_data_by_file.append(data)
    filenames_no_ext.append(filename_no_ext)

# Extract individual traces from the raw data
df_by_trace, corr_ints, trace_IDs = frap.get_traces_from_df_list(frap_data_by_file, 
                                    filenames_no_ext, exclude=excluded_traces)

In [ ]:
# Load FRAP data from non-clustered proteins

# Path to diffuse FRAP data (proteins diffusing freely, outside of large clusters)
# Output of "Manual_FRAP_ROI.ijm" ImageJ macro
data_dir_noclust = '../data/processed/IRE1_foci_FRAP_2018-10-11/manual_ROI_FRAP_non-clustered/intensities'

frap_files_noclust = sorted(glob.glob(os.path.join(data_dir_noclust,'*.csv')))

# list of all FRAP data:
frap_data_by_file_noclust = []
filenames_no_ext_noclust = []

# Go file by file and read data
for file in frap_files_noclust:
    
    # Read data from the provided source file
    data = pd.read_csv(file, delimiter=',')
    filename_no_ext = os.path.split(os.path.splitext(file)[0])[1]
    frap_data_by_file_noclust.append(data)
    filenames_no_ext_noclust.append(filename_no_ext)

# break up data into smaller data frames, one per trace
df_by_trace_noclust, corr_ints_noclust, trace_IDs_noclust = \
    frap.read_nonclust_frap_data(frap_data_by_file_noclust, 
                        filenames_no_ext_noclust, exclude=excluded_traces)

In [ ]:
# Analyze and plot the FRAP data from clusters
# Plotting and figure saving params
plot_figs = False
save_figs = True
save_dir = '../reports/figures/2018-10-11_FRAP_clusters-vs-diffuse/clusters'

# Input imaging parameters:
frame_interval = 1 # in seconds 
bleach_n_frames = 8 # how many frames bleaching takes (max)

# interactive plotting
if plot_figs: 
    %matplotlib    
    
# create save figure dirs
if save_figs:
    save_dir_png = os.path.join(save_dir, 'png')
    save_dir_svg = os.path.join(save_dir, 'svg')
    if not os.path.exists(save_dir_png):
        os.makedirs(save_dir_png)
    if not os.path.exists(save_dir_svg):
        os.makedirs(save_dir_svg)

# Fit the individual FRAP traces
fit, data = frap.fit_frap_smart(corr_ints, frame_interval, bleach_n_frames)

# Plot results
for f,d,trace_ID in zip(fit, data, trace_IDs):
    
    file_name = trace_ID[0]
    trace_num = trace_ID[1]
    
    full_name = file_name + '_trace-ID_' + str(trace_num)
    
    fig, axarr = frap.plot_fit_results(f,d)
    fig.canvas.set_window_title(full_name)
    plt.suptitle(full_name)
    if save_figs:
        fig_filename_svg = os.path.join(save_dir_svg, (full_name+'.svg'))
        fig_filename_png = os.path.join(save_dir_png, (full_name+'.png'))
        plt.savefig(fig_filename_svg)
        plt.savefig(fig_filename_png)
        plt.close(fig)
print("done")

In [ ]:
# Analyze and plot the FRAP data from diffuse proteins
# Plotting and figure saving params
plot_figs = False
save_figs = True
save_dir = '../reports/figures/2018-10-11_FRAP_clusters-vs-diffuse/diffuse'

# Input imaging parameters:
#frame_interval = 5 # in seconds #0.2
#bleach_n_frames = 8 # how many frames bleaching takes (max)

# interactive plotting
if plot_figs: 
    %matplotlib    
    
# create save figure dirs
if save_figs:
    save_dir_png = os.path.join(save_dir, 'png')
    save_dir_svg = os.path.join(save_dir, 'svg')
    if not os.path.exists(save_dir_png):
        os.makedirs(save_dir_png)
    if not os.path.exists(save_dir_svg):
        os.makedirs(save_dir_svg)

# Fit the individual FRAP traces
fit_noclust, data_noclust = frap.fit_frap_smart(corr_ints_noclust, frame_interval, bleach_n_frames)

    
# Plot results
for f,d,trace_ID in zip(fit_noclust, data_noclust, trace_IDs_noclust):
    
    file_name = trace_ID[0]
    trace_num = trace_ID[1]
    
    full_name = file_name + '_trace-ID_' + str(trace_num)
    
    fig, axarr = frap.plot_fit_results(f,d)
    fig.canvas.set_window_title(full_name)
    plt.suptitle(full_name)
    if save_figs:
        fig_filename_svg = os.path.join(save_dir_svg, (full_name+'.svg'))
        fig_filename_png = os.path.join(save_dir_png, (full_name+'.png'))
        plt.savefig(fig_filename_svg)
        plt.savefig(fig_filename_png)
        plt.close()
        print("Processed trace ID", trace_ID)
print("done")

In [ ]:
# Summarize fit results
save_dir = '../reports/figures/2018-10-11_FRAP_clusters-vs-diffuse/summary'
# create save figure dirs
if save_figs:
    save_dir_png = os.path.join(save_dir, 'png')
    save_dir_svg = os.path.join(save_dir, 'svg')
    if not os.path.exists(save_dir_png):
        os.makedirs(save_dir_png)
    if not os.path.exists(save_dir_svg):
        os.makedirs(save_dir_svg)

%matplotlib

# Prepare data for plottting
frap_fits_foci = fit
thalf_foci = [f['thalf'] for f in frap_fits_foci]
mobile_f_foci = [f['mobile_fraction'] for f in frap_fits_foci]   

thalf_noclust = [f['thalf'] for f in fit_noclust]
mobile_f_noclust = [f['mobile_fraction'] for f in fit_noclust]   

print(np.mean(thalf_foci), np.mean(mobile_f_foci))
print(np.mean(thalf_noclust), np.mean(mobile_f_noclust))


plt.style.use('seaborn-deep')
sns.set_style("darkgrid", {"axes.facecolor": ".9"})

# Create summary figure
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(10, 6))

df1 = pd.DataFrame({'Non-clustered' : thalf_noclust})
df2 = pd.DataFrame({'Clustered' : thalf_foci})
df_thalf = pd.concat([df1, df2], axis = 1)

df3 = pd.DataFrame({'Non-clustered' : mobile_f_noclust})
df4 = pd.DataFrame({'Clustered' : mobile_f_foci})
df_mobile = pd.concat([df3, df4], axis = 1)

sns.boxplot(data=df_thalf, linewidth=2.5, showfliers = False, ax=axes[0])
sns.swarmplot(data=df_thalf, color = 'yellow', ax=axes[0])
axes[0].set_ylabel('Half-time of recovery (s)')

sns.boxplot(data=df_mobile, linewidth=2.5, showfliers = False, ax=axes[1])
sns.swarmplot(data=df_mobile, color = 'yellow', ax=axes[1])
axes[1].set_ylabel('Mobile fraction')


if save_figs:
    fig_filename_svg = os.path.join(save_dir_svg, 'Clusters_vs_noClusters_recovery.svg')
    fig_filename_png = os.path.join(save_dir_png, 'Clusters_vs_noClusters_recovery.png')
    plt.savefig(fig_filename_svg)
    plt.savefig(fig_filename_png)

plt.show()