-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoOboParser1.py
107 lines (88 loc) · 3.01 KB
/
GoOboParser1.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/python3
import sys
import os
import re
import gzip
def main(args):
if len(sys.argv) <= 3:
help()
if not os.path.isfile(sys.argv[1]):
print(f''' The file {sys.argv[1]} does not exist ''')
sys.exit(0)
if not re.search('.gz$', sys.argv[1]):
print("This is not a gz file")
sys.exit(0)
if not sys.argv[2] in ["getEntry", "getParent", "getXref"]:
print("The command is not valid")
sys.exit(0)
if (sys.argv[2] in ["getEntry", "getParent"]):
if not re.search('^GO:[0-9]{7}', sys.argv[3]):
print("This is not a valid GO Id")
sys.exit(0)
else:
if not re.search('^[a-zA-Z0-9_]+', sys.argv[3]):
print("This is not a valid GO ID")
sys.exit(0)
gob = GoObo(sys.argv[1])
if sys.argv[2] == "getEntry":
res = gob.getEntry(sys.argv[3])
print(res)
elif sys.argv[2] == "getParent":
res = gob.getParent(sys.argv[3])
print(res)
else:
res = gob.getXref(sys.argv[3])
print(res)
class GoObo():
""" class initialiser """
def __init__ (self,filename):
""" Class initialiser """
self.filename = filename
def getEntry(self, GO_ID = ''):
file = gzip.open(self.filename, 'rt')
found = False
go_id = re.compile('^id: '+GO_ID)
res = ''
for line in file:
if re.search('^id: (GO:[0-9]{7})', line):
if re.search(go_id, line):
found = True
elif found:
break
if found:
res += line
file.close()
return(res)
def getParent(self, GO_ID = ''):
file = gzip.open(self.filename, 'rt')
found = False
go_id = re.compile('^id: '+GO_ID)
res = ''
for line in file:
if re.search('^id: (GO:[0-9]{7})', line):
if re.search(go_id, line):
found = True
elif found:
break
if found:
if re.search('is_a: (GO:[0-9]{7})', line):
res = re.sub('.+(GO:[0-9]{7}).*\n$',"\\1",line)
file.close()
return(res)
def getXref(self, Xref_ID = ''):
file = gzip.open(self.filename, 'rt')
#found = False
#xref_id = re.compile('xref: '+Xref_ID)
res = ''
for line in file:
if re.search('^id: (GO:[0-9]{7})', line):
id = re.sub('.+(GO:[0-9]{7}).*\n$',"\\1",line)
if re.search('^xref: '+Xref_ID, line):
res = id
file.close()
return(res)
def help():
print("GoUtils tool by Anil, 2021")
print("Usage: app.py obofilename getEntry|getParent|getXref Go_ID")
if __name__ == "__main__":
main(sys.argv)