-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtcga-hl7.py
More file actions
283 lines (248 loc) · 7.85 KB
/
Copy pathtcga-hl7.py
File metadata and controls
283 lines (248 loc) · 7.85 KB
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
from lxml import etree
from lxml import objectify
import xml.etree.ElementTree as et
import os, csv
import sys
import re
import argparse
import requests
import unicodedata
headers = []
data = {}
config = False
cde = {}
disease = ""
def parseit(xmlfile):
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse(xmlfile, parser)
try:
root = tree.getroot()
patRoot = '{' + root.nsmap[disease] + '}patient'
adminRoot = root.nsmap['admin']
topRoot = 'tcga_bcr'
nteRoot = '{' + root.nsmap[disease + '_nte'] + '}'
nte = '{' + root.nsmap['nte'] + '}'
for elem in root.iter():
#if not hasattr(elem.tag, 'find'): continue # (1)
#i = elem.tag.find('}')
#print(elem.tag)
itop = elem.tag.find(topRoot)
iadmin = elem.tag.find(adminRoot)
ipatroot = elem.tag.find(patRoot)
inteRoot = elem.tag.find(nteRoot)
inte = elem.tag.find(nte)
if itop < 0 and iadmin < 0 and ipatroot < 0 and inteRoot < 0 and inte < 0: # skip the tcga_bcr and admin levels
i = elem.tag.find('}')
elem.tag = elem.tag[i+1:]
if elem.tag and elem.text:
#print (elem.tag + " " + elem.text )
headers.append(elem.tag)
try:
pubid = elem.attrib['cde']
except Exception as e:
pubid = None
if elem.text.find(',') > 0:
et = '"' + elem.text + '"'
else:
et = elem.text
data[elem.tag] = et
cde[elem.tag] = pubid
return True
except Exception as e:
return False
def writecsv(csvfile):
if os.path.exists(csvfile):
os.remove(csvfile)
tcgafile = open(csvfile, 'w')
for h in headers:
tcgafile.write(h + ',')
tcgafile.write('\n')
for d in headers:
tcgafile.write(data[d] + ',')
tcgafile.write('\n')
tcgafile.close()
def writecfg(csvfile):
if os.path.exists(csvfile):
os.remove(csvfile)
tcgafile = open(csvfile, 'w')
i = 1
for h in headers:
pubid = cde[h]
label = ''
if pubid:
cdemeta = getCDEMeta(pubid)
if cdemeta:
label = cdemeta['label']
if label:
line = label + ',' + h +',,' + pubid # pretty name + the field name
tcgafile.write(line)
tcgafile.write('\n')
i = i + 1
tcgafile.close()
def getCDEMeta(cde):
metadata = {}
label = ''
definition = ''
url = "https://cadsrapi.nci.nih.gov/cadsrapi4/GetXML"
querystring = {"query":"DataElement[@publicId=" + cde + "]"}
xheaders = {
'cache-control': "no-cache",
'postman-token': "c50954f2-0591-a421-66ed-e969866bf5a4"
}
response = requests.request("GET", url, headers=xheaders, params=querystring)
if response.status_code == 200:
# must use this because CDE api gives it back XML as a string with unicode (lxml doesn't support it)
try:
root = et.fromstring(response.text)
for x in root.iter():
if x.tag == 'field':
if x.attrib['name'] == 'longName':
label = x.text
if x.attrib['name'] == 'preferredDefinition':
definition = x.text
if label and definition:
metadata = {'label': label, 'definition': definition, 'cde':cde}
except Exception as e:
print(e)
raise e
return metadata
def writehl7(hlfile):
if os.path.exists(hlfile):
os.remove(hlfile)
tcgafile = open(hlfile, 'w')
# write MSH
tcgafile.write(msh())
tcgafile.write('\n')
# write PID
tcgafile.write(pid())
tcgafile.write('\n')
# write OBR
tcgafile.write(obr())
tcgafile.write('\n')
# write OBX
obxs = obx(data['bcr_patient_barcode']) # get each line of obx as single obx line
if obx:
for x in obxs:
try:
tcgafile.write(str(x))
tcgafile.write('\n')
except Exception:
pass
# tcgafile.write(obx2(data['bcr_patient_barcode'])) this was for a simple block of text
tcgafile.close()
# HL7 message Header
def msh():
return "MSH|^~&|Python||TCGA||||ORU^R01|Q89864576T91907826|P|2.3"
# HL7 Patient info
def pid():
return "PID|1||" + data['bcr_patient_barcode'] + "||" + data['bcr_patient_barcode'] + "^" + \
data['bcr_patient_barcode'] + "||20000101|"+data['gender'] +"||"+data['race'].title() +"||||||||||" + data['bcr_patient_barcode']
# HL7 Observation Request
def obr():
return "OBR|||" + data['bcr_patient_barcode'] + "^TCGAReports^20180101|DIAGNOSIS:^PATHOLOGY|||201801081056|||||||||||||" + data['bcr_patient_barcode'] + "||201801081056|||F"
#HL7 Observation Results
def obx(whichId):
reportText = []
with open('../reports/report_manifest.txt', 'r') as csvfile:
mline = csv.reader(csvfile, delimiter='\t')
i = 0;
for row in mline:
if i > 0:
file = row[1]
parts = file.split(".") # arse filename becauseit contains the tcgaid
tcgaid = parts[0] # part 0 is tcga id
if tcgaid == whichId:
#filePath = '../reports/' + row[0] + '/' + file.replace(".pdf", ".txt")
filePath = '../converted/' + file + '.txt'
print(filePath)
if os.path.exists(filePath):
with open(filePath, 'r', encoding="utf8") as rptFile:
rline = csv.reader(rptFile)
lineCnt = 1
for rrow in rline:
try:
#print (rrow[0])
rawText = rrow[0]
#print(rawText)
#scrubbedText = rawText.encode("utf-8")
#print(type(scrubbedText))
#scrubbedText = unicodedata.normalize('NFC', reportText)
#scrubbedText = scrubbedText.replace('\"', '')
#scrubbedText = scrubbedText.replace('\'', '')
t = "OBX|" + str(lineCnt) + "||DIAGNOSIS:^PATHOLOGY||" + rawText + "||||||F|||201801090940"
#print(t)
reportText.append(t)
lineCnt = lineCnt + 1
except Exception as e:
#print(e)
pass
rptFile.close()
return reportText
i = i + 1
csvfile.close()
return ""
def obx2(whichId):
reportText = []
with open('../reports/report_manifest.txt', 'r') as csvfile:
mline = csv.reader(csvfile, delimiter='\t')
i = 0;
for row in mline:
if i > 0:
file = row[1]
parts = file.split(".") # arse filename becauseit contains the tcgaid
tcgaid = parts[0] # part 0 is tcga id
if tcgaid == whichId:
filePath = '../reports/' + row[0] + '/' + file.replace(".pdf", ".txt")
if os.path.exists(filePath):
rpt = open(filePath, 'r')
rawText = rpt.read();
reportText = unicodedata.normalize('NFC', rawText)
reportText = rawText.replace('\"', '')
t = "OBX|1||DIAGNOSIS:^PATHOLOGY||" + str(reportText) + "||||||F|||201801090940"
rpt.close()
return t
i = i + 1
csvfile.close()
return ""
if __name__ == "__main__":
parser = argparse.ArgumentParser()
# parser.add_argument("xmlfile", help="a xml data file")
# parser.add_argument("outfile", help="a out data file")
# parser.add_argument("-conf", action="store_true", help="to generate just a config file")
parser.add_argument("-disease", help="specify the disease (e.g., ov")
args = parser.parse_args()
# if args.xmlfile:
# xmlfile =args.xmlfile
# if args.outfile:
# outfile =args.outfile
# if args.conf:
# config = True
if args.disease:
disease = args.disease
errorList = []
# read the clinical manifest file to use as the master list
with open('MANIFEST.txt', 'r') as tsvfile:
mline = csv.reader(tsvfile, delimiter='\t')
i = 0;
for row in mline:
if i > 0:
xmlfile = row[1]
parts = xmlfile.split("/") # break into parts to get filename
if xmlfile.find(".xml") > -1:
xdir = parts[0]
xfile = parts[1]
outfile = xfile.replace('.xml', '.hl7')
print (xdir + " -- " + xfile + " ==> " + outfile)
if parseit(xdir + '/' + xfile):
print('Writing Output file...')
#writehl7(xdir + '/' + outfile)
writehl7('../converted/hl7/' + outfile)
else:
errorList.append(xdir + '/' + xfile)
i = i + 1
tsvfile.close()
if len(errorList) > 0:
print('Exception list')
print('--------------')
for e in errorList:
print(e)