use of org.vcell.model.rbm.MolecularType in project vcell by virtualcell.
the class MolecularTypeLargeShape method paintNarrowCompartmentRight.
public void paintNarrowCompartmentRight(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Color colorOld = g2.getColor();
Paint paintOld = g2.getPaint();
int z = shapePanel.getZoomFactor();
Rectangle r = getAnchorRectangleRight();
Rectangle2D border = new Rectangle2D.Double(r.x, r.y, r.width, r.height);
// a bit darker for border
Color darker = getDefaultColor(Color.gray);
g2.setColor(darker);
g2.draw(border);
Color lighter;
if (owner instanceof MolecularType) {
if (mt.getAnchors().size() == 0) {
lighter = MolecularComponentLargeShape.componentBad;
} else {
// molecules may be anchored to multiple structures,
// so we leave it white since it may be a mix of membranes and compartments
lighter = Color.white;
}
} else {
// non molecular type, we make sure before calling this that structure is not null
boolean found = false;
for (Structure struct : mt.getAnchors()) {
if (structure.getName().equals(struct.getName())) {
found = true;
break;
}
}
if (!found) {
// molecule can't be in the structure where the species pattern is
lighter = MolecularComponentLargeShape.componentBad;
} else {
if (structure.getTypeName().equals(Structure.TYPE_NAME_MEMBRANE)) {
// 192 208
lighter = new Color(192, 192, 192);
} else {
// 244
lighter = new Color(240, 240, 240);
}
}
}
Rectangle2D filling = new Rectangle2D.Double(r.x, r.y + 1, r.width, r.height - 1);
g2.setPaint(lighter);
g2.fill(filling);
g2.setPaint(paintOld);
g2.setColor(colorOld);
}
use of org.vcell.model.rbm.MolecularType in project vcell by virtualcell.
the class MolecularTypeSmallShape method paintSpecies.
// --------------------------------------------------------------------------------------
// paintComponent is being overridden in the renderer
//
private void paintSpecies(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Color colorOld = g2.getColor();
Color primaryColor = null;
Color border = Color.black;
int finalHeight = baseHeight;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (mt == null && mtp == null) {
if (shapeManager == null) {
primaryColor = Color.green.darker().darker();
} else {
primaryColor = shapeManager.isEditable() ? Color.green.darker().darker() : Color.gray;
}
finalHeight = baseHeight + 3;
Point2D center = new Point2D.Float(xPos + finalHeight / 3, yPos + finalHeight / 3);
float radius = finalHeight * 0.5f;
Point2D focus = new Point2D.Float(xPos + finalHeight / 3 - 1, yPos + finalHeight / 3 - 1);
float[] dist = { 0.1f, 1.0f };
Color[] colors = { Color.white, primaryColor };
RadialGradientPaint p = new RadialGradientPaint(center, radius, focus, dist, colors, CycleMethod.NO_CYCLE);
g2.setPaint(p);
Ellipse2D circle = new Ellipse2D.Double(xPos, yPos, finalHeight, finalHeight);
g2.fill(circle);
Ellipse2D circle2 = new Ellipse2D.Double(xPos - 1, yPos - 1, finalHeight, finalHeight);
g2.setPaint(getDefaultColor(Color.darkGray));
g2.draw(circle2);
g.setColor(colorOld);
return;
} else {
// molecular type, species pattern, observable
if (mt == null || mt.getModel() == null) {
primaryColor = Color.blue.darker().darker();
} else {
if (shapeManager != null) {
if (shapeManager.isShowMoleculeColor()) {
RbmModelContainer rbmmc = mt.getModel().getRbmModelContainer();
List<MolecularType> mtList = rbmmc.getMolecularTypeList();
int index = mtList.indexOf(mt);
index = index % 7;
primaryColor = MolecularTypeLargeShape.colorTable[index].darker().darker();
} else {
primaryColor = Color.gray;
}
border = shapeManager.isEditable() ? Color.black : LargeShapeCanvas.uneditableShape;
} else {
RbmModelContainer rbmmc = mt.getModel().getRbmModelContainer();
List<MolecularType> mtList = rbmmc.getMolecularTypeList();
int index = mtList.indexOf(mt);
index = index % 7;
primaryColor = MolecularTypeLargeShape.colorTable[index].darker().darker();
}
}
if (owner instanceof MolecularType && hasErrorIssues(owner, mt)) {
primaryColor = Color.red;
}
}
GradientPaint p = new GradientPaint(xPos, yPos, primaryColor, xPos, yPos + finalHeight / 2, Color.WHITE, true);
g2.setPaint(p);
RoundRectangle2D rect = new RoundRectangle2D.Float(xPos, yPos, width, finalHeight, cornerArc, cornerArc);
g2.fill(rect);
RoundRectangle2D inner = new RoundRectangle2D.Float(xPos + 1, yPos + 1, width - 2, finalHeight - 2, cornerArc - 3, cornerArc - 3);
g2.setPaint(border);
g2.draw(rect);
g.setColor(colorOld);
for (MolecularComponentSmallShape mcss : componentShapes) {
mcss.paintSelf(g);
}
g.setColor(colorOld);
}
use of org.vcell.model.rbm.MolecularType in project vcell by virtualcell.
the class ReactionCartoon method relistenToMolecule.
// private static Integer getStructureLevel(Structure s) {
// Structure s0 = s;
// int level = 0;
// while (s0 != null) {
// level += 1;
// s0 = s0.getParentStructure();
// }
// return level;
// }
private void relistenToMolecule(PropertyChangeEvent event) {
//
if (event.getPropertyName().equals(Model.RbmModelContainer.PROPERTY_NAME_MOLECULAR_TYPE_LIST)) {
System.out.println("relistenToMolecule, " + event.getSource().getClass() + ": " + event.getPropertyName());
for (MolecularType mt : getModel().getRbmModelContainer().getMolecularTypeList()) {
mt.removePropertyChangeListener(this);
mt.addPropertyChangeListener(this);
}
} else if (event.getSource() instanceof MolecularType) {
MolecularType mt = (MolecularType) event.getSource();
System.out.println("relistenToMolecule " + mt + ", " + event.getPropertyName() + ": " + event.getOldValue() + " -> " + event.getNewValue());
}
}
use of org.vcell.model.rbm.MolecularType in project vcell by virtualcell.
the class RulebasedMathMapping method addSpeciesPatterns.
private HashMap<SpeciesPattern, VolumeParticleSpeciesPattern> addSpeciesPatterns(Domain domain, List<ReactionRule> rrList) throws MathException {
// Particle Molecular Types
//
Model model = getSimulationContext().getModel();
List<RbmObservable> observableList = model.getRbmModelContainer().getObservableList();
List<MolecularType> molecularTypeList = model.getRbmModelContainer().getMolecularTypeList();
for (MolecularType molecularType : molecularTypeList) {
ParticleMolecularType particleMolecularType = new ParticleMolecularType(molecularType.getName());
for (MolecularComponent molecularComponent : molecularType.getComponentList()) {
String pmcName = molecularComponent.getName();
String pmcId = particleMolecularType.getName() + "_" + molecularComponent.getName();
ParticleMolecularComponent particleMolecularComponent = new ParticleMolecularComponent(pmcId, pmcName);
for (ComponentStateDefinition componentState : molecularComponent.getComponentStateDefinitions()) {
ParticleComponentStateDefinition pcsd = particleMolecularComponent.getComponentStateDefinition(componentState.getName());
if (pcsd == null) {
particleMolecularComponent.addComponentStateDefinition(new ParticleComponentStateDefinition(componentState.getName()));
}
}
particleMolecularType.addMolecularComponent(particleMolecularComponent);
}
if (!molecularType.isAnchorAll()) {
List<String> anchorList = new ArrayList<>();
for (Structure struct : molecularType.getAnchors()) {
anchorList.add(struct.getName());
}
particleMolecularType.setAnchorList(anchorList);
}
mathDesc.addParticleMolecularType(particleMolecularType);
}
//
// Assemble list of all Species Patterns (from observables, reaction rules, and seed species).
//
// linked hash set maintains insertion order
LinkedHashMap<SpeciesPattern, Structure> speciesPatternStructureMap = new LinkedHashMap<SpeciesPattern, Structure>();
for (RbmObservable observable : observableList) {
for (SpeciesPattern speciesPattern : observable.getSpeciesPatternList()) {
speciesPatternStructureMap.put(speciesPattern, observable.getStructure());
}
}
for (ReactionRule reactionRule : rrList) {
for (ReactantPattern rp : reactionRule.getReactantPatterns()) {
speciesPatternStructureMap.put(rp.getSpeciesPattern(), rp.getStructure());
}
for (ProductPattern pp : reactionRule.getProductPatterns()) {
speciesPatternStructureMap.put(pp.getSpeciesPattern(), pp.getStructure());
}
}
for (SpeciesContext sc : model.getSpeciesContexts()) {
if (!sc.hasSpeciesPattern()) {
continue;
}
speciesPatternStructureMap.put(sc.getSpeciesPattern(), sc.getStructure());
}
//
// add list of unique speciesPatterns
//
HashMap<String, VolumeParticleSpeciesPattern> speciesPatternVCMLMap = new HashMap<String, VolumeParticleSpeciesPattern>();
HashMap<SpeciesPattern, VolumeParticleSpeciesPattern> speciesPatternMap = new HashMap<SpeciesPattern, VolumeParticleSpeciesPattern>();
String speciesPatternName = "speciesPattern0";
for (SpeciesPattern speciesPattern : speciesPatternStructureMap.keySet()) {
VolumeParticleSpeciesPattern volumeParticleSpeciesPattern = new VolumeParticleSpeciesPattern(speciesPatternName, domain, speciesPatternStructureMap.get(speciesPattern).getName());
for (MolecularTypePattern molecularTypePattern : speciesPattern.getMolecularTypePatterns()) {
ParticleMolecularType particleMolecularType = mathDesc.getParticleMolecularType(molecularTypePattern.getMolecularType().getName());
ParticleMolecularTypePattern particleMolecularTypePattern = new ParticleMolecularTypePattern(particleMolecularType);
String participantMatchLabel = molecularTypePattern.getParticipantMatchLabel();
if (molecularTypePattern.getParticipantMatchLabel() != null) {
particleMolecularTypePattern.setMatchLabel(participantMatchLabel);
}
for (MolecularComponentPattern molecularComponentPattern : molecularTypePattern.getComponentPatternList()) {
MolecularComponent molecularComponent = molecularComponentPattern.getMolecularComponent();
ParticleMolecularComponent particleMolecularComponent = particleMolecularType.getMolecularComponent(molecularComponent.getName());
ParticleMolecularComponentPattern particleMolecularComponentPattern = new ParticleMolecularComponentPattern(particleMolecularComponent);
ComponentStatePattern componentState = molecularComponentPattern.getComponentStatePattern();
if (componentState != null) {
if (componentState.isAny()) {
ParticleComponentStatePattern pcsp = new ParticleComponentStatePattern();
particleMolecularComponentPattern.setComponentStatePattern(pcsp);
} else {
String name = componentState.getComponentStateDefinition().getName();
ParticleComponentStateDefinition pcsd = particleMolecularComponent.getComponentStateDefinition(name);
// ParticleComponentStateDefinition pcsd = new ParticleComponentStateDefinition(componentState.getComponentStateDefinition().getName());
// particleMolecularComponent.addComponentStateDefinition(pcsd);
ParticleComponentStatePattern pcsp = new ParticleComponentStatePattern(pcsd);
particleMolecularComponentPattern.setComponentStatePattern(pcsp);
}
} else {
ParticleComponentStatePattern pcsp = new ParticleComponentStatePattern();
particleMolecularComponentPattern.setComponentStatePattern(pcsp);
}
switch(molecularComponentPattern.getBondType()) {
case Specified:
{
particleMolecularComponentPattern.setBondType(ParticleBondType.Specified);
particleMolecularComponentPattern.setBondId(molecularComponentPattern.getBondId());
break;
}
case Exists:
{
particleMolecularComponentPattern.setBondType(ParticleBondType.Exists);
particleMolecularComponentPattern.setBondId(-1);
break;
}
case None:
{
particleMolecularComponentPattern.setBondType(ParticleBondType.None);
particleMolecularComponentPattern.setBondId(-1);
break;
}
case Possible:
{
particleMolecularComponentPattern.setBondType(ParticleBondType.Possible);
particleMolecularComponentPattern.setBondId(-1);
break;
}
}
particleMolecularTypePattern.addMolecularComponentPattern(particleMolecularComponentPattern);
}
volumeParticleSpeciesPattern.addMolecularTypePattern(particleMolecularTypePattern);
}
String speciesPatternVCML = volumeParticleSpeciesPattern.getVCML("tempName");
VolumeParticleSpeciesPattern uniqueVolumeParticleSpeciesPattern = speciesPatternVCMLMap.get(speciesPatternVCML);
if (uniqueVolumeParticleSpeciesPattern == null) {
speciesPatternVCMLMap.put(speciesPatternVCML, volumeParticleSpeciesPattern);
speciesPatternName = TokenMangler.getNextEnumeratedToken(speciesPatternName);
speciesPatternMap.put(speciesPattern, volumeParticleSpeciesPattern);
} else {
speciesPatternMap.put(speciesPattern, uniqueVolumeParticleSpeciesPattern);
}
}
return speciesPatternMap;
}
use of org.vcell.model.rbm.MolecularType in project vcell by virtualcell.
the class RulebasedTransformer method transform.
private void transform(SimulationContext originalSimContext, SimulationContext transformedSimulationContext, ArrayList<ModelEntityMapping> entityMappings, MathMappingCallback mathMappingCallback) throws PropertyVetoException {
Model newModel = transformedSimulationContext.getModel();
Model originalModel = originalSimContext.getModel();
ModelEntityMapping em = null;
// list of rules created from the reactions; we apply the symmetry factor computed by bionetgen only to these
Set<ReactionRule> fromReactions = new HashSet<>();
for (SpeciesContext newSpeciesContext : newModel.getSpeciesContexts()) {
final SpeciesContext originalSpeciesContext = originalModel.getSpeciesContext(newSpeciesContext.getName());
// map new and old species contexts
em = new ModelEntityMapping(originalSpeciesContext, newSpeciesContext);
entityMappings.add(em);
if (newSpeciesContext.hasSpeciesPattern()) {
// it's perfect already and can't be improved
continue;
}
try {
MolecularType newmt = newModel.getRbmModelContainer().createMolecularType();
newModel.getRbmModelContainer().addMolecularType(newmt, false);
MolecularTypePattern newmtp_sc = new MolecularTypePattern(newmt);
SpeciesPattern newsp_sc = new SpeciesPattern();
newsp_sc.addMolecularTypePattern(newmtp_sc);
newSpeciesContext.setSpeciesPattern(newsp_sc);
RbmObservable newo = new RbmObservable(newModel, "O0_" + newmt.getName() + "_tot", newSpeciesContext.getStructure(), RbmObservable.ObservableType.Molecules);
MolecularTypePattern newmtp_ob = new MolecularTypePattern(newmt);
SpeciesPattern newsp_ob = new SpeciesPattern();
newsp_ob.addMolecularTypePattern(newmtp_ob);
newo.addSpeciesPattern(newsp_ob);
newModel.getRbmModelContainer().addObservable(newo);
// map new observable to old species context
em = new ModelEntityMapping(originalSpeciesContext, newo);
entityMappings.add(em);
} catch (ModelException e) {
e.printStackTrace();
throw new RuntimeException("unable to transform species context: " + e.getMessage());
} catch (PropertyVetoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ReactionSpec[] reactionSpecs = transformedSimulationContext.getReactionContext().getReactionSpecs();
for (ReactionSpec reactionSpec : reactionSpecs) {
if (reactionSpec.isExcluded()) {
// we create rules only from those reactions which are not excluded
continue;
}
ReactionStep rs = reactionSpec.getReactionStep();
String name = rs.getName();
String mangled = TokenMangler.fixTokenStrict(name);
mangled = newModel.getReactionName(mangled);
Kinetics k = rs.getKinetics();
if (!(k instanceof MassActionKinetics)) {
throw new RuntimeException("Only Mass Action Kinetics supported at this time, reaction \"" + rs.getName() + "\" uses kinetic law type \"" + rs.getKinetics().getName() + "\"");
}
boolean bReversible = rs.isReversible();
ReactionRule rr = new ReactionRule(newModel, mangled, rs.getStructure(), bReversible);
fromReactions.add(rr);
MassActionKinetics massActionKinetics = (MassActionKinetics) k;
List<Reactant> rList = rs.getReactants();
List<Product> pList = rs.getProducts();
// counting the stoichiometry - 2A+B means 3 reactants
int numReactants = 0;
for (Reactant r : rList) {
numReactants += r.getStoichiometry();
if (numReactants > 2) {
String message = "NFSim doesn't support more than 2 reactants within a reaction: " + name;
throw new RuntimeException(message);
}
}
int numProducts = 0;
for (Product p : pList) {
numProducts += p.getStoichiometry();
if (bReversible && numProducts > 2) {
String message = "NFSim doesn't support more than 2 products within a reversible reaction: " + name;
throw new RuntimeException(message);
}
}
RateLawType rateLawType = RateLawType.MassAction;
RbmKineticLaw kineticLaw = new RbmKineticLaw(rr, rateLawType);
try {
String forwardRateName = massActionKinetics.getForwardRateParameter().getName();
Expression forwardRateExp = massActionKinetics.getForwardRateParameter().getExpression();
String reverseRateName = massActionKinetics.getReverseRateParameter().getName();
Expression reverseRateExp = massActionKinetics.getReverseRateParameter().getExpression();
LocalParameter fR = kineticLaw.getLocalParameter(RbmKineticLawParameterType.MassActionForwardRate);
fR.setName(forwardRateName);
LocalParameter rR = kineticLaw.getLocalParameter(RbmKineticLawParameterType.MassActionReverseRate);
rR.setName(reverseRateName);
if (rs.hasReactant()) {
kineticLaw.setParameterValue(fR, forwardRateExp, true);
}
if (rs.hasProduct()) {
kineticLaw.setParameterValue(rR, reverseRateExp, true);
}
//
for (KineticsParameter reaction_p : massActionKinetics.getKineticsParameters()) {
if (reaction_p.getRole() == Kinetics.ROLE_UserDefined) {
LocalParameter rule_p = kineticLaw.getLocalParameter(reaction_p.getName());
if (rule_p == null) {
//
// after lazy parameter creation we didn't find a user-defined rule parameter with this same name.
//
// there must be a global symbol with the same name, that the local reaction parameter has overridden.
//
ParameterContext.LocalProxyParameter rule_proxy_parameter = null;
for (ProxyParameter proxyParameter : kineticLaw.getProxyParameters()) {
if (proxyParameter.getName().equals(reaction_p.getName())) {
rule_proxy_parameter = (LocalProxyParameter) proxyParameter;
}
}
if (rule_proxy_parameter != null) {
// we want to convert to local
boolean bConvertToGlobal = false;
kineticLaw.convertParameterType(rule_proxy_parameter, bConvertToGlobal);
} else {
// could find neither local parameter nor proxy parameter
throw new RuntimeException("user defined parameter " + reaction_p.getName() + " from reaction " + rs.getName() + " didn't map to a reactionRule parameter");
}
} else if (rule_p.getRole() == RbmKineticLawParameterType.UserDefined) {
kineticLaw.setParameterValue(rule_p, reaction_p.getExpression(), true);
rule_p.setUnitDefinition(reaction_p.getUnitDefinition());
} else {
throw new RuntimeException("user defined parameter " + reaction_p.getName() + " from reaction " + rs.getName() + " mapped to a reactionRule parameter with unexpected role " + rule_p.getRole().getDescription());
}
}
}
} catch (ExpressionException e) {
e.printStackTrace();
throw new RuntimeException("Problem attempting to set RbmKineticLaw expression: " + e.getMessage());
}
rr.setKineticLaw(kineticLaw);
KineticsParameter[] kpList = k.getKineticsParameters();
ModelParameter[] mpList = rs.getModel().getModelParameters();
ModelParameter mp = rs.getModel().getModelParameter(kpList[0].getName());
ReactionParticipant[] rpList = rs.getReactionParticipants();
for (ReactionParticipant p : rpList) {
if (p instanceof Reactant) {
int stoichiometry = p.getStoichiometry();
for (int i = 0; i < stoichiometry; i++) {
SpeciesPattern speciesPattern = new SpeciesPattern(rs.getModel(), p.getSpeciesContext().getSpeciesPattern());
ReactantPattern reactantPattern = new ReactantPattern(speciesPattern, p.getStructure());
rr.addReactant(reactantPattern);
}
} else if (p instanceof Product) {
int stoichiometry = p.getStoichiometry();
for (int i = 0; i < stoichiometry; i++) {
SpeciesPattern speciesPattern = new SpeciesPattern(rs.getModel(), p.getSpeciesContext().getSpeciesPattern());
ProductPattern productPattern = new ProductPattern(speciesPattern, p.getStructure());
rr.addProduct(productPattern);
}
}
}
// commented code below is probably obsolete, we verify (above) in the reaction the number of participants,
// no need to do it again in the corresponding rule
// if(rr.getReactantPatterns().size() > 2) {
// String message = "NFSim doesn't support more than 2 reactants within a reaction: " + name;
// throw new RuntimeException(message);
// }
// if(rr.getProductPatterns().size() > 2) {
// String message = "NFSim doesn't support more than 2 products within a reaction: " + name;
// throw new RuntimeException(message);
// }
newModel.removeReactionStep(rs);
newModel.getRbmModelContainer().addReactionRule(rr);
}
for (ReactionRuleSpec rrs : transformedSimulationContext.getReactionContext().getReactionRuleSpecs()) {
if (rrs == null) {
continue;
}
ReactionRule rr = rrs.getReactionRule();
if (rrs.isExcluded()) {
// delete those rules which are disabled (excluded) in the Specifications / Reaction table
newModel.getRbmModelContainer().removeReactionRule(rr);
continue;
}
}
// now that we generated the rules we can delete the reaction steps they're coming from
for (ReactionStep rs : newModel.getReactionSteps()) {
newModel.removeReactionStep(rs);
}
try {
// we invoke bngl just for the purpose of generating the xml file, which we'll then use to extract the symmetry factor
generateNetwork(transformedSimulationContext, fromReactions, mathMappingCallback);
} catch (ClassNotFoundException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Finished RuleBased Transformer.");
}
Aggregations