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

Use utility functions in jsonutils to fix #151 #257

Merged
merged 3 commits into from
Dec 4, 2024
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
12 changes: 6 additions & 6 deletions meeko/molsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1377,7 +1377,7 @@ def from_json(obj):
obj["ring_closure_info"]["bonds_removed"],
obj["ring_closure_info"]["pseudos_by_atom"],
)
molsetup.rotamers = obj["rotamers"]
molsetup.rotamers = [{string_to_tuple(k, element_type=int): v for k,v in rotamer.items()} for rotamer in obj["rotamers"]]
molsetup.atom_params = obj["atom_params"]
molsetup.restraints = [Restraint.from_json(x) for x in obj["restraints"]]
molsetup.flexibility_model = obj["flexibility_model"]
Expand Down Expand Up @@ -2147,8 +2147,8 @@ def from_json(obj):
]
# TODO: Dihedral decoding may need another look
rdkit_molsetup.dihedral_interactions = obj["dihedral_interactions"]
rdkit_molsetup.dihedral_partaking_atoms = obj["dihedral_partaking_atoms"]
rdkit_molsetup.dihedral_labels = obj["dihedral_labels"]
rdkit_molsetup.dihedral_partaking_atoms = {string_to_tuple(k, element_type=int): v for k,v in obj["dihedral_partaking_atoms"].items()}
rdkit_molsetup.dihedral_labels = {string_to_tuple(k, element_type=int): v for k,v in obj["dihedral_labels"].items()}
rdkit_molsetup.atom_to_ring_id = {
int(k): [string_to_tuple(t) for t in v]
for k, v in obj["atom_to_ring_id"].items()
Expand Down Expand Up @@ -2332,7 +2332,7 @@ def default(self, obj):
for k, v in obj.rings.items()
},
"ring_closure_info": obj.ring_closure_info.__dict__,
"rotamers": obj.rotamers,
"rotamers": [{tuple_to_string(k): v for k, v in rotamer.items()} for rotamer in obj.rotamers],
"atom_params": obj.atom_params,
"restraints": [
self.restraint_encoder.default(x) for x in obj.restraints
Expand All @@ -2358,8 +2358,8 @@ def default(self, obj):
output_dict["mol"] = rdMolInterchange.MolToJSON(obj.mol)
output_dict["modified_atom_positions"] = obj.modified_atom_positions
output_dict["dihedral_interactions"] = obj.dihedral_interactions
output_dict["dihedral_partaking_atoms"] = obj.dihedral_partaking_atoms
output_dict["dihedral_labels"] = obj.dihedral_labels
output_dict["dihedral_partaking_atoms"] = {tuple_to_string(k): v for k,v in obj.dihedral_partaking_atoms.items()}
output_dict["dihedral_labels"] = {tuple_to_string(k): v for k,v in obj.dihedral_labels.items()}
output_dict["atom_to_ring_id"] = obj.atom_to_ring_id
output_dict["ring_corners"] = obj.ring_corners
output_dict["rmsd_symmetry_indices"] = obj.rmsd_symmetry_indices
Expand Down
24 changes: 24 additions & 0 deletions test/json_serialization_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@

from meeko.utils.pdbutils import PDBAtomInfo

try:
import openforcefields
_got_openff = True
except ImportError as err:
_got_openff = False

# from ..meeko.utils.pdbutils import PDBAtomInfo

pkgdir = pathlib.Path(meeko.__file__).parents[1]
Expand Down Expand Up @@ -413,6 +419,11 @@ def check_molsetup_equality(decoded_obj: MoleculeSetup, starting_obj: MoleculeSe
decoded_obj.restraints[idx], starting_obj.restraints[idx]
)

# dihedrals
assert decoded_obj.dihedral_partaking_atoms == starting_obj.dihedral_partaking_atoms
assert decoded_obj.dihedral_interactions == starting_obj.dihedral_interactions
assert decoded_obj.dihedral_labels == starting_obj.dihedral_labels

# Checking flexibility model
for key in starting_obj.flexibility_model:
assert key in decoded_obj.flexibility_model
Expand Down Expand Up @@ -763,5 +774,18 @@ def check_polymer_equality(
assert decoded_obj.log == starting_obj.log
return

@pytest.mark.skipif(not _got_openff, reason="requires openff-forcefields")
def test_dihedral_equality():
mk_prep = MoleculePreparation(
merge_these_atom_types=(),
dihedral_model="openff",
)
fn = str(pkgdir/"test"/"flexibility_data"/"non_sequential_atom_ordering_01.mol")
mol = Chem.MolFromMolFile(fn, removeHs=False)
starting_molsetup = mk_prep(mol)[0]
json_str = json.dumps(starting_molsetup, cls=MoleculeSetupEncoder)
decoded_molsetup = json.loads(json_str, object_hook=RDKitMoleculeSetup.from_json)
check_molsetup_equality(starting_molsetup, decoded_molsetup)
return

# endregion
Loading