-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathdecomp.py
More file actions
164 lines (134 loc) · 6.08 KB
/
Copy pathdecomp.py
File metadata and controls
164 lines (134 loc) · 6.08 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
###############################################################################
# #
# RMG - Reaction Mechanism Generator #
# #
# Copyright (c) 2002-2026 Prof. William H. Green (whgreen@mit.edu), #
# Prof. Richard H. West (r.west@neu.edu) and the RMG Team (rmg_dev@mit.edu) #
# #
# Permission is hereby granted, free of charge, to any person obtaining a #
# copy of this software and associated documentation files (the 'Software'), #
# to deal in the Software without restriction, including without limitation #
# the rights to use, copy, modify, merge, publish, distribute, sublicense, #
# and/or sell copies of the Software, and to permit persons to whom the #
# Software is furnished to do so, subject to the following conditions: #
# #
# The above copyright notice and this permission notice shall be included in #
# all copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING #
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER #
# DEALINGS IN THE SOFTWARE. #
# #
###############################################################################
"""
This module provides methods to decompose a molecule into
substructures/motifs. The presence of these motifs in the molecules in
the reference data can be used to weight the BAC fitting and thus fit
to maximally diversified data.
"""
from collections import Counter
from typing import List
from rdkit import Chem
def get_substructs(smi: str) -> Counter:
"""
Convert SMILES to counts of substructures.
Args:
smi: SMILES.
Returns:
Counter containing number of times each substructure SMILES occurs.
"""
mol = _smi_to_mol(smi)
cliques = substruct_decomp(mol)
return Counter(_get_substruct(mol, c) for c in cliques)
def substruct_decomp(mol: Chem.Mol) -> List[List[int]]:
"""
Decompose molecule into substructures. The substructures are
defined as atoms, bonds, two-bond and three-bond substructures that
are not in rings, rings, and bridged ring systems that share at
least three atoms.
Note:
Adapted from junction tree decomposition described in
https://arxiv.org/abs/1802.04364.
Args:
mol: RDKit molecule.
Returns:
List of indices contained in each substructure.
"""
n_atoms = mol.GetNumAtoms()
if n_atoms == 1:
return [[0]]
cliques = []
for bond in mol.GetBonds():
a1 = bond.GetBeginAtom().GetIdx()
a2 = bond.GetEndAtom().GetIdx()
if not bond.IsInRing():
cliques.append([a1, a2])
ssr = [list(x) for x in Chem.GetSymmSSSR(mol)]
cliques.extend(ssr)
nei_list = [[] for _ in range(n_atoms)]
for i, c in enumerate(cliques):
for atom in c:
nei_list[atom].append(i)
# Merge Rings with intersection > 2 atoms
for i, c in enumerate(cliques):
if len(c) > 2:
for atom in c:
for j in nei_list[atom]:
if i >= j or len(cliques[j]) <= 2:
continue
inter = set(c) & set(cliques[j])
if len(inter) > 2:
cliques[i].extend(cliques[j])
cliques[i] = list(set(cliques[i]))
cliques[j] = []
cliques = [c for c in cliques if len(c) > 0]
nei_list = [[] for _ in range(n_atoms)]
for i, c in enumerate(cliques):
for atom in c:
nei_list[atom].append(i)
# Add singleton cliques
for atom in range(n_atoms):
cnei = nei_list[atom]
if len(cnei) > 1:
bonds = [c for c in cnei if len(cliques[c]) == 2]
if len(bonds) > 1 or (len(bonds) <= 1 and len(cnei) > 2):
cliques.append([atom])
return cliques
def _get_substruct(mol: Chem.Mol, atoms: List[int]) -> str:
"""Convert a list of atom indices to a substructure."""
if mol.GetNumAtoms() == 1:
smiles = _mol_to_smi(mol)
else:
# For single-atom cliques, we want the substructure to contain its neighbors
if len(atoms) == 1:
atoms = atoms[:]
atoms.extend([nei.GetIdx() for nei in mol.GetAtomWithIdx(atoms[0]).GetNeighbors()])
smiles = Chem.MolFragmentToSmiles(mol, atoms, kekuleSmiles=True)
return _mol_to_smi(_copy_mol(Chem.MolFromSmiles(smiles, sanitize=False)))
def _mol_to_smi(mol: Chem.Mol) -> str:
return Chem.MolToSmiles(mol, kekuleSmiles=True)
def _smi_to_mol(smi: str) -> Chem.Mol:
mol = Chem.MolFromSmiles(smi)
Chem.Kekulize(mol)
return mol
def _copy_mol(mol: Chem.Mol) -> Chem.Mol:
new_mol = Chem.RWMol(Chem.MolFromSmiles(''))
for atom in mol.GetAtoms():
new_atom = _copy_atom(atom)
new_mol.AddAtom(new_atom)
for bond in mol.GetBonds():
a1 = bond.GetBeginAtom().GetIdx()
a2 = bond.GetEndAtom().GetIdx()
bt = bond.GetBondType()
new_mol.AddBond(a1, a2, bt)
return new_mol.GetMol()
def _copy_atom(atom: Chem.Atom) -> Chem.Atom:
new_atom = Chem.Atom(atom.GetSymbol())
new_atom.SetFormalCharge(atom.GetFormalCharge())
new_atom.SetNumRadicalElectrons(atom.GetNumRadicalElectrons())
return new_atom