Switch to unified view

a b/scripts/bilateral-knee-dess
1
#!/bin/bash
2
3
# 1. Separate dicom files from bilateral dess knee scan into left dicoms and right dicoms
4
# 2. Run qDESS analysis on left and right knees
5
#
6
# @usage (from terminal/command line):
7
# ./bilateral-knee-dess PATH_TO_DICOM_FOLDER PATIENT_ID
8
# eg: "./bilateral-knee-dess /Users/data/Patient07/005 07"
9
#
10
# @initialization protocol:
11
#   1. run "chmod +x bilateral-knee-dess" from the command line
12
#   2. Update `WEIGHTS_DIRECTORY` field below to point to the appropriate weights
13
#   3. Update 'TISSUES' field if additional tissues desired
14
#      e.g. '--fc --tc' for femoral cartilage and tibial cartilage
15
#
16
# @assumptions:
17
#   - Scan volumes are acquired in sagittal direction from patient left to patient right
18
#     (i.e. left knee, then right knee)
19
#   - Volume slices (1...N) - Left knee slices (1...N/2), right knee slices (N/2 + 1, ... N)
20
#   - Left knee - lateral --> medial
21
#   - Right knee - medial --> lateral
22
#
23
# @author: Arjun Desai, Stanford University
24
#          (c) Stanford University, 2018
25
26
27
SCRIPTS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
28
DOSMA_DIR="$( cd "$( dirname "${SCRIPTS_DIR}" )" >/dev/null 2>&1 && pwd )"
29
30
WEIGHTS_DIRECTORY=$DOSMA_DIR/"../weights"
31
TISSUES='--fc'
32
33
if [[ -z "$WEIGHTS_DIRECTORY" ]]; then
34
    echo "Please define WEIGHTS_DIRECTORY in script. Use the absolute path"
35
    exit 125
36
fi
37
38
if [[ $# -lt 1 ]]; then
39
    echo "Please provide path to dicom folder and patient id"
40
    exit 125
41
fi
42
43
if [[ $# -lt 2 ]]; then
44
    echo "Please provide patient id"
45
    exit 125
46
fi
47
48
DICOM_PATH=$1
49
PID=$2
50
echo "dicom path: $DICOM_PATH"
51
echo "patient id: $PID"
52
53
# get list of dicoms in this folder
54
dicom_list_str=$(find $DICOM_PATH -type f -name "*.dcm" -maxdepth 1 | sort)
55
dicom_array=()
56
for filepath in $dicom_list_str
57
do
58
    dicom_array+=($filepath)
59
done
60
61
echo "Number of dicoms: ${#dicom_array[@]}"
62
63
# halfpoint in dicom list to split 
64
half_point=$((${#dicom_array[@]} / 2))
65
66
# Assume directories exist, if they don't set this to false
67
DIRS_EXIST=1
68
69
LEFT_DIR="$DICOM_PATH/LEFT/"
70
if [[ ! -d "$LEFT_DIR" ]]; then
71
    mkdir $LEFT_DIR
72
    DIRS_EXIST=0
73
fi
74
75
RIGHT_DIR="$DICOM_PATH/RIGHT/"
76
if [[ ! -d "$RIGHT_DIR" ]]; then
77
    mkdir $RIGHT_DIR
78
    DIRS_EXIST=0
79
fi
80
81
# if the directories already exist, assume the data has already been separated into different folders
82
if [[ $DIRS_EXIST -eq 0 ]]; then
83
    counter=1
84
    for filepath in ${dicom_array[@]}
85
    do
86
87
        filename=$(basename $filepath)
88
89
        if [[ $counter -gt $half_point ]]; then
90
            # store in right directory
91
            cp $filepath $RIGHT_DIR
92
        else
93
            cp $filepath $LEFT_DIR
94
        fi
95
96
        counter=$(expr $counter + 1)
97
    done
98
fi
99
100
cd ..
101
102
base_dicom_path=$(dirname $DICOM_PATH)
103
base_filename=$(basename $DICOM_PATH)
104
DATA_DIR="$base_dicom_path/data/$base_filename"
105
106
echo "Save path: $DATA_DIR"
107
108
# 2. run analysis on the qdess files
109
#    if data directory already exist, skip analysis
110
111
DATA_DIR_LEFT="$DATA_DIR/LEFT"
112
113
dosma --d $LEFT_DIR --s $DATA_DIR_LEFT qdess $TISSUES segment --rms --weights_dir $WEIGHTS_DIRECTORY
114
dosma --l $DATA_DIR_LEFT qdess $TISSUES t2
115
dosma --l $DATA_DIR_LEFT knee --pid $PID $TISSUES
116
117
118
DATA_DIR_RIGHT="$DATA_DIR/RIGHT"
119
120
dosma --d $RIGHT_DIR --s $DATA_DIR_RIGHT qdess $TISSUES segment --rms --weights_dir $WEIGHTS_DIRECTORY
121
dosma --l $DATA_DIR_RIGHT qdess $TISSUES t2
122
dosma --l $DATA_DIR_RIGHT knee --ml --pid $PID $TISSUES
123