Getting Started¶
This is a short introduction to help you get started with the Chemaxon Python API.
API Documentation
For thorough documentation, go to the API Reference.
Molecule import¶
The Chemaxon Python API can import and export molecules of supported formats.
You can either specify the format yourself, or if you do not specify any, the function will recognize it automatically.
from chemaxon.io import import_mol
mol = import_mol("CC(=O)OC1=CC=CC=C1C(O)=O")
mol2 = import_mol("CC(=O)OC1=CC=CC=C1C(O)=O", "smiles")
The import_mol function returns a chemaxon.Molecule object on success.
Import of molecules from a single file is possible via iterators.
from chemaxon.io import open_for_import
with open_for_import("my_molecules.sdf") as mol_iterator:
for mol in mol_iterator:
process(mol) # do something with the molecule object
Molecule export¶
You can also export molecules into various formats. It is required to specify the format in this case.
from chemaxon.io import export_mol
mol_str = export_mol("CC(=O)OC1=CC=CC=C1C(O)=O", "smiles")
mol2_str = export_mol("CC(=O)OC1=CC=CC=C1C(O)=O", "mrv")
The export_mol function returns a str object on success, which contains the molecule in the specified format.
Exporting to a single file is also feasible.
from chemaxon.io import open_for_export, import_mol
molecule_list = ... # assume we have a list containing molecules
with open_for_export("file.sdf", "sdf") as out:
write_res = all(out.write(mol) for mol in molecule_list)
Calculations¶
Once you have a molecule, you can calculate various properties of it.
from chemaxon.io import import_mol
from chemaxon.calculations import logp, logd, pka, hlb
mol = import_mol('CC(=O)NC1=CC=C(O)C=C1') # paracetamol
print('logP: ', logp(mol))
print('logD[pH 9.0]:', logd(mol, ph=9.0))
print('pKa: ', pka(mol))
print('hlb: ', hlb(mol))
Further information about Calculator plugins: https://docs.chemaxon.com/display/docs/calculators_index.html
Exception handling¶
from chemaxon import Molecule
from chemaxon.calculations import LogPMethod, logp
from chemaxon.io import import_mol
try:
# for illustrating exception raising, a non-importable molecule is used
mol = import_mol("non-importable", None)
logp(mol, LogPMethod.CHEMAXON, -12, -34, consider_tautomerization=True, ph=15)
except RuntimeError as e:
handle(e) # custom error handler code
Further examples¶
There are several examples listed by topics in the Python API examples project.