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);
}
}
}
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;
}
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;
}
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;
}
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();
}
Aggregations