-
Notifications
You must be signed in to change notification settings - Fork 2
/
applyModelsToSentences.py
349 lines (271 loc) · 11.9 KB
/
applyModelsToSentences.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
import sys
import itertools
import kindred
import pickle
import argparse
import codecs
import time
import re
import string
from collections import defaultdict,Counter
import json
import html
def now():
return time.strftime("%Y-%m-%d %H:%M:%S")
def getNormalizedTerm(text,externalID,IDToTerm):
normalizedTerms = [ IDToTerm[eid] for eid in externalID.split(';') ]
normalizedTerms = sorted(list(set(normalizedTerms)))
normalizedTermsLower = [ st.lower() for st in normalizedTerms ]
textLower = text.lower()
if textLower in normalizedTermsLower:
index = normalizedTermsLower.index(textLower)
normalizedTerm = normalizedTerms[index]
else:
normalizedTerm = ";".join(normalizedTerms)
return normalizedTerm
def normalizeMIRName(externalID):
assert externalID.startswith('mirna|'), "Unexpected ID: %s" % externalID
normalizedName = externalID[6:]
search = re.search('mirna\|\D*(?P<id>\d+[A-Za-z]*)',externalID)
if search:
mirID = search.groupdict()['id']
if not mirID is None:
normalizedName = "miR-%s" % mirID
return normalizedName
def getFormattedSentence(sentence,entitiesToHighlight):
charArray = [ html.escape(c) for c in sentence.text ]
sentenceStart = sentence.tokens[0].startPos
for e in entitiesToHighlight:
for startPos,endPos in e.position:
startPos -= sentenceStart
endPos -= sentenceStart
try:
charArray[startPos] = '<b>' + charArray[startPos]
charArray[endPos-1] = charArray[endPos-1] + '</b>'
except:
print("ERROR in getFormattedSentence")
print(doc.text)
print(e.text)
print(e.position)
sys.exit(1)
return "".join(charArray)
headers = ['pmid','title','journal','journal_short','year','month','day','section','subsection','evidencetype','evidencetype_prob','cancer_id','cancer_text','cancer_normalized','cancer_start','cancer_end','gene_hugo_id','gene_entrez_id','gene_text','gene_normalized','gene_start','gene_end','drug_id','drug_text','drug_normalized','drug_start','drug_end','variant_id','variant_text','variant_normalized','variant_start','variant_end','variant_prob','sentence','formatted_sentence']
def applyFinalFilter(row):
# Filter out incorrect output with some rules
assert len(row) == len(headers), "Number of columns in output data (%d) doesn't match with header count (%d)" % (len(row),len(headers))
row = { h:v for h,v in zip(headers,row) }
# Check for the number of semicolons (suggesting a list)
if row['sentence'].count(';') > 5:
return False
if row['section'] == 'back':
return False
# Filter some erroneous variant associations for very common substitutions
expectedVariantAssociations = {'R399Q': 'XRCC1', 'R194W': 'XRCC1', 'T241M': 'XRCC3', 'V600E': 'BRAF', 'T790M': 'EGFR', 'L858R': 'EGFR'}
if row['variant_normalized'] == 'substitution':
substitution = row['variant_id'].split('|')[1]
if substitution in expectedVariantAssociations and not row['gene_normalized'] == expectedVariantAssociations[substitution]:
return False
return True
def civicmine(sentenceFile,modelFilenames,filterTerms,wordlistPickle,genes,cancerTypes,drugs,variants,variantStopwordsFile,outData,verbose=False):
if verbose:
print("%s : start" % now())
thresholds = {}
thresholds['AssociatedVariant'] = 0.7
with open(variantStopwordsFile) as f:
variantStopwords = [ line.strip() for line in f ]
models = {}
assert isinstance(modelFilenames,list)
for modelFilename in modelFilenames:
with open(modelFilename,'rb') as f:
models[modelFilename] = pickle.load(f)
IDToTerm = {}
HugoToEntrez = defaultdict(lambda : "N/A")
with codecs.open(genes,'r','utf-8') as f:
for line in f:
geneid,singleterm,_,entrez_geneid = line.strip().split('\t')
IDToTerm[geneid] = singleterm
HugoToEntrez[geneid] = entrez_geneid
with codecs.open(cancerTypes,'r','utf-8') as f:
for line in f:
cancerid,singleterm,_ = line.strip().split('\t')
IDToTerm[cancerid] = singleterm
with codecs.open(drugs,'r','utf-8') as f:
for line in f:
drugid,singleterm,_ = line.strip().split('\t')
IDToTerm[drugid] = singleterm
with codecs.open(variants,'r','utf-8') as f:
for line in f:
variantid,singleterm,_ = line.strip().split('\t')
IDToTerm[variantid] = singleterm
with codecs.open(filterTerms,'r','utf-8') as f:
filterTerms = [ line.strip().lower() for line in f ]
with open(wordlistPickle,'rb') as f:
termLookup = pickle.load(f)
# Truncate the output file
with codecs.open(outData,'w','utf-8') as outF:
pass
timers = Counter()
if verbose:
print("%s : loading..." % now())
with open(sentenceFile) as f:
sentenceData = json.load(f)
corpus = kindred.Corpus()
for sentence in sentenceData:
metadata = dict(sentence)
del metadata["sentence"]
doc = kindred.Document(sentence["sentence"],metadata=metadata)
corpus.addDocument(doc)
if verbose:
print("%s : loaded..." % now())
startTime = time.time()
parser = kindred.Parser(model='en_core_sci_sm')
parser.parse(corpus)
timers['parser'] += time.time() - startTime
if verbose:
print("%s : parsed" % now())
startTime = time.time()
ner = kindred.EntityRecognizer(lookup=termLookup,detectVariants=True,variantStopwords=variantStopwords,detectFusionGenes=True,detectMicroRNA=True,acronymDetectionForAmbiguity=True,mergeTerms=True,removePathways=True)
ner.annotate(corpus)
timers['ner'] += time.time() - startTime
if verbose:
print("%s : ner" % now())
with codecs.open(outData,'a','utf-8') as outF:
outF.write("\t".join(headers) + "\n")
startTime = time.time()
for modelname,model in models.items():
model.predict(corpus)
timers['predicted'] += time.time() - startTime
if verbose:
print("%s : predicted" % now())
startTime = time.time()
for doc in corpus.documents:
# Skip if no relations are found
if len(doc.relations) == 0:
continue
# Skip if there isn't an associated PMID
if not doc.metadata["pmid"]:
continue
journal_short = str(doc.metadata['journal'])
if len(journal_short) > 50:
journal_short = journal_short[:50] + '...'
entity_to_sentence = {}
for sentence in doc.sentences:
for entity,tokenIndices in sentence.entityAnnotations:
entity_to_sentence[entity] = sentence
# Remove entities with ambigious entities (with ; as ID delimiter, or & for combos/fusions)
doc.relations = [ r for r in doc.relations if not any ( [';' in e.externalID for e in r.entities] ) ]
doc.relations = [ r for r in doc.relations if not any ( [ e.externalID.startswith('combo|') and '&' in e.externalID for e in r.entities] ) ]
geneID2Variant = defaultdict(list)
for relation in doc.relations:
# We're only dealing with Variants in this loop
if relation.relationType != 'AssociatedVariant':
continue
typeToEntity = {}
for entity in relation.entities:
typeToEntity[entity.entityType] = entity
geneID = typeToEntity['gene'].entityID
v = typeToEntity['variant']
prob = relation.probability
if prob > thresholds['AssociatedVariant']:
#variant = (typeToEntity['variant'].externalID,typeToEntity['variant'].text,)
geneID2Variant[geneID].append((v,prob))
for relation in doc.relations:
# IgnoreVariant as we deal with them seperately (above)
if relation.relationType == 'AssociatedVariant':
continue
#print(relation)
sentence = entity_to_sentence[relation.entities[0]]
sentenceTextLower = sentence.text.lower()
hasFilterTerm = any( filterTerm in sentenceTextLower for filterTerm in filterTerms )
if not hasFilterTerm:
continue
#words = [ t.word for t in sentence.tokens ]
#text = " ".join(words)
sentenceStart = sentence.tokens[0].startPos
skip = False
relType = relation.relationType
entityData = {'cancer':['' for _ in range(5)], 'drug':['' for _ in range(5)], 'gene':['' for _ in range(5)] }
geneID = None
for entity in relation.entities:
startPos,endPos = entity.position[0]
if entity.entityType == 'gene':
assert geneID is None, 'Relation should only contain a single gene'
geneID = entity.entityID
afterText = doc.text[endPos:].strip()
if afterText.startswith('-AS') or afterText.startswith('et al'):
skip = True
if entity.externalID.startswith('combo'):
externalIDsplit = entity.externalID.split('|')
normalizedTerms = [ getNormalizedTerm("",st.replace('&',';'),IDToTerm) for st in externalIDsplit[1:] ]
normalizedTerm = "|".join(normalizedTerms)
elif entity.externalID.startswith('mirna|'):
normalizedTerm = normalizeMIRName(entity.externalID)
else:
normalizedTerm = getNormalizedTerm(entity.text,entity.externalID,IDToTerm)
assert len(entity.position) == 1, "Expecting entities that are contigious and have only one start and end position within the text"
tmp = []
tmp.append(entity.externalID)
if entity.entityType == 'gene':
tmp.append(HugoToEntrez[entity.externalID])
tmp.append(entity.text)
tmp.append(normalizedTerm)
tmp.append(startPos - sentenceStart)
tmp.append(endPos - sentenceStart)
entityData[entity.entityType] = tmp
if skip:
continue
associatedVariants = []
if geneID in geneID2Variant:
associatedVariants = geneID2Variant[geneID]
else:
associatedVariants = [ (None,None) ]
for associatedVariantEntity,variantProb in associatedVariants:
associatedVariant = ['' for _ in range(6)]
if associatedVariantEntity is not None:
startPos,endPos = associatedVariantEntity.position[0]
if associatedVariantEntity.externalID.startswith('substitution|'):
normalizedTerm = 'substitution'
else:
normalizedTerm = getNormalizedTerm(associatedVariantEntity.text,associatedVariantEntity.externalID,IDToTerm)
associatedVariant = []
associatedVariant.append(associatedVariantEntity.externalID)
associatedVariant.append(associatedVariantEntity.text)
associatedVariant.append(normalizedTerm)
associatedVariant.append(startPos - sentenceStart)
associatedVariant.append(endPos - sentenceStart)
associatedVariant.append(variantProb)
m = doc.metadata
if not 'subsection' in m:
m['subsection'] = None
combinedEntities = relation.entities + ([ associatedVariantEntity ] if associatedVariantEntity else [])
formattedSentence = getFormattedSentence(sentence, combinedEntities )
prob = relation.probability
combinedEntityData = entityData['cancer'] + entityData['gene'] + entityData['drug'] + associatedVariant
outData = [m['pmid'],m['title'],m['journal'],journal_short,m['year'],m['month'],m['day'],m['section'],m['subsection'],relType,prob] + combinedEntityData + [sentence.text, formattedSentence]
if applyFinalFilter(outData):
outLine = "\t".join(map(str,outData))
outF.write(outLine+"\n")
timers['output'] += time.time() - startTime
if verbose:
print("%s : output" % now())
sys.stdout.flush()
if verbose:
print("%s : done" % now())
for section,sectiontime in timers.items():
print("%s\t%f" % (section,sectiontime))
print("%s\t%f" % ("applyModelsToSentences total", sum(timers.values())))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Finds relations in Pubmed file')
parser.add_argument('--sentenceFile',required=True,help='BioC XML file to use')
parser.add_argument('--models',required=True)
parser.add_argument('--filterTerms',required=True)
parser.add_argument('--wordlistPickle',required=True)
parser.add_argument('--genes',required=True)
parser.add_argument('--cancerTypes',required=True)
parser.add_argument('--drugs',required=True)
parser.add_argument('--variants',required=True)
parser.add_argument('--variantStopwords',required=True,type=str,help='File of variants to skip')
parser.add_argument('--outData',required=True)
parser.add_argument('--verbose', action='store_true', help='Whether to print out information about run')
args = parser.parse_args()
civicmine(args.sentenceFile,args.models.split(','),args.filterTerms,args.wordlistPickle,args.genes,args.cancerTypes,args.drugs,args.variants,args.variantStopwords,args.outData,verbose=args.verbose)