1 //
2 //  Copyright (c) 2020 Greg Landrum
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 
11 #include <GraphMol/ChemReactions/Reaction.h>
12 #include <GraphMol/ChemReactions/ReactionPickler.h>
13 #include <GraphMol/ChemReactions/ReactionParser.h>
14 #include <GraphMol/FileParsers/PNGParser.h>
15 #include <boost/algorithm/string.hpp>
16 
17 namespace RDKit {
18 
19 namespace PNGData {
20 const std::string rxnSmilesTag = "ReactionSmiles";
21 const std::string rxnSmartsTag = "ReactionSmarts";
22 const std::string rxnRxnTag = "ReactionRxn";
23 const std::string rxnPklTag = "rdkitReactionPKL";
24 }  // namespace PNGData
25 
addChemicalReactionToPNGStream(const ChemicalReaction & rxn,std::istream & iStream,bool includePkl,bool includeSmiles,bool includeSmarts,bool includeRxn)26 std::string addChemicalReactionToPNGStream(const ChemicalReaction &rxn,
27                                            std::istream &iStream,
28                                            bool includePkl, bool includeSmiles,
29                                            bool includeSmarts,
30                                            bool includeRxn) {
31   std::vector<std::pair<std::string, std::string>> metadata;
32   if (includePkl) {
33     std::string pkl;
34     ReactionPickler::pickleReaction(rxn, pkl);
35     metadata.push_back(std::make_pair(augmentTagName(PNGData::rxnPklTag), pkl));
36   }
37   if (includeSmiles) {
38     std::string smi = ChemicalReactionToRxnSmiles(rxn);
39     metadata.push_back(
40         std::make_pair(augmentTagName(PNGData::rxnSmilesTag), smi));
41   }
42   if (includeSmarts) {
43     std::string smi = ChemicalReactionToRxnSmarts(rxn);
44     metadata.push_back(
45         std::make_pair(augmentTagName(PNGData::rxnSmartsTag), smi));
46   }
47   if (includeRxn) {
48     std::string mb = ChemicalReactionToRxnBlock(rxn);
49     metadata.push_back(std::make_pair(augmentTagName(PNGData::rxnRxnTag), mb));
50   }
51   return addMetadataToPNGStream(iStream, metadata);
52 };
53 
PNGStreamToChemicalReaction(std::istream & inStream)54 ChemicalReaction *PNGStreamToChemicalReaction(std::istream &inStream) {
55   ChemicalReaction *res = nullptr;
56   auto metadata = PNGStreamToMetadata(inStream);
57   bool formatFound = false;
58   for (const auto &pr : metadata) {
59     if (boost::starts_with(pr.first, PNGData::rxnPklTag)) {
60       res = new ChemicalReaction(pr.second);
61       formatFound = true;
62     } else if (boost::starts_with(pr.first, PNGData::rxnSmilesTag)) {
63       std::map<std::string, std::string> *replacements = nullptr;
64       bool useSmiles = true;
65       res = RxnSmartsToChemicalReaction(pr.second, replacements, useSmiles);
66       formatFound = true;
67     } else if (boost::starts_with(pr.first, PNGData::rxnSmartsTag)) {
68       res = RxnSmartsToChemicalReaction(pr.second);
69       formatFound = true;
70     } else if (boost::starts_with(pr.first, PNGData::rxnRxnTag)) {
71       res = RxnBlockToChemicalReaction(pr.second);
72       formatFound = true;
73     }
74     if (formatFound) {
75       break;
76     }
77   }
78   if (!formatFound) {
79     throw FileParseException("No suitable metadata found.");
80   }
81   return res;
82 }
83 
84 }  // namespace RDKit
85