我發現這對於快速查看來自PostgreSQL的源文件爲CSV文件的交互數據很有用。 [下面輸出重新格式化爲可讀性。]
## PSQL ['DUMMY' DATA]:
[interactions_practice]# \copy (SELECT gene_1, gene_2 FROM interactions
WHERE gene_1 in (SELECT gene_2 FROM interactions))
TO '/tmp/a.csv' WITH CSV -- << note: no terminating ";" for this query
## BASH:
[[email protected] ~]$ cat /tmp/a.csv
APC,TP73
BARD1,BRCA1
BARD1,ESR1
BARD1,KRAS2
BARD1,SLC22A18
BARD1,TP53
BRCA1,BRCA2
BRCA1,CHEK2
BRCA1,MLH1
BRCA1,PHB
BRCA2,CHEK2
BRCA2,TP53
CASP8,ESR1
CASP8,KRAS2
CASP8,PIK3CA
CASP8,SLC22A18
CDK2,CDKN1A
CHEK2,CDK2
ESR1,BRCA1
ESR1,KRAS2
ESR1,PPM1D
ESR1,SLC22A18
KRAS2,BRCA1
MLH1,CHEK2
MLH1,PMS2
PIK3CA,BRCA1
PIK3CA,ESR1
PIK3CA,RB1CC1
PIK3CA,SLC22A18
PMS2,TP53
PTEN,BRCA1
PTEN,MLH3
RAD51,BRCA1
RB1CC1,SLC22A18
SLC22A18,BRCA1
TP53,PTEN
## PYTHON 3.5 VENV (ANACONDA):
>>> import networkx as nx
>>> import pylab as plt
>>> G = nx.read_edgelist("/tmp/a.csv", delimiter=",")
>>> G.edges()
[('CDKN1A', 'CDK2'), ('MLH3', 'PTEN'), ('TP73', 'APC'), ('CHEK2', 'MLH1'),
('CHEK2', 'BRCA2'), ('CHEK2', 'CDK2'), ('CHEK2', 'BRCA1'), ('BRCA2', 'TP53'),
('BRCA2', 'BRCA1'), ('KRAS2', 'CASP8'), ('KRAS2', 'ESR1'), ('KRAS2', 'BRCA1'),
('KRAS2', 'BARD1'), ('PPM1D', 'ESR1'), ('BRCA1', 'PHB'), ('BRCA1', 'ESR1'),
('BRCA1', 'PIK3CA'), ('BRCA1', 'PTEN'), ('BRCA1', 'MLH1'), ('BRCA1', 'SLC22A18'),
('BRCA1', 'BARD1'), ('BRCA1', 'RAD51'), ('CASP8', 'ESR1'), ('CASP8', 'SLC22A18'),
('CASP8', 'PIK3CA'), ('TP53', 'PMS2'), ('TP53', 'PTEN'), ('TP53', 'BARD1'),
('PMS2', 'MLH1'), ('PIK3CA', 'SLC22A18'), ('PIK3CA', 'ESR1'), ('PIK3CA', 'RB1CC1'),
('SLC22A18', 'ESR1'), ('SLC22A18', 'RB1CC1'), ('SLC22A18', 'BARD1'), ('BARD1', 'ESR1')]
>>> G.number_of_edges()
36
>>> G.nodes()
['CDKN1A', 'MLH3', 'TP73', 'CHEK2', 'BRCA2', 'KRAS2', 'CDK2', 'PPM1D', 'BRCA1',
'CASP8', 'TP53', 'PMS2', 'RAD51', 'PIK3CA', 'MLH1', 'SLC22A18', 'BARD1', 'PHB', 'APC', 'ESR1', 'RB1CC1', 'PTEN']
>>> G.number_of_nodes()
22
>>> from networkx.drawing.nx_agraph import graphviz_layout
>>> ## nx.draw(G, pos=graphviz_layout(G))
## DUE TO AN UNIDENTIFIED BUG, I GET THIS ERROR THE FIRST TIME RUNNING THIS
## COMMAND; JUST RE-RUN IT:
>>> nx.draw(G, pos=graphviz_layout(G), node_size=1200, node_color='lightblue',
linewidths=0.25, font_size=10, font_weight='bold', with_labels=True)
QGtkStyle could not resolve GTK. Make sure you have installed the proper libraries.
>>> nx.draw(G, pos=graphviz_layout(G), node_size=1200, node_color='lightblue',
linewidths=0.25, font_size=10, font_weight='bold', with_labels=True)
>>> plt.show() ## plot1.png [opens in matplotlib popup window] attached
這是很難在這些靜態networkx/matplotlib曲線減小擁塞;一個解決辦法是增加的數字大小,每此StackOverflow的Q/A:High Resolution Image of a Graph using NetworkX and Matplotlib:
>>> plt.figure(figsize=(20,14))
<matplotlib.figure.Figure object at 0x7f1b65ea5e80>
>>> nx.draw(G, pos=graphviz_layout(G), node_size=1200, node_color='lightblue',
linewidths=0.25, font_size=10, font_weight='bold', with_labels=True, dpi=1000)
>>> plt.show() ## plot2.png attached
## RESET OUTPUT FIGURE SIZE TO SYSTEM DEFAULT:
>>> plt.figure()
<matplotlib.figure.Figure object at 0x7f1b454f1588>
plot1.png
plot2.png
獎金 - 最短路徑:
>>> nx.dijkstra_path(G, 'CDKN1A', 'MLH3')
['CDKN1A', 'CDK2', 'CHEK2', 'BRCA1', 'PTEN', 'MLH3']
對'graphviz_layout'的問題請參考http://stackoverflow.com/questions/35279733/what-could-cause-networkx-pygraphviz-to-work-fine-alone-but-not-together – Mrlenny
用法:'nx。繪製(G,pos = graphviz_layout(G))' – Mrlenny