RDKit
Open-source cheminformatics and machine learning.
MolPickler.h
Go to the documentation of this file.
1 ///
2 // Copyright (C) 2001-2008 Greg Landrum and Rational Discovery LLC
3 //
4 // @@ All Rights Reserved @@
5 // This file is part of the RDKit.
6 // The contents are covered by the terms of the BSD license
7 // which is included in the file license.txt, found at the root
8 // of the RDKit source tree.
9 //
10 #include <RDGeneral/export.h>
11 #ifndef _RD_MOLPICKLE_H
12 #define _RD_MOLPICKLE_H
13 
14 #include <Geometry/point.h>
15 #include <GraphMol/Atom.h>
16 #include <GraphMol/QueryAtom.h>
17 #include <GraphMol/Bond.h>
18 #include <GraphMol/QueryBond.h>
19 #include <RDGeneral/StreamOps.h>
20 #include <boost/utility/binary.hpp>
21 // Std stuff
22 #include <iostream>
23 #include <string>
24 #include <sstream>
25 #include <exception>
26 #ifdef WIN32
27 #include <ios>
28 #endif
29 #include <cstdint>
30 
31 namespace RDKit {
32 class ROMol;
33 class RingInfo;
34 
35 //! used to indicate exceptions whilst pickling (serializing) molecules
36 class RDKIT_GRAPHMOL_EXPORT MolPicklerException : public std::exception {
37  public:
38  MolPicklerException(const char *msg) : _msg(msg){};
39  MolPicklerException(const std::string msg) : _msg(msg){};
40  const char *what() const noexcept override { return _msg.c_str(); };
41  const char *message() const noexcept { return what(); };
42  ~MolPicklerException() noexcept {};
43 
44  private:
45  std::string _msg;
46 };
47 
48 namespace PicklerOps {
49 typedef enum {
50  NoProps = 0, // no data pickled (default pickling, single-precision coords)
51  MolProps = BOOST_BINARY(1), // only public non computed properties
52  AtomProps = BOOST_BINARY(10),
53  BondProps = BOOST_BINARY(100),
54  QueryAtomData = BOOST_BINARY(
55  10), // n.b. DEPRECATED and set to AtomProps (does the same work)
56  PrivateProps = BOOST_BINARY(10000),
57  ComputedProps = BOOST_BINARY(100000),
58  AllProps =
59  0x0000FFFF, // all data pickled
60  CoordsAsDouble = 0x0001FFFF // save coordinates in double precision
62 }
63 
64 //! handles pickling (serializing) molecules
66  public:
67  static const std::int32_t versionMajor; //!< mark the pickle major version
68  static const std::int32_t versionMinor; //!< mark the pickle minor version
69  static const std::int32_t versionPatch; //!< mark the pickle patch version
70  static const std::int32_t endianId; //! mark the endian-ness of the pickle
71 
72  //! the pickle format is tagged using these tags:
73  //! NOTE: if you add to this list, be sure to put new entries AT THE BOTTOM,
74  // otherwise
75  //! you will break old pickles.
76  typedef enum {
77  VERSION = 0,
142  } Tags;
143 
144  static unsigned int getDefaultPickleProperties();
145  static void setDefaultPickleProperties(unsigned int);
146 
148  static void addCustomPropHandler(const CustomPropHandler &handler);
149 
150  //! pickles a molecule and sends the results to stream \c ss
151  static void pickleMol(const ROMol *mol, std::ostream &ss);
152  static void pickleMol(const ROMol *mol, std::ostream &ss,
153  unsigned int propertyFlags);
154 
155  static void pickleMol(const ROMol &mol, std::ostream &ss);
156 
157  static void pickleMol(const ROMol &mol, std::ostream &ss,
158  unsigned int propertyFlags) {
159  MolPickler::pickleMol(&mol, ss, propertyFlags);
160  };
161 
162  //! pickles a molecule and adds the results to string \c res
163  static void pickleMol(const ROMol *mol, std::string &res);
164  static void pickleMol(const ROMol *mol, std::string &res,
165  unsigned int propertyFlags);
166  static void pickleMol(const ROMol &mol, std::string &res);
167  static void pickleMol(const ROMol &mol, std::string &res,
168  unsigned int propertyFlags) {
169  MolPickler::pickleMol(&mol, res, propertyFlags);
170  };
171 
172  //! constructs a molecule from a pickle stored in a string
173  static void molFromPickle(const std::string &pickle, ROMol *mol);
174  static void molFromPickle(const std::string &pickle, ROMol &mol) {
176  };
177 
178  //! constructs a molecule from a pickle stored in a stream
179  static void molFromPickle(std::istream &ss, ROMol *mol);
180  static void molFromPickle(std::istream &ss, ROMol &mol) {
181  MolPickler::molFromPickle(ss, &mol);
182  };
183 
184  private:
185  //! Pickle nonquery atom data
186  static std::int32_t _pickleAtomData(std::ostream &tss, const Atom *atom);
187  //! depickle nonquery atom data
188  static void _unpickleAtomData(std::istream &tss, Atom *atom, int version);
189 
190  static void _pickleQueryAtomData(std::ostream &tss, const Atom *atom);
191 
192  //! do the actual work of pickling a molecule
193  template <typename T>
194  static void _pickle(const ROMol *mol, std::ostream &ss,
195  unsigned int propertyFlags);
196 
197  //! do the actual work of pickling an Atom
198  template <typename T>
199  static void _pickleAtom(std::ostream &ss, const Atom *atom);
200 
201  //! do the actual work of pickling a Bond
202  template <typename T>
203  static void _pickleBond(std::ostream &ss, const Bond *bond,
204  std::map<int, int> &atomIdxMap);
205 
206  //! do the actual work of pickling an SSSR structure
207  template <typename T>
208  static void _pickleSSSR(std::ostream &ss, const RingInfo *ringInfo,
209  std::map<int, int> &atomIdxMap);
210 
211  //! do the actual work of pickling a SubstanceGroup
212  template <typename T>
213  static void _pickleSubstanceGroup(std::ostream &ss,
214  const SubstanceGroup &sgroup,
215  std::map<int, int> &atomIdxMap,
216  std::map<int, int> &bondIdxMap);
217 
218  //! do the actual work of pickling Stereo Group data
219  template <typename T>
220  static void _pickleStereo(std::ostream &ss,
221  const std::vector<StereoGroup> &groups,
222  std::map<int, int> &atomIdxMap);
223 
224  //! do the actual work of pickling a Conformer
225  template <typename T, typename C>
226  static void _pickleConformer(std::ostream &ss, const Conformer *conf);
227 
228  //! do the actual work of de-pickling a molecule
229  template <typename T>
230  static void _depickle(std::istream &ss, ROMol *mol, int version,
231  int numAtoms);
232 
233  //! extract atomic data from a pickle and add the resulting Atom to the
234  // molecule
235  template <typename T>
236  static Atom *_addAtomFromPickle(std::istream &ss, ROMol *mol,
237  RDGeom::Point3D &pos, int version,
238  bool directMap = false);
239 
240  //! extract bond data from a pickle and add the resulting Bond to the molecule
241  template <typename T>
242  static Bond *_addBondFromPickle(std::istream &ss, ROMol *mol, int version,
243  bool directMap = false);
244 
245  //! extract ring info from a pickle and add the resulting RingInfo to the
246  // molecule
247  template <typename T>
248  static void _addRingInfoFromPickle(std::istream &ss, ROMol *mol, int version,
249  bool directMap = false);
250 
251  //! extract a SubstanceGroup from a pickle
252  template <typename T>
253  static SubstanceGroup _getSubstanceGroupFromPickle(std::istream &ss,
254  ROMol *mol, int version);
255 
256  template <typename T>
257  static void _depickleStereo(std::istream &ss, ROMol *mol, int version);
258 
259  //! extract a conformation from a pickle
260  template <typename T, typename C>
261  static Conformer *_conformerFromPickle(std::istream &ss, int version);
262 
263  //! pickle standard properties
264  static void _pickleProperties(std::ostream &ss, const RDProps &props,
265  unsigned int pickleFlags);
266  //! unpickle standard properties
267  static void _unpickleProperties(std::istream &ss, RDProps &props);
268 
269  //! backwards compatibility
270  static void _pickleV1(const ROMol *mol, std::ostream &ss);
271  //! backwards compatibility
272  static void _depickleV1(std::istream &ss, ROMol *mol);
273  //! backwards compatibility
274  static void _addAtomFromPickleV1(std::istream &ss, ROMol *mol);
275  //! backwards compatibility
276  static void _addBondFromPickleV1(std::istream &ss, ROMol *mol);
277 };
278 }; // namespace RDKit
279 
280 #endif
RDKit::MolPickler::ATOM_ISAROMATIC
@ ATOM_ISAROMATIC
Definition: MolPickler.h:86
RDKit::MolPickler::QUERY_NULL
@ QUERY_NULL
Definition: MolPickler.h:117
RDKit::MolPicklerException::~MolPicklerException
~MolPicklerException() noexcept
Definition: MolPickler.h:42
RDKit::PicklerOps::ComputedProps
@ ComputedProps
Definition: MolPickler.h:57
RDKit::MolPickler::ATOM_PDB_RESIDUE_OCCUPANCY
@ ATOM_PDB_RESIDUE_OCCUPANCY
Definition: MolPickler.h:128
RDKit::MolPickler::ATOM_PDB_RESIDUE_INSERTIONCODE
@ ATOM_PDB_RESIDUE_INSERTIONCODE
Definition: MolPickler.h:127
point.h
RDKit::MolPickler::ENDQUERY
@ ENDQUERY
Definition: MolPickler.h:120
RDKit::MolPickler::QUERY_LESS
@ QUERY_LESS
Definition: MolPickler.h:113
RDKit::MolPickler::ENDSSSR
@ ENDSSSR
Definition: MolPickler.h:98
RDKit::EnumerationStrategyPickler::pickle
RDKIT_CHEMREACTIONS_EXPORT void pickle(const boost::shared_ptr< EnumerationStrategyBase > &enumerator, std::ostream &ss)
pickles a EnumerationStrategy and adds the results to a stream ss
RDKit::MolPickler::molFromPickle
static void molFromPickle(std::istream &ss, ROMol *mol)
constructs a molecule from a pickle stored in a stream
RDKit::CustomPropHandler
Definition: StreamOps.h:321
Bond.h
RDKit::MolPicklerException::MolPicklerException
MolPicklerException(const std::string msg)
Definition: MolPickler.h:39
RDKit::MolPickler::QUERY_XOR
@ QUERY_XOR
Definition: MolPickler.h:109
RDKit::MolPickler::ATOM_PDB_RESIDUE_TEMPFACTOR
@ ATOM_PDB_RESIDUE_TEMPFACTOR
Definition: MolPickler.h:129
RDKit::SubstanceGroup
The class for representing SubstanceGroups.
Definition: SubstanceGroup.h:53
RDKit::MolPicklerException
used to indicate exceptions whilst pickling (serializing) molecules
Definition: MolPickler.h:36
RDKit::MolPickler::addCustomPropHandler
static void addCustomPropHandler(const CustomPropHandler &handler)
RDKit::Bond
class for representing a bond
Definition: Bond.h:47
RDGeom::Point3D
Definition: point.h:46
RDKit::MolPickler::versionPatch
static const std::int32_t versionPatch
mark the pickle patch version
Definition: MolPickler.h:69
RDKit::MolPickler::BEGIN_ATOM_MONOMER
@ BEGIN_ATOM_MONOMER
Definition: MolPickler.h:122
RDKit::MolPickler::BEGINBOND
@ BEGINBOND
Definition: MolPickler.h:88
RDKit::MolPickler::ATOM_PDB_RESIDUE_RESIDUENUMBER
@ ATOM_PDB_RESIDUE_RESIDUENUMBER
Definition: MolPickler.h:132
RDKit::MolPickler::ENDBOND
@ ENDBOND
Definition: MolPickler.h:94
RDKit::PicklerOps::QueryAtomData
@ QueryAtomData
Definition: MolPickler.h:54
RDKit::PicklerOps::MolProps
@ MolProps
Definition: MolPickler.h:51
RDKit::MolPickler::BEGINQUERYATOMDATA
@ BEGINQUERYATOMDATA
Definition: MolPickler.h:137
RDKit::MolPickler::setDefaultPickleProperties
static void setDefaultPickleProperties(unsigned int)
RDKit::CustomPropHandlerVec
std::vector< std::shared_ptr< const CustomPropHandler > > CustomPropHandlerVec
Definition: StreamOps.h:332
RDKit::MolPicklerException::what
const char * what() const noexcept override
Definition: MolPickler.h:40
RDKit::PicklerOps::CoordsAsDouble
@ CoordsAsDouble
Definition: MolPickler.h:60
RDKit::MolPickler::BOND_TYPE
@ BOND_TYPE
Definition: MolPickler.h:92
RDKit::MolPickler::getCustomPropHandlers
static const CustomPropHandlerVec & getCustomPropHandlers()
RDKit::MolPickler::QUERY_GREATER
@ QUERY_GREATER
Definition: MolPickler.h:111
RDKit::MolPickler::BEGINSTEREOGROUP
@ BEGINSTEREOGROUP
Definition: MolPickler.h:139
RDKit::MolPickler::endianId
static const std::int32_t endianId
Definition: MolPickler.h:70
RDKit::Atom
The class for representing atoms.
Definition: Atom.h:69
QueryAtom.h
RDKit::MolPickler::versionMajor
static const std::int32_t versionMajor
mark the pickle major version
Definition: MolPickler.h:67
RDKit::MolPickler::END_ATOM_MONOMER
@ END_ATOM_MONOMER
Definition: MolPickler.h:134
RDKit::MolPickler::molFromPickle
static void molFromPickle(const std::string &pickle, ROMol *mol)
constructs a molecule from a pickle stored in a string
Atom.h
Defines the Atom class and associated typedefs.
RDKit::PicklerOps::PrivateProps
@ PrivateProps
Definition: MolPickler.h:56
RDKit::RingInfo
A class to store information about a molecule's rings.
Definition: RingInfo.h:28
RDKit::MolPickler::ATOM_INDEX
@ ATOM_INDEX
Definition: MolPickler.h:79
RDKit::MolPickler::BEGINATOM
@ BEGINATOM
Definition: MolPickler.h:78
RDKit::ROMol
Definition: ROMol.h:171
RDKit::PicklerOps::PropertyPickleOptions
PropertyPickleOptions
Definition: MolPickler.h:49
RDKit::MolPickler::pickleMol
static void pickleMol(const ROMol *mol, std::ostream &ss, unsigned int propertyFlags)
RDKit::MolPickler::BOND_DIR
@ BOND_DIR
Definition: MolPickler.h:93
RDKit::MolPickler::BEGINCONFPROPS
@ BEGINCONFPROPS
Definition: MolPickler.h:140
RDKit::MolPickler::BEGINSSSR
@ BEGINSSSR
Definition: MolPickler.h:97
RDKit::MolPickler::BEGINCONFS
@ BEGINCONFS
Definition: MolPickler.h:100
RDKit::MolPickler::pickleMol
static void pickleMol(const ROMol &mol, std::string &res)
RDKIT_GRAPHMOL_EXPORT
#define RDKIT_GRAPHMOL_EXPORT
Definition: export.h:307
RDKit::MolPickler::QUERY_RECURSIVE
@ QUERY_RECURSIVE
Definition: MolPickler.h:119
QueryBond.h
RDKit::MolPickler::pickleMol
static void pickleMol(const ROMol &mol, std::ostream &ss)
RDKit::MolPickler::ATOM_PDB_RESIDUE_SERIALNUMBER
@ ATOM_PDB_RESIDUE_SERIALNUMBER
Definition: MolPickler.h:123
RDKit::MolPickler::getDefaultPickleProperties
static unsigned int getDefaultPickleProperties()
RDKit::MolPickler::pickleMol
static void pickleMol(const ROMol *mol, std::string &res, unsigned int propertyFlags)
RDKit::MolPickler::QUERY_ISNEGATED
@ QUERY_ISNEGATED
Definition: MolPickler.h:104
RDKit::MolPickler::QUERY_VALUE
@ QUERY_VALUE
Definition: MolPickler.h:103
RDKit::Conformer
The class for representing 2D or 3D conformation of a molecule.
Definition: Conformer.h:44
RDKit::MolPickler::ATOM_MAPNUMBER
@ ATOM_MAPNUMBER
Definition: MolPickler.h:101
RDKit::MolPickler::versionMinor
static const std::int32_t versionMinor
mark the pickle minor version
Definition: MolPickler.h:68
RDKit::MolPickler::molFromPickle
static void molFromPickle(std::istream &ss, ROMol &mol)
Definition: MolPickler.h:180
RDKit::MolPickler::ATOM_DUMMYLABEL
@ ATOM_DUMMYLABEL
Definition: MolPickler.h:121
RDKit::PicklerOps::AllProps
@ AllProps
Definition: MolPickler.h:58
RDKit::MolPickler::pickleMol
static void pickleMol(const ROMol *mol, std::string &res)
pickles a molecule and adds the results to string res
RDKit::MolPickler::ATOM_PDB_RESIDUE_RESIDUENAME
@ ATOM_PDB_RESIDUE_RESIDUENAME
Definition: MolPickler.h:125
RDKit::RDProps
Definition: RDProps.h:14
RDKit::PicklerOps::BondProps
@ BondProps
Definition: MolPickler.h:53
RDKit::MolPickler::QUERY_ATOMRING
@ QUERY_ATOMRING
Definition: MolPickler.h:118
RDKit::MolPickler::pickleMol
static void pickleMol(const ROMol &mol, std::ostream &ss, unsigned int propertyFlags)
Definition: MolPickler.h:157
RDKit::MolPickler::BOND_BEGATOMIDX
@ BOND_BEGATOMIDX
Definition: MolPickler.h:90
RDKit::MolPickler::ATOM_NUMBER
@ ATOM_NUMBER
Definition: MolPickler.h:80
RDKit::MolPickler::QUERY_RANGE
@ QUERY_RANGE
Definition: MolPickler.h:115
RDKit::MolPickler::pickleMol
static void pickleMol(const ROMol *mol, std::ostream &ss)
pickles a molecule and sends the results to stream ss
RDKit::MolPickler::QUERY_SET
@ QUERY_SET
Definition: MolPickler.h:116
RDKit::MolPicklerException::MolPicklerException
MolPicklerException(const char *msg)
Definition: MolPickler.h:38
RDKit::MolPickler::ATOM_POS
@ ATOM_POS
Definition: MolPickler.h:81
RDKit
Std stuff.
Definition: Atom.h:30
RDKit::MolPickler::QUERY_LESSEQUAL
@ QUERY_LESSEQUAL
Definition: MolPickler.h:114
RDKit::MolPickler::ATOM_PDB_RESIDUE_CHAINID
@ ATOM_PDB_RESIDUE_CHAINID
Definition: MolPickler.h:126
RDKit::MolPickler::pickleMol
static void pickleMol(const ROMol &mol, std::string &res, unsigned int propertyFlags)
Definition: MolPickler.h:167
RDKit::MolPickler::BEGINPROPS
@ BEGINPROPS
Definition: MolPickler.h:95
RDKit::MolPickler::ATOM_PDB_RESIDUE_ALTLOC
@ ATOM_PDB_RESIDUE_ALTLOC
Definition: MolPickler.h:124
RDKit::MolPickler::BEGINBONDPROPS
@ BEGINBONDPROPS
Definition: MolPickler.h:136
RDKit::MolPickler::QUERY_NUMCHILDREN
@ QUERY_NUMCHILDREN
Definition: MolPickler.h:105
RDKit::MolPickler::BEGINQUERY
@ BEGINQUERY
Definition: MolPickler.h:102
RDKit::MolPickler::BEGINCONFS_DOUBLE
@ BEGINCONFS_DOUBLE
Definition: MolPickler.h:141
RDKit::MolPickler::BOND_INDEX
@ BOND_INDEX
Definition: MolPickler.h:89
RDKit::MolPickler::ATOM_PDB_RESIDUE_SEGMENTNUMBER
@ ATOM_PDB_RESIDUE_SEGMENTNUMBER
Definition: MolPickler.h:133
RDKit::PicklerOps::NoProps
@ NoProps
Definition: MolPickler.h:50
StreamOps.h
RDKit::MolPickler::ATOM_PDB_RESIDUE_ISHETEROATOM
@ ATOM_PDB_RESIDUE_ISHETEROATOM
Definition: MolPickler.h:130
RDKit::MolPickler::QUERY_AND
@ QUERY_AND
Definition: MolPickler.h:107
RDKit::MolPickler::BEGINSGROUP
@ BEGINSGROUP
Definition: MolPickler.h:138
RDKit::MolPickler
handles pickling (serializing) molecules
Definition: MolPickler.h:65
RDKit::MolPickler::BOND_ENDATOMIDX
@ BOND_ENDATOMIDX
Definition: MolPickler.h:91
RDKit::MolPickler::QUERY_EQUALS
@ QUERY_EQUALS
Definition: MolPickler.h:110
RDKit::MolPickler::molFromPickle
static void molFromPickle(const std::string &pickle, ROMol &mol)
Definition: MolPickler.h:174
RDKit::MolPicklerException::message
const char * message() const noexcept
Definition: MolPickler.h:41
RDKit::MolPickler::ATOM_CHIRALTAG
@ ATOM_CHIRALTAG
Definition: MolPickler.h:84
RDKit::MolPickler::QUERY_BOOL
@ QUERY_BOOL
Definition: MolPickler.h:106
RDKit::PicklerOps::AtomProps
@ AtomProps
Definition: MolPickler.h:52
RDKit::MolPickler::ATOM_CHARGE
@ ATOM_CHARGE
Definition: MolPickler.h:82
RDKit::MolPickler::ATOM_PDB_RESIDUE_SECONDARYSTRUCTURE
@ ATOM_PDB_RESIDUE_SECONDARYSTRUCTURE
Definition: MolPickler.h:131
RDKit::MolPickler::ATOM_MASS
@ ATOM_MASS
Definition: MolPickler.h:85
RDKit::MolPickler::ENDMOL
@ ENDMOL
Definition: MolPickler.h:99
RDKit::MolPickler::QUERY_GREATEREQUAL
@ QUERY_GREATEREQUAL
Definition: MolPickler.h:112
RDKit::MolPickler::BEGINATOMPROPS
@ BEGINATOMPROPS
Definition: MolPickler.h:135
RDKit::MolPickler::ENDPROPS
@ ENDPROPS
Definition: MolPickler.h:96
RDKit::MolPickler::ENDATOM
@ ENDATOM
Definition: MolPickler.h:87
RDKit::MolPickler::QUERY_OR
@ QUERY_OR
Definition: MolPickler.h:108
RDKit::MolPickler::ATOM_NEXPLICIT
@ ATOM_NEXPLICIT
Definition: MolPickler.h:83
export.h