Skip to content

Instantly share code, notes, and snippets.

@jamespeterschinner
Last active May 15, 2019 08:57
Show Gist options
  • Save jamespeterschinner/059b2be1c69f1ebf9de1d0c09564279a to your computer and use it in GitHub Desktop.
Save jamespeterschinner/059b2be1c69f1ebf9de1d0c09564279a to your computer and use it in GitHub Desktop.

Revisions

  1. jamespeterschinner revised this gist May 13, 2019. 1 changed file with 40 additions and 33 deletions.
    73 changes: 40 additions & 33 deletions Lineweaver-burk plot.py
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,6 @@
    import pandas as pd

    import matplotlib.pyplot as plt
    from matplotlib.text import Text
    import statsmodels.formula.api as sfa


    @@ -58,8 +57,6 @@ def chunks(l, n):
    a_y_intercept = a_fit[-1] - (a_grad * x_inv[-1])
    b_y_intercept = b_fit[-1] - (b_grad * x_inv[-1])

    a_y_intercept = b_y_intercept = mean([a_y_intercept, b_y_intercept])


    def lin_eq(grad, x, int):
    return (grad * x) + int
    @@ -93,61 +90,70 @@ def lin_eq(grad, x, int):
    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.3, a_y_intercept, f'{a_y_intercept:.3f}')
    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.2, 0.001, f'{a_x_intercept:.2f}')
    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.22, 0.001, f'{b_x_intercept:.2f}')

    ax.text(.12, 0.05, r'$\mathcal{y = 0.075x + 0.013}$', color='darkred', size=12, rotation=68.5)
    ax.text(b_x_intercept - 0.1, -0.0038, f'{b_x_intercept:.2f}',)

    ax.text(.06, 0.0301, r'$\mathcal{y = 0.021x + 0.013}$', color='red', size=12, rotation=38)
    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)

    # Set 2nd tick to right hand side
    ax.text(0.05, 0.0091, '0.01')
    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]\ (L\ mmol^{-1})}$', size=14)

    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
    v_max = 1/a_y_intercept
    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([(v_max*x)/(a_km + x) for x in x_axis])
    b_pred = np.array([(v_max*x)/(b_km + x) for x in x_axis])
    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(v_max, linestyle='dashed')
    ax.text(-3, v_max + 1, r'$V_{max}$' + f' {v_max:.2f} ' +r'$mmol\ min^{-1}$' )
    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')

    a_s_con = x_axis[np.argmax(a_pred >= (v_max / 2))]
    ax.axhline(v_max/2, linestyle='dashed', color='#1f77b4')
    ax.plot([a_km]*2, [0, v_max/2], linestyle='dashed', color='black', linewidth=1)
    ax.text(8, (b_v_max / 2) + 2,
    r'$\frac{V_{max}}{2} = $' + f'{b_v_max / 2:.2f}', color='darkred')

    b_s_con = x_axis[np.argmax(b_pred >= (v_max / 2))]
    ax.plot([b_km]*2, [0, v_max/2], linestyle='dashed', color='black', linewidth=1)
    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')
    @@ -156,16 +162,17 @@ def lin_eq(grad, x, int):
    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.2))
    ax.set_xlabel(r'$p-nitrophenol\ phosphate\ (L\ mmol^{-1})$')
    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, .5, f'{a_km:.2f}')
    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, .5, f'{b_km:.2f}')
    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',)
    fig.text(0.15, 0.01, 'https://gist.github.com/jamespeterschinner/059b2be1c69f1ebf9de1d0c09564279a', color='grey',
    size=8)
    plt.savefig('lineweaver_burk', )
    plt.show()
  2. jamespeterschinner revised this gist May 12, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Lineweaver-burk plot.py
    Original file line number Diff line number Diff line change
    @@ -115,7 +115,7 @@ def lin_eq(grad, x, int):
    ax.text(0.05, 0.0091, '0.01')

    ax.set_xlabel(r'$\frac{1}{[p-nitrophenol\ phosphate]\ (L\ mmol^{-1})}$', size=14)
    ax.text(-1, 0.034, r'$\frac{1}{V_{max}(L\ mmol^{-1})}$', 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)
  3. jamespeterschinner revised this gist May 12, 2019. 1 changed file with 27 additions and 20 deletions.
    47 changes: 27 additions & 20 deletions Lineweaver-burk plot.py
    Original file line number Diff line number Diff line change
    @@ -93,13 +93,13 @@ def lin_eq(grad, x, int):
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')

    ax.plot(0, b_y_intercept, color='black', marker='o', markersize=4)
    ax.plot(0, b_y_intercept, color='black', marker='o', markersize=3)
    ax.text(-0.3, a_y_intercept, f'{a_y_intercept:.3f}')

    ax.plot(a_x_intercept, 0, color='black', marker='o', markersize=4)
    ax.plot(a_x_intercept, 0, color='black', marker='o', markersize=3)
    ax.text(a_x_intercept - 0.2, 0.001, f'{a_x_intercept:.2f}')

    ax.plot(b_x_intercept, 0, color='black', marker='o', markersize=4)
    ax.plot(b_x_intercept, 0, color='black', marker='o', markersize=3)
    ax.text(b_x_intercept - 0.22, 0.001, f'{b_x_intercept:.2f}')

    ax.text(.12, 0.05, r'$\mathcal{y = 0.075x + 0.013}$', color='darkred', size=12, rotation=68.5)
    @@ -118,26 +118,33 @@ def lin_eq(grad, x, int):
    ax.text(-1, 0.034, r'$\frac{1}{V_{max}(L\ mmol^{-1})}$', size=14)
    ax = fig.add_subplot(122)
    ax.set_title('Standard curve')
    x_axis = np.linspace(0, 2, 100)
    a_pred = np.array([-1 / lin_eq(a_grad, x, a_y_intercept) for x in x_axis])
    b_pred = np.array([-1 / lin_eq(b_grad, x, b_y_intercept) for x in x_axis])
    x_axis = np.linspace(0, 15, 100)

    v_max = -1 / lin_eq(a_grad, 100, a_y_intercept) + abs(min(a_pred))
    a_pred = a_pred + abs(min(a_pred))
    b_pred = b_pred + abs(min(b_pred))
    a_km = -1/ a_x_intercept
    b_km = -1/ b_x_intercept
    v_max = 1/a_y_intercept

    ax.plot(x_axis, a_pred, label='competitive\ninhibition', color='darkred')
    ax.plot(x_axis, b_pred, label='uninhibited', color='red')
    a_pred = np.array([(v_max*x)/(a_km + x) for x in x_axis])
    b_pred = np.array([(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(v_max, linestyle='dashed')
    ax.text(-0.25, v_max + 1, r'$V_{max}$' + f' ={v_max:.2f} ' +r'$mmol\ min^{-1}$' )
    ax.text(-3, v_max + 1, r'$V_{max}$' + f' {v_max:.2f} ' +r'$mmol\ min^{-1}$' )

    a_s_con = x_axis[np.argmax(a_pred >= (v_max / 2))]
    ax.axhline(v_max/2, linestyle='dashed', color='#1f77b4')
    ax.plot([a_s_con]*2, [0, v_max/2], linestyle='dashed', color='black', linewidth=1)
    ax.plot([a_km]*2, [0, v_max/2], linestyle='dashed', color='black', linewidth=1)

    b_s_con = x_axis[np.argmax(b_pred >= (v_max / 2))]
    ax.plot([b_s_con]*2, [0, v_max/2], linestyle='dashed', color='black', linewidth=1)
    ax.plot([b_km]*2, [0, v_max/2], linestyle='dashed', color='black', linewidth=1)

    ax.set_ylim(0, 80)
    labels = [str(round(i, 3)) for i in ax.get_yticks()]
    labels[-1] = ''
    ax.set_yticklabels(labels)




    @@ -149,16 +156,16 @@ def lin_eq(grad, x, int):
    ax.spines['bottom'].set_smart_bounds(True)
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')
    ax.legend(loc=(0.4, 0.2))
    ax.legend(loc=(0.45, 0.2))
    ax.set_xlabel(r'$p-nitrophenol\ phosphate\ (L\ mmol^{-1})$')
    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 + 0.05, 1, f'{a_s_con:.2f}')
    # ax.plot(a_s_con, 0, color='black', marker='o', markersize=4)
    ax.text(a_s_con + .2, .5, f'{a_km:.2f}')

    ax.plot(b_s_con, 0, color='black', marker='o', markersize=4)
    ax.text(b_s_con + 0.05, 1, f'{b_s_con:.2f}')
    # ax.plot(b_s_con, 0, color='black', marker='o', markersize=4)
    ax.text(b_s_con + .2, .5, f'{b_km:.2f}')

    fig.text(0.35, 0, 'https://github.com/jamespeterschinner', color='grey', size=8)
    fig.text(0.15, 0.01, 'https://gist.github.com/jamespeterschinner/059b2be1c69f1ebf9de1d0c09564279a', color='grey', size=8)
    plt.savefig('lineweaver_burk',)
    plt.show()
  4. jamespeterschinner created this gist May 11, 2019.
    164 changes: 164 additions & 0 deletions Lineweaver-burk plot.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,164 @@
    import numpy as np

    from itertools import chain
    from statistics import mean
    import pandas as pd

    import matplotlib.pyplot as plt
    from matplotlib.text import Text
    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])

    a_y_intercept = b_y_intercept = mean([a_y_intercept, b_y_intercept])


    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, b_y_intercept, color='black', marker='o', markersize=4)
    ax.text(-0.3, a_y_intercept, f'{a_y_intercept:.3f}')

    ax.plot(a_x_intercept, 0, color='black', marker='o', markersize=4)
    ax.text(a_x_intercept - 0.2, 0.001, f'{a_x_intercept:.2f}')

    ax.plot(b_x_intercept, 0, color='black', marker='o', markersize=4)
    ax.text(b_x_intercept - 0.22, 0.001, f'{b_x_intercept:.2f}')

    ax.text(.12, 0.05, r'$\mathcal{y = 0.075x + 0.013}$', color='darkred', size=12, rotation=68.5)

    ax.text(.06, 0.0301, r'$\mathcal{y = 0.021x + 0.013}$', color='red', size=12, rotation=38)

    # Remove first y tick
    labels = [str(round(i, 3)) for i in ax.get_yticks()]
    labels[1] = labels[2] = ''
    ax.set_yticklabels(labels)

    # Set 2nd tick to right hand side
    ax.text(0.05, 0.0091, '0.01')

    ax.set_xlabel(r'$\frac{1}{[p-nitrophenol\ phosphate]\ (L\ mmol^{-1})}$', size=14)
    ax.text(-1, 0.034, r'$\frac{1}{V_{max}(L\ mmol^{-1})}$', size=14)
    ax = fig.add_subplot(122)
    ax.set_title('Standard curve')
    x_axis = np.linspace(0, 2, 100)
    a_pred = np.array([-1 / lin_eq(a_grad, x, a_y_intercept) for x in x_axis])
    b_pred = np.array([-1 / lin_eq(b_grad, x, b_y_intercept) for x in x_axis])

    v_max = -1 / lin_eq(a_grad, 100, a_y_intercept) + abs(min(a_pred))
    a_pred = a_pred + abs(min(a_pred))
    b_pred = b_pred + abs(min(b_pred))

    ax.plot(x_axis, a_pred, label='competitive\ninhibition', color='darkred')
    ax.plot(x_axis, b_pred, label='uninhibited', color='red')

    ax.axhline(v_max, linestyle='dashed')
    ax.text(-0.25, v_max + 1, r'$V_{max}$' + f' ={v_max:.2f} ' +r'$mmol\ min^{-1}$' )

    a_s_con = x_axis[np.argmax(a_pred >= (v_max / 2))]
    ax.axhline(v_max/2, linestyle='dashed', color='#1f77b4')
    ax.plot([a_s_con]*2, [0, v_max/2], linestyle='dashed', color='black', linewidth=1)

    b_s_con = x_axis[np.argmax(b_pred >= (v_max / 2))]
    ax.plot([b_s_con]*2, [0, v_max/2], linestyle='dashed', color='black', linewidth=1)



    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.4, 0.2))
    ax.set_xlabel(r'$p-nitrophenol\ phosphate\ (L\ mmol^{-1})$')
    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 + 0.05, 1, f'{a_s_con:.2f}')

    ax.plot(b_s_con, 0, color='black', marker='o', markersize=4)
    ax.text(b_s_con + 0.05, 1, f'{b_s_con:.2f}')

    fig.text(0.35, 0, 'https://github.com/jamespeterschinner', color='grey', size=8)
    plt.savefig('lineweaver_burk',)
    plt.show()