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

mol2morgan_fingerprint: remove deprecated fp function #83

Merged
merged 8 commits into from
Sep 23, 2024
13 changes: 6 additions & 7 deletions molpipeline/mol2any/mol2morgan_fingerprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,11 @@ def _explain_rdmol(self, mol_obj: RDKitMol) -> dict[int, list[tuple[int, int]]]:
dict[int, list[tuple[int, int]]]
Dictionary with bit position as key and list of tuples with atom index and radius as value.
"""
bit_info: dict[int, list[tuple[int, int]]] = {}
_ = AllChem.GetMorganFingerprintAsBitVect(
mol_obj,
self.radius,
useFeatures=self._use_features,
bitInfo=bit_info,
nBits=self._n_bits,
fp_generator = self._get_fp_generator()
additional_output = AllChem.AdditionalOutput()
additional_output.AllocateBitInfoMap()
_ = fp_generator.GetSparseFingerprint(
JochenSiegWork marked this conversation as resolved.
Show resolved Hide resolved
mol_obj, additionalOutput=additional_output
)
bit_info = additional_output.GetBitInfoMap()
return bit_info
21 changes: 21 additions & 0 deletions tests/test_elements/test_mol2any/test_mol2morgan_fingerprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,27 @@ def test_setter_getter_error_handling(self) -> None:
}
self.assertRaises(ValueError, mol_fp.set_params, **params)

def test_bit2atom_mapping(self) -> None:
"""Test that the mapping from bits to atom weights works as intended."""
# lower n_bit values, e.g. 2048, will lead to a bit clash during folding,
JochenSiegWork marked this conversation as resolved.
Show resolved Hide resolved
# for the test smiles "NCCOCCCC(=O)O".
# We want no folding clashes in this test to check the correct length
# of the bit-to-atom mapping.
n_bits = 2100
sparse_morgan = MolToMorganFP(radius=2, n_bits=n_bits, return_as="sparse")
dense_morgan = MolToMorganFP(radius=2, n_bits=n_bits, return_as="dense")
explicit_bit_vect_morgan = MolToMorganFP(
radius=2, n_bits=n_bits, return_as="explicit_bit_vect"
)

smi2mol = SmilesToMol()
for test_smi in test_smiles:
JochenSiegWork marked this conversation as resolved.
Show resolved Hide resolved
for fp_gen in [sparse_morgan, dense_morgan, explicit_bit_vect_morgan]:
mol = smi2mol.transform([test_smi])[0]
fp = fp_gen.transform([mol])
mapping = fp_gen.bit2atom_mapping(mol)
self.assertEqual(np.sum(fp), len(mapping)) # type: ignore
JochenSiegWork marked this conversation as resolved.
Show resolved Hide resolved


if __name__ == "__main__":
unittest.main()
Loading