Search in sources :

Example 51 with SimulationContext

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);
}
Also used : PrintStream(java.io.PrintStream) MathModel(cbit.vcell.mathmodel.MathModel) BioModelInfo(org.vcell.util.document.BioModelInfo) MathModelInfo(org.vcell.util.document.MathModelInfo) SimulationContext(cbit.vcell.mapping.SimulationContext) Geometry(cbit.vcell.geometry.Geometry) BioModel(cbit.vcell.biomodel.BioModel) GeometryInfo(cbit.vcell.geometry.GeometryInfo) VCDatabaseVisitor(cbit.vcell.modeldb.VCDatabaseVisitor) File(java.io.File)

Example 52 with SimulationContext

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);
    }
}
Also used : MathMappingCallbackTaskAdapter(cbit.vcell.mapping.MathMappingCallbackTaskAdapter) UniformOutputTimeSpec(cbit.vcell.solver.UniformOutputTimeSpec) TimeSeriesMultitrialData(org.vcell.stochtest.TimeSeriesMultitrialData) ArrayList(java.util.ArrayList) SimulationContext(cbit.vcell.mapping.SimulationContext) SpeciesContextSpec(cbit.vcell.mapping.SpeciesContextSpec) UniformOutputTimeSpec(cbit.vcell.solver.UniformOutputTimeSpec) OutputTimeSpec(cbit.vcell.solver.OutputTimeSpec) TempSimulation(cbit.vcell.solver.TempSimulation) Simulation(cbit.vcell.solver.Simulation) BioModel(cbit.vcell.biomodel.BioModel) NetworkGenerationRequirements(cbit.vcell.mapping.SimulationContext.NetworkGenerationRequirements) XMLSource(cbit.vcell.xml.XMLSource) File(java.io.File) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction)

Example 53 with SimulationContext

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);
}
Also used : QueryHashtable(cbit.sql.QueryHashtable) KeyValue(org.vcell.util.document.KeyValue) BigString(org.vcell.util.BigString) SimulationContext(cbit.vcell.mapping.SimulationContext) BigString(org.vcell.util.BigString) OverrideRepresentation(org.vcell.rest.common.OverrideRepresentation) ServerDocumentManager(cbit.vcell.modeldb.ServerDocumentManager) MathOverrides(cbit.vcell.solver.MathOverrides) Simulation(cbit.vcell.solver.Simulation) Random(java.util.Random) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) BioModel(cbit.vcell.biomodel.BioModel) XMLSource(cbit.vcell.xml.XMLSource) SimulationRep(cbit.vcell.modeldb.SimulationRep)

Example 54 with SimulationContext

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);
        }
    }
}
Also used : Geometry(cbit.vcell.geometry.Geometry) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) Version(org.vcell.util.document.Version) JLabel(javax.swing.JLabel) SimulationContext(cbit.vcell.mapping.SimulationContext)

Example 55 with SimulationContext

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();
}
Also used : BioModel(cbit.vcell.biomodel.BioModel) SimulationContext(cbit.vcell.mapping.SimulationContext)

Aggregations

SimulationContext (cbit.vcell.mapping.SimulationContext)181 BioModel (cbit.vcell.biomodel.BioModel)73 Simulation (cbit.vcell.solver.Simulation)57 MathDescription (cbit.vcell.math.MathDescription)32 Geometry (cbit.vcell.geometry.Geometry)29 PropertyVetoException (java.beans.PropertyVetoException)28 MathModel (cbit.vcell.mathmodel.MathModel)25 Model (cbit.vcell.model.Model)25 KeyValue (org.vcell.util.document.KeyValue)25 SpeciesContextSpec (cbit.vcell.mapping.SpeciesContextSpec)23 ArrayList (java.util.ArrayList)23 DataAccessException (org.vcell.util.DataAccessException)23 XMLSource (cbit.vcell.xml.XMLSource)22 XmlParseException (cbit.vcell.xml.XmlParseException)22 AsynchClientTask (cbit.vcell.client.task.AsynchClientTask)20 Expression (cbit.vcell.parser.Expression)20 ExpressionException (cbit.vcell.parser.ExpressionException)19 SpeciesContext (cbit.vcell.model.SpeciesContext)17 Structure (cbit.vcell.model.Structure)17 IOException (java.io.IOException)17