Ejemplo n.º 1
0
def nxvizPlot(
    adata,
    cluster,
    cluster_name,
    edges_list,
    plot="arcplot",
    network=None,
    weight_scale=5e3,
    weight_threshold=1e-4,
    figsize=(6, 6),
    save_show_or_return="show",
    save_kwargs={},
    **kwargs,
):
    """Arc or circos plot of gene regulatory network for a particular cell cluster.

    Parameters
    ----------
        adata: :class:`~anndata.AnnData`.
            AnnData object.
        cluster: `str`
            The group key that points to the columns of `adata.obs`.
        cluster_name: `str` (default: `None`)
            The group whose network and arcplot will be constructed and created.
        edges_list: `dict` of `pandas.DataFrame`
            A dictionary of dataframe of interactions between input genes for each group of cells based on ranking
            information of Jacobian analysis. Each composite dataframe has `regulator`, `target` and `weight` three
            columns.
        plot: `str` (default: `arcplot`)
            Which nxviz plot to use, one of {'arcplot', 'circosplot'}.
        network: class:`~networkx.classes.digraph.DiGraph`
            A direct network for this cluster constructed based on Jacobian analysis.
        weight_scale: `float` (default: `1e3`)
            Because values in Jacobian matrix is often small, the value will be multiplied by the weight_scale so that
            the edge will have proper width in display.
        weight_threshold: `float` (default: `weight_threshold`)
            The threshold of weight that will be used to trim the edges for network reconstruction.
        figsize: `None` or `[float, float]` (default: (6, 6)
            The width and height of each panel in the figure.
        save_show_or_return: `str` {'save', 'show', 'return'} (default: `show`)
            Whether to save, show or return the figure.
        save_kwargs: `dict` (default: `{}`)
            A dictionary that will passed to the save_fig function. By default it is an empty dictionary and the
            save_fig function will use the {"path": None, "prefix": 'arcplot', "dpi": None, "ext": 'pdf', "transparent":
            True, "close": True, "verbose": True} as its parameters. Otherwise you can provide a dictionary that
            properly modify those keys according to your needs.
        **kwargs:
            Additional parameters that will pass to ArcPlot or CircosPlot

    Returns
    -------
        Nothing but plot an ArcPlot of the input direct network.
    """

    _, has_labeling = (
        adata.uns["pp"].get("has_splicing"),
        adata.uns["pp"].get("has_labeling"),
    )
    layer = "M_s" if not has_labeling else "M_t"

    import matplotlib.pyplot as plt

    try:
        import networkx as nx
        import nxviz as nv
    except ImportError:
        raise ImportError(
            "You need to install the packages `networkx, nxviz`."
            "install networkx via `pip install networkx`."
            "install nxviz via `pip install nxviz`."
        )

    if edges_list is not None:
        network = nx.from_pandas_edgelist(
            edges_list[cluster_name].query("weight > @weight_threshold"),
            "regulator",
            "target",
            edge_attr="weight",
            create_using=nx.DiGraph(),
        )
        if len(network.node) == 0:
            raise ValueError(
                f"weight_threshold is too high, no edge has weight than {weight_threshold} " f"for cluster {cluster}."
            )

    # Iterate over all the nodes in G, including the metadata
    if type(cluster_name) is str:
        cluster_names = [cluster_name]
    for n, d in network.nodes(data=True):
        # Calculate the degree of each node: G.node[n]['degree']
        network.nodes[n]["degree"] = nx.degree(network, n)
        # data has to be float
        if cluster is not None:
            network.nodes[n]["size"] = (
                adata[adata.obs[cluster].isin(cluster_names), n].layers[layer].A.mean().astype(float)
            )
        else:
            network.nodes[n]["size"] = adata[:, n].layers[layer].A.mean().astype(float)

        network.nodes[n]["label"] = n
    for e in network.edges():
        network.edges[e]["weight"] *= weight_scale

    if plot.lower() == "arcplot":
        prefix = "arcPlot"
        # Create the customized ArcPlot object: a2
        nv_ax = nv.ArcPlot(
            network,
            node_order=kwargs.pop("node_order", "degree"),
            node_size=kwargs.pop("node_size", None),
            node_grouping=kwargs.pop("node_grouping", None),
            group_order=kwargs.pop("group_order", "alphabetically"),
            node_color=kwargs.pop("node_color", "size"),
            node_labels=kwargs.pop("node_labels", True),
            edge_width=kwargs.pop("edge_width", "weight"),
            edge_color=kwargs.pop("edge_color", None),
            data_types=kwargs.pop("data_types", None),
            nodeprops=kwargs.pop(
                "nodeprops",
                {
                    "facecolor": "None",
                    "alpha": 0.2,
                    "cmap": "viridis",
                    "label": "label",
                },
            ),
            edgeprops=kwargs.pop("edgeprops", {"facecolor": "None", "alpha": 0.2}),
            node_label_color=kwargs.pop("node_label_color", False),
            group_label_position=kwargs.pop("group_label_position", None),
            group_label_color=kwargs.pop("group_label_color", False),
            fontsize=kwargs.pop("fontsize", 10),
            fontfamily=kwargs.pop("fontfamily", "serif"),
            figsize=figsize,
        )
    elif plot.lower() == "circosplot":
        prefix = "circosPlot"
        # Create the customized CircosPlot object: a2
        nv_ax = nv.CircosPlot(
            network,
            node_order=kwargs.pop("node_order", "degree"),
            node_size=kwargs.pop("node_size", None),
            node_grouping=kwargs.pop("node_grouping", None),
            group_order=kwargs.pop("group_order", "alphabetically"),
            node_color=kwargs.pop("node_color", "size"),
            node_labels=kwargs.pop("node_labels", True),
            edge_width=kwargs.pop("edge_width", "weight"),
            edge_color=kwargs.pop("edge_color", None),
            data_types=kwargs.pop("data_types", None),
            nodeprops=kwargs.pop("nodeprops", None),
            node_label_layout="rotation",
            edgeprops=kwargs.pop("edgeprops", {"facecolor": "None", "alpha": 0.2}),
            node_label_color=kwargs.pop("node_label_color", False),
            group_label_position=kwargs.pop("group_label_position", None),
            group_label_color=kwargs.pop("group_label_color", False),
            fontsize=kwargs.pop("fontsize", 10),
            fontfamily=kwargs.pop("fontfamily", "serif"),
            figsize=figsize,
        )

    # recover network edge weights
    for e in network.edges():
        network.edges[e]["weight"] /= weight_scale

    if save_show_or_return == "save":
        # Draw a to the screen
        nv_ax.draw()
        plt.autoscale()
        s_kwargs = {
            "path": None,
            "prefix": prefix,
            "dpi": None,
            "ext": "pdf",
            "transparent": True,
            "close": True,
            "verbose": True,
        }
        s_kwargs = update_dict(s_kwargs, save_kwargs)

        save_fig(**s_kwargs)
    elif save_show_or_return == "show":
        # Draw a to the screen
        nv_ax.draw()
        plt.autoscale()
        # Display the plot
        plt.show()
        # plt.savefig('./unknown_arcplot.pdf', dpi=300)
    elif save_show_or_return == "return":
        return nv_ax
Ejemplo n.º 2
0
G.nodes(data=True)
nx.draw(G)
G.edges(data=True)
G.has_edge(1,2)
(1,2) in G.edges()

for n, d in G.nodes(data=True):
    # Calculate the degree of each node: G.node[n]['degree']
    G.node[n]['degree'] = nx.degree(G)[n]


import nxviz as nv
import matplotlib.pyplot as plt

# Create the ArcPlot object: a
a = nv.ArcPlot(G, node_order='degree', node_labels=True)
a.draw()
ap = nv.ArcPlot(G, node_order='date', node_color='date', node_labels=True, node_size='date')
ap.draw()

h = nv.MatrixPlot(G)#, node_grouping='grouping')
h.draw()

c = nv.CircosPlot(G, node_order='degree', node_grouping = 'date', node_color='date')
c.draw()


###IDENTIFYING IMPORTANT NODES
list(G.neighbors(1))
nx.degree_centrality(G)
nx.betweenness_centrality(G)
Ejemplo n.º 3
0
#
# Let's draw a network. There are quite a few options available, of which I'll plot just three or four. First up...
#
# ArcPlot
# ----------

# In[ ]:

import networkx as nx
import nxviz as nv

G = nx.from_pandas_dataframe(data,
                             'sender',
                             'recipient1',
                             edge_attr=['date', 'subject'])
plot = nv.ArcPlot(G)
plot.draw()
plt.show()

# We can see our 500 nodes (employees) at the bottom and the dominance of the one node on the right side.  Please bear in mind, of course, that I am taking the first 500 rows of a 517,000 row dataset here, so we are only looking at emails from one of the 150 Enron executives contained in the full dataset.
#
# Next we'll bend the ends of this plot and join them together to form a circle, otherwise known as a
#
# CircosPlot
# ----------

# In[ ]:

plot = nv.CircosPlot(G)
plot.draw()
plt.show()
Ejemplo n.º 4
0
# Twitter data
T = nx.DiGraph()
print(T.nodes)
"""
Network Visualization
-> Matrix Plot: 
Nodes are the rows and columns of the matrix. 
Cells are filled in according to whether an edge exists between the pair of nodes
Undirected graph will always result in symmetrical matrix
-> Arc Plot:
All the nodes are put in a single axis and edges are drawn using circular arcs
-> Circos Plots
Transformation of the arc plot such that the nodes are in a circle 
"""
import nxviz as nv
ap = nv.ArcPlot(G)
ap.draw()
plt.show()

# Convert T to a matrix format: A
A = nx.to_numpy_array(T)
# Convert A back to 'category' metadeta field is lost from each node
T_conv = nx.from_numpy_matrix(A, create_using=nx.DiGraph)

for n, d in T_conv.nodes(data=True):
    assert 'category' not in d.keys()

# nv.MatrixPlot(G): To draw a matrix plot
# nv.CircosPlot(G): To draw a line plot

# To specify the ordering and coloring of nodes
Ejemplo n.º 5
0
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 30 11:01:08 2017

@author: Utkarsh
"""
import nxviz as nv
import matplotlib.pyplot as ply

ap = nv.ArcPlot(G)
ap.draw()
plt.show()
Ejemplo n.º 6
0
bk1 = [G_book1]
for _, edge in book1_frame.iterrows():
    G_book1.add_edge(edge['Source'], edge['Target'], weight=edge['weight'])
bk1.append(G_book1)

# Creating a list of networks for all the books
books = [G_book1]
book_fnames = [book2, book3, book4, book5]
for book_fname in book_fnames:
    book = pd.read_csv(book_fname)
    G_book = nx.Graph()
    for _, edge in book.iterrows():
        G_book.add_edge(edge['Source'], edge['Target'], weight=edge['weight'])
    books.append(G_book)

ap = nv.ArcPlot(G_book1)
ap.draw()
plt.show()

#FINDING MOST IMPORTANT CHARACTER
# Calculating the degree centrality of book 1
deg_cen_book1 = nx.degree_centrality(books[0])

# Calculating the degree centrality of book 5
deg_cen_book5 = nx.degree_centrality(books[4])

# Sorting the dictionaries according to their degree centrality and storing the top 10
sorted_deg_cen_book1 = sorted(deg_cen_book1.items(),
                              key=lambda x: x[1],
                              reverse=True)[0:10]
# Sorting the dictionaries according to their degree centrality and storing the top 10
Ejemplo n.º 7
0
"""Circos plot which is much easier to read."""

deg_cen = nx.degree_centrality(g)
nodes['deg_cen'] = nodes['Id'].apply(lambda x: deg_cen[x])
g = make_graph(nodes, edges)

fig, ax = plt.subplots(figsize=(20,8))
sns.barplot(data=nodes.nlargest(50, 'deg_cen'), 
            x='Id', y='deg_cen', 
            ax=ax);
ax.set_xticklabels(ax.get_xticklabels(), rotation=90);

"""Determining the most important character using the degree centrality of the nodes."""

a = nxv.ArcPlot(g,  
                node_size=[10*g.nodes[n]['deg_cen'] for n in g.nodes], 
                edge_width=(edges['Weight'] / edges['Weight'].quantile(.95)).tolist(),
                figsize=(10,10))
a.draw()

"""Arc plot to visualize degree centrality."""

cmt = community.best_partition(g, weight='Weight')
nodes['cmt'] = [v for c,v in cmt.items()]
g = make_graph(nodes, edges)

c = nxv.CircosPlot(g, node_color='cmt', node_grouping='cmt',
                   node_labels=True, node_label_layout='rotation',
                   edge_width=(edges['Weight'] / edges['Weight'].quantile(0.98)).tolist(),
                   figsize=(8,8))
c.draw()
Ejemplo n.º 8
0
# %%

# you can use nxviz to for matrix plot or circos plot or arc plot

# https://stackoverflow.com/questions/53366634/how-to-set-edge-color-in-nxviz-arcplot
D = nx.DiGraph()
D.add_nodes_from([1, 2, 3])
D.add_edge(1, 2)
D.add_edge(2, 3)

m = nz.MatrixPlot(D)
m.draw()
plt.show()

a = nz.ArcPlot(D)
a.draw(
)  # here you can provide node_order and node_color which is provided in nodes metadata
plt.show()

c = nz.CircosPlot(D)
c.draw()
plt.show()
# %%

# to get the neighbours of the node
D = nx.DiGraph()
D.add_nodes_from([1, 2, 3])
D.add_edge(1, 2)
D.add_edge(2, 3)
Ejemplo n.º 9
0
def arcplot(
        adata,
        cluster,
        cluster_name,
        edges_list,
        network=None,
        weight_scale=5e3,
        figsize=(6, 6),
        save_show_or_return='show',
        save_kwargs={},
):
    """Arc plot of gene regulatory network for a particular cell cluster.

    Parameters
    ----------
        adata: :class:`~anndata.AnnData`.
            AnnData object, must at least have gene-wise Jacobian matrix calculated for each or selected cell.
        edges_list: `dict` of `pandas.DataFrame`
            A dictionary of dataframe of interactions between input genes for each group of cells based on ranking
            information of Jacobian analysis. Each composite dataframe has `regulator`, `target` and `weight` three columns.
        network: class:`~networkx.classes.digraph.DiGraph`
            A direct network for this cluster constructed based on Jacobian analysis.
        cluster: `str`
            The group key that points to the columns of `adata.obs`.
        cluster_name: `str` (default: `None`)
            The group whose network and arcplot will be constructed and created.
        weight_scale: `float` (default: `1e3`)
            Because values in Jacobian matrix, the value will be multiplied by the weigt_scale.
        figsize: `None` or `[float, float]` (default: (6, 6)
            The width and height of each panel in the figure.
        save_show_or_return: `str` {'save', 'show', 'return'} (default: `show`)
            Whether to save, show or return the figure.
        save_kwargs: `dict` (default: `{}`)
            A dictionary that will passed to the save_fig function. By default it is an empty dictionary and the save_fig
            function will use the {"path": None, "prefix": 'scatter', "dpi": None, "ext": 'pdf', "transparent": True,
            "close": True, "verbose": True} as its parameters. Otherwise you can provide a dictionary that properly
            modify those keys according to your needs.

    Returns
    -------
        Nothing but plot an ArcPlot of the input direct network.
    """

    import matplotlib.pyplot as plt
    try:
        import networkx as nx
        import nxviz as nv
    except ImportError:
        raise ImportError(
            f"You need to install the packages `networkx, nxviz`."
            f"install networkx via `pip install networkx`."
            f"install nxviz via `pip install nxviz`.")

    if edges_list is not None:
        network = nx.from_pandas_edgelist(edges_list[cluster],
                                          'regulator',
                                          'target',
                                          edge_attr='weight',
                                          create_using=nx.DiGraph())

    # Iterate over all the nodes in G, including the metadata
    if type(cluster_name) is str: cluster_names = [cluster_name]
    for n, d in network.nodes(data=True):
        # Calculate the degree of each node: G.node[n]['degree']
        network.nodes[n]['degree'] = nx.degree(network, n)
        # data has to be float
        network.nodes[n]['size'] = adata[
            adata.obs[cluster].isin(cluster_names),
            n].layers['M_s'].A.mean().astype(float)
        network.nodes[n]['label'] = n
    for e in network.edges():
        network.edges[e]['weight'] *= weight_scale

    # Create the customized ArcPlot object: a2
    nv_ax = nv.ArcPlot(
        network,
        node_order='degree',
        nodeprops={'cmap': 'viridis'},
        node_labels=True,
        node_color='size',
        edge_width='weight',
        # node_grouping='grouping',
        figsize=figsize,
    )

    if save_show_or_return == "save":
        s_kwargs = {
            "path": None,
            "prefix": 'arcplot',
            "dpi": None,
            "ext": 'pdf',
            "transparent": True,
            "close": True,
            "verbose": True
        }
        s_kwargs = update_dict(s_kwargs, save_kwargs)

        save_fig(**s_kwargs)
    elif save_show_or_return == "show":
        # Draw a to the screen
        nv_ax.draw()
        plt.autoscale()
        # Display the plot
        plt.show()
        # plt.savefig('./unknown_arcplot.pdf', dpi=300)
    elif save_show_or_return == "return":
        return nv_ax