/
parse_drug_file.pl
488 lines (417 loc) · 12.1 KB
/
parse_drug_file.pl
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
use strict;
use warnings;
use Time::Piece;
use Time::Seconds;
use Data::Dumper;
my $format = '%Y-%m-%d';
#Variable parameters
my $rxLengthProportion = 0.75;
my $adherenceProportion = 10000;
my $terminationProportion = 5;
my $terminationLimitInDays = 60;
my $extractDate = localtime;
my $limit = 100000000;
my @statements;
my @standard;
my @vals;
my $output;
my $pid;
my $dt;
my $tabs;
my $perDay;
my $mg;
my $family;
my $type;
my $pidLast=-1;
my $dtLast;
my $tabsLast;
my $perDayLast="";
my $mgLast="";
my $familyLast;
my $typeLast="";
my $pidChg=-1;
my $dtChg;
my $tabsChg;
my $perDayChg;
my $mgChg;
my $familyChg;
my $typeChg;
my %types=();
my @patientEvents=();
my @patientTypeCache=();
my $debug = 0;
#######################
# Min/max subroutines #
#######################
sub max ($$) { $_[$_[0] < $_[1]] }
sub min ($$) { $_[$_[0] > $_[1]] }
###############################
# Adds an event to the list #
###############################
sub addToList {
my ($pid, $dt, $item, $family, $dose, $evt) = @_;
if(ref($dt) eq "Time::Piece") {
$dt = $dt->strftime($format);
}
my %hash = (id => $pid,dt => "$dt 04:00:00",item => $item,family => $family, dose => $dose, evt => $evt);
if($debug){
print "OUT: $pid\t$dt 04:00:00\t$item\t$family\t$dose\t$evt\n";
}
push(@patientEvents, \%hash);
}
###############################
# Sorts the list of events and#
# concatenates to a single #
# variable - $output #
###############################
sub writeOutput {
my @sorted = sort { $a->{id} <=> $b->{id} or $a->{dt} cmp $b->{dt}} @patientEvents;
$output .= "$_->{id}\t$_->{dt}\t$_->{item}\t$_->{family}\t$_->{dose}\t$_->{evt}\n" foreach (@sorted);
}
###############################
# Helper to return number of #
# types. #
###############################
sub noTypes{
return (scalar keys %types);
}
###############################
# Checks if the last drug has #
# expired and if so adds a #
# drug termination event #
###############################
sub isDrugStopped {
my ($pid, $type, $family, $testDate) = @_;
if($debug) {
print "SUB [isDrugStopped] called with:\n";
print "$pid\t$type\t$family\t$testDate\n\n";
}
if($types{"expChk"} < $testDate) {
if($debug) {
print "STOPPED:\tEXPCHK:" .$types{"expChk"} . "\tEXP:" .$types{"exp"} . "\tTEST:$testDate\n";
}
addToList($pid,$types{"exp"},"$type","$family","0","STOPPED");
%types = ();
}
}
###############################
# Adds a new drug type to the #
# list. #
###############################
sub newType{
my ($family, $type, $dt, $perDay, $mg, $tabs, $pid) = @_;
if($debug) {
print "SUB [newType] called with:\n";
print "$family\t$type\t$dt\t$perDay\t$mg\t$tabs\t$pid\n\n";
}
if($perDay eq ""){
$perDay = 1;
}
my $dose = $mg*$perDay;
#New type
addToList($pid,$dt,"$type","$family","$dose","STARTED");
#Add type as well
%types=();
# Make test for expiry lower of 2 months and termProp * tabs per day
my $numDaysToExpiry = min($terminationProportion*$tabs/$perDay, $terminationLimitInDays);
my $dtExpires = $dt + (ONE_DAY*$tabs/$perDay);
my $dtExpLong = $dt + ($rxLengthProportion*ONE_DAY*$tabs/$perDay);
my $dtExpChck = $dt + ($numDaysToExpiry*ONE_DAY);
if($debug){
print $dtExpLong->strftime($format) . "\t" . $dtExpires->strftime($format) . "\t" . $dtExpChck->strftime($format) . "\n";
}
%types = (exp => $dtExpires, expLong => $dtExpLong, expChk => $dtExpChck, dose => $dose);
}
###############################
# Logic to determin the tabs #
# to be taken per day if the #
# value is empty. #
###############################
sub getPerDay{
my ($perDay, $perDayLast, $type, $typeLast, $mg, $mgLast) = @_;
if($perDay eq ""){
if($type eq $typeLast && $mg eq $mgLast) {
$perDay = $perDayLast;
} else {
$perDay = 1;
}
}
if($debug){
print "$perDay\t$perDayLast\t$type\t$typeLast\t$mg\t$mgLast\n";
}
return $perDay;
}
sub populateVariablesFromHash{
my (%hash) = @_;
$pid = $hash{"pid"};
$dt = $hash{"dt"};
$tabs = $hash{"tabs"};
$perDay = $hash{"perDay"};
$mg = $hash{"mg"};
$family = $hash{"family"};
$type = $hash{"type"};
}
###############################
# Takes all drugs of a certain#
# type and evaluates the stop #
# start and dose change events#
###############################
sub evaluate{
if($debug){
print "SUB [evaluate] called with:\n";
print "$pid, $dt, $tabs, $perDay, $mg, $family, $type\n\n";
}
#cache existing values
my $pidTmp = $pid;
my $dtTmp = $dt;
my $tabsTmp = $tabs;
my $perDayTmp = $perDay;
my $mgTmp = $mg;
my $familyTmp = $family;
my $typeTmp = $type;
$pidLast=-1;
$dtLast="";
$tabsLast="";
$perDayLast="";
$mgLast="---";
$familyLast="";
$typeLast="";
for(my $i = 0; $i<= $#patientTypeCache; $i++)
{
my %event = %{$patientTypeCache[$i]};
populateVariablesFromHash(%event);
if($perDay eq ""){
if($mg eq $mgLast) {
$perDay = $perDayLast;
} elsif($i< $#patientTypeCache) {
my $j = 1;
while($i+$j <= $#patientTypeCache){
%event = %{$patientTypeCache[$i+$j]};
$j++;
if($event{"perDay"} ne "") {
if($mg eq $event{"mg"}){
$perDay = $event{"perDay"};
last;
} else {
$perDay = 1;
last;
}
} elsif ($mg ne $event{"mg"}){
$perDay = 1;
last;
} else {
}
}
if($perDay eq "") {
$perDay = 1;
}
} else {
$perDay = 1;
}
}
if($debug){
print "$family, $type, $dt, $perDay, $mg, $tabs, $pid\n";
}
if($i==0) {
newType($family, $type, $dt, $perDay, $mg, $tabs, $pid);
}
isDrugStopped($pid, $type, $family, $dt);
# Make test for expiry lower of 2 months and termProp * tabs per day
my $numDaysToExpiry = min($terminationProportion*$tabs/$perDay, $terminationLimitInDays);
my $dtExpires = $dt + (ONE_DAY*$tabs/$perDay);
my $dtExpLong = $dt + ($rxLengthProportion*ONE_DAY*$tabs/$perDay);
my $dtExpChck = $dt + ($numDaysToExpiry*ONE_DAY);
my $dose = $mg*$perDay;
if(!$types{"exp"}) {
#Must have been a big gap
addToList($pid,$dt,"$type","$family","$dose","RESTARTED");
} else {
if($dose > $types{"dose"}){
#If an increase then we assume an actual increase
addToList($pid,$dt,"$type","$family","$dose","DOSE INCREASED");
###$types{$type} = $mg*$perDay;
} elsif ($dose < $types{"dose"}){
if($dt >= $types{"expLong"}){
#if a decrease and near to or after the last one expires then consider a decrease
addToList($pid,$dt,"$type","$family","$dose","DOSE DECREASED");
$dose = $mg*$perDay;
} else {
#else if a decrease during the last prescription...
($pidChg, $dtChg, $tabsChg, $perDayChg, $mgChg, $familyChg, $typeChg) =($pid, $dt, $tabs, $perDay, $mg, $family, $type);
my $index = $i;
my %mgs = ();
my $first = $types{"dose"};
my $second = $dose;
my $last = $second;
my $lastDate = $dt;
my $third = 0;
my $lastPerDay = $perDay;
my $lastTabs = $tabs;
#keep track of different amounts
$mgs{$first}++;
$mgs{$second}++;
$i++;
while($i < $index+5 && $i <= $#patientTypeCache){
%event = %{$patientTypeCache[$i]};
populateVariablesFromHash(%event);
$perDay = getPerDay($perDay, $lastPerDay, $type, $type, $mg, $mgChg);
if($debug) {
print "$i\n";
}
if($mg eq "" || $family eq "" || $type eq "") {
$i++;
next;
}
$mgs{$mg*$perDay}++;
if($last == $mg*$perDay){
#sustatined decrease
last;
}
$last = $mg*$perDay;
$lastDate = $dt;
$lastTabs = $tabs;
$lastPerDay = $perDay;
$i++;
}
if($mgs{$first+$second}){
#looks like the two meds are taken together so an increase on first prescription
$dose = $first+$second;
addToList($pidChg,$dtChg,"$type","$family","$dose","DOSE INCREASED");
} elsif ($mgs{$first} > 1 && $mgs{$second} == 1) {
#a change then a change back
if($first > $second) {
addToList($pidChg,$dtChg,"$typeChg","$family","$second","DOSE DECREASED");
addToList($pidChg,$lastDate,"$typeChg","$family","$first","DOSE INCREASED");
} else {
addToList($pidChg,$dtChg,"$typeChg","$family","$second","DOSE INCREASED");
addToList($pidChg,$lastDate,"$typeChg","$family","$first","DOSE DECREASED");
}
$dose = $first;
} elsif ($mgs{$first} == 1 && $mgs{$second} >= 1) {
#a change
if($first > $second) {
addToList($pidChg,$dtChg,"$typeChg","$family","$second","DOSE DECREASED");
if($last > $second){
addToList($pid,$lastDate,"$typeChg","$family","$last","DOSE INCREASED");
} elsif($last < $second) {
addToList($pid,$lastDate,"$typeChg","$family","$last","DOSE DECREASED");
}
} else {
addToList($pidChg,$dtChg,"$typeChg","$family","$last","DOSE INCREASED");
}
$dose = $last;
} elsif($last == $second){
#a change
if($first > $second) {
addToList($pidChg,$dtChg,"$typeChg","$family","$last","DOSE DECREASED");
} else {
addToList($pidChg,$dtChg,"$typeChg","$family","$last","DOSE INCREASED");
}
$dose = $last;
} elsif($last == $first){
#a change
if($first > $second) {
addToList($pidChg,$dtChg,"$typeChg","$family","$last","DOSE INCREASED");
} else {
addToList($pidChg,$dtChg,"$typeChg","$family","$last","DOSE DECREASED");
}
$dose = $last;
} else {
addToList($pidChg,$dt,"$typeChg","$family","$dose","DRUG-ERROR-2000");
print Dumper(%mgs) . "\n";
}
# Make test for expiry lower of 2 months and termProp * tabs per day
$numDaysToExpiry = min($terminationProportion*$tabs/$perDay, $terminationLimitInDays);
$dtExpires = $dt + (ONE_DAY*$tabs/$perDay);
$dtExpLong = $dt + ($rxLengthProportion*ONE_DAY*$tabs/$perDay);
$dtExpChck = $dt + ($numDaysToExpiry*ONE_DAY);
}
} else {
#no change - insert no events
}
}
%types = (exp => $dtExpires, expLong => $dtExpLong, expChk => $dtExpChck, dose => $dose);
}
if($pid>=0 && $type ne ""){
isDrugStopped($pid, $type, $family, $extractDate);
}
@patientTypeCache=();
$pid = $pidTmp;
$dt = $dtTmp;
$tabs = $tabsTmp;
$perDay = $perDayTmp;
$mg = $mgTmp;
$family = $familyTmp;
$type = $typeTmp;
}
#get filename from args exit if none
if($#ARGV != 0) {
print "\nUsage: parse_drug_file.pl [path_to_data_file]\n\n\n";
exit;
}
print "Reading file...\n";
open my $fh, '<', "$ARGV[0]" or die "Cannot open $ARGV[0]: $!";
#my $header = <$fh>; #No header
my $ix = 0;
while (<$fh> ) {
chomp;
## Reads a line from the file
($pid, $type, $dt, $tabs, $perDay, $mg, $family) = split /\t/, $_;
if($debug){
print "$pid:$dt:$tabs:$perDay:$mg:$family:$type\n"
}
## If more than 11M rows then start debugging - eh?
if($ix>11165000) {
$debug=1;
print "DEBUG\n";
}
if($ix++ > $limit) {
last;
}
###Pull out just one patients:
#$debug=1;
#if($pid != 1030) {
# next;
#}
#ignore record if date is <1900
if($dt =~ /^18/) {
print "$dt\n";
next;
}
#Convert string date to perl date
$dt = Time::Piece->strptime($dt, $format);
#If no mg or family or type then not much we can do so we skip
if($mg eq "" || $family eq "" || $type eq "") {
next;
}
## File is sorted first by patient id then by drug type, so if either of these
## has changed from the last row then either it's a new drug, or a new patient
## and it's time to evaluate the cached records
if($pid != $pidLast || $type ne $typeLast){
evaluate();
}
## If no number of tablets, then let's just assume 28
if($tabs eq "") {
$tabs = 28;
}
my %event = (pid=>$pid, dt=>$dt, tabs=>$tabs, perDay=>$perDay, mg=>$mg, family=>$family, type=>$type);
if($debug){
print "$pid\t$type\t$mg\t$#patientTypeCache\n";
}
if($dt == $dtLast) {
# get rid of last one and add mgs to it
pop @patientTypeCache;
$event{"mg"} += $mg;
}
push(@patientTypeCache, \%event);
($pidLast, $dtLast, $tabsLast, $perDayLast, $mgLast, $familyLast, $typeLast) =($pid, $dt, $tabs, $perDay, $mg, $family, $type);
}
evaluate();
writeOutput();
close($fh);
print "File processed.\n\n";
print "Writing output...\n\n";
open $fh, '>', "$ARGV[0].processed" or die "Cannot open $ARGV[0].processed: $!";
print $fh $output;
close ($fh);
print "Done!\n";