use of cbit.vcell.mapping.SimulationContext 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);
}
use of cbit.vcell.mapping.SimulationContext in project vcell by virtualcell.
the class StandaloneRuleBasedTest method checkNonspatialStochasticSimContext.
private static void checkNonspatialStochasticSimContext(SimulationContext srcSimContext, File baseDirectory, int numTrials, long bngTimeoutDuration) throws Exception {
if (!srcSimContext.getApplicationType().equals(Application.NETWORK_STOCHASTIC) || srcSimContext.getGeometry().getDimension() != 0) {
throw new RuntimeException("simContext is of type " + srcSimContext.getApplicationType() + " and geometry dimension of " + srcSimContext.getGeometry().getDimension() + ", expecting nonspatial stochastic");
}
BioModel origBioModel = srcSimContext.getBioModel();
BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(XmlHelper.bioModelToXML(origBioModel)));
bioModel.refreshDependencies();
// create ODE and RuleBased
SimulationContext newODEApp = SimulationContext.copySimulationContext(srcSimContext, "aUniqueNewODEApp", false, Application.NETWORK_DETERMINISTIC);
SimulationContext newRuleBasedApp = SimulationContext.copySimulationContext(srcSimContext, "aUniqueNewRuleBasedApp", false, Application.RULE_BASED_STOCHASTIC);
newODEApp.setBioModel(bioModel);
newRuleBasedApp.setBioModel(bioModel);
ArrayList<AnnotatedFunction> outputFunctionsList = srcSimContext.getOutputFunctionContext().getOutputFunctionsList();
// OutputContext outputContext = new OutputContext(outputFunctionsList.toArray(new AnnotatedFunction[outputFunctionsList.size()]));
newODEApp.getOutputFunctionContext().setOutputFunctions(outputFunctionsList);
newRuleBasedApp.getOutputFunctionContext().setOutputFunctions(outputFunctionsList);
NetworkGenerationRequirements networkGenRequirements = NetworkGenerationRequirements.getComputeFull(bngTimeoutDuration);
bioModel.addSimulationContext(newODEApp);
newODEApp.refreshMathDescription(new MathMappingCallbackTaskAdapter(null), networkGenRequirements);
bioModel.addSimulationContext(newRuleBasedApp);
newRuleBasedApp.refreshMathDescription(new MathMappingCallbackTaskAdapter(null), networkGenRequirements);
srcSimContext.refreshMathDescription(new MathMappingCallbackTaskAdapter(null), networkGenRequirements);
// Create non-spatialStoch, ODE and RuleBased sims
Simulation nonspatialStochAppNewSim = srcSimContext.addNewSimulation(STOCH_SIM_NAME, /*SimulationOwner.DEFAULT_SIM_NAME_PREFIX*/
new MathMappingCallbackTaskAdapter(null), networkGenRequirements);
Simulation newODEAppNewSim = newODEApp.addNewSimulation(ODE_SIM_NAME, new MathMappingCallbackTaskAdapter(null), networkGenRequirements);
Simulation newRuleBasedAppNewSim = newRuleBasedApp.addNewSimulation(NFS_SIM_NAME, new MathMappingCallbackTaskAdapter(null), networkGenRequirements);
nonspatialStochAppNewSim.setSimulationOwner(srcSimContext);
newODEAppNewSim.setSimulationOwner(newODEApp);
newRuleBasedAppNewSim.setSimulationOwner(newRuleBasedApp);
try {
bioModel.getModel().getSpeciesContexts();
ArrayList<String> varNameList = new ArrayList<String>();
for (SpeciesContextSpec scs : srcSimContext.getReactionContext().getSpeciesContextSpecs()) {
varNameList.add(scs.getSpeciesContext().getName());
}
String[] varNames = varNameList.toArray(new String[0]);
OutputTimeSpec outputTimeSpec = nonspatialStochAppNewSim.getSolverTaskDescription().getOutputTimeSpec();
ArrayList<Double> sampleTimeList = new ArrayList<Double>();
if (outputTimeSpec instanceof UniformOutputTimeSpec) {
double endingTime = nonspatialStochAppNewSim.getSolverTaskDescription().getTimeBounds().getEndingTime();
double dT = ((UniformOutputTimeSpec) outputTimeSpec).getOutputTimeStep();
int currTimeIndex = 0;
while (currTimeIndex * dT <= (endingTime + 1e-8)) {
sampleTimeList.add(currTimeIndex * dT);
currTimeIndex++;
}
}
double[] sampleTimes = new double[sampleTimeList.size()];
for (int i = 0; i < sampleTimes.length; i++) {
sampleTimes[i] = sampleTimeList.get(i);
}
TimeSeriesMultitrialData sampleDataStoch1 = new TimeSeriesMultitrialData("stochastic1", varNames, sampleTimes, numTrials);
TimeSeriesMultitrialData sampleDataStoch2 = new TimeSeriesMultitrialData("stochastic2", varNames, sampleTimes, numTrials);
TimeSeriesMultitrialData sampleDataDeterministic = new TimeSeriesMultitrialData("determinstic", varNames, sampleTimes, 1);
runsolver(nonspatialStochAppNewSim, baseDirectory, numTrials, sampleDataStoch1);
runsolver(newODEAppNewSim, baseDirectory, 1, sampleDataDeterministic);
runsolver(newRuleBasedAppNewSim, baseDirectory, numTrials, sampleDataStoch2);
StochtestFileUtils.writeVarDiffData(new File(baseDirectory, VARDIFF_FILE), sampleDataStoch1, sampleDataStoch2);
StochtestFileUtils.writeKolmogorovSmirnovTest(new File(baseDirectory, KS_TEST_FILE), sampleDataStoch1, sampleDataStoch2);
StochtestFileUtils.writeChiSquareTest(new File(baseDirectory, ChiSquared_TEST_FILE), sampleDataStoch1, sampleDataStoch2);
StochtestFileUtils.writeData(sampleDataStoch1, new File(baseDirectory, "data." + sampleDataStoch1.datasetName + ".json"));
StochtestFileUtils.writeData(sampleDataStoch2, new File(baseDirectory, "data." + sampleDataStoch2.datasetName + ".json"));
StochtestFileUtils.writeData(sampleDataDeterministic, new File(baseDirectory, "data." + sampleDataDeterministic.datasetName + ".json"));
} finally {
srcSimContext.removeSimulation(nonspatialStochAppNewSim);
newODEApp.removeSimulation(newODEAppNewSim);
newRuleBasedApp.removeSimulation(newRuleBasedAppNewSim);
}
}
use of cbit.vcell.mapping.SimulationContext 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.mapping.SimulationContext in project vcell by virtualcell.
the class ApplicationsPropertiesPanel method updateInterface.
/**
* Comment
*/
private void updateInterface() {
if (bioModel == null) {
return;
}
applicationsPanel.removeAll();
int gridy = 0;
SimulationContext[] simulationContexts = bioModel.getSimulationContexts();
if (simulationContexts != null) {
for (int i = 0; i < simulationContexts.length; i++) {
SimulationContext simContext = simulationContexts[i];
JLabel label = new JLabel(simContext.getName());
label.setFont(label.getFont().deriveFont(Font.BOLD));
label.setIcon(simulationContextIcon);
GridBagConstraints gbc = new java.awt.GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = gridy++;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
if (i > 0) {
gbc.insets = new Insets(4, 0, 0, 0);
}
applicationsPanel.add(label, gbc);
Geometry geometry = simContext.getGeometry();
String geometryText = "Compartmental geometry";
if (geometry != null) {
Version geometryVersion = geometry.getVersion();
int dimension = geometry.getDimension();
if (dimension > 0) {
String description = geometry.getDimension() + "D " + (geometry.getGeometrySpec().hasImage() ? "image" : "analytic") + " geometry";
geometryText = description;
if (geometryVersion != null) {
geometryText += " - " + geometryVersion.getName();
}
}
}
JLabel geometryLabel = new JLabel(geometryText);
geometryLabel.setIcon(geometryIcon);
JLabel detStochLabel = new JLabel(simContext.getMathType().getDescription());
detStochLabel.setIcon(appTypeIcon);
gbc.insets = new Insets(2, 20, 2, 2);
gbc.gridy = gridy++;
applicationsPanel.add(detStochLabel, gbc);
gbc.gridy = gridy++;
if (i == simulationContexts.length - 1) {
gbc.weighty = 1.0;
}
applicationsPanel.add(geometryLabel, gbc);
}
}
}
use of cbit.vcell.mapping.SimulationContext in project vcell by virtualcell.
the class ApplicationsPropertiesPanel method setBioModel.
/**
* Sets the speciesContext property (cbit.vcell.model.SpeciesContext) value.
* @param speciesContext The new value for the property.
* @see #getSpeciesContext
*/
public void setBioModel(BioModel newValue) {
if (newValue == bioModel) {
return;
}
BioModel oldValue = bioModel;
if (oldValue != null) {
oldValue.removePropertyChangeListener(eventHandler);
for (SimulationContext simContext : oldValue.getSimulationContexts()) {
simContext.removePropertyChangeListener(eventHandler);
}
}
bioModel = newValue;
if (newValue != null) {
newValue.addPropertyChangeListener(eventHandler);
for (SimulationContext simContext : newValue.getSimulationContexts()) {
simContext.addPropertyChangeListener(eventHandler);
}
}
updateInterface();
}
Aggregations