use of cbit.vcell.biomodel.BioModel in project vcell by virtualcell.
the class BatchTester method batchScanBioModels.
@SuppressWarnings("static-access")
public void batchScanBioModels(VCMultiBioVisitor databaseVisitor, String statusTable, int chunkSize) throws DataAccessException, XmlParseException, SQLException, IOException {
PrintStream current = System.out;
// System.setOut(new PrintStream(new NullStream()));
try {
String processHostId = ManagementFactory.getRuntimeMXBean().getName();
String filename = processHostId + ".txt";
FileOutputStream fos = new FileOutputStream(filename);
System.setOut(new PrintStream(fos));
OutputStreamWriter writer = new OutputStreamWriter(fos);
// autoflush
PrintWriter printWriter = new PrintWriter(writer, true);
Connection conn = connFactory.getConnection(null);
conn.setAutoCommit(true);
printWriter.println("reserving slots");
try (Statement statement = conn.createStatement()) {
String query = "Update " + statusTable + " set scan_process = '" + processHostId + "', log_file = '" + filename + "' where scanned = 0 and scan_process is null and rownum <= " + chunkSize;
int uCount = statement.executeUpdate(query);
if (uCount > chunkSize) {
throw new Error("logic / SQL bad");
}
if (uCount == 0) {
printWriter.println("No models to scan, exiting");
System.exit(100);
}
}
printWriter.println("finding ours");
ArrayList<BioModelIdent> models = new ArrayList<BatchTester.BioModelIdent>();
try (Statement statement = conn.createStatement()) {
String query = "Select id, user_id, model_id from " + statusTable + " where scan_process ='" + processHostId + "' and scanned = 0";
ResultSet rs = statement.executeQuery(query);
while (rs.next()) {
BioModelIdent mi = new BioModelIdent(rs);
models.add(mi);
printWriter.println("claiming " + mi.statusId);
}
}
try {
// start visiting models and writing log
printWriter.println("Start scanning bio-models......");
printWriter.println("\n");
PreparedStatement ps = conn.prepareStatement("Update " + statusTable + " set scanned = 1, good = ? , exception_type = ?, exception = ?, scan_process = null where id = ?");
for (BioModelIdent modelIdent : models) {
ScanStatus scanStatus = ScanStatus.PASS;
String exceptionMessage = null;
String exceptionClass = null;
try {
User user = new User("", convert(modelIdent.userId));
KeyValue modelKey = convert(modelIdent.modelId);
BigString bioModelXML = null;
// seconds
long dbSleepTime = 10;
while (bioModelXML == null) {
try {
bioModelXML = dbServerImpl.getBioModelXML(user, modelKey);
} catch (DataAccessException dae) {
Throwable cause = dae.getCause();
if (cause.getClass().getSimpleName().equals("UniversalConnectionPoolException")) {
printWriter.println("No db connection for " + modelIdent.statusId + ", sleeping " + dbSleepTime + " seconds");
Thread.currentThread().sleep(dbSleepTime * 1000);
// wait a little longer next time
dbSleepTime *= 1.5;
} else {
// other exception, just rethrow
throw dae;
}
}
}
BioModel storedModel = cbit.vcell.xml.XmlHelper.XMLToBioModel(new XMLSource(bioModelXML.toString()));
if (databaseVisitor.filterBioModel(storedModel)) {
storedModel.refreshDependencies();
boolean goodModel = verifyMathDescriptionsUnchanged(storedModel, printWriter);
if (goodModel) {
printWriter.println("Model for " + modelIdent.statusId + " good");
databaseVisitor.setBioModel(storedModel, printWriter);
for (BioModel bioModel : databaseVisitor) {
SimulationContext[] simContexts = bioModel.getSimulationContexts();
for (SimulationContext sc : simContexts) {
// try {
// long start = System.currentTimeMillis();
sc.createNewMathMapping().getMathDescription();
// long end = System.currentTimeMillis();
// printWriter.println("mapping took " + (end - start)/1000.0 + " sec ");
/*
} catch (Exception e) {
//printWriter.println("\t " + bioModel.getName() + " :: " + sc.getName() + " ----> math regeneration failed.s");
// e.printStackTrace();
}
*/
}
}
} else {
throw new MathRegenFail();
}
} else {
scanStatus = ScanStatus.FILTERED;
}
} catch (Exception e) {
lg.error(e.getMessage(), e);
scanStatus = ScanStatus.FAIL;
exceptionClass = e.getClass().getName();
exceptionMessage = e.getMessage();
printWriter.println("failed " + modelIdent.statusId);
e.printStackTrace(printWriter);
}
ps.setInt(1, scanStatus.code);
ps.setString(2, exceptionClass);
ps.setString(3, exceptionMessage);
ps.setLong(4, modelIdent.statusId);
boolean estat = ps.execute();
if (estat) {
throw new Error("logic");
}
int uc = ps.getUpdateCount();
if (uc != 1) {
throw new Error("logic / sql ");
}
}
} catch (Exception e) {
e.printStackTrace(printWriter);
}
printWriter.close();
} finally {
System.setOut(current);
}
}
use of cbit.vcell.biomodel.BioModel in project vcell by virtualcell.
the class DisplayBioModel method compute0.
@Override
protected void compute0(TaskContext context, final ClientTaskStatusSupport clientTaskStatusSupport) throws Exception {
// get input
BioModel biomodel = context.getData(bioModel);
// do op
DisplayBioModelOp op = new DisplayBioModelOp();
op.displayBioModel(biomodel);
// set output
context.setData(displayed, true);
}
use of cbit.vcell.biomodel.BioModel in project vcell by virtualcell.
the class Generate2DSimBioModel method compute0.
@Override
protected void compute0(TaskContext context, final ClientTaskStatusSupport clientTaskStatusSupport) throws Exception {
// get inputs
Extent extent = context.getData(this.extent);
ROI cellROI_2D = context.getData(this.cellROI_2D);
double[] timeStamps = context.getData(this.timeStamps);
Integer indexFirstPostbleach = context.getData(this.indexFirstPostbleach);
double primaryDiffusionRate = context.getData(this.primaryDiffusionRate);
double primaryFraction = context.getData(this.primaryFraction);
double bleachMonitorRate = context.getData(this.bleachMonitorRate);
Double secondaryDiffusionRate = context.getData(this.secondaryDiffusionRate);
double secondaryFraction = context.getData(this.secondaryFraction);
double bindingSiteConcentration = context.getData(this.bindingSiteConcentration);
double bindingOnRate = context.getData(this.bindingOnRate);
double bindingOffRate = context.getData(this.bindingOffRate);
String extracellularName = context.getData(this.extracellularName);
String cytosolName = context.getData(this.cytosolName);
User owner = context.getData(this.owner);
KeyValue simKey = context.getData(this.simKey);
// do op
Generate2DSimBioModelOp op = new Generate2DSimBioModelOp();
BioModel bioModel = op.generateBioModel(extent, cellROI_2D, timeStamps, indexFirstPostbleach, primaryDiffusionRate, primaryFraction, bleachMonitorRate, secondaryDiffusionRate, secondaryFraction, bindingSiteConcentration, bindingOnRate, bindingOffRate, extracellularName, cytosolName, owner, simKey);
// set output
context.setData(this.bioModel_2D, bioModel);
}
use of cbit.vcell.biomodel.BioModel in project vcell by virtualcell.
the class Generate2DExpModelOpAbstract method generateModel.
public final GeneratedModelResults generateModel(double deltaX, double bleachRadius, double cellRadius, double bleachDuration, double bleachRate, double postbleachDelay, double postbleachDuration, double psfSigma, double outputTimeStep, double primaryDiffusionRate, double primaryFraction, double bleachMonitorRate, double secondaryDiffusionRate, double secondaryFraction, String extracellularName, String cytosolName, Context context) throws PropertyVetoException, ExpressionException, GeometryException, ImageException, ModelException, MappingException, MathException, MatrixException {
double domainSize = 2.2 * cellRadius;
Extent extent = new Extent(domainSize, domainSize, 1.0);
Origin origin = new Origin(-extent.getX() / 2.0, -extent.getY() / 2.0, -extent.getZ() / 2.0);
String EXTRACELLULAR_NAME = extracellularName;
String CYTOSOL_NAME = cytosolName;
AnalyticSubVolume cytosolSubVolume = new AnalyticSubVolume(CYTOSOL_NAME, new Expression("pow(x,2)+pow(y,2)<pow(" + cellRadius + ",2)"));
AnalyticSubVolume extracellularSubVolume = new AnalyticSubVolume(EXTRACELLULAR_NAME, new Expression(1.0));
Geometry geometry = new Geometry("geometry", 2);
geometry.getGeometrySpec().setExtent(extent);
geometry.getGeometrySpec().setOrigin(origin);
geometry.getGeometrySpec().addSubVolume(extracellularSubVolume);
geometry.getGeometrySpec().addSubVolume(cytosolSubVolume, true);
geometry.getGeometrySurfaceDescription().updateAll();
BioModel bioModel = new BioModel(null);
bioModel.setName("unnamed");
Model model = new Model("model");
bioModel.setModel(model);
model.addFeature(EXTRACELLULAR_NAME);
Feature extracellular = (Feature) model.getStructure(EXTRACELLULAR_NAME);
model.addFeature(CYTOSOL_NAME);
Feature cytosol = (Feature) model.getStructure(CYTOSOL_NAME);
SpeciesContext immobileSC = model.createSpeciesContext(cytosol);
SpeciesContext primarySC = model.createSpeciesContext(cytosol);
SpeciesContext secondarySC = model.createSpeciesContext(cytosol);
//
// common bleaching rate for all species
//
double bleachStart = 10 * outputTimeStep - bleachDuration - postbleachDelay;
double bleachEnd = bleachStart + bleachDuration;
Expression bleachRateExp = createBleachExpression(bleachRadius, bleachRate, bleachMonitorRate, bleachStart, bleachEnd);
{
SimpleReaction immobileBWM = model.createSimpleReaction(cytosol);
GeneralKinetics immobileBWMKinetics = new GeneralKinetics(immobileBWM);
immobileBWM.setKinetics(immobileBWMKinetics);
immobileBWM.addReactant(immobileSC, 1);
immobileBWMKinetics.getReactionRateParameter().setExpression(Expression.mult(bleachRateExp, new Expression(immobileSC.getName())));
}
{
SimpleReaction primaryBWM = model.createSimpleReaction(cytosol);
GeneralKinetics primaryBWMKinetics = new GeneralKinetics(primaryBWM);
primaryBWM.setKinetics(primaryBWMKinetics);
primaryBWM.addReactant(primarySC, 1);
primaryBWMKinetics.getReactionRateParameter().setExpression(Expression.mult(bleachRateExp, new Expression(primarySC.getName())));
}
{
SimpleReaction secondaryBWM = model.createSimpleReaction(cytosol);
GeneralKinetics secondaryBWMKinetics = new GeneralKinetics(secondaryBWM);
secondaryBWM.setKinetics(secondaryBWMKinetics);
secondaryBWM.addReactant(secondarySC, 1);
secondaryBWMKinetics.getReactionRateParameter().setExpression(Expression.mult(bleachRateExp, new Expression(secondarySC.getName())));
}
// create simulation context
SimulationContext simContext = bioModel.addNewSimulationContext("simContext", SimulationContext.Application.NETWORK_DETERMINISTIC);
simContext.setGeometry(geometry);
FeatureMapping cytosolFeatureMapping = (FeatureMapping) simContext.getGeometryContext().getStructureMapping(cytosol);
FeatureMapping extracellularFeatureMapping = (FeatureMapping) simContext.getGeometryContext().getStructureMapping(extracellular);
SubVolume cytSubVolume = geometry.getGeometrySpec().getSubVolume(CYTOSOL_NAME);
SubVolume exSubVolume = geometry.getGeometrySpec().getSubVolume(EXTRACELLULAR_NAME);
// unused? SurfaceClass pmSurfaceClass = geometry.getGeometrySurfaceDescription().getSurfaceClass(exSubVolume, cytSubVolume);
cytosolFeatureMapping.setGeometryClass(cytSubVolume);
extracellularFeatureMapping.setGeometryClass(exSubVolume);
cytosolFeatureMapping.getUnitSizeParameter().setExpression(new Expression(1.0));
extracellularFeatureMapping.getUnitSizeParameter().setExpression(new Expression(1.0));
double fixedFraction = 1.0 - primaryFraction - secondaryFraction;
SpeciesContextSpec immobileSCS = simContext.getReactionContext().getSpeciesContextSpec(immobileSC);
immobileSCS.getInitialConditionParameter().setExpression(new Expression(fixedFraction));
immobileSCS.getDiffusionParameter().setExpression(new Expression(0.0));
SpeciesContextSpec primarySCS = simContext.getReactionContext().getSpeciesContextSpec(primarySC);
primarySCS.getInitialConditionParameter().setExpression(new Expression(primaryFraction));
primarySCS.getDiffusionParameter().setExpression(new Expression(primaryDiffusionRate));
SpeciesContextSpec secondarySCS = simContext.getReactionContext().getSpeciesContextSpec(secondarySC);
secondarySCS.getInitialConditionParameter().setExpression(new Expression(secondaryFraction));
secondarySCS.getDiffusionParameter().setExpression(new Expression(secondaryDiffusionRate));
simContext.getMicroscopeMeasurement().addFluorescentSpecies(immobileSC);
simContext.getMicroscopeMeasurement().addFluorescentSpecies(primarySC);
simContext.getMicroscopeMeasurement().addFluorescentSpecies(secondarySC);
simContext.getMicroscopeMeasurement().setConvolutionKernel(new GaussianConvolutionKernel(new Expression(psfSigma), new Expression(psfSigma)));
MathMapping mathMapping = simContext.createNewMathMapping();
MathDescription mathDesc = mathMapping.getMathDescription();
simContext.setMathDescription(mathDesc);
User owner = context.getDefaultOwner();
int meshSize = (int) (domainSize / deltaX);
if (meshSize % 2 == 0) {
// want an odd-sized mesh in x and y ... so centered at the origin.
meshSize = meshSize + 1;
}
TimeBounds timeBounds = new TimeBounds(0.0, postbleachDuration);
//
// simulation to use for data generation (output time steps as recorded by the microscope)
//
double bleachBlackoutBegin = bleachStart - postbleachDelay;
double bleachBlackoutEnd = bleachEnd + postbleachDelay;
// ArrayList<Double> times = new ArrayList<Double>();
// double time = 0;
// while (time<=timeBounds.getEndingTime()){
// if (time<=bleachBlackoutBegin || time>bleachBlackoutEnd){
// // postbleachDelay is the time it takes to switch the filters.
// times.add(time);
// }
// time += outputTimeStep.getData();
// }
// double[] timeArray = new double[times.size()];
// for (int i=0;i<timeArray.length;i++){
// timeArray[i] = times.get(i);
// }
// OutputTimeSpec fakeDataSimOutputTimeSpec = new ExplicitOutputTimeSpec(timeArray);
OutputTimeSpec fakeDataSimOutputTimeSpec = new UniformOutputTimeSpec(outputTimeStep);
KeyValue fakeDataSimKey = context.createNewKeyValue();
SimulationVersion fakeDataSimVersion = new SimulationVersion(fakeDataSimKey, "fakeDataSim", owner, new GroupAccessNone(), new KeyValue("0"), new BigDecimal(0), new Date(), VersionFlag.Current, "", null);
Simulation fakeDataSim = new Simulation(fakeDataSimVersion, mathDesc);
simContext.addSimulation(fakeDataSim);
fakeDataSim.getSolverTaskDescription().setTimeBounds(timeBounds);
fakeDataSim.getMeshSpecification().setSamplingSize(new ISize(meshSize, meshSize, 1));
fakeDataSim.getSolverTaskDescription().setSolverDescription(SolverDescription.SundialsPDE);
fakeDataSim.getSolverTaskDescription().setOutputTimeSpec(fakeDataSimOutputTimeSpec);
//
// simulation to use for viewing the protocol (output time steps to understand the physics)
//
KeyValue fullExperimentSimKey = context.createNewKeyValue();
SimulationVersion fullExperimentSimVersion = new SimulationVersion(fullExperimentSimKey, "fullExperiment", owner, new GroupAccessNone(), new KeyValue("0"), new BigDecimal(0), new Date(), VersionFlag.Current, "", null);
Simulation fullExperimentSim = new Simulation(fullExperimentSimVersion, mathDesc);
simContext.addSimulation(fullExperimentSim);
OutputTimeSpec fullExperimentOutputTimeSpec = new UniformOutputTimeSpec(outputTimeStep / 10.0);
fullExperimentSim.getSolverTaskDescription().setTimeBounds(timeBounds);
fullExperimentSim.getMeshSpecification().setSamplingSize(new ISize(meshSize, meshSize, 1));
fullExperimentSim.getSolverTaskDescription().setSolverDescription(SolverDescription.SundialsPDE);
fullExperimentSim.getSolverTaskDescription().setOutputTimeSpec(fullExperimentOutputTimeSpec);
GeneratedModelResults results = new GeneratedModelResults();
results.bioModel_2D = bioModel;
results.simulation_2D = fakeDataSim;
results.bleachBlackoutBeginTime = bleachBlackoutBegin;
results.bleachBlackoutEndTime = bleachBlackoutEnd;
return results;
}
use of cbit.vcell.biomodel.BioModel in project vcell by virtualcell.
the class RuleBasedTest method main.
public static void main(String[] args) {
try {
PropertyLoader.loadProperties();
} catch (Exception e) {
e.printStackTrace();
}
final int numTrials = 40;
VCDatabaseVisitor vcDatabaseVisitor = new VCDatabaseVisitor() {
@Override
public void visitMathModel(MathModel mathModel, PrintStream logFilePrintStream) {
throw new IllegalArgumentException("Not Implemented");
}
@Override
public void visitGeometry(Geometry geometry, PrintStream logFilePrintStream) {
throw new IllegalArgumentException("Not Implemented");
}
@Override
public void visitBioModel(BioModel bioModel, PrintStream logFilePrintStream) {
SimulationContext[] simulationContexts = bioModel.getSimulationContexts();
for (SimulationContext simContext : simulationContexts) {
if ((simContext.getApplicationType() == Application.NETWORK_STOCHASTIC) && simContext.getGeometry().getDimension() == 0) {
File baseDirectory = createDirFile(simContext);
try {
checkNonspatialStochasticSimContext(simContext, baseDirectory, numTrials);
} catch (Exception e) {
e.printStackTrace();
if (!e.getMessage().contains("Only Mass Action Kinetics supported ")) {
writeMessageTofile(baseDirectory, e.getMessage());
}
}
}
}
}
@Override
public boolean filterMathModel(MathModelInfo mathModelInfo) {
return false;
}
@Override
public boolean filterGeometry(GeometryInfo geometryInfo) {
return false;
}
@Override
public boolean filterBioModel(BioModelInfo bioModelInfo) {
return // bioModelInfo.getVersion().getName().equals("model");
bioModelInfo.getVersion().getName().equals("simpleModel_Network_orig");
}
};
String currentUserID = "schaff";
String[] allUsers = new String[] { /*-all*/
currentUserID, "-" };
VCDatabaseScanner.scanBioModels(allUsers, vcDatabaseVisitor, false);
}
Aggregations