-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_next_level_training.py
111 lines (96 loc) · 2.89 KB
/
gen_next_level_training.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
108
109
110
111
#!/usr/bin/env python
import numpy
from utils import (
shuffle, train_val_test_split, get_gene_ontology, get_model_max_features)
import sys
import os
LAMBDA = 24
DATA_ROOT = 'data/swiss/'
go = get_gene_ontology()
def get_go_set(go_id):
go_set = set()
q = deque()
q.append(go_id)
while len(q) > 0:
g_id = q.popleft()
go_set.add(g_id)
for ch_id in go[g_id]['children']:
q.append(ch_id)
return go_set
def get_subtree_set(go_id):
node = go[go_id]
if 'go_set' in node:
return node['go_set']
go_set = set()
go_set.add(go_id)
for ch_id in node['children']:
if ch_id not in go_set:
go_set |= get_subtree_set(ch_id)
node['go_set'] = go_set
return go_set
def load_data_by_prot_id(go_id):
data = dict()
with open(DATA_ROOT + 'level_2/data/' + go_id + '.txt') as f:
for line in f:
line = line.strip().split()
prot_id = line[0]
data[prot_id] = line[1:]
return data
def load_training_data(go_id):
go_set = get_subtree_set(go_id)
data = list()
with open(DATA_ROOT + 'train.txt', 'r') as f:
for line in f:
line = line.strip().split('\t')
prot_id = line[0]
gos = line[2].split('; ')
ok = False
go_ids = list()
for go_id in gos:
if go_id in go_set:
ok = True
go_ids.append(go_id)
if ok:
data.append((prot_id, go_ids))
return data
def main(*args, **kwargs):
if len(args) != 2:
raise Exception('Please provide function id')
go_id = args[1]
paacs = load_data_by_prot_id(go_id)
data = load_training_data(go_id)
go_node = go[go_id]
go_set = get_subtree_set(go_id)
for ch_id in go_node['children']:
ch_set = get_subtree_set(ch_id)
positives = list()
negatives = list()
for prot_id, gos in data:
if prot_id not in paacs:
continue
pos = False
for g_id in gos:
if g_id in ch_set:
pos = True
break
if pos:
positives.append(prot_id)
else:
negatives.append(prot_id)
n = len(positives)
shuffle(positives)
shuffle(negatives)
negatives = negatives[:n]
with open(DATA_ROOT + 'level_2/' + go_id + '/' + ch_id + '.txt', 'w') as f:
for prot_id in negatives:
f.write('0 ' + prot_id)
for p in paacs[prot_id]:
f.write(' ' + str(p))
f.write('\n')
for prot_id in positives:
f.write('1 ' + prot_id)
for p in paacs[prot_id]:
f.write(' ' + str(p))
f.write('\n')
if __name__ == '__main__':
main(*sys.argv)