xtract_viewer 4.62 KB
Newer Older
Saad Jbabdi's avatar
Saad Jbabdi committed
1
2


Saad Jbabdi's avatar
Saad Jbabdi committed
3
4
5
6
7
8
9
10
#!/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
11
# (Slightly modified by Saad Jbabdi 08/2019)
Saad Jbabdi's avatar
Saad Jbabdi committed
12
13
14
15
16

Usage() {
    cat << EOF

Usage:
Saad Jbabdi's avatar
Saad Jbabdi committed
17
    xtract_viewer -dir <xtractDir> [options]
Saad Jbabdi's avatar
Saad Jbabdi committed
18
19
20

    Compulsory arguments:

Saad Jbabdi's avatar
Saad Jbabdi committed
21
       -dir FOLDER                       Path to XTRACT output folder
Saad Jbabdi's avatar
Saad Jbabdi committed
22
23
24

    Optional arguments:

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

Saad Jbabdi's avatar
Saad Jbabdi committed
27
28
       -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
29
30
31
32
33
34
35

       -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
}

Saad Jbabdi's avatar
Saad Jbabdi committed
36
Splash (){
Saad Jbabdi's avatar
Saad Jbabdi committed
37

Saad Jbabdi's avatar
Saad Jbabdi committed
38
cat <<EOF
Saad Jbabdi's avatar
Saad Jbabdi committed
39

Saad Jbabdi's avatar
Saad Jbabdi committed
40
41
42
43
44
45
46
47
48
49
50
51
52
 __  _______ ____      _    ____ _____         _                        
 \ \/ /_   _|  _ \    / \  / ___|_   _| __   _(_) _____      _____ _ __ 
  \  /  | | | |_) |  / _ \| |     | |   \ \ / / |/ _ \ \ /\ / / _ \ '__|
  /  \  | | |  _ <  / ___ \ |___  | |    \ V /| |  __/\ V  V /  __/ |   
 /_/\_\ |_| |_| \_\/_/   \_\____| |_|     \_/ |_|\___| \_/\_/ \___|_|                                                                           
                                                             
EOF

}

Splash

[ "$1" = "" ] && Usage
Saad Jbabdi's avatar
Saad Jbabdi committed
53
54

# the colourmap options
Saad Jbabdi's avatar
Saad Jbabdi committed
55
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
56
57
58
cL="${#cmaps[@]}" # length of array for colourmap loop control

# Parse command-line arguments
Saad Jbabdi's avatar
Saad Jbabdi committed
59
60
61
62
63
# Set defaults
thr=0.001
uthr=0.1
brain=${FSLDIR}/data/standard/FSL_HCP1065_FA_1mm.nii.gz
str=ALL
Saad Jbabdi's avatar
Saad Jbabdi committed
64
65
66
while [ ! -z "$1" ];do
    case "$1" in
	-dir) dir=$2;shift;;
Saad Jbabdi's avatar
Saad Jbabdi committed
67
	-str) IFS=', ' read -r -a str <<< "$2";shift;;
Saad Jbabdi's avatar
Saad Jbabdi committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
	-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
if [ "$uthr" == "" ];then
  echo "You have set a lower threshold but not an upper threshold. Must set '-thr' using <number> <number>"
  errflag=1
fi
Saad Jbabdi's avatar
Saad Jbabdi committed
88
if [ `$FSLDIR/bin/imtest $brain` -eq 0 ];then
Saad Jbabdi's avatar
Saad Jbabdi committed
89
90
91
92
93
94
95
96
97
98
99
  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
Saad Jbabdi's avatar
Saad Jbabdi committed
100
101
102
103
104
105
106
107
108
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
109
110
111
112
113
114


# 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
115
116
117
# Useful bits
preT="tracts"
postT="/densityNorm.nii.gz"
Saad Jbabdi's avatar
Saad Jbabdi committed
118
119
120
121
122

# 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
123
for tract in "${str[@]}";do
Saad Jbabdi's avatar
Saad Jbabdi committed
124
125
126
127
128
129
130
131
132
  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
Saad Jbabdi's avatar
Saad Jbabdi committed
133
134
135
136
137
138
139
140
141
142
      if [[ $tract == *"_l"* ]];then	
	  tt=`echo ${tract} | sed s/_l/_r/`
	  if [[ "${str[@]}" =~ "$tt" ]];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"* ]] && [[ ! ${str[@]} == *"`echo ${tract} | sed s/_r/_l/`"* ]];then
Saad Jbabdi's avatar
Saad Jbabdi committed
143
      # if tract name has _r and hasn't been found already
Saad Jbabdi's avatar
Saad Jbabdi committed
144
145
	  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
146
      # if tract name doesn't have _l or _r
Saad Jbabdi's avatar
Saad Jbabdi committed
147
148
149
	  cmd="$cmd ${dir}/${preT}/${tract}${postT} $opts -cm ${cmaps[i]} -n ${tract}"
      fi
      ((i++))
Saad Jbabdi's avatar
Saad Jbabdi committed
150
151
152
153
154
  fi
done

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