Search in sources :

Example 61 with MolecularTypePattern

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);
            }
        }
    }
}
Also used : MolecularType(org.vcell.model.rbm.MolecularType) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) MolecularTypePattern(org.vcell.model.rbm.MolecularTypePattern) LinkedHashMap(java.util.LinkedHashMap)

Example 62 with MolecularTypePattern

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;
}
Also used : MolecularComponentPattern(org.vcell.model.rbm.MolecularComponentPattern) MolecularTypePattern(org.vcell.model.rbm.MolecularTypePattern) SpeciesPattern(org.vcell.model.rbm.SpeciesPattern)

Example 63 with MolecularTypePattern

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());
}
Also used : Issue(org.vcell.util.Issue) MolecularTypePattern(org.vcell.model.rbm.MolecularTypePattern)

Example 64 with MolecularTypePattern

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));
                        }
                    }
                }
            }
        }
    }
}
Also used : Displayable(org.vcell.util.Displayable) MolecularComponentPattern(org.vcell.model.rbm.MolecularComponentPattern) ComponentStatePattern(org.vcell.model.rbm.ComponentStatePattern) MolecularTypePattern(org.vcell.model.rbm.MolecularTypePattern) SpeciesPattern(org.vcell.model.rbm.SpeciesPattern)

Example 65 with MolecularTypePattern

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;
}
Also used : MolecularComponentPattern(org.vcell.model.rbm.MolecularComponentPattern) ComponentStatePattern(org.vcell.model.rbm.ComponentStatePattern) MolecularTypePattern(org.vcell.model.rbm.MolecularTypePattern) SpeciesPattern(org.vcell.model.rbm.SpeciesPattern)

Aggregations

MolecularTypePattern (org.vcell.model.rbm.MolecularTypePattern)72 MolecularComponentPattern (org.vcell.model.rbm.MolecularComponentPattern)49 SpeciesPattern (org.vcell.model.rbm.SpeciesPattern)39 ComponentStatePattern (org.vcell.model.rbm.ComponentStatePattern)19 Graphics (java.awt.Graphics)16 MolecularType (org.vcell.model.rbm.MolecularType)16 Point (java.awt.Point)14 BioModelNode (cbit.vcell.desktop.BioModelNode)11 RbmObservable (cbit.vcell.model.RbmObservable)10 SpeciesContext (cbit.vcell.model.SpeciesContext)10 Icon (javax.swing.Icon)10 ComponentStateDefinition (org.vcell.model.rbm.ComponentStateDefinition)10 MolecularComponent (org.vcell.model.rbm.MolecularComponent)10 ArrayList (java.util.ArrayList)9 SpeciesPatternLargeShape (cbit.vcell.graph.SpeciesPatternLargeShape)7 ReactionRule (cbit.vcell.model.ReactionRule)7 LinkedHashMap (java.util.LinkedHashMap)7 MolecularTypeLargeShape (cbit.vcell.graph.MolecularTypeLargeShape)6 MolecularTypeSmallShape (cbit.vcell.graph.MolecularTypeSmallShape)6 RuleAnalysisChanged (cbit.vcell.graph.ReactionCartoon.RuleAnalysisChanged)6