Search in sources :

Example 6 with GenericReaction

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

the class ReactionSequence method generateSequenceStepForReactionInstance.

public void generateSequenceStepForReactionInstance(ReactionSequenceLevel level, int moleculeIndex, GenericReaction reaction, List<IAtom> reactionInstance) throws Exception {
    ReactionSequenceStep step = new ReactionSequenceStep();
    step.reaction = reaction;
    IAtomContainer mol = level.molecules.get(moleculeIndex);
    IAtomContainer products = reaction.applyAtInstance(mol, reactionInstance, smrkMan, true);
    smrkMan.processProduct(products);
    // calculate reaction score
    TopLayer.setAtomTopLayers(products);
    GenericReactionInstance gri = new GenericReactionInstance(reaction, mol, reactionInstance, products);
    step.reactionScore = strategy.calcReactionScore(gri);
    IAtomContainerSet productFrags = ConnectivityChecker.partitionIntoMolecules(products);
    step.outputMolecules = new ArrayList<IAtomContainer>();
    for (IAtomContainer frag : productFrags.atomContainers()) {
        step.outputMolecules.add(frag);
        String inchiKey = setMoleculeInchiKey(frag);
        registerMolInchiKey(frag, inchiKey, level.levelIndex + 1);
        // Set new molecule status
        if (usedInchies.get(inchiKey).molecules.size() > 1)
            setMoleculeStatus(frag, MoleculeStatus.EQUIVALENT_TO_OTHER_MOLECULE);
        else {
            if (startMatDB.isStartingMaterial(inchiKey))
                setMoleculeStatus(frag, MoleculeStatus.STARTING_MATERIAL);
            else
                setMoleculeStatus(frag, MoleculeStatus.ADDED_TO_LEVEL);
        }
    }
    level.associateStep(moleculeIndex, step);
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) GenericReactionInstance(ambit2.reactions.GenericReactionInstance)

Example 7 with GenericReaction

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

the class ReactionSequence method handleReactionInstances.

public List<GenericReactionInstance> handleReactionInstances(Map<GenericReaction, List<List<IAtom>>> instances, IAtomContainer target) throws Exception {
    List<GenericReactionInstance> griList = new ArrayList<GenericReactionInstance>();
    Set<GenericReaction> grKeys = instances.keySet();
    for (GenericReaction gr : grKeys) {
        List<List<IAtom>> grInst = instances.get(gr);
        for (int i = 0; i < grInst.size(); i++) {
            IAtomContainer products = gr.applyAtInstance(target, grInst.get(i), smrkMan, true);
            smrkMan.processProduct(products);
            // calculate reaction score
            TopLayer.setAtomTopLayers(products);
            GenericReactionInstance gri = new GenericReactionInstance(gr, target, grInst.get(i), products);
            gri.reactionScore = strategy.calcReactionScore(gri);
            griList.add(gri);
        }
    // TODO take into account reaction instance occurrences
    }
    return griList;
}
Also used : GenericReaction(ambit2.reactions.GenericReaction) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) GenericReactionInstance(ambit2.reactions.GenericReactionInstance)

Example 8 with GenericReaction

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

the class ReactionSequence method iterateLevelMolecules.

public void iterateLevelMolecules(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;
            }
            List<GenericReactionInstance> griList = handleReactionInstances(allInstances, mol);
            GenericReactionInstance bestInst = getBestInstance(griList);
            generateSequenceStepForReactionInstance(level, i, bestInst);
            setMoleculeStatus(mol, MoleculeStatus.RESOLVED);
        }
    }
}
Also used : GenericReaction(ambit2.reactions.GenericReaction) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) ArrayList(java.util.ArrayList) List(java.util.List) GenericReactionInstance(ambit2.reactions.GenericReactionInstance) MoleculeStatus(ambit2.reactions.retrosynth.ReactionSequence.MoleculeStatus) IAtom(org.openscience.cdk.interfaces.IAtom)

Example 9 with GenericReaction

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

the class ReactionSequenceLevel method toString.

public String toString() {
    StringBuffer sb = new StringBuffer();
    sb.append("Level " + levelIndex + "\n");
    for (int i = 0; i < molecules.size(); i++) {
        String smi = null;
        try {
            IAtomContainer mol = molecules.get(i).clone();
            MoleculeTools.convertExplicitHAtomsToImplicit(mol);
            smi = SmartsHelper.moleculeToSMILES(mol, true);
        } catch (Exception x) {
        }
        ;
        sb.append("" + smi);
        sb.append("   " + MoleculeStatus.getShortString((MoleculeStatus) molecules.get(i).getProperty(ReactionSequence.MoleculeStatusProperty)));
        // sb.append("       " + molecules.get(i).getProperty(ReactionSequence.MoleculeInChIKeyProperty));
        Object[] obj = getGenerationInfo(i);
        if (obj != null) {
            GenericReaction genReaction = (GenericReaction) obj[0];
            sb.append("   <R" + genReaction.getExternId() + ",M" + ((Integer) obj[1] + 1));
            if (obj[2] != null) {
                ReactionScore rscore = (ReactionScore) obj[2];
                sb.append(",S" + ((Double) rscore.totalScore).intValue());
                sb.append(">");
                sb.append("     " + rscore.toStringLine());
            } else
                sb.append(">");
        }
        /*
			ReactionSequenceStep step = steps.get(i);
			if (step != null)
			{
				//TODO
			}
			*/
        sb.append("\n");
    }
    return sb.toString();
}
Also used : GenericReaction(ambit2.reactions.GenericReaction) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) ReactionScore(ambit2.reactions.rules.scores.ReactionScore)

Example 10 with GenericReaction

use of ambit2.reactions.GenericReaction 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

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