forked from aces/dicom-archive-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addSeriesAndFileRecords.pl
executable file
·241 lines (197 loc) · 6.54 KB
/
addSeriesAndFileRecords.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
#!/usr/bin/perl
# Jonathan Harlap
# jharlap@bic.mni.mcgill.ca
# Perl tool based on DCMSUM.pm and DICOM.pm to populate the series and file tables for a tarchive
# @VERSION : $Id: addSeriesAndFileRecords.pl 4 2007-12-11 20:21:51Z jharlap $
use strict;
use Cwd qw/ abs_path /;
use FindBin;
use Getopt::Tabular;
use FileHandle;
use File::Temp qw/ tempdir /;
use File::Basename;
use lib "$FindBin::Bin";
use DICOM::DICOM;
use DICOM::DCMSUM;
use DB::DBI;
my $profile;
my $verbose = 0;
my $version = 0;
my $versionInfo = sprintf "%d", q$Revision: 4 $ =~ /: (\d+)/;
################################
# array of dicom dirs
my @dcmDirs;
my $Usage = "------------------------------------------
Author : Jonathan Harlap
Date : 2007/03/27
Version : $versionInfo
WHAT THIS IS:
A tool to repopulate the tarchive_series and tarchive_files tables for an existing tarchive.
Usage:\n\t $0 </PATH/TO/TARCHIVE.tar> [options]
\n\n See $0 -help for more info\n\n";
my @arg_table =
(
["Main options","section"],
["-profile","string",1, \$profile, "Specify the name of the config file which resides in .loris_mri in the current directory."],
["General options", "section"],
["-verbose","boolean",1, \$verbose, "Be verbose."],
["-version","boolean",1, \$version, "Print version and revision number and exit"],
);
GetOptions(\@arg_table, \@ARGV) || exit 1;
# print version info and quit
if ($version) { print "$versionInfo\n"; exit; }
# checking for profile settings
if($profile && -f "$ENV{LORIS_CONFIG}/.loris_mri/$profile") { { package Settings; do "$ENV{LORIS_CONFIG}/.loris_mri/$profile" } }
if ($profile && !@Settings::db) { print "\n\tERROR: You don't have a configuration file named '$profile' in: $ENV{LORIS_CONFIG}/.loris_mri/ \n\n"; exit 33; }
# basic error checking on tarchive
if(scalar(@ARGV) != 1) { print $Usage; exit 1; }
my $tarchive = abs_path($ARGV[0]);
# establish database connection if database option is set
my $dbh;
$dbh = &DB::DBI::connect_to_db(@Settings::db); print "Testing for database connectivity. \n" if $verbose; $dbh->disconnect(); print "Database is available.\n\n" if $verbose;
####################### main ########################################### main ###########################################
my ($studyUnique, $metaname, @metaFiles, $dcmdir, $sumTypeVersion);
# make temp dir
my $TmpDir = tempdir( CLEANUP => 1 );
# extract the tarchive
my $dcmdir = &extract_tarchive($tarchive, $TmpDir);
# create new summary object
my $summary = DICOM::DCMSUM->new($TmpDir.'/'.$dcmdir,$TmpDir);
# determine the name for the summary file
$metaname = $summary->{'metaname'};
# get the summary type version
$sumTypeVersion = $summary->{'sumTypeVersion'};
# get the unique study ID
$studyUnique = $summary->{'studyuid'};
# if -dbase has been given create an entry based on unique studyID
# Create database entry checking for already existing entries...
$dbh = &DB::DBI::connect_to_db(@Settings::db);
# now get the TarchiveID
my $tarchiveID;
my $tarchiveBasename = basename($tarchive);
(my $query = <<QUERY) =~ s/\n/ /gm;
SELECT
TarchiveID
FROM
tarchive
WHERE
DicomArchiveID=?
AND ArchiveLocation like ?
QUERY
my $sth = $dbh->prepare($query);
$sth->execute($summary->{studyuid}, "%${tarchiveBasename}");
my @row = $sth->fetchrow_array();
$tarchiveID = $row[0];
if($tarchiveID > 0) {
print "Determined TarchiveID = $tarchiveID\n";
} else {
print "Could not determine TarchiveID for tarchive $tarchive\n";
exit 1;
}
# nuke series and files records then reinsert them
(my $delete_series = <<QUERY) =~ s/\n/ /gm;
DELETE FROM
tarchive_series
WHERE
TarchiveID=?
QUERY
(my $delete_files = <<QUERY) =~ s/\n/ /gm;
DELETE FROM
tarchive_files
WHERE
TarchiveID=?
QUERY
$sth = $dbh->prepare($delete_series);
$dbh->execute($tarchiveID);
$sth = $dbh->prepare($delete_files);
$dbh->execute($tarchiveID);
# now create the tarchive_series records
($query = <<QUERY) =~ s/\n/ /gm;
INSERT INTO
tarchive_series
(
TarchiveID, SeriesNumber, SeriesDescription, SequenceName,
EchoTime, RepetitionTime, InversionTime, SliceThickness,
PhaseEncoding, NumberOfFiles, SeriesUID
)
VALUES
(
?, ?, ?, ?,
?, ?, ?, ?,
?, ?, ?
)
QUERY
my $insert_series = $dbh->prepare($query);
foreach my $acq (@{$summary->{acqu_List}}) {
# insert the series
my ($seriesNum, $sequName, $echoT, $repT, $invT, $seriesName, $sl_thickness, $phaseEncode, $seriesUID, $num) = split(':::',$acq);
my @values =
(
$tarchiveID, $seriesNum, $seriesName, $sequName,
$echoT, $repT, $invT, $sl_thickness,
$phaseEncode, $num, $seriesUID
);
$insert_series->execute(@values);
}
# now create the tarchive_files records
($query = <<QUERY) =~ s/\n/ /gm;
INSERT INTO
tarchive_files
(
TarchiveID, SeriesNumber, FileNumber, EchoNumber,
SeriesDescription, Md5Sum, FileName
)
VALUES
(
?, ?, ?, ?,
?, ?, ?
)
QUERY
my $insert_file = $dbh->prepare($query);
foreach my $file (@{$summary->{'dcminfo'}}) {
# insert the file
my $filename = $file->[4];
$filename =~ s/^${TmpDir}\///;
my @values;
if($file->[21]) { # file is dicom
@values =
(
$tarchiveID, $file->[1], $file->[3], $file->[2],
$file->[12], $file->[20], $filename
);
$insert_file->execute(@values);
} else {
@values =
(
$tarchiveID, undef, undef, undef,
undef, $file->[20], $filename
);
$insert_file->execute(@values);
}
}
print "Done!\n";
exit;
######################################################################### end main ####################
=pod
################################################
Extract a tarchive into a temp dir
################################################
=cut
sub extract_tarchive {
my ($tarchive, $tempdir) = @_;
print "Extracting tarchive\n" if $verbose;
`cd $tempdir ; tar -xf $tarchive`;
opendir TMPDIR, $tempdir;
my @tars = grep { /\.tar\.gz$/ && -f "$tempdir/$_" } readdir(TMPDIR);
closedir TMPDIR;
if(scalar(@tars) != 1) {
print "Error: Could not find inner tar in $tarchive!\n";
print @tars . "\n";
exit(1);
}
my $dcmtar = $tars[0];
my $dcmdir = $dcmtar;
$dcmdir =~ s/\.tar\.gz$//;
`cd $tempdir ; tar -xzf $dcmtar`;
return $dcmdir;
}