Constructed Molecule JSONizer
- class ConstructedMoleculeJsonizer(key_makers=(InchiKey(),))[source]
Bases:
object
Abstract base class for creating JSONs of constructed molecules.
See also
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 Constructed Molecule to JSON
You want get a JSON representation of a
ConstructedMolecule
import stk # Make the molecule you want jsonize. polymer = stk.ConstructedMolecule( topology_graph=stk.polymer.Linear( building_blocks=( stk.BuildingBlock('BrCCBr', [stk.BromoFactory()]), ), repeating_unit='A', num_repeating_units=3, ) ) # Make a JSONizer. jsonizer = stk.ConstructedMoleculeJsonizer() # Get the JSON. json = jsonizer.to_json(polymer)
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 initializerimport stk # Make the molecule you want jsonize. polymer = stk.ConstructedMolecule( topology_graph=stk.polymer.Linear( building_blocks=( stk.BuildingBlock('BrCCBr', [stk.BromoFactory()]), ), repeating_unit='A', num_repeating_units=3, ) ) # Make a JSONizer. jsonizer = stk.ConstructedMoleculeJsonizer() # Get the JSON. json = jsonizer.to_json(polymer)
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 byMoleculeKeyMaker.get_key_name()
and the value given byMoleculeKeyMaker.get_key()
.Methods
to_json
(molecule)Serialize molecule.
- __init__(key_makers=(InchiKey(),))[source]
Initializes a
ConstructedMoleculeJsonizer
.- Parameters
key_makers (
tuple
ofMoleculeKeyMaker
) – 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]
Serialize molecule.
- Parameters
molecule (
ConstructedMolecule
) – The constructed molecule to serialize.- Returns
A JSON representation of molecule.
- Return type