Skip to content

Commit

Permalink
fix a problem with reading older pickles (#7180)
Browse files Browse the repository at this point in the history
  • Loading branch information
greglandrum committed Feb 22, 2024
1 parent a4c9ef9 commit b2606f5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
17 changes: 9 additions & 8 deletions Code/GraphMol/MolPickler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ void MolPickler::_pickle(const ROMol *mol, std::ostream &ss,
streamWrite(ss, BEGINFASTFIND);
break;
case RDKit::FIND_RING_TYPE::FIND_RING_TYPE_SSSR:
streamWrite(ss, BEGINSSSR);
streamWrite(ss, BEGINSSSR);
break;
case RDKit::FIND_RING_TYPE::FIND_RING_TYPE_SYMM_SSSR:
streamWrite(ss, BEGINSYMMSSSR);
Expand Down Expand Up @@ -2417,16 +2417,17 @@ void MolPickler::_depickleStereo(std::istream &ss, ROMol *mol, int version) {
atoms.push_back(mol->getAtomWithIdx(tmpT));
}

streamRead(ss, tmpT, version);
const auto numBonds = static_cast<unsigned>(tmpT);

std::vector<Bond *> bonds;
bonds.reserve(numBonds);
for (unsigned i = 0u; i < numBonds; ++i) {
if (version > 16000) {
streamRead(ss, tmpT, version);
bonds.push_back(mol->getBondWithIdx(tmpT));
}
const auto numBonds = static_cast<unsigned>(tmpT);

bonds.reserve(numBonds);
for (unsigned i = 0u; i < numBonds; ++i) {
streamRead(ss, tmpT, version);
bonds.push_back(mol->getBondWithIdx(tmpT));
}
}
groups.emplace_back(groupType, std::move(atoms), std::move(bonds), gId);
}

Expand Down
17 changes: 16 additions & 1 deletion Code/GraphMol/catch_pickles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,19 @@ M END
CHECK(mol2.getBondWithIdx(2)->getProp<std::string>(
common_properties::_MolFileBondEndPts) == "(3 1 2 3)");
}
}
}

TEST_CASE("parsing old pickles with many features") {
std::string pklName = getenv("RDBASE");
pklName += "/Code/GraphMol/test_data/mol_with_sgroups_and_stereo.pkl";

auto m =
"C/C=C/C[C@H](O)[C@@H](C)F |a:6,o2:4,r,SgD:5:data_pt:4.5::::|"_smiles;
REQUIRE(m);
std::ifstream inStream(pklName.c_str(), std::ios_base::binary);
RWMol m2;
// if the mol can be read, the primary problem was addressed
MolPickler::molFromPickle(inStream, m2);
CHECK(m2.getNumAtoms() == m->getNumAtoms());
CHECK(MolToCXSmiles(*m) == MolToCXSmiles(m2));
}
Binary file not shown.

0 comments on commit b2606f5

Please sign in to comment.