%template file for running BioNetGen simulations from Matlab

%In order to use the runbng function, you need to add to Matlab path 
%the directories containing the BioNetGen installation.  Make sure you save
%the path before exiting Matlab so that you don't need to add the
%directories again in the future.

%Note, the first time you run the runbng function, a window will pop up and
%ask you to give the location of the file BNG2.pl

%Change the current directory to your RuleBender project directory

nD = 10;

Atypes = zeros(nD,1);

aPoPM = 70*60/115; %average polymerase per minute
aRiPM =.5*30; %average ribosomes per minute
RNAd = .2; %average RNA degradation rate [molecules per minute]
expRa = aPoPM/RNAd*aRiPM; %average expression rate

changePar('CellDivision.bngl',{'I0', 'kIe'},[0 expRa]);
Atype = 0; %total number A types produced by one stem cell
for i = 1:nD
    runbng('','CellDivision.bngl'); %execute the simulation in the subdirectory PAR
    A = importdata('CellDivision.gdat',' ',1); %import the data
    time = A.data(:,1); %rename the time vector
    CChrom = A.data(:,3);
    I = A.data(:,4);
    GL = A.data(:,5);
    ind = find(CChrom==1,1,'first');
    Ie = I(ind);
    GLe = GL(ind);
    if GLe == 2
        Atype = Atype + 2;
        break
    else
        Atype = Atype + GLe;
        Ie
        changePar('CellDivision.bngl',{'I0'},Ie);
    end
    GLe
    pause
    close all
end

