|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Copy certain DICOMs of a subject from one project to another on XNAT |
| 4 | +# |
| 5 | +# A DAX installation is required, and its python venv must be active. |
| 6 | + |
| 7 | +# Default options |
| 8 | +skipvars="no" |
| 9 | + |
| 10 | +# Parse options |
| 11 | +while [[ $# -gt 0 ]] |
| 12 | +do |
| 13 | + key="${1}" |
| 14 | + case "${key}" in |
| 15 | + --source_project) |
| 16 | + source_project="${2}"; shift; shift ;; |
| 17 | + --dest_project) |
| 18 | + dest_project="${2}"; shift; shift ;; |
| 19 | + --subjects) |
| 20 | + subjects="${2}"; shift; shift ;; |
| 21 | + --scan_types) |
| 22 | + scan_types="${2}"; shift; shift ;; |
| 23 | + --skipvars) |
| 24 | + skipvars="yes"; shift ;; |
| 25 | + *) |
| 26 | + echo Unknown input "${1}"; shift ;; |
| 27 | + esac |
| 28 | +done |
| 29 | + |
| 30 | +# Check for params |
| 31 | +if [ -z "${source_project}" ] \ |
| 32 | + || [ -z "${dest_project}" ] \ |
| 33 | + || [ -z "${subjects}" ] \ |
| 34 | + || [ -z "${scan_types}" ] ; then |
| 35 | + cat << HERE |
| 36 | +Usage example: |
| 37 | + ${0} \\ |
| 38 | + --source_project SOURCE_PROJ \\ |
| 39 | + --dest_project DESTINATION_PROJ \\ |
| 40 | + --subjects 123456,234567,345678 \\ |
| 41 | + --scan_types "T1,Rest" |
| 42 | +HERE |
| 43 | + exit 0 |
| 44 | +fi |
| 45 | + |
| 46 | + |
| 47 | +# Where are our scripts? We need to find the python code |
| 48 | +function realpath() { |
| 49 | + if ! pushd $1 &> /dev/null; then |
| 50 | + pushd ${1##*/} &> /dev/null |
| 51 | + echo $( pwd -P )/${1%/*} |
| 52 | + else |
| 53 | + pwd -P |
| 54 | + fi |
| 55 | + popd > /dev/null |
| 56 | +} |
| 57 | +script_dir=$(realpath $(dirname "${0}")) |
| 58 | + |
| 59 | + |
| 60 | +# Check for subjects already present in dest project |
| 61 | +echo "Checking subject list ${subjects}" |
| 62 | +subjlist=$(echo ${subjects} | tr ',' ' ') |
| 63 | +for subj in ${subjlist} ; do |
| 64 | + python "${script_dir}"/check_for_subject.py "${dest_project}" "${subj}" \ |
| 65 | + || exit 1 |
| 66 | +done |
| 67 | + |
| 68 | + |
| 69 | +# Make temporary directory |
| 70 | +tmp_dir=$(mktemp -d copy_subject.XXXXXX) || exit 1 |
| 71 | + |
| 72 | +# Download |
| 73 | +Xnatdownload -p "${source_project}" -d "${tmp_dir}" --subj "${subjects}" -s "${scan_types}" --rs DICOM |
| 74 | + |
| 75 | +# Replace project name in download report CSV |
| 76 | +cat "${tmp_dir}"/download_report.csv | \ |
| 77 | + sed s/^scan,${source_project}/scan,${dest_project}/ > \ |
| 78 | + "${tmp_dir}"/upload.csv |
| 79 | + |
| 80 | +# Upload. Note, if target resource already exists, Xnatupload prints a warning and does |
| 81 | +# not do the upload. |
| 82 | +Xnatupload --csv "${tmp_dir}"/upload.csv |
| 83 | + |
| 84 | +# Clean up |
| 85 | +if [ -d "${tmp_dir}" ] ; then |
| 86 | + rm -fr "${tmp_dir}" |
| 87 | +fi |
| 88 | + |
| 89 | + |
| 90 | +# Set subject and session variables |
| 91 | +if [[ "${skipvars}" == "no" ]]; then |
| 92 | + subjlist=$(echo ${subjects} | tr ',' ' ') |
| 93 | + for subj in ${subjlist} ; do |
| 94 | + echo "Copying subject and session vars for ${subj}" |
| 95 | + python "${script_dir}"/set_vars.py "${source_project}" "${dest_project}" "${subj}" |
| 96 | + done |
| 97 | +fi |
0 commit comments