Skip to content

Instantly share code, notes, and snippets.

@maxpietsch
Last active December 27, 2022 12:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxpietsch/0085181a263e276139a93c3a0a1a00b5 to your computer and use it in GitHub Desktop.
Save maxpietsch/0085181a263e276139a93c3a0a1a00b5 to your computer and use it in GitHub Desktop.
generate and warp subject streamlines to template space
#/bin/env bash
do_nonlinear_registration=false
tmp_dir=$(mktemp -d -t tcktransform-XXXXXXXXXX)
cd $tmp_dir
# ____ 0. get DWI data and create two ODFs ____
for sub in sub-02 sub-03; do
wget --quiet https://github.com/MRtrix3/script_test_data/raw/master/BIDS/"${sub}"/dwi/"${sub}"_dwi.bval
wget --quiet https://github.com/MRtrix3/script_test_data/raw/master/BIDS/"${sub}"/dwi/"${sub}"_dwi.bvec
wget --quiet https://github.com/MRtrix3/script_test_data/raw/master/BIDS/"${sub}"/dwi/"${sub}"_brainmask.nii.gz
wget --quiet https://github.com/MRtrix3/script_test_data/raw/master/BIDS/"${sub}"/dwi/"${sub}"_dwi.nii.gz
wget --quiet https://github.com/MRtrix3/script_test_data/raw/master/BIDS/"${sub}"/dwi/"${sub}"_tissue-WM_response.txt
done
for sub in sub-02 sub-03; do
tail -1 "${sub}"_tissue-WM_response.txt > rf.tmp
dwi2fod csd "${sub}"_dwi.nii.gz rf.tmp -fslgrad "${sub}"_dwi.bvec "${sub}"_dwi.bval -mask "${sub}"_brainmask.nii.gz "${sub}".mif -lmax 4
done
# ____ 1. create subject and template files ____
# in this example, sub-02.mif transformed by a rigid transformation is used as subject (source) image
# sub-03.mif smoothed is used as template (target) image
echo "0.8 0.1 0 -100
-0.1 1 0 20
0 0 1.2 4" > A.txt
transformcalc A.txt rigid R.txt
mrtransform sub-02.mif -linear R.txt -reorient no subject.mif -force
mrfilter sub-03.mif smooth -std 3 template.mif -force
# _____ 2. create template to subject warp ____
if ${do_nonlinear_registration}; then # _____ nonlinear registration version
# co-register subject and template ____
mrregister subject.mif template.mif -nl_warp_full warp_full.mif
# create template to subject warp ____
warpconvert warp_full.mif warpfull2deformation w_t2s.mif -from 2 -template subject.mif
else # _____ linear registration version
# subject-to-template trafo
mrregister subject.mif template.mif -type rigid -rigid rigid_s2t.txt
# template-to-subject trafo
transformcalc rigid_s2t.txt invert rigid_t2s.txt
# make warp in subject-space
warpinit template.mif w_i.mif
transformcompose w_i.mif rigid_t2s.txt w_t2s.mif -template subject.mif
fi
# ____ 3. create subject tck and warp to template space ____
tckgen subject.mif subject.tck -seed_dynamic subject.mif -select 5k -cutoff 0.4
tcktransform subject.tck w_t2s.mif subject_at_template.tck
# ____ 4. create subject tck in template space ____
mrview template.mif -tractography.load subject_at_template.tck
echo "please delete content of $tmp_dir"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment