RDKit: MolPickler.h Source File

| RDKit Open-source cheminformatics and machine learning.

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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 #ifndef _RD_MOLPICKLE_H
11 #define _RD_MOLPICKLE_H
12
13 #include <Geometry/point.h>
14 #include <GraphMol/Atom.h>
15 #include <GraphMol/QueryAtom.h>
16 #include <GraphMol/Bond.h>
17 #include <GraphMol/QueryBond.h>
18 #include <boost/utility/binary.hpp>
19 // Std stuff
20 #include <iostream>
21 #include <string>
22 #include <sstream>
23 #include <exception>
24 #ifdef WIN32
25 #include <ios>
26 #endif
27 #include <boost/cstdint.hpp>
28
29 namespace RDKit {
30 class ROMol;
31 class RingInfo;
32
33 //! used to indicate exceptions whilst pickling (serializing) molecules
34 class MolPicklerException : public std::exception {
35 public:
36 MolPicklerException(const char *msg) : _msg(msg){};
37 MolPicklerException(const std::string msg) : _msg(msg){};
38 const char *message() const { return _msg.c_str(); };
39 ~MolPicklerException() throw(){};
40
41 private:
42 std::string _msg;
43 };
44
45 namespace PicklerOps {
46 typedef enum {
47 NoProps = 0, // no data pickled
48 MolProps = BOOST_BINARY(1), // only public non computed properties
49 AtomProps = BOOST_BINARY(10),
50 BondProps = BOOST_BINARY(100),
51 QueryAtomData = BOOST_BINARY(10), // n.b. DEPRECATED and set to AtomProps (does the same work)
52 PrivateProps = BOOST_BINARY(10000),
53 ComputedProps = BOOST_BINARY(100000),
54 AllProps =
55 0x7FFFFFFF, // all data pickled (only 31 bit flags in case enum==int)
56 } PropertyPickleOptions;
57 }
58
59 //! handles pickling (serializing) molecules
60 class MolPickler {
61 public:
62 static const boost::int32_t versionMajor, versionMinor,
63 versionPatch; //!< mark the pickle version
64 static const boost::int32_t endianId; //! mark the endian-ness of the pickle
65
66 //! the pickle format is tagged using these tags:
67 //! NOTE: if you add to this list, be sure to put new entries AT THE BOTTOM,
68 // otherwise
69 //! you will break old pickles.
70 typedef enum {
71 VERSION = 0,
72 BEGINATOM,
73 ATOM_INDEX,
74 ATOM_NUMBER,
75 ATOM_POS,
76 ATOM_CHARGE,
77 ATOM_NEXPLICIT,
78 ATOM_CHIRALTAG,
79 ATOM_MASS,
80 ATOM_ISAROMATIC,
81 ENDATOM,
82 BEGINBOND,
83 BOND_INDEX,
84 BOND_BEGATOMIDX,
85 BOND_ENDATOMIDX,
86 BOND_TYPE,
87 BOND_DIR,
88 ENDBOND,
89 BEGINPROPS,
90 ENDPROPS,
91 BEGINSSSR,
92 ENDSSSR,
93 ENDMOL,
94 BEGINCONFS,
95 ATOM_MAPNUMBER,
96 BEGINQUERY,
97 QUERY_VALUE,
98 QUERY_ISNEGATED,
99 QUERY_NUMCHILDREN,
100 QUERY_BOOL,
101 QUERY_AND,
102 QUERY_OR,
103 QUERY_XOR,
104 QUERY_EQUALS,
105 QUERY_GREATER,
106 QUERY_GREATEREQUAL,
107 QUERY_LESS,
108 QUERY_LESSEQUAL,
109 QUERY_RANGE,
110 QUERY_SET,
111 QUERY_NULL,
112 QUERY_ATOMRING,
113 QUERY_RECURSIVE,
114 ENDQUERY,
115 ATOM_DUMMYLABEL,
116 BEGIN_ATOM_MONOMER,
117 ATOM_PDB_RESIDUE_SERIALNUMBER,
118 ATOM_PDB_RESIDUE_ALTLOC,
119 ATOM_PDB_RESIDUE_RESIDUENAME,
120 ATOM_PDB_RESIDUE_CHAINID,
121 ATOM_PDB_RESIDUE_INSERTIONCODE,
122 ATOM_PDB_RESIDUE_OCCUPANCY,
123 ATOM_PDB_RESIDUE_TEMPFACTOR,
124 ATOM_PDB_RESIDUE_ISHETEROATOM,
125 ATOM_PDB_RESIDUE_SECONDARYSTRUCTURE,
126 ATOM_PDB_RESIDUE_RESIDUENUMBER,
127 ATOM_PDB_RESIDUE_SEGMENTNUMBER,
128 END_ATOM_MONOMER,
129 BEGINATOMPROPS,
130 BEGINBONDPROPS,
131 BEGINQUERYATOMDATA,
132 } Tags;
133
134 static unsigned int getDefaultPickleProperties();
135 static void setDefaultPickleProperties(unsigned int);
136
137 //! pickles a molecule and sends the results to stream \c ss
138 static void pickleMol(const ROMol *mol, std::ostream &ss);
139 static void pickleMol(const ROMol *mol, std::ostream &ss,
140 unsigned int propertyFlags);
141
142 static void pickleMol(const ROMol &mol, std::ostream &ss);
143
144 static void pickleMol(const ROMol &mol, std::ostream &ss,
145 unsigned int propertyFlags) {
146 MolPickler::pickleMol(&mol, ss, propertyFlags);
147 };
148
149 //! pickles a molecule and adds the results to string \c res
150 static void pickleMol(const ROMol *mol, std::string &res);
151 static void pickleMol(const ROMol *mol, std::string &res,
152 unsigned int propertyFlags);
153 static void pickleMol(const ROMol &mol, std::string &res);
154 static void pickleMol(const ROMol &mol, std::string &res,
155 unsigned int propertyFlags) {
156 MolPickler::pickleMol(&mol, res, propertyFlags);
157 };
158
159 //! constructs a molecule from a pickle stored in a string
160 static void molFromPickle(const std::string &pickle, ROMol *mol);
161 static void molFromPickle(const std::string &pickle, ROMol &mol) {
162 MolPickler::molFromPickle(pickle, &mol);
163 };
164
165 //! constructs a molecule from a pickle stored in a stream
166 static void molFromPickle(std::istream &ss, ROMol *mol);
167 static void molFromPickle(std::istream &ss, ROMol &mol) {
168 MolPickler::molFromPickle(ss, &mol);
169 };
170
171 private:
172 //! Pickle nonquery atom data
173 static boost::int32_t _pickleAtomData(std::ostream &tss, const Atom *atom);
174 //! depickle nonquery atom data
175 static void _unpickleAtomData(std::istream &tss, Atom *atom, int version);
176
177 static void _pickleQueryAtomData(std::ostream &tss, const Atom *atom);
178
179 //! do the actual work of pickling a molecule
180 template <typename T>
181 static void _pickle(const ROMol *mol, std::ostream &ss,
182 unsigned int propertyFlags);
183
184 //! do the actual work of pickling an Atom
185 template <typename T>
186 static void _pickleAtom(std::ostream &ss, const Atom *atom);
187
188 //! do the actual work of pickling a Bond
189 template <typename T>
190 static void _pickleBond(std::ostream &ss, const Bond *bond,
191 std::map<int, int> &atomIdxMap);
192
193 //! do the actual work of pickling an SSSR structure
194 template <typename T>
195 static void _pickleSSSR(std::ostream &ss, const RingInfo *ringInfo,
196 std::map<int, int> &atomIdxMap);
197
198 //! do the actual work of pickling a Conformer
199 template <typename T>
200 static void _pickleConformer(std::ostream &ss, const Conformer *conf);
201
202 //! do the actual work of de-pickling a molecule
203 template <typename T>
204 static void _depickle(std::istream &ss, ROMol *mol, int version,
205 int numAtoms);
206
207 //! extract atomic data from a pickle and add the resulting Atom to the
208 // molecule
209 template <typename T>
210 static Atom *_addAtomFromPickle(std::istream &ss, ROMol *mol,
211 RDGeom::Point3D &pos, int version,
212 bool directMap = false);
213
214 //! extract bond data from a pickle and add the resulting Bond to the molecule
215 template <typename T>
216 static Bond *_addBondFromPickle(std::istream &ss, ROMol *mol, int version,
217 bool directMap = false);
218
219 //! extract ring info from a pickle and add the resulting RingInfo to the
220 // molecule
221 template <typename T>
222 static void _addRingInfoFromPickle(std::istream &ss, ROMol *mol, int version,
223 bool directMap = false);
224
225 //! extract a conformation from a pickle
226 template <typename T>
227 static Conformer *_conformerFromPickle(std::istream &ss, int version);
228
229 //! pickle standard properties
230 static void _pickleProperties(std::ostream &ss, const RDProps &props,
231 unsigned int pickleFlags);
232 //! unpickle standard properties
233 static void _unpickleProperties(std::istream &ss, RDProps &props);
234
235 //! backwards compatibility
236 static void _pickleV1(const ROMol *mol, std::ostream &ss);
237 //! backwards compatibility
238 static void _depickleV1(std::istream &ss, ROMol *mol);
239 //! backwards compatibility
240 static void _addAtomFromPickleV1(std::istream &ss, ROMol *mol);
241 //! backwards compatibility
242 static void _addBondFromPickleV1(std::istream &ss, ROMol *mol);
243 };
244 };
245
246 #endif
RDKit::MolPickler::END_ATOM_MONOMER
Definition: MolPickler.h:128
RDKit::MolPickler::ENDQUERY
Definition: MolPickler.h:114
RDKit::MolPickler::QUERY_AND
Definition: MolPickler.h:101
RDKit::MolPickler::BEGINATOM
Definition: MolPickler.h:72
RDKit::MolPickler::ATOM_INDEX
Definition: MolPickler.h:73
RDCatalog::versionMinor
const int versionMinor
Definition: Catalog.h:32
RDKit::MolPickler::QUERY_XOR
Definition: MolPickler.h:103
RDKit::MolPickler::ATOM_PDB_RESIDUE_TEMPFACTOR
Definition: MolPickler.h:123
RDKit::MolPickler::BEGINBONDPROPS
Definition: MolPickler.h:130
RDKit::MolPickler::pickleMol
static void pickleMol(const ROMol *mol, std::ostream &ss)
pickles a molecule and sends the results to stream ss
RDKit::MolPickler::ATOM_PDB_RESIDUE_INSERTIONCODE
Definition: MolPickler.h:121
RDKit::MolPicklerException::MolPicklerException
MolPicklerException(const char *msg)
Definition: MolPickler.h:36
RDKit::MolPickler::ATOM_CHARGE
Definition: MolPickler.h:76
RDKit::MolPickler::BEGINQUERYATOMDATA
Definition: MolPickler.h:131
RDKit::MolPickler::ATOM_PDB_RESIDUE_SERIALNUMBER
Definition: MolPickler.h:117
RDKit::MolPickler::ATOM_MASS
Definition: MolPickler.h:79
QueryBond.h
RDKit::PicklerOps::MolProps
Definition: MolPickler.h:48
RDKit::MolPickler::BOND_DIR
Definition: MolPickler.h:87
RDKit::MolPickler::BEGINSSSR
Definition: MolPickler.h:91
RDKit::MolPickler::QUERY_GREATEREQUAL
Definition: MolPickler.h:106
point.h
RDKit::MolPickler::ENDPROPS
Definition: MolPickler.h:90
RDKit::PicklerOps::ComputedProps
Definition: MolPickler.h:53
RDKit::MolPickler::pickleMol
static void pickleMol(const ROMol &mol, std::string &res, unsigned int propertyFlags)
Definition: MolPickler.h:154
RDKit::MolPickler::Tags
Tags
mark the endian-ness of the pickle
Definition: MolPickler.h:70
RDKit::MolPickler::ATOM_NEXPLICIT
Definition: MolPickler.h:77
RDKit::MolPickler::BEGINBOND
Definition: MolPickler.h:82
RDKit::MolPickler::QUERY_RANGE
Definition: MolPickler.h:109
RDKit::RDProps
Definition: RDProps.h:9
RDKit::MolPickler::ENDATOM
Definition: MolPickler.h:81
RDKit::MolPickler::QUERY_RECURSIVE
Definition: MolPickler.h:113
RDKit::MolPickler::versionPatch
static const boost::int32_t versionPatch
mark the pickle version
Definition: MolPickler.h:62
RDKit::MolPickler::BOND_TYPE
Definition: MolPickler.h:86
RDKit::MolPickler::ENDSSSR
Definition: MolPickler.h:92
RDKit::ROMol
ROMol is a molecule class that is intended to have a fixed topology.
Definition: ROMol.h:103
RDKit::MolPickler::ATOM_PDB_RESIDUE_RESIDUENUMBER
Definition: MolPickler.h:126
RDKit::MolPickler::QUERY_LESSEQUAL
Definition: MolPickler.h:108
RDKit::MolPickler::QUERY_LESS
Definition: MolPickler.h:107
RDKit::MolPickler::BEGIN_ATOM_MONOMER
Definition: MolPickler.h:116
RDKit::MolPickler::BOND_BEGATOMIDX
Definition: MolPickler.h:84
RDKit::MolPickler::molFromPickle
static void molFromPickle(const std::string &pickle, ROMol &mol)
Definition: MolPickler.h:161
RDKit::MolPickler::QUERY_VALUE
Definition: MolPickler.h:97
RDKit::PicklerOps::AtomProps
Definition: MolPickler.h:49
RDKit::PicklerOps::PropertyPickleOptions
PropertyPickleOptions
Definition: MolPickler.h:46
RDKit::MolPickler::QUERY_ISNEGATED
Definition: MolPickler.h:98
RDKit::MolPickler::QUERY_EQUALS
Definition: MolPickler.h:104
RDKit::MolPickler::ATOM_MAPNUMBER
Definition: MolPickler.h:95
RDKit::MolPickler::endianId
static const boost::int32_t endianId
Definition: MolPickler.h:64
RDKit::MolPickler::BEGINCONFS
Definition: MolPickler.h:94
RDKit::PicklerOps::PrivateProps
Definition: MolPickler.h:52
Bond.h
RDKit::MolPickler::ATOM_DUMMYLABEL
Definition: MolPickler.h:115
RDKit::MolPickler::ATOM_PDB_RESIDUE_ALTLOC
Definition: MolPickler.h:118
RDKit::MolPickler::ATOM_PDB_RESIDUE_RESIDUENAME
Definition: MolPickler.h:119
RDKit
Std stuff.
Definition: Atom.h:29
RDKit::MolPickler::QUERY_NUMCHILDREN
Definition: MolPickler.h:99
RDKit::RingInfo
A class to store information about a molecule&#39;s rings.
Definition: RingInfo.h:21
RDKit::MolPicklerException
used to indicate exceptions whilst pickling (serializing) molecules
Definition: MolPickler.h:34
RDKit::MolPickler::ATOM_NUMBER
Definition: MolPickler.h:74
RDKit::MolPickler::ATOM_ISAROMATIC
Definition: MolPickler.h:80
RDKit::PicklerOps::AllProps
Definition: MolPickler.h:54
RDKit::MolPickler::QUERY_GREATER
Definition: MolPickler.h:105
RDKit::MolPickler::BOND_ENDATOMIDX
Definition: MolPickler.h:85
RDKit::Bond
class for representing a bond
Definition: Bond.h:47
RDKit::MolPicklerException::MolPicklerException
MolPicklerException(const std::string msg)
Definition: MolPickler.h:37
RDKit::MolPickler::BEGINATOMPROPS
Definition: MolPickler.h:129
RDGeom::Point3D
Definition: point.h:45
RDKit::MolPickler::QUERY_OR
Definition: MolPickler.h:102
RDKit::MolPickler::ATOM_PDB_RESIDUE_OCCUPANCY
Definition: MolPickler.h:122
RDKit::MolPickler::BEGINQUERY
Definition: MolPickler.h:96
RDKit::MolPickler
handles pickling (serializing) molecules
Definition: MolPickler.h:60
RDKit::PicklerOps::QueryAtomData
Definition: MolPickler.h:51
RDKit::MolPickler::BEGINPROPS
Definition: MolPickler.h:89
RDCatalog::versionMajor
const int versionMajor
Definition: Catalog.h:31
RDKit::Conformer
The class for representing 2D or 3D conformation of a molecule.
Definition: Conformer.h:41
RDKit::MolPickler::molFromPickle
static void molFromPickle(const std::string &pickle, ROMol *mol)
constructs a molecule from a pickle stored in a string
RDKit::MolPicklerException::message
const char * message() const
Definition: MolPickler.h:38
RDKit::MolPickler::BOND_INDEX
Definition: MolPickler.h:83
RDKit::PicklerOps::BondProps
Definition: MolPickler.h:50
RDKit::MolPickler::ATOM_PDB_RESIDUE_SEGMENTNUMBER
Definition: MolPickler.h:127
RDKit::MolPickler::molFromPickle
static void molFromPickle(std::istream &ss, ROMol &mol)
Definition: MolPickler.h:167
RDKit::MolPickler::ENDMOL
Definition: MolPickler.h:93
RDKit::MolPickler::ATOM_PDB_RESIDUE_ISHETEROATOM
Definition: MolPickler.h:124
RDKit::MolPickler::ATOM_PDB_RESIDUE_CHAINID
Definition: MolPickler.h:120
Atom.h
Defines the Atom class and associated typedefs.
RDKit::MolPickler::ATOM_CHIRALTAG
Definition: MolPickler.h:78
RDKit::MolPickler::QUERY_ATOMRING
Definition: MolPickler.h:112
RDKit::MolPickler::ATOM_PDB_RESIDUE_SECONDARYSTRUCTURE
Definition: MolPickler.h:125
RDKit::PicklerOps::NoProps
Definition: MolPickler.h:47
RDKit::MolPickler::ENDBOND
Definition: MolPickler.h:88
QueryAtom.h
RDKit::MolPickler::QUERY_BOOL
Definition: MolPickler.h:100
RDKit::MolPickler::QUERY_NULL
Definition: MolPickler.h:111
RDKit::MolPickler::QUERY_SET
Definition: MolPickler.h:110
RDKit::MolPickler::ATOM_POS
Definition: MolPickler.h:75
RDKit::MolPickler::pickleMol
static void pickleMol(const ROMol &mol, std::ostream &ss, unsigned int propertyFlags)
Definition: MolPickler.h:144
RDKit::Atom
The class for representing atoms.
Definition: Atom.h:68
RDKit::EnumerationStrategyPickler::pickle
void pickle(const boost::shared_ptr< EnumerationStrategyBase > &enumerator, std::ostream &ss)
pickles a EnumerationStrategy and adds the results to a stream ss
RDKit::MolPicklerException::~MolPicklerException
~MolPicklerException()
Definition: MolPickler.h:39