Skip to content

Commit ecf1ea8

Browse files
committed
Move surface species smiles workaround to proper function
The code to build a surface molecule from smiles was in the init function, but this fails to account for when people create a Molecule() and then add .from_smiles(). By moving this code to the from_smiles() function, it avoids code duplication and handles both cases.
1 parent a2c6428 commit ecf1ea8

1 file changed

Lines changed: 24 additions & 24 deletions

File tree

rmgpy/molecule/molecule.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,30 +1037,7 @@ def __init__(self, atoms=None, symmetry=-1, multiplicity=-187, reactive=True, pr
10371037
self.from_inchi(inchi)
10381038
self._inchi = inchi
10391039
elif smiles:
1040-
for surface_site_symbol in ['X', 'Pt']:
1041-
if surface_site_symbol in smiles:
1042-
assert 'Ar' not in smiles
1043-
self.from_smiles(smiles.replace(surface_site_symbol, 'Ar'))
1044-
lines = self.to_adjacency_list().split('\n')
1045-
for i, line in enumerate(lines):
1046-
if 'Ar' in line: # The adjacency list needs to use the identified 'X' for a site
1047-
lines[i] = lines[i].replace('Ar', surface_site_symbol)
1048-
# remove any extra electron pairs
1049-
m = re.search(r'p[0-9]+', lines[i]) # searches for p0, p1, p2, p3, etc
1050-
lines[i] = lines[i].replace(m[0], 'p0')
1051-
m = re.search(r'u[0-9]+', lines[i]) # searches for u0, u1, u2, u3, etc
1052-
lines[i] = lines[i].replace(m[0], 'u0')
1053-
1054-
# remove any extra charge
1055-
m = re.search(r'c[0-9+-]+', lines[i]) # searches for c0, c+2, c-1 etc
1056-
lines[i] = lines[i].replace(m[0], 'c0')
1057-
adj_list = '\n'.join(lines)
1058-
self = self.from_adjacency_list(adj_list)
1059-
# but now we have to change the symbol back to 'Pt or 'X' for the smiles
1060-
# self.smiles = self.smiles.replace('X', surface_site_symbol)
1061-
break
1062-
else:
1063-
self.from_smiles(smiles)
1040+
self.from_smiles(smiles)
10641041
self._smiles = smiles
10651042

10661043
if multiplicity != -187: # it was set explicitly, so re-set it (from_smiles etc may have changed it)
@@ -1888,6 +1865,29 @@ def from_smiles(self, smilesstr, backend='openbabel-first', raise_atomtype_excep
18881865
single backend or try different backends in sequence. The available options for the ``backend``
18891866
argument: 'openbabel-first'(default), 'rdkit-first', 'rdkit', or 'openbabel'.
18901867
"""
1868+
for surface_site_symbol in ['X', 'Pt']:
1869+
if surface_site_symbol in smilesstr:
1870+
assert 'Ar' not in smilesstr
1871+
self.from_smiles(smilesstr.replace(surface_site_symbol, 'Ar'))
1872+
lines = self.to_adjacency_list().split('\n')
1873+
for i, line in enumerate(lines):
1874+
if 'Ar' in line: # The adjacency list needs to use the identified 'X' for a site
1875+
lines[i] = lines[i].replace('Ar', surface_site_symbol)
1876+
# remove any extra electron pairs
1877+
m = re.search(r'p[0-9]+', lines[i]) # searches for p0, p1, p2, p3, etc
1878+
lines[i] = lines[i].replace(m[0], 'p0')
1879+
m = re.search(r'u[0-9]+', lines[i]) # searches for u0, u1, u2, u3, etc
1880+
lines[i] = lines[i].replace(m[0], 'u0')
1881+
1882+
# remove any extra charge
1883+
m = re.search(r'c[0-9+-]+', lines[i]) # searches for c0, c+2, c-1 etc
1884+
lines[i] = lines[i].replace(m[0], 'c0')
1885+
adj_list = '\n'.join(lines)
1886+
self = self.from_adjacency_list(adj_list)
1887+
# but now we have to change the symbol back to 'Pt or 'X' for the smiles
1888+
# self.smiles = self.smiles.replace('X', surface_site_symbol)
1889+
return self
1890+
18911891
translator.from_smiles(self, smilesstr, backend, raise_atomtype_exception=raise_atomtype_exception)
18921892
return self
18931893

0 commit comments

Comments
 (0)