Beispiel #1
0
def get_ec50(model: IfnModel, times: list or int, dose_species: str, response_species: str, custom_parameters={},
             rflag=False):
    if type(times) == int or type(times) == float:
        dr_curve = [el[0] for el in model.doseresponse([times], response_species, dose_species, list(logspace(-3, 5)),
                                      parameters=custom_parameters, return_type='list')[response_species]]

        top, K = fit_MM(list(logspace(-3, 5)), dr_curve, [max(dr_curve), 1000])
        if rflag:
            return top, K
        else:
            return K

    elif type(times) == list:
        dr_curve = model.doseresponse(times, response_species, dose_species, list(logspace(-3, 5)),
                                      parameters=custom_parameters, return_type='list')[response_species]

        top_list = []
        K_list = []
        for t in range(len(times)):
            tslice = [el[t] for el in dr_curve]
            top, K = fit_MM(list(logspace(-3, 5)), tslice, [max(tslice), 1000])
            top_list.append(top)
            K_list.append(K)
        if rflag:
            return top_list, K_list
        else:
            return K_list

    else:
        raise TypeError("Could not identify type for variable times")
Beispiel #2
0
def __unpackMCMC__(ID, jobs, result, countQ, build_model, build_data,
                   model_parameters, temperature):
    """
    This function is used by the MCMC class but must be defined externally.
    Should not be used by any other program.
    """
    model_name, fit_parameters, priors, jump_distributions = build_model
    data_set, data_name, build_conditions = build_data
    model = IfnModel(model_name)
    model.parameters = model_parameters
    if data_name is None:
        data = IfnData(name='custom', df=data_set, conditions=build_conditions)
    else:
        data = IfnData(data_name)
    processMCMC = MCMC(model, data, fit_parameters, priors, jump_distributions)
    processMCMC.temperature = temperature
    processMCMC.__run_chain__(ID, jobs, result, countQ)
Beispiel #3
0
    def likelihood(parameter_vector, sampled_parameter_names=param_names):
        # Make model
        from ifnclass.ifnmodel import IfnModel
        import numpy as np
        model = IfnModel(model_name)

        # Change model parameter values to current location in parameter space (values are in log10(value) format)
        param_dict = {
            pname: 10**pvalue
            for pname, pvalue in zip(sampled_parameter_names, parameter_vector)
        }
        model.set_parameters(param_dict)

        # Simulate experimentally measured values.

        total_simulation_data = model.doseresponse(
            times,
            response_species,
            dose_species,
            doses,
            parameters={n: 0
                        for n in IC_names if n != dose_species},
            return_type='dataframe',
            dataframe_labels=dose_species,
            scale_factor=100.0 / model.parameters['EpoR_IC'])

        sim_data = np.array([[el[0] for el in dose]
                             for dose in total_simulation_data.values
                             ]).flatten()

        # Calculate log probability contribution from simulated experimental values.

        logp_ctotal = np.sum(like_ctot.logpdf(sim_data))

        # If model simulation failed due to integrator errors, return a log probability of -inf.
        if np.isnan(logp_ctotal):
            logp_ctotal = -np.inf

        return logp_ctotal
Beispiel #4
0
def chi2(model: IfnModel, new_parameters: dict, times: list):
    reset_dict = dict(
        zip(new_parameters,
            [model.parameters[key] for key in new_parameters.keys()]))
    dra1 = model.doseresponse(times,
                              'TotalpSTAT',
                              'Ia',
                              list(logspace(-1, 5)),
                              parameters={'Ib': 0},
                              return_type='list')['TotalpSTAT']
    drb1 = model.doseresponse(times,
                              'TotalpSTAT',
                              'Ib',
                              list(logspace(-2, 4)),
                              parameters={'Ia': 0},
                              return_type='list')['TotalpSTAT']
    model.set_parameters(new_parameters)

    dra2 = model.doseresponse(times,
                              'TotalpSTAT',
                              'Ia',
                              list(logspace(-1, 5)),
                              parameters={'Ib': 0},
                              return_type='list')['TotalpSTAT']
    drb2 = model.doseresponse(times,
                              'TotalpSTAT',
                              'Ib',
                              list(logspace(-2, 4)),
                              parameters={'Ia': 0},
                              return_type='list')['TotalpSTAT']
    model.set_parameters(reset_dict)
    alpha_norm = max(max([item for sublist in dra1 for item in sublist]),
                     max([item for sublist in dra2 for item in sublist]))
    beta_norm = max(max([item for sublist in drb1 for item in sublist]),
                    max([item for sublist in drb2 for item in sublist]))

    value = (np.sum(np.square(np.divide(np.subtract(dra1, dra2), alpha_norm)))
             + np.sum(np.square(np.divide(np.subtract(drb1, drb2),
                                          beta_norm)))) / times[-1]
    value = np.nan_to_num(value)
    return value
Beispiel #5
0
from ifnclass.ifndata import IfnData
from ifnclass.ifnmodel import IfnModel
from ifnclass.ifnplot import Trajectory, TimecoursePlot, DoseresponsePlot
from ifnclass.ifnfit import StepwiseFit
from numpy import linspace, logspace, log10, nan
import seaborn as sns
from smooth_B6_IFN import *  # Imports smoothed data to fit to

if __name__ == '__main__':
    newdata = IfnData("20181113_B6_IFNs_Dose_Response_Bcells")
    Mixed_Model = IfnModel('')
    Mixed_Model.load_model('fitting_2_5.p')

    Mixed_Model.set_parameters({
        'kpu': 0.0028,
        'R2': 2300 * 2.5,
        'R1': 1800 * 1.8,
        'k_d4': 0.06,
        'kint_b': 0.0003,
        'krec_b1': 0.001,
        'k_a1': 4.98E-14,
        'k_a2': 8.30e-13 * 4,
        'kSOCSon': 0.9e-8,
        'ka1': 3.321155762205247e-14 * 0.3,
        'ka2': 4.98173364330787e-13 * 0.3,
        'kint_a': 0.0014,
        'krec_a1': 9e-03
    })
    scale_factor = 0.036
    alpha_palette = sns.color_palette("Reds", 9)
    beta_palette = sns.color_palette("Greens", 9)
Beispiel #6
0
        return logp_ctotal

    return likelihood


if __name__ == '__main__':
    # --------------------
    # Import data
    #---------------------
    Moraga_data = IfnData("2015_pSTAT5_Epo")

    # --------------------
    # Set up Model
    # --------------------
    model = IfnModel('Epo_model')

    # ---------------------------------
    # Make theory dose response curves
    # ---------------------------------
    IC_names = ['Epo_IC', 'EMP1_IC', 'EMP33_IC']
    # Make predictions
    response_species = 'T_Epo'
    dose_species = 'Epo_IC'
    times = [60.0]  # min
    Epo_doses = Moraga_data.get_doses()['T_Epo']  # pM
    dr_Epo = IfnData(
        'custom',
        df=model.doseresponse(
            times,
            response_species,
Beispiel #7
0
from ifnclass.ifnmodel import IfnModel
from numpy import linspace, logspace
import seaborn as sns
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
import numpy as np
import matplotlib

if __name__ == '__main__':
    alpha_palette = sns.color_palette("Reds", 6)
    beta_palette = sns.color_palette("Greens", 6)

    Mixed_Model = IfnModel('Mixed_IFN_ppCompatible')

    Mixed_Model.set_parameters({
        'R2': 2300 * 2.5,
        'R1': 1800 * 1.8,
        'k_d4': 0.06,
        'kint_b': 0.0003,
        'kpu': 0.0028,
        'krec_b1': 0.001,
        'krec_b2': 0.01,
        'k_a1': 4.98E-14,
        'k_a2': 8.30e-13 * 4,
        'kSOCSon': 0.9e-8,
        'ka1': 3.321155762205247e-14 * 0.3,
        'ka2': 4.98173364330787e-13 * 0.3,
        'kd4': 1.0,
        'kd3': 0.001,
        'kint_a': 0.0014,
        'krec_a1': 9e-03,
Beispiel #8
0
 def __init__(self, name, pop1_weight, pop2_weight):
     self.name = name
     self.model_1 = IfnModel(name)
     self.model_2 = IfnModel(name)
     self.w1 = pop1_weight
     self.w2 = pop2_weight
            n_list.append(n)
            K_list.append(K)
        if rflag:
            return top_list, n_list, K_list
        else:
            return K_list

    else:
        raise TypeError("Could not identify type for variable times")


if __name__ == '__main__':
    alpha_palette = sns.color_palette("Reds", 6)
    beta_palette = sns.color_palette("Greens", 6)

    Mixed_Model = IfnModel('Mixed_IFN_ppCompatible')

    Mixed_Model.set_parameters({
        'R2': 2300 * 2.5,
        'R1': 1800 * 1.8,
        'k_d4': 0.06,
        'kint_b': 0.0003,
        'kpu': 0.0028,
        'krec_b1': 0.001,
        'krec_b2': 0.01,
        'k_a1': 4.98E-14,
        'k_a2': 8.30e-13 * 4,
        'kSOCSon': 0.9e-8,
        'ka1': 3.321155762205247e-14 * 0.3,
        'ka2': 4.98173364330787e-13 * 0.3,
        'kd4': 1.0,
Beispiel #10
0
from ifnclass.ifndata import IfnData
from ifnclass.ifnmodel import IfnModel
from ifnclass.ifnplot import DoseresponsePlot
import seaborn as sns
from numpy import logspace, log10

if __name__ == '__main__':
    Mixed_Model = IfnModel('Mixed_IFN_Internalized_Signaling')
    # Mixed_Model = IfnModel('Mixed_IFN_ppCompatible')

    alpha_palette = sns.color_palette("Reds", 3)
    beta_palette = sns.color_palette("Greens", 3)

    # Arbitrarily chose best fit parameters for GAB 20190119 data
    Mixed_Model.set_parameters({
        'R2': 5700,
        'R1': 1800,
        'k_a1': 4.98E-14 * 2,
        'k_a2': 1.328e-12,
        'k_d3': 2.4e-06,
        'k_d4': 0.228,
        'kSOCSon': 5e-08,
        'kpu': 0.0011,
        'ka1': 3.3e-15,
        'ka2': 1.22e-12,
        'kd4': 0.86,
        'kd3': 1.74e-05,
        'kint_a': 0.000124,
        'kint_b': 0.00086,
        'krec_a1': 0.0028,
        'krec_a2': 0.01,
from ifnclass.ifndata import IfnData
from ifnclass.ifnmodel import IfnModel
from ifnclass.ifnplot import Trajectory, TimecoursePlot, DoseresponsePlot
from numpy import linspace, logspace, log10, nan
import seaborn as sns
import os


if __name__ == '__main__':
    alpha_palette = sns.color_palette("Reds", 6)
    beta_palette = sns.color_palette("Greens", 6)

    # This is the best fit parameters for GAB aligned data
    Mixed_Model = IfnModel('Mixed_IFN_ppCompatible')
    # Optimal parameters for fitting mean GAB data
    default_parameters = {'R2': 4920, 'R1': 1200,
     'k_a1': 2.0e-13, 'k_a2': 1.328e-12, 'k_d3': 1.13e-4, 'k_d4': 0.9,
     'kSOCSon': 5e-08, 'kpu': 0.0022, 'kpa': 2.36e-06,
     'ka1': 3.3e-15, 'ka2': 1.85e-12, 'kd4': 2.0,
     'kd3': 6.52e-05,
     'kint_a': 0.0015, 'kint_b': 0.002,
     'krec_a1': 0.01, 'krec_a2': 0.01, 'krec_b1': 0.005, 'krec_b2': 0.05}

    scale_factor = 1.46182313424

    Mixed_Model.set_parameters(default_parameters)
    Mixed_Model.default_parameters.update(default_parameters)

    # Produce plots
    times = [60]
    # No Negative Feedback
                bounds_error=False)

    # Sort the points by density, so that the densest points are plotted last
    if sort:
        idx = z.argsort()
        x, y, z = x[idx], y[idx], z[idx]

    ax.scatter(x, y, c=z, **kwargs)
    return ax


if __name__ == '__main__':
    # -------------------------------
    # Initialize model
    # -------------------------------
    Mixed_Model = IfnModel('Mixed_IFN_ppCompatible')
    initial_parameters = {
        'k_a1': 4.98E-14 * 2,
        'k_a2': 1.328e-12,
        'k_d3': 2.4e-06,
        'k_d4': 0.228,
        'kSOCSon': 8e-07,
        'kpu': 0.0011,
        'ka1': 3.3e-15,
        'ka2': 1.22e-12,
        'kd4': 0.86,
        'kd3': 1.74e-05,
        'kint_a': 0.000124,
        'kint_b': 0.00086,
        'krec_a1': 0.0028,
        'krec_a2': 0.01,
    newdata_3 = IfnData("20190121_pSTAT1_IFN_Bcell")
    newdata_4 = IfnData("20190214_pSTAT1_IFN_Bcell")

    alignment = DataAlignment()
    alignment.add_data([newdata_4, newdata_3, newdata_2, newdata_1])
    alignment.align()
    alignment.get_scaled_data()
    mean_data = alignment.summarize_data()

    # -------------------------------
    # Initialize model
    # -------------------------------
    times = newdata_4.get_times('Alpha')
    doses_alpha = newdata_4.get_doses('Alpha')
    doses_beta = newdata_4.get_doses('Beta')
    Mixed_Model = IfnModel('Mixed_IFN_ppCompatible')
    Mixed_Model.set_parameters({
        'R2': 4920,
        'R1': 1200,
        'k_a1': 2.0e-13,
        'k_a2': 1.328e-12,
        'k_d3': 1.13e-4,
        'k_d4': 0.9,
        'kSOCSon': 5e-08,
        'kpu': 0.0022,
        'kpa': 2.36e-06,
        'ka1': 3.3e-15,
        'ka2': 1.85e-12,
        'kd4': 2.0,
        'kd3': 6.52e-05,
        'kint_a': 0.00048,
Beispiel #14
0
from ifnclass.ifndata import IfnData
from ifnclass.ifnmodel import IfnModel
from ifnclass.ifnplot import Trajectory, TimecoursePlot, DoseresponsePlot
from numpy import linspace, logspace, log10, nan
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from ifnclass.ifnfit import MCMC, Prior
import pickle

if __name__ == '__main__':
    raw_data = IfnData("20190108_pSTAT1_IFN")
    Mixed_Model = IfnModel('Mixed_IFN_ppCompatible')
    best_parameters = {'R2': 5700, 'R1': 1800,
                     'k_a1': 4.98E-14 * 2, 'k_a2': 8.30e-13 * 4, 'k_d3': 2.4e-06, 'k_d4': 0.228,
                     'kSOCSon': 2e-7,'kpu': 0.0014,
                     'ka1': 3.321155762205247e-14 * 0.1, 'ka2': 4.98173364330787e-13 * 0.5, 'kd4': 0.84, 'kd3': 0.001,
                     'kint_a': 0.0002, 'kint_b': 0.00048,
                     'krec_a1': 1e-04, 'krec_a2': 0.02, 'krec_b1': 0.001, 'krec_b2': 0.005}
    Mixed_Model.set_parameters(best_parameters)

    scale_factor = 0.26
    scale_data = lambda q: (scale_factor * q[0], scale_factor * q[1])

    # Try MCMC sampling

    # first data set best fit parameters:
    """
    {'kpu': 0.0028,
     'R2': 5750,
     'R1': 3240,
Beispiel #15
0
from ifnclass.ifndata import IfnData
from ifnclass.ifnmodel import IfnModel
from ifnclass.ifnplot import DoseresponsePlot
from numpy import linspace, logspace, log10, nan
import seaborn as sns
from smooth_B6_IFN import *  # Imports smoothed data to fit to

if __name__ == '__main__':
    newdata = IfnData("20181113_B6_IFNs_Dose_Response_Bcells")
    Mixed_Model = IfnModel('')
    '''
    fit_parameters = OrderedDict(
        [('kd4', 1.0), ('krec_a1', 3.0000000000000001e-05), ('krec_a2', 0.050000000000000003), ('krec_b2', 0.01),
         ('krec_b1', 0.001), ('k_d4', 0.00059999999999999995), ('kSOCSon', 1e-08), ('kd3', 0.001),
         ('k_d3', 2.3999999999999999e-06)])
    Mixed_Model.set_parameters(fit_parameters)
    Mixed_Model.save_model('fitting_2_5.p')
    '''
    Mixed_Model.load_model('fitting_2_5.p')

    alpha_palette = sns.color_palette("Reds", 6)
    beta_palette = sns.color_palette("Greens", 6)

    dra60df = Mixed_Model.doseresponse([2.5, 10, 20, 60],
                                       'TotalpSTAT',
                                       'Ia',
                                       list(logspace(-3, 5)),
                                       parameters={'Ib': 0},
                                       return_type='dataframe',
                                       dataframe_labels='Alpha')
    drb60df = Mixed_Model.doseresponse([2.5, 10, 20, 60],
Beispiel #16
0
from ifnclass.ifndata import IfnData
from ifnclass.ifnmodel import IfnModel
from ifnclass.ifnplot import TimecoursePlot, DoseresponsePlot
from numpy import linspace, logspace, log10, nan
import seaborn as sns


if __name__ == '__main__':
    Sagar_data = IfnData("MacParland_Extended")
    Mixed_Model = IfnModel('')
    '''
    fit_parameters = OrderedDict(
        [('kd4', 1.0), ('krec_a1', 3.0000000000000001e-05), ('krec_a2', 0.050000000000000003), ('krec_b2', 0.01),
         ('krec_b1', 0.001), ('k_d4', 0.00059999999999999995), ('kSOCSon', 1e-08), ('kd3', 0.001),
         ('k_d3', 2.3999999999999999e-06)])
    Mixed_Model.set_parameters(fit_parameters)
    Mixed_Model.save_model('fitting_2_5.p')
    '''
    Mixed_Model.load_model('fitting_2_5.p')

    alpha_palette = sns.color_palette("Reds", 6)
    beta_palette = sns.color_palette("Greens", 6)

    # ---------------------------
    # Now try to improve the fit:
    # ---------------------------
    """
    Results of the 5 minute stepwise fit
    OrderedDict([('R2', 500.0), ('kpu', 0.0001), ('R1', 821.42857142857144), ('k_d4', 0.10000000000000001), ('kd4', 0.21642857142857141), ('kd3', 0.00021642857142857143), ('k_d3', 0.00040000000000000002)])
    10.6992020307
    # Previous attempt                 
from ifnclass.ifnmodel import IfnModel
from ifnclass.ifnplot import DoseresponsePlot
from ifnclass.ifndata import IfnData
import numpy as np
from numpy import logspace
import matplotlib.pyplot as plt
import seaborn as sns

ImmuneCell_Model = IfnModel('Immune_Cell_model')

IFNg_doses = list(logspace(-1.5, 2, num=15))
times = [15, 30, 60]
fig = DoseresponsePlot((2, 2))
fig.axes[0][0].set_title('pSTAT1')
fig.axes[0][1].set_title('pSTAT3')

print("Dose-response 1")
IFNg_naive_res = ImmuneCell_Model.doseresponse(times, ['pSTAT1', 'pSTAT3'],
                                               'IFN_gamma_IC',
                                               IFNg_doses,
                                               parameters={
                                                   'IFNAR1_IC': 0,
                                                   'IFNAR2_IC': 0,
                                                   'IFN_alpha2_IC': 0,
                                                   'IFN_beta_IC': 0,
                                                   'SOCS2_IC': 0
                                               },
                                               return_type='dataframe',
                                               dataframe_labels='IFNgamma')
naive_IFNg_pSTAT1_df = IfnData(name='custom', df=IFNg_naive_res.xs(['pSTAT1']))
naive_IFNg_pSTAT3_df = IfnData(name='custom', df=IFNg_naive_res.xs(['pSTAT3']))
Beispiel #18
0
    newdata_3 = IfnData("20190121_pSTAT1_IFN_Bcell")
    newdata_4 = IfnData("20190214_pSTAT1_IFN_Bcell")

    alignment = DataAlignment()
    alignment.add_data([newdata_4, newdata_3, newdata_2, newdata_1])
    alignment.align()
    alignment.get_scaled_data()
    mean_data = alignment.summarize_data()

    # -------------------------------
    # Initialize model
    # -------------------------------
    times = newdata_4.get_times('Alpha')
    doses_alpha = newdata_4.get_doses('Alpha')
    doses_beta = newdata_4.get_doses('Beta')
    Mixed_Model = IfnModel('Mixed_IFN_ppCompatible')
    Mixed_Model.set_parameters({
        'R2': 7280,
        'R1': 4920,
        'kSOCSon': 1e-7,
        'kpu': 0.001,
        'kpa': 1e-06,
        'kSOCS': 0.0046,
        'kint_a': 0,
        'kint_b': 0
    })
    scale_factor = 0.472732426722
    scale_data = lambda q: (scale_factor * q[0], scale_factor * q[1])

    # -------------------------------
    # Perform stepwise fit
Beispiel #19
0
    # By Jake VanderPlas
    # License: BSD-style
    """Create an N-bin discrete colormap from the specified input map"""

    # Note that if base_cmap is a string or None, you can simply do
    #    return plt.cm.get_cmap(base_cmap, N)
    # The following works for string, None, or a colormap instance:

    base = plt.cm.get_cmap(base_cmap)
    color_list = base(np.linspace(0, 1, N))
    cmap_name = base.name + str(N)
    return base.from_list(cmap_name, color_list, N)


if __name__ == '__main__':
    Detailed_Model = IfnModel('Mixed_IFN_detailed')
    # Put detailed model into parameter state best fit for Ratnadeep's data (20181113_B6_IFNs_Dose_Response_Bcells)
    baseline_parameters = {'R2': 2300 * 2.5,
         'R1': 1800 * 1.8, 'k_d4': 0.06, 'kint_b': 0.0008,
         'kpu': 0.0028,
         'k_a1': 4.98E-14, 'k_a2': 8.30e-13 * 6, 'kSOCSon': 0.9e-8, 'k_d4': 0.06,
         'ka1': 3.321155762205247e-14 * 0.3, 'ka2': 4.98173364330787e-13 * 0.3, 'kd4': 1.0, 'kd3': 0.001,
         'kint_a': 0.0014, 'krec_a1': 9e-03, 'krec_a2': 0.05}
    Detailed_Model.set_parameters(baseline_parameters)
    # -----------------
    # Compute Hessian
    # -----------------
    #   First create parameter vector to compute Hessian at
    parameters_to_test = list(baseline_parameters.keys())
    all_parameters = ['ka1', 'kd1', 'ka2', 'kd2', 'ka3', 'kd3', 'ka4', 'kd4', 'k_a1', 'k_d1', 'k_a2', 'k_d2', 'k_a3', 'k_d3', 'k_a4', 'k_d4',
     'kint_a', 'kint_b', 'krec_a1', 'krec_a2', 'krec_b1', 'krec_b2', 'kIntBasal_r1', 'kIntBasal_r2', 'krec_r1', 'krec_r2', 'kdeg_a', 'kdeg_b',
Beispiel #20
0
from ifnclass.ifnmodel import IfnModel
from ifnclass.ifnplot import DoseresponsePlot
from ifnclass.ifndata import IfnData
import numpy as np
from numpy import logspace
import matplotlib.pyplot as plt
import seaborn as sns

ImmuneCell_Model = IfnModel('Immune_Cell_model')
# -------------------------------------------
# Get naive responses (i.e. one IFN at a time)
# -------------------------------------------
IFNg_doses = list(logspace(-2, 2, num=30))
IFNa_doses = list(logspace(0, 4, num=30))  # (pM)
IFNb_doses = list(logspace(-1, 3, num=30))  # (pM)

times = [15, 30, 60]
# IFN gamma
IFNg_naive_res = ImmuneCell_Model.doseresponse(times, ['pSTAT1', 'pSTAT3'],
                                               'IFN_gamma_IC',
                                               IFNg_doses,
                                               parameters={
                                                   'IFNAR1_IC': 0,
                                                   'IFNAR2_IC': 0,
                                                   'IFN_alpha2_IC': 0,
                                                   'IFN_beta_IC': 0
                                               },
                                               return_type='dataframe',
                                               dataframe_labels='IFNgamma')
naive_IFNg_pSTAT1_df = IfnData(name='custom', df=IFNg_naive_res.xs(['pSTAT1']))
naive_IFNg_pSTAT3_df = IfnData(name='custom', df=IFNg_naive_res.xs(['pSTAT3']))
Beispiel #21
0
from ifnclass.ifndata import IfnData
from ifnclass.ifnmodel import IfnModel
from ifnclass.ifnplot import Trajectory, TimecoursePlot, DoseresponsePlot
from numpy import linspace, logspace, log10, nan
import seaborn as sns

if __name__ == '__main__':
    Sagar_data = IfnData("MacParland_Extended")
    #Sagar_data.data_set.drop(labels=[4000, 8000], level=1, inplace=True)
    Mixed_Model = IfnModel('Mixed_IFN_ppCompatible')
    '''
    fit_parameters = OrderedDict(
        [('kd4', 1.0), ('krec_a1', 3.0000000000000001e-05), ('krec_a2', 0.050000000000000003), ('krec_b2', 0.01),
         ('krec_b1', 0.001), ('k_d4', 0.00059999999999999995), ('kSOCSon', 1e-08), ('kd3', 0.001),
         ('k_d3', 2.3999999999999999e-06)])
    Mixed_Model.set_parameters(fit_parameters)
    Mixed_Model.save_model('fitting_2_5.p')
    '''
    #Mixed_Model.load_model('fitting_2_5.p')

    alpha_palette = sns.color_palette("Reds", 6)
    beta_palette = sns.color_palette("Greens", 6)

    # ---------------------------
    # Now try to improve the fit:
    # ---------------------------
    """
    Results of the 5 minute stepwise fit
    OrderedDict([('R2', 500.0), ('kpu', 0.0001), ('R1', 821.42857142857144), ('k_d4', 0.10000000000000001), ('kd4', 0.21642857142857141), ('kd3', 0.00021642857142857143), ('k_d3', 0.00040000000000000002)])
    10.6992020307
    # Previous attempt                 
    # ------------------------------
    newdata_1 = IfnData("20190108_pSTAT1_IFN_Bcell")
    newdata_2 = IfnData("20190119_pSTAT1_IFN_Bcell")
    newdata_3 = IfnData("20190121_pSTAT1_IFN_Bcell")
    newdata_4 = IfnData("20190214_pSTAT1_IFN_Bcell")

    alignment = DataAlignment()
    alignment.add_data([newdata_4, newdata_3, newdata_2, newdata_1])
    alignment.align()
    alignment.get_scaled_data()
    mean_data = alignment.summarize_data()

    # -------------------------------
    # Initialize model
    # -------------------------------
    Mixed_Model = IfnModel('Mixed_IFN_ppCompatible')
    Mixed_Model.set_parameters({'R2': 4920, 'R1': 1200,
                                'k_a1': 2.0e-13, 'k_a2': 1.328e-12, 'k_d3': 1.13e-4, 'k_d4': 0.9,
                                'kSOCSon': 5e-08, 'kpu': 0.0022, 'kpa': 2.36e-06,
                                'ka1': 3.3e-15, 'ka2': 1.85e-12, 'kd4': 2.0,
                                'kd3': 6.52e-05,
                                'kint_a':  0.0015, 'kint_b': 0.002,
                                'krec_a1': 0.01, 'krec_a2': 0.01, 'krec_b1': 0.005, 'krec_b2': 0.05})
    scale_factor = 1.46182313424

    # ------------------------------------------
    # Make model predictions for mixtures of IFN
    # ------------------------------------------
    times = newdata_4.get_times('Alpha')
    doses_alpha = newdata_4.get_doses('Alpha')
    doses_alpha = [0.0] + [round(el, 2) for el in list(np.logspace(np.log10(doses_alpha[0]), np.log10(doses_alpha[-1])))]
Beispiel #23
0
from ifnclass.ifnmodel import IfnModel
from ifnclass.ifndata import IfnData
from ifnclass.ifnplot import DoseresponsePlot, TimecoursePlot

from numpy import linspace, logspace
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

if __name__ == '__main__':
    alpha_palette = sns.color_palette("Reds", 6)
    beta_palette = sns.color_palette("Greens", 6)

    Mixed_Model = IfnModel('Mixed_IFN_ppCompatible')

    # This is the fitting_2_5 script best fit parameters, best at fitting Ratnadeep's B cell data
    Mixed_Model.set_parameters({
        'R2': 2300 * 2.5,
        'R1': 1800 * 1.8,
        'k_d4': 0.06,
        'kint_b': 0.0003,
        'kpu': 0.0028,
        'krec_b1': 0.001,
        'krec_b2': 0.01,
        'k_a1': 4.98E-14,
        'k_a2': 8.30e-13 * 4,
        'kSOCSon': 0.9e-8,
        'ka1': 3.321155762205247e-14 * 0.3,
        'ka2': 4.98173364330787e-13 * 0.3,
        'kd4': 1.0,
        'kd3': 0.001,
Beispiel #24
0
    'kpu': 0.00095,
    'ka2': 4.98e-13 * 2.45,
    'kd4': 0.3 * 2.867,
    'kint_a': 0.00052,
    'kint_b': 0.00052,
    'krec_a1': 0.0028,
    'krec_a2': 0.01,
    'krec_b1': 0.005,
    'krec_b2': 0.05,
    'kSOCSon': 6e-07,
    'krec_a1': 0.001,
    'krec_a2': 0.1,
}
scale_factor = 1.227

IFN_Model = IfnModel('Mixed_IFN_ppCompatible')
IFN_Model.set_parameters(initial_parameters)
IFN_Model.set_parameters({'R1': 12000.0, 'R2': 1511.1})

# ---------------------------------
# Make theory dose response curves
# ---------------------------------
# Make predictions
times = [2.5, 5.0, 7.5, 10.0, 20.0, 60.0]
alpha_doses_20190108 = [0, 10, 100, 300, 1000, 3000, 10000, 100000]
beta_doses_20190108 = [0, 0.2, 6, 20, 60, 200, 600, 2000]

dradf = IFN_Model.doseresponse(times,
                               'TotalpSTAT',
                               'Ia',
                               list(logspace(1, 5.2)),
Beispiel #25
0
from ifnclass.ifndata import IfnData
from ifnclass.ifnmodel import IfnModel
from ifnclass.ifnplot import Trajectory, TimecoursePlot, DoseresponsePlot
from ifnclass.ifnfit import StepwiseFit
from numpy import linspace, logspace, log10, nan
import seaborn as sns
from smooth_B6_IFN import * # Imports smoothed data to fit to
import pickle

if __name__ == '__main__':
    newdata = IfnData("20181113_B6_IFNs_Dose_Response_Bcells")
    Mixed_Model = IfnModel('Mixed_IFN_ppCompatible')
    alpha_palette = sns.color_palette("Reds", 6)
    beta_palette = sns.color_palette("Greens", 6)

    # Stepwise fitting
    # ----------------
    # First fit the 2.5 minute data
    stepfit25 = StepwiseFit(Mixed_Model, smooth5IfnData,
                            {'kpu': (0.001, 0.006), 'kd4': (0.03, 0.1), 'k_d4': (0.03, 0.1),
                             'R1': (3000, 4000), 'R2': (4000, 5000),
                             'ka1': (3.321e-14 * 0.5, 3.321e-14 * 2),
                             'ka2': (4.9817e-13 * 0.5, 4.9817e-13 * 2)}, n=2)
    best_parameters, best_scale_factor = stepfit25.fit()
    print("The fit for 5 minutes was:")
    print(best_parameters)
    print(best_scale_factor)
    scale_data = lambda q: (best_scale_factor*q[0], best_scale_factor*q[1])

    # Generate a figure of this fit
    #   First simulate continuous dose-response curve
Beispiel #26
0
def load_model(model_name='Mixed_IFN_ppCompatible',
               MODEL_TYPE=MODEL_TYPE,
               AFFINITY_SPECIES=AFFINITY_SPECIES):
    # Dose-response method must return a Pandas DataFrame which is compatible
    # with an IfnData object

    if MODEL_TYPE in ['ENSEMBLE', 'BOOTSTRAP']:  # MCMC ensemble
        if AFFINITY_SPECIES != 'HUMAN':
            print(
                "Non-human IFN affinities not implemented for this model type.\n Reverting to human affinities.\n"
            )
        param_file_dir = os.path.join(os.getcwd(), 'pydream', 'GAB',
                                      PYDREAM_DIR)
        param_names = np.load(param_file_dir + os.sep + 'param_names.npy')
        prior_file_name = param_file_dir + os.sep + 'init_params.pkl'

        if MODEL_TYPE == 'ENSEMBLE':  # use MCMC samples
            param_file_name = param_file_dir + os.sep + 'mixed_IFN_samples.npy'
        else:  # use maximum posterior from each MCMC chain
            param_file_name = param_file_dir + os.sep + 'mixed_IFN_ML_samples.txt'

        model = EnsembleModel(model_name, param_file_name, param_names,
                              prior_file_name)
        DR_method = model.posterior_prediction

    else:  # Not MCMC

        if MODEL_TYPE == 'MEDIAN':  # use distribution variables, with * at end of name
            # median parameters from MCMC, but only use variance in R for model variance
            initial_parameters = {
                'kSOCSon': 9.091e-07,
                'kpa': 1.e-06,
                'kint_a': 0.000615,
                'kint_b': 0.0002299,
                'krec_a1': 0.001239,
                'krec_a2': 0.004999,
                'R1_mu*': 1969.703,
                'R1_std*': 0.20,
                'R2_mu*': 2092.941,
                'R2_std*': 0.20
            }
            # if AFFINITY_SPECIES == 'HUMAN':
            #     initial_parameters = {'kSOCSon': 1.03992e-06, 'kpa': 1.e-06,
            #                           'kint_a': 3.737e-05, 'kint_b': 0.0002085,
            #                           'krec_a1': 0.00179, 'krec_a2': 0.00912,
            #                           'R1_mu*': 2000., 'R1_std*': 0.190,
            #                           'R2_mu*': 2023., 'R2_std*': 0.182}
            if AFFINITY_SPECIES == 'MOUSE':
                NAxvolEC = 6.022E23 * 1E-5
                initial_parameters.update({
                    'ka1': 3.75E5 /
                    NAxvolEC,  # 2666 nM Kd -> 3.75E5 M^-1 association rate when kd1=1
                    'ka2': 6.88E6 /
                    NAxvolEC,  # 2.18 nM -> 6.88E6 M^-1 association rate when kd2=0.015
                    'k_a1': 2.368E6 /
                    NAxvolEC,  # 12.67 nM Kd -> 2.368E6 M^-1 association rate when k_d1=0.03
                    'k_a2': 1.195E3 /
                    NAxvolEC,  # 1673 nM -> 1.195E3 M^-1 association rate when kd2=0.002
                    'k_d4': 0.00163,
                    'k_a4': 3.62e-4
                })  # from detailed balance (see SI)
            param_file_dir = os.getcwd()
            param_names = np.array(list(initial_parameters.keys()))
            prior_file_name = param_file_dir + os.sep + 'init_params_temp.pkl'
            param_file_name = param_file_dir + os.sep + 'mixed_IFN_ML_samples_temp.txt'
            # create dummy files to initiate EnsembleModel instance
            with open(prior_file_name, 'wb') as f:
                temp_model = IfnModel(model_name)
                pickle.dump(dict(temp_model.parameters), f)
            print([initial_parameters], file=open(param_file_name, 'w'))
            # initiate model and DR method
            model = EnsembleModel(model_name,
                                  param_file_name,
                                  param_names,
                                  prior_file_name,
                                  num_dist_samples=30)
            DR_method = model.posterior_prediction
            # delete dummy files
            if os.path.isfile(prior_file_name):
                os.remove(prior_file_name)
            if os.path.isfile(param_file_name):
                os.remove(param_file_name)

        elif MODEL_TYPE == 'SINGLE_CELL':  # use stagewise fit params in DualMixedPopulation
            # initial_parameters = {'k_a1': 4.98E-14 * 1.33, 'k_a2': 8.30e-13 * 2,
            #                       'k_d4': 0.006 * 3.8,
            #                       'kpu': 0.00095, 'kSOCSon': 6e-07,
            #                       'ka2': 4.98e-13 * 1.33, 'kd4': 0.3 * 2.867,
            #                       'kint_a': 0.00052, 'kint_b': 0.00052,
            #                       'krec_a1': 0.001, 'krec_a2': 0.1,
            #                       'krec_b1': 0.005, 'krec_b2': 0.05}
            # model = DualMixedPopulation(model_name, 0.8, 0.2)
            # model.model_1.set_parameters(initial_parameters)
            # model.model_1.set_parameters({'R1': 12000.0, 'R2': 1511.1})
            # model.model_2.set_parameters(initial_parameters)
            # model.model_2.set_parameters({'R1': 6755.56, 'R2': 1511.1})
            # DR_method = model.mixed_dose_response
            median_parameters = {
                'kSOCSon': 1.03992e-06,
                'kpa': 1.e-06,
                'kint_a': 3.737e-05,
                'kint_b': 0.0002085,
                'krec_a1': 0.00179,
                'krec_a2': 0.00912,
                #'krec_b1': 8.049335e-05, 'krec_b2': 0.000801,
                'R1': 2000.,
                'R2': 2023.
            }
            # initiate model and DR method
            model = IfnModel(model_name)
            model.set_parameters(median_parameters)
            DR_method = model.doseresponse

        else:
            raise ValueError('Did not recognize model type requested')

    return model, DR_method
Beispiel #27
0
from ifnclass.ifndata import IfnData
from ifnclass.ifnmodel import IfnModel
from ifnclass.ifnplot import Trajectory, TimecoursePlot, DoseresponsePlot
from numpy import linspace, logspace, log10, nan
import seaborn as sns

if __name__ == '__main__':
    alpha_palette = sns.color_palette("Reds", 6)
    beta_palette = sns.color_palette("Greens", 6)

    Mixed_Model = IfnModel('Mixed_IFN_ppCompatible')

    # This is the fitting_2_5 script best fit parameters, best at fitting Ratnadeep's B cell data
    Mixed_Model.set_parameters({
        'R2': 2300 * 2.5,
        'R1': 1800 * 1.8,
        'k_d4': 0.06,
        'kint_b': 0.0003,
        'kpu': 0.0028,
        'krec_b1': 0.001,
        'krec_b2': 0.01,
        'k_a1': 4.98E-14,
        'k_a2': 8.30e-13 * 4,
        'kSOCSon': 0.9e-8,
        'ka1': 3.321155762205247e-14 * 0.3,
        'ka2': 4.98173364330787e-13 * 0.3,
        'kd4': 1.0,
        'kd3': 0.001,
        'kint_a': 0.0014,
        'krec_a1': 9e-03,
        'krec_a2': 0.05
Beispiel #28
0
    newdata_3 = IfnData("20190121_pSTAT1_IFN_Bcell")
    newdata_4 = IfnData("20190214_pSTAT1_IFN_Bcell")

    alignment = DataAlignment()
    alignment.add_data([newdata_4, newdata_3, newdata_2, newdata_1])
    alignment.align()
    alignment.get_scaled_data()
    mean_data = alignment.summarize_data()

    # -------------------------------
    # Initialize model
    # -------------------------------
    times = newdata_4.get_times('Alpha')
    doses_alpha = newdata_4.get_doses('Alpha')
    doses_beta = newdata_4.get_doses('Beta')
    Mixed_Model = IfnModel('Mixed_IFN_ppCompatible')
    Mixed_Model.set_parameters({'R2': 4920, 'R1': 1200,
                                'k_a1': 2.0e-13, 'k_a2': 1.328e-12, 'k_d3': 1.13e-4, 'k_d4': 0.9,
                                'kSOCSon': 5e-08, 'kpu': 0.0022, 'kpa': 2.36e-06,
                                'ka1': 3.3e-15, 'ka2': 1.85e-12, 'kd4': 2.0,
                                'kd3': 6.52e-05,
                                'kint_a': 0.0015, 'kint_b': 0.002,
                                'krec_a1': 0.01, 'krec_a2': 0.01, 'krec_b1': 0.005, 'krec_b2': 0.05})
    scale_factor = 1.46182313424

    # -------------------------------
    # 60 minute IFN dosed at 20 pM
    # -------------------------------
    alpha_cell_size_curve = []
    beta_cell_size_curve = []
    volPM_typical = 2 * 30E-6 ** 2 + 4 * 30E-6 * 8E-6
Beispiel #29
0
    newdata_3 = IfnData("20190121_pSTAT1_IFN_Bcell")
    newdata_4 = IfnData("20190214_pSTAT1_IFN_Bcell")

    alignment = DataAlignment()
    alignment.add_data([newdata_4, newdata_3, newdata_2, newdata_1])
    alignment.align()
    alignment.get_scaled_data()
    mean_data = alignment.summarize_data()

    # -------------------------------
    # Initialize model
    # -------------------------------
    times = newdata_4.get_times('Alpha')
    doses_alpha = newdata_4.get_doses('Alpha')
    doses_beta = newdata_4.get_doses('Beta')
    Mixed_Model = IfnModel('Mixed_IFN_ppCompatible')
    Mixed_Model.set_parameters({
        'R2': 4920,
        'R1': 1200,
        'k_a1': 2.0e-13,
        'k_a2': 1.328e-12,
        'k_d3': 1.13e-4,
        'k_d4': 0.9,
        'kSOCSon': 5e-08,
        'kpu': 0.0022,
        'kpa': 2.36e-06,
        'ka1': 3.3e-15,
        'ka2': 1.85e-12,
        'kd4': 2.0,
        'kd3': 6.52e-05,
        'kint_a': 0.0015,
Beispiel #30
0
from ifnclass.ifndata import IfnData
from ifnclass.ifnmodel import IfnModel
from ifnclass.ifnplot import Trajectory, TimecoursePlot, DoseresponsePlot
from numpy import linspace, logspace, log10, nan
import seaborn as sns
from ifnclass.ifnfit import StepwiseFit

if __name__ == '__main__':
    raw_data = IfnData("20190121_pSTAT1_IFN_Bcell")
    Mixed_Model = IfnModel('Mixed_IFN_ppCompatible')

    alpha_palette = sns.color_palette("Reds", 6)
    beta_palette = sns.color_palette("Greens", 6)

    times = [2.5, 5, 7.5, 10, 20, 60]
    alpha_doses_20190108 = [0, 10, 100, 300, 1000, 3000, 10000, 100000]
    beta_doses_20190108 = [0, 0.2, 6, 20, 60, 200, 600, 2000]

    Mixed_Model.set_parameters({
        'R2': 4140,
        'R1': 4920,
        'k_a1': 2.49e-15,
        'k_a2': 1.328e-12,
        'k_d3': 7.5e-06,
        'k_d4': 0.06,
        'kSOCSon': 5e-08,
        'kpu': 0.0024,
        'kpa': 2.08e-06,
        'ka1': 5.3e-15,
        'ka2': 1.22e-12,
        'kd4': 0.86,