-
Notifications
You must be signed in to change notification settings - Fork 3
/
common.py
77 lines (50 loc) · 1.44 KB
/
common.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import logging
LOG = logging.getLogger(__name__)
__version__ = "0.1.0"
__author__ = ("Junpeng Fan",)
__email__ = "[email protected]"
def read_org(file):
"""
read .org file create by get_organism.py
:return: a list [org.abbr, name, url]
"""
r = []
LOG.info("get organism infomation from %r" % file)
for line in open(file):
line = line.strip()
if line:
r.append(line.split("\t"))
LOG.info("get %s records" % len(r))
return r
def read_org_ko(file):
"""
read KEGG organism KO .keg file
:param file: file name
:return: dict contains {protein_id: {"ko": [], "path": []}}, if protein_id has no ko, the ko will be "-"
"""
r = {}
path_id = ""
for n, line in enumerate(open(file)):
line = line.strip()
if not line:
continue
tag = line[0]
if tag == "C":
path_id = "ko"+line[-6:-1]
continue
if tag != "D":
continue
tmp = line.split("\t")
gene = tmp[0].split()[1]
if len(tmp) == 2:
ko = tmp[1].split()[0]
else:
LOG.warning("line %s: %r has no ko" % (n+1, line))
ko = ""
if gene not in r:
r[gene] = {"ko": [], "path": []}
if ko not in r[gene]["ko"]:
r[gene]["ko"].append(ko)
if path_id not in r[gene]["path"]:
r[gene]["path"].append(path_id)
return r