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();
}
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);
}
}
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;
}
Aggregations