-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprog
executable file
·376 lines (335 loc) · 12.9 KB
/
prog
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
#!/usr/bin/env perl
use utf8;
use 5.014;
use strict;
use warnings;
use DBI;
use Text::CSV::Encoded;
use Text::CSV_XS;
use Data::Dumper;
use File::Basename;
die unless @ARGV == 4;
my $master_lookup_file = shift;
my $serial_local_id_file = shift;
my $target_header_file = shift;
my $target_data_file = shift;
my $pat_localid = qr/^\d{8}$/;
my $pat_serial = qr/^\w{1,2}\d{6}$/;
my $main_serial = qr/^\d{5}$/;
#my $master_lookup_file = '../csv/lookup.csv'; # %master
#my $serial_local_id_file = '../csv/precombat_logfail-mapping/file-localid.csv'; # %serial_localid
#my $target_header_file = '../csv/precombat_logfail-mapping/file-header.csv';
#my $target_data_file = '../csv/precombat_logfail-mapping/precombat_logfail-mapping_C02_MdedicalHistoryRiskFactor.csv';
my $target_table ='';
my $master = load_master($master_lookup_file);
my $serial_localid = load_localid($serial_local_id_file);
my $meta = load_header($target_header_file);
my @type_a_pattern = qw/
C01_SubjectList
C02_MedicalHistoryRiskFactor
C03_DiagnosisExam
C04_PatientLevelInformation
C05_LeftMainLesionData
C07_MedicationData
C09_InHospital_Laboratory
C10_InHospital_Outcomes
E01_Bleeding
E03_Death
E04_MI
E05_RepeatRevascularization
C02_AdmissionData
C03_RiskFactorHistory
C04_NonlnvasiveTest
C05_IndexProcedureData
C06_TargetLeftmainData
C08_AntiplateletelInhospital
C09_AntiplateleteFollowUp
C10_OtherConcomitant
C11_DischargeData
C12_LaboratoryData
E01_Death
E02_MyocardialInfarction
E03_RepeatRevascularization
E04_StentThrombosis
E05_CerebralVascularAccident
E06_Bleeding
AMC-main-compare-mapping
BMS-DES-CABG-list
/;
my @type_b_pattern = qw/
C11_FU_outcomes
C12_FU_AngiographicData
C13_ClinicalFollowUp
C14_AngiographicFunctionalTe
/;
# target_table =
# $target_header_file{ column_name } => [ TABLE_NAME, COLUMN_NAME ]
# %target_data{ serial } => [
#
# ]
#my $target_data_file = '../csv/precombat_logfail-mapping/precombat_logfail-mapping_C01_SubjectList.csv';
my @rows;
my $csv = Text::CSV_XS->new(
{
binary => 1,
eol => $/,
always_quote => 1
}
) or die "Cannot use CSV: " . Text::CSV_XS->error_diag();
open my $fh, "<:encoding(utf8)", $target_data_file
or die "$target_data_file: $!";
my $header = $csv->getline($fh);
$csv->column_names(@$header);
my $dbh = DBI->connect(
"DBI:mysql:database=rum-test;host=localhost",
"rumidier",
"rumidier",
{
RaiseError => 1,
AutoCommit => 1,
}
);
$dbh->do("set names utf8");
my %lookup_basename;
my $futestno;
while ( my $row = $csv->getline_hr($fh) ) {
my $target_table = get_type($target_data_file);
given ($target_table) {
my $old_serial = $row->{serial};
my $localid = $serial_localid->{$old_serial};
my $serial = $master->{$localid};
my $lab_id = 1;
my $status = 'INCOMPLETE';
when (@type_a_pattern) {
say "[DEBUG]: $target_header_file $target_data_file";
foreach my $key (@$header) {
next if $key eq 'serial';
die $key unless $meta->{$key};
my $table = ${ $meta->{$key} }[0];
my $column = ${ $meta->{$key} }[1];
die unless $table;
die unless $column;
say "[DEBUG]: $target_header_file $target_data_file $serial";
my $sth = $dbh->prepare("SELECT COUNT(*) FROM $table WHERE serial=?");
$sth->execute($serial) or die $!;
my $count = $sth->fetchrow_arrayref->[0];
if ($count) {
$sth = $dbh->prepare("UPDATE $table SET $column=? WHERE serial=?");
$sth->execute( $row->{$key}, $serial ) or die $!;
}
else {
if ( $table eq 'C01_SubjectList' ) {
$sth = $dbh->prepare(
qq/
INSERT INTO `$table` (
`serial`,
`lab_id`,
`$column`,
`localid`,
`status`
) VALUES ( ?, ?, ?, ?, ? )
/
);
$sth->execute(
$serial,
$lab_id,
$row->{$key},
$localid,
$status
) or die $!;
}
else {
$sth = $dbh->prepare(
qq/
INSERT INTO `$table` (
`serial`,
`lab_id`,
`status`,
`$column`
) VALUES ( ?, ?, ?, ?)
/
);
$sth->execute(
$serial,
$lab_id,
$status,
$row->{$key}
) or die $!;
}
}
}
}
when (@type_b_pattern) {
say "[DEBUG]: $target_header_file $target_data_file";
given ($target_table) {
when (/^(C11_FU_outcomes|C13_ClinicalFollowUp)$/) {
my $check_month=$row->{'month'};
next if lc $check_month eq lc '9m';
foreach my $key (@$header) {
next if $key eq 'serial';
next if $key eq 'month';
die $key unless $meta->{$key};
my $table = ${ $meta->{$key} }[0];
my $column = ${ $meta->{$key} }[1];
die unless $table;
die unless $column;
#key : colum name
#{ $meta->{$key} }[0] : table name
#{ $meta->{$key} }[1] : table colum name
say "[DEBUG]: $target_header_file $target_data_file $serial";
my $sth = $dbh->prepare("SELECT COUNT(*) FROM $table
WHERE serial=? AND FU_VISIT=?");
$sth->execute($serial, $check_month) or die $!;
my $count = $sth->fetchrow_arrayref->[0];
if($count) {
$sth = $dbh->prepare("UPDATE $table
SET $column=?
WHERE serial=? AND FU_VISIT=?");
$sth->execute( $row->{$key},
$serial,$check_month ) or die $!;
} else {
$sth = $dbh->prepare(
qq/
INSERT INTO `$table` (
`serial`,
`lab_id`,
`FU_VISIT`,
`$column`,
`status`
) VALUES ( ?, ?, ?, ?, ? )
/
);
$sth->execute(
$serial,
$lab_id,
$check_month,
$row->{$key},
$status,
) or die $!;
}
}
}
when (/^(C12_FU_AngiographicData|C14_AngiographicFunctionalTe)$/) {
unless (defined($lookup_basename{$old_serial})) {
$lookup_basename{$old_serial} = 1;
}
foreach my $key (@$header) {
next if $key eq 'serial';
die $key unless $meta->{$key};
my $table = ${ $meta->{$key} }[0];
my $column = ${ $meta->{$key} }[1];
die unless $table;
die unless $column;
#key : colum name
#{ $meta->{$key} }[0] : table name
#{ $meta->{$key} }[1] : table colum name
say "[DEBUG]: $target_header_file $target_data_file $serial";
my $sth = $dbh->prepare("SELECT COUNT(*) FROM $table
WHERE serial=?
AND FUTESTNO=?");
$sth->execute($serial, $lookup_basename{$old_serial}) or die $!;
my $count = $sth->fetchrow_arrayref->[0];
if ($count) {
$sth = $dbh->prepare("UPDATE $table
SET $column=?
WHERE serial=?
AND FUTESTNO=?");
$sth->execute( $row->{$key},
$serial,
$lookup_basename{$old_serial} ) or die $!;
}
else {
$sth = $dbh->prepare(
qq/
INSERT INTO `$table` (
`serial`,
`lab_id`,
`$column`,
`status`,
`FUTESTNO`
) VALUES ( ?, ?, ?, ?, ? )
/
);
$sth->execute(
$serial,
$lab_id,
$row->{$key},
$status,
$lookup_basename{$old_serial},
) or die $!;
}
}
$lookup_basename{$old_serial}++;
}
}
}
default {
print "BUG : $target_table\n";
die;
}
}
}
close $fh;
sub load_localid {
my ($serial_local_id_file) = @_;
my $csv = Text::CSV_XS->new( { binary => 1, eol => $/, always_quote => 1 } )
or die "Cannot use CSV: " . Text::CSV_XS->error_diag();
open $fh, "<:encoding(utf8)", "$serial_local_id_file"
or die "$serial_local_id_file:e $!";
my %serial_localid;
while ( my $row = $csv->getline($fh) ) {
unless ($row->[0] =~ $main_serial) {
unless ($row->[0] =~ $pat_serial) {
print "[$row->[0]]\n";
die unless ($row->[0] =~ $pat_localid);
}
}
# die unless $row->[1] =~ $pat_localid;
$serial_localid{ $row->[0] } = $row->[1];
}
close $fh;
return \%serial_localid;
}
sub load_master {
my ($master_lookup_file) = @_;
my $csv = Text::CSV_XS->new( { binary => 1, eol => $/, always_quote => 1 } )
or die "Cannot use CSV: " . Text::CSV_XS->error_diag();
open my $fh, "<:encoding(utf8)", "$master_lookup_file"
or die "$master_lookup_file:e $!";
my %master;
while ( my $row = $csv->getline($fh) ) {
die unless $row->[0] =~ $pat_localid;
die unless $row->[1] =~ $pat_serial;
$master{ $row->[0] } = $row->[1];
}
close $fh;
die unless scalar keys %master == 2113;
return \%master;
}
sub load_header {
# %header = [ COLUMN_NAME->TABLE_NAME->COLUMN_INFO ]
my ($target_header_file) = @_;
my $csv = Text::CSV_XS->new( { binary => 1, eol => $/, always_quote => 1 } )
or die "Cannot use CSV: " . Text::CSV_XS->error_diag();
open $fh, "<:encoding(utf8)", "$target_header_file"
or die "$target_header_file:e $!";
my %header;
while ( my $row = $csv->getline($fh) ) {
$header{ $row->[0] } = [ $row->[1], $row->[2], $row->[3] ];
}
close $fh;
return \%header;
}
sub get_type {
my ( $path ) = @_;
my ( $filename, $dirname, $suffix ) = fileparse($path, qr/\..*$/);
return unless $suffix eq '.csv';
if ($filename =~ m/([B|A|C|E].*)/) {
$filename = $1;
return $filename;
}
else {
print "get_type : $filename\n";
return;
}
}