File: JSONParsers.cpp

package info (click to toggle)
rdkit 202503.6-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 222,024 kB
  • sloc: cpp: 411,111; python: 78,482; ansic: 26,181; java: 8,285; javascript: 4,404; sql: 2,393; yacc: 1,626; lex: 1,267; cs: 1,090; makefile: 580; xml: 229; fortran: 183; sh: 121
file content (122 lines) | stat: -rw-r--r-- 5,009 bytes parent folder | download | duplicates (2)
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
//
//  Copyright (C) 2024 Novartis Biomedical Research and other RDKit contributors
//
//   @@ 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.
//

#define USE_BETTER_ENUMS
#include "JSONParsers.h"
#include <GraphMol/MolPickler.h>
#include <GraphMol/FileParsers/PNGParser.h>
#include <GraphMol/SmilesParse/SmilesJSONParsers.h>
#include <RDGeneral/BoostStartInclude.h>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <RDGeneral/BoostEndInclude.h>

namespace RDKit {
namespace MinimalLib {

void updatePropertyPickleOptionsFromJSON(unsigned int &propFlags,
                                         const char *details_json) {
  if (details_json && details_json[0]) {
    std::istringstream ss;
    boost::property_tree::ptree pt;
    ss.str(details_json);
    boost::property_tree::read_json(ss, pt);
    const auto nodeIt = pt.find("propertyFlags");
    if (nodeIt != pt.not_found()) {
      auto propertyFlagsFromJson =
          (+PicklerOps::PropertyPickleOptions::NoProps)._to_integral();
      for (const auto *key : PicklerOps::PropertyPickleOptions::_names()) {
        if (nodeIt->second.get(key, false)) {
          propertyFlagsFromJson |=
              PicklerOps::PropertyPickleOptions::_from_string(key)
                  ._to_integral();
        }
      }
      propFlags = propertyFlagsFromJson;
    }
  }
}

void updateSanitizeFlagsFromJSON(unsigned int &sanitizeFlags,
                                 const char *details_json) {
  if (details_json && details_json[0]) {
    std::istringstream ss;
    boost::property_tree::ptree pt;
    ss.str(details_json);
    boost::property_tree::read_json(ss, pt);
    auto sanitizeFlagsFromJson =
        (+MolOps::SanitizeFlags::SANITIZE_NONE)._to_integral();
    for (const auto *key : MolOps::SanitizeFlags::_names()) {
      if (pt.get(key, false)) {
        sanitizeFlagsFromJson |=
            MolOps::SanitizeFlags::_from_string(key)._to_integral();
      }
    }
    sanitizeFlags = sanitizeFlagsFromJson;
  }
}

void updateRemoveHsParametersFromJSON(MolOps::RemoveHsParameters &ps,
                                      bool &sanitize,
                                      const char *details_json) {
  if (details_json && details_json[0]) {
    boost::property_tree::ptree pt;
    std::istringstream ss;
    ss.str(details_json);
    boost::property_tree::read_json(ss, pt);
    ps.removeDegreeZero = pt.get("removeDegreeZero", ps.removeDegreeZero);
    ps.removeHigherDegrees =
        pt.get("removeHigherDegrees", ps.removeHigherDegrees);
    ps.removeOnlyHNeighbors =
        pt.get("removeOnlyHNeighbors", ps.removeOnlyHNeighbors);
    ps.removeIsotopes = pt.get("removeIsotopes", ps.removeIsotopes);
    ps.removeAndTrackIsotopes =
        pt.get("removeAndTrackIsotopes", ps.removeAndTrackIsotopes);
    ps.removeDummyNeighbors =
        pt.get("removeDummyNeighbors", ps.removeDummyNeighbors);
    ps.removeDefiningBondStereo =
        pt.get("removeDefiningBondStereo", ps.removeDefiningBondStereo);
    ps.removeWithWedgedBond =
        pt.get("removeWithWedgedBond", ps.removeWithWedgedBond);
    ps.removeWithQuery = pt.get("removeWithQuery", ps.removeWithQuery);
    ps.removeMapped = pt.get("removeMapped", ps.removeMapped);
    ps.removeInSGroups = pt.get("removeInSGroups", ps.removeInSGroups);
    ps.showWarnings = pt.get("showWarnings", ps.showWarnings);
    ps.removeNonimplicit = pt.get("removeNonimplicit", ps.removeNonimplicit);
    ps.updateExplicitCount =
        pt.get("updateExplicitCount", ps.updateExplicitCount);
    ps.removeHydrides = pt.get("removeHydrides", ps.removeHydrides);
    ps.removeNontetrahedralNeighbors = pt.get("removeNontetrahedralNeighbors",
                                              ps.removeNontetrahedralNeighbors);
    sanitize = pt.get("sanitize", sanitize);
  }
}

void updatePNGMetadataParamsFromJSON(PNGMetadataParams &params,
                                     const char *details_json) {
  if (details_json && strlen(details_json)) {
    boost::property_tree::ptree pt;
    std::istringstream ss;
    ss.str(details_json);
    boost::property_tree::read_json(ss, pt);
    params.includePkl = pt.get("includePkl", params.includePkl);
    params.includeSmiles = pt.get("includeSmiles", params.includeSmiles);
    params.includeMol = pt.get("includeMol", params.includeMol);
    updatePropertyPickleOptionsFromJSON(params.propertyFlags, details_json);
    updateSmilesWriteParamsFromJSON(params.smilesWriteParams, details_json);
    unsigned int restoreBondDirs = params.restoreBondDirs;
    updateCXSmilesFieldsFromJSON(params.cxSmilesFlags, restoreBondDirs,
                                 details_json);
    params.restoreBondDirs =
        RestoreBondDirOption::_from_integral(restoreBondDirs);
  }
}
}  // end namespace MinimalLib
}  // end namespace RDKit