Molecule JSONizer

class MoleculeJsonizer(key_makers=(InchiKey(),))[source]

Bases: object

Abstract base class for creating JSONs of molecules.

Notes

You might notice that the public methods of this abstract base class are implemented. These are just default implementations, which can be safely ignored or overridden, when implementing subclasses. However, the default implementation can be used directly, if it suits your needs.

Examples

Converting a Molecule to JSON

You want to create a JSON representation of a molecule

import stk

jsonizer = stk.MoleculeJsonizer()
json = jsonizer.to_json(stk.BuildingBlock('NCCN'))

Adding Additional Molecular Keys

Apart from atoms, bonds and the position matrix, the JSON representation holds additional fields, one for each MoleculeKeyMaker provided to the initializer

import stk

jsonizer = stk.MoleculeJsonizer(
    key_makers=(
        stk.Inchi(),
        stk.InchiKey(),
    ),
)
json = jsonizer.to_json(stk.BuildingBlock('NCCN'))

In this case, json will look something like

{
    # A tuple of JSON atom representations.
    'atoms': (...),

    # A tuple of JSON bond representations.
    'bonds': (...),

    'InChI': 'The InChI of the molecule',
    'InChIKey': 'The InChIKey of the molecule',
}

For every MoleculeKeyMaker provided to key_makers, a new key will be added to the JSON representation, with its name given by MoleculeKeyMaker.get_key_name() and the value given by MoleculeKeyMaker.get_key().

Methods

to_json(molecule)

Return a JSON representation of molecule.

__init__(key_makers=(InchiKey(),))[source]

Initialize a MoleculeJsonizer instance.

Parameters

key_makers (Union[MoleculeKeyMaker, Iterable[MoleculeKeyMaker]]) – Used to make the keys of molecules, which should be included in their JSON representations. Keys allow molecular data to reference itself when split across multiple JSONs.

to_json(molecule)[source]

Return a JSON representation of molecule.

Parameters

molecule (Molecule) – The molecule to serialize.

Return type

_MoleculeJson

Returns

A JSON representation of molecule.