Skip to content

Commit

Permalink
Add smiles string conversion using pybel backend (#1056)
Browse files Browse the repository at this point in the history
* add smiles string conversion using pybel backend

* add unit test for several smiles string conversion scenarios

* restrict python version, attempt to fix bleeding test
  • Loading branch information
daico007 committed Oct 25, 2022
1 parent fd3fe39 commit ba6fead
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 3 deletions.
2 changes: 1 addition & 1 deletion environment-dev-win.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ dependencies:
- pycifrw
- pytest
- pytest-cov
- python
- python<3.10
- rdkit>=2021
- scipy
2 changes: 1 addition & 1 deletion environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ dependencies:
- pytest
- pytest-azurepipelines
- pytest-cov
- python
- python<3.10
- rdkit>=2021
- scipy
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ dependencies:
- packmol>=18
- gmso>=0.9.0
- parmed>=3.4.3
- python
- python<3.10
- rdkit>=2021
- scipy
16 changes: 16 additions & 0 deletions mbuild/compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -2728,6 +2728,22 @@ def to_pybel(
show_ports=show_ports,
)

def to_smiles(self, backend="pybel"):
"""Create a SMILES string from an mbuild compound.
Parameters
----------
compound : mb.Compound.
The mbuild compound to be converted.
backend : str, optional, default="pybel"
Backend used to do the conversion.
Return
------
smiles_string : str
"""
return conversion.to_smiles(self, backend)

def from_pybel(
self,
pybel_mol,
Expand Down
29 changes: 29 additions & 0 deletions mbuild/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,35 @@ def to_pybel(
return pybelmol


def to_smiles(compound, backend="pybel"):
"""Create a SMILES string from an mbuild compound.
Parameters
----------
compound : mb.Compound.
The mbuild compound to be converted.
backend : str, optional, default="pybel"
Backend used to do the conversion.
Return
------
smiles_string : str
"""
if backend == "pybel":
mol = to_pybel(compound)

warn(
"The bond orders will be guessed using pybel"
"OBMol.PerceviedBondOrders()"
)
mol.OBMol.PerceiveBondOrders()
smiles_string = mol.write("smi").replace("\t", " ").split(" ")[0]

return smiles_string
else:
raise NotImplementedError(f"Backend {backend} not implemented.")


def to_networkx(compound, names_only=False):
"""Create a NetworkX graph representing the hierarchy of a Compound.
Expand Down
16 changes: 16 additions & 0 deletions mbuild/tests/test_compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,22 @@ def test_reload(self):
assert p3ht1.n_particles == 33
assert p3ht1.n_bonds == 33

@pytest.mark.skipif(
not has_openbabel, reason="Open Bable package not installed"
)
def test_to_smiles(self, ethane, benzene):
# Test predefined molecule
eth_smiles = "CC"
benzene_smiles = "c1ccccc1"

assert ethane.to_smiles() == eth_smiles
assert benzene.to_smiles() == benzene_smiles

# Test molecules loaded in with smiles string
for smiles in ["CCO", "CC(O)O", "Cc1ccccc1"]:
compound = mb.load(smiles, smiles=True)
assert compound.to_smiles() == smiles

@pytest.mark.parametrize(
"extension", [(".xyz"), (".pdb"), (".mol2"), (".gro")]
)
Expand Down

0 comments on commit ba6fead

Please sign in to comment.