xtract_viewer 5.53 KB
Newer Older
Saad Jbabdi's avatar
Saad Jbabdi committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/bin/bash

#   Copyright (C) 2019 University of Oxford
#
#   SHCOPYRIGHT

# Script to view output of XTRACT in fsleyes
# Written by Shaun Warrington 07/2019
# Modified by Saad Jbabdi 08/2019

Usage() {
    cat << EOF

Usage:
    xtract_viewer -dir <xtractDir> -str <structuresFile> [options]

    Compulsory arguments:

       -dir <folder>                     Path to Xtract folder
                                         e.g. for subject, /data/subID/Xtract
                                              for atlas, /data/tract_atlases

       -str <file>                       Structures file (can be same as xtract input structures list)

    Optional arguments:

       -sub                              Use this flag to view a single subject's data
                                                          (look's for densityNorm.nii.gz under <dir>/tracts/)
                                         Default is to view a tract atlas (looks for <structure>.nii.gz under <dir>)

       -mip                              Use this flag to view tracts as mips, window of 5 slices, spline interpolation

       -thr <number> <number>            The lower and upper thresholds applied to the tracts for viewing
                                         Default for subject = 0.001 0.1, default for atlas = 0.3 1.0

       -brain                            The brain image to use for the background overlay - must be in the same space as tracts.
                                         Default is the FSL_HCP065_FA map
EOF
    exit 1
}

echo "---"
echo "----"
echo "-----"
echo "------"
echo "-------"
echo "------- Xtract Viewer"
echo "-------"
echo "------"
echo "-----"
echo "----"
echo "---"

[ "$1" = "" ] && Usage

# Set defaults
sub=0
mip=0
thr=""
brain=""

# the colourmap options
cmaps=(blue red green blue-lightblue pink red-yellow cool yellow copper \
 hot hsv coolwarm spring summer winter Oranges)
cL="${#cmaps[@]}" # length of array for colourmap loop control

# Parse command-line arguments
while [ ! -z "$1" ];do
    case "$1" in
	-dir) dir=$2;shift;;
	-str) str=$2;shift;;
	-sub) sub=1;;
	-thr) thr=$2;uthr=$3;shift;shift;;
	-mip) mip=1;;
	-brain) brain=$2;shift;;
	*) echo "Unknown option '$1'";exit 1;;
    esac
    shift
done

# Default threshold values and mip opts
if [ "$thr" == "" ];then
  if [ "$sub" == "1" ]; then
    thr=0.001
    uthr=0.1
  elif [ "$sub" == "0" ]; then
    thr=0.3
    uthr=1.0
  fi
fi

if [ "$brain" == "" ];then
  # the FA atlas
  brain=${FSLDIR}/data/standard/FSL_HCP1065_FA_1mm.nii.gz
fi

# Check compulsory arguments
errflag=0
if [ "$dir" == "" ];then
    echo "Must set compulsory argument '-dir'"
    errflag=1
elif [ ! -d $dir ];then
    echo "Xtract folder $dir not found"
    errflag=1
fi
if [ "$str" == "" ];then
  echo "Must set compulsory argument '-str'"
  errflag=1
elif [ ! -f $str ];then
  echo "Structure file $str not found"
  errflag=1
fi
if [ "$uthr" == "" ];then
  echo "You have set a lower threshold but not an upper threshold. Must set '-thr' using <number> <number>"
  errflag=1
fi
if [ ! -f $brain ];then
  echo "Brain overlay file $brain not found"
  errflag=1
fi
if [ "$errflag" -eq 1 ];then
  echo ""
  echo "Exit without doing anything.."
  exit 1
fi

# build struct array - removing any empty/comment lines
# and check for and remove any file extentions
strlist=()
while read structstring; do
  struct=`echo $structstring | awk '{print $1}'`
  # skip empty lines and lines that start with '#'
  if [ "${struct:0:1}" == "#" ];then
    foo=0 #do nothing
  elif [ ! "$struct" == "" ];then
    ss=`echo $struct | sed s/.nii.gz//`
    strlist+=("$ss")
  fi
done < $str


# start the fsleyes command with basic options
cmd="${FSLDIR}/bin/fsleyes $brain -dr 0 `fslstats $brain -r | awk '{print $2}'`"
opts="-dr $thr $uthr"
# Using mip?
if [ "$mip" == "1" ];then
  opts="$opts --overlayType mip --interpolation spline --window 5.0"
fi

# subject or atlas data structure?
if [ "$sub" == "1" ];then
  preT="/tracts"
  postT="/densityNorm.nii.gz"
else
  preT=""
  postT=".nii.gz"
fi

# Now loop and check for left/right tracts to colour the same
# checks for _l and matches any _r
# if no _?, then just move on to next line
i=0
for tract in "${strlist[@]}"
do
  if [ $i -gt $((cL - 1)) ]; then i=0; fi # control colourmap loop
  # check tract exists
  if [ ! -f "${dir}/${preT}/${tract}${postT}" ]; then
    echo "Couldn't find ${tract} image."
    echo "Moving on to the next structure."
    echo "Check ${dir}/${preT}/${tract}${postT} and try again"
  else
    # if you find a left tract, then find the corresponding right tract
    # and colour in the same way
    if [[ $tract == *"_l"* ]];then
      tt=`echo ${tract} | sed s/_l/_r/`
      if [ "`grep ${tt} $str`" != "" ];then
        # append _l and _r to fsleyes command with viewing options
        cmd="$cmd ${dir}/${preT}/${tract}${postT} $opts -cm ${cmaps[i]} -n ${tract} ${dir}/${preT}/${tt}${postT} $opts -cm ${cmaps[i]} -n ${tt}"
      else
        # else, just add the current tract
        cmd="$cmd ${dir}/${preT}/${tract}${postT} $opts -cm ${cmaps[i]} -n ${tract}"
      fi
    elif [[ $tract == *"_r"* ]]  && [[ ! $cmd == *"$tract"* ]] && [[ ! ${strlist[@]} == *"`echo ${tract} | sed s/_r/_l/`"* ]];then
      # if tract name has _r and hasn't been found already
      cmd="$cmd ${dir}/${preT}/${tract}${postT} $opts -cm ${cmaps[i]} -n ${tract}"
    elif [[ $tract != *"_r"*  && $tract != *"_l"* ]];then
      # if tract name doesn't have _l or _r
      cmd="$cmd ${dir}/${preT}/${tract}${postT} $opts -cm ${cmaps[i]} -n ${tract}"
    fi
    ((i++))
  fi
done

echo $cmd
echo "Launching FSLeyes..."
bash $cmd &
#echo $cmd