% ODE model for use with MATLAB solvers such as ode15s 
%
% call by anonymous function handle @(t, c)(MHB_ODE(c, Hill, tau))
%
% in each compartment the species are ordered as follows: otx, gbx, fgf,
% wnt, en, pax, the species fgf^ext and wnt^ext are  handled implicitly
% Hill: 2x14 matrix containing the Hill parameters for all 14 interactions, for paper results set Hill(1,:)=5, Hill(2,:)=0.1
% tau: life-times of all 6 species, for paper results set to ones(6,1)
%
% dominik.wittmann@helmholtz-muenchen.de, 2009


function xdot = MHB_ODE(c, Hill, tau) 


xdot=zeros(36, 1); 

% 1. Compartment 
xdot(1) = ( negreg(c(2), Hill(1, 1), Hill(2, 1))-c(1))/tau(1); 
xdot(2) = ( negreg(c(1), Hill(1, 2), Hill(2, 2))-c(2))/tau(2); 
xdot(3) = ( negreg(c(1), Hill(1, 3), Hill(2, 3))*posreg(c(2), Hill(1, 4), Hill(2, 4))*posreg((c(4)+c(10))/2, Hill(1, 5), Hill(2, 5))*posreg(c(5), Hill(1, 9), Hill(2, 9))*posreg(c(6), Hill(1, 10), Hill(2, 10))-c(3))/tau(3); 
xdot(4) = ( posreg(c(1), Hill(1, 6), Hill(2, 6))*negreg(c(2), Hill(1, 7), Hill(2, 7))*posreg((c(3)+c(9))/2, Hill(1, 8), Hill(2, 8))-c(4))/tau(4); 
xdot(5) = ( (posreg((c(3)+c(9))/2, Hill(1, 11), Hill(2, 11))+posreg((c(4)+c(10))/2, Hill(1, 12), Hill(2,12))-posreg((c(4)+c(10))/2, Hill(1, 12), Hill(2,12))*posreg((c(3)+c(9))/2, Hill(1, 11), Hill(2, 11)))*posreg(c(6), Hill(1, 13), Hill(2, 13))-c(5))/tau(5);
xdot(6) = ( posreg(c(5), Hill(1, 14), Hill(2, 14))-c(6))/tau(6);

% 2. Compartment 
xdot(7) = ( negreg(c(8), Hill(1, 1), Hill(2, 1))-c(7))/tau(1); 
xdot(8) = ( negreg(c(7), Hill(1, 2), Hill(2, 2))-c(8))/tau(2); 
xdot(9) = ( negreg(c(7), Hill(1, 3), Hill(2, 3))*posreg(c(8), Hill(1, 4), Hill(2, 4))*posreg((c(4)+c(10)+c(16))/3, Hill(1, 5), Hill(2, 5))*posreg(c(11), Hill(1, 9), Hill(2, 9))*posreg(c(12), Hill(1, 10), Hill(2, 10))-c(9))/tau(3); 
xdot(10) = ( posreg(c(7), Hill(1, 6), Hill(2, 6))*negreg(c(8), Hill(1, 7), Hill(2, 7))*posreg((c(3)+c(9)+c(15))/3, Hill(1, 8), Hill(2, 8))-c(10))/tau(4); 
xdot(11) = ( (posreg((c(3)+c(9)+c(15))/3, Hill(1, 11), Hill(2, 11))+posreg((c(4)+c(10)+c(16))/3, Hill(1, 12), Hill(2,12))-posreg((c(4)+c(10)+c(16))/3, Hill(1, 12), Hill(2,12))*posreg((c(3)+c(9)+c(15))/3, Hill(1, 11), Hill(2, 11)))*posreg(c(12), Hill(1, 13), Hill(2, 13))-c(11))/tau(5);
xdot(12) = ( posreg(c(11), Hill(1, 14), Hill(2, 14))-c(12))/tau(6);

% 3. Compartment 
xdot(13) = ( negreg(c(14), Hill(1, 1), Hill(2, 1))-c(13))/tau(1); 
xdot(14) = ( negreg(c(13), Hill(1, 2), Hill(2, 2))-c(14))/tau(2); 
xdot(15) = ( negreg(c(13), Hill(1, 3), Hill(2, 3))*posreg(c(14), Hill(1, 4), Hill(2, 4))*posreg((c(10)+c(16)+c(22))/3, Hill(1, 5), Hill(2, 5))*posreg(c(17), Hill(1, 9), Hill(2, 9))*posreg(c(18), Hill(1, 10), Hill(2, 10))-c(15))/tau(3); 
xdot(16) = ( posreg(c(13), Hill(1, 6), Hill(2, 6))*negreg(c(14), Hill(1, 7), Hill(2, 7))*posreg((c(9)+c(15)+c(21))/3, Hill(1, 8), Hill(2, 8))-c(16))/tau(4); 
xdot(17) = ( (posreg((c(9)+c(15)+c(21))/3, Hill(1, 11), Hill(2, 11))+posreg((c(10)+c(16)+c(22))/3, Hill(1, 12), Hill(2,12))-posreg((c(10)+c(16)+c(22))/3, Hill(1, 12), Hill(2,12))*posreg((c(9)+c(15)+c(21))/3, Hill(1, 11), Hill(2, 11)))*posreg(c(18), Hill(1, 13), Hill(2, 13))-c(17))/tau(5);
xdot(18) = ( posreg(c(17), Hill(1, 14), Hill(2, 14))-c(18))/tau(6);

% 4. Compartment 
xdot(19) = ( negreg(c(20), Hill(1, 1), Hill(2, 1))-c(19))/tau(1); 
xdot(20) = ( negreg(c(19), Hill(1, 2), Hill(2, 2))-c(20))/tau(2); 
xdot(21) = ( negreg(c(19), Hill(1, 3), Hill(2, 3))*posreg(c(20), Hill(1, 4), Hill(2, 4))*posreg((c(16)+c(22)+c(28))/3, Hill(1, 5), Hill(2, 5))*posreg(c(23), Hill(1, 9), Hill(2, 9))*posreg(c(24), Hill(1, 10), Hill(2, 10))-c(21))/tau(3); 
xdot(22) = ( posreg(c(19), Hill(1, 6), Hill(2, 6))*negreg(c(20), Hill(1, 7), Hill(2, 7))*posreg((c(15)+c(21)+c(27))/3, Hill(1, 8), Hill(2, 8))-c(22))/tau(4); 
xdot(23) = ( (posreg((c(15)+c(21)+c(27))/3, Hill(1, 11), Hill(2, 11))+posreg((c(16)+c(22)+c(28))/3, Hill(1, 12), Hill(2,12))-posreg((c(16)+c(22)+c(28))/3, Hill(1, 12), Hill(2,12))*posreg((c(15)+c(21)+c(27))/3, Hill(1, 11), Hill(2, 11)))*posreg(c(24), Hill(1, 13), Hill(2, 13))-c(23))/tau(5);
xdot(24) = ( posreg(c(23), Hill(1, 14), Hill(2, 14))-c(24))/tau(6);

% 5. Compartment 
xdot(25) = ( negreg(c(26), Hill(1, 1), Hill(2, 1))-c(25))/tau(1); 
xdot(26) = ( negreg(c(25), Hill(1, 2), Hill(2, 2))-c(26))/tau(2); 
xdot(27) = ( negreg(c(25), Hill(1, 3), Hill(2, 3))*posreg(c(26), Hill(1, 4), Hill(2, 4))*posreg((c(22)+c(28)+c(34))/3, Hill(1, 5), Hill(2, 5))*posreg(c(29), Hill(1, 9), Hill(2, 9))*posreg(c(30), Hill(1, 10), Hill(2, 10))-c(27))/tau(3); 
xdot(28) = ( posreg(c(25), Hill(1, 6), Hill(2, 6))*negreg(c(26), Hill(1, 7), Hill(2, 7))*posreg((c(21)+c(27)+c(33))/3, Hill(1, 8), Hill(2, 8))-c(28))/tau(4); 
xdot(29) = ( (posreg((c(21)+c(27)+c(33))/3, Hill(1, 11), Hill(2, 11))+posreg((c(22)+c(28)+c(34))/3, Hill(1, 12), Hill(2,12))-posreg((c(22)+c(28)+c(34))/3, Hill(1, 12), Hill(2,12))*posreg((c(21)+c(27)+c(33))/3, Hill(1, 11), Hill(2, 11)))*posreg(c(30), Hill(1, 13), Hill(2, 13))-c(29))/tau(5);
xdot(30) = ( posreg(c(29), Hill(1, 14), Hill(2, 14))-c(30))/tau(6);

% 6. Compartment 
xdot(31) = ( negreg(c(32), Hill(1, 1), Hill(2, 1))-c(31))/tau(1); 
xdot(32) = ( negreg(c(31), Hill(1, 2), Hill(2, 2))-c(32))/tau(2); 
xdot(33) = ( negreg(c(31), Hill(1, 3), Hill(2, 3))*posreg(c(32), Hill(1, 4), Hill(2, 4))*posreg((c(28)+c(34))/2, Hill(1, 5), Hill(2, 5))*posreg(c(35), Hill(1, 9), Hill(2, 9))*posreg(c(36), Hill(1, 10), Hill(2, 10))-c(33))/tau(3); 
xdot(34) = ( posreg(c(31), Hill(1, 6), Hill(2, 6))*negreg(c(32), Hill(1, 7), Hill(2, 7))*posreg((c(27)+c(33))/2, Hill(1, 8), Hill(2, 8))-c(34))/tau(4); 
xdot(35) = ( (posreg((c(27)+c(33))/2, Hill(1, 11), Hill(2, 11))+posreg((c(28)+c(34))/2, Hill(1, 12), Hill(2,12))-posreg((c(28)+c(34))/2, Hill(1, 12), Hill(2,12))*posreg((c(27)+c(33))/2, Hill(1, 11), Hill(2, 11)))*posreg(c(36), Hill(1, 13), Hill(2, 13))-c(35))/tau(5);
xdot(36) = ( posreg(c(35), Hill(1, 14), Hill(2, 14))-c(36))/tau(6);

end


function ant=posreg(c, n, k) % positive Hill function
   ant=c^n/(c^n+k^n); 
end 

function ant=negreg(c, n, k) % negative Hill function
   ant=k^n/(c^n+k^n); 
end