Skip to content

Commit ecb1262

Browse files
committed
define set_rdkit_ring_dihedrals and make test
1 parent fb16f4a commit ecb1262

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

arc/species/converter.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,6 +1852,49 @@ def set_rdkit_dihedrals(conf, rd_mol, torsion, deg_increment=None, deg_abs=None)
18521852
return new_xyz
18531853

18541854

1855+
def set_rdkit_ring_dihedrals(rd_mol, ring_head, ring_tail, torsions, dihedrals):
1856+
"""
1857+
A helper function for setting dihedral angles in a ring using RDKit.
1858+
1859+
Args:
1860+
rd_mol: The respective RDKit molecule.
1861+
ring_head: The first atom index of the ring(0-indexed).
1862+
ring_tail: The last atom index of the ring(0-indexed).
1863+
torsions: A list of torsions, each corresponding to a dihedral.
1864+
dihedrals: A list of dihedral angles in degrees, each corresponding to a torsion.
1865+
1866+
Example of a 6-membered ring:
1867+
ring_head = 0
1868+
ring_tail = 5
1869+
torsions = [(0, 1, 2, 3), (1, 2, 3, 4), (2, 3, 4, 5)]
1870+
dihedrals = [30, 300, 30]
1871+
1872+
Returns:
1873+
dict: The xyz with the new dihedral, ordered according to the map.
1874+
"""
1875+
1876+
rd_mol_mod = Chem.RWMol(rd_mol)
1877+
rd_mol_mod.RemoveBond(ring_head, ring_tail)
1878+
Chem.SanitizeMol(rd_mol_mod)
1879+
conf_mod = rd_mol_mod.GetConformer()
1880+
for torsion, dihedral in zip(torsions, dihedrals):
1881+
torsion_0_indexed = [tor - 0 for tor in torsion]
1882+
set_rdkit_dihedrals(conf_mod, rd_mol_mod, torsion_0_indexed, deg_abs=dihedral)
1883+
1884+
rd_mol_mod.AddBond(ring_head, ring_tail, Chem.BondType.SINGLE)
1885+
Chem.SanitizeMol(rd_mol_mod)
1886+
Chem.AllChem.MMFFOptimizeMolecule(rd_mol_mod)
1887+
conf_mod = rd_mol_mod.GetConformer()
1888+
1889+
coords = list()
1890+
symbols = list()
1891+
for i, atom in enumerate(list(rd_mol_mod.GetAtoms())):
1892+
coords.append([conf_mod.GetAtomPosition(i).x, conf_mod.GetAtomPosition(i).y, conf_mod.GetAtomPosition(i).z])
1893+
symbols.append(atom.GetSymbol())
1894+
new_xyz = xyz_from_data(coords=coords, symbols=symbols)
1895+
return new_xyz
1896+
1897+
18551898
def check_isomorphism(mol1, mol2, filter_structures=True, convert_to_single_bonds=False):
18561899
"""
18571900
Convert ``mol1`` and ``mol2`` to RMG Species objects, and generate resonance structures.

arc/species/converter_test.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4066,6 +4066,43 @@ def test_set_rdkit_dihedrals(self):
40664066
H 2.16336803 0.09985803 0.03295192"""
40674067
self.assertEqual(converter.xyz_to_str(new_xyz4), expected_xyz4)
40684068

4069+
def test_set_rdkit_ring_dihedrals(self):
4070+
"""Test setting the dihedral angles of an RDKit ring molecule"""
4071+
xyz_original = """C 1.17528959 0.88689342 -0.09425887
4072+
C -0.23165323 1.40815606 -0.37444021
4073+
C -1.28915380 0.60789983 0.38119602
4074+
C -1.17528947 -0.88689346 0.09425817
4075+
C 0.23165279 -1.40815571 0.37444068
4076+
C 1.28915350 -0.60789979 -0.38119592
4077+
H 1.90063595 1.43610053 -0.70501194
4078+
H 1.43190556 1.07695419 0.95523181
4079+
H -0.29672067 2.46483469 -0.09164586
4080+
H -0.43309289 1.35229514 -1.45133707
4081+
H -2.28822258 0.96189799 0.10312701
4082+
H -1.17664390 0.78164432 1.45848873
4083+
H -1.43190253 -1.07695588 -0.95523291
4084+
H -1.90063264 -1.43610606 0.70501042
4085+
H 0.29671416 -2.46483479 0.09164748
4086+
H 0.43309139 -1.35229454 1.45133785
4087+
H 1.17664469 -0.78164459 -1.45848883
4088+
H 2.28822409 -0.96189136 -0.10312655"""
4089+
xyz_original = converter.str_to_xyz(xyz_original)
4090+
4091+
ring_head = 0
4092+
ring_tail = 5
4093+
torsions = [(0, 1, 2, 3), (1, 2, 3, 4), (2, 3, 4, 5)]
4094+
dihedrals = [29.167577928701704, 299.8936870462789, 29.167577208303104]
4095+
4096+
s_mol, b_mol = converter.molecules_from_xyz(xyz_original)
4097+
mol = b_mol if b_mol is not None else s_mol
4098+
_, rd_mol = converter.rdkit_conf_from_mol(mol, xyz_original)
4099+
4100+
xyz_final = converter.set_rdkit_ring_dihedrals(rd_mol, ring_head, ring_tail, torsions, dihedrals)
4101+
4102+
self.assertAlmostEqual(calculate_dihedral_angle(xyz_final,[0,1,2,3]), 29.167577928701704, 2)
4103+
self.assertAlmostEqual(calculate_dihedral_angle(xyz_final,[1,2,3,4]), 299.8936870462789, 2)
4104+
self.assertAlmostEqual(calculate_dihedral_angle(xyz_final,[2,3,4,5]), 29.167577208303104, 2)
4105+
40694106
def test_get_center_of_mass(self):
40704107
"""Test calculating the center of mass for coordinates"""
40714108
xyz = """O 1.28706525 0.52121353 0.04219198

0 commit comments

Comments
 (0)