(* for "Multiple nutrient transporters enable cells to mitigate a rate-affinity tradeoff"
by Montano-Gutierrez et al. Contact peter.swain@ed.ac.uk *)

(* ODEs for four states of transporter *)
dTi=-ri Ti+re Te -fi gi Ti+bi Tpi;
dTpi=-rpi Tpi+rpe Tpe+fi gi Ti-bi Tpi;
dTe=-re Te+ri Ti-fe ge Te+be Tpe;
dS=Simplify[Solve[{dTi==0,dTpi==0,dTe==0},{Tpi,Ti,Te}]];
tpe=Tpe/.Simplify[Solve[((t0-Tpi-Ti-Te-Tpe)/.dS[[1]])==0,Tpe]][[1]];
exflux=Simplify[((-fi Ti gi+bi Tpi)/.dS[[1]])/.Tpe->tpe]

(* flux is a monotonic increasing function ri and rpe and a decreasing function of re and rpi;  *)
Simplify[D[exflux,ri]]
Simplify[D[exflux,re]]
Simplify[D[exflux,rpi]]
Simplify[D[exflux,rpe]]

Simplify[(Simplify[exflux/t0/.{ri->1/e,rpe->1/e,rpi->0,re->0}]/.e->0) ]

(* apply symmetry of flipping 
and detailed balance: fe bi rpe ri = be rpi fi re *)
ef=Simplify[exflux/.{re->r,ri->r,rpe->rp,rpi->rp,bi->be/fe fi  }]

(* limit of large ge when gi = c ge *)Simplify[Normal[Series[Simplify[ef/. gi ->c ge]/.ge->1/e,{e,0,1}]]/.e->1/ge/.c->gi/ge]

(* value of c at maximal flux when gi =c ge *)efmaxsol=Simplify[Solve[Simplify[D[Simplify[ef/.gi->c ge],ge]]==0,c]/.{rp->r,be->kd fe}]

(* flux = kc dg/(km + dg) *)
ef2=Simplify[ef/.ge->dg +gi];
den=Denominator[ef2];
kc=Simplify[Numerator[ef2/dg/t0]/Coefficient[den,dg,1]]
km=Simplify[Coefficient[den,dg,0]/Coefficient[den,dg,1]]
Simplify[kc dg t0/(km +dg) - ef2]

(* tradeoff with fe *)
Simplify[D[kc,fe]]
Simplify[D[1/km,fe]]

(* and derivative of flux can change sign *)
Simplify[D[ef2,fe]]

(* possible tradeoff with fi *)
Simplify[D[kc,fi]]
Simplify[D[1/km,fi]]

(* and derivative of flux cannot change sign *)
Simplify[D[ef2,fi]]

(* tradeoff with r *)
Simplify[D[kc,r]]
Simplify[D[1/km,r]]

(* and derivative of flux cannot change sign *)
Simplify[D[ef2,r]]

(* possible tradeoff with rp *)
Simplify[D[kc,rp]]
Simplify[D[1/km,rp]]

(* and derivative of flux cannot change sign *)
Simplify[D[ef2,rp]]

(* possible tradeoff with be  *)
Simplify[D[kc,be]]
Simplify[D[1/km,be]]

(* and derivative of flux cannot change sign *)
Simplify[D[ef2,be]]

(* ************************************************* *)

(* let r= rp  *)
kcs=Simplify[kc/.{rp->r}]
kms=Simplify[km/.{rp->r}]
efs=Simplify[ef2/.rp->r]
Simplify[kcs dg t0/(kms +dg) - efs]

(* tradeoff with fe  *)
Simplify[D[kcs,fe]]
Simplify[D[1/kms,fe]]

(* and derivative of flux can change sign *)
Simplify[D[efs,fe]]

(* tradeoff with fi *)
Simplify[D[kcs,fi]]
Simplify[D[1/kms,fi]]

(* but derivative of flux cannot change sign ; fi maximal ? *)
Simplify[D[efs,fi]]

(* tradeoff with be *)
Simplify[D[kcs,be]]
Simplify[D[1/kms,be]]

(* and derivative of flux can change sign *)
Simplify[D[efs,be]]

(* tradeoff with r *)
Simplify[D[kcs,r]]
Simplify[D[1/kms,r]]

(* but *)
Simplify[D[efs,r]]

(* km in terms of kc *)
Simplify[Simplify[kms/.Solve[kcc==kcs,fi][[1]]]]

(* max and min values of Km *)

Simplify[kms/.r->0]
Simplify[kms/.r->1/e]/.e->0

Simplify[kms/.fi->0]
Simplify[kms/.fi->1/e]/.e->0