RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
Templates.h
Go to the documentation of this file.
1//
2// Copyright (C) 2023 Schrödinger, 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
11#include <GraphMol/ROMol.h>
13#include <GraphMol/MolOps.h>
14
15#include "TemplateSmarts.h"
16
17#include <fstream>
18#include <unordered_map>
19
20namespace RDDepict {
21class RDKIT_DEPICTOR_EXPORT CoordinateTemplates {
22 public:
23 //! returns a reference to the singleton CoordinateTemplates
24 /*
25 \return a reference to the singleton CoordinateTemplates
26
27 <b>Notes:</b>
28 - if the singleton CoordinateTemplates has already been instantiated
29 the singleton will be returned, otherwise the singleton will
30 be constructed.
31
32 */
33 static CoordinateTemplates &getRingSystemTemplates() {
34 static CoordinateTemplates template_mols;
35 return template_mols;
36 }
37
38 bool hasTemplateOfSize(unsigned int atomCount) {
39 if (m_templates.find(atomCount) != m_templates.end()) {
40 return true;
41 }
42 return false;
43 }
44
45 const std::vector<std::shared_ptr<RDKit::ROMol>> &getMatchingTemplates(
46 unsigned int atomCount) {
47 return m_templates[atomCount];
48 }
49
50 void setRingSystemTemplates(const std::string &templatePath);
51 void addRingSystemTemplates(const std::string &templatePath);
52
53 //! check if a template is considered valid
54 /*
55 \param template mol
56 \param smiles for logging
57 \return whether the template is valid
58
59 <b>A template is considered valid if it:</b>
60 - has 2D coordinates
61 - is a ring system (spiro'd ring systems are OK)
62 - consists of only 1 fragment
63
64 */
65 static void assertValidTemplate(RDKit::ROMol &mol, const std::string &smiles);
66
68 clearTemplates();
69 // load default templates into m_templates map by atom count
70 for (const auto &smarts : TEMPLATE_SMARTS) {
71 std::shared_ptr<RDKit::ROMol> mol(RDKit::SmartsToMol(smarts));
72 if (!mol) {
73 continue;
74 }
75 // Initialize ring info using symmetrizeSSSR to match depictor ring counting
78 m_templates[mol->getNumAtoms()].push_back(mol);
79 }
80 }
81
82 private:
83 CoordinateTemplates() { loadDefaultTemplates(); }
84 CoordinateTemplates(const CoordinateTemplates &) = delete;
85 CoordinateTemplates &operator=(const CoordinateTemplates &) = delete;
86
87 void clearTemplates() {
88 for (auto &[atom_cout, romols] : m_templates) {
89 romols.clear();
90 }
91 m_templates.clear();
92 }
93
94 ~CoordinateTemplates() { clearTemplates(); }
95
96 void loadTemplatesFromPath(
97 const std::string &templatePath,
98 std::unordered_map<
99 unsigned int, std::vector<std::shared_ptr<RDKit::ROMol>>> &templates);
100
101 std::unordered_map<unsigned int, std::vector<std::shared_ptr<RDKit::ROMol>>>
102 m_templates;
103};
104} // namespace RDDepict
Defines the primary molecule class ROMol as well as associated typedefs.
const std::vector< std::string > TEMPLATE_SMARTS
const std::vector< std::shared_ptr< RDKit::ROMol > > & getMatchingTemplates(unsigned int atomCount)
Definition Templates.h:45
bool hasTemplateOfSize(unsigned int atomCount)
Definition Templates.h:38
void addRingSystemTemplates(const std::string &templatePath)
static void assertValidTemplate(RDKit::ROMol &mol, const std::string &smiles)
check if a template is considered valid
void setRingSystemTemplates(const std::string &templatePath)
static CoordinateTemplates & getRingSystemTemplates()
returns a reference to the singleton CoordinateTemplates
Definition Templates.h:33
#define RDKIT_DEPICTOR_EXPORT
Definition export.h:97
RDKIT_GRAPHMOL_EXPORT int symmetrizeSSSR(ROMol &mol, std::vector< std::vector< int > > &res, bool includeDativeBonds=false, bool includeHydrogenBonds=false)
symmetrize the molecule's Smallest Set of Smallest Rings
RWMol * SmartsToMol(const std::string &sma, const SmartsParserParams &ps)
std::vector< INT_VECT > VECT_INT_VECT
Definition types.h:240