Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
211 changes: 211 additions & 0 deletions batch_uploads_imageuploader
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
#!/usr/bin/perl -w
use strict;
use warnings;
no warnings 'once';
use File::Basename;
use Getopt::Tabular;
use NeuroDB::DBI;
use NeuroDB::Notify;

my $profile = undef;
my $upload_id = undef;
my $fullpath = undef;
my $phantom = undef;
my $patientname = 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."]
);

&Getopt::Tabular::GetOptions( \@opt_table, \@ARGV ) || exit 1;

################################################################
################ Get config setting#############################
################################################################
{ 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 2;
}

################################################################
################ Establish database connection #################
################################################################
my $dbh = &NeuroDB::DBI::connect_to_db(@Settings::db);

my ($stdoutbase, $stderrbase) = ("$Settings::data_dir/batch_output/imuploadstdout.log",
"$Settings::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 @linearray = ();
my @resultsarray = ();
my @patientname = ();
my @patientnamearray = ();
my @fullpath = ();
my @fullpatharray = ();
my @phantom = ();
my @phantomarray = ();
my @submitted = ();

my $counter = 0;

while(my $line = <STDIN>)
{
chomp $line;
my @linearray = split(" " , $line);
push (@resultsarray, @linearray);
@fullpath = $linearray[0];
@phantom = $linearray[1];
@patientname = $linearray[2];
push (@patientnamearray, @patientname);
push (@fullpatharray, @fullpath);
push (@phantomarray, @phantom);
}
close STDIN;

## foreach series, batch magic
foreach my $input (@resultsarray)
{
$counter++;
$stdout = $stdoutbase.$counter;
$stderr = $stderrbase.$counter;

#$stdout = '/dev/null';
#$stderr = '/dev/null';

$fullpath = $fullpatharray[$counter-1];
$phantom = $phantomarray[$counter-1];
$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 "The file on line $counter is not of type .tgz, tar.gz, or .zip and will not be processed\n";
exit(3);
}
if (($phantom eq '') || (($phantom ne 'N') && ($phantom ne 'Y'))) {
print "Make sure the Phantom entry is filled out " .
"with Y if the scan if for a phantom, and N otherwise\n";
exit(4);
}
if ($phantom eq 'N') {
if ($base ne $patientname) {
print "Make sure the patient name $patientname " .
"for non-phantom entries matches the file " .
"name $base in $path\n";
exit(5);
}
}
else {
if ($patientname ne '') {
print "Please leave the patient name blank " .
"for phantom entries\n";
exit(6);
}
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 = "$Settings::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 ($Settings::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 $Settings::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

Arguments:
$patientname: The patient name
$phantom : 'Y' if the entry is for a phantom, and 'N' othwrwose
$fullpath: Path to the uploaded file

Returns: $upload_id : The Upload ID
=cut


sub insertIntoMRIUpload {

my ( $dbhr, $patientname, $phantom, $fullpath ) = @_;
my $User = `whoami`;

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 0 for find to consider this -cmd true (in case we ever run it that way...)
exit(0);