Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batch upload and insertion of scans for projects not using the front-end imaging uploader #133

Merged
merged 6 commits into from
Aug 11, 2016
Merged
Show file tree
Hide file tree
Changes from 4 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
129 changes: 129 additions & 0 deletions batch_uploads_imageuploader
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#!/usr/bin/perl -w
use strict;
use warnings;
use File::Basename;
use Getopt::Tabular;

my $profile = 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" }
##############################################################################

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);
@patientname = $linearray[0];
@fullpath = $linearray[1];
@phantom = $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';

my $patientname = $patientnamearray[$counter-1];
my $fullpath = $fullpatharray[$counter-1];
my $phantom = $phantomarray[$counter-1];

## Ensure that
## 1) the uploaded file is of type .tgz or .tar.gz or .zip
## 2) the patient name and path entries are identical; this mimics the imaging uploader in the front-end
## 3) check that input file provides phantom details (Y for phantom, N for real candidates)
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(1);
}
if ($base ne $patientname) {
print "Make sure the patient name $patientname " .
"matches the file name $base in $path\n";
exit(2);
}
if (($phantom eq '') || ($phantom ne 'N') || ($phantom eq 'Y')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MounaSafiHarab If you modify your logic for this line to:

if (($phantom eq '') || (($phantom ne 'N') && ($phantom ne 'Y')))

the phantoms will get inserted.

print "Make sure the Phantom entry is filled out " .
"with Y if the scan if for a phantom, and N otherwise\n";
exit(3);
}

## 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 -phantom $phantom -patient_name $patientname $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 {
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;

## exit 0 for find to consider this -cmd true (in case we ever run it that way...)
exit(0);

93 changes: 87 additions & 6 deletions uploadNeuroDB/imaging_upload_file.pl
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@
$year + 1900,
$mon + 1, $mday, $hour, $min, $sec
);
my $profile = undef; # this should never be set unless you are in a
my $profile = undef; # this should never be set unless you are in a
# stable production environment
my $upload_id = undef; # The uploadID
my $phantom = undef; # Y if Phantom
my $patient_name_batch = undef; # Patient Name as obtained from batch upload
my $pname = undef;
my $template = "ImagingUpload-$hour-$min-XXXXXX"; # for tempdir
my $TmpDir_decompressed_folder =
tempdir( $template, TMPDIR => 1, CLEANUP => 1 );
Expand All @@ -52,6 +55,16 @@
"-upload_id", "string", 1, \$upload_id,
"The uploadID of the given scan uploaded"
],
[
"-patient_name_batch", "string", 1, \$patient_name_batch,
"The patient name; needed only when using batch_upload_imaguploader"
],
[
"-phantom", "string", 1, \$phantom,
"Y for phantom and N for real study participants; " .
" needed only when using batch_upload_imaguploader"

],
["-verbose", "boolean", 1, \$verbose, "Be verbose."],
[ "Advanced options", "section" ],
[ "Fancy options", "section" ]
Expand Down Expand Up @@ -109,10 +122,14 @@
exit 3;
}

if ( !$upload_id ) {
print $Help;
print "$Usage\n\tERROR: The Upload_id is missing \n\n";
exit 4;
# check if patient name is provided in case of no upload_id;
# expected when script is called from batch_upload_imageuploader
if ( !$upload_id ) {
if (!$patient_name_batch) {
print $Help;
print "$Usage\n\tERROR: No upload_id or patient name provided \n\n";
exit 4;
}
}

$uploaded_file = abs_path( $ARGV[0] );
Expand Down Expand Up @@ -158,7 +175,30 @@
############### Get Patient_name using UploadID#################
################################################################
################################################################
my $pname = getPnameUsingUploadID($upload_id);
if (!$patient_name_batch) {
$pname = getPnameUsingUploadID($upload_id);
}
else {
$pname = $patient_name_batch;
}

################################################################
############# For entries coming from batch_upload #############
########### Created entries into the mri_upload table ##########
################################################################
if ($patient_name_batch) {
if (!$phantom) {
print "Please ensure that entries from " .
"batch_upload_imageuploader contain " .
"a Y or N for phantoms or no phantoms, ".
"respectively\n";
}
else {
$upload_id = insertIntoMRIUpload($patient_name_batch,
$phantom,
$uploaded_file);
}
}

################################################################
################ ImagingUpload Object #########################
Expand Down Expand Up @@ -280,6 +320,47 @@ sub getPnameUsingUploadID {
return $patient_name;
}

################################################################
############### insertIntoMRIUpload ############################
################################################################
=pod
insertIntoMRIUpload()
Description:
- Insert into the mri_upload table entries for data coming
from batch_upload_imageuploader

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

Returns: $upload_id : The Upload ID
=cut


sub insertIntoMRIUpload {

my ( $patient_name_batch, $phantom, $uploaded_file ) = @_;
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,$patient_name_batch,
$phantom, $uploaded_file);

my $where = " WHERE mu.UploadLocation =?";
$query = "SELECT mu.UploadID FROM mri_upload mu";
$query .= $where;
my $sth = $dbh->prepare($query);
$sth->execute($uploaded_file);
my $upload_id = $sth->fetchrow_array;

return $upload_id;
}

################################################################
############### spool()#########################################
################################################################
Expand Down