Skip to content

Instantly share code, notes, and snippets.

@rviki84
Created January 28, 2021 00:17
Show Gist options
  • Save rviki84/626e14d02b9c2378147bad52f01356db to your computer and use it in GitHub Desktop.
Save rviki84/626e14d02b9c2378147bad52f01356db to your computer and use it in GitHub Desktop.
Uploads images listed in a csv spreadsheet into an xnat server
#!/bin/bash
print_usage() {
echo "
Uploads images listed in a csv spreadsheet into an xnat server
Uses the script create-session.sh
The spreadsheet should contain the following columns, separated by spaces:
<project> <subject-id> <acquisition-date> <session-type (MR or CT)> <session-label> <scan-label> <image-file-path>
The subject ids have to be already present in the database. This can be achieved using the
'upload spreadsheet' function from the web interface.
Date format: MM\/DD\/YY (US style, month first!)
Usage:
./upload-imagedata.sh (-h|--host) <server URI> (-u|--user) <username> (-p|--password) <password>
(-i|--input) <csv spreadsheet>
"
exit 1
}
check_next_arg() {
if [ $# -lt 2 ]
then
echo "Missing argument."
print_usage
fi
next_arg="$2"
if [ ${#next_arg} -eq 0 ]
then
echo "Value is missing for option $1."
print_usage
fi
if [ "${next_arg:0:1}" == "-" ]
then
echo "Value is missing for option $1."
print_usage
fi
}
while [ $# -gt 0 ]
do
if [ "$1" == "-h" ] || [ "$1" == "--host" ]
then
check_next_arg ${@}
xnat="$2"
shift 2
elif [ "$1" == "-u" ] || [ "$1" == "--user" ]
then
check_next_arg ${@}
user="$2"
shift 2
elif [ "$1" == "-p" ] || [ "$1" == "--password" ]
then
password="$2"
shift 2
elif [ "$1" == "-i" ] || [ "$1" == "--input" ]
then
input="$2"
shift 2
else
echo "Unknown argument: $1"
print_usage
fi
done
if [ "$user" == "" ] || [ "$xnat" == "" ] || [ "$input" == "" ]
then
echo "missing one of these required options:"
echo "xnat, user, input"
print_usage
fi
if [ "$password" == "" ]
then
read -p "password: " -s password
echo
fi
if [ $password == "" ]
then
echo "Invalid password."
print_usage
fi
# Path to the curl binary
curl_dir=/usr/bin
# Remove the last character of the url if it finishes by "/"
xnat=${xnat%"/"}
echo "input data :"
echo "xnat: $xnat"
echo "user: $user"
echo "password: $password"
echo "input: $input"
# ------------------------------------------------------------------------
# XNAT
# ------------------------------------------------------------------------
function check_response {
if [ $1 -ne 0 ]
then
echo "Connection to the XNAT server has failed."
success=0;
else
success=1;
fi
if grep -q "Login attempt failed" <<< $2
then
echo "Login attempt failed."
echo $2
success=0;
else
success=1;
fi
}
Curl=$curl_dir/curl
curlArgs="-u $user:$password --insecure --silent"
curlArgsProgress="-u $user:$password --insecure"
while read p; do
project=`echo $p | awk -F, '{print $1}'`
subject=`echo $p | awk -F, '{print $2}'`
date=`echo $p | awk -F, '{print $3}'`
type=`echo $p | awk -F, '{print $4}' | tr '[A-Z]' '[a-z]'`
experiment=`echo $p | awk -F, '{print $5}'`
scan=`echo $p | awk -F, '{print $6}' | awk '{gsub(/[[:punct:]]/,"-")}1'`
scanType=`echo $p | awk -F, '{print $7}'`
file=`echo $p | awk -F, '{print $8}'`
echo "project: $project"
echo "subject: $subject"
echo "date: $date"
echo "type: $type"
echo "experiment: $experiment"
echo "scan: $scan"
echo "scanType: $scanType"
echo "file: $file"
# define imageid from the adni xml description (specific ADNI upload)
# imageid=`echo $file | sed 's/.*_I\(.*\).xml/\1/g'`
# find the actual image file (specific ADNI upload)
# imagefile=`ls ../renamed/*_$subject*$imageid*.nii.gz`
# imagefile=`ls $imageid.zip`
# define imageid as the file without extension
imageid=${file%\.*}
# find the actual image file full path (more global use)
imagefile=$(cd $(dirname $file); pwd)/$(basename $file)
echo "imagefile: $imagefile"
if [ ! -f "$imagefile" ]; then
echo "image file not found: $imagefile"
echo "skipping imageid $imageid"
skippedids=$skippedids$imageid"\n";
continue
fi
if [ "$project" == "" ] || [ "$subject" == "" ] || [ "$experiment" == "" ] ||
[ "$scan" == "" ] || [ "$date" == "" ] || [ "$type" == "" ] || [ "$scanType" == "" ] || [ "$file" == "" ]
then
echo "missing one of these required arguments for upload:"
echo "project, subject, type, experiment, scan, scanType, date, file"
echo "skipping imageid $imageid"
skippedids=$skippedids$imageid"\n";
continue
fi
session="${experiment}"
xnatUri=$xnat/REST
projectUri=$xnatUri/projects/$project
subjectUri=$projectUri/subjects/$subject
experimentUri=$subjectUri/experiments/$session
# ------------------------------------------------------------------------
# Checking project
# ------------------------------------------------------------------------
echo -n "Checking project... "
response=$($Curl $curlArgs -X GET "$projectUri?format=xml")
exitVal=$?
check_response $exitVal $response
if [ $success -eq 0 ]
then
echo "skipping imageid $imageid"
skippedids=$skippedids$imageid"\n";
continue
fi
if grep -q "Unable to find the specified subjectThe server has not found anything matching the request URI" <<< $response
then
echo "Project does not exist"
echo "skipping imageid $imageid"
skippedids=$skippedids$imageid"\n";
continue;
else
echo "OK"
fi
# ------------------------------------------------------------------------
# Checking subject
# ------------------------------------------------------------------------
echo -n "Checking subject... "
response=$($Curl $curlArgs -X GET "$subjectUri?format=xml")
exitVal=$?
check_response $exitVal $response
if [ $success -eq 0 ]
then
echo "skipping imageid $imageid"
skippedids=$skippedids$imageid"\n";
continue;
fi
if grep -q "Unable to find the specified subject" <<< $response
then
echo "Subject does not exist: $subject"
echo "skipping imageid $imageid"
skippedids=$skippedids$imageid"\n";
continue;
else
echo "OK"
fi
# ------------------------------------------------------------------------
# Checking experiment
# ------------------------------------------------------------------------
echo -n "Checking experiment... "
response=$($Curl $curlArgs -X GET "$experimentUri?format=xml")
exitVal=$?
check_response $exitVal $response
if [ $success -eq 0 ]
then
echo "skipping imageid $imageid"
skippedids=$skippedids$imageid"\n";
continue;
fi
grep -q "Unable to find the specified experiment." <<< $response
sessionDoesNotExist=$?
if [ $sessionDoesNotExist -eq 0 ]
then
# ------------------------------------------------------------------------
# Creating session
# ------------------------------------------------------------------------
echo
echo "Session does not exist."
echo -n "Creating session... "
experimentArgs="?xsi:type=xnat:"$type"SessionData"
experimentArgs+="&xnat:"$type"SessionData/date=$date"
experimentArgs+="&xnat:"$type"SessionData/label=$session"
command="$Curl $curlArgs -X PUT \"$experimentUri$experimentArgs\""
echo "command: $command"
response=$($Curl $curlArgs -X PUT "$experimentUri$experimentArgs")
exitVal=$?
check_response $exitVal $response
if [ $success -eq 0 ]
then
echo "skipping imageid $imageid"
skippedids=$skippedids$imageid"\n";
continue;
fi
grep -q "The server encountered an unexpected condition which prevented it from fulfilling the request" <<< $response
creatingSessionFailed=$?
if [ $creatingSessionFailed -eq 0 ]
then
echo
echo "Cannot create the session."
echo "Exit code: $exitVal"
echo "Response from server: $response"
echo "skipping imageid $imageid"
skippedids=$skippedids$imageid"\n";
continue;
else
echo "OK"
fi
experimentId=$response
echo "New experiment created: $experimentId"
else
echo "OK"
fi
# ------------------------------------------------------------------------
# Creating scan
# ------------------------------------------------------------------------
#scan="1"
if [ "$experimentId" != "" ]
then
experimentUri="$subjectUri/experiments/$experimentId"
fi
scanUri=$experimentUri/scans/$scan
echo -n "Creating scan... "
scanArgs="?xsi:type=xnat:"$type"ScanData"
scanArgs+="&xnat:"$type"ScanData/type=$scanType"
scanArgs+="&xnat:"$type"ScanData/quality=usable"
command="$Curl $curlArgs -X PUT \"$scanUri$scanArgs\""
echo "command: $command"
response=$($Curl $curlArgs -X PUT "$scanUri$scanArgs")
exitVal=$?
check_response $exitVal $response
if [ $success -eq 0 ]
then
echo "skipping imageid $imageid"
skippedids=$skippedids$imageid"\n";
continue;
fi
grep -q "The server encountered an unexpected condition which prevented it from fulfilling the request" <<< $response
creatingScanFailed=$?
if [ $creatingScanFailed -eq 0 ]
then
echo
echo "Cannot create the scan."
echo "Exit code: $exitVal"
echo "Response from server: $response"
echo "skipping imageid $imageid"
skippedids=$skippedids$imageid"\n";
continue;
else
echo "OK"
fi
# ------------------------------------------------------------------------
# Uploading file (specific for DICOM file upload)
# ------------------------------------------------------------------------
echo -n "Uploading file... "
fileName=${imagefile##*/}
fileUri="$scanUri/resources/DICOM/files/$fileName"
fileArgs="?format=DICOM"
fileArgs+="&content=RAW"
fileArgs+="&overwrite=true"
fileArgs+="&inbody=true"
response=$($Curl $curlArgsProgress -X PUT --data-binary @$imagefile "$fileUri$fileArgs" )
exitVal=$?
check_response $exitVal $response
if [ $success -eq 0 ]
then
echo "skipping imageid $imageid"
skippedids=$skippedids$imageid"\n";
continue;
fi
grep -q "Status page" <<< $response
commandFailed=$?
if [ $commandFailed -eq 0 ]
then
echo
echo "Cannot upload the file."
echo "Exit code: $exitVal"
echo "Response from server: $response"
echo "skipping imageid $imageid"
skippedids=$skippedids$imageid"\n";
continue;
else
echo "OK"
fi
done < "$input"
echo -e $skippedids > skippedids.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment