-
Notifications
You must be signed in to change notification settings - Fork 1
/
bio_utils.py
373 lines (342 loc) · 13.6 KB
/
bio_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
from nltk import sent_tokenize
import re
import numpy as np
np.random.seed(1)
import math
import json
import os
import glob
import argparse
simple_events = ["Gene_expression", "Transcription", "Protein_catabolism", "Localization", "Binding", "Protein_modification",
"Phosphorylation", "Ubiquitination", "Acetylation", "Deacetylation"]
class Lang:
def __init__(self, name):
self.name = name
self.word2index = {"EOS":0,"UNK":1,"THEME":2}
self.index2word = {0: "EOS", 1:"UNK", 2:"THEME"}
self.n_words = 3 # Count SOS and EOS
self.labels = ["NoRel"]
self.label2id = {"NoRel":0}
def addSentence(self, sentence):
for word in sentence:
self.addWord(word)
def addWord(self, word):
if word not in self.word2index:
self.word2index[word] = self.n_words
self.index2word[self.n_words] = word
self.n_words += 1
def load_embeddings(file, lang):
emb_matrix = None
emb_dict = dict()
for line in open(file):
if not len(line.split()) == 2:
if "\t" in line:
delimiter = "\t"
else:
delimiter = " "
line_split = line.rstrip().split(delimiter)
# extract word and vector
word = line_split[0]
vector = np.array([float(i) for i in line_split[1:]])
embedding_size = vector.shape[0]
emb_dict[word] = vector
for i in range(3, lang.n_words):
base = math.sqrt(6/embedding_size)
word = lang.index2word[i]
try:
vector = emb_dict[word]
except KeyError:
vector = np.random.uniform(-base,base,embedding_size)
if np.any(emb_matrix):
emb_matrix = np.vstack((emb_matrix, vector))
else:
emb_matrix = np.random.uniform(-base,base,(4, embedding_size))
emb_matrix[3] = vector
return emb_matrix
def sanitizeWord(w):
if w.startswith("$T"):
return w
if w == ("xTHEMEx"):
return "THEME"
w = w.lower()
if is_number(w):
return "xnumx"
w = re.sub("[^a-z_]+","",w)
if w:
return w
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
def word_tokenize(text):
return re.findall(r"[\w|#\w|@\w]+|[^\w\s,]",text)
def get_token_spans(text):
"""
returns (words, start_offsets, end_offsets)
for each sentence in the provided text
"""
offset = 0
for s in sent_tokenize(text):
offset = text.find(s, offset)
yield sentence_tokens(s, offset)
def sentence_tokens(sentence, offset):
"""this is meant to be used by get_token_spans() only"""
pos = 0
starts = []
ends = []
words = word_tokenize(sentence)
for w in words:
pos = sentence.find(w, pos)
starts.append(pos + offset)
pos += len(w)
ends.append(pos + offset)
return words, starts, ends
def get_trigger(s, e, entities, phosphorylations):
for tlbl, trigger, entity, rule in phosphorylations:
if entities[trigger][2] >= s and entities[trigger][1] <= e:
yield tlbl, trigger, entity, rule
def check_entity(e, x):
for p in x:
if e == p[-2]:
return False
return True
def get_entity(s, e, entities, x):
for entity in entities:
if entities[entity][2] > s and entities[entity][1] < e and entities[entity][0] == "Protein" and check_entity(entity,x):
yield entity
def token_span(entity, starts):
res = []
offset = entity[1]
for w in word_tokenize(entity[-1]):
if entity[-1][offset-entity[1]] == " ":
offset += 1
try:
res.append(starts.index(offset))
except:
for i, s in enumerate(starts):
if i<len(starts)-1 and s<offset and starts[i+1]>offset:
res.append(i)
break
offset += len(w)
return res
def get_id(proteins, i, starts, entities):
for p in proteins:
if token_span(entities[p], starts) and i == token_span(entities[p], starts)[0]:
return p
return None
def replace_protein(words, entities, starts, proteins):
res = []
ps = []
for p in proteins:
ps += token_span(entities[p], starts)
for i, w in enumerate(words):
if i not in ps:
res.append(w)
p = get_id(proteins, i, starts, entities)
if p:
res.append("$"+p)
return res
def prepare_data(dirname, valids=None):
if valids:
vj = json.load(open(valids))
else:
vj = dict()
with open("rules/raw.json") as f:
rules = json.load(f)
maxl = 0
input_lang = Lang("input")
pos_lang = Lang("position")
char_lang = Lang("char")
train = []
for fname in glob.glob(os.path.join(dirname, '*.a1')):
root = os.path.splitext(fname)[0]
name = os.path.basename(root)
txt = root + '.txt'
a1 = root + '.a1'
a2 = root + '.a2'
entities = dict()
with open(a1) as f:
for line in f:
line = line.strip()
if line.startswith('T'):
[id, data, text] = line.split('\t')
[label, start, end] = data.split(' ')
entities[id] = (label, int(start), int(end), text)
phosphorylations = []
with open(a2) as f:
for line in f:
line = line.strip()
if line.startswith('T'):
[id, data, text] = line.split('\t')
[label, start, end] = data.split(' ')
entities[id] = (label, int(start), int(end), text)
if line.startswith('E') and "Phosphorylation" in line:
[id, data] = line.split('\t')
temp = data.split(' ')
[tlbl, trigger] = temp[0].split(':')
[elbl, entity] = temp[1].split(':')
if valids and root+"/"+id in vj:
rule = (vj[root+"/"+id])
else:
rule = "null"
if ((tlbl, trigger, entity, rule)) not in phosphorylations:
phosphorylations.append((tlbl, trigger, entity, rule))
if tlbl not in input_lang.labels:
input_lang.label2id[tlbl] = len(input_lang.labels)
input_lang.labels.append(tlbl)
with open(txt) as f:
text = f.read()
for words, starts, ends in get_token_spans(text):
if len(words) > maxl:
maxl = len(words)
s = int(starts[0])
e = int(ends[-1])
x = list(get_trigger(s, e, entities, phosphorylations))
y = list(get_entity(s, e, entities, x))
# proteins = [t[2] for t in x]
# triggers = [t[1] for t in x]
# new_words = replace_protein(words, entities, starts, triggers+proteins)
# temp = []
# for i,w in enumerate(new_words):
# sw = sanitizeWord(w)
# if sw:
# temp.append(sw)
# new_words = temp
for res in x:
try:
tlbl, trigger, entity, rule = res
if rule in rules:
rule = word_tokenize(rules[rule])
else:
rule = []
new_words = replace_protein(words, entities, starts, [trigger, entity])
temp = []
for i,w in enumerate(new_words):
sw = sanitizeWord(w)
if sw:
temp.append(sw)
new_words = temp
trigger_pos = (new_words.index("$"+trigger))
new_words[trigger_pos] = sanitizeWord(entities[trigger][-1])
e_pos = new_words.index("$"+entity)
st_pos = e_pos-10 if e_pos-10 > 0 else 0
ed_pos = e_pos+11 if e_pos+11 < len(new_words) else len(new_words)
if trigger_pos < ed_pos and trigger_pos > st_pos:
trigger_pos = trigger_pos - st_pos
else:
trigger_pos = -1
pos = [i-e_pos for i in range(st_pos, e_pos)]+[0]+[i-e_pos for i in range(e_pos+1, ed_pos)]
pos_lang.addSentence(pos)
res = new_words[st_pos: ed_pos]#["OTHER" if w.startswith("$T") and w != "$"+entity else w for w in new_words[st_pos: ed_pos]]
res[e_pos-st_pos] = "THEME"
input_lang.addSentence(res)
train.append((res, entity, e_pos-st_pos, trigger_pos, tlbl, pos, rule))
except:
continue
# print (words)
# print (new_words)
# print ([(p,entities[p]) for p in triggers+proteins])
for entity in y:
new_words = replace_protein(words, entities, starts, [entity])
temp = []
for i,w in enumerate(new_words):
sw = sanitizeWord(w)
if sw:
temp.append(sw)
new_words = temp
try:
e_pos = new_words.index("$"+entity)
st_pos = e_pos-10 if e_pos-10 > 0 else 0
ed_pos = e_pos+11 if e_pos+11 < len(new_words) else len(new_words)
pos = [i-e_pos for i in range(st_pos, e_pos)]+[0]+[i-e_pos for i in range(e_pos+1, ed_pos)]
pos_lang.addSentence(pos)
res = new_words[st_pos: ed_pos]#["OTHER" if w.startswith("$T") and w != "$"+entity else w for w in new_words[st_pos: ed_pos]]
res[e_pos-st_pos] = "THEME"
input_lang.addSentence(res)
train.append((res,entity, e_pos-st_pos, -1, None, pos, []))
except:
continue
# print (words)
# print (new_words)
# print ([(p,entities[p]) for p in triggers+proteins])
for i, w in input_lang.index2word.items():
char_lang.addSentence(w)
return input_lang, pos_lang, char_lang, train
def parse_json_data(input_lang, pos_lang, char_lang, train):
with open("rules/raw.json") as f:
rules = json.load(f)
rule_lang = Lang("rule")
with open("events.json") as f:
j = json.load(f)
for event in j:
sentence = event["sentence"]
rule = rules[event["rule"]]
rule = word_tokenize(rule)
entity = event["entity"]
sentence = sentence[:entity[1][0]]+"xTHEMEx"+sentence[entity[1][1]:]
trigger = event["trigger"]
words = []
for w in word_tokenize(sentence):
w = sanitizeWord(w)
if w:
words.append(w)
e_pos = words.index("THEME")
st_pos = e_pos-10 if e_pos-10 > 0 else 0
ed_pos = e_pos+11 if e_pos+11 < len(words) else len(words)
words = words[st_pos: ed_pos]
pos = [i-e_pos for i in range(st_pos, e_pos)]+[0]+[i-e_pos for i in range(e_pos+1, ed_pos)]
strigger = sanitizeWord(word_tokenize(trigger[0])[0])
if not strigger:
for w in word_tokenize(trigger[0]):
if sanitizeWord(w):
strigger = w
break
trigger_pos = None
try:
trigger_pos = words.index(strigger)
except:
continue
if trigger_pos:
input_lang.addSentence(words)
pos_lang.addSentence(pos)
rule_lang.addSentence(rule)
train.append((words, "T?", e_pos-st_pos, trigger_pos, "Phosphorylation", pos, rule))
for i, w in input_lang.index2word.items():
char_lang.addSentence(w)
return input_lang, pos_lang, char_lang, rule_lang, train
# if __name__ == '__main__':
# parser = argparse.ArgumentParser()
# parser.add_argument('datadir')
# args = parser.parse_args()
# print ("///")
# input_lang, pos_lang, char_lang, train = prepare_data(args.datadir, "valids.json")
# print (len(train))
# # # input_lang, pos_lang, char_lang, rule_lang, train = parse_json_data(input_lang, pos_lang, char_lang, train)
# # # # print (load_embeddings("embeddings_november_2016.txt", input_lang))
# # # offset_dict = dict()
# for t in train:
# if t[3] != -1:
# print (t[-1])
# print (t[-1])
# print (len(t[0]), len(t[-1]))
# print (t[3])
# print (t[0][t[3]], t[4])
# if t[3] != -1:
# print (t[2], t[3], t[-1][t[3]])
# try:
# offset_dict[t[-1][t[3]]] += 1
# except KeyError:
# offset_dict[t[-1][t[3]]] = 1
# with open("histogram.tsv", "w") as f:
# for i in range(min(offset_dict.keys()), max(offset_dict.keys())+1):
# l = offset_dict[i] if i in offset_dict else 0
# f.write("%d\t%d\n"%(i,l))