Search in sources :

Example 1 with ParameterRoleEnum

use of cbit.vcell.mapping.ParameterContext.ParameterRoleEnum in project vcell by virtualcell.

the class XmlReader method readParameters.

private void readParameters(List<Element> parameterElements, ParameterContext parameterContext, HashMap<String, ParameterRoleEnum> roleHash, ParameterRoleEnum userDefinedRole, HashSet<String> xmlRolesTagsToIgnore, Model model) throws XmlParseException {
    String contextName = parameterContext.getNameScope().getName();
    try {
        // 
        // prepopulate varHash with reserved symbols
        // 
        VariableHash varHash = new VariableHash();
        addResevedSymbols(varHash, model);
        // 
        for (Element xmlParam : parameterElements) {
            String parsedParamName = unMangle(xmlParam.getAttributeValue(XMLTags.NameAttrTag));
            String parsedRoleString = xmlParam.getAttributeValue(XMLTags.ParamRoleAttrTag);
            String parsedExpressionString = xmlParam.getText();
            // 
            if (xmlRolesTagsToIgnore.contains(parsedRoleString)) {
                varHash.removeVariable(parsedParamName);
                continue;
            }
            Expression paramExp = null;
            if (parsedExpressionString.trim().length() > 0) {
                paramExp = unMangleExpression(parsedExpressionString);
            }
            if (varHash.getVariable(parsedParamName) == null) {
                Domain domain = null;
                varHash.addVariable(new Function(parsedParamName, paramExp, domain));
            } else {
                if (model.getReservedSymbolByName(parsedParamName) != null) {
                    varHash.removeVariable(parsedParamName);
                    Domain domain = null;
                    varHash.addVariable(new Function(parsedParamName, paramExp, domain));
                }
            }
            // 
            // get the parameter for this xml role string
            // 
            ParameterRoleEnum paramRole = roleHash.get(parsedRoleString);
            if (paramRole == null) {
                throw new XmlParseException("parameter '" + parsedParamName + "' has unexpected role '" + parsedRoleString + "' in '" + contextName + "'");
            }
            // 
            if (paramRole != userDefinedRole) {
                LocalParameter paramWithSameRole = parameterContext.getLocalParameterFromRole(paramRole);
                if (paramWithSameRole == null) {
                    throw new XmlParseException("can't find parameter with role '" + parsedRoleString + "' in '" + contextName + "'");
                }
                // 
                if (!paramWithSameRole.getName().equals(parsedParamName)) {
                    // 
                    // first rename other parameters with same name
                    // 
                    LocalParameter paramWithSameNameButDifferentRole = parameterContext.getLocalParameterFromName(parsedParamName);
                    if (paramWithSameNameButDifferentRole != null) {
                        // 
                        // find available name
                        // 
                        int n = 0;
                        String newName = parsedParamName + "_" + n++;
                        while (parameterContext.getEntry(newName) != null) {
                            newName = parsedParamName + "_" + n++;
                        }
                        parameterContext.renameLocalParameter(parsedParamName, newName);
                    }
                    // 
                    // then rename parameter with correct role
                    // 
                    parameterContext.renameLocalParameter(paramWithSameRole.getName(), parsedParamName);
                }
            }
        }
        // 
        // create unresolved parameters for all unresolved symbols
        // 
        String unresolvedSymbol = varHash.getFirstUnresolvedSymbol();
        while (unresolvedSymbol != null) {
            try {
                Domain domain = null;
                // will turn into an UnresolvedParameter.
                varHash.addVariable(new Function(unresolvedSymbol, new Expression(0.0), domain));
            } catch (MathException e) {
                e.printStackTrace(System.out);
                throw new XmlParseException(e.getMessage());
            }
            parameterContext.addUnresolvedParameter(unresolvedSymbol);
            unresolvedSymbol = varHash.getFirstUnresolvedSymbol();
        }
        // 
        // in topological order, add parameters to model (getting units also).
        // note that all pre-defined parameters already have the correct names
        // here we set expressions on pre-defined parameters and add user-defined parameters
        // 
        Variable[] sortedVariables = varHash.getTopologicallyReorderedVariables();
        ModelUnitSystem modelUnitSystem = model.getUnitSystem();
        for (int i = sortedVariables.length - 1; i >= 0; i--) {
            if (sortedVariables[i] instanceof Function) {
                Function paramFunction = (Function) sortedVariables[i];
                Element xmlParam = null;
                for (int j = 0; j < parameterElements.size(); j++) {
                    Element tempParam = (Element) parameterElements.get(j);
                    if (paramFunction.getName().equals(unMangle(tempParam.getAttributeValue(XMLTags.NameAttrTag)))) {
                        xmlParam = tempParam;
                        break;
                    }
                }
                if (xmlParam == null) {
                    // must have been an unresolved parameter
                    continue;
                }
                String symbol = xmlParam.getAttributeValue(XMLTags.VCUnitDefinitionAttrTag);
                VCUnitDefinition unit = null;
                if (symbol != null) {
                    unit = modelUnitSystem.getInstance(symbol);
                }
                LocalParameter tempParam = parameterContext.getLocalParameterFromName(paramFunction.getName());
                if (tempParam == null) {
                    tempParam = parameterContext.addLocalParameter(paramFunction.getName(), new Expression(0.0), userDefinedRole, unit, userDefinedRole.getDescription());
                    parameterContext.setParameterValue(tempParam, paramFunction.getExpression(), true);
                } else {
                    if (tempParam.getExpression() != null) {
                        // if the expression is null, it should remain null.
                        parameterContext.setParameterValue(tempParam, paramFunction.getExpression(), true);
                    }
                    tempParam.setUnitDefinition(unit);
                }
            }
        }
    } catch (PropertyVetoException | ExpressionException | MathException e) {
        e.printStackTrace(System.out);
        throw new XmlParseException("Exception while setting parameters for '" + contextName + "': " + e.getMessage(), e);
    }
}
Also used : FilamentVariable(cbit.vcell.math.FilamentVariable) OutsideVariable(cbit.vcell.math.OutsideVariable) StochVolVariable(cbit.vcell.math.StochVolVariable) RandomVariable(cbit.vcell.math.RandomVariable) VolumeRandomVariable(cbit.vcell.math.VolumeRandomVariable) VolumeParticleVariable(cbit.vcell.math.VolumeParticleVariable) VolumeRegionVariable(cbit.vcell.math.VolumeRegionVariable) InsideVariable(cbit.vcell.math.InsideVariable) VolVariable(cbit.vcell.math.VolVariable) MembraneRegionVariable(cbit.vcell.math.MembraneRegionVariable) PointVariable(cbit.vcell.math.PointVariable) MembraneRandomVariable(cbit.vcell.math.MembraneRandomVariable) MembraneParticleVariable(cbit.vcell.math.MembraneParticleVariable) ParticleVariable(cbit.vcell.math.ParticleVariable) MemVariable(cbit.vcell.math.MemVariable) FilamentRegionVariable(cbit.vcell.math.FilamentRegionVariable) Variable(cbit.vcell.math.Variable) VariableHash(cbit.vcell.math.VariableHash) Element(org.jdom.Element) ExpressionException(cbit.vcell.parser.ExpressionException) LocalParameter(cbit.vcell.mapping.ParameterContext.LocalParameter) PropertyVetoException(java.beans.PropertyVetoException) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction) Function(cbit.vcell.math.Function) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression) MathException(cbit.vcell.math.MathException) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) FilamentSubDomain(cbit.vcell.math.FilamentSubDomain) PointSubDomain(cbit.vcell.math.PointSubDomain) Domain(cbit.vcell.math.Variable.Domain) ParameterRoleEnum(cbit.vcell.mapping.ParameterContext.ParameterRoleEnum) ModelUnitSystem(cbit.vcell.model.ModelUnitSystem)

Example 2 with ParameterRoleEnum

use of cbit.vcell.mapping.ParameterContext.ParameterRoleEnum in project vcell by virtualcell.

the class XmlReader method getRbmReactionRule.

private ReactionRule getRbmReactionRule(Element reactionRuleElement, Model newModel) throws XmlParseException {
    String n = reactionRuleElement.getAttributeValue(XMLTags.NameAttrTag);
    if (n == null || n.isEmpty()) {
        System.out.println("XMLReader: getRbmReactionRule: name is missing.");
        return null;
    }
    try {
        boolean reversible = Boolean.valueOf(reactionRuleElement.getAttributeValue(XMLTags.RbmReactionRuleReversibleTag));
        // get 1st structure if attribute missing
        String structureName = reactionRuleElement.getAttributeValue(XMLTags.StructureAttrTag, newModel.getStructures()[0].getName());
        Structure structure = newModel.getStructure(structureName);
        ReactionRule reactionRule = new ReactionRule(newModel, n, structure, reversible);
        // we ignore this, name and label are the same thing for now
        String reactionRuleLabel = reactionRuleElement.getAttributeValue(XMLTags.RbmReactionRuleLabelTag);
        // 
        // old style kinetics placed parameter values as attributes
        // look for attributes named ("MassActionKf","MassActionKr","MichaelisMentenKcat","MichaelisMentenKm","SaturableKs","SaturableVmax")
        String[] oldKineticsAttributes = new String[] { XMLTags.RbmMassActionKfAttrTag_DEPRECATED, XMLTags.RbmMassActionKrAttrTag_DEPRECATED, XMLTags.RbmMichaelisMentenKcatAttrTag_DEPRECATED, XMLTags.RbmMichaelisMentenKmAttrTag_DEPRECATED, XMLTags.RbmSaturableKsAttrTag_DEPRECATED, XMLTags.RbmSaturableVmaxAttrTag_DEPRECATED };
        boolean bOldKineticsFound = false;
        for (String oldKineticsAttribute : oldKineticsAttributes) {
            if (reactionRuleElement.getAttribute(oldKineticsAttribute) != null) {
                bOldKineticsFound = true;
            }
        }
        if (bOldKineticsFound) {
            readOldRbmKineticsAttributes(reactionRuleElement, reactionRule);
        } else {
            Element kineticsElement = reactionRuleElement.getChild(XMLTags.KineticsTag, vcNamespace);
            if (kineticsElement != null) {
                String kineticLawTypeString = kineticsElement.getAttributeValue(XMLTags.KineticsTypeAttrTag);
                RbmKineticLaw.RateLawType rateLawType = null;
                if (XMLTags.RbmKineticTypeMassAction.equals(kineticLawTypeString)) {
                    rateLawType = RateLawType.MassAction;
                } else if (XMLTags.RbmKineticTypeMichaelisMenten.equals(kineticLawTypeString)) {
                    rateLawType = RateLawType.MichaelisMenten;
                } else if (XMLTags.RbmKineticTypeSaturable.equals(kineticLawTypeString)) {
                    rateLawType = RateLawType.Saturable;
                } else {
                    throw new RuntimeException("unexpected rate law type " + kineticLawTypeString);
                }
                reactionRule.setKineticLaw(new RbmKineticLaw(reactionRule, rateLawType));
                List<Element> parameterElements = kineticsElement.getChildren(XMLTags.ParameterTag, vcNamespace);
                HashMap<String, ParameterRoleEnum> roleHash = new HashMap<String, ParameterContext.ParameterRoleEnum>();
                roleHash.put(XMLTags.RbmMassActionKfRole, RbmKineticLawParameterType.MassActionForwardRate);
                roleHash.put(XMLTags.RbmMassActionKrRole, RbmKineticLawParameterType.MassActionReverseRate);
                roleHash.put(XMLTags.RbmMichaelisMentenKcatRole, RbmKineticLawParameterType.MichaelisMentenKcat);
                roleHash.put(XMLTags.RbmMichaelisMentenKmRole, RbmKineticLawParameterType.MichaelisMentenKm);
                roleHash.put(XMLTags.RbmSaturableVmaxRole, RbmKineticLawParameterType.SaturableVmax);
                roleHash.put(XMLTags.RbmSaturableKsRole, RbmKineticLawParameterType.SaturableKs);
                roleHash.put(XMLTags.RbmUserDefinedRole, RbmKineticLawParameterType.UserDefined);
                HashSet<String> xmlRolesToIgnore = new HashSet<String>();
                xmlRolesToIgnore.add(XMLTags.RbmRuleRateRole);
                ParameterContext parameterContext = reactionRule.getKineticLaw().getParameterContext();
                readParameters(parameterElements, parameterContext, roleHash, RbmKineticLawParameterType.UserDefined, xmlRolesToIgnore, newModel);
            }
        }
        Element e1 = reactionRuleElement.getChild(XMLTags.RbmReactantPatternsListTag, vcNamespace);
        getRbmReactantPatternsList(e1, reactionRule, newModel);
        Element e2 = reactionRuleElement.getChild(XMLTags.RbmProductPatternsListTag, vcNamespace);
        getRbmProductPatternsList(e2, reactionRule, newModel);
        reactionRule.checkMatchConsistency();
        return reactionRule;
    } catch (PropertyVetoException | ExpressionException ex) {
        ex.printStackTrace(System.out);
        throw new RuntimeException("failed to parse kinetics for reaction rule '" + n + "': " + ex.getMessage(), ex);
    }
}
Also used : ReactionRule(cbit.vcell.model.ReactionRule) HashMap(java.util.HashMap) Element(org.jdom.Element) RbmKineticLaw(cbit.vcell.model.RbmKineticLaw) ExpressionException(cbit.vcell.parser.ExpressionException) PropertyVetoException(java.beans.PropertyVetoException) ParameterContext(cbit.vcell.mapping.ParameterContext) RateLawType(cbit.vcell.model.RbmKineticLaw.RateLawType) Structure(cbit.vcell.model.Structure) ParameterRoleEnum(cbit.vcell.mapping.ParameterContext.ParameterRoleEnum) HashSet(java.util.HashSet)

Example 3 with ParameterRoleEnum

use of cbit.vcell.mapping.ParameterContext.ParameterRoleEnum in project vcell by virtualcell.

the class Xmlproducer method getXML.

private Element getXML(RbmKineticLaw param) {
    Element kinetics = new Element(XMLTags.KineticsTag);
    switch(param.getRateLawType()) {
        case MassAction:
            {
                kinetics.setAttribute(XMLTags.KineticsTypeAttrTag, XMLTags.RbmKineticTypeMassAction);
                break;
            }
        case MichaelisMenten:
            {
                kinetics.setAttribute(XMLTags.KineticsTypeAttrTag, XMLTags.RbmKineticTypeMichaelisMenten);
                break;
            }
        case Saturable:
            {
                kinetics.setAttribute(XMLTags.KineticsTypeAttrTag, XMLTags.RbmKineticTypeSaturable);
                break;
            }
    }
    HashMap<ParameterRoleEnum, String> roleHash = new HashMap<ParameterRoleEnum, String>();
    roleHash.put(RbmKineticLawParameterType.MassActionForwardRate, XMLTags.RbmMassActionKfRole);
    roleHash.put(RbmKineticLawParameterType.MassActionReverseRate, XMLTags.RbmMassActionKrRole);
    roleHash.put(RbmKineticLawParameterType.MichaelisMentenKcat, XMLTags.RbmMichaelisMentenKcatRole);
    roleHash.put(RbmKineticLawParameterType.MichaelisMentenKm, XMLTags.RbmMichaelisMentenKmRole);
    roleHash.put(RbmKineticLawParameterType.SaturableVmax, XMLTags.RbmSaturableVmaxRole);
    roleHash.put(RbmKineticLawParameterType.SaturableKs, XMLTags.RbmSaturableKsRole);
    roleHash.put(RbmKineticLawParameterType.RuleRate, XMLTags.RbmRuleRateRole);
    roleHash.put(RbmKineticLawParameterType.UserDefined, XMLTags.RbmUserDefinedRole);
    // Add Kinetics Parameters
    LocalParameter[] parameters = param.getLocalParameters();
    for (int i = 0; i < parameters.length; i++) {
        LocalParameter parm = parameters[i];
        Element tempparameter = new Element(XMLTags.ParameterTag);
        // Get parameter attributes
        tempparameter.setAttribute(XMLTags.NameAttrTag, mangle(parm.getName()));
        tempparameter.setAttribute(XMLTags.ParamRoleAttrTag, roleHash.get(parm.getRole()));
        VCUnitDefinition unit = parm.getUnitDefinition();
        if (unit != null) {
            tempparameter.setAttribute(XMLTags.VCUnitDefinitionAttrTag, unit.getSymbol());
        }
        Expression expression = parm.getExpression();
        if (expression != null) {
            tempparameter.addContent(mangleExpression(expression));
        }
        // Add the parameter to the general kinetics object
        kinetics.addContent(tempparameter);
    }
    return kinetics;
}
Also used : LocalParameter(cbit.vcell.mapping.ParameterContext.LocalParameter) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) HashMap(java.util.HashMap) Expression(cbit.vcell.parser.Expression) Element(org.jdom.Element) ParameterRoleEnum(cbit.vcell.mapping.ParameterContext.ParameterRoleEnum)

Aggregations

ParameterRoleEnum (cbit.vcell.mapping.ParameterContext.ParameterRoleEnum)3 Element (org.jdom.Element)3 LocalParameter (cbit.vcell.mapping.ParameterContext.LocalParameter)2 Expression (cbit.vcell.parser.Expression)2 ExpressionException (cbit.vcell.parser.ExpressionException)2 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)2 PropertyVetoException (java.beans.PropertyVetoException)2 HashMap (java.util.HashMap)2 ParameterContext (cbit.vcell.mapping.ParameterContext)1 CompartmentSubDomain (cbit.vcell.math.CompartmentSubDomain)1 FilamentRegionVariable (cbit.vcell.math.FilamentRegionVariable)1 FilamentSubDomain (cbit.vcell.math.FilamentSubDomain)1 FilamentVariable (cbit.vcell.math.FilamentVariable)1 Function (cbit.vcell.math.Function)1 InsideVariable (cbit.vcell.math.InsideVariable)1 MathException (cbit.vcell.math.MathException)1 MemVariable (cbit.vcell.math.MemVariable)1 MembraneParticleVariable (cbit.vcell.math.MembraneParticleVariable)1 MembraneRandomVariable (cbit.vcell.math.MembraneRandomVariable)1 MembraneRegionVariable (cbit.vcell.math.MembraneRegionVariable)1