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