[docs]classBonds(typing.TypedDict):"""JSON representation of bonds."""atom1:list[int]"""The indices of the first atom of each bond."""atom2:list[int]"""The indices of the second atom of each bond."""order:list[float]"""The bond order of each bond."""
[docs]classAromaticBonds(typing.TypedDict):"""JSON representation of aromatic bonds."""atom1:list[int]"""The indices of the first atom of each bond."""atom2:list[int]"""The indices of the second atom of each bond."""
[docs]classMolecule(typing.TypedDict):"""A JSON molecule."""atomic_numbers:list[int]"""Atomic numbers of atoms in the molecule."""atom_charges:typing.NotRequired[list[int]]"""Charges of atoms in the molecule."""bonds:typing.NotRequired[Bonds]"""Bonds of the molecule."""dative_bonds:typing.NotRequired[Bonds]"""Dative bonds of the molecule."""aromatic_bonds:typing.NotRequired[AromaticBonds]"""Aromatic bonds of the molecule."""conformers:typing.NotRequired[list[Conformer]]"""Conformers of the molecule."""
[docs]defjson_from_rdkit(molecule:rdkit.Mol)->Molecule:# noqa: C901"""Create a JSON representation of an :mod:`rdkit` molecule. Parameters: molecule: The molecule to convert to JSON. Returns: A JSON molecule. """atomic_numbers=[]atom_charges=[]save_charges=Falseforatominmolecule.GetAtoms():atomic_numbers.append(atom.GetAtomicNum())atom_charges.append(atom.GetFormalCharge())ifatom.GetFormalCharge()!=0:save_charges=Truebonds:Bonds={"atom1":[],"atom2":[],"order":[],}dative_bonds:Bonds={"atom1":[],"atom2":[],"order":[],}aromatic_bonds:AromaticBonds={"atom1":[],"atom2":[],}forbondinmolecule.GetBonds():matchbond.GetBondType():case(rdkit.BondType.SINGLE|rdkit.BondType.DOUBLE|rdkit.BondType.TRIPLE|rdkit.BondType.QUADRUPLE|rdkit.BondType.QUINTUPLE|rdkit.BondType.HEXTUPLE):bonds["atom1"].append(bond.GetBeginAtomIdx())bonds["atom2"].append(bond.GetEndAtomIdx())bonds["order"].append(bond.GetBondTypeAsDouble())caserdkit.BondType.DATIVE:dative_bonds["atom1"].append(bond.GetBeginAtomIdx())dative_bonds["atom2"].append(bond.GetEndAtomIdx())dative_bonds["order"].append(1.0)caserdkit.BondType.AROMATIC:aromatic_bonds["atom1"].append(bond.GetBeginAtomIdx())aromatic_bonds["atom2"].append(bond.GetEndAtomIdx())case_:msg=f"unsupported bond type: {bond.GetBondType()}"raiseRuntimeError(msg)d:Molecule={"atomic_numbers":atomic_numbers,}ifsave_charges:d["atom_charges"]=atom_chargesifbonds["atom1"]:d["bonds"]=bondsifdative_bonds["atom1"]:d["dative_bonds"]=dative_bondsifaromatic_bonds["atom1"]:d["aromatic_bonds"]=aromatic_bondsifmolecule.GetNumConformers()>0:d["conformers"]=[conformer.GetPositions().tolist()forconformerinmolecule.GetConformers()]returnd
[docs]defjson_to_rdkit(molecule:Molecule)->rdkit.Mol:# noqa: C901"""Create an :mod:`rdkit` molecule from a JSON representation. Parameters: molecule: The JSON molecule. Returns: The :mod:`rdkit` molecule. """mol=rdkit.EditableMol(rdkit.Mol())foratomic_numberinmolecule["atomic_numbers"]:rdkit_atom=rdkit.Atom(atomic_number)mol.AddAtom(rdkit_atom)if"bonds"inmolecule:foratom1,atom2,orderinzip(molecule["bonds"]["atom1"],molecule["bonds"]["atom2"],molecule["bonds"]["order"],strict=True,):mol.AddBond(atom1,atom2,rdkit.BondType(order))if"dative_bonds"inmolecule:foratom1,atom2inzip(molecule["dative_bonds"]["atom1"],molecule["dative_bonds"]["atom2"],strict=True,):mol.AddBond(atom1,atom2,rdkit.BondType.DATIVE)if"aromatic_bonds"inmolecule:foratom1,atom2inzip(molecule["aromatic_bonds"]["atom1"],molecule["aromatic_bonds"]["atom2"],strict=True,):mol.AddBond(atom1,atom2,rdkit.BondType.AROMATIC)mol=mol.GetMol()foratom,chargeinzip(mol.GetAtoms(),molecule.get("atom_charges",[]),strict=False,):atom.SetFormalCharge(charge)if"conformers"inmolecule:num_atoms=len(molecule["atomic_numbers"])forconformerinmolecule["conformers"]:rdkit_conf=rdkit.Conformer(num_atoms)foratom_id,atom_coordinenumerate(conformer):rdkit_conf.SetAtomPosition(atom_id,atom_coord)mol.AddConformer(rdkit_conf)returnmol