Search in sources :

Example 6 with RateRule

use of cbit.vcell.mapping.RateRule in project vcell by virtualcell.

the class SimContextTable method readAppComponents.

/**
 * readAppComponents : reads the additional simContext components like bioevents/application related flags (for stochastic, at the moment), if present, and sets them on simContext.
 * @param con
 * @param simContext
 * @return
 * @throws SQLException
 * @throws DataAccessException
 * @throws PropertyVetoException
 */
public void readAppComponents(Connection con, SimulationContext simContext, DatabaseSyntax dbSyntax) throws SQLException, DataAccessException, PropertyVetoException {
    try {
        Element appComponentsElement = getAppComponentsElement(con, simContext.getVersion().getVersionKey(), dbSyntax);
        if (appComponentsElement != null) {
            Element appRelatedFlags = appComponentsElement.getChild(XMLTags.ApplicationSpecificFlagsTag);
            if (appRelatedFlags != null) {
                // for now, only reading the 'randomizeInitCondition' attribute, since 'isStoch' and 'isUsingconcentration' are read in by other means; so not messing with those fields of simContext.
                boolean bRandomizeInitCondition = false;
                if ((appRelatedFlags.getAttributeValue(XMLTags.RandomizeInitConditionTag) != null) && (appRelatedFlags.getAttributeValue(XMLTags.RandomizeInitConditionTag).equals("true"))) {
                    bRandomizeInitCondition = true;
                }
                simContext.setRandomizeInitConditions(bRandomizeInitCondition);
            }
            if ((appComponentsElement.getAttributeValue(XMLTags.InsufficientIterationsTag) != null) && (appComponentsElement.getAttributeValue(XMLTags.InsufficientIterationsTag).equals("true"))) {
                simContext.setInsufficientIterations(true);
            } else {
                simContext.setInsufficientIterations(false);
            }
            if ((appComponentsElement.getAttributeValue(XMLTags.InsufficientMaxMoleculesTag) != null) && (appComponentsElement.getAttributeValue(XMLTags.InsufficientMaxMoleculesTag).equals("true"))) {
                simContext.setInsufficientMaxMolecules(true);
            } else {
                simContext.setInsufficientMaxMolecules(false);
            }
            XmlReader xmlReader = new XmlReader(false);
            NetworkConstraints nc = null;
            Element ncElement = appComponentsElement.getChild(XMLTags.RbmNetworkConstraintsTag);
            if (ncElement != null) {
                // one network constraint element
                nc = xmlReader.getAppNetworkConstraints(ncElement, simContext.getModel());
            }
            simContext.setNetworkConstraints(nc);
            // get spatial objects
            Element spatialObjectsElement = appComponentsElement.getChild(XMLTags.SpatialObjectsTag);
            if (spatialObjectsElement != null) {
                SpatialObject[] spatialObjects = xmlReader.getSpatialObjects(simContext, spatialObjectsElement);
                simContext.setSpatialObjects(spatialObjects);
            }
            // get application parameters
            Element appParamsElement = appComponentsElement.getChild(XMLTags.ApplicationParametersTag);
            if (appParamsElement != null) {
                SimulationContextParameter[] appParams = xmlReader.getSimulationContextParams(appParamsElement, simContext);
                simContext.setSimulationContextParameters(appParams);
            }
            // get bioEvents
            Element bioEventsElement = appComponentsElement.getChild(XMLTags.BioEventsTag);
            if (bioEventsElement != null) {
                BioEvent[] bioEvents = xmlReader.getBioEvents(simContext, bioEventsElement);
                simContext.setBioEvents(bioEvents);
            }
            // get spatial processes
            Element spatialProcessesElement = appComponentsElement.getChild(XMLTags.SpatialProcessesTag);
            if (spatialProcessesElement != null) {
                SpatialProcess[] spatialProcesses = xmlReader.getSpatialProcesses(simContext, spatialProcessesElement);
                simContext.setSpatialProcesses(spatialProcesses);
            }
            // get microscope measurements
            Element element = appComponentsElement.getChild(XMLTags.MicroscopeMeasurement);
            if (element != null) {
                xmlReader.getMicroscopeMeasurement(element, simContext);
            }
            // get rate rules
            Element rateRulesElement = appComponentsElement.getChild(XMLTags.RateRulesTag);
            if (rateRulesElement != null) {
                RateRule[] rateRules = xmlReader.getRateRules(simContext, rateRulesElement);
                simContext.setRateRules(rateRules);
            }
            // get reaction rule specs
            Element reactionRuleSpecsElement = appComponentsElement.getChild(XMLTags.ReactionRuleSpecsTag);
            if (reactionRuleSpecsElement != null) {
                ReactionRuleSpec[] reactionRuleSpecs = xmlReader.getReactionRuleSpecs(simContext, reactionRuleSpecsElement);
                simContext.getReactionContext().setReactionRuleSpecs(reactionRuleSpecs);
            }
        }
    } catch (XmlParseException e) {
        e.printStackTrace(System.out);
        throw new DataAccessException("Error retrieving bioevents : " + e.getMessage());
    }
}
Also used : ReactionRuleSpec(cbit.vcell.mapping.ReactionRuleSpec) Element(org.jdom.Element) XmlReader(cbit.vcell.xml.XmlReader) XmlParseException(cbit.vcell.xml.XmlParseException) SimulationContextParameter(cbit.vcell.mapping.SimulationContext.SimulationContextParameter) SpatialObject(cbit.vcell.mapping.spatial.SpatialObject) SpatialProcess(cbit.vcell.mapping.spatial.processes.SpatialProcess) RateRule(cbit.vcell.mapping.RateRule) BioEvent(cbit.vcell.mapping.BioEvent) DataAccessException(org.vcell.util.DataAccessException) NetworkConstraints(org.vcell.model.rbm.NetworkConstraints)

Example 7 with RateRule

use of cbit.vcell.mapping.RateRule in project vcell by virtualcell.

the class ModelUnitConverter method createBioModelWithNewUnitSystem.

public static BioModel createBioModelWithNewUnitSystem(BioModel oldBioModel, ModelUnitSystem newUnitSystem) throws ExpressionException, XmlParseException {
    // new BioModel has new unit system applied to all built-in units ... but expressions still need to be corrected (see below).
    BioModel newBioModel = XmlHelper.cloneBioModelWithNewUnitSystem(oldBioModel, newUnitSystem);
    Model newModel = newBioModel.getModel();
    Model oldModel = oldBioModel.getModel();
    for (Parameter p : newBioModel.getModel().getModelParameters()) {
        convertVarsWithUnitFactors(oldBioModel.getModel(), newBioModel.getModel(), p);
    }
    for (ReactionStep reactionStep : newBioModel.getModel().getReactionSteps()) {
        SymbolTable oldSymbolTable = oldBioModel.getModel().getReactionStep(reactionStep.getName());
        SymbolTable newSymbolTable = reactionStep;
        for (Parameter p : reactionStep.getKinetics().getUnresolvedParameters()) {
            convertVarsWithUnitFactors(oldSymbolTable, newSymbolTable, p);
        }
        for (Parameter p : reactionStep.getKinetics().getKineticsParameters()) {
            convertVarsWithUnitFactors(oldSymbolTable, newSymbolTable, p);
        }
    }
    for (ReactionRule reactionRule : newBioModel.getModel().getRbmModelContainer().getReactionRuleList()) {
        SymbolTable oldSymbolTable = oldBioModel.getModel().getRbmModelContainer().getReactionRule(reactionRule.getName()).getKineticLaw().getScopedSymbolTable();
        SymbolTable newSymbolTable = reactionRule.getKineticLaw().getScopedSymbolTable();
        for (Parameter p : reactionRule.getKineticLaw().getUnresolvedParameters()) {
            convertVarsWithUnitFactors(oldSymbolTable, newSymbolTable, p);
        }
        for (Parameter p : reactionRule.getKineticLaw().getLocalParameters()) {
            convertVarsWithUnitFactors(oldSymbolTable, newSymbolTable, p);
        }
    }
    for (SimulationContext simContext : newBioModel.getSimulationContexts()) {
        SimulationContext oldSimContext = oldBioModel.getSimulationContext(simContext.getName());
        // ArrayList<Parameter> parameterList = new ArrayList<Parameter>();
        for (StructureMapping mapping : simContext.getGeometryContext().getStructureMappings()) {
            Structure oldStructure = oldModel.getStructure(mapping.getStructure().getName());
            StructureMapping oldMapping = oldSimContext.getGeometryContext().getStructureMapping(oldStructure);
            for (Parameter p : mapping.computeApplicableParameterList()) {
                convertVarsWithUnitFactors(oldMapping, mapping, p);
            }
        }
        for (SpeciesContextSpec spec : simContext.getReactionContext().getSpeciesContextSpecs()) {
            SpeciesContext oldSpeciesContext = oldModel.getSpeciesContext(spec.getSpeciesContext().getName());
            SpeciesContextSpec oldSpec = oldSimContext.getReactionContext().getSpeciesContextSpec(oldSpeciesContext);
            for (Parameter p : spec.computeApplicableParameterList()) {
                convertVarsWithUnitFactors(oldSpec, spec, p);
            }
        }
        for (int i = 0; i < simContext.getElectricalStimuli().length; i++) {
            ElectricalStimulus newElectricalStimulus = simContext.getElectricalStimuli()[i];
            ElectricalStimulus oldElectricalStimulus = oldSimContext.getElectricalStimuli()[i];
            for (Parameter p : newElectricalStimulus.getParameters()) {
                convertVarsWithUnitFactors(oldElectricalStimulus.getNameScope().getScopedSymbolTable(), newElectricalStimulus.getNameScope().getScopedSymbolTable(), p);
            }
        }
        // convert events : trigger and delay parameters and event assignments
        for (int i = 0; simContext.getBioEvents() != null && oldSimContext.getBioEvents() != null && i < simContext.getBioEvents().length; i++) {
            BioEvent newBioEvent = simContext.getBioEvents()[i];
            BioEvent oldBioEvent = oldSimContext.getBioEvent(newBioEvent.getName());
            for (Parameter p : newBioEvent.getEventParameters()) {
                convertVarsWithUnitFactors(oldBioEvent.getNameScope().getScopedSymbolTable(), newBioEvent.getNameScope().getScopedSymbolTable(), p);
            }
            // for each event assignment expression
            for (int e = 0; e < newBioEvent.getEventAssignments().size(); e++) {
                ScopedSymbolTable newSymbolTable = newBioEvent.getNameScope().getScopedSymbolTable();
                ScopedSymbolTable oldSymbolTable = oldBioEvent.getNameScope().getScopedSymbolTable();
                EventAssignment newEventAssignment = newBioEvent.getEventAssignments().get(e);
                EventAssignment oldEventAssignment = oldBioEvent.getEventAssignments().get(e);
                VCUnitDefinition oldTargetUnit = oldEventAssignment.getTarget().getUnitDefinition();
                VCUnitDefinition newTargetUnit = newEventAssignment.getTarget().getUnitDefinition();
                Expression eventAssgnExpr = newEventAssignment.getAssignmentExpression();
                convertExprWithUnitFactors(oldSymbolTable, newSymbolTable, oldTargetUnit, newTargetUnit, eventAssgnExpr);
            }
        }
        /**
         * @TODO: If rate rule variable unit is TBD, we still need to handle the rate expression unit.
         */
        // convert rate rules
        RateRule[] rateRules = simContext.getRateRules();
        if (rateRules != null && rateRules.length > 0) {
            for (RateRule rateRule : rateRules) {
                RateRule oldRateRule = oldSimContext.getRateRule(rateRule.getName());
                ScopedSymbolTable oldSymbolTable = oldRateRule.getSimulationContext();
                ScopedSymbolTable newSymbolTable = rateRule.getSimulationContext();
                VCUnitDefinition oldTargetUnit = oldRateRule.getRateRuleVar().getUnitDefinition();
                VCUnitDefinition newTargetUnit = rateRule.getRateRuleVar().getUnitDefinition();
                Expression rateRuleExpr = rateRule.getRateRuleExpression();
                convertExprWithUnitFactors(oldSymbolTable, newSymbolTable, oldTargetUnit, newTargetUnit, rateRuleExpr);
            }
        }
    }
    // end  for - simulationContext
    return newBioModel;
}
Also used : ReactionRule(cbit.vcell.model.ReactionRule) EventAssignment(cbit.vcell.mapping.BioEvent.EventAssignment) ScopedSymbolTable(cbit.vcell.parser.ScopedSymbolTable) SymbolTable(cbit.vcell.parser.SymbolTable) SpeciesContext(cbit.vcell.model.SpeciesContext) SimulationContext(cbit.vcell.mapping.SimulationContext) SpeciesContextSpec(cbit.vcell.mapping.SpeciesContextSpec) StructureMapping(cbit.vcell.mapping.StructureMapping) ElectricalStimulus(cbit.vcell.mapping.ElectricalStimulus) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression) ReactionStep(cbit.vcell.model.ReactionStep) Model(cbit.vcell.model.Model) Parameter(cbit.vcell.model.Parameter) RateRule(cbit.vcell.mapping.RateRule) ScopedSymbolTable(cbit.vcell.parser.ScopedSymbolTable) BioEvent(cbit.vcell.mapping.BioEvent) Structure(cbit.vcell.model.Structure)

Example 8 with RateRule

use of cbit.vcell.mapping.RateRule in project vcell by virtualcell.

the class XmlReader method getRateRules.

public RateRule[] getRateRules(SimulationContext simContext, Element rateRulesElement) throws XmlParseException {
    Iterator<Element> rateRulesIterator = rateRulesElement.getChildren(XMLTags.RateRuleTag, vcNamespace).iterator();
    Vector<RateRule> rateRulesVector = new Vector<RateRule>();
    while (rateRulesIterator.hasNext()) {
        Element rrElement = (Element) rateRulesIterator.next();
        RateRule newRateRule = null;
        try {
            String rrName = unMangle(rrElement.getAttributeValue(XMLTags.NameAttrTag));
            String varname = rrElement.getAttributeValue(XMLTags.RateRuleVariableAttrTag);
            SymbolTableEntry rrVar = simContext.getEntry(varname);
            Expression rrExp = unMangleExpression(rrElement.getText());
            newRateRule = new RateRule(rrName, rrVar, rrExp, simContext);
            newRateRule.bind();
        } catch (ExpressionBindingException e) {
            e.printStackTrace(System.out);
            throw new XmlParseException(e.getMessage());
        }
        if (newRateRule != null) {
            rateRulesVector.add(newRateRule);
        }
    }
    return ((RateRule[]) BeanUtils.getArray(rateRulesVector, RateRule.class));
}
Also used : SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) Expression(cbit.vcell.parser.Expression) Element(org.jdom.Element) RateRule(cbit.vcell.mapping.RateRule) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) Vector(java.util.Vector)

Example 9 with RateRule

use of cbit.vcell.mapping.RateRule in project vcell by virtualcell.

the class SimContextTable method getAppComponentsForDatabase.

/**
 * getXMLStringForDatabase : this returns the XML string for the container element <AppComponents> for application-related protocols
 * and other extra specifications. For now, BioEvents falls under this category, so the BioEvents element (list of bioevents)
 * is obtained from the simContext (via the XMLProducer) and added as content to <AppComponents> element. The <AppComponents>
 * element is converted to XML string which is the return value of this method. This string is stored in the database in the
 * SimContextTable. Instead of creating new fields for each possible application component, it is convenient to store them
 * all under a blanket XML element <AppComponents>.
 * @param simContext
 * @return
 */
public static String getAppComponentsForDatabase(SimulationContext simContext) {
    Element appComponentsElement = new Element(XMLTags.ApplicationComponents);
    // for now, create the element only if application is stochastic. Can change it later.
    if (simContext.isStoch()) {
        // add 'randomizeInitCondition' flag only if simContext is non-spatial
        if (simContext.getGeometry().getDimension() == 0) {
            Element appRelatedFlagsElement = new Element(XMLTags.ApplicationSpecificFlagsTag);
            if (simContext.isRandomizeInitCondition()) {
                appRelatedFlagsElement.setAttribute(XMLTags.RandomizeInitConditionTag, "true");
            } else {
                appRelatedFlagsElement.setAttribute(XMLTags.RandomizeInitConditionTag, "false");
            }
            appComponentsElement.addContent(appRelatedFlagsElement);
        }
    }
    if (simContext.isInsufficientIterations()) {
        appComponentsElement.setAttribute(XMLTags.InsufficientIterationsTag, "true");
    } else {
        appComponentsElement.setAttribute(XMLTags.InsufficientIterationsTag, "false");
    }
    if (simContext.isInsufficientMaxMolecules()) {
        appComponentsElement.setAttribute(XMLTags.InsufficientMaxMoleculesTag, "true");
    } else {
        appComponentsElement.setAttribute(XMLTags.InsufficientMaxMoleculesTag, "false");
    }
    Xmlproducer xmlProducer = new Xmlproducer(false);
    NetworkConstraints constraints = simContext.getNetworkConstraints();
    if (constraints != null) {
        appComponentsElement.addContent(xmlProducer.getXML(constraints));
    }
    // first fill in bioevents from simContext
    BioEvent[] bioEvents = simContext.getBioEvents();
    if (bioEvents != null && bioEvents.length > 0) {
        try {
            Element bioEventsElement = xmlProducer.getXML(bioEvents);
            appComponentsElement.addContent(bioEventsElement);
        } catch (XmlParseException e) {
            e.printStackTrace(System.out);
            throw new RuntimeException("Error generating XML for bioevents : " + e.getMessage());
        }
    }
    SimulationContextParameter[] appParams = simContext.getSimulationContextParameters();
    if (appParams != null && appParams.length > 0) {
        try {
            Element appParamsElement = xmlProducer.getXML(appParams);
            appComponentsElement.addContent(appParamsElement);
        } catch (Exception e) {
            e.printStackTrace(System.out);
            throw new RuntimeException("Error generating XML for application parameters : " + e.getMessage());
        }
    }
    SpatialObject[] spatialObjects = simContext.getSpatialObjects();
    if (spatialObjects != null && spatialObjects.length > 0) {
        try {
            Element spatialObjectsElement = xmlProducer.getXML(spatialObjects);
            appComponentsElement.addContent(spatialObjectsElement);
        } catch (XmlParseException e) {
            e.printStackTrace(System.out);
            throw new RuntimeException("Error generating XML for spatialObjects : " + e.getMessage());
        }
    }
    SpatialProcess[] spatialProcesses = simContext.getSpatialProcesses();
    if (spatialProcesses != null && spatialProcesses.length > 0) {
        try {
            Element spatialProcessesElement = xmlProducer.getXML(spatialProcesses);
            appComponentsElement.addContent(spatialProcessesElement);
        } catch (XmlParseException e) {
            e.printStackTrace(System.out);
            throw new RuntimeException("Error generating XML for spatialProcesses : " + e.getMessage());
        }
    }
    // microscope measurements
    Element element = xmlProducer.getXML(simContext.getMicroscopeMeasurement());
    appComponentsElement.addContent(element);
    // rate rules
    RateRule[] rateRules = simContext.getRateRules();
    if (rateRules != null && rateRules.length > 0) {
        try {
            Element rateRulesElement = xmlProducer.getXML(rateRules);
            appComponentsElement.addContent(rateRulesElement);
        } catch (XmlParseException e) {
            e.printStackTrace(System.out);
            throw new RuntimeException("Error generating XML for bioevents : " + e.getMessage());
        }
    }
    // ReactionRuleSpecs
    ReactionRuleSpec[] reactionRuleSpecs = simContext.getReactionContext().getReactionRuleSpecs();
    if (reactionRuleSpecs != null && reactionRuleSpecs.length > 0) {
        Element reactionRuleSpecsElement = xmlProducer.getXML(reactionRuleSpecs);
        appComponentsElement.addContent(reactionRuleSpecsElement);
    }
    String appComponentsXMLStr = null;
    if (appComponentsElement.getContent() != null) {
        appComponentsXMLStr = XmlUtil.xmlToString(appComponentsElement);
    }
    return appComponentsXMLStr;
}
Also used : Xmlproducer(cbit.vcell.xml.Xmlproducer) ReactionRuleSpec(cbit.vcell.mapping.ReactionRuleSpec) Element(org.jdom.Element) XmlParseException(cbit.vcell.xml.XmlParseException) SimulationContextParameter(cbit.vcell.mapping.SimulationContext.SimulationContextParameter) PropertyVetoException(java.beans.PropertyVetoException) SQLException(java.sql.SQLException) XmlParseException(cbit.vcell.xml.XmlParseException) DataAccessException(org.vcell.util.DataAccessException) SpatialObject(cbit.vcell.mapping.spatial.SpatialObject) SpatialProcess(cbit.vcell.mapping.spatial.processes.SpatialProcess) RateRule(cbit.vcell.mapping.RateRule) BioEvent(cbit.vcell.mapping.BioEvent) NetworkConstraints(org.vcell.model.rbm.NetworkConstraints)

Aggregations

RateRule (cbit.vcell.mapping.RateRule)9 BioEvent (cbit.vcell.mapping.BioEvent)5 Element (org.jdom.Element)5 SimulationContextParameter (cbit.vcell.mapping.SimulationContext.SimulationContextParameter)4 SpatialObject (cbit.vcell.mapping.spatial.SpatialObject)4 SpatialProcess (cbit.vcell.mapping.spatial.processes.SpatialProcess)4 NetworkConstraints (org.vcell.model.rbm.NetworkConstraints)4 ReactionRuleSpec (cbit.vcell.mapping.ReactionRuleSpec)3 SimulationContext (cbit.vcell.mapping.SimulationContext)3 Expression (cbit.vcell.parser.Expression)3 PropertyVetoException (java.beans.PropertyVetoException)3 ElectricalStimulus (cbit.vcell.mapping.ElectricalStimulus)2 StructureMapping (cbit.vcell.mapping.StructureMapping)2 AnalysisTask (cbit.vcell.modelopt.AnalysisTask)2 ParameterEstimationTask (cbit.vcell.modelopt.ParameterEstimationTask)2 ExpressionBindingException (cbit.vcell.parser.ExpressionBindingException)2 AnnotatedFunction (cbit.vcell.solver.AnnotatedFunction)2 XmlParseException (cbit.vcell.xml.XmlParseException)2 ImageException (cbit.image.ImageException)1 DataContext (cbit.vcell.data.DataContext)1