use of org.openscience.cdk.isomorphism.matchers.smarts.SMARTSBond in project ambit-mirror by ideaconsult.
the class SmartsParser method setDoubleBondStereoElement.
void setDoubleBondStereoElement(SMARTSBond doubleBond, IAtom atom, IAtom at0, SMARTSBond dirBond, int direction, int atomPos) {
// Input defines a fragment of the type "a0--atom=="
// Direction of bond "a0--atom" is defined by parameters: direction and
// atPos
// atomPos is the number ot 'atom' inside the CDK representation of bond
// "a0--atom"
// One of the bonds "atom1--at2" or "atom1--at3" must be a directional
// bond otherwise
// the stereo configuration is not determined.
//
// The remaining part of this fragment is determined first
// at0 at2
// \ doubleBond /
// atom == atom1
// / \
// at1 at3
// Determining only atoms: atom1 and at2
// atoms at1 and at3 are not needed for storing the stereo element
// System.out.println("*** setDoubleBondStereoElement");
IAtom atom1;
IAtom at2 = null;
IBond dirBond2 = null;
if (doubleBond.getAtom(0) == atom)
atom1 = doubleBond.getAtom(1);
else
atom1 = doubleBond.getAtom(0);
java.util.List ca = container.getConnectedAtomsList(atom1);
for (int k = 0; k < ca.size(); k++) {
IAtom otherAt = (IAtom) ca.get(k);
if (otherAt == atom)
continue;
IBond bo = container.getBond(atom1, otherAt);
if (at2 == null) {
if (isDirectionalBond((SMARTSBond) bo)) {
at2 = otherAt;
dirBond2 = bo;
break;
}
}
}
if (at2 == null)
// Stereo element is not complete
return;
// The stereo info object is created
DoubleBondStereoInfo dbsi = new DoubleBondStereoInfo();
dbsi.ligand1 = at0;
dbsi.ligand2 = at2;
// Get stored direction for the second direction bond
int dirBond2Index = directionalBonds.indexOf(dirBond2);
int direction2 = directions.get(dirBond2Index).intValue();
// Get normalized directions
int norm_dir = getNormalizedBondDirection(direction, atom, at0);
int norm_dir2 = getNormalizedBondDirection(direction2, atom1, at2);
boolean isUp = (norm_dir == SmartsConst.BT_UP) || (norm_dir == SmartsConst.BT_UPUNSPEC);
boolean isUp2 = (norm_dir2 == SmartsConst.BT_UP) || (norm_dir2 == SmartsConst.BT_UPUNSPEC);
boolean isUndefined = (norm_dir == SmartsConst.BT_UPUNSPEC) || (norm_dir == SmartsConst.BT_DOWNUNSPEC) || (norm_dir2 == SmartsConst.BT_UPUNSPEC) || (norm_dir2 == SmartsConst.BT_DOWNUNSPEC);
if (isUp == isUp2) {
if (isUndefined)
dbsi.conformation = DBStereo.TOGETHER_OR_UNDEFINED;
else
dbsi.conformation = DBStereo.TOGETHER;
} else {
if (isUndefined)
dbsi.conformation = DBStereo.OPPOSITE_OR_UNDEFINED;
else
dbsi.conformation = DBStereo.OPPOSITE;
}
if (doubleBond instanceof DoubleNonAromaticBond) {
DoubleNonAromaticBond db = (DoubleNonAromaticBond) doubleBond;
db.setStereoInfo(dbsi);
processedDoubleBonds.add(doubleBond);
processedDirBonds.add((SMARTSBond) dirBond2);
} else {
if (doubleBond instanceof DoubleBondAromaticityNotSpecified) {
DoubleBondAromaticityNotSpecified db = (DoubleBondAromaticityNotSpecified) doubleBond;
db.setStereoInfo(dbsi);
processedDoubleBonds.add(doubleBond);
processedDirBonds.add((SMARTSBond) dirBond2);
} else if (doubleBond instanceof SmartsBondExpression) {
SmartsBondExpression db = (SmartsBondExpression) doubleBond;
db.setStereoInfo(dbsi);
processedDoubleBonds.add(doubleBond);
processedDirBonds.add((SMARTSBond) dirBond2);
}
}
}
use of org.openscience.cdk.isomorphism.matchers.smarts.SMARTSBond in project ambit-mirror by ideaconsult.
the class SmartsManager method getAtomMapsFromBondMaps.
/**
* This function returns a vector of all positions (IAtoms) at which The
* Query is matched (i.e. it first atom is matched) The full atom mapping is
* not obtained from this function !!! This function is a helper utility for
* the CDK isomorphism algorithm
*/
List<IAtom> getAtomMapsFromBondMaps(List bondMapping, IAtomContainer target, IAtomContainer recQuery) {
// The query must contain at least 3 atoms and 2 bonds.
// The first bond is always 0-1
// Two cases are considered for the second bond:
// a0-a1-a2 (e.g. CCC)
// a0(-a1)-a2 (e.g. C(C)C)
// So the common atom of bonds 0 and 1 is either atom 1 or atom 0
// The latter assumptions are based on the way recursive SMARTS are
// parsed
List<IAtom> atomMaps = new ArrayList<IAtom>();
if (recQuery.getBondCount() < 2)
return (atomMaps);
SMARTSBond qBond0 = (SMARTSBond) recQuery.getBond(0);
SMARTSBond qBond1 = (SMARTSBond) recQuery.getBond(1);
int qID0 = recQuery.getBondNumber(qBond0);
int qID1 = recQuery.getBondNumber(qBond1);
SMARTSAtom qAtom0 = (SMARTSAtom) recQuery.getAtom(0);
SMARTSAtom qAtom1 = (SMARTSAtom) recQuery.getAtom(1);
IAtom commonQAt = getBondsCommonAtom(qBond0, qBond1);
if ((commonQAt != qAtom0) && (commonQAt != qAtom1))
// This should never happen
return (atomMaps);
IBond tBond0 = null;
IBond tBond1 = null;
IAtom commonTAt;
RMap map;
List mapList;
for (Object aList : bondMapping) {
mapList = (List) aList;
// Search RMaps to find the corresponding target bonds
// (qBond0<-->tBond0, qBond1<-->tBond1)
// Note: map.getId2 corresponds to the query; map.getId1 corresponds
// to the target
int found = 0;
for (Object aMap : mapList) {
map = (RMap) aMap;
if (map.getId2() == qID0) {
found++;
tBond0 = target.getBond(map.getId1());
}
if (map.getId2() == qID1) {
found++;
tBond1 = target.getBond(map.getId1());
}
if (found >= 2)
break;
}
if (// This should never happen
found < 2)
continue;
commonTAt = getBondsCommonAtom(tBond0, tBond1);
if (commonQAt == qAtom0)
atomMaps.add(commonTAt);
else {
// So the other atom (not commonTAt) must be added to atomMaps
if (commonTAt == tBond0.getAtom(0))
atomMaps.add(tBond0.getAtom(1));
else
atomMaps.add(tBond0.getAtom(0));
}
}
return (atomMaps);
}
use of org.openscience.cdk.isomorphism.matchers.smarts.SMARTSBond in project ambit-mirror by ideaconsult.
the class SmartsManager method getFirstPosAtomMappingsFor2AtomQuery.
List<IAtom> getFirstPosAtomMappingsFor2AtomQuery(IAtomContainer target, IAtomContainer recQuery) {
List<IAtom> atomMaps = new ArrayList<IAtom>();
if (recQuery.getBondCount() == 0)
// The two atoms must be connected otherwise
return (atomMaps);
// substr. search returns no matches
SMARTSAtom qAtom0 = (SMARTSAtom) recQuery.getAtom(0);
SMARTSAtom qAtom1 = (SMARTSAtom) recQuery.getAtom(1);
SMARTSBond qBond = (SMARTSBond) recQuery.getBond(0);
for (int i = 0; i < target.getAtomCount(); i++) {
IAtom at = target.getAtom(i);
if (qAtom0.matches(at)) {
List ca = target.getConnectedAtomsList(at);
for (int j = 0; j < ca.size(); j++) if (qAtom1.matches((IAtom) ca.get(j))) {
IBond bo = target.getBond(at, (IAtom) ca.get(j));
if (bo != null)
if (qBond.matches(bo)) {
atomMaps.add(at);
// for j-cycle
break;
}
}
}
}
return (atomMaps);
}
use of org.openscience.cdk.isomorphism.matchers.smarts.SMARTSBond in project cdk by cdk.
the class SmartsQueryVisitor method visit.
public Object visit(ASTSmarts node, Object data) {
SMARTSAtom atom;
SMARTSBond bond = null;
ASTAtom first = (ASTAtom) node.jjtGetChild(0);
atom = (SMARTSAtom) first.jjtAccept(this, null);
if (data != null) {
// this is a sub smarts
bond = (SMARTSBond) ((Object[]) data)[1];
IAtom prev = (SMARTSAtom) ((Object[]) data)[0];
if (bond == null) {
// since no bond was specified it could be aromatic or single
bond = new AromaticOrSingleQueryBond(builder);
bond.setAtoms(new IAtom[] { prev, atom });
} else {
bond.setAtoms(new IAtom[] { prev, atom });
}
if (neighbors.containsKey(prev)) {
neighbors.get(prev).add(atom);
}
query.addBond(bond);
bond = null;
}
// first ATOM in expresion
query.addAtom(atom);
if (tetrahedral.get(query.getAtomCount() - 1)) {
List<IAtom> localNeighbors = new ArrayList<>(query.getConnectedAtomsList(atom));
localNeighbors.add(atom);
neighbors.put(atom, localNeighbors);
}
// now process the rest of the bonds/atoms
for (int i = 1; i < node.jjtGetNumChildren(); i++) {
Node child = node.jjtGetChild(i);
if (child instanceof ASTLowAndBond) {
bond = (SMARTSBond) child.jjtAccept(this, data);
} else if (child instanceof ASTAtom) {
SMARTSAtom newAtom = (SMARTSAtom) child.jjtAccept(this, null);
if (bond == null) {
// since no bond was specified it could be aromatic or single
bond = new AromaticOrSingleQueryBond(builder);
}
bond.setAtoms(new IAtom[] { atom, newAtom });
query.addBond(bond);
query.addAtom(newAtom);
if (neighbors.containsKey(atom)) {
neighbors.get(atom).add(newAtom);
}
if (tetrahedral.get(query.getAtomCount() - 1)) {
List<IAtom> localNeighbors = new ArrayList<>(query.getConnectedAtomsList(newAtom));
localNeighbors.add(newAtom);
neighbors.put(newAtom, localNeighbors);
}
atom = newAtom;
bond = null;
} else if (child instanceof ASTSmarts) {
// another smarts
child.jjtAccept(this, new Object[] { atom, bond });
bond = null;
} else if (child instanceof ASTRingIdentifier) {
handleRingClosure(atom, (ASTRingIdentifier) child);
} else {
throw new IllegalStateException("Unhandled node type: " + child.getClass());
}
}
return query;
}
use of org.openscience.cdk.isomorphism.matchers.smarts.SMARTSBond in project ambit-mirror by ideaconsult.
the class SmartsParser method getNeighborDoubleBond.
SMARTSBond getNeighborDoubleBond(SMARTSBond bond, int atNum) {
// bond is a directional single bond
// the neighbor double is search among the "atNum" neighbors
IAtom at = bond.getAtom(atNum);
java.util.List ca = container.getConnectedAtomsList(at);
for (int k = 0; k < ca.size(); k++) {
IBond bo = container.getBond(at, (IAtom) ca.get(k));
if (bo != bond) {
if (bo instanceof DoubleNonAromaticBond)
return (SMARTSBond) bo;
if (bo instanceof DoubleBondAromaticityNotSpecified)
return (SMARTSBond) bo;
if (bo instanceof SmartsBondExpression) {
SmartsToChemObject stco = new SmartsToChemObject(SilentChemObjectBuilder.getInstance());
IBond bondFromExpr = stco.smartsExpressionToBond((SmartsBondExpression) bo);
if (bondFromExpr != null)
if (bondFromExpr.getOrder() == IBond.Order.DOUBLE)
return (SMARTSBond) bo;
}
}
}
return (null);
}
Aggregations