Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add smiles string conversion using pybel backend #1056

Merged
merged 4 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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