%-------------------------------------------
% Text S6. Function required to generate figure 9.
%-------------------------------------------

%-------------------------------------------
% File name: model_check_osc_winit_full_function_varmdm2.m
% 
% This is function definition file. Please rename this file as above.
%-------------------------------------------

function y=model_check_osc_winit_full_function_varmdm2(x0, params, p53bp, rtime, tp1s);

%-------------------------------------------
% author: Sohyoung Kim
% last updated: Jul-24-2009
% matlab 7.2.0.232 (R2006a) was used.
%% Description: this function simulates early DNA damage response and returns max value of mRNA for rtime.
%               pre-equilibration is performed before DNA damage.
%   
%      y=model_check_osc_winit_full_function_varmdm2(x0, params,p53bp, rtime, tp1s);
%           calls 
%               ode15s('mdmx_p53_mdm2_model_full',[0 500000], x0,options, params)
%           x0: initial condition
%           params: kinetic parameter
%           b53bp: basal p53 production rate
%           rtime: post DNA damage simulation time 
%           tp1s: ex. tp1s=logspace(-5, 1, 50);
%                   range of Mdm2 basal production rate to be varied.
%-------------------------------------------


options=[];
y1s15=[];
x1maxp=[];
x0s=[];
params(1)=p53bp;

for i=1:length(tp1s),
       
        params(6)=tp1s(i); % vary mdm2
        damage=params([3,8,17]);
        params(1)=p53bp/3; % p53 basal expression is three times enhanced after DNA damage.
        params([3,8,17])=0;
        [x1, y1]=ode15s('mdmx_p53_mdm2_model_full',[0 500000], x0, options, params);
        params(1)=p53bp;
        params([3,8,17])=damage;
        [x1, y1]=ode15s('mdmx_p53_mdm2_model_full',[0 rtime], y1(length(x1),:), options, params);
        
        ind_decrease=find((diff(y1(1:length(x1),15)) < 0) & (abs(diff(y1(1:length(x1),15))) > max(y1(:,15))/1000));
        if isempty(ind_decrease),
            [Y,I] =max(y1(:,15));
            x1maxp=[x1maxp; x1(I)];
            y1s15=[y1s15; Y];            
            x0s=[x0s; y1(length(x1),:) y1(I,:)];
        else            
            x1maxp=[x1maxp; x1(min(ind_decrease))];
            y1s15=[y1s15; y1(min(ind_decrease),15)];            
            x0s=[x0s; y1(length(x1),:) y1(min(ind_decrease),:)];            
        end;    
end;    

y=[x1maxp y1s15 x0s];