[030aeb]: / scripts / bilateral-knee-dess

Download this file

124 lines (95 with data), 3.4 kB

#!/bin/bash

# 1. Separate dicom files from bilateral dess knee scan into left dicoms and right dicoms
# 2. Run qDESS analysis on left and right knees
#
# @usage (from terminal/command line):
# ./bilateral-knee-dess PATH_TO_DICOM_FOLDER PATIENT_ID
# eg: "./bilateral-knee-dess /Users/data/Patient07/005 07"
#
# @initialization protocol:
#   1. run "chmod +x bilateral-knee-dess" from the command line
#   2. Update `WEIGHTS_DIRECTORY` field below to point to the appropriate weights
#   3. Update 'TISSUES' field if additional tissues desired
#      e.g. '--fc --tc' for femoral cartilage and tibial cartilage
#
# @assumptions:
#   - Scan volumes are acquired in sagittal direction from patient left to patient right
#     (i.e. left knee, then right knee)
#   - Volume slices (1...N) - Left knee slices (1...N/2), right knee slices (N/2 + 1, ... N)
#   - Left knee - lateral --> medial
#   - Right knee - medial --> lateral
#
# @author: Arjun Desai, Stanford University
#          (c) Stanford University, 2018


SCRIPTS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
DOSMA_DIR="$( cd "$( dirname "${SCRIPTS_DIR}" )" >/dev/null 2>&1 && pwd )"

WEIGHTS_DIRECTORY=$DOSMA_DIR/"../weights"
TISSUES='--fc'

if [[ -z "$WEIGHTS_DIRECTORY" ]]; then
    echo "Please define WEIGHTS_DIRECTORY in script. Use the absolute path"
    exit 125
fi

if [[ $# -lt 1 ]]; then
	echo "Please provide path to dicom folder and patient id"
	exit 125
fi

if [[ $# -lt 2 ]]; then
	echo "Please provide patient id"
	exit 125
fi

DICOM_PATH=$1
PID=$2
echo "dicom path: $DICOM_PATH"
echo "patient id: $PID"

# get list of dicoms in this folder
dicom_list_str=$(find $DICOM_PATH -type f -name "*.dcm" -maxdepth 1 | sort)
dicom_array=()
for filepath in $dicom_list_str
do
    dicom_array+=($filepath)
done

echo "Number of dicoms: ${#dicom_array[@]}"

# halfpoint in dicom list to split 
half_point=$((${#dicom_array[@]} / 2))

# Assume directories exist, if they don't set this to false
DIRS_EXIST=1

LEFT_DIR="$DICOM_PATH/LEFT/"
if [[ ! -d "$LEFT_DIR" ]]; then
	mkdir $LEFT_DIR
	DIRS_EXIST=0
fi

RIGHT_DIR="$DICOM_PATH/RIGHT/"
if [[ ! -d "$RIGHT_DIR" ]]; then
	mkdir $RIGHT_DIR
    DIRS_EXIST=0
fi

# if the directories already exist, assume the data has already been separated into different folders
if [[ $DIRS_EXIST -eq 0 ]]; then
    counter=1
    for filepath in ${dicom_array[@]}
    do

        filename=$(basename $filepath)

        if [[ $counter -gt $half_point ]]; then
            # store in right directory
            cp $filepath $RIGHT_DIR
        else
            cp $filepath $LEFT_DIR
        fi

        counter=$(expr $counter + 1)
    done
fi

cd ..

base_dicom_path=$(dirname $DICOM_PATH)
base_filename=$(basename $DICOM_PATH)
DATA_DIR="$base_dicom_path/data/$base_filename"

echo "Save path: $DATA_DIR"

# 2. run analysis on the qdess files
#    if data directory already exist, skip analysis

DATA_DIR_LEFT="$DATA_DIR/LEFT"

dosma --d $LEFT_DIR --s $DATA_DIR_LEFT qdess $TISSUES segment --rms --weights_dir $WEIGHTS_DIRECTORY
dosma --l $DATA_DIR_LEFT qdess $TISSUES t2
dosma --l $DATA_DIR_LEFT knee --pid $PID $TISSUES


DATA_DIR_RIGHT="$DATA_DIR/RIGHT"

dosma --d $RIGHT_DIR --s $DATA_DIR_RIGHT qdess $TISSUES segment --rms --weights_dir $WEIGHTS_DIRECTORY
dosma --l $DATA_DIR_RIGHT qdess $TISSUES t2
dosma --l $DATA_DIR_RIGHT knee --ml --pid $PID $TISSUES