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
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 //! handles pickling (serializing) molecules
46 class MolPickler {
47 public:
48 static const boost::int32_t versionMajor, versionMinor,
49 versionPatch; //!< mark the pickle version
50 static const boost::int32_t endianId; //! mark the endian-ness of the pickle
51
52 //! the pickle format is tagged using these tags:
53 //! NOTE: if you add to this list, be sure to put new entries AT THE BOTTOM,
54 // otherwise
55 //! you will break old pickles.
56 typedef enum {
57 VERSION = 0,
58 BEGINATOM,
59 ATOM_INDEX,
60 ATOM_NUMBER,
61 ATOM_POS,
62 ATOM_CHARGE,
63 ATOM_NEXPLICIT,
64 ATOM_CHIRALTAG,
65 ATOM_MASS,
66 ATOM_ISAROMATIC,
67 ENDATOM,
68 BEGINBOND,
69 BOND_INDEX,
70 BOND_BEGATOMIDX,
71 BOND_ENDATOMIDX,
72 BOND_TYPE,
73 BOND_DIR,
74 ENDBOND,
75 BEGINPROPS,
76 ENDPROPS,
77 BEGINSSSR,
78 ENDSSSR,
79 ENDMOL,
80 BEGINCONFS,
81 ATOM_MAPNUMBER,
82 BEGINQUERY,
83 QUERY_VALUE,
84 QUERY_ISNEGATED,
85 QUERY_NUMCHILDREN,
86 QUERY_BOOL,
87 QUERY_AND,
88 QUERY_OR,
89 QUERY_XOR,
90 QUERY_EQUALS,
91 QUERY_GREATER,
92 QUERY_GREATEREQUAL,
93 QUERY_LESS,
94 QUERY_LESSEQUAL,
95 QUERY_RANGE,
96 QUERY_SET,
97 QUERY_NULL,
98 QUERY_ATOMRING,
99 QUERY_RECURSIVE,
100 ENDQUERY,
101 ATOM_DUMMYLABEL,
102 BEGIN_ATOM_MONOMER,
103 ATOM_PDB_RESIDUE_SERIALNUMBER,
104 ATOM_PDB_RESIDUE_ALTLOC,
105 ATOM_PDB_RESIDUE_RESIDUENAME,
106 ATOM_PDB_RESIDUE_CHAINID,
107 ATOM_PDB_RESIDUE_INSERTIONCODE,
108 ATOM_PDB_RESIDUE_OCCUPANCY,
109 ATOM_PDB_RESIDUE_TEMPFACTOR,
110 ATOM_PDB_RESIDUE_ISHETEROATOM,
111 ATOM_PDB_RESIDUE_SECONDARYSTRUCTURE,
112 ATOM_PDB_RESIDUE_RESIDUENUMBER,
113 ATOM_PDB_RESIDUE_SEGMENTNUMBER,
114 END_ATOM_MONOMER,
115 } Tags;
116
117 //! pickles a molecule and sends the results to stream \c ss
118 static void pickleMol(const ROMol *mol, std::ostream &ss);
119 static void pickleMol(const ROMol &mol, std::ostream &ss) {
120 MolPickler::pickleMol(&mol, ss);
121 };
122 //! pickles a molecule and adds the results to string \c res
123 static void pickleMol(const ROMol *mol, std::string &res);
124 static void pickleMol(const ROMol &mol, std::string &res) {
125 MolPickler::pickleMol(&mol, res);
126 };
127
128 //! constructs a molecule from a pickle stored in a string
129 static void molFromPickle(const std::string &pickle, ROMol *mol);
130 static void molFromPickle(const std::string &pickle, ROMol &mol) {
131 MolPickler::molFromPickle(pickle, &mol);
132 };
133
134 //! constructs a molecule from a pickle stored in a stream
135 static void molFromPickle(std::istream &ss, ROMol *mol);
136 static void molFromPickle(std::istream &ss, ROMol &mol) {
137 MolPickler::molFromPickle(ss, &mol);
138 };
139
140 private:
141 //! do the actual work of pickling a molecule
142 template <typename T>
143 static void _pickle(const ROMol *mol, std::ostream &ss);
144
145 //! do the actual work of pickling an Atom
146 template <typename T>
147 static void _pickleAtom(std::ostream &ss, const Atom *atom);
148
149 //! do the actual work of pickling a Bond
150 template <typename T>
151 static void _pickleBond(std::ostream &ss, const Bond *bond,
152 std::map<int, int> &atomIdxMap);
153
154 //! do the actual work of pickling an SSSR structure
155 template <typename T>
156 static void _pickleSSSR(std::ostream &ss, const RingInfo *ringInfo,
157 std::map<int, int> &atomIdxMap);
158
159 //! do the actual work of pickling a Conformer
160 template <typename T>
161 static void _pickleConformer(std::ostream &ss, const Conformer *conf);
162
163 //! do the actual work of de-pickling a molecule
164 template <typename T>
165 static void _depickle(std::istream &ss, ROMol *mol, int version,
166 int numAtoms);
167
168 //! extract atomic data from a pickle and add the resulting Atom to the
169 // molecule
170 template <typename T>
171 static Atom *_addAtomFromPickle(std::istream &ss, ROMol *mol,
172 RDGeom::Point3D &pos, int version,
173 bool directMap = false);
174
175 //! extract bond data from a pickle and add the resulting Bond to the molecule
176 template <typename T>
177 static Bond *_addBondFromPickle(std::istream &ss, ROMol *mol, int version,
178 bool directMap = false);
179
180 //! extract ring info from a pickle and add the resulting RingInfo to the
181 // molecule
182 template <typename T>
183 static void _addRingInfoFromPickle(std::istream &ss, ROMol *mol, int version,
184 bool directMap = false);
185
186 //! extract a conformation from a pickle
187 template <typename T>
188 static Conformer *_conformerFromPickle(std::istream &ss, int version);
189
190 //! backwards compatibility
191 static void _pickleV1(const ROMol *mol, std::ostream &ss);
192 //! backwards compatibility
193 static void _depickleV1(std::istream &ss, ROMol *mol);
194 //! backwards compatibility
195 static void _addAtomFromPickleV1(std::istream &ss, ROMol *mol);
196 //! backwards compatibility
197 static void _addBondFromPickleV1(std::istream &ss, ROMol *mol);
198 };
199 };
200
201 #endif
RDKit::MolPickler::END_ATOM_MONOMER
Definition: MolPickler.h:114
RDKit::MolPickler::ENDQUERY
Definition: MolPickler.h:100
RDKit::MolPickler::QUERY_AND
Definition: MolPickler.h:87
RDKit::MolPickler::BEGINATOM
Definition: MolPickler.h:58
RDKit::MolPickler::ATOM_INDEX
Definition: MolPickler.h:59
RDKit::MolPickler::QUERY_XOR
Definition: MolPickler.h:89
RDKit::MolPickler::ATOM_PDB_RESIDUE_TEMPFACTOR
Definition: MolPickler.h:109
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:107
RDKit::MolPicklerException::MolPicklerException
MolPicklerException(const char *msg)
Definition: MolPickler.h:36
RDKit::MolPickler::ATOM_CHARGE
Definition: MolPickler.h:62
RDKit::MolPickler::ATOM_PDB_RESIDUE_SERIALNUMBER
Definition: MolPickler.h:103
RDKit::MolPickler::ATOM_MASS
Definition: MolPickler.h:65
QueryBond.h
RDKit::MolPickler::BOND_DIR
Definition: MolPickler.h:73
RDKit::MolPickler::BEGINSSSR
Definition: MolPickler.h:77
RDKit::MolPickler::pickleMol
static void pickleMol(const ROMol &mol, std::string &res)
Definition: MolPickler.h:124
RDKit::MolPickler::QUERY_GREATEREQUAL
Definition: MolPickler.h:92
point.h
RDKit::MolPickler::ENDPROPS
Definition: MolPickler.h:76
RDKit::MolPickler::Tags
Tags
mark the endian-ness of the pickle
Definition: MolPickler.h:56
RDKit::MolPickler::versionMajor
static const boost::int32_t versionMajor
Definition: MolPickler.h:48
RDKit::MolPickler::ATOM_NEXPLICIT
Definition: MolPickler.h:63
RDKit::MolPickler::BEGINBOND
Definition: MolPickler.h:68
RDKit::MolPickler::versionMinor
static const boost::int32_t versionMinor
Definition: MolPickler.h:48
RDKit::MolPickler::QUERY_RANGE
Definition: MolPickler.h:95
RDKit::MolPickler::ENDATOM
Definition: MolPickler.h:67
RDKit::MolPickler::QUERY_RECURSIVE
Definition: MolPickler.h:99
RDKit::MolPickler::versionPatch
static const boost::int32_t versionPatch
mark the pickle version
Definition: MolPickler.h:48
RDKit::MolPickler::BOND_TYPE
Definition: MolPickler.h:72
RDKit::MolPickler::ENDSSSR
Definition: MolPickler.h:78
RDKit::ROMol
ROMol is a molecule class that is intended to have a fixed topology.
Definition: ROMol.h:102
RDKit::MolPickler::ATOM_PDB_RESIDUE_RESIDUENUMBER
Definition: MolPickler.h:112
RDKit::MolPickler::QUERY_LESSEQUAL
Definition: MolPickler.h:94
RDKit::MolPickler::QUERY_LESS
Definition: MolPickler.h:93
RDKit::MolPickler::BEGIN_ATOM_MONOMER
Definition: MolPickler.h:102
RDKit::MolPickler::BOND_BEGATOMIDX
Definition: MolPickler.h:70
RDKit::MolPickler::molFromPickle
static void molFromPickle(const std::string &pickle, ROMol &mol)
Definition: MolPickler.h:130
RDKit::MolPickler::QUERY_VALUE
Definition: MolPickler.h:83
RDKit::MolPickler::QUERY_ISNEGATED
Definition: MolPickler.h:84
RDKit::MolPickler::QUERY_EQUALS
Definition: MolPickler.h:90
RDKit::MolPickler::ATOM_MAPNUMBER
Definition: MolPickler.h:81
RDKit::MolPickler::endianId
static const boost::int32_t endianId
Definition: MolPickler.h:50
RDKit::MolPickler::BEGINCONFS
Definition: MolPickler.h:80
Bond.h
RDKit::MolPickler::ATOM_DUMMYLABEL
Definition: MolPickler.h:101
RDKit::MolPickler::ATOM_PDB_RESIDUE_ALTLOC
Definition: MolPickler.h:104
RDKit::MolPickler::ATOM_PDB_RESIDUE_RESIDUENAME
Definition: MolPickler.h:105
RDKit
Includes a bunch of functionality for handling Atom and Bond queries.
Definition: Atom.h:28
RDKit::MolPickler::QUERY_NUMCHILDREN
Definition: MolPickler.h:85
RDKit::RingInfo
A class to store information about a molecule's rings.
Definition: RingInfo.h:21
RDKit::MolPicklerException
used to indicate exceptions whilst pickling (serializing) molecules
Definition: MolPickler.h:34
RDKit::MolPickler::VERSION
Definition: MolPickler.h:57
RDKit::MolPickler::ATOM_NUMBER
Definition: MolPickler.h:60
RDKit::MolPickler::ATOM_ISAROMATIC
Definition: MolPickler.h:66
RDKit::MolPickler::QUERY_GREATER
Definition: MolPickler.h:91
RDKit::MolPickler::BOND_ENDATOMIDX
Definition: MolPickler.h:71
RDKit::Bond
class for representing a bond
Definition: Bond.h:46
RDKit::MolPicklerException::MolPicklerException
MolPicklerException(const std::string msg)
Definition: MolPickler.h:37
RDGeom::Point3D
Definition: point.h:45
RDKit::MolPickler::QUERY_OR
Definition: MolPickler.h:88
RDKit::MolPickler::ATOM_PDB_RESIDUE_OCCUPANCY
Definition: MolPickler.h:108
RDKit::MolPickler::BEGINQUERY
Definition: MolPickler.h:82
RDKit::MolPickler
handles pickling (serializing) molecules
Definition: MolPickler.h:46
RDKit::MolPickler::BEGINPROPS
Definition: MolPickler.h:75
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:69
RDKit::MolPickler::ATOM_PDB_RESIDUE_SEGMENTNUMBER
Definition: MolPickler.h:113
RDKit::MolPickler::molFromPickle
static void molFromPickle(std::istream &ss, ROMol &mol)
Definition: MolPickler.h:136
RDKit::MolPickler::ENDMOL
Definition: MolPickler.h:79
RDKit::MolPickler::ATOM_PDB_RESIDUE_ISHETEROATOM
Definition: MolPickler.h:110
RDKit::MolPickler::ATOM_PDB_RESIDUE_CHAINID
Definition: MolPickler.h:106
Atom.h
Defines the Atom class and associated typedefs.
RDKit::MolPickler::ATOM_CHIRALTAG
Definition: MolPickler.h:64
RDKit::MolPickler::QUERY_ATOMRING
Definition: MolPickler.h:98
RDKit::MolPickler::pickleMol
static void pickleMol(const ROMol &mol, std::ostream &ss)
Definition: MolPickler.h:119
RDKit::MolPickler::ATOM_PDB_RESIDUE_SECONDARYSTRUCTURE
Definition: MolPickler.h:111
RDKit::MolPickler::ENDBOND
Definition: MolPickler.h:74
QueryAtom.h
RDKit::MolPickler::QUERY_BOOL
Definition: MolPickler.h:86
RDKit::MolPickler::QUERY_NULL
Definition: MolPickler.h:97
RDKit::MolPickler::QUERY_SET
Definition: MolPickler.h:96
RDKit::MolPickler::ATOM_POS
Definition: MolPickler.h:61
RDKit::Atom
The class for representing atoms.
Definition: Atom.h:67
RDKit::MolPicklerException::~MolPicklerException
~MolPicklerException()
Definition: MolPickler.h:39