-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
58 lines (47 loc) · 1.8 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import networkx as nx
import matplotlib.pyplot as plt
import scipy as sp
homer = open("a.txt", "r")
def read_nodes(homer):
"""Reads in the nodes of the graph from the input file.
Args:
homer: A handle for the file containing the graph data, starting at the top.
Returns:
A generator of the nodes in the graph, yielding a list of the form:
['CH', 'AG, 'ME', ...]
"""
line = homer.readline()
while line != '\n':
line = line.replace('\n', '')
if len(line) != 0:
if line[0] != '*':
if line[:2] != '1:':
yield line[:2]
else: break
line = homer.readline()
def read_edges(homer):
"""Reads in the edges of the graph from the input file.
Args:
homer: A handle for the file containing the graph data, starting at the top
of the edges section.
Returns:
A generator of the edges in the graph, yielding a list of pairs of the form:
[('CH', 'AG'), ('AG', 'ME'), ...]
"""
for line in homer.readlines():
line = line.replace('\n', '')
if len(line) > 1:
if ':' in line:
idx = line.index(':')
line = line[(idx+1):]
all_related_nodes = line.split(';')
for related_nodes in all_related_nodes:
related_nodes = related_nodes.split(',')
for idx_node1 in range(len(related_nodes)-1):
for idx_node2 in range(idx_node1+1, len(related_nodes)):
pair = (related_nodes[idx_node1], related_nodes[idx_node2])
yield pair
G = nx.Graph()
G.add_nodes_from(read_nodes(homer))
G.add_edges_from(read_edges(homer))
nx.draw(G)