Search in sources :

Example 6 with InitialAssignment

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

the class SBMLExporter method addSpecies.

/**
 * addSpecies comment.
 * @throws XMLStreamException
 * @throws SbmlException
 */
protected void addSpecies() throws XMLStreamException, SbmlException {
    Model vcModel = vcBioModel.getModel();
    SpeciesContext[] vcSpeciesContexts = vcModel.getSpeciesContexts();
    for (int i = 0; i < vcSpeciesContexts.length; i++) {
        org.sbml.jsbml.Species sbmlSpecies = sbmlModel.createSpecies();
        sbmlSpecies.setId(vcSpeciesContexts[i].getName());
        if (vcSpeciesContexts[i].getSbmlName() != null) {
            sbmlSpecies.setName(vcSpeciesContexts[i].getSbmlName());
        }
        // Assuming that at this point, the compartment(s) for the model are already filled in.
        Compartment compartment = sbmlModel.getCompartment(TokenMangler.mangleToSName(vcSpeciesContexts[i].getStructure().getName()));
        if (compartment != null) {
            sbmlSpecies.setCompartment(compartment.getId());
        }
        // 'hasSubstanceOnly' field will be 'true', since export to SBML is done by converting to initial amounts.
        sbmlSpecies.setHasOnlySubstanceUnits(true);
        // Get (and set) the initial concentration value
        if (getSelectedSimContext() == null) {
            throw new RuntimeException("No simcontext (application) specified; Cannot proceed.");
        }
        // Get the speciesContextSpec in the simContext corresponding to the 'speciesContext'; and extract its initial concentration value.
        SpeciesContextSpec vcSpeciesContextsSpec = getSelectedSimContext().getReactionContext().getSpeciesContextSpec(vcSpeciesContexts[i]);
        // since we are setting the substance units for species to 'molecule' or 'item', a unit that is originally in uM (or molecules/um2),
        // we need to convert concentration from uM -> molecules/um3; this can be achieved by dividing by KMOLE.
        // for now we don't do this here and defer to the mechanisms built into the SimContext to convert and set amount instead of concentration
        // TO-DO: change to export either concentrations or amounts depending on the type of SimContext and setting
        SpeciesContextSpecParameter initCount = vcSpeciesContextsSpec.getInitialCountParameter();
        if (initCount.getExpression() == null) {
            try {
                getSelectedSimContext().convertSpeciesIniCondition(false);
            } catch (MappingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            } catch (PropertyVetoException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        }
        Expression initCountExpr = initCount.getExpression();
        try {
            sbmlSpecies.setInitialAmount(initCountExpr.evaluateConstant());
        } catch (cbit.vcell.parser.ExpressionException e) {
            // If exporting to L2V3, if species concentration is not an expr with x, y, z or other species, add as InitialAssignment, else complain.
            if (initCountExpr != null) {
                if ((sbmlLevel == 2 && sbmlVersion >= 3) || (sbmlLevel > 2)) {
                    // L2V3 and above - add expression as init assignment
                    cbit.vcell.mapping.AssignmentRule vcellAs = getSelectedSimContext().getAssignmentRule(vcSpeciesContexts[i]);
                    if (vcellAs == null) {
                        // we don't create InitialAssignment for an AssignmentRule variable (Reference: L3V1 Section 4.8)
                        ASTNode initAssgnMathNode = getFormulaFromExpression(initCountExpr);
                        InitialAssignment initAssignment = sbmlModel.createInitialAssignment();
                        initAssignment.setSymbol(vcSpeciesContexts[i].getName());
                        initAssignment.setMath(initAssgnMathNode);
                    }
                } else {
                // L2V1 (or L1V2 also??)
                // do nothing - we no longer support export to level <3
                // // L2V1 (and L1V2?) and species is 'fixed' (constant), and not fn of x,y,z, other sp, add expr as assgn rule
                // ASTNode assgnRuleMathNode = getFormulaFromExpression(initCountExpr);
                // AssignmentRule assgnRule = sbmlModel.createAssignmentRule();
                // assgnRule.setVariable(vcSpeciesContexts[i].getName());
                // assgnRule.setMath(assgnRuleMathNode);
                }
            }
        }
        // Get (and set) the boundary condition value
        boolean bBoundaryCondition = getBoundaryCondition(vcSpeciesContexts[i]);
        sbmlSpecies.setBoundaryCondition(bBoundaryCondition);
        // mandatory for L3, optional for L2
        sbmlSpecies.setConstant(false);
        // set species substance units as 'molecules' - same as defined in the model; irrespective of it is in surface or volume.
        UnitDefinition unitDefn = getOrCreateSBMLUnit(sbmlExportSpec.getSubstanceUnits());
        sbmlSpecies.setSubstanceUnits(unitDefn);
        // need to do the following if exporting to SBML spatial
        if (bSpatial) {
            // Required for setting BoundaryConditions : structureMapping for vcSpeciesContext[i] & sbmlGeometry.coordinateComponents
            StructureMapping sm = getSelectedSimContext().getGeometryContext().getStructureMapping(vcSpeciesContexts[i].getStructure());
            SpatialModelPlugin mplugin = (SpatialModelPlugin) sbmlModel.getPlugin(SBMLUtils.SBML_SPATIAL_NS_PREFIX);
            org.sbml.jsbml.ext.spatial.Geometry sbmlGeometry = mplugin.getGeometry();
            CoordinateComponent ccX = sbmlGeometry.getListOfCoordinateComponents().get(vcModel.getX().getName());
            CoordinateComponent ccY = sbmlGeometry.getListOfCoordinateComponents().get(vcModel.getY().getName());
            CoordinateComponent ccZ = sbmlGeometry.getListOfCoordinateComponents().get(vcModel.getZ().getName());
            // add diffusion, advection, boundary condition parameters for species, if they exist
            Parameter[] scsParams = vcSpeciesContextsSpec.getParameters();
            if (scsParams != null) {
                for (int j = 0; j < scsParams.length; j++) {
                    if (scsParams[j] != null) {
                        SpeciesContextSpecParameter scsParam = (SpeciesContextSpecParameter) scsParams[j];
                        // no need to add parameters in SBML for init conc or init count
                        int role = scsParam.getRole();
                        switch(role) {
                            case SpeciesContextSpec.ROLE_BoundaryValueXm:
                                {
                                    break;
                                }
                            case SpeciesContextSpec.ROLE_BoundaryValueXp:
                                {
                                    break;
                                }
                            case SpeciesContextSpec.ROLE_BoundaryValueYm:
                                {
                                    break;
                                }
                            case SpeciesContextSpec.ROLE_BoundaryValueYp:
                                {
                                    break;
                                }
                            case SpeciesContextSpec.ROLE_BoundaryValueZm:
                                {
                                    break;
                                }
                            case SpeciesContextSpec.ROLE_BoundaryValueZp:
                                {
                                    break;
                                }
                            case SpeciesContextSpec.ROLE_DiffusionRate:
                                {
                                    break;
                                }
                            case SpeciesContextSpec.ROLE_InitialConcentration:
                                {
                                    // done elsewhere??
                                    continue;
                                // break;
                                }
                            case SpeciesContextSpec.ROLE_InitialCount:
                                {
                                    // done elsewhere??
                                    continue;
                                // break;
                                }
                            case SpeciesContextSpec.ROLE_VelocityX:
                                {
                                    break;
                                }
                            case SpeciesContextSpec.ROLE_VelocityY:
                                {
                                    break;
                                }
                            case SpeciesContextSpec.ROLE_VelocityZ:
                                {
                                    break;
                                }
                            default:
                                {
                                    throw new RuntimeException("SpeciesContext Specification parameter with role " + SpeciesContextSpec.RoleNames[role] + " not yet supported for SBML export");
                                }
                        }
                        // if diffusion is 0 && vel terms are not specified, boundary condition not present
                        if (vcSpeciesContextsSpec.isAdvecting() || vcSpeciesContextsSpec.isDiffusing()) {
                            Expression diffExpr = vcSpeciesContextsSpec.getDiffusionParameter().getExpression();
                            boolean bDiffExprNull = (diffExpr == null);
                            boolean bDiffExprIsZero = false;
                            if (!bDiffExprNull && diffExpr.isNumeric()) {
                                try {
                                    bDiffExprIsZero = (diffExpr.evaluateConstant() == 0.0);
                                } catch (Exception e) {
                                    e.printStackTrace(System.out);
                                    throw new RuntimeException("Unable to evalute numeric value of diffusion parameter for speciesContext '" + vcSpeciesContexts[i] + "'.");
                                }
                            }
                            boolean bDiffusionZero = (bDiffExprNull || bDiffExprIsZero);
                            Expression velX_Expr = vcSpeciesContextsSpec.getVelocityXParameter().getExpression();
                            SpatialQuantity[] velX_Quantities = vcSpeciesContextsSpec.getVelocityQuantities(QuantityComponent.X);
                            boolean bVelX_ExprIsNull = (velX_Expr == null && velX_Quantities.length == 0);
                            Expression velY_Expr = vcSpeciesContextsSpec.getVelocityYParameter().getExpression();
                            SpatialQuantity[] velY_Quantities = vcSpeciesContextsSpec.getVelocityQuantities(QuantityComponent.Y);
                            boolean bVelY_ExprIsNull = (velY_Expr == null && velY_Quantities.length == 0);
                            Expression velZ_Expr = vcSpeciesContextsSpec.getVelocityZParameter().getExpression();
                            SpatialQuantity[] velZ_Quantities = vcSpeciesContextsSpec.getVelocityQuantities(QuantityComponent.Z);
                            boolean bVelZ_ExprIsNull = (velZ_Expr == null && velZ_Quantities.length == 0);
                            boolean bAdvectionNull = (bVelX_ExprIsNull && bVelY_ExprIsNull && bVelZ_ExprIsNull);
                            if (bDiffusionZero && bAdvectionNull) {
                                continue;
                            }
                        }
                        // for example, if scsParam is BC_Zm and if coordinateComponent 'ccZ' is null, no SBML parameter should be created for BC_Zm
                        if ((((role == SpeciesContextSpec.ROLE_BoundaryValueXm) || (role == SpeciesContextSpec.ROLE_BoundaryValueXp)) && (ccX == null)) || (((role == SpeciesContextSpec.ROLE_BoundaryValueYm) || (role == SpeciesContextSpec.ROLE_BoundaryValueYp)) && (ccY == null)) || (((role == SpeciesContextSpec.ROLE_BoundaryValueZm) || (role == SpeciesContextSpec.ROLE_BoundaryValueZp)) && (ccZ == null))) {
                            continue;
                        }
                        org.sbml.jsbml.Parameter sbmlParam = createSBMLParamFromSpeciesParam(vcSpeciesContexts[i], (SpeciesContextSpecParameter) scsParams[j]);
                        if (sbmlParam != null) {
                            BoundaryConditionType vcBCType_Xm = vcSelectedSimContext.getGeometryContext().getStructureMapping(vcSpeciesContexts[i].getStructure()).getBoundaryConditionTypeXm();
                            BoundaryConditionType vcBCType_Xp = vcSelectedSimContext.getGeometryContext().getStructureMapping(vcSpeciesContexts[i].getStructure()).getBoundaryConditionTypeXp();
                            BoundaryConditionType vcBCType_Ym = vcSelectedSimContext.getGeometryContext().getStructureMapping(vcSpeciesContexts[i].getStructure()).getBoundaryConditionTypeYm();
                            BoundaryConditionType vcBCType_Yp = vcSelectedSimContext.getGeometryContext().getStructureMapping(vcSpeciesContexts[i].getStructure()).getBoundaryConditionTypeYp();
                            BoundaryConditionType vcBCType_Zm = vcSelectedSimContext.getGeometryContext().getStructureMapping(vcSpeciesContexts[i].getStructure()).getBoundaryConditionTypeZm();
                            BoundaryConditionType vcBCType_Zp = vcSelectedSimContext.getGeometryContext().getStructureMapping(vcSpeciesContexts[i].getStructure()).getBoundaryConditionTypeZp();
                            SpatialParameterPlugin spplugin = (SpatialParameterPlugin) sbmlParam.getPlugin(SBMLUtils.SBML_SPATIAL_NS_PREFIX);
                            if (role == SpeciesContextSpec.ROLE_DiffusionRate) {
                                // set diffusionCoefficient element in SpatialParameterPlugin for param
                                DiffusionCoefficient sbmlDiffCoeff = new DiffusionCoefficient();
                                sbmlDiffCoeff.setVariable(vcSpeciesContexts[i].getName());
                                sbmlDiffCoeff.setDiffusionKind(DiffusionKind.isotropic);
                                sbmlDiffCoeff.setSpeciesRef(vcSpeciesContexts[i].getName());
                                spplugin.setParamType(sbmlDiffCoeff);
                            }
                            if ((role == SpeciesContextSpec.ROLE_BoundaryValueXm) && (ccX != null)) {
                                // set BoundaryCondn Xm element in SpatialParameterPlugin for param
                                BoundaryCondition sbmlBCXm = new BoundaryCondition();
                                spplugin.setParamType(sbmlBCXm);
                                sbmlBCXm.setType(getBoundaryConditionKind(vcBCType_Xm));
                                sbmlBCXm.setVariable(vcSpeciesContexts[i].getName());
                                sbmlBCXm.setCoordinateBoundary(ccX.getBoundaryMinimum().getId());
                            }
                            if ((role == SpeciesContextSpec.ROLE_BoundaryValueXp) && (ccX != null)) {
                                // set BoundaryCondn Xp element in SpatialParameterPlugin for param
                                BoundaryCondition sbmlBCXp = new BoundaryCondition();
                                spplugin.setParamType(sbmlBCXp);
                                sbmlBCXp.setType(getBoundaryConditionKind(vcBCType_Xp));
                                sbmlBCXp.setVariable(vcSpeciesContexts[i].getName());
                                // sbmlBCXp.setType(sm.getBoundaryConditionTypeXp().boundaryTypeStringValue());
                                sbmlBCXp.setCoordinateBoundary(ccX.getBoundaryMaximum().getId());
                            }
                            if ((role == SpeciesContextSpec.ROLE_BoundaryValueYm) && (ccY != null)) {
                                // set BoundaryCondn Ym element in SpatialParameterPlugin for param
                                BoundaryCondition sbmlBCYm = new BoundaryCondition();
                                spplugin.setParamType(sbmlBCYm);
                                sbmlBCYm.setType(getBoundaryConditionKind(vcBCType_Yp));
                                sbmlBCYm.setVariable(vcSpeciesContexts[i].getName());
                                // sbmlBCYm.setType(sm.getBoundaryConditionTypeYm().boundaryTypeStringValue());
                                sbmlBCYm.setCoordinateBoundary(ccY.getBoundaryMinimum().getId());
                            }
                            if ((role == SpeciesContextSpec.ROLE_BoundaryValueYp) && (ccY != null)) {
                                // set BoundaryCondn Yp element in SpatialParameterPlugin for param
                                BoundaryCondition sbmlBCYp = new BoundaryCondition();
                                spplugin.setParamType(sbmlBCYp);
                                sbmlBCYp.setType(getBoundaryConditionKind(vcBCType_Yp));
                                sbmlBCYp.setVariable(vcSpeciesContexts[i].getName());
                                // sbmlBCYp.setType(sm.getBoundaryConditionTypeYp().boundaryTypeStringValue());
                                sbmlBCYp.setCoordinateBoundary(ccY.getBoundaryMaximum().getId());
                            }
                            if ((role == SpeciesContextSpec.ROLE_BoundaryValueZm) && (ccZ != null)) {
                                // set BoundaryCondn Zm element in SpatialParameterPlugin for param
                                BoundaryCondition sbmlBCZm = new BoundaryCondition();
                                spplugin.setParamType(sbmlBCZm);
                                sbmlBCZm.setType(getBoundaryConditionKind(vcBCType_Zm));
                                sbmlBCZm.setVariable(vcSpeciesContexts[i].getName());
                                // sbmlBCZm.setType(sm.getBoundaryConditionTypeZm().boundaryTypeStringValue());
                                sbmlBCZm.setCoordinateBoundary(ccZ.getBoundaryMinimum().getId());
                            }
                            if ((role == SpeciesContextSpec.ROLE_BoundaryValueZp) && (ccZ != null)) {
                                // set BoundaryCondn Zp element in SpatialParameterPlugin for param
                                BoundaryCondition sbmlBCZp = new BoundaryCondition();
                                spplugin.setParamType(sbmlBCZp);
                                sbmlBCZp.setType(getBoundaryConditionKind(vcBCType_Zp));
                                sbmlBCZp.setVariable(vcSpeciesContexts[i].getName());
                                // sbmlBCZp.setType(sm.getBoundaryConditionTypeZp().boundaryTypeStringValue());
                                sbmlBCZp.setCoordinateBoundary(ccZ.getBoundaryMaximum().getId());
                            }
                            if (role == SpeciesContextSpec.ROLE_VelocityX) {
                                // set advectionCoeff X element in SpatialParameterPlugin for param
                                AdvectionCoefficient sbmlAdvCoeffX = new AdvectionCoefficient();
                                spplugin.setParamType(sbmlAdvCoeffX);
                                sbmlAdvCoeffX.setVariable(vcSpeciesContexts[i].getName());
                                sbmlAdvCoeffX.setCoordinate(CoordinateKind.cartesianX);
                            }
                            if (role == SpeciesContextSpec.ROLE_VelocityY) {
                                // set advectionCoeff Y element in SpatialParameterPlugin for param
                                AdvectionCoefficient sbmlAdvCoeffY = new AdvectionCoefficient();
                                spplugin.setParamType(sbmlAdvCoeffY);
                                sbmlAdvCoeffY.setVariable(vcSpeciesContexts[i].getName());
                                sbmlAdvCoeffY.setCoordinate(CoordinateKind.cartesianY);
                            }
                            if (role == SpeciesContextSpec.ROLE_VelocityZ) {
                                // set advectionCoeff Z element in SpatialParameterPlugin for param
                                AdvectionCoefficient sbmlAdvCoeffZ = new AdvectionCoefficient();
                                spplugin.setParamType(sbmlAdvCoeffZ);
                                sbmlAdvCoeffZ.setVariable(vcSpeciesContexts[i].getName());
                                sbmlAdvCoeffZ.setCoordinate(CoordinateKind.cartesianZ);
                            }
                        }
                    // if sbmlParam != null
                    }
                // if scsParams[j] != null
                }
            // end for scsParams
            }
        // end scsParams != null
        }
        // end if (bSpatial)
        // Add the common name of species to annotation, and add an annotation element to the species.
        // This is required later while trying to read in fluxes ...
        // new Element(XMLTags.VCellRelatedInfoTag, sbml_vcml_ns);
        Element sbmlImportRelatedElement = null;
        // Element speciesElement = new Element(XMLTags.SpeciesTag, sbml_vcml_ns);
        // speciesElement.setAttribute(XMLTags.NameAttrTag, TokenMangler.mangleToSName(vcSpeciesContexts[i].getSpecies().getCommonName()));
        // sbmlImportRelatedElement.addContent(speciesElement);
        // Get RDF annotation for species from SBMLAnnotationUtils
        sbmlAnnotationUtil.writeAnnotation(vcSpeciesContexts[i].getSpecies(), sbmlSpecies, sbmlImportRelatedElement);
        // Now set notes,
        sbmlAnnotationUtil.writeNotes(vcSpeciesContexts[i].getSpecies(), sbmlSpecies);
    }
}
Also used : Compartment(org.sbml.jsbml.Compartment) Element(org.jdom.Element) BoundaryConditionType(cbit.vcell.math.BoundaryConditionType) SpatialParameterPlugin(org.sbml.jsbml.ext.spatial.SpatialParameterPlugin) SpeciesContext(cbit.vcell.model.SpeciesContext) SpeciesContextSpec(cbit.vcell.mapping.SpeciesContextSpec) StructureMapping(cbit.vcell.mapping.StructureMapping) MappingException(cbit.vcell.mapping.MappingException) ASTNode(org.sbml.jsbml.ASTNode) SpatialQuantity(cbit.vcell.mapping.spatial.SpatialObject.SpatialQuantity) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) UnitDefinition(org.sbml.jsbml.UnitDefinition) SpeciesContextSpecParameter(cbit.vcell.mapping.SpeciesContextSpec.SpeciesContextSpecParameter) CoordinateComponent(org.sbml.jsbml.ext.spatial.CoordinateComponent) AdvectionCoefficient(org.sbml.jsbml.ext.spatial.AdvectionCoefficient) AssignmentRule(org.sbml.jsbml.AssignmentRule) SpatialModelPlugin(org.sbml.jsbml.ext.spatial.SpatialModelPlugin) ExpressionException(cbit.vcell.parser.ExpressionException) DiffusionCoefficient(org.sbml.jsbml.ext.spatial.DiffusionCoefficient) InteriorPoint(org.sbml.jsbml.ext.spatial.InteriorPoint) XMLStreamException(javax.xml.stream.XMLStreamException) MatrixException(cbit.vcell.matrix.MatrixException) SbmlException(org.vcell.sbml.SbmlException) ParseException(org.sbml.jsbml.text.parser.ParseException) RuntimeCryptoException(org.bouncycastle.crypto.RuntimeCryptoException) XmlParseException(cbit.vcell.xml.XmlParseException) MappingException(cbit.vcell.mapping.MappingException) PropertyVetoException(java.beans.PropertyVetoException) ImageException(cbit.image.ImageException) SBMLException(org.sbml.jsbml.SBMLException) ModelException(cbit.vcell.model.ModelException) ExpressionException(cbit.vcell.parser.ExpressionException) MathException(cbit.vcell.math.MathException) PropertyVetoException(java.beans.PropertyVetoException) InitialAssignment(org.sbml.jsbml.InitialAssignment) Expression(cbit.vcell.parser.Expression) BoundaryCondition(org.sbml.jsbml.ext.spatial.BoundaryCondition) Model(cbit.vcell.model.Model) BioModel(cbit.vcell.biomodel.BioModel) StructureMappingParameter(cbit.vcell.mapping.StructureMapping.StructureMappingParameter) Parameter(cbit.vcell.model.Parameter) KineticsParameter(cbit.vcell.model.Kinetics.KineticsParameter) ModelParameter(cbit.vcell.model.Model.ModelParameter) SpeciesContextSpecParameter(cbit.vcell.mapping.SpeciesContextSpec.SpeciesContextSpecParameter) LocalParameter(cbit.vcell.mapping.ParameterContext.LocalParameter)

Aggregations

InitialAssignment (org.sbml.jsbml.InitialAssignment)6 InteriorPoint (org.sbml.jsbml.ext.spatial.InteriorPoint)6 BioModel (cbit.vcell.biomodel.BioModel)5 Model (cbit.vcell.model.Model)5 Expression (cbit.vcell.parser.Expression)5 ExpressionException (cbit.vcell.parser.ExpressionException)5 PropertyVetoException (java.beans.PropertyVetoException)5 XMLStreamException (javax.xml.stream.XMLStreamException)5 Compartment (org.sbml.jsbml.Compartment)5 SBMLException (org.sbml.jsbml.SBMLException)5 SbmlException (org.vcell.sbml.SbmlException)5 ModelPropertyVetoException (cbit.vcell.model.ModelPropertyVetoException)4 IOException (java.io.IOException)4 ListOf (org.sbml.jsbml.ListOf)4 SpeciesContextSpec (cbit.vcell.mapping.SpeciesContextSpec)3 StructureMapping (cbit.vcell.mapping.StructureMapping)3 ModelParameter (cbit.vcell.model.Model.ModelParameter)3 XmlParseException (cbit.vcell.xml.XmlParseException)3 HashMap (java.util.HashMap)3 Element (org.jdom.Element)3