forked from aces/Loris-MRI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch_uploads_imageuploader.pl
executable file
·343 lines (257 loc) · 10.1 KB
/
batch_uploads_imageuploader.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
#!/usr/bin/perl -w
=pod
=head1 NAME
batch_uploads_imageuploader -- a script that runs C<imaging_upload_file.pl> in
batch mode
=head1 SYNOPSIS
./batch_uploads_imageuploader -profile prod < list_of_scans.txt > log_batch_imageuploader.txt 2>&1 C[options]>
Available options are:
-profile: name of the config file in C<../dicom-archive/.loris_mri>
-verbose: if set, be verbose
=head1 DESCRIPTION
This script runs the Loris-MRI insertion pipeline on multiple scans. The list of
scans are provided through a text file (e.g. C<list_of_scans.txt>) with one scan
details per line.
The scan details includes the path to the scan, identification as to whether the
scan is for a phantom (Y) or not (N), and the candidate name for non-phantom
entries.
Like the LORIS Imaging Uploader interface, this script also validates the
candidate's name against the (start of the) filename and creates an entry in the
C<mri_upload> table.
An example of what C<list_of_scans.txt> might contain for 3 uploads to be
inserted:
/data/incoming/PSC0001_123457_V1.tar.gz N PSC0000_123456_V1
/data/incoming/lego_Phantom_MNI_20140101.zip Y
/data/incoming/PSC0001_123457_V1_RES.tar.gz N PSC0000_123456_V1
=head2 Methods
=cut
use strict;
use warnings;
no warnings 'once';
use File::Basename;
use Getopt::Tabular;
use NeuroDB::DBI;
use NeuroDB::Notify;
use NeuroDB::ExitCodes;
use NeuroDB::Database;
use NeuroDB::DatabaseException;
use NeuroDB::objectBroker::ObjectBrokerException;
use NeuroDB::objectBroker::ConfigOB;
my $profile = '';
my $upload_id = undef;
my ($debug, $verbose) = (0,1);
my $stdout = '';
my $stderr = '';
my @opt_table = (
[ "Basic options", "section" ],
[
"-profile", "string", 1, \$profile,
"name of config file in ../dicom-archive/.loris_mri"
],
["-verbose", "boolean", 1, \$verbose, "Be verbose."]
);
my $Help = <<HELP;
******************************************************************************
Run imaging_upload_file.pl in batch mode
******************************************************************************
This script runs the Loris-MRI insertion pipeline on multiple scans. The list of
scans are provided through a text file (e.g. C<list_of_scans.txt>) with one scan
details per line.
The scan details includes the path to the scan, identification as to whether the
scan is for a phantom (Y) or not (N), and the candidate name for non-phantom
entries.
Like the LORIS Imaging Uploader interface, this script also validates the
candidate's name against the (start of the) filename and creates an entry in the
mri_upload table.
An example of what C<list_of_scans.txt> might contain for 3 uploads to be
inserted:
/data/incoming/PSC0001_123457_V1.tar.gz N PSC0000_123456_V1
/data/incoming/Lego_Phantom_MNI_20140101.zip Y
/data/incoming/PSC0001_123457_V1_RES.tar.gz N PSC0000_123456_V1
Documentation: perldoc batch_uploads_imageuploader
HELP
my $Usage = <<USAGE;
usage: ./batch_uploads_imageuploader.pl -profile prod < list_of_scans.txt > log_batch_imageuploader.txt 2>&1 [options]
$0 -help to list options
USAGE
&Getopt::Tabular::SetHelp( $Help, $Usage );
&Getopt::Tabular::GetOptions( \@opt_table, \@ARGV )
|| exit $NeuroDB::ExitCodes::GETOPT_FAILURE;
################################################################
################ Get config setting#############################
################################################################
if ( !$profile ) {
print $Help;
print STDERR "$Usage\n\tERROR: missing -profile argument\n\n";
exit $NeuroDB::ExitCodes::PROFILE_FAILURE;
}
{ package Settings; do "$ENV{LORIS_CONFIG}/.loris_mri/$profile" }
if ( !@Settings::db ) {
print STDERR "\n\tERROR: You don't have a \@db setting in the file "
. "$ENV{LORIS_CONFIG}/.loris_mri/$profile \n\n";
exit $NeuroDB::ExitCodes::DB_SETTINGS_FAILURE;
}
# ----------------------------------------------------------------
## Establish database connection
# ----------------------------------------------------------------
# old database connection
my $dbh = &NeuroDB::DBI::connect_to_db(@Settings::db);
# new Moose database connection
my $db = NeuroDB::Database->new(
databaseName => $Settings::db[0],
userName => $Settings::db[1],
password => $Settings::db[2],
hostName => $Settings::db[3]
);
$db->connect();
# ----------------------------------------------------------------
## Get config setting using ConfigOB
# ----------------------------------------------------------------
my $configOB = NeuroDB::objectBroker::ConfigOB->new(db => $db);
my $data_dir = $configOB->getDataDirPath();
my $mail_user = $configOB->getMailUser();
my $bin_dir = $configOB->getMriCodePath();
my $is_qsub = $configOB->getIsQsub();
my ($stdoutbase, $stderrbase) = ("$data_dir/batch_output/imuploadstdout.log",
"$data_dir/batch_output/imuploadstderr.log");
while($_ = $ARGV[0] // '', /^-/) {
shift;
last if /^--$/; ## -- ends argument processing
if (/^-D/) { $debug++ } ## debug level
if (/^-v/) { $verbose++ } ## verbosity
}
## read input from STDIN, store into array @inputs (`find ....... | this_script`)
my @patientnamearray = ();
my @fullpatharray = ();
my @phantomarray = ();
my @submitted = ();
my $counter = 0;
while(my $line = <STDIN>)
{
chomp $line;
my @linearray = split(" " , $line);
push (@fullpatharray, $linearray[0]);
push (@phantomarray, $linearray[1]);
push (@patientnamearray, $linearray[2]);
}
close STDIN;
## foreach series, batch magic
foreach my $input (@fullpatharray)
{
$counter++;
$stdout = $stdoutbase.$counter;
$stderr = $stderrbase.$counter;
#$stdout = '/dev/null';
#$stderr = '/dev/null';
my $fullpath = $fullpatharray[$counter-1];
my $phantom = $phantomarray[$counter-1];
my $patientname = $patientnamearray[$counter-1];
## Ensure that
## 1) the uploaded file is of type .tgz or .tar.gz or .zip
## 2) check that input file provides phantom details (Y for phantom, N for real candidates)
## 3) for non-phantoms, the patient name and path entries are identical; this mimics the imaging uploader in the front-end
my ($base,$path,$type) = fileparse($fullpath, qr{\..*});
if (($type ne '.tgz') && ($type ne '.tar.gz') && ($type ne '.zip')) {
print STDERR "The file on line $counter is not of type .tgz, tar.gz, or "
. ".zip and will not be processed\n";
exit $NeuroDB::ExitCodes::FILE_TYPE_CHECK_FAILURE;
}
if (($phantom eq '') || (($phantom ne 'N') && ($phantom ne 'Y'))) {
print STDERR "Make sure the Phantom entry is filled out "
. "with Y if the scan if for a phantom, and N otherwise\n";
exit $NeuroDB::ExitCodes::PHANTOM_ENTRY_FAILURE;
}
if ($phantom eq 'N') {
if ($patientname ne (substr ($base, 0, length($patientname)))) {
print STDERR "Make sure the patient name $patientname for "
. "non-phantom entries matches the start of $base "
. "filename in $path\n";
exit $NeuroDB::ExitCodes::PNAME_FILENAME_MISMATCH;
}
}
else {
if ($patientname ne '') {
print STDERR "Please leave the patient name blank for phantom "
. "entries\n";
exit $NeuroDB::ExitCodes::PNAME_FILENAME_MISMATCH;
}
else {
$patientname = 'NULL';
}
}
## Populate the mri_upload table with necessary entries and get an upload_id
$upload_id = insertIntoMRIUpload(\$dbh,
$patientname,
$phantom,
$fullpath);
## this is where the subprocesses are created... should basically run processor script with study directory as argument.
## processor will do all the real magic
my $command = "$bin_dir/uploadNeuroDB/imaging_upload_file.pl "
. "-profile $profile -upload_id $upload_id $fullpath";
if ($verbose) {
$command .= " -verbose";
}
##if qsub is enabled use it
if ($is_qsub) {
open QSUB, "| qsub -V -e $stderr -o $stdout -N process_imageuploader_${counter}";
print QSUB $command;
close QSUB;
}
##if qsub is not enabled
else {
print "Running now the following command: $command\n" if $verbose;
system($command);
}
push @submitted, $input;
}
open MAIL, "|mail $mail_user";
print MAIL "Subject: BATCH_UPLOADS_IMAGEUPLOADER: ".scalar(@submitted)." studies submitted.\n";
print MAIL join("\n", @submitted)."\n";
close MAIL;
################################################################
############### insertIntoMRIUpload ############################
################################################################
=pod
insertIntoMRIUpload()
Description:
- Insert into the mri_upload table entries for data coming
from batch_upload_imageuploader.pl
=head3 insertIntoMRIUpload($patientname, $phantom, $fullpath)
Function that inserts into the C<mri_upload> table entries for data coming from
the list of scans in the text file provided when calling
C<batch_upload_imageuploader>
INPUTS:
- $patientname : The patient name
- $phantom : 'Y' if the entry is for a phantom,
'N' otherwise
- $fullpath : Path to the uploaded file
RETURNS: $upload_id : The upload ID
=cut
sub insertIntoMRIUpload {
my ( $dbhr, $patientname, $phantom, $fullpath ) = @_;
my $User = getpwuid($>);
my $query = "INSERT INTO mri_upload ".
"(UploadedBy, UploadDate, PatientName, ".
"IsPhantom, UploadLocation) ".
"VALUES (?, now(), ?, ?, ?)";
my $mri_upload_insert = $dbh->prepare($query);
$mri_upload_insert->execute($User,$patientname,
$phantom, $fullpath);
my $where = " WHERE mu.UploadLocation =?";
$query = "SELECT mu.UploadID FROM mri_upload mu";
$query .= $where;
my $sth = $dbh->prepare($query);
$sth->execute($fullpath);
my $upload_id = $sth->fetchrow_array;
return $upload_id;
}
## exit $NeuroDB::ExitCodes::SUCCESS for find to consider this -cmd true (in case we ever run it that way...)
exit $NeuroDB::ExitCodes::SUCCESS;
__END__
=pod
=head1 LICENSING
License: GPLv3
=head1 AUTHORS
LORIS community <[email protected]> and McGill Centre for Integrative
Neuroscience
=cut