我想绘制一组基因的有向图。假设有些基因是癌基因,有些是驱动基因。此外,基因与基因的相互作用使用特定的形状和颜色进行加权和表示。我使用以下代码来绘制有向图:
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
# Your weighted adjacency matrix (replace this with your actual matrix)
adjacency_matrix = np.array([
[0, 0.2, 0, 0, 0.4],
[0.0, 0, 0, 0, 0.1],
[0.1, 0, 0, 0.1, 0],
[0, 0, 0.3, 0, 0],
[0.0, 0.0, 0, 0, 0]
])
# Your Katz centrality scores (replace this with your actual scores)
katz_centrality_scores = [0.95, 0.03, 0.65, 0.12, 0.06]
# Relative scaling to range [1,10]
katz_centrality_scores = [9*(i-min(katz_centrality_scores))/(max(katz_centrality_scores) - min(katz_centrality_scores)) + 1 for i in katz_centrality_scores]
# Gene labels (replace with your gene labels)
gene_labels = ["Gene1", "Gene2", "Gene3", "Gene4", "Gene5"]
# Gene types (1 for oncogene, 2 for driver gene)
gene_types = [1, 2, 1, 2, 2]
# Create a graph
G = nx.DiGraph()
# Add nodes with attributes
for i in range(len(gene_labels)):
node_color = 'red' if gene_types[i] == 1 else 'green'
node_shape = 'v' if gene_types[i] == 1 else 's'
node_size = katz_centrality_scores[i]*80 # Adjust the scaling factor as needed
G.add_node(gene_labels[i], color=node_color, shape=node_shape, size=node_size)
node_colors = [v['color'] for v in dict(G.nodes(data=True)).values()]
# Add edges from the adjacency matrix
for i in range(len(gene_labels)):
for j in range(len(gene_labels)):
if adjacency_matrix[i][j] > 0:
G.add_edge(gene_labels[i], gene_labels[j], weight=katz_centrality_scores[i], color=node_colors[i])
# Extract node attributes
node_colors = [G.nodes[n]['color'] for n in G.nodes()]
node_shapes = [G.nodes[n]['shape'] for n in G.nodes()]
node_sizes = [G.nodes[n]['size'] for n in G.nodes()]
edge_colors = [G[u][v]['color'] for u, v in G.edges()]
# Extract edge weights
edge_weights = [G[u][v]['weight'] for u, v in G.edges()]
# Draw the graph
pos = nx.spring_layout(G, seed=42) # You can use other layout algorithms
curved_edges = [edge for edge in G.edges() if reversed(edge) in G.edges()]
straight_edges = [edge for edge in G.edges() if not reversed(edge) in G.edges()]
nx.draw(G,
pos,
node_color=node_colors,
node_size=node_sizes,
edge_color=edge_colors,
# node_shape=node_shapes,
width=edge_weights,
with_labels=True,
edgelist=straight_edges,
arrowsize=25,
arrowstyle='->')
nx.draw(G,
pos,
node_color=node_colors,
node_size=node_sizes,
edge_color=edge_colors,
# node_shape=node_shapes,
width=edge_weights,
with_labels=True,
edgelist=curved_edges,
connectionstyle='arc3, rad = 0.25',
arrowsize=25,
arrowstyle='->')
# Create a legend
red_patch = plt.Line2D([0], [0], marker='v', color='red', label='Oncogene', markersize=10, linestyle='None')
green_patch = plt.Line2D([0], [0], marker='s', color='green', label='Driver Gene', markersize=10, linestyle='None')
plt.legend(handles=[red_patch, green_patch], loc='upper right')
# Show the plot
plt.title('Gene Network')
plt.axis('off') # Turn off axis labels and ticks
plt.show()
运行上述代码后,图的边属性如下:
list(G.edges(data=True))
[('Gene1', 'Gene2', {'weight': 10.0, 'color': 'red'}),
('Gene1', 'Gene5', {'weight': 10.0, 'color': 'red'}),
('Gene2', 'Gene5', {'weight': 1.0, 'color': 'green'}),
('Gene3', 'Gene1', {'weight': 7.065217391304349, 'color': 'red'}),
('Gene3', 'Gene4', {'weight': 7.065217391304349, 'color': 'red'}),
('Gene4', 'Gene3', {'weight': 1.8804347826086958, 'color': 'green'})]
`
上面的代码生成以下图表(这是不正确的):
请注意,图像不符合代码中提到的节点和边标准。例如,
节点对 (Gene3, Gene4) 和 (Gene4, Gene3) 的边颜色和权重分别为 (red,7.06) 和 (green,1.88)。但在生成的图中,(Gene4, Gene3) 之间的边颜色显示为红色(不是绿色),并且边厚度与 (Gene3, Gene4) 相同(这是错误的)。
当我取消注释
node_shape
中的 nx.draw
参数时,出现以下错误:ValueError: Unrecognized marker style ['v', 's', 'v', 's', 's']
。我无法弄清楚如何给出每个基因类别的节点形状(例如,致癌基因为三角形,驱动基因为正方形)
任何人都可以向我建议我在上面的代码中缺少什么吗?
谢谢。
curved_edges = [edge for edge in G.edges() if reversed(edge) in G.edges()]
straight_edges = [edge for edge in G.edges() if not reversed(edge) in G.edges()]
这里有两个不同的列表,并调用绘制两次,每个列表一次。然而,每次调用时您都会传递相同的颜色列表
edge_color=edge_colors,
您需要两个颜色列表,一个用于直边,一个用于曲边,以便绘制方法将为每个边获得正确的颜色。