-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapplyModelsToSentences.py
259 lines (194 loc) · 7.79 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
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[4:]
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','role','predictprob','cancer_id','cancer_name','cancer_normalized','cancer_start','cancer_end','gene_hugo_id','gene_entrez_id','gene_name','gene_normalized','gene_start','gene_end','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
return True
def cancermine(sentenceFile,modelFilenames,filterTerms,wordlistPickle,genes,cancerTypes,outData):
print("%s : start" % now())
models = {}
assert isinstance(modelFilenames,list)
for modelFilename in modelFilenames:
with open(modelFilename,'rb') as f:
models[modelFilename] = pickle.load(f)
IDToTerm = {}
Hugo2Entrez = defaultdict(lambda : 'NA')
with codecs.open(genes,'r','utf-8') as f:
for line in f:
gene_hugo_id,singleterm,_,gene_entrez_id = line.strip().split('\t')
IDToTerm[gene_hugo_id] = singleterm
Hugo2Entrez[gene_hugo_id] = gene_entrez_id
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(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()
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)
print("%s : loaded..." % now())
startTime = time.time()
parser = kindred.Parser()
parser.parse(corpus)
timers['parser'] += time.time() - startTime
print("%s : parsed" % now())
startTime = time.time()
ner = kindred.EntityRecognizer(lookup=termLookup,detectFusionGenes=False,detectMicroRNA=False,acronymDetectionForAmbiguity=True,mergeTerms=True,removePathways=True)
ner.annotate(corpus)
timers['ner'] += time.time() - startTime
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
print("%s : predicted" % now())
startTime = time.time()
for doc in corpus.documents:
#print(doc)
if len(doc.relations) == 0:
continue
if not doc.metadata["pmid"]:
continue
entity_to_sentence = {}
for sentence in doc.sentences:
for entity,tokenIndices in sentence.entityAnnotations:
assert not entity in entity_to_sentence
entity_to_sentence[entity] = sentence
journal_short = doc.metadata['journal']
if journal_short and len(journal_short) > 50:
journal_short = journal_short[:50] + '...'
for relation in doc.relations:
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)
entitiesAreAmbiguous = any ( [';' in e.externalID for e in relation.entities] )
if entitiesAreAmbiguous:
continue
sentenceStart = sentence.tokens[0].startPos
skip = False
relType = relation.relationType
entityData = []
for entity in relation.entities:
entityData.append(entity.externalID)
startPos,endPos = entity.position[0]
if entity.entityType == 'gene':
entityData.append(Hugo2Entrez[entity.externalID])
afterText = doc.text[endPos:].strip()
if afterText.startswith('-AS'):
skip = True
entityData.append(entity.text)
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)
entityData.append(normalizedTerm)
assert len(entity.position) == 1, "Expecting entities that are contigious and have only one start and end position within the text"
entityData.append(startPos - sentenceStart)
entityData.append(endPos - sentenceStart)
if skip:
continue
m = doc.metadata
if not 'subsection' in m:
m['subsection'] = None
formattedSentence = getFormattedSentence(sentence,relation.entities)
prob = relation.probability
outData = [m['pmid'],m['title'],m["journal"],journal_short,m["year"],m["month"],m["day"],m['section'],m['subsection'],relType,prob] + entityData + [sentence.text, formattedSentence]
if applyFinalFilter(outData):
outLine = "\t".join(map(str,outData))
outF.write(outLine+"\n")
timers['output'] += time.time() - startTime
print("%s : output" % now())
sys.stdout.flush()
print("%s : done" % now())
for section,sectiontime in timers.items():
print("%s\t%f" % (section,sectiontime))
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('--outData',required=True)
args = parser.parse_args()
cancermine(args.sentenceFile,args.models.split(','),args.filterTerms,args.wordlistPickle,args.genes,args.cancerTypes,args.outData)