scmagnify.plotting.GRNVisualizer#

class scmagnify.plotting.GRNVisualizer(gdata)#

Class for visualizing gene regulatory networks (GRNs) from GRNMuData objects.

This class integrates data preprocessing, flexible filtering, and visualization. It extracts and constructs a regulatory network from a gdata (GRNMuData) object, allows users to filter the network by specifying a regulon or lists of TFs/TGs, and finally generates a high-quality static network plot using netgraph.

Key Features: 1. Places TF, cCRE, and Target nodes on distinct concentric circles for triplet networks. 2. Places TF and Target nodes on two concentric circles for bipartite networks. 3. Automatically clusters target genes by their regulating TFs and optimizes the cluster layout. 4. Uses an iterative algorithm to optimize node angles on the circles, reducing edge crossings. 5. Resolves overlapping cCRE nodes using a “jitter” mechanism to ensure visibility. 6. Renders the final plot using netgraph for a highly customizable and aesthetic result.

Parameters:

gdata (GRNMuData) – The GRNMuData object containing gene regulatory network data.

Methods table#

add_continuous_mapping(node_type, ...[, ...])

Configure a continuous mapping from numerical data to a visual property.

example_interactive_plot()

Example function to demonstrate the creation of an interactive plot.

export(path[, format, export_style, ...])

Exports the network graph file and can optionally export a companion Cytoscape style file.

load_network(network_df[, tf_col, ccre_col, ...])

Loads a network directly from a DataFrame, bypassing gdata processing.

plot([figsize, dpi, title, title_fontsize, ...])

Executes the full layout pipeline and draws the network plot using netgraph.

prepare_network([regulon, tf_list, tg_list, ...])

Prepares and filters the network for visualization.

Methods#

GRNVisualizer.add_continuous_mapping(node_type, visual_property, modal, map_range, layer=None, var_key=None, varm_key=None, transform='mean', legend_title=None)#

Configure a continuous mapping from numerical data to a visual property. …

Parameters:
  • ...

  • legend_title (str, optional) – A title for the legend that will be drawn for this mapping. If None, a default title will be generated.

  • ...

  • node_type (str)

  • visual_property (str)

  • modal (str)

  • map_range (tuple[float, float] | str)

  • layer (str | None)

  • var_key (str | None)

  • varm_key (tuple[str, str] | None)

  • transform (str)

Return type:

GRNVisualizer

GRNVisualizer.example_interactive_plot()#

Example function to demonstrate the creation of an interactive plot.

GRNVisualizer.export(path, format='graphml', export_style=False, style_path=None, style_name='scMagnify_GRN_Style', tf_layout_mode='uniform', node_color_map=None, node_size_map=None, node_shape_map=None, highlight=None, highlight_edge_color='#d62728', highlight_edge_width=1.0, default_edge_color='lightgrey', default_edge_width=0.5)#

Exports the network graph file and can optionally export a companion Cytoscape style file.

Parameters:
  • path (str) – The file path to save the network to.

  • format (str, default="graphml") – The format of the output file. Either “graphml” or “gexf”.

  • export_style (bool, default=False) – If True, an additional Cytoscape style file in .xml format will be generated.

  • style_path (str, optional) – The save path for the Cytoscape style file. If not provided, it will be generated automatically based on the network filename.

  • style_name (str, default="scMagnify_GRN_Style") – The name of the style as it will appear in Cytoscape.

  • tf_layout_mode (str, default="uniform") – The layout mode for TFs, consistent with the plot() method.

  • ...

  • node_color_map (dict[str, any] | None)

  • node_size_map (dict[str, float] | None)

  • node_shape_map (dict[str, str] | None)

  • highlight (dict[str, list[str]] | None)

  • highlight_edge_color (str)

  • highlight_edge_width (float)

  • default_edge_color (str)

  • default_edge_width (float)

Return type:

None

GRNVisualizer.load_network(network_df, tf_col='TF', ccre_col='cCRE', target_col='Target', network_type='auto', add_suffixes=True, target_clusters=None)#

Loads a network directly from a DataFrame, bypassing gdata processing.

Args:

network_df (pd.DataFrame): DataFrame containing the network connections. tf_col (str): The name of the column containing Transcription Factors. ccre_col (str): The name of the column containing cCREs/peaks. target_col (str): The name of the column containing Target Genes. network_type (str): The type of network (‘triplet’, ‘bipartite’, or ‘auto’).

‘auto’ detects type based on presence of ccre_col.

add_suffixes (bool): If True, adds “_TF” and “_TG” suffixes to node names. target_clusters (Dict, optional): A dictionary to manually define TG clusters.

e.g., {“Cluster A”: [“GENE1”, “GENE2”]}. If provided, overrides automatic TF-based clustering.

Return type:

GRNVisualizer

Returns:

self: The instance itself, to allow for method chaining.

Parameters:
GRNVisualizer.plot(figsize=(10, 10), dpi=300, title='TF-cCRE-Target Network', title_fontsize=16, tf_layout_mode='uniform', node_color_map=None, node_size_map=None, node_shape_map=None, interactive=False, jitter_strength=5.0, label_mode='external', tf_label_font_dict=None, tf_label_bbox_dict=None, tf_label_patheffects=None, target_label_font_dict=None, target_label_bbox_dict=None, label_offset_factor=1.0, label_only_highlighted_targets=False, highlight=None, highlight_edge_color='#d62728', highlight_edge_width=1.0, highlight_shadow=False, highlight_shadow_color='#ffcccc', highlight_shadow_alpha=0.15, highlight_shadow_expansion=1.4, highlight_shadow_smoothness=0.1, highlight_shadow_resolution=200, draw_cluster_wedges=False, draw_cluster_labels=True, legend_fontsize=8, context=None, default_context=None, theme='ticks', font_scale=1, save=None, show=None, **kwargs)#

Executes the full layout pipeline and draws the network plot using netgraph.

Parameters:
  • (Tuple[int (figsize)

  • int]) (Size of the figure.)

  • (int) (dpi)

  • (str) (label_mode)

  • (str)

  • (Dict (highlight)

  • optional) (Dictionary specifying nodes to highlight.)

  • (Dict

  • optional)

  • (Dict

  • optional)

  • (bool) (interactive) – Best used in a Jupyter environment.

  • (float) (label_offset_factor)

  • (str)

  • (Dict

  • optional)

  • (Dict

  • optional)

  • (List (tf_label_patheffects)

  • optional)

  • (Dict

  • optional)

  • (Dict

  • optional)

  • (float)

  • (Dict

  • optional)

  • **kwargs (Additional keyword arguments passed to netgraph.draw().)

  • figsize (tuple[int, int])

  • dpi (int)

  • title (str)

  • title_fontsize (int)

  • tf_layout_mode (str)

  • node_color_map (dict[str, any] | None)

  • node_size_map (dict[str, float] | None)

  • node_shape_map (dict[str, str] | None)

  • interactive (bool)

  • jitter_strength (float)

  • label_mode (str)

  • tf_label_font_dict (dict | None)

  • tf_label_bbox_dict (dict | None)

  • tf_label_patheffects (list | None)

  • target_label_font_dict (dict | None)

  • target_label_bbox_dict (dict | None)

  • label_offset_factor (float)

  • label_only_highlighted_targets (bool)

  • highlight (dict[str, list[str]] | None)

  • highlight_edge_color (str)

  • highlight_edge_width (float)

  • highlight_shadow (bool)

  • highlight_shadow_color (str)

  • highlight_shadow_alpha (float)

  • highlight_shadow_expansion (float)

  • highlight_shadow_smoothness (float)

  • highlight_shadow_resolution (int)

  • draw_cluster_wedges (bool)

  • draw_cluster_labels (bool)

  • legend_fontsize (int)

  • context (str | None)

  • default_context (dict | None)

  • theme (str | None)

  • font_scale (float | None)

  • save (str | None)

  • show (bool | None)

Return type:

tuple[Figure, Axes] | None

Returns:

Tuple[matplotlib.figure.Figure, matplotlib.axes.Axes] or None: The figure and axes objects for static plots, or None for interactive plots.

GRNVisualizer.prepare_network(regulon=None, tf_list=None, tg_list=None, net_key='network', add_suffixes=True, target_clusters=None)#

Prepares and filters the network for visualization.

Parameters:
  • (str (regulon)

  • optional) (A dictionary to manually define TG clusters.)

  • (List[str] (tg_list)

  • optional)

  • (List[str]

  • optional)

  • (bool) (add_suffixes)

  • (Dict (target_clusters)

  • optional)

  • regulon (str | None)

  • tf_list (list[str] | None)

  • tg_list (list[str] | None)

  • net_key (str)

  • add_suffixes (bool)

  • target_clusters (dict[str, list[str]] | None)

Return type:

GRNVisualizer