-
Notifications
You must be signed in to change notification settings - Fork 0
/
CallJsonAPI.py
62 lines (37 loc) · 1.24 KB
/
CallJsonAPI.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
import os,hashlib,pickle,requests
def check_gene(gene):
if gene == "None":
return False
if gene.isnumeric():
return False
link = "https://www.ebi.ac.uk/proteins/api/proteins?offset=0&size=1&exact_gene="+ gene.upper().strip()
data = get_from_json_api(link)
if (len(data)) < 1:
return False
return True
def get_from_json_api(link):
key = link+"v2"
encoded = key.encode('utf-8')
m = hashlib.md5()
m.update(encoded)
filename = m.hexdigest()
filename = "./cache/" + filename
if os.path.exists(filename):
with open(filename, 'rb') as file:
data = pickle.load(file)
else:
headers = {"Accept": "application/json"}
data = requests.get(link, headers=headers).json()
with open(filename, 'wb') as file:
pickle.dump(data, file)
return data
def get_go_description(go, returnType="definition"):
link ="https://api.geneontology.cloud/go/"+go.upper()
hlink = "https://api.geneontology.cloud/go/" + go.upper()+"/hierarchy"
data = get_from_json_api(link)
hdata = get_from_json_api(hlink)
data["hierarchy"] = hdata
desc = ""
if (returnType in data):
desc = data[returnType]
return desc