Visualizing Routes
Example use
To visualize a path string, you can use the following snippet:
from directmultistep.utils.web_visualize import draw_tree_from_path_string
path = "{'smiles':'O=C(c1ccc(NS(=O)(=O)c2cccc3cccnc23)cc1)N1CCN(CC2CC2)CC1','children':[{'smiles':'O=C(O)c1ccc(NS(=O)(=O)c2cccc3cccnc23)cc1','children':[{'smiles':'CCOC(=O)c1ccc(NS(=O)(=O)c2cccc3cccnc23)cc1','children':[{'smiles':'CCOC(=O)c1ccc(N)cc1'},{'smiles':'O=S(=O)(Cl)c1cccc2cccnc12'}]}]},{'smiles':'C1CN(CC2CC2)CCN1'}]}"
svg_str = draw_tree_from_path_string(
path_string=path,
save_path=Path("data/figures/desired_file_name"),
width=400,
height=400,
x_margin=50,
y_margin=100,
theme="light",
)
Source Code
directmultistep.utils.web_visualize
ThemeType = Literal['light', 'dark']
module-attribute
FilteredDict
Bases: TypedDict
A dictionary format for multistep routes, used in DirectMultiStep models.
This dictionary is designed to represent a node in a synthetic route tree.
It contains the SMILES string of a molecule and a list of its child nodes.
To get its string format, use stringify_dict
.
Attributes:
Name | Type | Description |
---|---|---|
smiles |
str
|
SMILES string of the molecule. |
children |
list[FilteredDict]
|
List of child nodes, each a FilteredDict. |
Source code in src/directmultistep/utils/pre_process.py
ColorPalette
Bases: NamedTuple
Defines a color palette for drawing molecules.
Attributes:
Name | Type | Description |
---|---|---|
atom_colors |
dict[int, tuple[float, float, float]]
|
A dictionary mapping atomic numbers to RGB color tuples. |
annotation |
tuple[float, float, float, float]
|
An RGBA color tuple for annotations. |
border |
tuple[float, float, float]
|
An RGB color tuple for borders. |
text |
tuple[float, float, float]
|
An RGB color tuple for text. |
background |
tuple[float, float, float, float]
|
An RGBA color tuple for background. |
Source code in src/directmultistep/utils/web_visualize.py
RetroSynthesisTree
Basic tree structure for retrosynthesis visualization.
Source code in src/directmultistep/utils/web_visualize.py
__init__(idx=0)
Initializes a new node in the retrosynthesis tree.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
idx
|
int
|
The unique identifier for the node. |
0
|
Source code in src/directmultistep/utils/web_visualize.py
__str__()
Returns a string representation of the tree node and its children.
Source code in src/directmultistep/utils/web_visualize.py
build_tree(path_dict)
Recursively builds the retrosynthesis tree from a dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path_dict
|
FilteredDict
|
A dictionary representing the tree structure. |
required |
Returns:
Type | Description |
---|---|
int
|
The next available node ID. |
Source code in src/directmultistep/utils/web_visualize.py
TreeDimensions
compute_subtree_dimensions(tree, img_width, img_height, y_offset)
Compute dimensions of a subtree for layout.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tree
|
RetroSynthesisTree
|
The subtree to compute dimensions for. |
required |
img_width
|
int
|
The width of the molecule image. |
required |
img_height
|
int
|
The height of the molecule image. |
required |
y_offset
|
int
|
The vertical offset between nodes. |
required |
Returns:
Type | Description |
---|---|
TreeDimensions
|
The dimensions of the subtree. |
Source code in src/directmultistep/utils/web_visualize.py
compute_canvas_dimensions(tree, img_width, img_height, y_offset)
Compute overall canvas dimensions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tree
|
RetroSynthesisTree
|
The retrosynthesis tree. |
required |
img_width
|
int
|
The width of the molecule image. |
required |
img_height
|
int
|
The height of the molecule image. |
required |
y_offset
|
int
|
The vertical offset between nodes. |
required |
Returns:
Type | Description |
---|---|
TreeDimensions
|
The dimensions of the canvas. |
Source code in src/directmultistep/utils/web_visualize.py
check_overlap(new_x, new_y, existing_boxes, img_width, img_height)
Check if a new node overlaps with existing nodes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
new_x
|
int
|
The x-coordinate of the new node. |
required |
new_y
|
int
|
The y-coordinate of the new node. |
required |
existing_boxes
|
list[tuple[int, int]]
|
A list of tuples representing the coordinates of existing nodes. |
required |
img_width
|
int
|
The width of the molecule image. |
required |
img_height
|
int
|
The height of the molecule image. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if there is an overlap, False otherwise. |
Source code in src/directmultistep/utils/web_visualize.py
draw_molecule(smiles, size, theme)
Render a SMILES string as base64-encoded PNG.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
smiles
|
str
|
The SMILES string of the molecule. |
required |
size
|
tuple[int, int]
|
The desired size (width, height) of the image. |
required |
theme
|
ThemeType
|
The color theme ("light" or "dark"). |
required |
Returns:
Type | Description |
---|---|
str
|
The base64-encoded PNG image data. |
Raises:
Type | Description |
---|---|
ValueError
|
If the SMILES string is invalid. |
Source code in src/directmultistep/utils/web_visualize.py
draw_tree_svg(tree, width, height, x_margin, y_margin, theme, force_canvas_width=None)
Create SVG visualization of the retrosynthesis tree.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tree
|
RetroSynthesisTree
|
The retrosynthesis tree to visualize. |
required |
width
|
int
|
The width of each molecule image. |
required |
height
|
int
|
The height of each molecule image. |
required |
x_margin
|
int
|
The horizontal margin between nodes. |
required |
y_margin
|
int
|
The vertical margin between nodes. |
required |
theme
|
ThemeType
|
The color theme ("light" or "dark"). |
required |
force_canvas_width
|
int | None
|
An optional width to force for the canvas. |
None
|
Returns:
Type | Description |
---|---|
str
|
The SVG content as a string. |
Source code in src/directmultistep/utils/web_visualize.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
|
create_tree_from_path_string(path_string)
Parse a dictionary-like string into a RetroSynthesisTree.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path_string
|
str
|
A string representing the tree structure as a dictionary. |
required |
Returns:
Type | Description |
---|---|
RetroSynthesisTree
|
A RetroSynthesisTree object. |
Source code in src/directmultistep/utils/web_visualize.py
draw_tree_from_path_string(path_string, save_path, width=400, height=400, x_margin=50, y_margin=100, theme='light')
Generate SVG and PDF visualizations from a path string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path_string
|
str
|
A string representing the tree structure as a dictionary. |
required |
save_path
|
Path
|
The path to save the generated SVG and PDF files. |
required |
width
|
int
|
The width of each molecule image. |
400
|
height
|
int
|
The height of each molecule image. |
400
|
x_margin
|
int
|
The horizontal margin between nodes. |
50
|
y_margin
|
int
|
The vertical margin between nodes. |
100
|
theme
|
str
|
The color theme ("light" or "dark"). |
'light'
|
Returns:
Type | Description |
---|---|
str
|
The SVG content as a string. |