forked from MOZI-AI/knowledge-import
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sif.py
55 lines (43 loc) · 1.28 KB
/
sif.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
"""
SIF parser
see http://manual.cytoscape.org/en/stable/Supported_Network_File_Formats.html#sif-format
"""
import re
import io
tab_re = re.compile('.*(\t).*')
class Item:
def __init__(self, node_a, relation, nodes_b):
self.node_a = node_a
self.relation = relation
self.nodes_b = nodes_b
class SIF:
def __init__(self, path_or_file):
# read file and search for tabs
lines = []
if not hasattr(path_or_file, 'readlines'):
fileobj = open(path_or_file)
else:
fileobj = path_or_file
lines, has_tab = self._read_lines(fileobj)
if has_tab:
sep = '\t'
else:
sep = '\s'
self.lines = []
for line in lines:
items = [x.strip() for x in line.split(sep)]
node_a = items[0]
relation = items[1]
nodes_b = items[2:]
self.lines.append(Item(node_a, relation, nodes_b))
def _read_lines(self, fileobj):
lines = []
has_tab = False
for line in fileobj.readlines():
match = tab_re.match(line)
if match is not None:
has_tab = True
lines.append(line)
return lines, has_tab
def __len__(self):
return len(lines)