1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
// $Id: MolPickler.cpp 1123 2009-06-01 13:04:33Z glandrum $
//
// Copyright (C) 2009 Greg Landrum
// Copyright (c) 2014, Novartis Institutes for BioMedical Research Inc.
//
// @@ All Rights Reserved @@
// This file is part of the RDKit.
// The contents are covered by the terms of the BSD license
// which is included in the file license.txt, found at the root
// of the RDKit source tree.
//
#include <sstream>
#include <GraphMol/RDKitBase.h>
#include <GraphMol/MolPickler.h>
#include <GraphMol/ChemReactions/Reaction.h>
#include <GraphMol/ChemReactions/ReactionPickler.h>
#include <RDGeneral/utils.h>
#include <RDGeneral/RDLog.h>
#include <RDGeneral/StreamOps.h>
#include <RDGeneral/types.h>
#include <boost/cstdint.hpp>
using boost::int32_t;
using boost::uint32_t;
namespace RDKit {
const int32_t ReactionPickler::versionMajor = 1;
const int32_t ReactionPickler::versionMinor = 1;
const int32_t ReactionPickler::versionPatch = 0;
const int32_t ReactionPickler::endianId = 0xDEADBEEF;
void streamWrite(std::ostream &ss, ReactionPickler::Tags tag) {
int32_t tmp = tag;
streamWrite(ss, tmp);
}
template <typename T>
void streamWrite(std::ostream &ss, ReactionPickler::Tags tag, const T &what) {
streamWrite(ss, tag);
streamWrite(ss, what);
};
void streamRead(std::istream &ss, ReactionPickler::Tags &tag) {
int32_t tmp;
streamRead(ss, tmp);
tag = static_cast<ReactionPickler::Tags>(tmp);
}
void ReactionPickler::pickleReaction(const ChemicalReaction *rxn,
std::ostream &ss) {
PRECONDITION(rxn, "empty reaction");
streamWrite(ss, endianId);
streamWrite(ss, VERSION);
streamWrite(ss, versionMajor);
streamWrite(ss, versionMinor);
streamWrite(ss, versionPatch);
_pickle(rxn, ss);
}
void ReactionPickler::pickleReaction(const ChemicalReaction *rxn,
std::string &res) {
PRECONDITION(rxn, "empty reaction");
std::stringstream ss(std::ios_base::binary | std::ios_base::out |
std::ios_base::in);
ReactionPickler::pickleReaction(rxn, ss);
res = ss.str();
}
// NOTE: if the reaction passed in here already has reactants and products
// be left intact.
void ReactionPickler::reactionFromPickle(std::istream &ss,
ChemicalReaction *rxn) {
PRECONDITION(rxn, "empty reaction");
Tags tag;
int32_t tmpInt;
streamRead(ss, tmpInt);
if (tmpInt != endianId) {
throw ReactionPicklerException(
"Bad pickle format: bad endian ID or invalid file format");
}
streamRead(ss, tag);
if (tag != VERSION) {
throw ReactionPicklerException("Bad pickle format: no version tag");
}
int32_t majorVersion, minorVersion, patchVersion;
streamRead(ss, majorVersion);
streamRead(ss, minorVersion);
streamRead(ss, patchVersion);
if (majorVersion > versionMajor ||
(majorVersion == versionMajor && minorVersion > versionMinor)) {
BOOST_LOG(rdWarningLog) << "Depickling from a version number ("
<< majorVersion << "." << minorVersion << ")"
<< "that is higher than our version ("
<< versionMajor << "." << versionMinor
<< ").\nThis probably won't work." << std::endl;
}
majorVersion = 1000 * majorVersion + minorVersion * 10 + patchVersion;
_depickle(ss, rxn, majorVersion);
}
void ReactionPickler::reactionFromPickle(const std::string &pickle,
ChemicalReaction *rxn) {
PRECONDITION(rxn, "empty reaction");
std::stringstream ss(std::ios_base::binary | std::ios_base::out |
std::ios_base::in);
ss.write(pickle.c_str(), pickle.length());
ReactionPickler::reactionFromPickle(ss, rxn);
}
void ReactionPickler::_pickle(const ChemicalReaction *rxn, std::ostream &ss) {
PRECONDITION(rxn, "empty reaction");
uint32_t tmpInt;
tmpInt = static_cast<int32_t>(rxn->getNumReactantTemplates());
streamWrite(ss, tmpInt);
tmpInt = static_cast<int32_t>(rxn->getNumProductTemplates());
streamWrite(ss, tmpInt);
tmpInt = static_cast<int32_t>(rxn->getNumAgentTemplates());
streamWrite(ss, tmpInt);
uint32_t flag = 0;
if (rxn->getImplicitPropertiesFlag()) flag |= 0x1;
if (rxn->df_needsInit) flag |= 0x2;
streamWrite(ss, flag);
// -------------------
//
// Write Reactants
//
// -------------------
streamWrite(ss, BEGINREACTANTS);
for (MOL_SPTR_VECT::const_iterator tmpl = rxn->beginReactantTemplates();
tmpl != rxn->endReactantTemplates(); ++tmpl) {
MolPickler::pickleMol(tmpl->get(), ss);
}
streamWrite(ss, ENDREACTANTS);
streamWrite(ss, BEGINPRODUCTS);
for (MOL_SPTR_VECT::const_iterator tmpl = rxn->beginProductTemplates();
tmpl != rxn->endProductTemplates(); ++tmpl) {
MolPickler::pickleMol(tmpl->get(), ss);
}
streamWrite(ss, ENDPRODUCTS);
if (rxn->getNumAgentTemplates()) {
streamWrite(ss, BEGINAGENTS);
for (MOL_SPTR_VECT::const_iterator tmpl = rxn->beginAgentTemplates();
tmpl != rxn->endAgentTemplates(); ++tmpl) {
MolPickler::pickleMol(tmpl->get(), ss);
}
streamWrite(ss, ENDAGENTS);
}
streamWrite(ss, ENDREACTION);
} // end of _pickle
void ReactionPickler::_depickle(std::istream &ss, ChemicalReaction *rxn,
int version) {
PRECONDITION(rxn, "empty reaction");
Tags tag;
uint32_t numReactants, numProducts, numAgents = 0;
streamRead(ss, numReactants);
streamRead(ss, numProducts);
if (version > 1000) {
streamRead(ss, numAgents);
}
// we use this here and below to set df_needsInit, so don't re-use the
// variable
uint32_t flag = 0;
streamRead(ss, flag);
rxn->setImplicitPropertiesFlag(flag & 0x1);
// -------------------
//
// Read Reactants
//
// -------------------
streamRead(ss, tag);
if (tag != BEGINREACTANTS) {
throw ReactionPicklerException(
"Bad pickle format: BEGINREACTANTS tag not found.");
}
for (unsigned int i = 0; i < numReactants; ++i) {
ROMol *mol = new ROMol();
MolPickler::molFromPickle(ss, mol);
rxn->addReactantTemplate(ROMOL_SPTR(mol));
}
streamRead(ss, tag);
if (tag != ENDREACTANTS) {
throw ReactionPicklerException(
"Bad pickle format: ENDREACTANTS tag not found.");
}
streamRead(ss, tag);
if (tag != BEGINPRODUCTS) {
throw ReactionPicklerException(
"Bad pickle format: BEGINPRODUCTS tag not found.");
}
for (unsigned int i = 0; i < numProducts; ++i) {
ROMol *mol = new ROMol();
MolPickler::molFromPickle(ss, mol);
rxn->addProductTemplate(ROMOL_SPTR(mol));
}
streamRead(ss, tag);
if (tag != ENDPRODUCTS) {
throw ReactionPicklerException(
"Bad pickle format: ENDPRODUCTS tag not found.");
}
if (numAgents != 0) {
streamRead(ss, tag);
if (tag != BEGINAGENTS) {
throw ReactionPicklerException(
"Bad pickle format: BEGINAGENTS tag not found.");
}
for (unsigned int i = 0; i < numAgents; ++i) {
ROMol *mol = new ROMol();
MolPickler::molFromPickle(ss, mol);
rxn->addAgentTemplate(ROMOL_SPTR(mol));
}
streamRead(ss, tag);
if (tag != ENDAGENTS) {
throw ReactionPicklerException(
"Bad pickle format: ENDAGENTS tag not found.");
}
}
// need to do this after we add reactants and products
rxn->df_needsInit = flag & 0x2;
} // end of _depickle
}; // end of RDKit namespace
|