xenopict package

Subpackages

Submodules

Module contents

XenoPict: A library for molecular visualization with a focus on 2D depiction.

class xenopict.Xenopict(input_mol, **kwargs)[source]

Bases: object

This class draws an RDK molecule with sensible defaults, cleaning up the output SVG for easier modification and reduced size.

>>> from rdkit import Chem
>>> import rdkit.Chem.rdPartialCharges
>>> diclofenac = mol = Chem.MolFromSmiles('O=C(O)Cc1ccccc1Nc1c(Cl)cccc1Cl')
>>> rdkit.Chem.rdPartialCharges.ComputeGasteigerCharges(mol)

Partial charge (scaled to be in [-1, 1])

>>> shading = np.array([a.GetDoubleProp("_GasteigerCharge")  for a in mol.GetAtoms()])
>>> shading = shading / abs(shading).max()

SVG of molecule shaded by partial charge,

>>> drawer = Xenopict(mol)
>>> drawer.shade(shading)
<xenopict.drawer.Xenopict ...>
>>> str(drawer)
'<...>'

Atoms can also be annotated with a circle,

>>> drawer.mark_atoms([1, 2])
<xenopict.drawer.Xenopict ...>

The underlying svg dom (xml.dom.minidom) is accessible:

>>> drawer.svgdom
<xml.dom.minidom.Document ...>
add_atom_indices: bool = False
add_bond_indices: bool = False
align_to(template)[source]

Align this molecule to a template molecule using MCS.

This method aligns the current molecule to match the orientation of the template molecule by finding their maximum common substructure. The alignment is done in-place, modifying the current molecule’s coordinates.

Parameters:

template (Union[str, Mol, Xenopict]) – Template molecule to align to. Can be: - SMILES string - RDKit Mol object - Another Xenopict object

Return type:

Xenopict

Returns:

self for method chaining

Examples

>>> from rdkit import Chem
>>> # Create ethanol and align propanol to it
>>> ethanol = Xenopict("CCO")
>>> propanol = Xenopict("CCCO")
>>> propanol.align_to(ethanol)  # Aligns by OH group
<xenopict.drawer.Xenopict ...>
cmap: Union[str, Colormap] = 'xenosite'
color_map(color)[source]
copy()[source]
Return type:

Xenopict

diverging_cmap: bool = False
down_scale: float = 0.7
draw_mol(mol=None)[source]
dummies_are_attachments: bool = False
embed_script: bool = False
filter(atoms, bonds)[source]

Filter the molecule to show only the specified atoms and bonds.

Return type:

Xenopict

classmethod from_smarts(smarts, **kwargs)[source]

Create a Xenopict object from a SMARTS pattern.

Parameters:
  • smarts (str) – SMARTS pattern string

  • **kwargs – Additional arguments passed to Xenopict constructor

Return type:

Xenopict

Returns:

Xenopict object initialized with the SMARTS pattern

get_cmap()[source]
Return type:

Colormap

halo()[source]
Return type:

Xenopict

mark_atoms(atoms)[source]
Return type:

Xenopict

mark_down_scale: float = 1.0
mark_substructure(atoms, substr_bonds=None)[source]
Return type:

Xenopict

optimize_svg: bool = True
plot_dot: PlotDot = <xenopict.plotdot.PlotDot object>
reframe(padding=1.5, atoms=None)[source]
Return type:

Xenopict

scale: float = 20
set_backbone_color(color)[source]

Set the color of the molecule’s backbone and atom labels.

Parameters:

color (str) – Hex color code (e.g. ‘#FF0000’ for red)

Return type:

Xenopict

Returns:

self for method chaining

shade(atom_shading=None, bond_shading=None)[source]
Return type:

Xenopict

shade_substructure(substrs_by_atoms, shading, substrs_bonds=None)[source]

shade_substructure shades a list of substruture, each one defined as a list of atom idxs.

By default, all the bonds connecting these structures are included. Optionally, a list of bonds can be provided instead.

Parameters:
  • substrs_by_atoms (Sequence[Sequence[AtomIdx]]) – A list of substructures, each one defined as a list of atoms.

  • shading (Sequence[float]) – A list of shading intensities, one for each substructure.

  • substrs_bonds (Optional[Sequence[Sequence[AtomIdx]]], optional) – Optionally specify the bonds

  • None. (to include for each substructure. Defaults to)

Returns:

Modifies object in place, but returns copy of self to enable chaining.

Return type:

Xenopict

shapely_resolution: int = 6
substructure_focus(atoms, substr_bonds=None)[source]

Focus on a substructure by dimming the rest of the molecule.

Return type:

Xenopict

to_html(svg_datauri=False)[source]

Return the HTML string depicting the molecule, embedding the SVG element within a white-background styled div. Optionally, the SVG can be placed into an img tag’s datauri isntead.

Return type:

str

to_svg(uniquify_internal_refs=True, hash_length=10, svg_attributes={})[source]

Convert Xenopict into an svg. By default, this function will uniquify all id/hrefs int the svg with the same md5 hash. This prevents id clashes in any documents into which svgs are embedded.

xenopict.parse(spec)[source]

Parse a specification into a list of Xenopict objects.

This function serves as the main entry point for the declarative API. It accepts specifications in multiple formats and returns a list of Xenopict objects.

Parameters:

spec (Union[Dict[str, Any], XenopictSpec, str, Path]) – The specification for creating molecules. Can be one of: - A dict matching the XenopictSpec schema - A XenopictSpec instance - A JSON string matching the XenopictSpec schema - A Path to a JSON file containing a XenopictSpec

Return type:

List[Xenopict]

Returns:

List of Xenopict objects, one for each molecule in the specification

Raises:

ValueError – If the input is invalid or required fields are missing

Examples

>>> # From a dict
>>> spec = {
...     "molecules": {
...         "smiles": "CCO"
...     }
... }
>>> xenopicts = parse(spec)
>>> len(xenopicts)
1
>>> # From a JSON string with marking
>>> json_str = '''
... {
...     "molecules": [
...         {
...             "smiles": "CCO",
...             "mark": {
...                 "atoms": [0, 1]
...             }
...         },
...         {
...             "smiles": "CCCO",
...             "mark": {
...                 "substructure_atoms": [0, 1, 2]
...             }
...         }
...     ]
... }
... '''
>>> xenopicts = parse(json_str)
>>> len(xenopicts)
2
>>> # From a file path
>>> from pathlib import Path
>>> path = Path("molecules.json")  
>>> xenopicts = parse(path)