Search in sources :

Example 6 with AssignmentRule

use of org.sbml.jsbml.AssignmentRule in project vcell by virtualcell.

the class SBMLExporter method addParameters.

/**
 * addKineticParameterUnits:
 * @throws SbmlException
 */
// private void addKineticAndGlobalParameterUnits() throws SbmlException {
// 
// //
// // Get all kinetic parameters from simple reactions and flux reactions from the Biomodel
// // And all Model (global) parameters from Model.
// // For each parameter,
// //		get its unit (VCunitDefinition)
// //		check if it is part of unitsList - if so, continue
// //		check if it is a base unit - if so, continue
// //		else, get the converted unit (VC -> SBML)
// //		add unit to sbmlModel unit definition
// //
// 
// Vector<Parameter> paramsVector = new Vector<Parameter>();
// // Add globals
// Model vcModel = vcBioModel.getModel();
// ModelParameter[] globalParams = vcModel.getModelParameters();
// for (int i = 0; i < globalParams.length; i++) {
// paramsVector.addElement(globalParams[i]);
// }
// // Add reaction kinetic parameters
// ReactionStep[] vcReactions = vcModel.getReactionSteps();
// for (int i = 0; i < vcReactions.length; i++) {
// Kinetics rxnKinetics = vcReactions[i].getKinetics();
// Parameter[] kineticParams = rxnKinetics.getKineticsParameters();
// for (int j = 0; j < kineticParams.length; j++) {
// paramsVector.addElement(kineticParams[j]);
// }
// }
// 
// for (int i = 0; i < paramsVector.size(); i++){
// Parameter param = (Parameter)paramsVector.elementAt(i);
// VCUnitDefinition paramUnitDefn = param.getUnitDefinition();
// if (paramUnitDefn == null || paramUnitDefn.isTBD()) {
// continue;
// }
// String unitSymbol = org.vcell.util.TokenMangler.mangleToSName(paramUnitDefn.getSymbol());
// if (unitSymbol == null) {
// continue;
// }
// getOrCreateSBMLUnit(paramUnitDefn);
// }
// }
/**
 * At present, the Virtual cell doesn't support global parameters
 * @throws SbmlException
 */
protected void addParameters() throws ExpressionException, SbmlException {
    Model vcModel = getSelectedSimContext().getModel();
    // add VCell global parameters to the SBML listofParameters
    ModelParameter[] vcGlobalParams = vcModel.getModelParameters();
    if (vcGlobalParams != null) {
        for (ModelParameter vcParam : vcGlobalParams) {
            org.sbml.jsbml.Parameter sbmlParam = sbmlModel.createParameter();
            sbmlParam.setId(vcParam.getName());
            sbmlParam.setConstant(vcParam.isConstant());
            Expression paramExpr = new Expression(vcParam.getExpression());
            boolean bParamIsNumeric = true;
            if (paramExpr.isNumeric()) {
                // For a VCell global param, if it is numeric, it has a constant value and is not defined by a rule, hence set Constant = true.
                sbmlParam.setValue(paramExpr.evaluateConstant());
                // the expression for modelParam might be numeric, but modelParam could have a rate rule, if so, set constant attribute to 'false'
                if (getSelectedSimContext().getRateRule(vcParam) != null) {
                    bParamIsNumeric = false;
                }
            } else {
                // non-numeric VCell global parameter will be defined by a (assignment) rule, hence mark Constant = false.
                bParamIsNumeric = false;
                // add assignment rule for param
                ASTNode paramFormulaNode = getFormulaFromExpression(paramExpr);
                AssignmentRule sbmlParamAssignmentRule = sbmlModel.createAssignmentRule();
                sbmlParamAssignmentRule.setVariable(vcParam.getName());
                sbmlParamAssignmentRule.setMath(paramFormulaNode);
            }
            sbmlParam.setConstant(bParamIsNumeric);
            VCUnitDefinition vcParamUnit = vcParam.getUnitDefinition();
            if (!vcParamUnit.isTBD()) {
                sbmlParam.setUnits(getOrCreateSBMLUnit(vcParamUnit));
            }
        }
    }
}
Also used : ModelParameter(cbit.vcell.model.Model.ModelParameter) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression) AssignmentRule(org.sbml.jsbml.AssignmentRule) Model(cbit.vcell.model.Model) BioModel(cbit.vcell.biomodel.BioModel) ASTNode(org.sbml.jsbml.ASTNode)

Example 7 with AssignmentRule

use of org.sbml.jsbml.AssignmentRule in project vcell by virtualcell.

the class SBMLExporter method createSBMLParamFromSpeciesParam.

/**
 * createSBMLParamFromSpeciesParam : creates an SBML parameter for each speciesContextSpecParameter (diffusion coefficient,
 * advection coeffs, boundary conditions (X,Y,Z).
 *
 * @param spContext
 * @param scsParam
 * @return
 * @throws SbmlException
 */
org.sbml.jsbml.Parameter createSBMLParamFromSpeciesParam(SpeciesContext spContext, SpeciesContextSpecParameter scsParam) throws SbmlException {
    try {
        Expression paramExpr = scsParam.getExpression();
        // if scsParam is diff, Vel X, Y, Z parameter and if its expression is null or 0.0, don't create parameter.
        int role = scsParam.getRole();
        if (((role == SpeciesContextSpec.ROLE_DiffusionRate) || (role == SpeciesContextSpec.ROLE_VelocityX) || (role == SpeciesContextSpec.ROLE_VelocityY) || (role == SpeciesContextSpec.ROLE_VelocityZ)) && ((paramExpr == null) || (paramExpr.isNumeric() && (scsParam.getConstantValue() == 0.0)))) {
            return null;
        }
        // if scsParam is a BoundaryCondition, and paramExpr is null, values are set based on boundary condition type.
        if (((role == SpeciesContextSpec.ROLE_BoundaryValueXm) || (role == SpeciesContextSpec.ROLE_BoundaryValueXp) || (role == SpeciesContextSpec.ROLE_BoundaryValueYm) || (role == SpeciesContextSpec.ROLE_BoundaryValueYp) || (role == SpeciesContextSpec.ROLE_BoundaryValueZm) || (role == SpeciesContextSpec.ROLE_BoundaryValueZp)) && (paramExpr == null)) {
            StructureMapping sm = getSelectedSimContext().getGeometryContext().getStructureMapping(spContext.getStructure());
            Expression initCondnExpr = getSelectedSimContext().getReactionContext().getSpeciesContextSpec(spContext).getInitialConditionParameter().getExpression();
            // if BC type is Neumann (flux), its value is 0.0
            if ((role == SpeciesContextSpec.ROLE_BoundaryValueXm)) {
                if (sm.getBoundaryConditionTypeXm().isDIRICHLET()) {
                    paramExpr = new Expression(initCondnExpr);
                } else if (sm.getBoundaryConditionTypeXm().isNEUMANN()) {
                    paramExpr = new Expression(0.0);
                }
            }
            if ((role == SpeciesContextSpec.ROLE_BoundaryValueXp)) {
                if (sm.getBoundaryConditionTypeXp().isDIRICHLET()) {
                    paramExpr = new Expression(initCondnExpr);
                } else if (sm.getBoundaryConditionTypeXp().isNEUMANN()) {
                    paramExpr = new Expression(0.0);
                }
            }
            if ((role == SpeciesContextSpec.ROLE_BoundaryValueYm)) {
                if (sm.getBoundaryConditionTypeYm().isDIRICHLET()) {
                    paramExpr = new Expression(initCondnExpr);
                } else if (sm.getBoundaryConditionTypeYm().isNEUMANN()) {
                    paramExpr = new Expression(0.0);
                }
            }
            if ((role == SpeciesContextSpec.ROLE_BoundaryValueYp)) {
                if (sm.getBoundaryConditionTypeYp().isDIRICHLET()) {
                    paramExpr = new Expression(initCondnExpr);
                } else if (sm.getBoundaryConditionTypeYp().isNEUMANN()) {
                    paramExpr = new Expression(0.0);
                }
            }
            if ((role == SpeciesContextSpec.ROLE_BoundaryValueZm)) {
                if (sm.getBoundaryConditionTypeZm().isDIRICHLET()) {
                    paramExpr = new Expression(initCondnExpr);
                } else if (sm.getBoundaryConditionTypeZm().isNEUMANN()) {
                    paramExpr = new Expression(0.0);
                }
            }
            if ((role == SpeciesContextSpec.ROLE_BoundaryValueZp)) {
                if (sm.getBoundaryConditionTypeZp().isDIRICHLET()) {
                    paramExpr = new Expression(initCondnExpr);
                } else if (sm.getBoundaryConditionTypeZp().isNEUMANN()) {
                    paramExpr = new Expression(0.0);
                }
            }
        }
        // create SBML parameter
        org.sbml.jsbml.Parameter param = sbmlModel.createParameter();
        param.setId(TokenMangler.mangleToSName(spContext.getName() + "_" + scsParam.getName()));
        UnitDefinition unitDefn = getOrCreateSBMLUnit(scsParam.getUnitDefinition());
        param.setUnits(unitDefn);
        param.setConstant(scsParam.isConstant());
        if (paramExpr.isNumeric()) {
            param.setValue(paramExpr.evaluateConstant());
            param.setConstant(true);
        } else {
            // we need to create a parameter and a rule for the non-numeric expr of diffParam
            param.setValue(0.0);
            param.setConstant(false);
            // now add assignment rule in SBML for the diff param
            ASTNode assgnRuleMathNode = getFormulaFromExpression(paramExpr);
            AssignmentRule assgnRule = sbmlModel.createAssignmentRule();
            assgnRule.setVariable(param.getId());
            assgnRule.setMath(assgnRuleMathNode);
        }
        return param;
    } catch (ExpressionException e) {
        e.printStackTrace(System.out);
        throw new RuntimeException("Unable to interpret parameter '" + scsParam.getName() + "' of species : " + spContext.getName());
    }
}
Also used : Expression(cbit.vcell.parser.Expression) AssignmentRule(org.sbml.jsbml.AssignmentRule) ASTNode(org.sbml.jsbml.ASTNode) StructureMapping(cbit.vcell.mapping.StructureMapping) InteriorPoint(org.sbml.jsbml.ext.spatial.InteriorPoint) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) UnitDefinition(org.sbml.jsbml.UnitDefinition) ExpressionException(cbit.vcell.parser.ExpressionException)

Aggregations

AssignmentRule (org.sbml.jsbml.AssignmentRule)7 Expression (cbit.vcell.parser.Expression)6 ASTNode (org.sbml.jsbml.ASTNode)6 InteriorPoint (org.sbml.jsbml.ext.spatial.InteriorPoint)6 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)5 BioModel (cbit.vcell.biomodel.BioModel)4 StructureMapping (cbit.vcell.mapping.StructureMapping)4 Model (cbit.vcell.model.Model)4 ExpressionException (cbit.vcell.parser.ExpressionException)4 UnitDefinition (org.sbml.jsbml.UnitDefinition)4 Element (org.jdom.Element)3 Compartment (org.sbml.jsbml.Compartment)3 ImageException (cbit.image.ImageException)2 KineticsParameter (cbit.vcell.model.Kinetics.KineticsParameter)2 ModelParameter (cbit.vcell.model.Model.ModelParameter)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 InitialAssignment (org.sbml.jsbml.InitialAssignment)2 RateRule (org.sbml.jsbml.RateRule)2 MembraneMapping (cbit.vcell.mapping.MembraneMapping)1 LocalParameter (cbit.vcell.mapping.ParameterContext.LocalParameter)1