use of org.vcell.model.rbm.MolecularTypePattern in project vcell by virtualcell.
the class ReactionRule method resolveMatches.
private void resolveMatches() {
// for each molecular type, make a lists of all corresponding molecular type patterns, one for reactants, one for products
Map<MolecularType, List<MolecularTypePattern>> reactantMap = new LinkedHashMap<MolecularType, List<MolecularTypePattern>>();
Map<MolecularType, List<MolecularTypePattern>> productMap = new LinkedHashMap<MolecularType, List<MolecularTypePattern>>();
for (MolecularType mt : model.getRbmModelContainer().getMolecularTypeList()) {
List<MolecularTypePattern> mtpReactantList = populateMaps(mt, ReactionRuleParticipantType.Reactant);
reactantMap.put(mt, mtpReactantList);
List<MolecularTypePattern> mtpProductList = populateMaps(mt, ReactionRuleParticipantType.Product);
productMap.put(mt, mtpProductList);
}
// we'll build a list with all tha mapped pairs so that we won't have to worry about them
Map<String, MolecularTypePattern> pairAlreadyMapped = new LinkedHashMap<String, MolecularTypePattern>();
// if, for a certain MolecularType, we have 0 or 1 MoleculartypePatterns both in reactant and product we ignore because it's trivial
for (MolecularType mt : model.getRbmModelContainer().getMolecularTypeList()) {
if (reactantMap.get(mt).size() < 2 && productMap.get(mt).size() < 2) {
// we make sure that the match flag is "*" (indifferent) for these
if (reactantMap.get(mt).size() == 1) {
reactantMap.get(mt).get(0).setParticipantMatchLabel("*");
}
if (productMap.get(mt).size() == 1) {
productMap.get(mt).get(0).setParticipantMatchLabel("*");
}
continue;
}
// those patterns with a valid match we take out from this lists and we put them in separate lists
// if we find orphans either in the reactant or product list we set them to indifferent / any
List<MolecularTypePattern> mtpReactantList = reactantMap.get(mt);
List<MolecularTypePattern> mtpProductList = productMap.get(mt);
List<MolecularTypePattern> mtpReactantsToRemove = new ArrayList<MolecularTypePattern>();
for (MolecularTypePattern mtpr : mtpReactantList) {
if (mtpr.hasExplicitParticipantMatch()) {
String matchKey = mtpr.getParticipantMatchLabel();
// we look for a match in the products
MolecularTypePattern mtpp = findMatch(matchKey, mtpProductList);
if (mtpp == null) {
// no product has the matching key, so this reactant must be orphan
// we set it to any
mtpr.setParticipantMatchLabel("*");
} else {
// we add this pair to the map of already mapped pairs and take it out from here
if (pairAlreadyMapped.containsKey(matchKey)) {
// key already in use, we generate a new one
matchKey = generateNewMatchKey(pairAlreadyMapped);
mtpr.setParticipantMatchLabel(matchKey);
mtpp.setParticipantMatchLabel(matchKey);
}
// we'll remove the reactants after finishing the iterations
mtpReactantsToRemove.add(mtpr);
mtpProductList.remove(mtpp);
pairAlreadyMapped.put(matchKey, mtpr);
}
}
}
for (MolecularTypePattern mtpr : mtpReactantsToRemove) {
// now we can remove the reactants without generating concurrent exceptions
mtpReactantList.remove(mtpr);
}
// mtpProductList may still contain orphans with match flag set to something not "any", we need to fix them
for (MolecularTypePattern mtpp : mtpProductList) {
if (mtpp.hasExplicitParticipantMatch()) {
String matchKey = mtpp.getParticipantMatchLabel();
// we look for a match in the reactants
MolecularTypePattern mtpr = findMatch(matchKey, mtpReactantList);
if (mtpr == null) {
// no reactant has the matching key, so this product must be orphan
// we set it to any
mtpp.setParticipantMatchLabel("*");
} else {
throw new RuntimeException("Found a reactant match for an orphan product, this should not be possible.");
}
}
}
// we try to match orphaned reactant mtp and product mtp of the same kind, as long neither list is empty
for (MolecularTypePattern mtpr : mtpReactantList) {
if (mtpProductList.isEmpty()) {
// nothing left to match
break;
}
MolecularTypePattern mtpp = mtpProductList.get(0);
if (mtpp != null) {
// these 2 are orphans and can be matched to each other
String matchKey = generateNewMatchKey(pairAlreadyMapped);
mtpr.setParticipantMatchLabel(matchKey);
mtpp.setParticipantMatchLabel(matchKey);
// mtpReactantList.remove(mtpr); // we can't remove them because of concurrent exception and actually we don't need to
mtpProductList.remove(mtpp);
pairAlreadyMapped.put(matchKey, mtpr);
}
}
}
}
use of org.vcell.model.rbm.MolecularTypePattern in project vcell by virtualcell.
the class ReactionRule method deleteComponentFromPatterns.
public boolean deleteComponentFromPatterns(MolecularType mt, MolecularComponent mc) {
for (ProductPattern pp : getProductPatterns()) {
SpeciesPattern sp = pp.getSpeciesPattern();
for (MolecularTypePattern mtp : sp.getMolecularTypePatterns()) {
if (mtp.getMolecularType() == mt) {
List<MolecularComponentPattern> componentPatterns = mtp.getComponentPatternList();
for (Iterator<MolecularComponentPattern> iterator = componentPatterns.iterator(); iterator.hasNext(); ) {
MolecularComponentPattern mcp = iterator.next();
if (mcp.getMolecularComponent() == mc) {
iterator.remove();
}
}
}
}
sp.resolveBonds();
}
for (ReactantPattern rp : getReactantPatterns()) {
SpeciesPattern sp = rp.getSpeciesPattern();
for (MolecularTypePattern mtp : sp.getMolecularTypePatterns()) {
if (mtp.getMolecularType() == mt) {
List<MolecularComponentPattern> componentPatterns = mtp.getComponentPatternList();
for (Iterator<MolecularComponentPattern> iterator = componentPatterns.iterator(); iterator.hasNext(); ) {
MolecularComponentPattern mcp = iterator.next();
if (mcp.getMolecularComponent() == mc) {
iterator.remove();
}
}
}
}
sp.resolveBonds();
}
return true;
}
use of org.vcell.model.rbm.MolecularTypePattern in project vcell by virtualcell.
the class ReactionRule method checkProductPatterns.
public void checkProductPatterns(IssueContext issueContext, List<Issue> issueList) {
issueContext = issueContext.newChildContext(ContextType.ReactionRule, this);
int reactantCnt = 0;
for (ReactantPattern rp : reactantPatterns) {
reactantCnt += rp.getSpeciesPattern().getMolecularTypePatterns().size();
}
int productCnt = 0;
int cnt = 0;
// StringBuilder warning = new StringBuilder();
for (ProductPattern pp : productPatterns) {
productCnt += pp.getSpeciesPattern().getMolecularTypePatterns().size();
++cnt;
if (pp.getSpeciesPattern().getMolecularTypePatterns().size() == 0) {
String msg = "Product " + cnt + " does not have any molecules.\n";
// warning.append(msg);
if (issueList != null) {
issueList.add(new Issue(this, issueContext, IssueCategory.Identifiers, msg, Issue.Severity.ERROR));
}
} else {
pp.getSpeciesPattern().checkSpeciesPattern(issueContext, issueList);
for (MolecularTypePattern mtpThis : pp.getSpeciesPattern().getMolecularTypePatterns()) {
checkComponentStateConsistency(issueContext, issueList, mtpThis);
}
}
}
if (productCnt != reactantCnt) {
String msg = "The number of molecules in products does not match the number of molecules in reactants.";
// warning.append(msg);
if (issueList != null) {
issueList.add(new Issue(this, issueContext, IssueCategory.Identifiers, msg, Issue.SEVERITY_INFO));
}
}
// setProductWarning(warning.length() == 0 ? null : warning.toString());
}
use of org.vcell.model.rbm.MolecularTypePattern in project vcell by virtualcell.
the class ReactionRule method findStateUsage.
public void findStateUsage(MolecularType mt, MolecularComponent mc, ComponentStateDefinition csd, Map<String, Pair<Displayable, SpeciesPattern>> usedHere) {
for (ProductPattern pp : getProductPatterns()) {
SpeciesPattern sp = pp.getSpeciesPattern();
for (MolecularTypePattern mtp : sp.getMolecularTypePatterns()) {
if (mtp.getMolecularType() == mt) {
List<MolecularComponentPattern> componentPatterns = mtp.getComponentPatternList();
for (MolecularComponentPattern mcp : componentPatterns) {
if (mcp.isImplied()) {
// we don't care about these
continue;
}
if (mcp.getMolecularComponent() == mc) {
// found mc in use
// now let's look at component state definition
ComponentStatePattern csp = mcp.getComponentStatePattern();
if (csp == null) {
continue;
}
if (csp.getComponentStateDefinition() == csd) {
String key = getDisplayType() + getDisplayName() + sp.getDisplayName();
usedHere.put(key, new Pair<Displayable, SpeciesPattern>(this, sp));
}
}
}
}
}
}
for (ReactantPattern rp : getReactantPatterns()) {
SpeciesPattern sp = rp.getSpeciesPattern();
for (MolecularTypePattern mtp : sp.getMolecularTypePatterns()) {
if (mtp.getMolecularType() == mt) {
List<MolecularComponentPattern> componentPatterns = mtp.getComponentPatternList();
for (MolecularComponentPattern mcp : componentPatterns) {
if (mcp.isImplied()) {
continue;
}
if (mcp.getMolecularComponent() == mc) {
ComponentStatePattern csp = mcp.getComponentStatePattern();
if (csp == null) {
continue;
}
if (csp.getComponentStateDefinition() == csd) {
String key = getDisplayType() + getDisplayName() + sp.getDisplayName();
usedHere.put(key, new Pair<Displayable, SpeciesPattern>(this, sp));
}
}
}
}
}
}
}
use of org.vcell.model.rbm.MolecularTypePattern in project vcell by virtualcell.
the class ReactionRule method deleteStateFromPatterns.
public boolean deleteStateFromPatterns(MolecularType mt, MolecularComponent mc, ComponentStateDefinition csd) {
for (ProductPattern pp : getProductPatterns()) {
SpeciesPattern sp = pp.getSpeciesPattern();
for (MolecularTypePattern mtp : sp.getMolecularTypePatterns()) {
if (mtp.getMolecularType() == mt) {
List<MolecularComponentPattern> componentPatterns = mtp.getComponentPatternList();
for (MolecularComponentPattern mcp : componentPatterns) {
if (!(mcp.getMolecularComponent() == mc)) {
// not our mc
continue;
}
ComponentStatePattern csp = mcp.getComponentStatePattern();
if (csp == null) {
continue;
}
if (csp.isAny()) {
if (mc.getComponentStateDefinitions().size() == 1) {
// we are about to delete the last possible state, so we set the ComponentStatePattern to null
mcp.setComponentStatePattern(null);
}
continue;
}
if (csp.getComponentStateDefinition() == csd) {
if (mc.getComponentStateDefinitions().size() == 1) {
// we are about to delete the last possible state, so we set the ComponentStatePattern to null
mcp.setComponentStatePattern(null);
} else {
// some other state is still available, we set the ComponentStatePattern to Any and let the user deal with it
csp = new ComponentStatePattern();
mcp.setComponentStatePattern(csp);
}
}
}
}
}
}
for (ReactantPattern rp : getReactantPatterns()) {
SpeciesPattern sp = rp.getSpeciesPattern();
for (MolecularTypePattern mtp : sp.getMolecularTypePatterns()) {
if (mtp.getMolecularType() == mt) {
List<MolecularComponentPattern> componentPatterns = mtp.getComponentPatternList();
for (MolecularComponentPattern mcp : componentPatterns) {
if (!(mcp.getMolecularComponent() == mc)) {
continue;
}
ComponentStatePattern csp = mcp.getComponentStatePattern();
if (csp == null) {
continue;
}
if (csp.isAny()) {
if (mc.getComponentStateDefinitions().size() == 1) {
mcp.setComponentStatePattern(null);
}
continue;
}
if (csp.getComponentStateDefinition() == csd) {
if (mc.getComponentStateDefinitions().size() == 1) {
mcp.setComponentStatePattern(null);
} else {
csp = new ComponentStatePattern();
mcp.setComponentStatePattern(csp);
}
}
}
}
}
}
return true;
}
Aggregations