Created
September 20, 2020 21:49
-
-
Save rherrick/4e3b0fbacecf7853b88ddfaf64352240 to your computer and use it in GitHub Desktop.
Get resource files from XNAT dataset collection
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
#!/usr/bin/env bash | |
showHelp() { | |
echo | |
echo "Usage: ${0} --server <URL> --username <USER> --password <PASSWORD> --project <PROJECT> --collection <COLLECTION_ID>" | |
echo | |
echo "Note: all options are required, with the exception of --password. If --password" | |
echo " is omitted, you must enter your password when the script runs." | |
echo | |
exit 255 | |
} | |
pause() { | |
read -n 1 -p "Press any key to continue > " | |
echo | |
} | |
while [ ! -z ${1} ]; do | |
case ${1} in | |
--collection|-c) | |
COLLECTION="${2}" | |
shift 2 | |
;; | |
--password|-p) | |
PASSWORD="${2}" | |
shift 2 | |
;; | |
--project|-P) | |
PROJECT="${2}" | |
shift 2 | |
;; | |
--server|-s) | |
SERVER="${2}" | |
shift 2 | |
;; | |
--username|-u) | |
USERNAME="${2}" | |
shift 2 | |
;; | |
*) | |
echo "Unknown option: ${1}" | |
showHelp | |
;; | |
esac | |
done | |
[[ -z ${COLLECTION} || -z ${PROJECT} || -z ${SERVER} || -z ${USERNAME} ]] && { echo "You must specify a value for all script options other than \"--password\""; showHelp; } | |
[[ -z ${PASSWORD} ]] && { | |
stty -echo | |
printf "Enter password for user ${USERNAME} on server ${SERVER}: " | |
read PASSWORD | |
stty echo | |
} | |
[[ -z ${PASSWORD} ]] && { echo "No password entered!"; showHelp; } | |
echo "Server: ${SERVER}" | |
echo "Username: ${USERNAME}" | |
echo "Project: ${PROJECT}" | |
echo "Collection: ${COLLECTION}" | |
SESSION="$(http --auth=${USERNAME}:${PASSWORD} --session=${USERNAME} --pretty=format --check-status --body ${SERVER}/data/JSESSION)" | |
[[ ${?} == 0 ]] && { echo "Authenticated with session ID ${SESSION}, beginning"; } || { echo "Failed to authenticate"; exit 1; } | |
# Get IDs of all collections in project | |
# http --session=${USERNAME} --pretty=format --body ${SERVER}/xapi/sets/collections/projects/${PROJECT} | jq '.[] | .id' | sort | tr -d '"' | |
# Select a collection | |
# Could select from the list returned by the call above. For now, collection is a required input parameter. | |
# Get a list of files in the collection: commented call below is plain call to render full collection object, second renders just the file paths, third extracts experiment label. Format of path is something like: | |
# | |
# /data/xnat/archive/AbdomenCT-RSNA2019/arc001/48/SCANS/1/NIFTI/input_IMAGES_20191025163500_1.nii | |
# | |
# http --session=${USERNAME} --pretty=format --body ${SERVER}/xapi/sets/collections/${COLLECTION} format==json | |
# http --session=${USERNAME} --pretty=format --body ${SERVER}/xapi/sets/collections/${COLLECTION} format==json | jq '.files | .[] | .image, .label' | sort | tr -d '"' | |
# Validate collection ID | |
http --session=${USERNAME} --pretty=format --check-status --body ${SERVER}/xapi/sets/collections/${COLLECTION} format==json &> /dev/null | |
[[ ${?} != 0 ]] && { echo "The collection ID ${COLLECTION} doesn't appear to be a valid ID. Please check the ID and try again."; exit 3; } | |
http --session=${USERNAME} --pretty=format --body ${SERVER}/xapi/sets/collections/${COLLECTION} format==json | jq '.files | .[] | .image, .label' | sort | tr -d '"' | while read DATA_PATH; do | |
EXPERIMENT="$(echo ${DATA_PATH} | sed -E "s#^.*/${PROJECT}/arc[0-9]+/([^/]+)/.*#\1#")" | |
EXPT_ID="$(http --session=${USERNAME} --pretty=format --ignore-stdin ${SERVER}/data/projects/${PROJECT}/experiments/${EXPERIMENT} format==json | jq '.items | .[] | .data_fields.ID' | tr -d '"')" | |
SCAN="$(echo ${DATA_PATH} | sed -E "s#^.*/${PROJECT}/arc[0-9]+/${EXPERIMENT}/SCANS/([^/]+)/.*#\1#")" | |
RESOURCE="$(echo ${DATA_PATH} | sed -E "s#^.*/${PROJECT}/arc[0-9]+/${EXPERIMENT}/SCANS/${SCAN}/([^/]+)/.*#\1#")" | |
FILE="$(echo ${DATA_PATH} | sed -E "s#^.*/${PROJECT}/arc[0-9]+/${EXPERIMENT}/SCANS/${SCAN}/${RESOURCE}/(.*)#\1#")" | |
OUTPUT_FOLDER="${PROJECT}/${SUBJECT}/${EXPERIMENT}/${SCAN}/${RESOURCE}" | |
echo "Downloading file ${FILE} to ${OUTPUT_FOLDER}" | |
mkdir -p ${OUTPUT_FOLDER} | |
http --session=${USERNAME} --pretty=format --download --output=${OUTPUT_FOLDER}/${FILE} --ignore-stdin --body ${SERVER}/data/experiments/${EXPT_ID}/scans/${SCAN}/resources/${RESOURCE}/files/${FILE} | |
done | |
# Close session | |
http --session=${USERNAME} --pretty=format --check-status --body DELETE ${SERVER}/data/JSESSION |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment