use of cbit.vcell.modeldb.ServerDocumentManager in project vcell by virtualcell.
the class RestDatabaseService method saveSimulation.
public SimulationSaveResponse saveSimulation(BiomodelSimulationSaveServerResource resource, User vcellUser, List<OverrideRepresentation> overrideRepresentations) throws PermissionException, ObjectNotFoundException, DataAccessException, SQLException, XmlParseException, PropertyVetoException, MappingException, ExpressionException {
String simId = resource.getAttribute(VCellApiApplication.SIMULATIONID);
KeyValue simKey = new KeyValue(simId);
SimulationRep simRep = getSimulationRep(simKey);
if (simRep == null) {
throw new ObjectNotFoundException("Simulation with key " + simKey + " not found");
}
boolean myModel = simRep.getOwner().compareEqual(vcellUser);
// get the bioModel
String biomodelId = resource.getAttribute(VCellApiApplication.BIOMODELID);
KeyValue biomodelKey = new KeyValue(biomodelId);
BigString bioModelXML = this.databaseServerImpl.getBioModelXML(vcellUser, biomodelKey);
BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(bioModelXML.toString()));
// copy the simulation as new
Simulation origSimulation = null;
for (Simulation sim : bioModel.getSimulations()) {
if (sim.getKey().equals(simKey)) {
origSimulation = sim;
}
}
if (origSimulation == null) {
throw new RuntimeException("cannot find original Simulation");
}
SimulationContext simContext = bioModel.getSimulationContext(origSimulation);
Simulation newUnsavedSimulation = simContext.copySimulation(origSimulation);
// make appropriate changes
// MATH OVERRIDES
MathOverrides mathOverrides = new MathOverrides(newUnsavedSimulation);
for (OverrideRepresentation overrideRep : overrideRepresentations) {
overrideRep.applyMathOverrides(mathOverrides);
}
newUnsavedSimulation.setMathOverrides(mathOverrides);
// save bioModel
String editedBioModelXML = XmlHelper.bioModelToXML(bioModel);
ServerDocumentManager serverDocumentManager = new ServerDocumentManager(this.databaseServerImpl);
String modelName = bioModel.getName();
if (!myModel) {
modelName = modelName + "_" + Math.abs(new Random().nextInt());
}
String newBioModelXML = serverDocumentManager.saveBioModel(new QueryHashtable(), vcellUser, editedBioModelXML, modelName, null);
BioModel savedBioModel = XmlHelper.XMLToBioModel(new XMLSource(newBioModelXML));
Simulation savedSimulation = null;
for (Simulation sim : savedBioModel.getSimulations()) {
if (sim.getName().equals(newUnsavedSimulation.getName())) {
savedSimulation = sim;
}
}
if (savedSimulation == null) {
throw new RuntimeException("cannot find new Simulation");
}
return new SimulationSaveResponse(savedBioModel, savedSimulation);
}
use of cbit.vcell.modeldb.ServerDocumentManager in project vcell by virtualcell.
the class StochtestRunService method runOne.
public void runOne() throws IllegalArgumentException, SQLException, DataAccessException, XmlParseException, PropertyVetoException, ExpressionException, MappingException, GeometryException, ImageException, IOException {
StochtestRun stochtestRun = StochtestDbUtils.acceptNextWaitingStochtestRun(conFactory);
String biomodelXML = null;
if (stochtestRun != null) {
String networkGenProbs = null;
try {
User user = new User(PropertyLoader.ADMINISTRATOR_ACCOUNT, new KeyValue(PropertyLoader.ADMINISTRATOR_ID));
ServerDocumentManager serverDocumentManager = new ServerDocumentManager(this.dbServerImpl);
biomodelXML = serverDocumentManager.getBioModelXML(new QueryHashtable(), user, stochtestRun.stochtest.biomodelRef, true);
BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(biomodelXML));
bioModel.refreshDependencies();
SimulationContext srcSimContext = null;
for (SimulationContext sc : bioModel.getSimulationContexts()) {
if (sc.getKey().equals(stochtestRun.stochtest.simContextRef)) {
srcSimContext = sc;
}
}
if (srcSimContext == null) {
throw new RuntimeException("cannot find simcontext with key=" + stochtestRun.stochtest.simContextRef);
}
//
for (SpeciesContextSpec scs : srcSimContext.getReactionContext().getSpeciesContextSpecs()) {
scs.setConstant(false);
}
SimulationContext simContext = srcSimContext;
StochtestMathType parentMathType = stochtestRun.parentMathType;
StochtestMathType mathType = stochtestRun.mathType;
if (parentMathType != mathType) {
if (parentMathType == StochtestMathType.nonspatialstochastic && mathType == StochtestMathType.rules) {
simContext = SimulationContext.copySimulationContext(srcSimContext, "generatedRules", false, Application.RULE_BASED_STOCHASTIC);
} else if (parentMathType == StochtestMathType.rules && mathType == StochtestMathType.nonspatialstochastic) {
simContext = SimulationContext.copySimulationContext(srcSimContext, "generatedSSA", false, Application.NETWORK_STOCHASTIC);
} else {
throw new RuntimeException("unexpected copy of simcontext from " + parentMathType + " to " + mathType);
}
bioModel.addSimulationContext(simContext);
}
MathMappingCallback mathMappingCallback = new MathMappingCallback() {
@Override
public void setProgressFraction(float fractionDone) {
}
@Override
public void setMessage(String message) {
}
@Override
public boolean isInterrupted() {
return false;
}
};
MathMapping mathMapping = simContext.createNewMathMapping(mathMappingCallback, NetworkGenerationRequirements.ComputeFullStandardTimeout);
MathDescription mathDesc = mathMapping.getMathDescription(mathMappingCallback);
simContext.setMathDescription(mathDesc);
if (simContext.isInsufficientIterations()) {
networkGenProbs = "insufficientIterations";
} else if (simContext.isInsufficientMaxMolecules()) {
networkGenProbs = "insufficientMaxMolecules";
}
File baseDirectory = StochtestFileUtils.createDirFile(baseDir, stochtestRun);
try {
OutputTimeSpec outputTimeSpec = new UniformOutputTimeSpec(0.5);
double endTime = 10.0;
computeTrials(simContext, stochtestRun, baseDirectory, outputTimeSpec, endTime, numTrials);
StochtestDbUtils.finalizeAcceptedStochtestRun(conFactory, stochtestRun, StochtestRun.StochtestRunStatus.complete, null, networkGenProbs);
} finally {
StochtestFileUtils.clearDir(baseDirectory);
}
} catch (Exception e) {
StochtestDbUtils.finalizeAcceptedStochtestRun(conFactory, stochtestRun, StochtestRun.StochtestRunStatus.failed, e.getMessage(), networkGenProbs);
//
if (biomodelXML != null) {
XmlUtil.writeXMLStringToFile(biomodelXML, new File(baseDir, "stochtestrun_" + stochtestRun.stochtest.key + ".vcml").getPath(), false);
}
//
// write exception trace to .txt file
//
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
printWriter.flush();
System.out.println(stringWriter.getBuffer().toString());
XmlUtil.writeXMLStringToFile(stringWriter.getBuffer().toString(), new File(baseDir, "stochtestrun_" + stochtestRun.stochtest.key + "_error.txt").getPath(), false);
}
} else {
System.out.println("no jobs waiting");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Aggregations