cgsmiles.drawing module¶
Utilities for drawing molecules and cgsmiles graphs.
- cgsmiles.drawing.draw_molecule(graph, ax=None, layout_method='vespr_refined', pos=None, cg_mapping=True, colors=None, labels=None, scale=2, outline=False, use_weights=False, align_with='diag', fontsize=10, text_color='black', edge_widths=3, mapped_edge_width=20, default_bond=1, layout_kwargs={})[source]¶
Draw the graph of a molecule optionally with a coarse-grained projection if cg_mapping is set to True. The membership of atoms to the CG projection is taken from the ‘fragid’ node attribute.
Positions or one of three layout methods must be specified. The layout options are vespr_refined, vespr, and circular. Note that the vespr_refined is slow but yields the best quality results. For a quick look vespr is recommended. The drawing function also accepts a layout_kwarg dictionary specifiying options to be given to the chosen layout method. See also cgsmiles.graph_layout.
For example to draw Benzene using the Martini 3 mapping:
>>> import matplotlib.pyplot as plt >>> from cgsmiles import MoleculeResolver >>> from cgsmiles import draw_molecule
>>> cgsmiles_str = "{[#TC5]1[#TC5][#TC5]1}.{#TC5=[$]cc[$]}" >>> resolver = MoleculeResolver.from_string(cgsmiles_str) >>> cg_mol, aa_mol = resolver.resolve() >>> draw_molecule(aa_mol, layout_method="vespr_refined") >>> plt.show()
The drawing is always of fixed size based on the canvas size. This means that molecules drawn on a canvas with the same size will have the same dimensions (i.e. bond length, atom size). Using the scale argument a drawing can be scaled to make it larger or smaller on a given canvas. That means if your molecule is too small or large redraw it setting a different scale parameter.
- Parameters:
ax (matplotlib.axes.Axes) – mpl axis object
layout_method (str) – choice of vespr, vespr_refined, circular (default: ‘vespr_refined’)
pos (dict) – a dict mapping nodes to 2D positions
cg_mapping (bool) – draw outline for the CG mapping (default: True)
colors (dict) – a dict mapping nodes to colors or fragids to colors depending on if cg_mapping is True
labels (list) – list of node_labels; must follow the order of nodes in graph
scale (float) – scale the drawing relative to the total canvas size
outline (bool) – draw an outline around each node
use_weights (bool) – color nodes according to weight attribute (default: False)
align_with (str or
numpy.ndarray) – align the longest distance in molecule with one of x, y, diag or a custom axis as numpy 2D arrayfontsize (float) – fontsize of labels
text_color (str) – color of the node labels (default: black)
edge_widths (float) – the width of the bonds
mapped_edge_widths (float) – the width of the mapped projection
default_bond (float) – default bond length (default: 1)
layout_kwargs (dict) – dict with arguments passed to the layout methods
- Returns:
matplotlib.axes.Axes – the updated axis object
dict – a dict of positions