clf
clear all

%=============================================
%build the GUI
%define the plot button
plotbutton=uicontrol('style','pushbutton',...
   'string','Run', ...
   'fontsize',12, ...
   'position',[100,400,50,20], ...
   'callback', 'run=1;');

%define the stop button
erasebutton=uicontrol('style','pushbutton',...
   'string','Stop', ...
   'fontsize',12, ...
   'position',[200,400,50,20], ...
   'callback','freeze=1;');

%define the Quit button
quitbutton=uicontrol('style','pushbutton',...
   'string','Quit', ...
   'fontsize',12, ...
   'position',[300,400,50,20], ...
   'callback','stop=1;close;');

number = uicontrol('style','text', ...
    'string','1', ...
   'fontsize',12, ...
   'position',[20,400,50,20]);
    
% Number of cells in the grid

n=15;

%initialize the arrays
z = zeros(n,n);


cellsNotch = z;  % array for Notch values for each cell
cellsDelta = z;  % array for Delta values for each cell

% Set random initial conditions for Notch and Delta values 

% Generate values from the uniform distribution on the interval [a,b] to 
% generate a random initial condition

a = 0.95; 
b = 1;

cellsNotch = a + (b-a).*rand(n,n);
cellsDelta = a + (b-a).*rand(n,n);

%Specify boundary conditions for delta activity 

% % Figure 6, first boundary condition
% cellsDelta(1,1) = 0;
% cellsDelta(1,n) = 0;
% cellsDelta(n,n) = 0;
% cellsDelta(n,1) = 0;


notchActThresh = 0.995;  % Notch activity threshold above which the cell has high Notch activity

gridCells = z;  % these are the states of cells on the display 
                % red = high Notch value and black is low Notch activity 

%Initialize the grid cells corresponding to Notch activity in cellsNotch 

gridCells = (cellsNotch >= notchActThresh);

%build an image and display it
imh = image(cat(3,gridCells,z,z));
set(imh, 'erasemode', 'none')
axis equal
axis tight


%Main event loop
stop= 0; %wait for a quit button push
run = 0; %wait for a draw 
freeze = 0; %wait for a freeze

% Assume there are two cell fates - primary(low Notch activity) and 
% secondary (high Notch activity)
% Assume that each cell has 8 neighboring cells 


while (stop==0) 
    
    if (run==1)
       
        cellsNotchNext = cellsNotch;
        cellsDeltaNext = cellsDelta;
        

        % Calculate the mean of delta activity (deltaMean) for the cell's 8 neighboring cells
        
        for (x=2:n-1)
            for (y=2:n-1)
                deltaMean(x,y) = % write expression for mean delta values for neighboring cells            
                initCond = [cellsNotch(x,y);cellsDelta(x,y)];
		
		% Write your own notchDeltaDynamics file with the system ODEs
                [t,out] = ode15s(@notchDeltaDynamics,[0 1], initCond,[],deltaMean(x,y));
                
                cellsNotchNext(x,y) = out(length(out),1);
                cellsDeltaNext(x,y) = out(length(out),2);          
                             
            end
        end
        
        % Now do something with the grid, the grid represents the Notch
        % state in either primary (low Notch activity) or secondary (high
        % Notch activity)
   
        % The grid rule
        
        cellsNotch = cellsNotchNext;
        cellsDelta = cellsDeltaNext;
        
        gridCells = (cellsNotchNext >= notchActThresh);    
        
        % Draw the new image
        set(imh, 'cdata', cat(3,gridCells,z,z) )
        
        % Update the step number display
        stepnumber = 1 + str2num(get(number,'string'));
        set(number,'string',num2str(stepnumber))
        
        
    end
    
    if (freeze==1)
        run = 0;
        freeze = 0;
    end
    
    drawnow  %need this in the loop for controls to work
    
end

  
    
