Last active
August 29, 2015 14:26
-
-
Save mattsouth/1a1a8b4dd3d81f7d6452 to your computer and use it in GitHub Desktop.
Script to upload dicom images for a particular subject session to the tng xnat instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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. | |
# 3. The script is located in the same directory as the session directories | |
# | |
# Note that this script zips the dicoms up before sending, instead of sending them all individually | |
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 directory is an existing 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" | |
dirname=${d%/} # remove trailing slash | |
zip $dirname.zip * | |
scan=${dirname//\./_} # swap periods for underscores as they break xnat | |
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" | |
curl -X PUT -u $1:$2 $url/subjects/$subject_id/experiments/$5/scans/$scan/resources/DICOM/files?extract=true -F "$dirname.zip=@./$dirname.zip" | |
echo "Files uploaded" | |
rm $dirname.zip | |
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