Skip to content

Commit

Permalink
fixed inconsistent bond types when parsing smiles with multiple, comm…
Browse files Browse the repository at this point in the history
…a-separated bond types
  • Loading branch information
thsa committed Jan 14, 2025
1 parent 8aeee28 commit 14198e4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 43 deletions.
49 changes: 49 additions & 0 deletions src/main/java/com/actelion/research/chem/Molecule.java
Original file line number Diff line number Diff line change
Expand Up @@ -3498,6 +3498,55 @@ public void setBondQueryFeature(int bond, int feature, boolean value) {
}


/**
* If a molecule is a substructure fragment, and if multiple allowed bond types
* are defined in a bond's query features, then the explicit bond type must match
* the lowest order of the allowed query feature bond types.
* Call this function after setting bond type and query features to make sure,
* the correct bond type is used.
* @param bond
*/
public void adaptBondTypeToQueryFeatures(int bond) {
int bondType = -1;
int selectionCount = 0;

if ((mBondQueryFeatures[bond] & Molecule.cBondTypeMetalLigand) != 0) {
bondType = Molecule.cBondTypeMetalLigand;
selectionCount++;
}
if ((mBondQueryFeatures[bond] & Molecule.cBondTypeQuintuple) != 0) {
bondType = Molecule.cBondTypeQuintuple;
selectionCount++;
}
if ((mBondQueryFeatures[bond] & Molecule.cBondTypeQuadruple) != 0) {
bondType = Molecule.cBondTypeQuadruple;
selectionCount++;
}
if ((mBondQueryFeatures[bond] & Molecule.cBondTypeTriple) != 0) {
bondType = Molecule.cBondTypeTriple;
selectionCount++;
}
if ((mBondQueryFeatures[bond] & Molecule.cBondTypeDouble) != 0) {
bondType = Molecule.cBondTypeDouble;
selectionCount++;
}
if ((mBondQueryFeatures[bond] & Molecule.cBondTypeDelocalized) != 0) {
bondType = Molecule.cBondTypeDelocalized;
selectionCount++;
}
if ((mBondQueryFeatures[bond] & Molecule.cBondTypeSingle) != 0) {
bondType = Molecule.cBondTypeSingle;
selectionCount++;
}

if (bondType != -1 && bondType != (mBondType[bond] & cBondTypeMaskSimple))
mBondType[bond] = bondType; // set to the lowest bond order of query options

if (selectionCount < 2)
mBondQueryFeatures[bond] &= ~(cBondQFBondTypes + cBondQFRareBondTypes);
}


/**
* Sets the bond type based on bond order without stereo orientation.
* @param bond
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/actelion/research/chem/SmilesParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ public void parse(StereoMolecule mol, byte[] smiles, int position, int endIndex,
if (bondQueryFeatures != 0) {
mSmartsFeatureFound = true;
mMol.setBondQueryFeature(bond, bondQueryFeatures, true);
mMol.adaptBondTypeToQueryFeatures(bond);
}
}

Expand Down Expand Up @@ -462,6 +463,7 @@ public void parse(StereoMolecule mol, byte[] smiles, int position, int endIndex,
if (bondQueryFeatures != 0) {
mSmartsFeatureFound = true;
mMol.setBondQueryFeature(bond, bondQueryFeatures, true);
mMol.adaptBondTypeToQueryFeatures(bond);
}
}

Expand Down Expand Up @@ -667,6 +669,7 @@ else if (bondType == Molecule.cBondTypeDown)
if (bondQueryFeatures != 0) {
mSmartsFeatureFound = true;
mMol.setBondQueryFeature(bond, ringClosureBondQueryFeatures[number], true);
mMol.adaptBondTypeToQueryFeatures(bond);
}
ringClosureAtom[number] = -1; // for number re-usage
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,51 +296,20 @@ private void setQueryFeatures(int bond) {
mMol.setBondType(bond, Molecule.cBondTypeSingle);
}
else {
// priority in order of bond orders (except 0)
int bondType = -1;
int selectionCount = 0;

if (mCBMetalLigand.isSelected()) {
bondType = Molecule.cBondTypeMetalLigand;
queryFeatures |= Molecule.cBondTypeMetalLigand;
selectionCount++;
}
if (mCBQuintuple.isSelected()) {
bondType = Molecule.cBondTypeQuintuple;
queryFeatures |= Molecule.cBondTypeQuintuple;
selectionCount++;
}
if (mCBQuadruple.isSelected()) {
bondType = Molecule.cBondTypeQuadruple;
queryFeatures |= Molecule.cBondTypeQuadruple;
selectionCount++;
}
if (mCBTriple.isSelected()) {
bondType = Molecule.cBondTypeTriple;
queryFeatures |= Molecule.cBondTypeTriple;
selectionCount++;
}
if (mCBDouble.isSelected()) {
bondType = Molecule.cBondTypeDouble;
if (mCBSingle.isSelected())
queryFeatures |= Molecule.cBondTypeSingle;
if (mCBDouble.isSelected())
queryFeatures |= Molecule.cBondTypeDouble;
selectionCount++;
}
if (mCBDelocalized.isSelected()) {
bondType = Molecule.cBondTypeDelocalized;
if (mCBTriple.isSelected())
queryFeatures |= Molecule.cBondTypeTriple;
if (mCBQuadruple.isSelected())
queryFeatures |= Molecule.cBondTypeQuadruple;
if (mCBQuintuple.isSelected())
queryFeatures |= Molecule.cBondTypeQuintuple;
if (mCBDelocalized.isSelected())
queryFeatures |= Molecule.cBondTypeDelocalized;
selectionCount++;
}
if (mCBSingle.isSelected()) {
bondType = Molecule.cBondTypeSingle;
queryFeatures |= Molecule.cBondTypeSingle;
selectionCount++;
}

if (bondType != -1)
mMol.setBondType(bond, bondType); // set to the lowest bond order of query options

if (selectionCount < 2)
queryFeatures = 0;
if (mCBMetalLigand.isSelected())
queryFeatures |= Molecule.cBondTypeMetalLigand;

if (mComboBoxRing.getSelectedIndex() != 0) {
if (mComboBoxRing.getSelectedIndex() == 1) {
Expand Down Expand Up @@ -374,6 +343,7 @@ else if (mComboBoxRing.getSelectedIndex() == 4) {

mMol.setBondQueryFeature(bond, Molecule.cBondQFAllFeatures, false);
mMol.setBondQueryFeature(bond, queryFeatures, true);
mMol.adaptBondTypeToQueryFeatures(bond);
}

private boolean isSelectedBond(int bond) {
Expand Down

0 comments on commit 14198e4

Please sign in to comment.