Skip to content

Instantly share code, notes, and snippets.

@dmd
Created March 30, 2021 00:10
Show Gist options
  • Save dmd/681a4f2f079ec1c3d7d21260767105a0 to your computer and use it in GitHub Desktop.
Save dmd/681a4f2f079ec1c3d7d21260767105a0 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# 2021 ddrucker
set -o errexit
if [ $# -lt 2 ]; then
echo "Usage: $0 xnat_username accessionid [accessionid accessionid ...]"
echo "accessionid can be the MGB-generated one, which (typically) starts with E,"
echo "OR the XNAT-generated one, which starts with XNAT_E."
exit 1
fi
URL=https://image-test.mclean.harvard.edu
WHO=$1
shift
## auth to xnat
echo "Please enter your XNAT password."
JSESSIONID=$(curl -s -u $WHO "$URL/data/JSESSION")
if [[ "$JSESSIONID" == *"Unauthorized"* ]]; then
echo "Login failed."
exit 1
fi
## retrieve each one
for ACC in $@; do
echo "Retrieving $ACC ..."
if [[ $ACC == XNAT_S* ]]; then
echo "$ACC is an XNAT Subject ID, not a Session id."
echo "Session IDs start with XNAT_E."
continue
fi
if [[ $ACC == XNAT_E* ]]; then
SESSIONID=$ACC
else
SESSIONID=$(curl -s -b JSESSIONID=$JSESSIONID "$URL/data/experiments?format=json&label=$ACC" | jq -r '.ResultSet.Result[0].ID' )
if [[ $SESSIONID == null ]]; then
echo "Something went wrong trying to get $ACC."
continue
fi
fi
curl -b JSESSIONID=$JSESSIONID "$URL/data/experiments/$SESSIONID/scans/ALL/files?format=zip" -o $ACC.zip
7z x $ACC.zip
rm -f $ACC.zip
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment