Search in sources :

Example 1 with CoordinateKind

use of org.sbml.jsbml.ext.spatial.CoordinateKind in project vcell by virtualcell.

the class SBMLImporter method addParameters.

/**
 * addParameters : Adds global parameters from SBML model to VCell model. If
 * expression for global parameter contains species, creates a conc_factor
 * parameter (conversion from SBML - VCell conc units) and adds this factor
 * to VC global params list, and replaces occurances of 'sp' with
 * 'sp*concFactor' in original param expression.
 *
 * @throws PropertyVetoException
 */
protected void addParameters() throws Exception {
    ListOf listofGlobalParams = sbmlModel.getListOfParameters();
    if (listofGlobalParams == null) {
        System.out.println("No Global Parameters");
        return;
    }
    Model vcModel = vcBioModel.getSimulationContext(0).getModel();
    ArrayList<ModelParameter> vcModelParamsList = new ArrayList<Model.ModelParameter>();
    // create a hash of reserved symbols so that if there is any reserved
    // symbol occurring as a global parameter in the SBML model,
    // the hash can be used to check for reserved symbols, so that it will
    // not be added as a global parameter in VCell,
    // since reserved symbols cannot be used as other variables (species,
    // structureSize, parameters, reactions, etc.).
    HashSet<String> reservedSymbolHash = new HashSet<String>();
    for (ReservedSymbol rs : vcModel.getReservedSymbols()) {
        reservedSymbolHash.add(rs.getName());
    }
    ModelUnitSystem modelUnitSystem = vcModel.getUnitSystem();
    for (int i = 0; i < sbmlModel.getNumParameters(); i++) {
        Parameter sbmlGlobalParam = (Parameter) listofGlobalParams.get(i);
        String paramName = sbmlGlobalParam.getId();
        SpatialParameterPlugin spplugin = null;
        if (bSpatial) {
            // check if parameter id is x/y/z : if so, check if its
            // 'spatialSymbolRef' child's spatial id and type are non-empty.
            // If so, the parameter represents a spatial element.
            // If not, throw an exception, since a parameter that does not
            // represent a spatial element cannot have an id of x/y/z
            spplugin = (SpatialParameterPlugin) sbmlGlobalParam.getPlugin(SBMLUtils.SBML_SPATIAL_NS_PREFIX);
            if (paramName.equals("x") || paramName.equals("y") || paramName.equals("z")) {
                boolean bSpatialParam = (spplugin != null && spplugin.getParamType() instanceof SpatialSymbolReference);
                // if (a) and (b) are true, continue with the next parameter
                if (!bSpatialParam) {
                    throw new RuntimeException("Parameter '" + paramName + "' is not a spatial parameter : Cannot have a variable in VCell named '" + paramName + "' unless it is a spatial variable.");
                } else {
                    // parameter to the list of vcell parameters.
                    continue;
                }
            }
        }
        // 
        // Get param value if set or get its expression from rule
        // 
        // Check if param is defined by an assignment rule or initial
        // assignment. If so, that value overrides the value existing in the
        // param element.
        // assignment rule, first
        Expression valueExpr = getValueFromAssignmentRule(paramName);
        if (valueExpr == null) {
            if (sbmlGlobalParam.isSetValue()) {
                double value = sbmlGlobalParam.getValue();
                valueExpr = new Expression(value);
            } else {
                // if value for global param is not set and param has a rate
                // rule, need to set an init value for param (else, there
                // will be a problem in reaction which uses this parameter).
                // use a 'default' initial value of '0'
                valueExpr = new Expression(0.0);
            // logger.sendMessage(VCLogger.Priority.MediumPriority,
            // VCLogger.Priority.LowPriority,
            // "Parameter did not have an initial value, but has a rate rule specified. Using a default value of 0.0.");
            }
        }
        if (valueExpr != null) {
            // valueExpr will be changed
            valueExpr = adjustExpression(valueExpr, vcModel);
        }
        // extension
        if (bSpatial) {
            VCAssert.assertTrue(spplugin != null, "invalid initialization logic");
            ParameterType sbmlParamType = spplugin.getParamType();
            SpeciesContext paramSpContext = null;
            SpeciesContextSpec vcSpContextsSpec = null;
            // Check for diffusion coefficient(s)
            if (sbmlParamType instanceof DiffusionCoefficient) {
                DiffusionCoefficient diffCoeff = (DiffusionCoefficient) sbmlParamType;
                if (diffCoeff != null && diffCoeff.isSetVariable()) {
                    // get the var of diffCoeff; find appropriate spContext
                    // in vcell; set its diff param to param value.
                    paramSpContext = vcModel.getSpeciesContext(diffCoeff.getVariable());
                    if (paramSpContext != null) {
                        vcSpContextsSpec = vcBioModel.getSimulationContext(0).getReactionContext().getSpeciesContextSpec(paramSpContext);
                        vcSpContextsSpec.getDiffusionParameter().setExpression(valueExpr);
                    }
                    // coeff parameter to the list of vcell parameters.
                    continue;
                }
            }
            // Check for advection coefficient(s)
            if (sbmlParamType instanceof AdvectionCoefficient) {
                AdvectionCoefficient advCoeff = (AdvectionCoefficient) sbmlParamType;
                if (advCoeff != null && advCoeff.isSetVariable()) {
                    // get the var of advCoeff; find appropriate spContext
                    // in vcell; set its adv param to param value.
                    paramSpContext = vcModel.getSpeciesContext(advCoeff.getVariable());
                    if (paramSpContext != null) {
                        vcSpContextsSpec = vcBioModel.getSimulationContext(0).getReactionContext().getSpeciesContextSpec(paramSpContext);
                        CoordinateKind coordKind = advCoeff.getCoordinate();
                        SpeciesContextSpecParameter param = null;
                        switch(coordKind) {
                            case cartesianX:
                                {
                                    param = vcSpContextsSpec.getParameterFromRole(SpeciesContextSpec.ROLE_VelocityX);
                                    break;
                                }
                            case cartesianY:
                                {
                                    param = vcSpContextsSpec.getParameterFromRole(SpeciesContextSpec.ROLE_VelocityY);
                                    break;
                                }
                            case cartesianZ:
                                {
                                    param = vcSpContextsSpec.getParameterFromRole(SpeciesContextSpec.ROLE_VelocityZ);
                                    break;
                                }
                        }
                        param.setExpression(valueExpr);
                    }
                    // coeff parameter to the list of vcell parameters.
                    continue;
                }
            }
            // Check for Boundary condition(s)
            if (sbmlParamType instanceof BoundaryCondition) {
                BoundaryCondition bCondn = (BoundaryCondition) sbmlParamType;
                if (bCondn != null && bCondn.isSetVariable()) {
                    // get the var of boundaryCondn; find appropriate
                    // spContext in vcell;
                    // set the BC param of its speciesContextSpec to param
                    // value.
                    paramSpContext = vcModel.getSpeciesContext(bCondn.getVariable());
                    if (paramSpContext == null) {
                        throw new RuntimeException("unable to process boundary condition for variable " + bCondn.getVariable());
                    }
                    StructureMapping sm = vcBioModel.getSimulationContext(0).getGeometryContext().getStructureMapping(paramSpContext.getStructure());
                    vcSpContextsSpec = vcBioModel.getSimulationContext(0).getReactionContext().getSpeciesContextSpec(paramSpContext);
                    for (CoordinateComponent coordComp : getSbmlGeometry().getListOfCoordinateComponents()) {
                        if (bCondn.getSpatialRef().equals(coordComp.getBoundaryMinimum().getSpatialId())) {
                            switch(coordComp.getType()) {
                                case cartesianX:
                                    {
                                        vcSpContextsSpec.getBoundaryXmParameter().setExpression(valueExpr);
                                    }
                                case cartesianY:
                                    {
                                        vcSpContextsSpec.getBoundaryYmParameter().setExpression(valueExpr);
                                    }
                                case cartesianZ:
                                    {
                                        vcSpContextsSpec.getBoundaryZmParameter().setExpression(valueExpr);
                                    }
                            }
                        }
                        if (bCondn.getSpatialRef().equals(coordComp.getBoundaryMaximum().getSpatialId())) {
                            switch(coordComp.getType()) {
                                case cartesianX:
                                    {
                                        vcSpContextsSpec.getBoundaryXpParameter().setExpression(valueExpr);
                                    }
                                case cartesianY:
                                    {
                                        vcSpContextsSpec.getBoundaryYpParameter().setExpression(valueExpr);
                                    }
                                case cartesianZ:
                                    {
                                        vcSpContextsSpec.getBoundaryZpParameter().setExpression(valueExpr);
                                    }
                            }
                        }
                    }
                    continue;
                }
            }
            // Check for Boundary condition(s)
            if (sbmlParamType instanceof SpatialSymbolReference) {
                SpatialSymbolReference spatialSymbolRef = (SpatialSymbolReference) sbmlParamType;
                throw new RuntimeException("generic Spatial Symbol References not yet supported, unresolved spatial reference '" + spatialSymbolRef.getSpatialRef() + "'");
            }
        }
        // doesn't exist.
        if (vcModel.getModelParameter(paramName) == null) {
            VCUnitDefinition glParamUnitDefn = sbmlUnitIdentifierHash.get(sbmlGlobalParam.getUnits());
            // set it to TBD or check if it was dimensionless.
            if (glParamUnitDefn == null) {
                glParamUnitDefn = modelUnitSystem.getInstance_TBD();
            }
            // VCell : cannot add reserved symbol to model params.
            if (!reservedSymbolHash.contains(paramName)) {
                ModelParameter vcGlobalParam = vcModel.new ModelParameter(paramName, valueExpr, Model.ROLE_UserDefined, glParamUnitDefn);
                if (paramName.length() > 64) {
                    // record global parameter name in annotation if it is
                    // longer than 64 characeters
                    vcGlobalParam.setDescription("Parameter Name : " + paramName);
                }
                vcModelParamsList.add(vcGlobalParam);
            }
        }
    }
    // end for - sbmlModel.parameters
    vcModel.setModelParameters(vcModelParamsList.toArray(new ModelParameter[0]));
}
Also used : ReservedSymbol(cbit.vcell.model.Model.ReservedSymbol) ArrayList(java.util.ArrayList) SpatialParameterPlugin(org.sbml.jsbml.ext.spatial.SpatialParameterPlugin) SpeciesContext(cbit.vcell.model.SpeciesContext) SpeciesContextSpec(cbit.vcell.mapping.SpeciesContextSpec) StructureMapping(cbit.vcell.mapping.StructureMapping) CoordinateKind(org.sbml.jsbml.ext.spatial.CoordinateKind) ListOf(org.sbml.jsbml.ListOf) HashSet(java.util.HashSet) ModelUnitSystem(cbit.vcell.model.ModelUnitSystem) SpeciesContextSpecParameter(cbit.vcell.mapping.SpeciesContextSpec.SpeciesContextSpecParameter) CoordinateComponent(org.sbml.jsbml.ext.spatial.CoordinateComponent) AdvectionCoefficient(org.sbml.jsbml.ext.spatial.AdvectionCoefficient) ParameterType(org.sbml.jsbml.ext.spatial.ParameterType) BioEventParameterType(cbit.vcell.mapping.BioEvent.BioEventParameterType) DiffusionCoefficient(org.sbml.jsbml.ext.spatial.DiffusionCoefficient) InteriorPoint(org.sbml.jsbml.ext.spatial.InteriorPoint) SpatialSymbolReference(org.sbml.jsbml.ext.spatial.SpatialSymbolReference) ModelParameter(cbit.vcell.model.Model.ModelParameter) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression) BoundaryCondition(org.sbml.jsbml.ext.spatial.BoundaryCondition) Model(cbit.vcell.model.Model) BioModel(cbit.vcell.biomodel.BioModel) KineticsParameter(cbit.vcell.model.Kinetics.KineticsParameter) Parameter(org.sbml.jsbml.Parameter) ModelParameter(cbit.vcell.model.Model.ModelParameter) SpeciesContextSpecParameter(cbit.vcell.mapping.SpeciesContextSpec.SpeciesContextSpecParameter) LocalParameter(org.sbml.jsbml.LocalParameter) KineticsProxyParameter(cbit.vcell.model.Kinetics.KineticsProxyParameter) UnresolvedParameter(cbit.vcell.model.Kinetics.UnresolvedParameter)

Aggregations

BioModel (cbit.vcell.biomodel.BioModel)1 BioEventParameterType (cbit.vcell.mapping.BioEvent.BioEventParameterType)1 SpeciesContextSpec (cbit.vcell.mapping.SpeciesContextSpec)1 SpeciesContextSpecParameter (cbit.vcell.mapping.SpeciesContextSpec.SpeciesContextSpecParameter)1 StructureMapping (cbit.vcell.mapping.StructureMapping)1 KineticsParameter (cbit.vcell.model.Kinetics.KineticsParameter)1 KineticsProxyParameter (cbit.vcell.model.Kinetics.KineticsProxyParameter)1 UnresolvedParameter (cbit.vcell.model.Kinetics.UnresolvedParameter)1 Model (cbit.vcell.model.Model)1 ModelParameter (cbit.vcell.model.Model.ModelParameter)1 ReservedSymbol (cbit.vcell.model.Model.ReservedSymbol)1 ModelUnitSystem (cbit.vcell.model.ModelUnitSystem)1 SpeciesContext (cbit.vcell.model.SpeciesContext)1 Expression (cbit.vcell.parser.Expression)1 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ListOf (org.sbml.jsbml.ListOf)1 LocalParameter (org.sbml.jsbml.LocalParameter)1 Parameter (org.sbml.jsbml.Parameter)1