-
Notifications
You must be signed in to change notification settings - Fork 0
/
micro.py
290 lines (243 loc) · 9.39 KB
/
micro.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
'''
Created on Oct 8, 2012
@author: gardnerga
@summary: Reformat a list of microbiology reports into spreadsheet/delimited format. Parse lines to separate information.
'''
import mmap
import sys
SEP = "|"
def parse_date(dateStr):
if len(dateStr) == 8:
year = dateStr[:4]
month = dateStr[4:6]
day = dateStr[6:]
return "{0}/{1}/{2}".format(month, day, year)
else:
raise ValueError("{0} does not appear to be a valid date".format(dateStr))
def parse_time(time):
if not ":" in time:
if len(time)==4:
return ":".join([time[:2], time[2:]])
else:
raise ValueError("{0} does not appear to be a valid time".format(time))
if len(time) == 5:
return time
else:
raise ValueError("{0} does not appear to be a valid time".format(time))
class NonparsedLine(object):
def __init__(self, line):
if isinstance(line, ORG):
self.data = {'ORG':[line]}
else:
data = line.split(": ", 1)[1].split(None, 1)
attr = data[0].replace(":", "")
val = data[1].strip()
self.data = {attr : val}
def add_data(self, line):
data = line.split(": ", 1)[1].split(None, 1)
if len(data) == 1:
data.append("")
self.data.update({data[0].replace(":", "") : data[1].strip()})
def add_org(self, org):
if 'ORG' in self.data:
self.data['ORG'].append(org)
else:
self.data['ORG'] = [org]
def __str__(self):
out = []
for k,v in self.data.items():
out.extend([k,v])
return SEP.join(out)
class ParsedLine(object):
def __init__(self, line):
data = line.split(": ", 1)[1]
for i, d in enumerate(data.split()):
self.set_attr_num(i+1, d)
self._clean()
def _clean(self):
self.set_attr_num(2, parse_date(self.get_attr_num(2)))
self.set_attr_num(3, parse_time(self.get_attr_num(3)))
def get_attr_num(self, num):
return getattr(self, "data{0}".format(num))
def get_attrs(self):
attrs = []
for i in xrange(1,self.__class__.NUMATTRS+1):
attrs.append(self.get_attr_num(i))
return attrs
def set_attr_num(self, num, val):
setattr(self, "data{0}".format(num), val)
def __str__(self):
return SEP.join(self.get_attrs())
class ACC(ParsedLine):
NUMATTRS = 6
def __init__(self, line):
super(ACC, self).__init__(line)
class BAT(ParsedLine):
NUMATTRS = 4
def __init__(self, line):
super(BAT, self).__init__(line)
class DAT(NonparsedLine):
def __init__(self, line):
super(DAT, self).__init__(line)
class TXT(NonparsedLine):
def __init__(self, line):
super(TXT, self).__init__(line)
class ORG(object):
def __init__(self, line):
self.data = {'ORG' : " ".join(line.split()[2:])}
def add_attr(self, line):
data = line.split()
attr = data[1].replace(":", "").strip()
val = " ".join(data[2:])
self.data.update({attr : val})
def __str__(self):
return self.data['ORG']
class ReportParser(object):
SOH = "S_O_H"
EOH = "E_O_H"
EOR = "E_O_R"
SEP = "|"
def __init__(self, filename):
self.filename = filename
self.records = self.get_records(filename)
self.numOrgs, self.orgattrs = self._inspect_orgs()
self._inspect_reports()
def get_header_vars(self, header):
split = header.split("|")
mrn = split[1].split()[0]
datetime = [x.strip() for x in split[3].split()]
print datetime
date = parse_date(datetime[0])
time = parse_time(datetime[1])
return mrn, date, time
def get_header(self):
return SEP.join(self.structure)
def get_records(self, filename):
records = []
fread = open(self.filename, 'r+')
buf = mmap.mmap(fread.fileno(), 0)
readline = buf.readline
data = ""
sectionFlags = [False, False, False]
while True:
line = readline()
if line:
if line.startswith(ReportParser.SOH):
sectionFlags = [True, False, False]
continue
elif line.startswith(ReportParser.EOH):
sectionFlags = [False, True, False]
continue
elif line.startswith(ReportParser.EOR):
sectionFlags = [False, False, True]
continue
if sectionFlags[0]:
mrn, date, time = self.get_header_vars(line)
elif sectionFlags[1]:
if line.strip():
data += line
else:
if data:
records.append(uBioReport(mrn, date, time, data.strip()))
data = ""
elif sectionFlags[2]:
data = ""
else:
break
fread.close()
return records
def _inspect_orgs(self):
orgattrs = set()
num = 0
for report in self.records:
if 'ORG' in report.DAT.data:
orgData = report.DAT.data['ORG']
orgCount = len(orgData)
num = num if orgCount < num else orgCount
for org in orgData:
orgattrs.update(org.data.keys())
return num, orgattrs
def _inspect_reports(self):
self.structure = ["MRN", "DATE", "TIME"]
for CLS in [ACC, BAT]:
for i in xrange(1, CLS.NUMATTRS+1):
self.structure.append("{0}{1}".format(CLS.__name__, i))
for report in self.records:
dat = report.DAT
for type in dat.data:
if type=='ORG' or type in self.orgattrs:
continue
datField = "DAT_{0}".format(type)
if datField not in self.structure:
self.structure.extend([datField, "TXT_{0}".format(type)])
orgattrs = sorted(list(self.orgattrs))
orgattrs.remove('ORG')
orgattrs = ['ORG'] + orgattrs
for i in xrange(0, self.numOrgs):
for attr in orgattrs:
self.structure.extend(["DAT_{0}{1}".format(attr, i+1), "TXT_{0}{1}".format(attr, i+1)])
def write_reports(self, filename):
fwrite = open(filename, 'w')
fwrite.write(self.get_header() + "\n")
for report in self.records:
entry = []
types = []
for attr in self.structure:
if "_" in attr:
type, subtype = attr.split("_")
try:
entry.append(getattr(report, type).data[subtype])
except KeyError:
try:
subtype, num = subtype[:-1], subtype[-1]
entry.append(getattr(report, type).data['ORG'][int(num)-1].data[subtype])
except (KeyError, ValueError, IndexError):
entry.append("")
else:
try:
entry.append(str(getattr(report, attr)))
except AttributeError:
type, num = attr[:-1], attr[-1]
if type not in types:
entry.append(str(getattr(report, type)))
types.append(type)
fwrite.write(SEP.join(entry) + "\n")
fwrite.close()
class uBioReport(object):
ATTRS = ["ACC", "BAT", "DAT", "TXT"]
def __init__(self, mrn, date, time, report):
self.MRN = mrn
self.DATE = date
self.TIME = time
self.report = report
self.parse()
def parse(self):
org = None
for line in self.report.splitlines():
type = line[:3]
subtype = line.split()[1].replace(":","")
if subtype == "ORG":
org = ORG(line)
try:
if org:
if subtype == "ORG":
getattr(self, type).add_org(org)
else:
getattr(self, type).data['ORG'][-1].add_attr(line)
else:
getattr(self,type).add_data(line)
except AttributeError:
if org:
setattr(self,type,getattr(sys.modules[__name__], type)(org))
else:
setattr(self,type,getattr(sys.modules[__name__], type)(line))
def __str__(self):
out = [SEP.join([self.mrn, self.date, self.time])]
for attr in uBioReport.ATTRS:
out.append(str(getattr(self, attr)))
return SEP.join(out)
if __name__ == "__main__":
inputfile = "pilotcult.out"
outfile = "pilotcult_parsed.pipe"
reportParser = ReportParser(inputfile)
reportParser.write_reports(outfile)