-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathqsiprep2bl.sh
executable file
·103 lines (85 loc) · 3.94 KB
/
qsiprep2bl.sh
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
#!/bin/bash
#####################################################################################
# reogranize qsiprep outputs for brainlife
#####################################################################################
module load singularity 2> /dev/null
set -x
set -e
# outstem is the top directory that is created by qsiprep to dump all of the
# derivatives
outstem=output
# get basename for output
sub=$(jq -r '._inputs[0].meta.subject' config.json)
space=$(jq -r .output_space config.json)
xflip=$(jq -r .xflip config.json)
ses=$(jq -r '._inputs[] | select(.id == "dwi") | .meta.session' config.json)
ses=($ses)
acq=$(jq -r '._inputs[] | select(.id == "dwi") | .meta.acq' config.json)
acq=($acq)
# organize output
mkdir -p output_anat_preproc
mkdir -p output_dseg
mkdir -p output_brainmask
mkdir -p output_dwi
mkdir -p output_report
# set file paths and stems. outdir is the output dir + qsiprep, and is used to
# grab the html and figures files. outsub is the outdir + the subject stem, and
# is used to grab the anatomy and dwi data. SRCDIR is the outsub + the anat
# directory, and is used specifically to grab the appropriate t1 data. outfile
# is the subject stem, and is used to make identifying the appropriate dwi
# files easier
outdir=$outstem/qsiprep
outsub="$outdir/sub-${sub}"
SRCDIR=$outsub/anat
outfile="sub-${sub}"
# if a session tag exists, append to outsub and outfile
[ "$ses" != "null" ] && outsub=$outsub/ses-${ses[0]}
[ "$ses" != "null" ] && outfile=${outfile}_ses-${ses[0]}
# if an acquisition tag exists, append to outfile
[ "$acq" != "null" ] && outfile=${outfile}_acq-${acq[0]}
# copy the appropriate anatomy data, based on space input
if [ $space == "T1w" ]; then
cp $SRCDIR/sub-${sub}_desc-preproc_T1w.nii.gz output_anat_preproc/t1.nii.gz;
cp $SRCDIR/sub-${sub}_dseg.nii.gz output_dseg/parc.nii.gz;
cp $SRCDIR/sub-${sub}_desc-brain_mask.nii.gz output_brainmask/mask.nii.gz;
elif [ $space == "MNI152NLin2009cAsym" ]; then
cp $SRCDIR/sub-${sub}_space-MNI152NLin2009cAsym_desc-preproc_T1w.nii.gz output_anat_preproc/t1.nii.gz;
cp $SRCDIR/sub-${sub}_space-MNI152NLin2009cAsym_dseg.nii.gz output_dseg/parc.nii.gz;
cp $SRCDIR/sub-${sub}_space-MNI152NLin2009cAsym_desc-brain_mask.nii.gz output_brainmask/mask.nii.gz;
fi
# copy dwi output to bl output dir
cp $outsub/dwi/${outfile}_space-T1w_desc-preproc_dwi.nii.gz output_dwi/dwi.nii.gz
cp $outsub/dwi/${outfile}_space-T1w_desc-preproc_dwi.bvec output_dwi/dwi.bvecs
cp $outsub/dwi/${outfile}_space-T1w_desc-preproc_dwi.bval output_dwi/dwi.bvals
# copy over report html to output dir
for html in $(cd $outstem && find -name "*.html"); do
mkdir -p output_report/$(dirname $html)
cp $outstem/$html output_report/$html
done
for dir in $(cd $outstem && find ./ -name figures); do
mkdir -p output_report/$(dirname $dir)
cp -r $outstem/$dir output_report/$(dirname $dir)
done
# rename the parent directory to confirm to brainlife html output
mv output_report/qsiprep output_report/html
# flip bvecs
if [ $xflip == "true" ]; then
echo "Save output bvecs in MRtrix-style"
#sed -i '$s/}/,\n"bvecs_out":".\/output_dwi\/dwi.bvecs"}/' config.json #input bvecs
#singularity exec -e docker://brainlife/mcr:neurodebian1604-r2017a ./compiled/main
#rm output_dwi/dwi.bvecs #input bvecs
#cp dwi.bvecs output_dwi/dwi.bvecs #flipped bvecs
# simpler version
grad=$outsub/dwi/${outfile}_space-T1w_desc-preproc_dwi.b
time singularity exec -e docker://brainlife/mrtrix3:3.0.3 \
mrconvert output_dwi/dwi.nii.gz -grad $grad output.mif \
-export_grad_fsl output_dwi/dwi.bvecs dwi.bvals -force
rm output.mif
rm dwi.bvals
fi
# copy confounds.tsv file to regressors directory
[ ! -d ./regressors ] && mkdir -p regressors
[ ! -f ./regressors/regressors.tsv ] && cp $outsub/dwi/*_confounds.tsv ./regressors/regressors.tsv
# copy dwiqc.json file to dwiqc directory
[ ! -d ./dwiqc ] && mkdir -p dwiqc
[ ! -f ./dwiqc/dwiqc.json ] && cp $outdir/dwiqc.json ./dwiqc/dwiqc.json