Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graph plots #197

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
622d7b8
Fix param name typo in function docstring
kamurani Jul 2, 2022
8a3f3a4
add scaling node size by "rsa" feature as well as degree
kamurani Jul 2, 2022
9c9520b
add option for scaling node size by meiler embedding dimensions. Tak…
kamurani Jul 2, 2022
44a0bf8
remove walrus operator := for compatability
kamurani Jul 6, 2022
920e14e
Merge pull request #1 from a-r-j/master
kamurani Jul 7, 2022
efc69bb
Merge pull request #2 from a-r-j/master
kamurani Jul 8, 2022
a2806b6
Add type hints
a-r-j Jul 8, 2022
024e7a0
Update changelog
a-r-j Jul 8, 2022
7569751
add support for sizing nodes by RSA and colouring by hydrophobicity i…
kamurani Jul 22, 2022
66baf18
Merge remote-tracking branch 'origin/master' into graph_plots
kamurani Jul 22, 2022
3513e0c
Merge branch 'master' into graph_plots
a-r-j Jul 22, 2022
21d496a
add amino acid 3-letter code mapping to hydrophobicity scales from th…
kamurani Jul 29, 2022
2f4f0ee
add amino acid 3-letter code mapping to hydrophobicity scales from th…
kamurani Jul 29, 2022
fbac7ea
colour by hydrophobicity implemented for different scales
kamurani Jul 29, 2022
5878e4f
refactor `_node_feature` function; colour_by and size_by msupported w…
kamurani Jul 29, 2022
8948b1f
fix import statement to use graphein actual
kamurani Jul 29, 2022
1212d5e
add `hydrophobicity()`. not sure if should be passed a parameter dec…
kamurani Jul 29, 2022
6b29498
"Add a utility for getting the names of node, edge and graph attribut…
a-r-j Aug 1, 2022
00f99a5
fix edge attribute selection in util
a-r-j Aug 1, 2022
28b0ee1
add test for attribute name selection util
a-r-j Aug 1, 2022
8f22fed
use typing_extensions Literal for 3.7 support and update changelog
a-r-j Aug 2, 2022
2b69d6e
docstring, black
a-r-j Oct 23, 2022
9743332
black
a-r-j Oct 23, 2022
43bc240
fix type import; black
a-r-j Oct 23, 2022
c5e6d83
Merge branch 'master' into graph_plots
a-r-j Oct 31, 2022
f43f5e7
Merge branch 'master' into graph_plots
a-r-j Feb 16, 2023
fd1f996
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 16, 2023
37aaee8
Merge branch 'master' into graph_plots
a-r-j Mar 25, 2024
34c816b
Merge branch 'master' into graph_plots
a-r-j Oct 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
### 1.5.1
* [Feature] - [#197](https://github.com/a-r-j/graphein/pull/197/) adds support for sizing and colouring nodes in asteroid plots


#### Protein

Expand All @@ -8,6 +10,7 @@
* [Feature] - [#189](https://github.com/a-r-j/graphein/pull/189/) adds a `residue_id` column to PDB dfs to enable easier accounting in atom graphs.
* [Feature] - [#189](https://github.com/a-r-j/graphein/pull/189/) refactors torch geometric datasets to use parallelised download for faster dataset preparation.


#### Bugfixes

* [Patch] - [#187](https://github.com/a-r-j/graphein/pull/187) updates sequence retrieval due to UniProt API changes.
Expand Down
69 changes: 58 additions & 11 deletions graphein/protein/visualisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# Code Repository: https://github.com/a-r-j/graphein
from __future__ import annotations

import re
import logging
import re
from itertools import count
Expand Down Expand Up @@ -714,6 +715,7 @@ def asteroid_plot(
node_id: str,
k: int = 2,
colour_nodes_by: str = "shell", # residue_name
size_nodes_by: str = "degree",
colour_edges_by: str = "kind",
edge_colour_map: plt.cm.Colormap = plt.cm.plasma,
edge_alpha: float = 1.0,
Expand All @@ -724,6 +726,7 @@ def asteroid_plot(
use_plotly: bool = True,
show_edges: bool = False,
show_legend: bool = True,
node_size_min: float = 20,
node_size_multiplier: float = 10,
) -> Union[plotly.graph_objects.Figure, matplotlib.figure.Figure]:
"""Plots a k-hop subgraph around a node as concentric shells.
Expand All @@ -738,6 +741,8 @@ def asteroid_plot(
:type k: int
:param colour_nodes_by: Colour the nodes by this attribute. Currently only ``"shell"`` is supported.
:type colour_nodes_by: str
:param size_nodes_by: Size the nodes by an attribute.
:type size_nodes_by: str
:param colour_edges_by: Colour the edges by this attribute. Currently only ``"kind"`` is supported.
:type colour_edges_by: str
:param edge_colour_map: Colour map for edges. Defaults to ``plt.cm.plasma``.
Expand All @@ -756,8 +761,10 @@ def asteroid_plot(
:type show_edges: bool
:param show_legend: Whether to show the legend of the edges. Fefaults to `True``.
:type show_legend: bool
:param node_size_min: Specifies node minimum size. Defaults to ``20.0``.
:type node_size_min: float
:param node_size_multiplier: Multiplier for the size of the nodes. Defaults to ``10``.
:type node_size_multiplier: float.
:type node_size_multiplier: float
:returns: Plotly figure or matplotlib figure.
:rtpye: Union[plotly.graph_objects.Figure, matplotlib.figure.Figure]
"""
Expand Down Expand Up @@ -817,21 +824,61 @@ def asteroid_plot(
node_x.append(x)
node_y.append(y)

degrees = [
subgraph.degree(n) * node_size_multiplier for n in subgraph.nodes()
]
def node_size_function(g: nx.Graph, feature: str):
a-r-j marked this conversation as resolved.
Show resolved Hide resolved
if feature == 'degree':
return lambda k : g.degree(k)
elif feature == 'rsa':
return lambda k : g.nodes(data=True)[k]['rsa']
else:
raise NotImplementedError(f"Size by {size_nodes_by} not implemented.")

node_size = node_size_function(subgraph, size_nodes_by)
node_sizes = [node_size_min + node_size(n) * node_size_multiplier for n in subgraph.nodes()]

if colour_nodes_by == "shell":
node_colours = []
for n in subgraph.nodes():
for k, v in nodes.items():
if n in v:
node_colours.append(k)
elif colour_nodes_by == "hydrophobicity":

"""
TODO Does a function like this already exist somewhere?
"""
def hydrophobicity_of_residue(res: str, mapping: str = 'a'):
a-r-j marked this conversation as resolved.
Show resolved Hide resolved
hmap = {
"ILE" : 4.5,
"VAL" : 4.2,
"LEU" : 3.8,
"PHE" : 2.8,
"CYS" : 2.5,
"MET" : 1.9,
"ALA" : 1.8,
"GLY" : -0.4,
"THR" : -0.7,
"SER" : -0.8,
"TRP" : -0.9,
"TYR" : -1.3,
"PRO" : -1.6,
"HIS" : -3.2,
"GLU" : -3.5,
"GLN" : -3.5,
"ASP" : -3.5,
"ASN" : -3.5,
"LYS" : -3.9,
"ARG" : -4.5,
}
return hmap[res]

node_colours = []
for n in subgraph.nodes():
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you can replace this nested loop with:

for n, d in subgraph.nodes(data=True)
...

for k, v in nodes.items():
if n in v:
node_colours.append(hydrophobicity_of_residue(n.split(':')[1]))
else:
raise NotImplementedError(
f"Colour by {colour_nodes_by} not implemented."
)
# TODO colour by AA type
raise NotImplementedError(f"Colour by {colour_nodes_by} not implemented.")

node_trace = go.Scatter(
x=node_x,
y=node_y,
Expand All @@ -841,13 +888,13 @@ def asteroid_plot(
textposition="bottom center",
showlegend=False,
marker=dict(
colorscale="YlGnBu",
colorscale="viridis",
reversescale=True,
color=node_colours,
size=degrees,
size=node_sizes,
colorbar=dict(
thickness=15,
title="Shell",
title=str.capitalize(colour_nodes_by),
tickvals=list(range(k)),
xanchor="left",
titleside="right",
Expand Down