Skip to content

Instantly share code, notes, and snippets.

@jamespeterschinner
Last active May 15, 2019 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamespeterschinner/059b2be1c69f1ebf9de1d0c09564279a to your computer and use it in GitHub Desktop.
Save jamespeterschinner/059b2be1c69f1ebf9de1d0c09564279a to your computer and use it in GitHub Desktop.
Example lineweaver-burk plot of competitive inhibition
import numpy as np
from itertools import chain
from statistics import mean
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.formula.api as sfa
def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(l), n):
yield l[i:i + n]
x_axis = [2.5, 5, 10, 15]
a_data = np.array([301, 284, 318, 524, 575, 583, 645, 645])
b_data = np.array([140, 155, 171, 171, 398, 383, 524, 546])
c_data = np.array([24, 27, 50, 54, 90, 91, 296, 255])
a_diff = a_data - c_data
b_diff = b_data - c_data
a_diff = a_diff / 100
b_diff = b_diff / 100
a_raw = list(a_diff / 10.2 * 200)
b_raw = list(b_diff / 10.2 * 200)
a_mean = [mean(i) for i in chunks(a_raw, 2)]
b_mean = [mean(i) for i in chunks(b_raw, 2)]
a_inv = [1 / i for i in a_mean]
b_inv = [1 / i for i in b_mean]
x_inv = [1 / i for i in x_axis]
df = pd.DataFrame({'a': a_inv, 'b': b_inv, 'x': x_inv})
a_reg = sfa.ols('a ~ x', data=df).fit()
b_reg = sfa.ols('b ~ x', data=df).fit()
a_fit = list(a_reg.fittedvalues)
b_fit = list(b_reg.fittedvalues)
a_grad = (a_fit[0] - a_fit[-1]) / (x_inv[0] - x_inv[-1])
b_grad = (b_fit[0] - b_fit[-1]) / (x_inv[0] - x_inv[-1])
a_y_intercept = a_fit[-1] - (a_grad * x_inv[-1])
b_y_intercept = b_fit[-1] - (b_grad * x_inv[-1])
def lin_eq(grad, x, int):
return (grad * x) + int
x_min, x_max = -0.65, 0.65
x_axis = np.linspace(x_min, x_max, 1000)
a_values = [lin_eq(a_grad, x, a_y_intercept) for x in x_axis]
b_values = [lin_eq(b_grad, x, b_y_intercept) for x in x_axis]
a_x_intercept = -a_y_intercept / a_grad
b_x_intercept = -b_y_intercept / b_grad
plot_df = pd.DataFrame({'a': a_values, 'b': b_values, 'x': x_axis})
y_min, y_max = 0, max(chain(a_values, b_values))
fig = plt.figure()
ax = fig.add_subplot(121)
ax.set_title('Lineweaver-Burk')
mask = plot_df.a >= 0
ax.plot(plot_df.x[mask], plot_df.a[mask], label='uninhibited', color='red')
mask = plot_df.b >= 0
ax.plot(plot_df.x[mask], plot_df.b[mask], label='competitive\ninhibition', color='darkred')
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
ax.spines['left'].set_smart_bounds(True)
ax.spines['bottom'].set_smart_bounds(True)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.plot(0, a_y_intercept, color='black', marker='o', markersize=3)
ax.text(0.05, a_y_intercept - 0.001, f'{a_y_intercept:.3f}',)
ax.plot(0, b_y_intercept, color='black', marker='o', markersize=3)
ax.text(-0.25, b_y_intercept - 0.001, f'{b_y_intercept:.3f}',)
ax.plot(a_x_intercept, 0, color='black', marker='o', markersize=3)
ax.text(a_x_intercept - 0.1, -0.0038, f'{a_x_intercept:.2f}',)
ax.plot(b_x_intercept, 0, color='black', marker='o', markersize=3)
ax.text(b_x_intercept - 0.1, -0.0038, f'{b_x_intercept:.2f}',)
ax.text(.06, 0.025, r'$\mathcal{y=0.02x + 0.01}$', color='red', size=12, rotation=34)
ax.text(.11, 0.05, r'$\mathcal{y=0.07x + 0.02}$', color='darkred', size=12, rotation=66)
# Remove first y tick
labels = [str(round(i, 3)) for i in ax.get_yticks()]
labels[1] = labels[2] = ''
ax.set_yticklabels(labels)
labels = [str(round(i, 3)) for i in ax.get_xticks()]
labels[1] = ''
ax.set_xticklabels(labels)
ax.set_xlabel(r'$\frac{1}{[p-nitrophenol\ phosphate]\ (mM)}$', size=14)
ax.text(-1, 0.034, r'$\frac{1}{V_{max}(min\ mmol^{-1})}$', size=14)
ax = fig.add_subplot(122)
ax.set_title('Standard curve')
x_axis = np.linspace(0, 15, 100)
a_km = -1 / a_x_intercept
b_km = -1 / b_x_intercept
a_v_max = 1 / a_y_intercept
b_v_max = 1 / b_y_intercept
a_pred = np.array([(a_v_max * x) / (a_km + x) for x in x_axis])
b_pred = np.array([(b_v_max * x) / (b_km + x) for x in x_axis])
ax.plot(x_axis, a_pred, label='uninhibited', color='red')
ax.plot(x_axis, b_pred, label='competitive\ninhibition', color='darkred')
ax.axhline(a_v_max, linestyle='dashed')
ax.text(8, (a_v_max / 2) + 2,
r'$\frac{V_{max}}{2} = $' + f'{a_v_max / 2:.2f}', color='red')
ax.text(8, (b_v_max / 2) + 2,
r'$\frac{V_{max}}{2} = $' + f'{b_v_max / 2:.2f}', color='darkred')
a_s_con = x_axis[np.argmax(a_pred >= (a_v_max / 2))]
b_s_con = x_axis[np.argmax(b_pred >= (b_v_max / 2))]
ax.axhline(a_v_max / 2, linestyle='dashed', color='red')
ax.axhline(b_v_max / 2, linestyle='dashed', color='darkred')
ax.plot([a_km] * 2, [0, a_v_max / 2], linestyle='dashed', color='red', linewidth=1)
ax.plot([b_km] * 2, [0, b_v_max / 2], linestyle='dashed', color='darkred', linewidth=1)
ax.set_ylim(0, 80)
labels = [str(round(i, 3)) for i in ax.get_yticks()]
labels[-1] = ''
ax.set_yticklabels(labels)
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
ax.spines['left'].set_smart_bounds(True)
ax.spines['bottom'].set_smart_bounds(True)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.legend(loc=(0.45, 0.15))
ax.set_xlabel(r'$p-nitrophenol\ phosphate\ (mM)$')
ax.set_ylabel(r'$V_{max}\ (nmol\ min^{-1})$')
# ax.plot(a_s_con, 0, color='black', marker='o', markersize=4)
ax.text(a_s_con + .2, 4, f'{a_km:.2f}', rotation='45')
# ax.plot(b_s_con, 0, color='black', marker='o', markersize=4)
ax.text(b_s_con + .2, 4, f'{b_km:.2f}', rotation='45')
fig.text(0.15, 0.01, 'https://gist.github.com/jamespeterschinner/059b2be1c69f1ebf9de1d0c09564279a', color='grey',
size=8)
plt.savefig('lineweaver_burk', )
plt.show()
@jamespeterschinner
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment