Search in sources :

Example 1 with ReactorNode

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

the class Reactor method addReagent.

void addReagent(ReactorNode node, IAtomContainer mol) {
    if (strategy.FlagCalcProductInchiKey) {
        try {
            InChIGenerator ig = igf.getInChIGenerator(mol, igf_options);
            mol.setProperty(PropertyInchiKey, ig.getInchiKey());
        } catch (Exception e) {
        }
        ;
    }
    if (strategy.FlagRemoveReagentIfAllowedProduct) {
        String inchiKey = mol.getProperty(PropertyInchiKey);
        if (isAllowedProduct(inchiKey)) {
            node.allowedProducts.addAtomContainer(mol);
            return;
        }
    }
    if (strategy.FlagRemoveReagentIfForbiddenProduct) {
        String inchiKey = mol.getProperty(PropertyInchiKey);
        if (isForbiddenProduct(inchiKey)) {
            node.forbiddenProducts.addAtomContainer(mol);
            return;
        }
    }
    node.reagents.addAtomContainer(mol);
}
Also used : InChIGenerator(org.openscience.cdk.inchi.InChIGenerator) EmptyMoleculeException(ambit2.base.exceptions.EmptyMoleculeException)

Example 2 with ReactorNode

use of ambit2.reactions.reactor.ReactorNode 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 3 with ReactorNode

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

the class ReactionTestUtils method testReactor.

public static void testReactor(String smiles, String reactionDBFile, int reactorStepSize) throws Exception {
    System.out.println("Setting reactor and reaction database...");
    Reactor reactor = new Reactor();
    ReactionDataBase reactDB = new ReactionDataBase(reactionDBFile);
    System.out.println("Configuring reaction database...");
    // reactDB.configureReactions(reactor.getSMIRKSManager());
    reactDB.configureGenericReactions(reactor.getSMIRKSManager());
    reactor.setReactionDataBase(reactDB);
    System.out.println("Configuring reactor strategy ...");
    // strategy is in the same file
    ReactorStrategy strategy = new ReactorStrategy(new File(reactionDBFile));
    strategy.FlagStoreFailedNodes = true;
    strategy.FlagStoreSuccessNodes = true;
    // if 0 then the reactor will stop after the first success node
    strategy.maxNumOfSuccessNodes = 0;
    strategy.FlagCheckNodeDuplicationOnPush = true;
    strategy.FlagTraceReactionPath = true;
    strategy.FlagLogMainReactionFlow = true;
    strategy.FlagLogReactionPath = true;
    strategy.FlagLogNameInReactionPath = false;
    strategy.FlagLogExplicitHToImplicit = true;
    reactor.setStrategy(strategy);
    // Setup Smirks manager
    reactor.getSMIRKSManager().setFlagProcessResultStructures(true);
    reactor.getSMIRKSManager().setFlagClearImplicitHAtomsBeforeResultProcess(false);
    reactor.getSMIRKSManager().setFlagAddImplicitHAtomsOnResultProcess(false);
    reactor.getSMIRKSManager().setFlagConvertExplicitHToImplicitOnResultProcess(false);
    if (FlagPrintReactionDB) {
        System.out.println("Reaction database:");
        for (int i = 0; i < reactDB.genericReactions.size(); i++) {
            GenericReaction r = reactDB.genericReactions.get(i);
            System.out.println("  " + r.getName() + "  " + r.getSmirks() + "  " + r.getReactionClass());
        }
    }
    if (FlagPrintReactionStrategy) {
        System.out.println();
        System.out.println(strategy.toJSONString(""));
        System.out.println(strategy.toString());
    }
    IAtomContainer mol = SmartsHelper.getMoleculeFromSmiles(smiles, true);
    System.out.println();
    System.out.println("Reactor on target: " + smiles);
    System.out.println();
    if (reactorStepSize <= 0) {
        ReactorResult result = reactor.react(mol);
    } else {
        reactor.initializeReactor(mol);
        List<ReactorNode> nodes = reactor.reactNext(reactorStepSize);
        System.out.println("Handled " + nodes.size() + " nodes");
        while (!nodes.isEmpty()) {
            nodes = reactor.reactNext(reactorStepSize);
            System.out.println("Handled " + nodes.size() + " nodes");
        }
    }
}
Also used : GenericReaction(ambit2.reactions.GenericReaction) ReactorNode(ambit2.reactions.reactor.ReactorNode) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) ReactorResult(ambit2.reactions.reactor.ReactorResult) ReactorStrategy(ambit2.reactions.reactor.ReactorStrategy) ReactionDataBase(ambit2.reactions.ReactionDataBase) Reactor(ambit2.reactions.reactor.Reactor) File(java.io.File)

Aggregations

EmptyMoleculeException (ambit2.base.exceptions.EmptyMoleculeException)2 GenericReaction (ambit2.reactions.GenericReaction)2 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)2 ReactionDataBase (ambit2.reactions.ReactionDataBase)1 Reactor (ambit2.reactions.reactor.Reactor)1 ReactorNode (ambit2.reactions.reactor.ReactorNode)1 ReactorResult (ambit2.reactions.reactor.ReactorResult)1 ReactorStrategy (ambit2.reactions.reactor.ReactorStrategy)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 InChIGenerator (org.openscience.cdk.inchi.InChIGenerator)1 IAtom (org.openscience.cdk.interfaces.IAtom)1 IAtomContainerSet (org.openscience.cdk.interfaces.IAtomContainerSet)1