RDKit: Templates.h Source File

| 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>
12#include <GraphMol/SmilesParse/SmilesParse.h>
13#include <GraphMol/MolOps.h>
14
15#include "TemplateSmiles.h"
16
17#include <fstream>
18#include <unordered_map>
19
20namespace RDDepict {
21 class 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
67 void loadDefaultTemplates() {
68 clearTemplates();
69 // load default templates into m_templates map by atom count
70 for (const auto &smiles : TEMPLATE_SMILES) {
71 std::shared_ptr<RDKit::ROMol> mol(RDKit::SmilesToMol(smiles));
72 m_templates[mol->getNumAtoms()].push_back(mol);
73 }
74 }
75
76 private:
77 CoordinateTemplates() { loadDefaultTemplates(); }
78 CoordinateTemplates(const CoordinateTemplates &) = delete;
79 CoordinateTemplates &operator=(const CoordinateTemplates &) = delete;
80
81 void clearTemplates() {
82 for (auto &[atom_cout, romols] : m_templates) {
83 romols.clear();
84 }
85 m_templates.clear();
86 }
87
88 ~CoordinateTemplates() { clearTemplates(); }
89
90 void loadTemplatesFromPath(
91 const std::string &templatePath,
92 std::unordered_map<
93 unsigned int, std::vector<std::shared_ptr<RDKit::ROMol>>> &templates);
94
95 std::unordered_map<unsigned int, std::vector<std::shared_ptr<RDKit::ROMol>>>
96 m_templates;
97};
98} // namespace RDDepict
MolOps.h
ROMol.h
Defines the primary molecule class ROMol as well as associated typedefs.
SmilesParse.h
TemplateSmiles.h
TEMPLATE_SMILES
const std::vector< std::string > TEMPLATE_SMILES
Definition TemplateSmiles.h:20
RDDepict::CoordinateTemplates
Definition Templates.h:21
RDDepict::CoordinateTemplates::getMatchingTemplates
const std::vector< std::shared_ptr< RDKit::ROMol > > & getMatchingTemplates(unsigned int atomCount)
Definition Templates.h:45
RDDepict::CoordinateTemplates::hasTemplateOfSize
bool hasTemplateOfSize(unsigned int atomCount)
Definition Templates.h:38
RDDepict::CoordinateTemplates::addRingSystemTemplates
void addRingSystemTemplates(const std::string &templatePath)
RDDepict::CoordinateTemplates::assertValidTemplate
static void assertValidTemplate(RDKit::ROMol &mol, const std::string &smiles)
check if a template is considered valid
RDDepict::CoordinateTemplates::loadDefaultTemplates
void loadDefaultTemplates()
Definition Templates.h:67
RDDepict::CoordinateTemplates::setRingSystemTemplates
void setRingSystemTemplates(const std::string &templatePath)
RDDepict::CoordinateTemplates::getRingSystemTemplates
static CoordinateTemplates & getRingSystemTemplates()
returns a reference to the singleton CoordinateTemplates
Definition Templates.h:33
RDKit::ROMol
Definition ROMol.h:199
RDKIT_DEPICTOR_EXPORT
#define RDKIT_DEPICTOR_EXPORT
Definition export.h:97
RDDepict
Definition Reaction.h:477
RDKit::v1::SmilesToMol
RDKit::RWMol * SmilesToMol(const std::string &smi, const SmilesParserParams &ps)
Definition SmilesParse.h:118