Skip to content

Commit c90d95a

Browse files
committed
Implemented new demographic noise, by simple multiplication of the
exchange fluxes with the noisy biomass. ID 12/06/2022
1 parent 382bf86 commit c90d95a

1 file changed

Lines changed: 223 additions & 4 deletions

File tree

  • comets_simplified/src/edu/bu/segrelab/comets/fba

comets_simplified/src/edu/bu/segrelab/comets/fba/FBACell.java

Lines changed: 223 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class FBACell extends edu.bu.segrelab.comets.Cell
4747
private final int id;
4848
private int cellColor;
4949
private double[] biomass;
50+
private double[] old_biomass;
5051
private double[] convectionRHS1;
5152
private double[] convectionRHS2;
5253
private double jointRHS1;
@@ -675,7 +676,8 @@ public synchronized int run(FBAModel[] models)
675676
allModelsGrowthRates = new double[models.length];
676677

677678
deltaMedia = new double[models.length][]; //DJORDJE
678-
679+
old_biomass = new double[models.length];
680+
679681
FBAstatus = new int[models.length];
680682

681683
double biomassGrowthRate = 0.0;
@@ -906,6 +908,7 @@ else if (cParams.getNumLayers() > 1)
906908
*/
907909

908910
// System.out.print("flux");
911+
909912
/*
910913
for (int j=0; j<mediaDelta.length; j++)
911914
{
@@ -920,6 +923,8 @@ else if (cParams.getNumLayers() > 1)
920923
allModelsGrowthRates[i]=biomassGrowthRate;
921924
deltaBiomass[i] *= (1-(double)(((FBAModel)models[i]).getGenomeCost()));
922925

926+
927+
old_biomass[i]=biomass[i];
923928
biomass[i]+=deltaBiomass[i];
924929
double oldBiomass=biomass[i];
925930

@@ -930,6 +935,7 @@ else if (cParams.getNumLayers() > 1)
930935
}
931936
deltaMedia[i] = mediaDelta;
932937

938+
933939
//Neutral drift block. Only if the death rate is zero. Get the sigmas from the model and
934940
// calculate biomass=(sigma^2*timestep/2)*Gamm(Poiss(2*biomass/sigma^2*timesteo))
935941
if(fbaModels[i].getNeutralDrift() && deltaBiomass[i]>0.0 && cParams.getDeathRate()==0.0)
@@ -940,13 +946,14 @@ else if (cParams.getNumLayers() > 1)
940946

941947
for (int j=0; j<mediaDelta.length; j++)
942948
{
943-
if(exchFlux[j]<0.0 && biomass[i]>oldBiomass)mediaDelta[j] = (double)exchFlux[j] * biomass[i] * cParams.getTimeStep();
944-
if(exchFlux[j]>0.0 && biomass[i]<oldBiomass)mediaDelta[j] = (double)exchFlux[j] * biomass[i] * cParams.getTimeStep();
949+
//if(exchFlux[j]<0.0 && biomass[i]>oldBiomass)mediaDelta[j] = (double)exchFlux[j] * biomass[i] * cParams.getTimeStep();
950+
//if(exchFlux[j]>0.0 && biomass[i]<oldBiomass)
951+
mediaDelta[j] = (double)exchFlux[j] * biomass[i] * cParams.getTimeStep();
945952

946953
}
947954
deltaMedia[i] = mediaDelta;
948955

949-
}
956+
}
950957
else if(fbaModels[i].getNeutralDrift() && cParams.getDeathRate()!=0.0)
951958
{
952959
System.out.println("Error in model "+i+": Demographic noise is applies only if the death rate for the model is zero. Noise will not be applied.");
@@ -962,6 +969,218 @@ else if(fbaModels[i].getNeutralDrift() && cParams.getDeathRate()!=0.0)
962969
}
963970

964971

972+
if (stationaryStatus == false)
973+
{
974+
/* If there are models with positive growth (i.e. stationaryStatus=false)
975+
* get uptake for every model and media component, and compute the
976+
* amount remaining after each model takes whatever needed, given
977+
* the concentration
978+
*/
979+
double[][] uptakeMat = world.simulateCellUpdateMedia(x, y, models, deltaMedia);
980+
boolean reOptimizeFlag = false;
981+
double[] thisCellMedia = world.getMediaAt(x, y); // all media in cell
982+
double[] totalUptakes = new double[thisCellMedia.length];
983+
984+
// loop over all external metabolites (media components) present in the current cell
985+
for (int k=0; k<thisCellMedia.length; k++) {
986+
987+
// what is the total uptake for current metabolite?
988+
double totUptake = 0;
989+
990+
// what models are uptaking it?
991+
ArrayList<Integer> uptakingModels = new ArrayList<Integer>();
992+
993+
for (int l=0; l<models.length; l++)
994+
{
995+
totUptake += uptakeMat[l][k];
996+
if (uptakeMat[l][k] < 0)
997+
{
998+
uptakingModels.add(l);
999+
}
1000+
}
1001+
1002+
if (totUptake<0 && totUptake<(-thisCellMedia[k])) // if current metabolite isrunning out
1003+
{
1004+
reOptimizeFlag = true;
1005+
for (Integer l : uptakingModels) // for all models uptaking it
1006+
{
1007+
// Calculate new uptake by multiplying it by the fraction of the total
1008+
double newUptake = thisCellMedia[k] * (uptakeMat[l][k]/totUptake);
1009+
1010+
// Figure out the index of the metabolite in the lb vector
1011+
int[] modelMediaIndexes = world.getModelMediaIndexes(x, y, l);
1012+
int kIndexInModel = ArrayUtils.indexOf(modelMediaIndexes, k);
1013+
1014+
// update the lb
1015+
lb[l][kIndexInModel] = -newUptake / (biomass[l] * cParams.getTimeStep());
1016+
}
1017+
}
1018+
}
1019+
1020+
/*
1021+
* If any compound has run out, lower bounds for all models were fixed above, and now we
1022+
* need to re-run everything and update media and biomasses.
1023+
*/
1024+
1025+
if (reOptimizeFlag == true)
1026+
{
1027+
for (int a=0; a<models.length; a++)
1028+
{
1029+
int i = a;
1030+
1031+
// if no biomass, or the total biomass has overflowed, skip to the next.
1032+
if (biomass[i] == 0 || Utility.sum(biomass) >= cParams.getMaxSpaceBiomass())
1033+
{
1034+
continue;
1035+
}
1036+
//try to activate, if not active skip to next.
1037+
if(cParams.getSimulateActivation() && !((FBAModel)models[i]).activate(cParams.getActivateRate()))
1038+
{
1039+
continue;
1040+
}
1041+
1042+
// if in stationary phase do not bother with the optimisation.
1043+
if (stationaryStatus == true){
1044+
continue;
1045+
1046+
}
1047+
1048+
/* Here we skip calculating exchange fluxes, because it is already done. Now we just
1049+
* need to update the exchange reaction bounds (some of which were changed) in the model.
1050+
*/
1051+
1052+
((FBAModel)models[i]).setExchLowerBounds(lb[i]); // here is where the new bounds are set
1053+
1054+
/************************* SET MAX BIOMASS *****************************/
1055+
//only set if the upper bound due to space constraints is lower than the default UB
1056+
double bioub = (cParams.getMaxSpaceBiomass() - (Utility.sum(biomass) + Utility.sum(deltaBiomass))) / (biomass[i] * cParams.getTimeStep());
1057+
double basebioub = ((FBAModel)models[i]).getUpperBounds()[((FBAModel)models[i]).getBiomassReaction() - 1];
1058+
((FBAModel)models[i]).setBiomassUpperBound(Math.min(basebioub, bioub));
1059+
1060+
if (DEBUG)
1061+
{
1062+
System.out.println("ALL FLUX BOUNDS");
1063+
lb[i] = ((FBAModel)models[i]).getLowerBounds();
1064+
ub[i] = ((FBAModel)models[i]).getUpperBounds();
1065+
for (int j=0; j<lb[i].length; j++)
1066+
{
1067+
System.out.println(lb[i][j] + "\t" + ub[i][j]);
1068+
}
1069+
}
1070+
1071+
/*************************** RUN THE FBA! ****************************/
1072+
int stat = models[i].run();
1073+
fluxes[i] = ((FBAModel)models[i]).getFluxes();
1074+
1075+
if (stat != 5 && stat != 180)
1076+
{
1077+
// failure! don't do anything right now.
1078+
// System.out.println("FBA failure status: " + stat);
1079+
//error check for JEAN (again may be redundant in later versions).
1080+
deltaBiomass[i]=0.0;
1081+
1082+
}
1083+
if (stat == 5 || stat == 180)
1084+
{
1085+
// We have a valid solution, so update this cell and the world.
1086+
1087+
/***************** GET MEDIA CONCENTRATION CHANGE ********************/
1088+
double[] exchFlux = ((FBAModel)models[i]).getExchangeFluxes();
1089+
allExchFluxes[i] = exchFlux;
1090+
1091+
double[] mediaDelta = new double[exchFlux.length];
1092+
1093+
/* modify the media (in mmol) by changing the fluxes back
1094+
* into concentrations
1095+
* delta = v * biomass * time_step
1096+
*/
1097+
1098+
// System.out.print("flux");
1099+
for (int j=0; j<mediaDelta.length; j++)
1100+
{
1101+
mediaDelta[j] = (double)exchFlux[j] * biomass[i] * cParams.getTimeStep();
1102+
// System.out.print("\t" + exchFlux[j]);
1103+
double [][] lightAbsorption = ((FBAModel)models[i]).getLightAbsorption();
1104+
if ((lightAbsorption[j][0]+lightAbsorption[j][1]) > 0) {
1105+
// Light is not used up as this is a flux
1106+
mediaDelta[j] = 0;
1107+
}
1108+
else
1109+
mediaDelta[j] = (double)exchFlux[j] * biomass[i] * cParams.getTimeStep();
1110+
}
1111+
deltaMedia[i] = mediaDelta;
1112+
1113+
/***************** GET BIOMASS CONCENTRATION CHANGE ****************/
1114+
// biomass is in grams
1115+
biomassGrowthRate = (double)(((FBAModel)models[i]).getBiomassFluxSolution());
1116+
deltaBiomass[i] = (double)(((FBAModel)models[i]).getBiomassFluxSolution()) * cParams.getTimeStep() * biomass[i];
1117+
allModelsGrowthRates[i]=biomassGrowthRate;
1118+
deltaBiomass[i] *= (1-(double)(((FBAModel)models[i]).getGenomeCost()));
1119+
biomass[i]=old_biomass[i]+deltaBiomass[i];
1120+
1121+
// if no biomass change don't change media //JEAN
1122+
if (!pParams.getAllowFluxWithoutGrowth()) {
1123+
if(deltaBiomass[i]<0.0){
1124+
deltaBiomass[i]=0.0;
1125+
for (int j=0; j<deltaMedia[i].length; j++)
1126+
{
1127+
deltaMedia[i][j] = 0.0;
1128+
}
1129+
}
1130+
}
1131+
1132+
/***************** REPORT IF THERE IS AN INFEASIBLE SOLUTION ****************/
1133+
}
1134+
// else //there's an error
1135+
// {
1136+
// System.out.print("flux");
1137+
// double[] exchFlux = ((FBAModel)fbaModels[i]).getExchangeFluxes();
1138+
// for (int j=0; j<exchFlux.length; j++)
1139+
// {
1140+
// System.out.print("\t" + 0);
1141+
// }
1142+
// System.out.println();
1143+
// }
1144+
}
1145+
}
1146+
1147+
// Now update media
1148+
/*for (int a=0; a<models.length; a++)
1149+
{
1150+
// JMC: removed '|| deltaBiomass[a]==0.0' part of if statement so toxins can degrade.
1151+
if (biomass[a] == 0 || Utility.sum(biomass) >= cParams.getMaxSpaceBiomass())
1152+
continue;
1153+
1154+
if(cParams.getNumLayers() == 1)
1155+
world.changeModelMedia(x, y, a, deltaMedia[a]);
1156+
else if (cParams.getNumLayers() > 1)
1157+
world3D.changeModelMedia(x, y, z, a, deltaMedia[a]);
1158+
1159+
}
1160+
*/
1161+
}
1162+
1163+
/* [Jean] Section for batch dilute Checks if models are growing
1164+
* and if they all stopped growing sets stationary phase in cell.
1165+
* This flag will remain on until the environment is updated.
1166+
*/
1167+
if (cParams.getBatchDilution()==true)
1168+
{
1169+
stationaryStatus =true;
1170+
for (int a=0; a<deltaMedia.length; a++)
1171+
{
1172+
if (deltaMedia[a] != null)
1173+
{
1174+
for (int b=0; b<deltaMedia[a].length; b++)
1175+
{
1176+
if(deltaMedia[a][b]!=0.0)
1177+
{
1178+
stationaryStatus = false;
1179+
}
1180+
}
1181+
}
1182+
}
1183+
}
9651184

9661185

9671186
// Now update media

0 commit comments

Comments
 (0)