Skip to content

Instantly share code, notes, and snippets.

@mattsouth
Last active August 29, 2015 14:25
Show Gist options
  • Save mattsouth/295a889a15bacf5929c5 to your computer and use it in GitHub Desktop.
Save mattsouth/295a889a15bacf5929c5 to your computer and use it in GitHub Desktop.
Bash script for uploading a directory of dicom images to xnat 1.6.4
#!/bin/bash
# uploads dicom images for a particular subject session to the tng xnat instance
# takes five parameters:
# 1: username - xnat username
# 2: password - xnat password
# 3: project - project id
# 4: scan date - session date e.g. '01/02/14'
# 5: directory - the subdirectory that contains the session files
#
# Assumptions!
# 1. The directory name is composed of ${PID}_${SUBJECTNO} and the subject ID can be
# constructed as WH_${SUBJECTNO}. In this script the PID is assumed to be 11 chars long.
# 2. There is a set of sub-directories under the specified directory each
# corresponding to a separate scan and that the dicom image files are all in
# those scan sub-directories.
#url="https://tng.psych.ox.ac.uk/xnat/data/archive/projects/$3"
url="http://192.168.50.50:8080/xnat/data/archive/projects/$3"
function mapType {
if [[ "$1" == DTI_*]]; then
type='DTI'
elif [[ "$1" == ep-moco-nav-set* ]]; then
type='Navigator'
elif [[ "$1" == fieldmap_* ]]; then
type='FieldMap'
elif [[ "$1" == gre_0.8inplane_5mm_* ]]; then
type='T2*'
elif [[ "$1" == localizer_* ]]; then
type='Localizer'
elif [[ "$1" == mb6_hcp_2mmiso_TR1300__* ]]; then
type='Resting-state'
elif [[ "$1" == t2_tirm_tra_dark-fluid_3mm_9_1 ]]; then
type='FLAIR'
elif [[ "$1" == tfl-multiecho-epinav-711_NoMoCo_* ]]; then
type='T1'
elif [[ "$1" == to_PCASL_perf_ep2d_bold_BGS_inc_PLD* ]]; then
type='ASL'
else
# default to directory name without the trailing slash
type="${1%/}"
fi
}
# step 1 - check for directory
if [ -d $5 ]; then
cd $5
# step 2 - check for project
project_exists=$(curl -I -u $1:$2 $url)
if [[ $project_exists == HTTP/1.1\ 200* ]]; then
echo "Confirmed project $3"
subject_id="WH_${5:12}"
# step 3 - check for subject and create if it doesnt exist
subject_exists=$(curl -I -u $1:$2 $url/subjects/$subject_id)
if [[ $subject_exists == HTTP/1.1\ 200* ]]; then
echo "Using existing subject $subject_id"
else
echo "Creating subject $subject_id"
curl -X PUT -u $1:$2 $url/subjects/$subject_id
fi
# step 4 - create session
session=$(curl -X PUT -u $1:$2 $url/subjects/$subject_id/experiments/$5?xnat:mrSessionData/date=$4)
echo "Created session $session"
# step 5 - iterate through scans
for d in */ ; do
cd $d
# assign $type
mapType $d
echo "mapped type: $type from directory $d"
scan=${d%/}
curl -X PUT -u $1:$2 $url/subjects/$subject_id/experiments/$5/scans/$scan?xsiType=xnat:mrScanData\&xnat:mrScanData/type=$type
# create resource
curl -X PUT -u $1:$2 $url/subjects/$subject_id/experiments/$5/scans/$scan/resources/DICOM?format=DICOM\&content=${type}_RAW
echo "Created scan $scan from directory $5/$d"
for f in * ; do
curl -X PUT -u $1:$2 $url/subjects/$subject_id/experiments/$5/scans/$scan/resources/DICOM/files/$f -F "$f=@./$f"
echo "Upload file $f"
done
cd ..
done
cd ..
else
echo "project $3 not found - do the passed credentials have access to it?"
fi
else
echo "unable to find directory $5"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment