-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcafa_utils.py
107 lines (88 loc) · 2.74 KB
/
cafa_utils.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
import numpy as np
import pandas as pd
#### LOAD ONTOLOGY ####
def get_ancestors(ontology, term):
list_of_terms = []
list_of_terms.append(term)
data = []
while len(list_of_terms) > 0:
new_term = list_of_terms.pop(0)
if new_term not in ontology:
break
data.append(new_term)
for parent_term in ontology[new_term]['parents']:
if parent_term in ontology:
list_of_terms.append(parent_term)
return data
def generate_ontology(file, specific_space=False, name_specific_space=''):
ontology = {}
gene = {}
flag = False
with open(file) as f:
for line in f.readlines():
line = line.replace('\n','')
if line == '[Term]':
if 'id' in gene:
ontology[gene['id']] = gene
gene = {}
gene['parents'], gene['alt_ids'] = [], []
flag = True
elif line == '[Typedef]':
flag = False
else:
if not flag:
continue
items = line.split(': ')
if items[0] == 'id':
gene['id'] = items[1]
elif items[0] == 'alt_id':
gene['alt_ids'].append(items[1])
elif items[0] == 'namespace':
if specific_space:
if name_specific_space == items[1]:
gene['namespace'] = items[1]
else:
gene = {}
flag = False
else:
gene['namespace'] = items[1]
elif items[0] == 'is_a':
gene['parents'].append(items[1].split(' ! ')[0])
elif items[0] == 'name':
gene['name'] = items[1]
elif items[0] == 'is_obsolete':
gene = {}
flag = False
key_list = list(ontology.keys())
for key in key_list:
ontology[key]['ancestors'] = get_ancestors(ontology, key)
for alt_ids in ontology[key]['alt_ids']:
ontology[alt_ids] = ontology[key]
for key, value in ontology.items():
if 'children' not in value:
value['children'] = []
for p_id in value['parents']:
if p_id in ontology:
if 'children' not in ontology[p_id]:
ontology[p_id]['children'] = []
ontology[p_id]['children'].append(key)
return ontology
def get_and_print_children(ontology, term):
children = {}
if term in ontology:
for i in ontology[term]['children']:
children[i] = ontology[i]
print(i, ontology[i]['name'])
return children
#### GENERATE DATA BASED ON SLIDING WINDOW TECHNIQUE ####
def generate_label(indf):
y = []
label = []
df = pd.read_csv(indf)
for i in tqdm(range(len(df))):
y.append(df.iloc[i, 2:])
return df.columns.values[2:], np.array(y, dtype=int)
#### EMBEDDINGS EXTRACTION ####
def save_numpy(path, file):
with open(path, 'wb') as f:
np.save(f, file)