forked from rishabgit/genomic-info-from-papers
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhybrid_extraction.py
306 lines (266 loc) · 13 KB
/
hybrid_extraction.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
#!/usr/bin/env python
# coding: utf-8
import json
import re
import nltk
import nltk.data
import numpy as np
import pandas as pd
from regex_wrapper import regex_block
from textpresso import wbtools_get_papers, textpresso_paper_text
def ner_mutations(tokenClassificationPipeline, stop_words, sentence):
mutations = []
try:
ner_output = tokenClassificationPipeline(sentence)
for i, grp in enumerate(ner_output):
if grp['entity_group'] == 'LABEL_0':
mut = grp['word']
for j in range(i+1, len(ner_output)):
if ner_output[j]['entity_group'] == 'LABEL_1':
mut = mut + ' ' + ner_output[j]['word']
else:
# NER would be handling only data in NL form
if len(mut.split()) > 3 and any(word in mut.split() for word in stop_words):
mutations.append([mut, sentence])
break
except(Exception):
pass
return mutations
def get_paper_sentences_with_wbtools(paper_ids, settings):
'''
Takes WB Paper IDs, returns a list of sentences after filtering
Arg:
wbpids - List of wb papers ids
e.g. ['WBPaper00002379']
config_path - Config file path
store_ppr_path - Folder path to store the paper flatfiles
retrieved from TextPresso for future use
Returns:
paperid_sentence_list: List of paper ID and sentence
e.g. [['WBPaper00002379', 'First sentence'],
['WBPaper00002379', 'Second sentence'], ....]
'''
corpus_manager = wbtools_get_papers(settings['db_config'], paper_ids)
stop_words = set(nltk.corpus.stopwords.words('english'))
stop_words = [w for w in stop_words if len(w) > 1]
all_special_chars = []
with open('data/nala/train_dev.json') as f:
for jsonObj in f:
nala_json = json.loads(jsonObj)['tokens']
for word in nala_json:
if not word.isalnum():
all_special_chars.append(word)
# list of special characters to keep during inference
# helps with clearing out the bad characters from old papers
all_special_chars = list(set(all_special_chars))
paperid_sentence_list = []
for paper in corpus_manager.get_all_papers():
txt = paper.get_text_docs(split_sentences=True)
count_total_rows = len(txt)
for current_i, row in enumerate(txt):
if (row.lower().find("we thank") == 0 or
row.lower().find("this work was supported") == 0 or
row.lower().find("references") == 0 or
row.lower().find("we also thank") == 0 or
row.lower().find("this research was supported") == 0 or
row.lower().find("we acknowledge") == 0 or
row.lower().find("acknowledgments") == 0 or
row.lower().find('literature cited') != -1):
if current_i > count_total_rows/3:
break
# usually is bad sentence
if len(row) < 40 or not any(word in row.lower().split() for word in stop_words):
continue
# remove sentences with links and email ids
if re.search('\S+@\S+\.', row) or re.search('www\.\S+\.', row) or re.search('http.?://', row):
continue
# filters one word sentences
if len(row.split()) == 1:
continue
# sentences comprised of only single characters
# ^ seems to be issue with wbtools extraction pipeline
if all(len(word) < 5 for word in row.split()):
continue
row = re.sub("\( *cid *: *\d+ *\)", " ", row)
# TODO: replace this block with a regex sub
temp_row = row
for c in temp_row:
if (not c.isalnum() and not c == ' ') and c not in all_special_chars:
row = row.replace(c, "")
row = 'Line ' + str(current_i) + ': ' + row.strip()
paperid_sentence_list.append((id, row))
return paperid_sentence_list[1:]
def get_paper_sentences_with_TE(wbpids, settings):
'''
Takes WB Paper IDs, returns a list of sentences after filtering
Arg:
wbpids - List of wb papers ids
e.g. ['WBPaper00002379']
settings - Dictionary with db_config properties and texpresso token
Returns:
paperid_sentence_list: List of paper ID and sentence
e.g. [['WBPaper00002379', 'First sentence'],
['WBPaper00002379', 'Second sentence'], ....]
'''
token = settings['db_config']['textpresso']['token']
stop_words = set(nltk.corpus.stopwords.words('english'))
stop_words = [w for w in stop_words if len(w) > 1]
all_special_chars = []
with open('data/nala/train_dev.json') as f:
for jsonObj in f:
nala_json = json.loads(jsonObj)['tokens']
for word in nala_json:
if not word.isalnum():
all_special_chars.append(word)
# list of special characters to keep during inference
# helps with clearing out the bad characters from old papers
all_special_chars = list(set(all_special_chars))
paperid_sentence_list = []
for curr_ppr_i, id in enumerate(wbpids):
txt = textpresso_paper_text(id, token)
count_total_rows = len(txt)
for current_i, row in enumerate(txt):
if any([row.lower().find('literature cited') != -1,
row.lower().find('this work was supported') == 0,
row.lower().find('references') == 0,
row.lower().find('we also thank') == 0,
row.lower().find('this research was supported') == 0,
row.lower().find('we acknowledge') == 0,
row.lower().find('acknowledgments') == 0,
row.lower().find('we thank') == 0]):
if current_i > count_total_rows/3:
break
# usually is bad sentence
if len(row) < 40 or not any(word in row.lower().split() for word in stop_words):
continue
# remove sentences with links and email ids
if re.search('\S+@\S+\.', row) or re.search('www\.\S+\.', row) or re.search('http.?://', row):
continue
# filters one word sentences
if len(row.split()) == 1:
continue
# sentences comprised of only single characters
# ^ seems to be issue with wbtools extraction pipeline
if all(len(word) < 5 for word in row.split()):
continue
row = re.sub('\( *cid *: *\d+ *\)', ' ', row)
# TODO: replace this block with a regex sub
temp_row = row
for c in temp_row:
if (not c.isalnum() and not c == ' ') and c not in all_special_chars:
row = row.replace(c, "")
paperid_sentence_list.append((id, row))
return paperid_sentence_list # [1:]
def findVariants(settings, paper_ids, method):
''' find variants in papers '''
paperid_sentence_list = []
if method == 'wbtools':
paperid_sentence_list = get_paper_sentences_with_wbtools(paper_ids, settings)
elif method == 'textpresso':
paperid_sentence_list = get_paper_sentences_with_TE(paper_ids, settings)
custom_mut_extract = settings['custom_mut_extract']
tokenClassificationPipeline = settings['nala_ner']
stop_words = settings['stop_words']
# remove duplicates keeping the order
seen = set()
paperid_sentence_list = np.array([x for x in paperid_sentence_list if x not in seen and not seen.add(x)])
final = [
['temporary', 'temporary', 'temporary', 'temporary', 'temporary', 'temporary'],\
['WBPaper ID', 'Method', '*Genes', '*Gene-Variant combo', 'Mutation', 'Sentence']]
total_sentences = len(paperid_sentence_list)
print_snips = False
for ppr_sen_idx, row in enumerate(paperid_sentence_list):
if (ppr_sen_idx+1) % 50 == 0:
print(f"{ppr_sen_idx+1}>{len(final)-1}", end=" ")
paper_id = row[0]
sentence = str()
# traverse upto 2 sentences at a time (if they aren't super long)
# this should be removed if table content thing from wbtools is ever fixed
# or else, it might group lines of table which will result in higher false positive count
limit = min(ppr_sen_idx+2, total_sentences)
# some sentences - mostly table content are all in a single sentence
# temp fix, need to have a nice sentence splitter to minimize manual verification time
not_single_sentence = False
for i in range(ppr_sen_idx, limit):
sentence = sentence + paperid_sentence_list[i][1] + ' '
raw_sentence = sentence
if (len(sentence) > 250 and not_single_sentence):
break
if paper_id != paperid_sentence_list[i][0]:
break
var_plus_genes = ''
# Look for the special data e.g. gene-variant combo (e.g 'ced-3(n2888)') only on single sentences
if not not_single_sentence:
var_plus_genes = []
all_genes = []
genome_versions = []
annotation_versions = []
for data_and_cat in custom_mut_extract.var_and_gene_close(sentence.strip()):
var_plus_genes.append(data_and_cat[0])
if var_plus_genes:
var_plus_genes = list(set(var_plus_genes))
var_plus_genes = "'" + "', '".join(var_plus_genes) + "'"
else:
var_plus_genes = ''
for data_and_cat in custom_mut_extract.get_genes(sentence.strip()):
all_genes.append(data_and_cat[0])
if all_genes:
all_genes = list(set(all_genes))
# Removing gene mentions from sentence
# e.g. "lysine 36 by MET-1/Set2" regex will classify this as protein mutation
# but MET-1 is a gene name so the mutation isn't valid
for gene in all_genes:
sentence = sentence.replace(gene, "")
all_genes = "'" + "', '".join(all_genes) + "'"
else:
all_genes = ''
output = regex_block(settings, sentence.strip())
if output:
mutations = []
for mut_and_snip in output:
# temp fix to deal with same mutation getting detected due to stiching multiple sentences
if ((mut_and_snip[0] not in final[-1][4][1:-1].split(", ") and
mut_and_snip[0] not in final[-2][4][1:-1].split(", ")) and
mut_and_snip[0] not in mutations):
mutations.append(mut_and_snip[0])
if mutations:
mutations = "'" + "', '".join(mutations) + "'"
if print_snips:
print(1, mutations)
final.append([paper_id, 'Regex', all_genes, var_plus_genes, mutations, raw_sentence.strip()])
break
output = ner_mutations(tokenClassificationPipeline, stop_words, sentence.strip())
if output:
mutations = []
for mut_and_snip in output:
# temp fix to deal with same mutation getting detected due to stiching multiple sentences
if ((mut_and_snip[0] not in final[-1][4][1:-1].split(", ") and
mut_and_snip[0] not in final[-2][4][1:-1].split(", ")) and
not all(len(word) < 4 for word in mut_and_snip[0].split()) and
mut_and_snip[0] not in mutations):
mutations.append(mut_and_snip[0])
if mutations:
mutations = "'" + "', '".join(mutations) + "'"
if print_snips:
print(2, mutations)
final.append([paper_id, 'NER', all_genes, var_plus_genes, mutations, raw_sentence.strip()])
break
# these data, if found, are going to be important if no mutations are in that sentence
if var_plus_genes or all_genes:
final.append([paper_id, '', all_genes, var_plus_genes, '', raw_sentence.strip()])
not_single_sentence = True
temp = final[2:] # removing the temporary first row and header
# this sheet will contain high number of duplicates
# which will get filtered in the refinement process
# columns with asterisk contain data which are useful
# regardless of whether the sentence has mutation info
return pd.DataFrame(
temp[:],
columns=['WBPaper ID', 'Method', '* Genes', '* Gene-Variant combo',
'Mutation', 'Sentence'])
if __name__ == "__main__":
from settings import setSettings
settings = setSettings()
wbpids = ['WBPaper00002627', 'WBPaper00006391']
df = findVariants(settings, wbpids, 'wbtools')
df.to_csv('variants.csv', index=False)