Skip to content

Commit

Permalink
Merge pull request #52 from Julicancode/3D_to_2D
Browse files Browse the repository at this point in the history
Update visualisation of molecules to 2D
  • Loading branch information
RiesBen committed Aug 21, 2024
2 parents 5408678 + 8786ac2 commit 64fd89d
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/konnektor/visualization/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ def get_node_connectivities(cg: LigandNetwork) -> list[int]:

# some code borrowed from pen:
# https://iwatobipen.wordpress.com/2020/03/30/draw-scaffold-tree-as-network-with-molecular-image-rdkit-cytoscape/
def mol2svg(mol: Chem.Mol) -> str:
def mol2svg(mol: Chem.Mol, represent_molecules_twoD:bool=False) -> str:
try:
Chem.rdmolops.Kekulize(mol)
except:
pass

if represent_molecules_twoD:
Chem.rdDepictor.Compute2DCoords(mol)

drawer = rdMolDraw2D.MolDraw2DSVG(350, 300)
rdMolDraw2D.PrepareAndDrawMolecule(drawer, mol) # , legend=mol.GetProp("_Name"))
drawer.SetColour((184 / 256, 87 / 256, 65 / 256)) # Transparent white background
Expand Down Expand Up @@ -64,6 +68,7 @@ def _build_cytoscape(
network: gufe.LigandNetwork,
layout: str = "concentric",
show_molecules: bool = True,
represent_molecules_twoD: bool = False,
show_mappings: bool = False,
) -> ipycytoscape.CytoscapeWidget:
ligands = list(network.nodes)
Expand Down Expand Up @@ -95,7 +100,7 @@ def _build_cytoscape(
{
"name": n.name,
"classes": "ligand",
"img": mol2svg(n.to_rdkit()),
"img": mol2svg(n.to_rdkit(), represent_molecules_twoD=represent_molecules_twoD),
"col": c,
},
)
Expand Down Expand Up @@ -218,6 +223,7 @@ def draw_network_widget(
network: gufe.LigandNetwork,
layout: str = "cose",
show_molecules: bool = True,
represent_molecules_twoD: bool = False
show_mappings: bool = False,
) -> ipycytoscape.CytoscapeWidget:
"""For use in a jupyter noterbook, visualise a LigandNetwork
Expand All @@ -231,6 +237,8 @@ def draw_network_widget(
defaults to 'cose'
show_molecule: bool, optional
if to show molecule images on the representation, default True
represent_molecules_twoD: bool, optional
show the molecules in the nodes as 2D representations.
show_mappings: bool, optional
if to show mapping images on the representation, default False
"""
Expand All @@ -243,12 +251,14 @@ def interactive_widget(
network=network,
layout=layout,
show_molecules=show_molecules,
represent_molecules_twoD=represent_molecules_twoD,
show_mappings=show_mappings,
):
v = _build_cytoscape(
network=network,
layout=layout,
show_molecules=show_molecules,
represent_molecules_twoD=represent_molecules_twoD,
show_mappings=show_mappings,
)
return v
Expand Down

0 comments on commit 64fd89d

Please sign in to comment.