Search in sources :

Example 1 with GenericReaction

use of ambit2.reactions.GenericReaction in project ambit-mirror by ideaconsult.

the class ReactionSequence method iterateLevelMoleculesRandomly.

public void iterateLevelMoleculesRandomly(ReactionSequenceLevel level) throws Exception {
    for (int i = 0; i < level.molecules.size(); i++) {
        IAtomContainer mol = level.molecules.get(i);
        MoleculeStatus status = getMoleculeStatus(mol);
        if (status == MoleculeStatus.ADDED_TO_LEVEL) {
            // check for starting material is performed on product generation
            Map<GenericReaction, List<List<IAtom>>> allInstances = generateAllReactionInstances(mol);
            if (allInstances.isEmpty()) {
                setMoleculeStatus(mol, MoleculeStatus.UNRESOLVED);
                continue;
            }
            Object[] obj = SyntheticStrategy.getRandomSelection(allInstances);
            GenericReaction gr = (GenericReaction) obj[0];
            List<IAtom> inst = (List<IAtom>) obj[1];
            generateSequenceStepForReactionInstance(level, i, gr, inst);
            setMoleculeStatus(mol, MoleculeStatus.RESOLVED);
        }
    }
}
Also used : GenericReaction(ambit2.reactions.GenericReaction) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) ArrayList(java.util.ArrayList) List(java.util.List) MoleculeStatus(ambit2.reactions.retrosynth.ReactionSequence.MoleculeStatus) IAtom(org.openscience.cdk.interfaces.IAtom)

Example 2 with GenericReaction

use of ambit2.reactions.GenericReaction in project ambit-mirror by ideaconsult.

the class GenericReaction method getReactionFromJsonNode.

public static GenericReaction getReactionFromJsonNode(JsonNode node) throws Exception {
    GenericReaction reaction = new GenericReaction();
    Boolean use = JSONParsingUtils.extractBooleanKeyword(node, "USE", false);
    if (use != null)
        reaction.setFlagUse(use);
    reaction.name = JSONParsingUtils.extractStringKeyword(node, "NAME", true);
    reaction.smirks = JSONParsingUtils.extractStringKeyword(node, "SMIRKS", true);
    reaction.reactionClass = JSONParsingUtils.extractStringKeyword(node, "CLASS", false);
    Object[] obj = JSONParsingUtils.extractArrayKeyword(node, "USE_CONDITIONS", false, true);
    if (obj != null) {
        List<ICondition> conditions = new ArrayList<ICondition>();
        for (int i = 0; i < obj.length; i++) {
            if (obj[i] == null)
                continue;
            if (obj[i] instanceof String) {
                ICondition condition = ReactionParser.getConditionFromString((String) obj[i]);
                conditions.add(condition);
            } else {
                if (obj[i] instanceof JsonNode) {
                    IDescriptorValueCondition dvc = ConditionJsonParser.getDescriptorValueCondition((JsonNode) obj[i]);
                    conditions.add(dvc);
                } else
                    throw new Exception("Incorrect USE_CONDITIONS[" + (i + 1) + "]. It is not a string! " + obj[i]);
            }
        }
        reaction.setConditions(conditions);
    }
    return reaction;
}
Also used : IDescriptorValueCondition(ambit2.rules.conditions.IDescriptorValueCondition) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ICondition(ambit2.rules.conditions.ICondition) EmptyMoleculeException(ambit2.base.exceptions.EmptyMoleculeException)

Example 3 with GenericReaction

use of ambit2.reactions.GenericReaction in project ambit-mirror by ideaconsult.

the class SyntheticStrategy method getRandomSelection.

public static Object[] getRandomSelection(Map<GenericReaction, List<List<IAtom>>> instances) {
    Random rn = new Random();
    rn.setSeed(1234555);
    Set<GenericReaction> grSet = instances.keySet();
    int grNum = rn.nextInt(grSet.size());
    int n = 0;
    for (GenericReaction gr : instances.keySet()) {
        if (n == grNum) {
            List<List<IAtom>> rInst = instances.get(gr);
            int rInstNum = rn.nextInt(rInst.size());
            // System.out.println("grNum = " + grNum + "  rInstNum = " + rInstNum + " rInst.size() = " + rInst.size());
            List<IAtom> inst = rInst.get(rInstNum);
            Object[] obj = new Object[2];
            obj[0] = gr;
            obj[1] = inst;
            return obj;
        } else
            n++;
    }
    return null;
}
Also used : GenericReaction(ambit2.reactions.GenericReaction) Random(java.util.Random) ArrayList(java.util.ArrayList) List(java.util.List) IAtom(org.openscience.cdk.interfaces.IAtom)

Example 4 with GenericReaction

use of ambit2.reactions.GenericReaction in project ambit-mirror by ideaconsult.

the class Reactor method generateChildrenNodes.

int generateChildrenNodes(ReactorNode node, IAtomContainer reagent) throws EmptyMoleculeException {
    int numOfReactionInstances = 0;
    for (int i = 0; i < reactionDataBase.genericReactions.size(); i++) {
        GenericReaction reaction = reactionDataBase.genericReactions.get(i);
        List<List<IAtom>> instances = reaction.findReactionInstances(reagent, smrkMan);
        // Check reaction conditions
        if (strategy.FlagCheckReactionConditions)
            if (reaction.getConditions() != null) {
                ReactorInfoPack ri = new ReactorInfoPack();
                ri.reactor = this;
                ri.reaction = reaction;
                ri.reagent = reagent;
                if (!reaction.checkConditionsForTarget(ri))
                    continue;
            }
        numOfReactionInstances += instances.size();
        for (int k = 0; k < instances.size(); k++) {
            List<IAtom> instance = instances.get(k);
            IAtomContainer products = null;
            try {
                products = reaction.applyAtInstance(reagent, instance, smrkMan, true);
            } catch (Exception x) {
                logger.severe("Reaction application error: " + reaction.getName() + "  " + reaction.getSmirks() + "  applied for reagent " + molToSmiles(reagent) + "   " + x.getMessage());
            }
            if (products == null)
                continue;
            if (smrkMan.isFlagProcessResultStructures()) {
                try {
                    smrkMan.processProduct(products);
                } catch (Exception e) {
                }
            }
            // System.out.println("Reaction: " + reaction.getSmirks()+"\n" + "Products: " + molToSmiles(products));
            IAtomContainerSet productsSet = ConnectivityChecker.partitionIntoMolecules(products);
            ReactorNode newNode = node.clone();
            // Update reactorResult status
            reactorResult.numReactions++;
            reactorResult.numReactorNodes++;
            // newNode.level = ...
            if (strategy.FlagTraceParentNodes)
                newNode.parentNode = node;
            if (strategy.FlagTraceReactionPath)
                setReactionPath(productsSet, reagent, reaction);
            addReagents(newNode, productsSet);
            if (strategy.FlagCheckNodeDuplicationOnPush) {
                String hash = newNode.calcNodeHash();
                if (!nodeHashCodes.contains(hash)) {
                    nodeHashCodes.add(hash);
                    reactorNodes.push(newNode);
                }
            } else
                reactorNodes.push(newNode);
        }
    }
    if (numOfReactionInstances == 0) {
        // No reaction was applied then moving the reagent to finalized/forbidden/allowed products
        ReactorNode newNode = node.clone();
        finalizeProduct(newNode, reagent);
        // updateNodeState(newNode);
        reactorNodes.push(newNode);
        reactorResult.numReactorNodes++;
    }
    return numOfReactionInstances;
}
Also used : GenericReaction(ambit2.reactions.GenericReaction) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) ArrayList(java.util.ArrayList) List(java.util.List) IAtom(org.openscience.cdk.interfaces.IAtom) EmptyMoleculeException(ambit2.base.exceptions.EmptyMoleculeException)

Example 5 with GenericReaction

use of ambit2.reactions.GenericReaction in project ambit-mirror by ideaconsult.

the class Reactor method reactionPathToString.

public String reactionPathToString(IAtomContainer mol, boolean printName) {
    StringBuffer sb = new StringBuffer();
    Object obj = mol.getProperty(PropertyReactionIds);
    if (obj != null) {
        int[] rids = (int[]) obj;
        for (int i = 0; i < rids.length; i++) {
            sb.append(" " + rids[i]);
            if (printName) {
                GenericReaction r = reactionDataBase.getGenericReactionByID(rids[i]);
                if (r != null)
                    sb.append(" " + r.getName());
            }
        }
    }
    return sb.toString();
}
Also used : GenericReaction(ambit2.reactions.GenericReaction)

Aggregations

GenericReaction (ambit2.reactions.GenericReaction)9 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)7 ArrayList (java.util.ArrayList)6 List (java.util.List)5 IAtom (org.openscience.cdk.interfaces.IAtom)4 GenericReactionInstance (ambit2.reactions.GenericReactionInstance)3 EmptyMoleculeException (ambit2.base.exceptions.EmptyMoleculeException)2 ReactionDataBase (ambit2.reactions.ReactionDataBase)2 MoleculeStatus (ambit2.reactions.retrosynth.ReactionSequence.MoleculeStatus)2 IAtomContainerSet (org.openscience.cdk.interfaces.IAtomContainerSet)2 Reactor (ambit2.reactions.reactor.Reactor)1 ReactorNode (ambit2.reactions.reactor.ReactorNode)1 ReactorResult (ambit2.reactions.reactor.ReactorResult)1 ReactorStrategy (ambit2.reactions.reactor.ReactorStrategy)1 ReactionScore (ambit2.reactions.rules.scores.ReactionScore)1 ICondition (ambit2.rules.conditions.ICondition)1 IDescriptorValueCondition (ambit2.rules.conditions.IDescriptorValueCondition)1 SMIRKSManager (ambit2.smarts.SMIRKSManager)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 File (java.io.File)1