/
abstractsummary.py
executable file
·315 lines (261 loc) · 14.6 KB
/
abstractsummary.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
#!/usr/bin/python
# author: Rodney Summerscales
"""
EBM summary for an abstract
"""
import xmlutil
import time
import xml.dom.minidom
import StringIO
import demographicelements
import subjectlist
import outcomelist
import htmlutil
class Summary:
""" Summary for a given abstract. All mentions, quantities should be found
and associated. """
abstract = None # abstract for which this is a summary
subjectList = None # list of groups w/descriptions
outcomeList = None # list of outcomes measured in abstract
locationList = None # list of locations where study is performed
summaryStatsList = None # list of summary statistics in abstract
randomized = False # is the abstract describing an RCT?
randomizedTerms = {'randomized', 'randomised', 'randomly', 'randomization',
'randomisation'}
useTrialReports = False
def __init__(self, abstract, useAnnotated=False, useTrialReports=False):
""" build EBM related summary for a given abstract object """
self.abstract = abstract
self.useTrialReports = useTrialReports
self.subjectList = subjectlist.SubjectList(abstract, useAnnotated, self.useTrialReports)
self.outcomeList = outcomelist.OutcomeList(abstract, useAnnotated, self.useTrialReports)
self.locationList = demographicelements.LocationList(abstract, self.useTrialReports)
self.summaryStatList = None
self.randomized = False
for s in abstract.titleSentences:
if self.containsRandomized(s):
self.randomized = True
break
if self.randomized is False:
for s in abstract.allSentences():
if self.containsRandomized(s):
self.randomized = True
break
def getEvaluationStrings(self, version):
""" Return list of SQL strings containing summary element evaluations.
These strings are part of SQL insert command into the answer table in EBM db.
"""
eStrings = []
tString = time.strftime("%a, %d %b %Y %H:%M:%S")
for av in self.subjectList.ageInfo.ageValues.values():
eStrings.append(self.sqlEvaluationString('Age', av.evaluation.id, av.evaluation.rating,
version, tString))
for cTemplate in self.subjectList.conditionTemplates:
eStrings.append(self.sqlEvaluationString('Condition', cTemplate.evaluation.id,
cTemplate.evaluation.rating, version, tString))
for gTemplate in self.subjectList.groupTemplates:
eStrings.append(self.sqlEvaluationString('Group', gTemplate.evaluation.id,
gTemplate.evaluation.rating, version, tString))
if gTemplate.groupSizeEvaluation.isComplete():
eStrings.append(self.sqlEvaluationString('Group_Size', gTemplate.groupSizeEvaluation.id,
gTemplate.groupSizeEvaluation.rating, version, tString))
for oTemplate in self.outcomeList.outcomeTemplates:
eStrings.append(self.sqlEvaluationString('Outcome', oTemplate.evaluation.id,
oTemplate.evaluation.rating, version, tString))
if oTemplate.primaryOutcomeEvaluation.isComplete():
eStrings.append(self.sqlEvaluationString('Primary_Outcome',
oTemplate.primaryOutcomeEvaluation.id,
oTemplate.primaryOutcomeEvaluation.rating, version, tString))
for ssTemplate in oTemplate.summaryStats:
eStrings.append(self.sqlEvaluationString('Endpoint', ssTemplate.evaluation.id,
ssTemplate.evaluation.rating, version, tString))
eStrings.append(self.sqlEvaluationString('actual_Age', 'Actual_Ages_no.s',
self.subjectList.ageInfo.nTrueAgeValues, version, tString))
eStrings.append(self.sqlEvaluationString('actual_Condition', 'Actual_Condition_no.s',
self.subjectList.nTrueConditions, version, tString))
eStrings.append(self.sqlEvaluationString('actual_Group', 'Actual_Group_no.s',
self.subjectList.nTrueGroups, version, tString))
eStrings.append(self.sqlEvaluationString('actual_Outcome', 'Actual_Outcomes_no.s',
self.outcomeList.nTrueOutcomes, version, tString))
eStrings.append(self.sqlEvaluationString('actual_Endpoint', 'Actual_ARR_no.s',
len(self.abstract.trueSummaryStats.stats), version, tString))
return eStrings
def sqlEvaluationString(self, elementType, elementId, answerDesc, version, timeString):
""" return SQL element evaluation string """
eString = '(%s, \'%s\', \'%s\', \'%s\', \'%s\',\'%s\', \'%s\')' \
% (self.abstract.id, version, elementType, elementId, answerDesc, 'auto', timeString)
return eString
def getElementStrings(self):
""" Return list of SQL strings containing evaluated summary element values.
These strings are part of SQL insert command into the element table in EBM db.
"""
eStrings = []
for av in self.subjectList.ageInfo.ageValues.values():
eStrings.append(self.sqlElementString(av.evaluation.id, av.toString(), None, False))
for cTemplate in self.subjectList.conditionTemplates:
eStrings.append(self.sqlElementString(cTemplate.evaluation.id,
cTemplate.toString(), cTemplate.matchedTemplate,
cTemplate.exactMatch))
for gTemplate in self.subjectList.groupTemplates:
eStrings.append(self.sqlElementString(gTemplate.evaluation.id,
gTemplate.toString(), gTemplate.matchedTemplate,
gTemplate.exactMatch))
if gTemplate.groupSizeEvaluation.isComplete():
eStrings.append(self.sqlElementString(gTemplate.groupSizeEvaluation.id,
gTemplate.getSize(maxSize=True), None, False))
for oTemplate in self.outcomeList.outcomeTemplates:
eStrings.append(self.sqlElementString(oTemplate.evaluation.id,
oTemplate.toString(), oTemplate.matchedTemplate,
oTemplate.exactMatch))
if oTemplate.primaryOutcomeEvaluation.isComplete():
eStrings.append(self.sqlElementString(oTemplate.primaryOutcomeEvaluation.id,
oTemplate.toString(), None, False))
for ssTemplate in oTemplate.summaryStats:
eStrings.append(self.sqlElementString(ssTemplate.evaluation.id,
ssTemplate.toString(), ssTemplate.matchingStat,
ssTemplate.correctlyMatched))
return eStrings
@staticmethod
def sqlElementString(elementId, elementDescription, matchingElement, exactMatch):
""" return SQL element description string """
if matchingElement is None:
eString = '(\'%s\', \'%s\', NULL, %s)' % (elementId, elementDescription, exactMatch)
else:
eString = '(\'%s\', \'%s\', \'%s\', %s)' \
% (elementId, elementDescription, matchingElement.toString(), exactMatch)
return eString
def containsRandomized(self, sentence):
""" return True if given sentence contains language that implies that the study
was randomized """
for token in sentence:
if token.text.lower() in self.randomizedTerms:
return True
return False
def writeXML(self, path, version):
""" Write summary to XML file in the destination directory specified
by path. """
idPrefix = self.abstract.id + 'v' + version
# build XML summary
doc = xml.dom.minidom.Document()
articleNode = doc.createElement('Study')
if self.randomized:
articleNode.setAttribute('RandomizationPresent', 'true')
else:
articleNode.setAttribute('RandomizationPresent', 'false')
articleNode.setAttribute('Version', version)
gmTime = time.gmtime()
cNode = doc.createElement('Created')
articleNode.appendChild(cNode)
cNode.appendChild(xmlutil.createNodeWithTextChild(doc, 'Month', str(gmTime.tm_mon)))
cNode.appendChild(xmlutil.createNodeWithTextChild(doc, 'Day', str(gmTime.tm_mday)))
cNode.appendChild(xmlutil.createNodeWithTextChild(doc, 'Year', str(gmTime.tm_year)))
nameNode = xmlutil.createNodeWithTextChild(doc, 'Name', self.abstract.id)
articleNode.appendChild(nameNode)
titleString = ''
for s in self.abstract.titleSentences:
titleString += s.toString()
titleNode = xmlutil.createNodeWithTextChild(doc, 'Title', titleString)
articleNode.appendChild(titleNode)
link = 'http://www.ncbi.nlm.nih.gov/pubmed/' + self.abstract.id
linkNode = xmlutil.createNodeWithTextChild(doc, 'AbstractLink', link)
articleNode.appendChild(linkNode)
if self.abstract.report is not None and self.abstract.report.id[0:3] == 'NCT':
link = 'http://clinicaltrials.gov/ct2/show/study/' + self.abstract.report.id
linkNode = xmlutil.createNodeWithTextChild(doc, 'TrialRegistryLink', link)
articleNode.appendChild(linkNode)
node = self.locationList.getXML(doc, idPrefix)
if node is not None:
articleNode.appendChild(node)
if self.useTrialReports and self.abstract.report is not None \
and len(self.abstract.report.conditions) > 0:
cListNode = doc.createElement('ConditionsOfInterest')
articleNode.appendChild(cListNode)
for condition in self.abstract.report.conditions:
cNode = doc.createElement('Condition')
cNode.appendChild(xmlutil.createNodeWithTextChild(doc, 'Name',
condition.sentences[0].toString()))
cNode.setAttribute('source', 'trial_registry')
cListNode.appendChild(cNode)
node = self.subjectList.getXML(doc, idPrefix)
if node is not None:
articleNode.appendChild(node)
node = self.outcomeList.getXML(doc, idPrefix)
if node is not None:
articleNode.appendChild(node)
if self.abstract.meshHeadingList is not None:
articleNode.appendChild(self.abstract.meshHeadingList.getXML(doc))
htmlString = StringIO.StringIO()
self.writeHTML(htmlString, showError=False)
htmlNode = xmlutil.createNodeWithTextChild(doc, 'HTMLData', htmlString.getvalue())
articleNode.appendChild(htmlNode)
htmlString.close()
# write summary to XML file
filename = path + self.abstract.id + '.summary.xml'
out = open(filename, 'w')
out.write('<?xml version="1.0" encoding="utf-8"?>\n')
# out.write('<?xml-stylesheet href="abstract.xsl" type="text/xsl"?>\n')
xmlutil.writexml(articleNode, out)
out.close()
def writeHTML(self, out, includeReport=False, showError=True):
""" write summary in html format to given output stream. """
htmlutil.HTMLBeginTable(out, classStyle='publicationinfotable')
htmlutil.HTMLBeginRow(out)
link = 'http://www.ncbi.nlm.nih.gov/pubmed/' + self.abstract.id
out.write('<h2><a href="%s">%s:</a></h2>\n' % (link, self.abstract.id))
htmlutil.HTMLColumnBreak(out)
titleString = ''
for s in self.abstract.titleSentences:
titleString += s.toPrettyString()
out.write('<h2>' + titleString + '</h2>')
htmlutil.HTMLEndRow(out)
htmlutil.HTMLBeginRow(out)
htmlutil.HTMLBeginRow(out)
htmlutil.HTMLWriteText(out,'Authors:', bold=True)
htmlutil.HTMLColumnBreak(out)
authorList = self.abstract.publicationInformation.getAuthors()
authorString = ', '.join(authorList)
out.write(authorString)
htmlutil.HTMLEndRow(out)
htmlutil.HTMLBeginRow(out)
htmlutil.HTMLWriteText(out,'Affiliation:', bold=True)
htmlutil.HTMLColumnBreak(out)
for affiliationSentence in self.abstract.affiliationSentences:
out.write(affiliationSentence.toPrettyString()+' ')
htmlutil.HTMLEndRow(out)
htmlutil.HTMLBeginRow(out)
htmlutil.HTMLWriteText(out,'Journal:', bold=True)
htmlutil.HTMLColumnBreak(out)
out.write(self.abstract.publicationInformation.getJournal())
htmlutil.HTMLEndRow(out)
htmlutil.HTMLBeginRow(out)
htmlutil.HTMLWriteText(out,'J. Country:', bold=True)
htmlutil.HTMLColumnBreak(out)
country = self.abstract.publicationInformation.getCountry()
out.write(country)
htmlutil.HTMLEndRow(out)
htmlutil.HTMLBeginRow(out)
htmlutil.HTMLWriteText(out,'Year pub:', bold=True)
htmlutil.HTMLColumnBreak(out)
out.write(self.abstract.publicationInformation.getPublicationDate())
htmlutil.HTMLEndRow(out)
htmlutil.HTMLBeginRow(out)
htmlutil.HTMLWriteText(out,'Pub. types:', bold=True)
htmlutil.HTMLColumnBreak(out)
pTypeList = self.abstract.publicationInformation.getPublicationTypes()
for pType in pTypeList:
htmlutil.HTMLWriteText(out, pType+'<br>')
htmlutil.HTMLEndRow(out)
htmlutil.HTMLEndTable(out)
# output abstract
htmlutil.HTMLBeginTable(out, classStyle='abstractsummarytable')
htmlutil.HTMLBeginRow(out, firstColumnWidth='60%', firstColumnBordersOn=False)
self.abstract.writeHTML(out, ['group', 'outcome', 'condition',
'eventrate', 'on', 'gs', 'cost_value'], showError)
# self.abstract.writeHTML(out)
htmlutil.HTMLColumnBreak(out, width='40%', nextColumnBordersOn=True)
# output summary
self.subjectList.writeHTML(out)
self.outcomeList.writeHTML(out, showError)
htmlutil.HTMLEndRow(out)
htmlutil.HTMLEndTable(out)
out.write('<br><br>')