Skip to content

Commit

Permalink
Rdmol iterators (#103)
Browse files Browse the repository at this point in the history
* allow rdkit mols in interators

* pytables 3.9 not compatible with python 3.8

* add simple tests

* fix issue when using generated ids

* version
  • Loading branch information
eloyfelix authored Oct 6, 2023
1 parent 814bbb5 commit 10f0399
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion FPSim2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
except Exception as e:
pass

__version__ = "0.4.5"
__version__ = "0.5.0"
23 changes: 11 additions & 12 deletions FPSim2/io/chem.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,28 +94,27 @@ def process_fp(fp, mol_id):
return mol_id, *fp, popcnt


def load_molecule(mol_string: str) -> Chem.Mol:
def load_molecule(molecule: Any) -> Chem.Mol:
"""Reads SMILES, molblock or InChI and returns a RDKit mol.
Parameters
----------
mol_string : str
SMILES, molblock or InChI.
molecule : Any
Chem.Mol, SMILES, molblock or InChI.
Returns
-------
mol: ROMol
RDKit molecule.
"""
if re.search(MOLFILE_RE, mol_string, flags=re.MULTILINE):
rdmol = Chem.MolFromMolBlock(mol_string)
elif mol_string.startswith("InChI="):
try:
rdmol = Chem.MolFromInchi(mol_string)
except:
rdmol = None
if isinstance(molecule, Chem.Mol):
return molecule
if re.search(MOLFILE_RE, molecule, flags=re.MULTILINE):
rdmol = Chem.MolFromMolBlock(molecule)
elif molecule.startswith("InChI="):
rdmol = Chem.MolFromInchi(molecule)
else:
rdmol = Chem.MolFromSmiles(mol_string)
rdmol = Chem.MolFromSmiles(molecule)
return rdmol


Expand Down Expand Up @@ -201,7 +200,7 @@ def it_mol_supplier(
mol_id = new_mol_id
else:
if gen_ids:
mol_string = mol[0]
mol_string = mol
mol_id = new_mol_id
else:
try:
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Eloy Félix Manzanares
Copyright (c) 2019-2023 Eloy Félix Manzanares

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ def build_extensions(self):
long_description_content_type="text/markdown",
ext_modules=ext_modules,
install_requires=[
"rdkit>=2022.3.3",
"tables>=3.4.4",
"rdkit>=2023.03.2",
"tables>=3.4.4,<=3.8.0",
"numpy >=1.14",
"sqlalchemy>=1.4.47",
"scipy",
Expand Down
7 changes: 5 additions & 2 deletions tests/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,11 @@ def test_similarity(n_workers):
in_file = os.path.join(TESTS_DIR, "data/test.h5")
fpe = FPSim2Engine(in_file, storage_backend="pytables")
results = fpe.similarity(query_smi, 0.7, n_workers=n_workers)
assert results.shape[0] == 4
assert list(results[0]) == [1, 1.0]
results_romol = fpe.similarity(
Chem.MolFromSmiles(query_smi), 0.7, n_workers=n_workers
)
assert results.shape[0] == results_romol.shape[0] == 4
assert list(results[0]) == list(results_romol[0]) == [1, 1.0]


@pytest.mark.parametrize("n_workers", (1, 2, 4))
Expand Down
13 changes: 12 additions & 1 deletion tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,18 @@ def test_suppliers():
it_mols = [
Chem.MolToSmiles(x[1]) for x in it_mol_supplier(smiles_list, gen_ids=True)
]
assert smi_mols == sdf_mols == sdfgz_mols == it_mols
mols = [Chem.MolFromSmiles(x) for x in smiles_list]
rd_it_mols = [Chem.MolToSmiles(x[1]) for x in it_mol_supplier(mols, gen_ids=True)]

assert smi_mols == sdf_mols == sdfgz_mols == it_mols == rd_it_mols
assert (
len(smi_mols)
== len(sdf_mols)
== len(sdfgz_mols)
== len(it_mols)
== len(rd_it_mols)
== len(smiles_list)
)


def test_get_mol_supplier():
Expand Down

0 comments on commit 10f0399

Please sign in to comment.