Created
October 31, 2018 14:28
-
-
Save mattsouth/b75c7195ec064da8380533f654d8409c to your computer and use it in GitHub Desktop.
bash script to remove unwanted files in xnat using the XnatDataClient
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 | |
# bash script to remove unwanted files in xnat using the XnatDataClient | |
# iterates through all sessions looking for particular scan types with particular file types and file name patterns | |
# CLI parameters: | |
# 1. url - xnat url | |
# 2. username - xnat username | |
# 3. project - xnat project | |
# 4. scan type - e.g. "STRUCTURAL", "FLAIR" | |
# 5. file type - e.g. "NIFTI", "SNAPSHOT" | |
# 6. filesuffix - the suffix of the files to be remove, e.g. "_N.nii.gz" | |
# set this to 0,1,2 depending on required level of feedback | |
verbose=1 | |
# set this to 0 to actually delete the files | |
dryrun=0 | |
function log { | |
if [[ $verbose -gt 0 ]]; then | |
echo "spiderdel: $1" | |
fi | |
} | |
function debug { | |
if [[ $verbose -eq 2 ]]; then | |
echo "spiderdel (debug): $1" | |
fi | |
} | |
# check for correct number of arguments | |
if [ "$#" -ne 6 ]; then | |
echo -e "Incorrect number of arguments - expected 6" | |
echo "Usage: spiderdel.sh xnat-url xnat-username xnat-project scan_type file_type filesuffix" | |
else | |
echo -n "XNAT password: " | |
read -s password | |
echo | |
XNAT_URL=$1 | |
XNAT_USER=$2 | |
XNAT_PROJECT=$3 | |
SCAN_TYPE=$4 | |
FILE_TYPE=$5 | |
FILE_SUFFIX=$6 | |
session=$(./XnatDataClient -c -u $XNAT_USER -p $password -r $XNAT_URL/data/JSESSION) | |
if [[ $(echo "$session" | tail -n 1) == HTTP\ status\:\ 200 ]]; then | |
session_id=$(echo "$session" | head -c 32) | |
debug "tomcat session: $session_id" | |
# head -n -1 strips bottom line, i.e. "Z bytes" | |
sessions=$(./XnatDataClient -s $session_id -r $XNAT_URL/data/archive/projects/$XNAT_PROJECT/experiments\?format=csv | head -n -1 ) | |
# sessions is csv file with a header: | |
# xnat:subjectassessordata/id,ID,project,date,xsiType,label,insert_date,URI | |
session_count=$(($(echo "$sessions" | wc -l)-1)) | |
debug "session_count=$session_count from $sessions" | |
if [[ $session_count -gt 0 ]]; then | |
echo "$sessions" | while IFS="," read -r col1 col2 col3; do | |
if [ "$col2" != "ID" ]; then | |
# scans is csv file with a header: | |
# xnat_imagescandata_id,ID,type,quality,xsiType,note,series_description,URI | |
scans=$(./XnatDataClient -s $session_id -r $XNAT_URL/data/archive/experiments/$col2/scans?format=csv | head -n -1 | tail -n +2 | grep $SCAN_TYPE) | |
debug "scans: $scans" | |
echo "$scans" | while IFS="," read -r sc1 sc2 sc3; do | |
resources=$(./XnatDataClient -s $session_id -r $XNAT_URL/data/archive/experiments/$col2/scans/$sc2/resources?format=csv | head -n -1 | tail -n +2 | grep $FILE_TYPE) | |
debug "resources: $resources" | |
echo "$resources" | while IFS="," read -r rs1 rs2; do | |
files=$(./XnatDataClient -s $session_id -r $XNAT_URL/data/archive/experiments/$col2/scans/$sc2/resources/$rs1/files?format=csv | head -n -1 | tail -n +2) | |
debug "files: $files" | |
echo "$files" | while IFS="," read -r f1 f2; do | |
if [[ "$f1" == *$FILE_SUFFIX ]]; then | |
if [ $dryrun == 1 ]; then | |
echo "TO DELETE:" | |
echo "scan_id: $sc1" | |
echo "resource_id: $rs1" | |
echo "file: $f1" | |
else | |
#TODO: implement delete call. Can I use XnatDataClient? | |
echo "deleting: $f1" | |
./XnatDataClient -s $session_id -m DELETE -r $XNAT_URL/data/archive/experiments/$col2/scans/$sc2/resources/$rs1/files/$f1 | |
echo "deleted" | |
fi | |
fi | |
done | |
done | |
done | |
fi | |
done | |
fi | |
else | |
log "Failed: authentication failed" | |
log "xnat: $url" | |
log "user: $1" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment