%% This MATLAB code is for simulating the correlation coefficient's dependence on the numbers of coupling factors and phase-specific factors by number
% The code simulate the correlation coefficients between two cell cycle
% phase durations (G1 and S, for example) and how it depends on the number
% of factors that have shared control over the two phases, and on the
% number of factors that have a phase-spefic effect
%% 
clear all
cell_num=200; % number of cells for each simulation for obtaining correlation coefficient

figure
for IspeciN=1:10
    specific_mol_type=2^IspeciN; % number of phase-specific molecule types
    for k=1:10
        n_mol_type=2^k; % number of phase-coupling molecule types
        for Irepeat=1:200 % number of iterations for each condition 
        
            a_g1=normrnd(0,0.01,1,n_mol_type); % The effect coefficient of each phase-coupling factor on the duration of G1 is randomly drawn from a normal distribution
            a_s=normrnd(0,0.01,1,n_mol_type); % The effect coefficient of each phase-coupling factor on the duration of S is randomly drawn from a normal distribution
            a_g1_specific=normrnd(0,0.01,1,specific_mol_type); % The effect coefficient of each phase-specific factor on the duration of G1 is randomly drawn from a normal distribution
            a_s_specific=normrnd(0,0.01,1,specific_mol_type); % The effect coefficient of each phase-specific factor on the duration of S is randomly drawn from a normal distribution
    
            mol_type_mean= lognrnd(log(10^3),0.6,1,n_mol_type); % The average number of each phase-coupling molecule type is randomly drawn from a lognormal distribution
            mol_type_mean_g1_spec=lognrnd(log(10^3),0.6,1,specific_mol_type); % The average number of each G1 phase-specific molecule type is randomly drawn from a lognormal distribution
            mol_type_mean_s_spec=lognrnd(log(10^3),0.6,1,specific_mol_type); % The average number of each S phase-specific molecule type is randomly drawn from a lognormal distribution
    
            % randomly generate the number of molecules for each type
            num_mol=[];
            for i=1:n_mol_type
                num_mol(:,i)=poissrnd(mol_type_mean(i),cell_num,1); % The number of each phase-coupling molecule is randomly drawn from a poisson distribution
            end
            num_mol_g1_spec=[];
            num_mol_s_spec=[];
            for i=1:specific_mol_type
                num_mol_g1_spec(:,i)=poissrnd(mol_type_mean_g1_spec(i),cell_num,1); % The number of each G1 phase-specific molecule is randomly drawn from a poisson distribution
                num_mol_s_spec(:,i)=poissrnd(mol_type_mean_s_spec(i),cell_num,1); % The number of each S phase-specific molecule is randomly drawn from a poisson distribution
            end

            % Determine the duration of G1 and S
            for j=1:cell_num
                G1_length(j)=9/(1/0.875+0.00001*(sum(a_g1.*num_mol(j,:)) +sum(a_g1_specific.*num_mol_g1_spec(j,:)))); % The duration of G1 is calculated from the Erlang parameters (k=9, lambda=1/0.875), modulated by the effects of the factors' number
                S_length(j)=80/(1/0.0946+0.001*(sum(a_s.*num_mol(j,:)) +sum(a_s_specific.*num_mol_s_spec(j,:)))); % The duration of S is calculated from the Erlang parameters (k=80, lambda=1/0.0946), modulated by the effects of the factors' number
            end
            [r,p] = corrcoef(G1_length,S_length); % Calculating the correlation coefficient based on the 200 cells

            r_repeat(Irepeat)=r(1,2);
            p_repeat(Irepeat)=p(1,2);
        end
        r_prob(IspeciN, k)=mean(abs(r_repeat));
        p_prob(IspeciN,k)=sum(p_repeat<0.05)./numel(p_repeat);
     
    end
end

figure,
imagesc(r_prob)
xlabel('Coupling factor type #')
ylabel('Specific factor type #')
xticklabels = 2.^(1:1:10);
xticks = linspace(1, 10, numel(xticklabels));
set(gca, 'XTick', xticks, 'XTickLabel', xticklabels)
yticklabels = 2.^(1:1:10);
yticks = linspace(1, 10, numel(yticklabels));
set(gca, 'YTick', yticks, 'YTickLabel', yticklabels)
h = colorbar;
ylabel(h, 'Mean |R|')


%% Simulate R dependence on molecule type number and specific factors by number-- with CDK2 inhibitor
cell_num=200; % number of cells for each simulation for obtaining correlation coefficient


for IspeciN=1:10
   
    specific_mol_type=2^IspeciN; % number of phase-specific molecule types
    
    for k=1:10
        n_mol_type=2^k; % number of phase-coupling molecule types
        for Irepeat=1:200 % number of iterations for each condition 
        
            a_g1=normrnd(0,0.01,1,n_mol_type); % The effect coefficient of each phase-coupling factor on the duration of G1 is randomly drawn from a normal distribution
            a_s=normrnd(0,0.01,1,n_mol_type); % The effect coefficient of each phase-coupling factor on the duration of S is randomly drawn from a normal distribution
            a_g1_specific=normrnd(0,0.01,1,specific_mol_type); % The effect coefficient of each phase-specific factor on the duration of G1 is randomly drawn from a normal distribution
            a_s_specific=normrnd(0,0.01,1,specific_mol_type); % The effect coefficient of each phase-specific factor on the duration of S is randomly drawn from a normal distribution
    
            mol_type_mean= lognrnd(log(10^3),0.6,1,n_mol_type); % The average number of each phase-coupling molecule type is randomly drawn from a lognormal distribution
            mol_type_mean_g1_spec=lognrnd(log(10^3),0.6,1,specific_mol_type); % The average number of each G1 phase-specific molecule type is randomly drawn from a lognormal distribution
            mol_type_mean_s_spec=lognrnd(log(10^3),0.6,1,specific_mol_type); % The average number of each S phase-specific molecule type is randomly drawn from a lognormal distribution
          
            % randomly generate the number of molecules for each type
            num_mol=[];
            for i=1:n_mol_type
                num_mol(:,i)=poissrnd(mol_type_mean(i),cell_num,1); % The number of each phase-coupling molecule is randomly drawn from a poisson distribution
            end
            num_mol_g1_spec=[];
            num_mol_s_spec=[];
            for i=1:specific_mol_type
                num_mol_g1_spec(:,i)=poissrnd(mol_type_mean_g1_spec(i),cell_num,1); % The number of each G1 phase-specific molecule is randomly drawn from a poisson distribution
                num_mol_s_spec(:,i)=poissrnd(mol_type_mean_s_spec(i),cell_num,1); % The number of each S phase-specific molecule is randomly drawn from a poisson distribution
            end
    

            % CDK2 inhibitor's effect
            CDK2_id=find(max(a_g1 .* a_s)); % Finding a factor that has strong effects on both phases

            num_mol_inhibited=num_mol; % Copying the number of molecules in the unperturbed state
            num_mol_inhibited(:,CDK2_id) = num_mol(:,CDK2_id )*10; % Increasing the number of that strong factor by 10 


            for j=1:cell_num
                G1_length(j)=9/(1/0.875+0.0001*(sum(a_g1.*num_mol_inhibited(j,:)) +sum(a_g1_specific.*num_mol_g1_spec(j,:)))); % The duration of G1 is calculated from the Erlang parameters (k=9, lambda=1/0.875), modulated by the effects of the factors' number
                S_length(j)=80/(1/0.0946+0.001*(sum(a_s.*num_mol_inhibited(j,:)) +sum(a_s_specific.*num_mol_s_spec(j,:)))); % The duration of S is calculated from the Erlang parameters (k=9, lambda=1/0.875), modulated by the effects of the factors' number
            end
            
            [r,p] = corrcoef(G1_length,S_length); % Calculating the correlation coefficient based on the 200 cells

            r_repeat(Irepeat)=r(1,2);
            p_repeat(Irepeat)=p(1,2);
        end
        r_prob(IspeciN, k)=mean(abs(r_repeat));
        p_prob(IspeciN,k)=sum(p_repeat<0.05)./numel(p_repeat);
     
    end
end

figure,
imagesc(r_prob)
xlabel('Coupling factor type #')
ylabel('Specific factor type #')
xticklabels = 2.^(1:1:10);
xticks = linspace(1, 10, numel(xticklabels));
set(gca, 'XTick', xticks, 'XTickLabel', xticklabels)
yticklabels = 2.^(1:1:10);
yticks = linspace(1, 10, numel(yticklabels));
set(gca, 'YTick', yticks, 'YTickLabel', yticklabels)
h = colorbar;
ylabel(h, 'Mean |R|')
