xtract_viewer 5.2 KB
Newer Older
Saad Jbabdi's avatar
Saad Jbabdi committed
1
2
3
4
5
6
7
8
#!/bin/bash

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

# Script to view output of XTRACT in fsleyes
# Written by Shaun Warrington 07/2019
Saad Jbabdi's avatar
Saad Jbabdi committed
9
# (Slightly modified by Saad Jbabdi 08/2019)
Saad Jbabdi's avatar
Saad Jbabdi committed
10
11
12
13
14

Usage() {
    cat << EOF

Usage:
Shaun Warrington's avatar
Shaun Warrington committed
15
16
17
    xtract_viewer -dir <xtractDir> -species HUMAN [options]
    xtract_viewer -dir <xtractDir> -species MACAQUE [options]
    xtract_viewer -dir <xtractDir> -brain <PATH> [options]
Saad Jbabdi's avatar
Saad Jbabdi committed
18
19
20

    Compulsory arguments:

Shaun Warrington's avatar
Shaun Warrington committed
21
22
23
24
25
26
27
28
       -dir <FOLDER>                     Path to XTRACT output folder

       And EITHER:
       -species <SPECIES>                One of HUMAN or MACAQUE

       OR:
       -brain <PATH>                     The brain image to use for the background overlay - must be in the same space as tracts.
                                         Default is the FSL_HCP065_FA map for HUMAN and F99 T1 brain for MACAQUE
Saad Jbabdi's avatar
Saad Jbabdi committed
29
30
31

    Optional arguments:

Saad Jbabdi's avatar
Saad Jbabdi committed
32
       -str STRUCTURE,STRUCTURE,...      Structures (comma separated (default = display all that is found in input folder)
Saad Jbabdi's avatar
Saad Jbabdi committed
33

Saad Jbabdi's avatar
Saad Jbabdi committed
34
35
       -thr NUMBER NUMBER                The lower and upper thresholds applied to the tracts for viewing
                                         Default = 0.001 0.1
Saad Jbabdi's avatar
Saad Jbabdi committed
36
37
38
39
40

EOF
    exit 1
}

Saad Jbabdi's avatar
Saad Jbabdi committed
41
Splash (){
Saad Jbabdi's avatar
Saad Jbabdi committed
42

Saad Jbabdi's avatar
Saad Jbabdi committed
43
cat <<EOF
Saad Jbabdi's avatar
Saad Jbabdi committed
44

Shaun Warrington's avatar
Shaun Warrington committed
45
46
 __  _______ ____      _    ____ _____         _
 \ \/ /_   _|  _ \    / \  / ___|_   _| __   _(_) _____      _____ _ __
Saad Jbabdi's avatar
Saad Jbabdi committed
47
  \  /  | | | |_) |  / _ \| |     | |   \ \ / / |/ _ \ \ /\ / / _ \ '__|
Shaun Warrington's avatar
Shaun Warrington committed
48
49
50
  /  \  | | |  _ <  / ___ \ |___  | |    \ V /| |  __/\ V  V /  __/ |
 /_/\_\ |_| |_| \_\/_/   \_\____| |_|     \_/ |_|\___| \_/\_/ \___|_|

Saad Jbabdi's avatar
Saad Jbabdi committed
51
52
53
54
55
56
57
EOF

}

Splash

[ "$1" = "" ] && Usage
Saad Jbabdi's avatar
Saad Jbabdi committed
58
59

# the colourmap options
Saad Jbabdi's avatar
Saad Jbabdi committed
60
cmaps=(blue red green blue-lightblue pink red-yellow cool yellow copper hot hsv coolwarm spring summer winter Oranges)
Saad Jbabdi's avatar
Saad Jbabdi committed
61
62
63
cL="${#cmaps[@]}" # length of array for colourmap loop control

# Parse command-line arguments
Saad Jbabdi's avatar
Saad Jbabdi committed
64
65
66
67
# Set defaults
thr=0.001
uthr=0.1
str=ALL
Shaun Warrington's avatar
Shaun Warrington committed
68
spec=""
Saad Jbabdi's avatar
Saad Jbabdi committed
69
70
71
while [ ! -z "$1" ];do
    case "$1" in
	-dir) dir=$2;shift;;
Shaun Warrington's avatar
Shaun Warrington committed
72
  -species) spec=$2;shift;;
Saad Jbabdi's avatar
Saad Jbabdi committed
73
	-str) IFS=', ' read -r -a str <<< "$2";shift;;
Saad Jbabdi's avatar
Saad Jbabdi committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
	-thr) thr=$2;uthr=$3;shift;shift;;
	-brain) brain=$2;shift;;
	*) echo "Unknown option '$1'";exit 1;;
    esac
    shift
done

# 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
Shaun Warrington's avatar
Shaun Warrington committed
90
91
92
93
94
95
96
97
98
99
100
101

if [ "$spec" == "" ];then
  if [ "$brain" == "" ];then
    echo "Must set compulsory argument '-brain' OR specify '-species <SPECIES>'"
    errflag=1
  elif [ `$FSLDIR/bin/imtest $brain` -eq 0 ];then
    echo "Brain overlay file $brain not found"
    errflag=1
  fi
elif [ $spec == HUMAN ];then
    brain=${FSLDIR}/data/standard/FSL_HCP1065_FA_1mm.nii.gz
elif [ $spec == MACAQUE ];then
102
    brain=${FSLDIR}/data/xtract_data/standard/F99/mri/struct_brain.nii.gz
Shaun Warrington's avatar
Shaun Warrington committed
103
104
elif [ ! "$spec" == "HUMAN" ] && [ ! "$spec" == "MACAQUE" ];then
  echo "Unrecognised option '$spec'. Must set '-species' using HUMAN or MACAQUE"
Saad Jbabdi's avatar
Saad Jbabdi committed
105
106
  errflag=1
fi
Shaun Warrington's avatar
Shaun Warrington committed
107
108
109

if [ "$uthr" == "" ];then
  echo "You have set a lower threshold but not an upper threshold. Must set '-thr' using <number> <number>"
Saad Jbabdi's avatar
Saad Jbabdi committed
110
111
112
113
114
115
116
117
118
119
  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
Saad Jbabdi's avatar
Saad Jbabdi committed
120
121
122
123
124
125
126
127
128
if [ $str == ALL ];then
    str=(`ls $dir/tracts`)
    #str=`IFS=' ' read -r -a str <<< "$strlist"`
fi
echo Structures to be displayed
for index in "${!str[@]}"
do
    echo "$index ${str[index]}"
done
Saad Jbabdi's avatar
Saad Jbabdi committed
129
130
131
132
133
134


# start the fsleyes command with basic options
cmd="${FSLDIR}/bin/fsleyes $brain -dr 0 `fslstats $brain -r | awk '{print $2}'`"
opts="-dr $thr $uthr"

Saad Jbabdi's avatar
Saad Jbabdi committed
135
136
137
# Useful bits
preT="tracts"
postT="/densityNorm.nii.gz"
Saad Jbabdi's avatar
Saad Jbabdi committed
138
139
140
141
142

# 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
Saad Jbabdi's avatar
Saad Jbabdi committed
143
for tract in "${str[@]}";do
Saad Jbabdi's avatar
Saad Jbabdi committed
144
145
146
147
148
149
150
151
152
  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
Shaun Warrington's avatar
Shaun Warrington committed
153
      if [[ $tract == *"_l"* ]];then
Saad Jbabdi's avatar
Saad Jbabdi committed
154
	  tt=`echo ${tract} | sed s/_l/_r/`
Shaun Warrington's avatar
Shaun Warrington committed
155
	  if [[ "${str[@]}" =~ "$tt" ]];then
Saad Jbabdi's avatar
Saad Jbabdi committed
156
157
158
159
160
161
162
              # 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"* ]] && [[ ! ${str[@]} == *"`echo ${tract} | sed s/_r/_l/`"* ]];then
Saad Jbabdi's avatar
Saad Jbabdi committed
163
      # if tract name has _r and hasn't been found already
Saad Jbabdi's avatar
Saad Jbabdi committed
164
165
	  cmd="$cmd ${dir}/${preT}/${tract}${postT} $opts -cm ${cmaps[i]} -n ${tract}"
      elif [[ $tract != *"_r"*  && $tract != *"_l"* ]];then
Saad Jbabdi's avatar
Saad Jbabdi committed
166
      # if tract name doesn't have _l or _r
Saad Jbabdi's avatar
Saad Jbabdi committed
167
168
169
	  cmd="$cmd ${dir}/${preT}/${tract}${postT} $opts -cm ${cmaps[i]} -n ${tract}"
      fi
      ((i++))
Saad Jbabdi's avatar
Saad Jbabdi committed
170
171
172
173
174
  fi
done

echo $cmd
echo "Launching FSLeyes..."
Saad Jbabdi's avatar
Saad Jbabdi committed
175
eval $cmd &