Search in sources :

Example 6 with LumpedKinetics

use of cbit.vcell.model.LumpedKinetics in project vcell by virtualcell.

the class StructureAnalyzer method getCorrectedRateExpression.

public Expression getCorrectedRateExpression(ReactionStep reactionStep, ReactionParticipant reactionParticipant, RateType rateType) throws Exception {
    if (reactionParticipant instanceof Catalyst) {
        throw new Exception("Catalyst " + reactionParticipant + " doesn't have a rate for this reaction");
    // return new Expression(0.0);
    }
    double stoich = reactionStep.getStoichiometry(reactionParticipant.getSpeciesContext());
    if (stoich == 0.0) {
        return new Expression(0.0);
    }
    // 
    // make distributed rate with correct stoichiometry for this participant
    // 
    VCUnitDefinition correctedReactionRateUnit = null;
    Expression distribRate = null;
    if (reactionStep.getKinetics() instanceof DistributedKinetics) {
        DistributedKinetics distributedKinetics = (DistributedKinetics) reactionStep.getKinetics();
        KineticsParameter distribReactionRateParameter = distributedKinetics.getReactionRateParameter();
        distribRate = new Expression(distribReactionRateParameter, mathMapping.getNameScope());
        correctedReactionRateUnit = distribReactionRateParameter.getUnitDefinition();
    } else if (reactionStep.getKinetics() instanceof LumpedKinetics) {
        // 
        // need to put this into concentration/time with respect to structure for reaction.
        // 
        Structure.StructureSize structureSize = reactionStep.getStructure().getStructureSize();
        LumpedKinetics lumpedKinetics = (LumpedKinetics) reactionStep.getKinetics();
        KineticsParameter lumpedReactionRateParameter = lumpedKinetics.getLumpedReactionRateParameter();
        Expression lumpedReactionRateExp = new Expression(lumpedReactionRateParameter, mathMapping.getNameScope());
        distribRate = Expression.div(lumpedReactionRateExp, new Expression(structureSize, mathMapping.getNameScope()));
        ;
        correctedReactionRateUnit = lumpedReactionRateParameter.getUnitDefinition().divideBy(structureSize.getUnitDefinition());
    }
    // correct for stoichiometry
    Expression distribRateWithStoich = distribRate;
    if (stoich != 1) {
        distribRateWithStoich = Expression.mult(new Expression(stoich), distribRateWithStoich);
    }
    // flux correction if reaction and reactionParticipant are in different compartments. (not necessarily dimensionless, use KFlux parameter).
    Expression distribRateWithStoichFlux = distribRateWithStoich;
    if (reactionStep.getStructure() != reactionParticipant.getStructure()) {
        StructureMapping reactionSM = mathMapping.getSimulationContext().getGeometryContext().getStructureMapping(reactionStep.getStructure());
        StructureMapping speciesSM = mathMapping.getSimulationContext().getGeometryContext().getStructureMapping(reactionParticipant.getStructure());
        Parameter fluxCorrectionParameter = mathMapping.getFluxCorrectionParameter(reactionSM, speciesSM);
        Expression fluxCorrection = new Expression(fluxCorrectionParameter, mathMapping.getNameScope());
        distribRateWithStoichFlux = Expression.mult(fluxCorrection, distribRateWithStoichFlux);
        correctedReactionRateUnit = correctedReactionRateUnit.multiplyBy(fluxCorrectionParameter.getUnitDefinition());
    }
    // apply unit factor for difference substance
    ModelUnitSystem unitSystem = mathMapping.getSimulationContext().getModel().getUnitSystem();
    VCUnitDefinition timeUnit = unitSystem.getTimeUnit();
    VCUnitDefinition speciesConcUnit = reactionParticipant.getSpeciesContext().getUnitDefinition();
    VCUnitDefinition speciesConcRateUnit = speciesConcUnit.divideBy(timeUnit);
    Expression unitFactor = null;
    if (rateType == RateType.ConcentrationRate) {
        unitFactor = mathMapping.getUnitFactor(speciesConcRateUnit.divideBy(correctedReactionRateUnit));
    } else if (rateType == RateType.ResolvedFluxRate) {
        unitFactor = mathMapping.getUnitFactor(speciesConcRateUnit.multiplyBy(unitSystem.getLengthUnit()).divideBy(correctedReactionRateUnit));
    }
    return Expression.mult(unitFactor, distribRateWithStoichFlux).flatten();
}
Also used : DistributedKinetics(cbit.vcell.model.DistributedKinetics) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) KineticsParameter(cbit.vcell.model.Kinetics.KineticsParameter) LumpedKinetics(cbit.vcell.model.LumpedKinetics) Expression(cbit.vcell.parser.Expression) MathMappingParameter(cbit.vcell.mapping.AbstractMathMapping.MathMappingParameter) Parameter(cbit.vcell.model.Parameter) KineticsParameter(cbit.vcell.model.Kinetics.KineticsParameter) Catalyst(cbit.vcell.model.Catalyst) ModelUnitSystem(cbit.vcell.model.ModelUnitSystem)

Example 7 with LumpedKinetics

use of cbit.vcell.model.LumpedKinetics in project vcell by virtualcell.

the class PotentialMapping method getTotalMembraneCurrent.

/**
 * Insert the method's description here.
 * Creation date: (2/19/2002 12:56:13 PM)
 * @return cbit.vcell.parser.Expression
 * @param simContext cbit.vcell.mapping.SimulationContext
 * @param membrane cbit.vcell.model.Membrane
 */
private static Expression getTotalMembraneCurrent(SimulationContext simContext, Membrane membrane, MathMapping_4_8 mathMapping_4_8) throws ExpressionException {
    MembraneMapping membraneMapping = (MembraneMapping) simContext.getGeometryContext().getStructureMapping(membrane);
    if (!membraneMapping.getCalculateVoltage()) {
        return new Expression(0.0);
    }
    // 
    // gather current terms
    // 
    Expression currentExp = new Expression(0.0);
    ReactionSpec[] reactionSpecs = simContext.getReactionContext().getReactionSpecs();
    StructureMappingParameter sizeParameter = membraneMapping.getSizeParameter();
    Expression area = null;
    if (simContext.getGeometry().getDimension() == 0 && (sizeParameter.getExpression() == null || sizeParameter.getExpression().isZero())) {
        System.out.println("size not set for membrane \"" + membrane.getName() + "\", refer to Structure Mapping in Application \"" + mathMapping_4_8.getSimulationContext().getName() + "\"");
        area = membraneMapping.getNullSizeParameterValue();
    } else {
        area = new Expression(sizeParameter, mathMapping_4_8.getNameScope());
    }
    for (int i = 0; i < reactionSpecs.length; i++) {
        // 
        if (reactionSpecs[i].isExcluded()) {
            continue;
        }
        if (reactionSpecs[i].getReactionStep().getKinetics() instanceof DistributedKinetics) {
            ReactionStep rs = reactionSpecs[i].getReactionStep();
            DistributedKinetics distributedKinetics = (DistributedKinetics) rs.getKinetics();
            if (rs.getStructure() == membrane) {
                if (!distributedKinetics.getCurrentDensityParameter().getExpression().isZero()) {
                    // 
                    // change sign convension from inward current to outward current (which is consistent with voltage convension)
                    // 
                    currentExp = Expression.add(currentExp, Expression.negate(Expression.mult(new Expression(distributedKinetics.getCurrentDensityParameter(), mathMapping_4_8.getNameScope()), area)));
                }
            }
        } else {
            ReactionStep rs = reactionSpecs[i].getReactionStep();
            LumpedKinetics lumpedKinetics = (LumpedKinetics) rs.getKinetics();
            if (rs.getStructure() == membrane) {
                if (!lumpedKinetics.getLumpedCurrentParameter().getExpression().isZero()) {
                    // 
                    if (mathMapping_4_8.getResolved(membraneMapping)) {
                        throw new RuntimeException("math generation for total currents within spatial electrophysiology not yet implemented");
                    }
                    Expression lumpedCurrentSymbolExp = new Expression(lumpedKinetics.getLumpedCurrentParameter(), mathMapping_4_8.getNameScope());
                    currentExp = Expression.add(currentExp, Expression.negate(lumpedCurrentSymbolExp));
                }
            }
        }
    }
    return currentExp.flatten();
}
Also used : DistributedKinetics(cbit.vcell.model.DistributedKinetics) MembraneMapping(cbit.vcell.mapping.MembraneMapping) LumpedKinetics(cbit.vcell.model.LumpedKinetics) Expression(cbit.vcell.parser.Expression) ReactionSpec(cbit.vcell.mapping.ReactionSpec) ReactionStep(cbit.vcell.model.ReactionStep) StructureMappingParameter(cbit.vcell.mapping.StructureMapping.StructureMappingParameter)

Example 8 with LumpedKinetics

use of cbit.vcell.model.LumpedKinetics in project vcell by virtualcell.

the class StructureAnalyzer method getReactionRateExpression.

public Expression getReactionRateExpression(ReactionStep reactionStep, ReactionParticipant reactionParticipant) throws Exception {
    if (reactionParticipant instanceof Catalyst) {
        throw new Exception("Catalyst " + reactionParticipant + " doesn't have a rate for this reaction");
    // return new Expression(0.0);
    }
    double stoich = reactionStep.getStoichiometry(reactionParticipant.getSpeciesContext());
    if (stoich == 0.0) {
        return new Expression(0.0);
    }
    if (reactionStep.getKinetics() instanceof DistributedKinetics) {
        DistributedKinetics distributedKinetics = (DistributedKinetics) reactionStep.getKinetics();
        if (stoich != 1) {
            Expression exp = Expression.mult(new Expression(stoich), new Expression(distributedKinetics.getReactionRateParameter(), mathMapping_4_8.getNameScope()));
            return exp;
        } else {
            Expression exp = new Expression(distributedKinetics.getReactionRateParameter(), mathMapping_4_8.getNameScope());
            return exp;
        }
    } else if (reactionStep.getKinetics() instanceof LumpedKinetics) {
        Structure.StructureSize structureSize = reactionStep.getStructure().getStructureSize();
        // 
        // need to put this into concentration/time with respect to structure for reaction.
        // 
        LumpedKinetics lumpedKinetics = (LumpedKinetics) reactionStep.getKinetics();
        Expression factor = null;
        ModelUnitSystem unitSystem = mathMapping_4_8.getSimulationContext().getModel().getUnitSystem();
        if (reactionStep.getStructure() instanceof Feature || ((reactionStep.getStructure() instanceof Membrane) && reactionStep instanceof FluxReaction)) {
            VCUnitDefinition lumpedToVolumeSubstance = unitSystem.getVolumeSubstanceUnit().divideBy(unitSystem.getLumpedReactionSubstanceUnit());
            factor = Expression.div(new Expression(lumpedToVolumeSubstance.getDimensionlessScale()), new Expression(structureSize, mathMapping_4_8.getNameScope()));
        } else if (reactionStep.getStructure() instanceof Membrane && reactionStep instanceof SimpleReaction) {
            VCUnitDefinition lumpedToVolumeSubstance = unitSystem.getMembraneSubstanceUnit().divideBy(unitSystem.getLumpedReactionSubstanceUnit());
            factor = Expression.div(new Expression(lumpedToVolumeSubstance.getDimensionlessScale()), new Expression(structureSize, mathMapping_4_8.getNameScope()));
        } else {
            throw new RuntimeException("failed to create reaction rate expression for reaction " + reactionStep.getName() + ", with kinetic type of " + reactionStep.getKinetics().getClass().getName());
        }
        if (stoich != 1) {
            Expression exp = Expression.mult(new Expression(stoich), Expression.mult(new Expression(lumpedKinetics.getLumpedReactionRateParameter(), mathMapping_4_8.getNameScope()), factor));
            return exp;
        } else {
            Expression exp = Expression.mult(new Expression(lumpedKinetics.getLumpedReactionRateParameter(), mathMapping_4_8.getNameScope()), factor);
            return exp;
        }
    } else {
        throw new RuntimeException("unexpected kinetic type " + reactionStep.getKinetics().getClass().getName());
    }
}
Also used : DistributedKinetics(cbit.vcell.model.DistributedKinetics) SimpleReaction(cbit.vcell.model.SimpleReaction) LumpedKinetics(cbit.vcell.model.LumpedKinetics) FluxReaction(cbit.vcell.model.FluxReaction) Feature(cbit.vcell.model.Feature) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression) Membrane(cbit.vcell.model.Membrane) Catalyst(cbit.vcell.model.Catalyst) ModelUnitSystem(cbit.vcell.model.ModelUnitSystem)

Example 9 with LumpedKinetics

use of cbit.vcell.model.LumpedKinetics in project vcell by virtualcell.

the class SBMLExporter method addReactions.

/**
 * addReactions comment.
 * @throws SbmlException
 * @throws XMLStreamException
 */
protected void addReactions() throws SbmlException, XMLStreamException {
    // Check if any reaction has electrical mapping
    boolean bCalculatePotential = false;
    StructureMapping[] structureMappings = getSelectedSimContext().getGeometryContext().getStructureMappings();
    for (int i = 0; i < structureMappings.length; i++) {
        if (structureMappings[i] instanceof MembraneMapping) {
            if (((MembraneMapping) structureMappings[i]).getCalculateVoltage()) {
                bCalculatePotential = true;
            }
        }
    }
    // If it does, VCell doesn't export it to SBML (no representation).
    if (bCalculatePotential) {
        throw new RuntimeException("This VCell model has Electrical mapping; cannot be exported to SBML at this time");
    }
    l2gMap.clear();
    ReactionSpec[] vcReactionSpecs = getSelectedSimContext().getReactionContext().getReactionSpecs();
    for (int i = 0; i < vcReactionSpecs.length; i++) {
        if (vcReactionSpecs[i].isExcluded()) {
            continue;
        }
        ReactionStep vcReactionStep = vcReactionSpecs[i].getReactionStep();
        // Create sbml reaction
        String rxnName = vcReactionStep.getName();
        org.sbml.jsbml.Reaction sbmlReaction = sbmlModel.createReaction();
        sbmlReaction.setId(org.vcell.util.TokenMangler.mangleToSName(rxnName));
        sbmlReaction.setName(rxnName);
        String rxnSbmlName = vcReactionStep.getSbmlName();
        if (rxnSbmlName != null && !rxnSbmlName.isEmpty()) {
            sbmlReaction.setName(rxnSbmlName);
        }
        // If the reactionStep is a flux reaction, add the details to the annotation (structure, carrier valence, flux carrier, fluxOption, etc.)
        // If reactionStep is a simple reaction, add annotation to indicate the structure of reaction.
        // Useful when roundtripping ...
        Element sbmlImportRelatedElement = null;
        // try {
        // sbmlImportRelatedElement = getAnnotationElement(vcReactionStep);
        // } catch (XmlParseException e1) {
        // e1.printStackTrace(System.out);
        // //			throw new RuntimeException("Error ");
        // }
        // Get annotation (RDF and non-RDF) for reactionStep from SBMLAnnotationUtils
        sbmlAnnotationUtil.writeAnnotation(vcReactionStep, sbmlReaction, sbmlImportRelatedElement);
        // Now set notes,
        sbmlAnnotationUtil.writeNotes(vcReactionStep, sbmlReaction);
        // Get reaction kineticLaw
        Kinetics vcRxnKinetics = vcReactionStep.getKinetics();
        org.sbml.jsbml.KineticLaw sbmlKLaw = sbmlReaction.createKineticLaw();
        try {
            // Convert expression from kinetics rate parameter into MathML and use libSBMl utilities to convert it to formula
            // (instead of directly using rate parameter's expression infix) to maintain integrity of formula :
            // for example logical and inequalities are not handled gracefully by libSBMl if expression.infix is used.
            final Expression localRateExpr;
            final Expression lumpedRateExpr;
            if (vcRxnKinetics instanceof DistributedKinetics) {
                localRateExpr = ((DistributedKinetics) vcRxnKinetics).getReactionRateParameter().getExpression();
                lumpedRateExpr = null;
            } else if (vcRxnKinetics instanceof LumpedKinetics) {
                localRateExpr = null;
                lumpedRateExpr = ((LumpedKinetics) vcRxnKinetics).getLumpedReactionRateParameter().getExpression();
            } else {
                throw new RuntimeException("unexpected Rate Law '" + vcRxnKinetics.getClass().getSimpleName() + "', not distributed or lumped type");
            }
            // if (vcRxnKinetics instanceof DistributedKinetics)
            // Expression correctedRateExpr = kineticsAdapter.getExpression();
            // Add parameters, if any, to the kineticLaw
            Kinetics.KineticsParameter[] vcKineticsParams = vcRxnKinetics.getKineticsParameters();
            // In the first pass thro' the kinetic params, store the non-numeric param names and expressions in arrays
            String[] kinParamNames = new String[vcKineticsParams.length];
            Expression[] kinParamExprs = new Expression[vcKineticsParams.length];
            for (int j = 0; j < vcKineticsParams.length; j++) {
                if (true) {
                    // Since local reaction parameters cannot be defined by a rule, such parameters (with rules) are exported as global parameters.
                    if ((vcKineticsParams[j].getRole() == Kinetics.ROLE_CurrentDensity && (!vcKineticsParams[j].getExpression().isZero())) || (vcKineticsParams[j].getRole() == Kinetics.ROLE_LumpedCurrent && (!vcKineticsParams[j].getExpression().isZero()))) {
                        throw new RuntimeException("Electric current not handled by SBML export; failed to export reaction \"" + vcReactionStep.getName() + "\" at this time");
                    }
                    if (!vcKineticsParams[j].getExpression().isNumeric()) {
                        // NON_NUMERIC KINETIC PARAM
                        // Create new name for kinetic parameter and store it in kinParamNames, store corresponding exprs in kinParamExprs
                        // Will be used later to add this param as global.
                        String newParamName = TokenMangler.mangleToSName(vcKineticsParams[j].getName() + "_" + vcReactionStep.getName());
                        kinParamNames[j] = newParamName;
                        kinParamExprs[j] = new Expression(vcKineticsParams[j].getExpression());
                    }
                }
            }
            // If so, these need to be added as global param (else the SBML doc will not be valid)
            for (int j = 0; j < vcKineticsParams.length; j++) {
                final KineticsParameter vcKParam = vcKineticsParams[j];
                if ((vcKParam.getRole() != Kinetics.ROLE_ReactionRate) && (vcKParam.getRole() != Kinetics.ROLE_LumpedReactionRate)) {
                    // if expression of kinetic param evaluates to a double, the parameter value is set
                    if ((vcKParam.getRole() == Kinetics.ROLE_CurrentDensity && (!vcKParam.getExpression().isZero())) || (vcKParam.getRole() == Kinetics.ROLE_LumpedCurrent && (!vcKParam.getExpression().isZero()))) {
                        throw new RuntimeException("Electric current not handled by SBML export; failed to export reaction \"" + vcReactionStep.getName() + "\" at this time");
                    }
                    if (vcKParam.getExpression().isNumeric()) {
                        // NUMERIC KINETIC PARAM
                        // check if it is used in other parameters that have expressions,
                        boolean bAddedParam = false;
                        String origParamName = vcKParam.getName();
                        String newParamName = TokenMangler.mangleToSName(origParamName + "_" + vcReactionStep.getName());
                        VCUnitDefinition vcUnit = vcKParam.getUnitDefinition();
                        for (int k = 0; k < vcKineticsParams.length; k++) {
                            if (kinParamExprs[k] != null) {
                                // The param could be in the expression for any other param
                                if (kinParamExprs[k].hasSymbol(origParamName)) {
                                    // mangle its name to avoid conflict with other globals
                                    if (globalParamNamesHash.get(newParamName) == null) {
                                        globalParamNamesHash.put(newParamName, newParamName);
                                        org.sbml.jsbml.Parameter sbmlKinParam = sbmlModel.createParameter();
                                        sbmlKinParam.setId(newParamName);
                                        sbmlKinParam.setValue(vcKParam.getConstantValue());
                                        final boolean constValue = vcKParam.isConstant();
                                        sbmlKinParam.setConstant(true);
                                        // Set SBML units for sbmlParam using VC units from vcParam
                                        if (!vcUnit.isTBD()) {
                                            UnitDefinition unitDefn = getOrCreateSBMLUnit(vcUnit);
                                            sbmlKinParam.setUnits(unitDefn);
                                        }
                                        Pair<String, String> origParam = new Pair<String, String>(rxnName, origParamName);
                                        l2gMap.put(origParam, newParamName);
                                        bAddedParam = true;
                                    } else {
                                    // need to get another name for param and need to change all its refereces in the other kinParam euqations.
                                    }
                                    // update the expression to contain new name, since the globalparam has new name
                                    kinParamExprs[k].substituteInPlace(new Expression(origParamName), new Expression(newParamName));
                                }
                            }
                        }
                        // If the param hasn't been added yet, it is definitely a local param. add it to kineticLaw now.
                        if (!bAddedParam) {
                            org.sbml.jsbml.LocalParameter sbmlKinParam = sbmlKLaw.createLocalParameter();
                            sbmlKinParam.setId(origParamName);
                            sbmlKinParam.setValue(vcKParam.getConstantValue());
                            System.out.println("tis constant " + sbmlKinParam.isExplicitlySetConstant());
                            // Set SBML units for sbmlParam using VC units from vcParam
                            if (!vcUnit.isTBD()) {
                                UnitDefinition unitDefn = getOrCreateSBMLUnit(vcUnit);
                                sbmlKinParam.setUnits(unitDefn);
                            }
                        } else {
                            // hence change its occurance in rate expression if it contains that param name
                            if (localRateExpr != null && localRateExpr.hasSymbol(origParamName)) {
                                localRateExpr.substituteInPlace(new Expression(origParamName), new Expression(newParamName));
                            }
                            if (lumpedRateExpr != null && lumpedRateExpr.hasSymbol(origParamName)) {
                                lumpedRateExpr.substituteInPlace(new Expression(origParamName), new Expression(newParamName));
                            }
                        }
                    }
                }
            }
            // (using the kinParamNames and kinParamExprs above) to ensure uniqueness in the global parameter names.
            for (int j = 0; j < vcKineticsParams.length; j++) {
                if (((vcKineticsParams[j].getRole() != Kinetics.ROLE_ReactionRate) && (vcKineticsParams[j].getRole() != Kinetics.ROLE_LumpedReactionRate)) && !(vcKineticsParams[j].getExpression().isNumeric())) {
                    String oldName = vcKineticsParams[j].getName();
                    String newName = kinParamNames[j];
                    // change the name of this parameter in the rate expression
                    if (localRateExpr != null && localRateExpr.hasSymbol(oldName)) {
                        localRateExpr.substituteInPlace(new Expression(oldName), new Expression(newName));
                    }
                    if (lumpedRateExpr != null && lumpedRateExpr.hasSymbol(oldName)) {
                        lumpedRateExpr.substituteInPlace(new Expression(oldName), new Expression(newName));
                    }
                    // Change the occurence of this param in other param expressions
                    for (int k = 0; k < vcKineticsParams.length; k++) {
                        if (((vcKineticsParams[k].getRole() != Kinetics.ROLE_ReactionRate) && (vcKineticsParams[j].getRole() != Kinetics.ROLE_LumpedReactionRate)) && !(vcKineticsParams[k].getExpression().isNumeric())) {
                            if (k != j && vcKineticsParams[k].getExpression().hasSymbol(oldName)) {
                                // for all params except the current param represented by index j (whose name was changed)
                                kinParamExprs[k].substituteInPlace(new Expression(oldName), new Expression(newName));
                            }
                            if (k == j && vcKineticsParams[k].getExpression().hasSymbol(oldName)) {
                                throw new RuntimeException("A parameter cannot refer to itself in its expression");
                            }
                        }
                    }
                // end for - k
                }
            }
            // In the fifth pass thro' the kinetic params, the non-numeric params are added to the global params of the model
            for (int j = 0; j < vcKineticsParams.length; j++) {
                if (((vcKineticsParams[j].getRole() != Kinetics.ROLE_ReactionRate) && (vcKineticsParams[j].getRole() != Kinetics.ROLE_LumpedReactionRate)) && !(vcKineticsParams[j].getExpression().isNumeric())) {
                    // Now, add this param to the globalParamNamesHash and add a global parameter to the sbmlModel
                    String paramName = kinParamNames[j];
                    if (globalParamNamesHash.get(paramName) == null) {
                        globalParamNamesHash.put(paramName, paramName);
                    } else {
                    // need to get another name for param and need to change all its refereces in the other kinParam euqations.
                    }
                    Pair<String, String> origParam = new Pair<String, String>(rxnName, paramName);
                    // keeps its name but becomes a global (?)
                    l2gMap.put(origParam, paramName);
                    ASTNode paramFormulaNode = getFormulaFromExpression(kinParamExprs[j]);
                    AssignmentRule sbmlParamAssignmentRule = sbmlModel.createAssignmentRule();
                    sbmlParamAssignmentRule.setVariable(paramName);
                    sbmlParamAssignmentRule.setMath(paramFormulaNode);
                    org.sbml.jsbml.Parameter sbmlKinParam = sbmlModel.createParameter();
                    sbmlKinParam.setId(paramName);
                    if (!vcKineticsParams[j].getUnitDefinition().isTBD()) {
                        sbmlKinParam.setUnits(getOrCreateSBMLUnit(vcKineticsParams[j].getUnitDefinition()));
                    }
                    // Since the parameter is being specified by a Rule, its 'constant' field shoud be set to 'false' (default - true).
                    sbmlKinParam.setConstant(false);
                }
            }
            // end for (j) - fifth pass
            // After making all necessary adjustments to the rate expression, now set the sbmlKLaw.
            final ASTNode exprFormulaNode;
            if (lumpedRateExpr != null) {
                exprFormulaNode = getFormulaFromExpression(lumpedRateExpr);
            } else {
                if (bSpatial) {
                    exprFormulaNode = getFormulaFromExpression(localRateExpr);
                } else {
                    exprFormulaNode = getFormulaFromExpression(Expression.mult(localRateExpr, new Expression(vcReactionStep.getStructure().getName())));
                }
            }
            sbmlKLaw.setMath(exprFormulaNode);
        } catch (cbit.vcell.parser.ExpressionException e) {
            e.printStackTrace(System.out);
            throw new RuntimeException("Error getting value of parameter : " + e.getMessage());
        }
        // Add kineticLaw to sbmlReaction - not needed now, since we use sbmlRxn.createKLaw() ??
        // sbmlReaction.setKineticLaw(sbmlKLaw);
        // Add reactants, products, modifiers
        // Simple reactions have catalysts, fluxes have 'flux'
        cbit.vcell.model.ReactionParticipant[] rxnParticipants = vcReactionStep.getReactionParticipants();
        for (ReactionParticipant rxnParticpant : rxnParticipants) {
            SimpleSpeciesReference ssr = null;
            SpeciesReference sr = null;
            // to get unique ID when the same species is both a reactant and a product
            String rolePostfix = "";
            if (rxnParticpant instanceof cbit.vcell.model.Reactant) {
                rolePostfix = "r";
                ssr = sr = sbmlReaction.createReactant();
            } else if (rxnParticpant instanceof cbit.vcell.model.Product) {
                rolePostfix = "p";
                ssr = sr = sbmlReaction.createProduct();
            }
            if (rxnParticpant instanceof cbit.vcell.model.Catalyst) {
                rolePostfix = "c";
                ssr = sbmlReaction.createModifier();
            }
            if (ssr != null) {
                ssr.setSpecies(rxnParticpant.getSpeciesContext().getName());
            }
            if (sr != null) {
                sr.setStoichiometry(Double.parseDouble(Integer.toString(rxnParticpant.getStoichiometry())));
                String modelUniqueName = vcReactionStep.getName() + '_' + rxnParticpant.getName() + rolePostfix;
                sr.setId(TokenMangler.mangleToSName(modelUniqueName));
                // SBML-REVIEW
                sr.setConstant(true);
            // int rcode = sr.appendNotes("<
            // we know that in VCell we can't override stoichiometry anywhere, below is no longer questionable
            // try {
            // SBMLHelper.addNote(sr, "VCELL guess: how do we know if reaction is constant?");
            // } catch (Exception e) {
            // e.printStackTrace();
            // }
            }
        }
        sbmlReaction.setFast(vcReactionSpecs[i].isFast());
        // this attribute is mandatory for L3, optional for L2. So explicitly setting value.
        sbmlReaction.setReversible(true);
        if (bSpatial) {
            // set compartment for reaction if spatial
            sbmlReaction.setCompartment(vcReactionStep.getStructure().getName());
            // CORE  HAS ALT MATH true
            // set the "isLocal" attribute = true (in 'spatial' namespace) for each species
            SpatialReactionPlugin srplugin = (SpatialReactionPlugin) sbmlReaction.getPlugin(SBMLUtils.SBML_SPATIAL_NS_PREFIX);
            srplugin.setIsLocal(vcRxnKinetics instanceof DistributedKinetics);
        }
    }
}
Also used : MembraneMapping(cbit.vcell.mapping.MembraneMapping) LumpedKinetics(cbit.vcell.model.LumpedKinetics) Element(org.jdom.Element) StructureMapping(cbit.vcell.mapping.StructureMapping) SimpleSpeciesReference(org.sbml.jsbml.SimpleSpeciesReference) KineticsParameter(cbit.vcell.model.Kinetics.KineticsParameter) SpeciesReference(org.sbml.jsbml.SpeciesReference) SimpleSpeciesReference(org.sbml.jsbml.SimpleSpeciesReference) ASTNode(org.sbml.jsbml.ASTNode) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) UnitDefinition(org.sbml.jsbml.UnitDefinition) Pair(org.vcell.util.Pair) DistributedKinetics(cbit.vcell.model.DistributedKinetics) SpatialReactionPlugin(org.sbml.jsbml.ext.spatial.SpatialReactionPlugin) ReactionSpec(cbit.vcell.mapping.ReactionSpec) AssignmentRule(org.sbml.jsbml.AssignmentRule) ExpressionException(cbit.vcell.parser.ExpressionException) InteriorPoint(org.sbml.jsbml.ext.spatial.InteriorPoint) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression) ReactionStep(cbit.vcell.model.ReactionStep) Kinetics(cbit.vcell.model.Kinetics) DistributedKinetics(cbit.vcell.model.DistributedKinetics) LumpedKinetics(cbit.vcell.model.LumpedKinetics) ReactionParticipant(cbit.vcell.model.ReactionParticipant)

Example 10 with LumpedKinetics

use of cbit.vcell.model.LumpedKinetics in project vcell by virtualcell.

the class ReactionPropertiesPanel method updateKineticChoice.

private void updateKineticChoice() {
    KineticsDescription newKineticChoice = (KineticsDescription) getKineticsTypeComboBox().getSelectedItem();
    // 
    if (reactionStep == null || reactionStep.getKinetics().getKineticsDescription().equals(newKineticChoice)) {
        return;
    }
    boolean bLumpedToGeneral = false;
    boolean bGeneralToLumped = false;
    if (newKineticChoice.equals(KineticsDescription.General) && reactionStep.getKinetics().getKineticsDescription().equals(KineticsDescription.GeneralLumped)) {
        bLumpedToGeneral = true;
    } else if (newKineticChoice.equals(KineticsDescription.GeneralLumped) && reactionStep.getKinetics().getKineticsDescription().equals(KineticsDescription.General)) {
        bGeneralToLumped = true;
    }
    boolean bFoundKineticType = false;
    KineticsDescription[] kineticTypes = reactionStep instanceof SimpleReaction ? Simple_Reaction_Kinetic_Types : Flux_Reaction_KineticTypes;
    for (int i = 0; i < kineticTypes.length; i++) {
        if (kineticTypes[i].equals(newKineticChoice)) {
            bFoundKineticType = true;
            break;
        }
    }
    if (!bFoundKineticType) {
        return;
    }
    try {
        Kinetics newKinetics = null;
        if (bLumpedToGeneral) {
            newKinetics = DistributedKinetics.toDistributedKinetics((LumpedKinetics) reactionStep.getKinetics());
        } else if (bGeneralToLumped) {
            newKinetics = LumpedKinetics.toLumpedKinetics((DistributedKinetics) reactionStep.getKinetics());
        } else {
            newKinetics = newKineticChoice.createKinetics(reactionStep);
        }
        reactionStep.setKinetics(newKinetics);
    } catch (Exception exc) {
        handleException(exc);
    }
}
Also used : SimpleReaction(cbit.vcell.model.SimpleReaction) KineticsDescription(cbit.vcell.model.KineticsDescription) GeneralLumpedKinetics(cbit.vcell.model.GeneralLumpedKinetics) LumpedKinetics(cbit.vcell.model.LumpedKinetics) Macroscopic_IRRKinetics(cbit.vcell.model.Macroscopic_IRRKinetics) Kinetics(cbit.vcell.model.Kinetics) MassActionKinetics(cbit.vcell.model.MassActionKinetics) DistributedKinetics(cbit.vcell.model.DistributedKinetics) GeneralKinetics(cbit.vcell.model.GeneralKinetics) GeneralLumpedKinetics(cbit.vcell.model.GeneralLumpedKinetics) HMM_IRRKinetics(cbit.vcell.model.HMM_IRRKinetics) Microscopic_IRRKinetics(cbit.vcell.model.Microscopic_IRRKinetics) LumpedKinetics(cbit.vcell.model.LumpedKinetics) HMM_REVKinetics(cbit.vcell.model.HMM_REVKinetics) PropertyVetoException(java.beans.PropertyVetoException)

Aggregations

LumpedKinetics (cbit.vcell.model.LumpedKinetics)16 DistributedKinetics (cbit.vcell.model.DistributedKinetics)14 Expression (cbit.vcell.parser.Expression)10 ReactionStep (cbit.vcell.model.ReactionStep)9 ModelUnitSystem (cbit.vcell.model.ModelUnitSystem)8 Kinetics (cbit.vcell.model.Kinetics)6 KineticsParameter (cbit.vcell.model.Kinetics.KineticsParameter)6 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)6 ReactionSpec (cbit.vcell.mapping.ReactionSpec)5 Feature (cbit.vcell.model.Feature)5 ReactionParticipant (cbit.vcell.model.ReactionParticipant)5 Vector (java.util.Vector)5 SubVolume (cbit.vcell.geometry.SubVolume)4 MembraneMapping (cbit.vcell.mapping.MembraneMapping)4 FluxReaction (cbit.vcell.model.FluxReaction)4 SimpleReaction (cbit.vcell.model.SimpleReaction)4 PropertyVetoException (java.beans.PropertyVetoException)4 StructureMappingParameter (cbit.vcell.mapping.StructureMapping.StructureMappingParameter)3 CompartmentSubDomain (cbit.vcell.math.CompartmentSubDomain)3 Constant (cbit.vcell.math.Constant)3