A couple of InChI bug fixes (#3656)

Fixes #3645
Fixes #3655
This commit is contained in:
Greg Landrum
2020-12-17 15:32:51 +01:00
committed by GitHub
parent 72340e33c2
commit 454abb257c
2 changed files with 46 additions and 7 deletions

View File

@@ -952,6 +952,7 @@ bool _Valence7SCleanUp1(RWMol& mol, Atom* atom) {
int neighborsO = 0;
RWMol::ADJ_ITER nid, nid1, end1;
boost::tie(nid1, end1) = mol.getAtomNeighbors(atom);
nid = end1;
while (nid1 != end1) {
Atom* otherAtom = mol.getAtomWithIdx(*nid1);
if (otherAtom->getAtomicNum() == 8) {
@@ -975,7 +976,7 @@ bool _Valence7SCleanUp1(RWMol& mol, Atom* atom) {
}
nid1++;
}
if (neighborsC == 1 || neighborsO == 3) {
if (nid != end1 && (neighborsC == 1 || neighborsO == 3)) {
mol.getBondBetweenAtoms(*nid, aid)->setBondType(Bond::SINGLE);
Atom* otherAtom = mol.getAtomWithIdx(*nid);
otherAtom->setFormalCharge(-1);
@@ -1674,13 +1675,17 @@ RWMol* InchiToMol(const std::string& inchi, ExtraInchiReturnValues& rv,
// clean up the molecule to be acceptable to RDKit
if (m) {
cleanUp(*m);
if (sanitize) {
if (removeHs) {
MolOps::removeHs(*m, false, false);
} else {
MolOps::sanitizeMol(*m);
try {
if (sanitize) {
if (removeHs) {
MolOps::removeHs(*m, false, false);
} else {
MolOps::sanitizeMol(*m);
}
}
} catch (const MolSanitizeException&) {
delete m;
throw;
}
// call assignStereochemistry just to be safe; otherwise, MolToSmiles may
// overwrite E/Z and/or bond direction on double bonds.

View File

@@ -777,6 +777,39 @@ void testGithub3365() {
BOOST_LOG(rdInfoLog) << "done" << std::endl;
}
void testGithub3645() {
BOOST_LOG(rdErrorLog) << "-------------------------------------" << std::endl;
BOOST_LOG(rdInfoLog) << "testing github #3645: Seg fault when parsing InChI"
<< std::endl;
{
std::string inchi =
"InChI=1S/C9H22O3SSi/c1-5-9-13(14,10-6-2,11-7-3)12-8-4/h5-9H2,1-4H3";
ExtraInchiReturnValues tmp;
bool ok = false;
try {
std::unique_ptr<ROMol> m{InchiToMol(inchi, tmp)};
} catch (const MolSanitizeException &) {
ok = true;
}
TEST_ASSERT(ok);
}
{
std::string inchi =
"InChI=1S/C8H20O3SSi/c1-5-9-12(13,8-4,10-6-2)11-7-3/h5-8H2,1-4H3";
ExtraInchiReturnValues tmp;
bool ok = false;
try {
std::unique_ptr<ROMol> m{InchiToMol(inchi, tmp)};
} catch (const MolSanitizeException &) {
ok = true;
}
TEST_ASSERT(ok);
}
BOOST_LOG(rdInfoLog) << "done" << std::endl;
}
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
@@ -797,4 +830,5 @@ int main() {
#endif
testGithubIssue562();
testGithub3365();
testGithub3645();
}