Search in sources :

Example 11 with ReactionRuleSpec

use of cbit.vcell.mapping.ReactionRuleSpec in project vcell by virtualcell.

the class RbmNetworkGenerator method writeBngl_internal.

public static void writeBngl_internal(SimulationContext simulationContext, PrintWriter writer, Map<FakeReactionRuleRateParameter, LocalParameter> kineticsParameterMap, Map<FakeSeedSpeciesInitialConditionsParameter, Pair<SpeciesContext, Expression>> speciesEquivalenceMap, NetworkGenerationRequirements networkGenerationRequirements, CompartmentMode compartmentMode) {
    String callerClassName = new Exception().getStackTrace()[1].getClassName();
    String networkTransformerClassName = NetworkTransformer.class.getName();
    String rulebasedTransformerClassName = RulebasedTransformer.class.getName();
    if (!callerClassName.equals(networkTransformerClassName) && !callerClassName.equals(rulebasedTransformerClassName)) {
        throw new UnsupportedOperationException("This method may only be called from within a " + networkTransformerClassName + " or " + rulebasedTransformerClassName + " instance.");
    }
    Model model = simulationContext.getModel();
    RbmModelContainer rbmModelContainer = model.getRbmModelContainer();
    checkConsistency(model);
    // first we prepare the fake parameters we need to maintain the relationship between the species context and the seed species
    List<FakeSeedSpeciesInitialConditionsParameter> fakeParameterList = new ArrayList<FakeSeedSpeciesInitialConditionsParameter>();
    List<String> seedSpeciesList = new ArrayList<String>();
    SpeciesContext[] speciesContexts = model.getSpeciesContexts();
    for (int i = 0; i < speciesContexts.length; i++) {
        SpeciesContext sc = speciesContexts[i];
        if (!sc.hasSpeciesPattern()) {
            continue;
        }
        SpeciesContextSpec scs = simulationContext.getReactionContext().getSpeciesContextSpec(sc);
        Expression initialConcentration = scs.getParameter(SpeciesContextSpec.ROLE_InitialConcentration).getExpression();
        // fake initial values for the seed species, we need to present them to bngl as parameters
        FakeSeedSpeciesInitialConditionsParameter fakeSeedSpeciesParam = new FakeSeedSpeciesInitialConditionsParameter(sc.getName());
        Pair<SpeciesContext, Expression> p = new Pair<SpeciesContext, Expression>(sc, initialConcentration);
        speciesEquivalenceMap.put(fakeSeedSpeciesParam, p);
        String modified;
        if (compartmentMode == CompartmentMode.show) {
            modified = RbmUtils.toBnglString(sc.getSpeciesPattern(), null, CompartmentMode.hide, 0);
            modified = "@" + sc.getStructure().getName() + ":" + modified;
        } else if (compartmentMode == CompartmentMode.asSite) {
            modified = RbmUtils.toBnglString(sc.getSpeciesPattern(), sc.getStructure(), CompartmentMode.asSite, 0);
        } else {
            // CompartmentMode.hide
            modified = RbmUtils.toBnglString(sc.getSpeciesPattern(), null, CompartmentMode.hide, 0);
        }
        modified += " " + fakeSeedSpeciesParam.fakeParameterName;
        // we label the seed species with the index
        modified = (i + 1) + " " + modified;
        // we build the seed species list now, we write it later (in the BEGIN SPECIES block)
        seedSpeciesList.add(modified);
        fakeParameterList.add(fakeSeedSpeciesParam);
    }
    // second we produce the bngl file
    writer.println(BEGIN_MODEL);
    writer.println();
    for (ReactionRuleSpec rrs : simulationContext.getReactionContext().getReactionRuleSpecs()) {
        if (!rrs.isExcluded()) {
            ReactionRule reactionRule = rrs.getReactionRule();
            RbmKineticLaw kineticLaw = reactionRule.getKineticLaw();
            switch(kineticLaw.getRateLawType()) {
                case MassAction:
                    {
                        FakeReactionRuleRateParameter fakeRateParameterForward = new FakeReactionRuleRateParameter(reactionRule, RbmKineticLawParameterType.MassActionForwardRate);
                        LocalParameter origForwardRateParameter = kineticLaw.getLocalParameter(RbmKineticLawParameterType.MassActionForwardRate);
                        kineticsParameterMap.put(fakeRateParameterForward, origForwardRateParameter);
                        if (reactionRule.isReversible()) {
                            FakeReactionRuleRateParameter fakeRateParameterReverse = new FakeReactionRuleRateParameter(reactionRule, RbmKineticLawParameterType.MassActionReverseRate);
                            LocalParameter origReverseRateParameter = kineticLaw.getLocalParameter(RbmKineticLawParameterType.MassActionReverseRate);
                            kineticsParameterMap.put(fakeRateParameterReverse, origReverseRateParameter);
                        }
                        break;
                    }
                case MichaelisMenten:
                    {
                        FakeReactionRuleRateParameter fakeParameterVmax = new FakeReactionRuleRateParameter(reactionRule, RbmKineticLawParameterType.MichaelisMentenVmax);
                        FakeReactionRuleRateParameter fakeParameterKm = new FakeReactionRuleRateParameter(reactionRule, RbmKineticLawParameterType.MichaelisMentenKm);
                        LocalParameter origVmaxParameter = kineticLaw.getLocalParameter(RbmKineticLawParameterType.MichaelisMentenVmax);
                        LocalParameter origKmParameter = kineticLaw.getLocalParameter(RbmKineticLawParameterType.MichaelisMentenKm);
                        kineticsParameterMap.put(fakeParameterVmax, origVmaxParameter);
                        kineticsParameterMap.put(fakeParameterKm, origKmParameter);
                        break;
                    }
                default:
                    {
                        throw new RuntimeException("kinetic law type " + kineticLaw.getRateLawType().name() + " not yet implemented");
                    }
            }
        }
    }
    if (compartmentMode == CompartmentMode.show) {
        RbmNetworkGenerator.writeCompartments(writer, model, simulationContext);
    }
    writer.println(BEGIN_PARAMETERS);
    int speciesLimit = simulationContext.getNetworkConstraints().getSpeciesLimit();
    int reactionsLimit = simulationContext.getNetworkConstraints().getReactionsLimit();
    writer.println(NetworkConstraints.SPECIES_LIMIT_PARAMETER + "\t\t" + speciesLimit);
    writer.println(NetworkConstraints.REACTIONS_LIMIT_PARAMETER + "\t\t" + reactionsLimit);
    // the fake parameters used for reaction rule kinetics
    for (FakeReactionRuleRateParameter p : kineticsParameterMap.keySet()) {
        writer.println(p.fakeParameterName + "\t\t1");
    }
    // the fake parameters used at initial values for the seed species
    for (FakeSeedSpeciesInitialConditionsParameter s : fakeParameterList) {
        writer.println(s.fakeParameterName + "\t\t1");
    }
    writer.println(END_PARAMETERS);
    writer.println();
    RbmNetworkGenerator.writeMolecularTypes(writer, model, compartmentMode);
    // write modified version of seed species while maintaining the connection between the species context and the real seed species
    writer.println(BEGIN_SPECIES);
    for (String s : seedSpeciesList) {
        writer.println(s);
    }
    writer.println(END_SPECIES);
    writer.println();
    RbmNetworkGenerator.writeObservables(writer, rbmModelContainer, compartmentMode);
    RbmNetworkGenerator.writeReactions_internal(writer, simulationContext, compartmentMode);
    writer.println(END_MODEL);
    writer.println();
    if (callerClassName.equals(networkTransformerClassName)) {
        RbmNetworkGenerator.writeNetworkConstraints(writer, rbmModelContainer, simulationContext, networkGenerationRequirements);
    } else if (callerClassName.equals(rulebasedTransformerClassName)) {
        writer.println();
        writer.println("writeXML()");
    }
    writer.println();
}
Also used : ReactionRule(cbit.vcell.model.ReactionRule) ReactionRuleSpec(cbit.vcell.mapping.ReactionRuleSpec) ArrayList(java.util.ArrayList) SpeciesContext(cbit.vcell.model.SpeciesContext) RbmKineticLaw(cbit.vcell.model.RbmKineticLaw) SpeciesContextSpec(cbit.vcell.mapping.SpeciesContextSpec) LocalParameter(cbit.vcell.mapping.ParameterContext.LocalParameter) RbmModelContainer(cbit.vcell.model.Model.RbmModelContainer) Expression(cbit.vcell.parser.Expression) BioModel(cbit.vcell.biomodel.BioModel) Model(cbit.vcell.model.Model) Pair(org.vcell.util.Pair)

Example 12 with ReactionRuleSpec

use of cbit.vcell.mapping.ReactionRuleSpec in project vcell by virtualcell.

the class ModelProcessSpecsTableModel method setValueAt.

public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
    ModelProcessSpec modelProcessSpec = getValueAt(rowIndex);
    ColumnType columnType = columns.get(columnIndex);
    try {
        switch(columnType) {
            case COLUMN_ENABLED:
                {
                    boolean bEnabled = ((Boolean) aValue).booleanValue();
                    if (modelProcessSpec instanceof ReactionSpec) {
                        ReactionSpec reactionSpec = (ReactionSpec) modelProcessSpec;
                        if (bEnabled) {
                            reactionSpec.setReactionMapping(ReactionSpec.INCLUDED);
                        } else {
                            reactionSpec.setReactionMapping(ReactionSpec.EXCLUDED);
                        }
                    } else if (modelProcessSpec instanceof ReactionRuleSpec) {
                        ReactionRuleSpec reactionRuleSpec = (ReactionRuleSpec) modelProcessSpec;
                        if (bEnabled) {
                            reactionRuleSpec.setReactionRuleMapping(ReactionRuleMappingType.INCLUDED);
                        } else {
                            reactionRuleSpec.setReactionRuleMapping(ReactionRuleMappingType.EXCLUDED);
                        }
                    }
                    fireTableRowsUpdated(rowIndex, rowIndex);
                    break;
                }
            case COLUMN_FAST:
                {
                    boolean bFast = ((Boolean) aValue).booleanValue();
                    if (modelProcessSpec instanceof ReactionSpec) {
                        ReactionSpec reactionSpec = (ReactionSpec) modelProcessSpec;
                        if (bFast) {
                            reactionSpec.setReactionMapping(ReactionSpec.FAST);
                        } else {
                            reactionSpec.setReactionMapping(ReactionSpec.INCLUDED);
                        }
                    }
                    fireTableRowsUpdated(rowIndex, rowIndex);
                    break;
                }
        }
    } catch (java.beans.PropertyVetoException e) {
        e.printStackTrace(System.out);
    }
}
Also used : ModelProcessSpec(cbit.vcell.mapping.ModelProcessSpec) ReactionSpec(cbit.vcell.mapping.ReactionSpec) ReactionRuleSpec(cbit.vcell.mapping.ReactionRuleSpec)

Example 13 with ReactionRuleSpec

use of cbit.vcell.mapping.ReactionRuleSpec in project vcell by virtualcell.

the class SimContextTable method getAppComponentsForDatabase.

/**
 * getXMLStringForDatabase : this returns the XML string for the container element <AppComponents> for application-related protocols
 * and other extra specifications. For now, BioEvents falls under this category, so the BioEvents element (list of bioevents)
 * is obtained from the simContext (via the XMLProducer) and added as content to <AppComponents> element. The <AppComponents>
 * element is converted to XML string which is the return value of this method. This string is stored in the database in the
 * SimContextTable. Instead of creating new fields for each possible application component, it is convenient to store them
 * all under a blanket XML element <AppComponents>.
 * @param simContext
 * @return
 */
public static String getAppComponentsForDatabase(SimulationContext simContext) {
    Element appComponentsElement = new Element(XMLTags.ApplicationComponents);
    // for now, create the element only if application is stochastic. Can change it later.
    if (simContext.isStoch()) {
        // add 'randomizeInitCondition' flag only if simContext is non-spatial
        if (simContext.getGeometry().getDimension() == 0) {
            Element appRelatedFlagsElement = new Element(XMLTags.ApplicationSpecificFlagsTag);
            if (simContext.isRandomizeInitCondition()) {
                appRelatedFlagsElement.setAttribute(XMLTags.RandomizeInitConditionTag, "true");
            } else {
                appRelatedFlagsElement.setAttribute(XMLTags.RandomizeInitConditionTag, "false");
            }
            appComponentsElement.addContent(appRelatedFlagsElement);
        }
    }
    if (simContext.isInsufficientIterations()) {
        appComponentsElement.setAttribute(XMLTags.InsufficientIterationsTag, "true");
    } else {
        appComponentsElement.setAttribute(XMLTags.InsufficientIterationsTag, "false");
    }
    if (simContext.isInsufficientMaxMolecules()) {
        appComponentsElement.setAttribute(XMLTags.InsufficientMaxMoleculesTag, "true");
    } else {
        appComponentsElement.setAttribute(XMLTags.InsufficientMaxMoleculesTag, "false");
    }
    if (simContext.isUsingMassConservationModelReduction()) {
        appComponentsElement.setAttribute(XMLTags.MassConservationModelReductionTag, "true");
    } else {
        appComponentsElement.setAttribute(XMLTags.MassConservationModelReductionTag, "false");
    }
    Xmlproducer xmlProducer = new Xmlproducer(false);
    NetworkConstraints constraints = simContext.getNetworkConstraints();
    if (constraints != null) {
        appComponentsElement.addContent(xmlProducer.getXML(constraints, simContext));
    }
    // first fill in bioevents from simContext
    BioEvent[] bioEvents = simContext.getBioEvents();
    if (bioEvents != null && bioEvents.length > 0) {
        try {
            Element bioEventsElement = xmlProducer.getXML(bioEvents);
            appComponentsElement.addContent(bioEventsElement);
        } catch (XmlParseException e) {
            throw new RuntimeException("Error generating XML for bioevents : " + e.getMessage(), e);
        }
    }
    SimulationContextParameter[] appParams = simContext.getSimulationContextParameters();
    if (appParams != null && appParams.length > 0) {
        try {
            Element appParamsElement = xmlProducer.getXML(appParams);
            appComponentsElement.addContent(appParamsElement);
        } catch (Exception e) {
            throw new RuntimeException("Error generating XML for application parameters : " + e.getMessage(), e);
        }
    }
    SpatialObject[] spatialObjects = simContext.getSpatialObjects();
    if (spatialObjects != null && spatialObjects.length > 0) {
        try {
            Element spatialObjectsElement = xmlProducer.getXML(spatialObjects);
            appComponentsElement.addContent(spatialObjectsElement);
        } catch (XmlParseException e) {
            throw new RuntimeException("Error generating XML for spatialObjects : " + e.getMessage(), e);
        }
    }
    SpatialProcess[] spatialProcesses = simContext.getSpatialProcesses();
    if (spatialProcesses != null && spatialProcesses.length > 0) {
        try {
            Element spatialProcessesElement = xmlProducer.getXML(spatialProcesses);
            appComponentsElement.addContent(spatialProcessesElement);
        } catch (XmlParseException e) {
            throw new RuntimeException("Error generating XML for spatialProcesses : " + e.getMessage(), e);
        }
    }
    // microscope measurements
    Element element = xmlProducer.getXML(simContext.getMicroscopeMeasurement());
    appComponentsElement.addContent(element);
    // rate rules
    RateRule[] rateRules = simContext.getRateRules();
    if (rateRules != null && rateRules.length > 0) {
        try {
            Element rateRulesElement = xmlProducer.getXML(rateRules);
            appComponentsElement.addContent(rateRulesElement);
        } catch (XmlParseException e) {
            throw new RuntimeException("Error generating XML for bioevents : " + e.getMessage(), e);
        }
    }
    AssignmentRule[] assignmentRules = simContext.getAssignmentRules();
    if (assignmentRules != null && assignmentRules.length > 0) {
        try {
            Element assignmentRulesElement = xmlProducer.getXML(assignmentRules);
            appComponentsElement.addContent(assignmentRulesElement);
        } catch (XmlParseException e) {
            throw new RuntimeException("Error generating XML for bioevents : " + e.getMessage(), e);
        }
    }
    // ReactionRuleSpecs
    ReactionRuleSpec[] reactionRuleSpecs = simContext.getReactionContext().getReactionRuleSpecs();
    if (reactionRuleSpecs != null && reactionRuleSpecs.length > 0) {
        Element reactionRuleSpecsElement = xmlProducer.getXML(reactionRuleSpecs);
        appComponentsElement.addContent(reactionRuleSpecsElement);
    }
    String appComponentsXMLStr = null;
    if (appComponentsElement.getContent() != null) {
        appComponentsXMLStr = XmlUtil.xmlToString(appComponentsElement);
    }
    return appComponentsXMLStr;
}
Also used : Xmlproducer(cbit.vcell.xml.Xmlproducer) AssignmentRule(cbit.vcell.mapping.AssignmentRule) ReactionRuleSpec(cbit.vcell.mapping.ReactionRuleSpec) Element(org.jdom.Element) XmlParseException(cbit.vcell.xml.XmlParseException) SimulationContextParameter(cbit.vcell.mapping.SimulationContext.SimulationContextParameter) PropertyVetoException(java.beans.PropertyVetoException) SQLException(java.sql.SQLException) XmlParseException(cbit.vcell.xml.XmlParseException) DataAccessException(org.vcell.util.DataAccessException) SpatialObject(cbit.vcell.mapping.spatial.SpatialObject) SpatialProcess(cbit.vcell.mapping.spatial.processes.SpatialProcess) RateRule(cbit.vcell.mapping.RateRule) BioEvent(cbit.vcell.mapping.BioEvent) NetworkConstraints(org.vcell.model.rbm.NetworkConstraints)

Aggregations

ReactionRuleSpec (cbit.vcell.mapping.ReactionRuleSpec)13 Element (org.jdom.Element)6 ReactionSpec (cbit.vcell.mapping.ReactionSpec)5 BioEvent (cbit.vcell.mapping.BioEvent)4 SpatialObject (cbit.vcell.mapping.spatial.SpatialObject)4 SpatialProcess (cbit.vcell.mapping.spatial.processes.SpatialProcess)4 ReactionRule (cbit.vcell.model.ReactionRule)4 AssignmentRule (cbit.vcell.mapping.AssignmentRule)3 RateRule (cbit.vcell.mapping.RateRule)3 SimulationContext (cbit.vcell.mapping.SimulationContext)3 SimulationContextParameter (cbit.vcell.mapping.SimulationContext.SimulationContextParameter)3 SpeciesContextSpec (cbit.vcell.mapping.SpeciesContextSpec)3 NetworkConstraints (org.vcell.model.rbm.NetworkConstraints)3 BioModel (cbit.vcell.biomodel.BioModel)2 DataSymbol (cbit.vcell.data.DataSymbol)2 ParameterEstimationTask (cbit.vcell.modelopt.ParameterEstimationTask)2 ImageException (cbit.image.ImageException)1 VCMetaData (cbit.vcell.biomodel.meta.VCMetaData)1 PathwayData (cbit.vcell.client.desktop.biomodel.BioModelEditorPathwayCommonsPanel.PathwayData)1 DocumentEditorTreeFolderClass (cbit.vcell.client.desktop.biomodel.DocumentEditorTreeModel.DocumentEditorTreeFolderClass)1