-
Notifications
You must be signed in to change notification settings - Fork 2
/
4dConcatSubBriks
executable file
·111 lines (93 loc) · 4.07 KB
/
4dConcatSubBriks
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
#!/usr/bin/env bash
set -euo pipefail
trap 'e=$?; [ $e -ne 0 ] && echo "$0 exited in error"' EXIT
env | grep -q ^DRYRUN=. && DRYRUN=echo || DRYRUN=""
env | grep -q ^NOAFNI= || NOAFNI=1
usage(){
cat >&2 <<HEREDOC
USAGE:
$(basename $0) [options] output.nii.gz 'gl*ob' [subbrik]"
EXAMPLE:
$(basename $0) allmprage.nii.gz '1*_2*/struct/*/*_G2_256x240*/mprage_warp_linear.nii.gz'
$(basename $0) -n 2 -p '\\d' all_3rdvol.nii.gz 'file*.nii.gz'
$(basename $0) -p '\\d' all_3rdvol.nii.gz -- file1.nii.gz file2.nii.gz
SYNOPSIS:
wrap around 3dbucket and 3drefit
extract a subbrick from a list of nifti
label with luna ids
Useful for quality checking many structurals, subject masks, or individual contrasts
OPTIONS: (are optional)
-a # show afni afteward
-p '\d{5}_\d{8}' # change id pattern
-h # this help
-n SUBBRICKIDX # use zero-index subbrick instead of first (0)
-o output.nii.gz # can use -o. if not assumed first argument will be output file name
-- file1 file2 ...# instead of using 'gl*b', expliclty list input files after --
ARGUMENTS:
output.nii.gz - final file to create
'gl*ob' - file glob matching all images to concat. MUST BE QUOTED.
NOTES:
* options must come before arguments.
good: 4dConcatSubBriks -n 2 output.nii.gz '/path/to/*nii.gz'
bad: 4dConcatSubBriks output.nii.gz '/path/to/*nii.gz' -n 2
* 'gl*ob' should be quoted. if you want to expliclty enumerate files, use -- file1 file2 ....
* cant mix '--' and '-n'
HEREDOC
exit 1
}
[ $# -lt 2 ] && usage
PATTERN='\d{5}_\d{8}'
FILES=
prefix=
subbrik=0
while [ $# -gt 0 ]; do
case $1 in
-p) PATTERN="$2"; shift 2;;
-a) NOAFNI=""; shift;;
-h) usage;;
-n) subbrik="$2"; shift 2;;
-o) prefix="$2"; shift 2;;
--) shift; FILES=("$@"); set -- ;;
*) break;; # nothing to do. will use stuff later
esac
done
# initially allowed prefix to be first argument (after options)
# continue allowing that if we didn't specify with -o
[ $# -eq 0 ] && [ -z "$prefix" ] && echo "output prefix not specified! For help, see $0 -h" && exit 1
[ -z "$prefix" ] && prefix="$1" && shift
if [ -z "$FILES" ]; then
[ $# -eq 0 ] && echo "no files specified! For help, see $0 -h" && exit 1
glob="$1"; shift
[ $# -gt 0 ] && echo "too many input argumetns ($#)! maybe you need single quotes around the 'gl*ob'?" >&2 && exit 1
! [[ $prefix =~ nii$|nii.gz$|.HEAD$ ]] && echo "prefix '$prefix' must end in .nii, .nii.gz, or .HEAD" && exit 1
[ -z "$(ls $glob)" ] && echo "bad glob provided ('$glob'). afni files? maybe add '.HEAD' to glob?" >&2 && exit 1
ids=($(ls $glob | grep -Po "$PATTERN" || :))
[ -z "$ids" ] && echo "could not find ID using pattern '$PATTERN' in provided glob '$glob'" >&2 && exit 1
FILES=($(ls $glob | sed "s/$/[$subbrik]/"))
else
[ $subbrik -ne 0 ] && echo "ERROR: -n not supported with '--' explicit file list. add it with sed if you must" && exit 1
# check all exist -- might have [] or other afni syntax. use 3dinfo instead of just [ -f ... ]
for f in "${FILES[@]}"; do ! 3dinfo -space $f >/dev/null && echo "ERROR: cannot read $f" && exit 1; done
# extract ids
ids=($(for f in "${FILES[@]}"; do echo $f; done | grep -Po "$PATTERN" || :))
fi
echo "# Input Files: ${FILES[*]}"
[ ${#ids[@]} -ne ${#FILES[@]} ] && echo "ERROR: have ${#ids[@]} ids but ${#FILES[@]} files. Consider modifying pattern '$PATTERN' with -p."
if [ ! -r "$prefix" ]; then
echo "3dbucket: combine"
$DRYRUN 3dbucket -prefix "$prefix" "${FILES[@]}"
echo "3drefit: relabel"
$DRYRUN 3drefit -relabel_all_str "${ids[*]}" "$prefix"
else
echo "rm $prefix # to regenerate; skipping 3dbucket and 3drefit"
fi
[ -n "$DRYRUN" ] && exit 0
[ -n "$NOAFNI" ] && exit 0
afni -com 'OPEN_WINDOW axialgraph' "$prefix" >/dev/null 2>&1
outidx=$(3dROIstats -quiet -mask "3dcalc( -expr step(a) -a $prefix[0] )" $prefix |
Rscript -e 'd<-read.table("stdin");cat(paste(sep=",",collapse=",",which( abs(d$V1) > abs(mean(d$V1))+2*sd(d$V1) )-1))')
if [ -n "$outidx" ]; then
echo "outlier idxes: $outidx"
echo -n "outlier labels: "
3dinfo -label "$prefix[$outidx]"
fi