【初心者向け】基礎&実践プログラミング

初心者がつまづきやすいところ、最短で実力が身につく方法をお伝えします。

【FSL】MRI画像のレジストレーション(Registration)と関心領域(ROI)解析

f:id:AIProgrammer:20200724185625p:plain

目的

  • JHU White-matter labels & tractography atlasでROI解析する方法
  • Desikan Killiany AtlasでROI解析
  • FreeSurferのwm.seg.nii.gzをDWI空間に位置合わせする方法

JHU White-matter labels & tractography atlasでROI解析する方法

標準空間(template (standard) space)にあるJHU-ICBM-labels-1mm.nii.gz、JHU-ICBM-tracts-maxprob-thr25-1mm.nii.gz をDWI空間に位置合わせしてROI解析するスクリプト。

ディレクトリ構造

最低限 FA.nii.gz は用意。

SUBJ1/
 + map/
    + FA.nii.gz
    + MD.nii.gz
    + AD.nii.gz
       :
SUBJ2/
 + map/
    + FA.nii.gz
    + MD.nii.gz
    + AD.nii.gz
       :

実行方法

cd SUBJ1
bash ../reg_FMRIB_to_FA.sh

ソースコード

function reg_FMRIB-Atlas_to_FA () {
    flirt -in map/FA -ref ${FSLDIR}/data/standard/FMRIB58_FA_1mm -omat FA_to_FMRIB.mat
    fnirt --in=map/FA --aff=FA_to_FMRIB.mat --config=FA_2_FMRIB58_1mm --cout=warp_FA_to_FMRIB
    invwarp -w warp_FA_to_FMRIB -o warp_FMRIB_to_FA -r ${FSLDIR}/data/standard/FMRIB58_FA_1mm

    applywarp --in=${FSLDIR}/data/atlases/JHU/JHU-ICBM-labels-1mm --ref=map/FA --warp=warp_FMRIB_to_FA --interp=nn --out=JHU-ICBM-labels_to_FA
    applywarp --in=${FSLDIR}/data/atlases/JHU/JHU-ICBM-tracts-maxprob-thr25-1mm --ref=map/FA --warp=warp_FMRIB_to_FA --interp=nn --out=JHU-ICBM-tracts-maxprob-thr25_to_FA
}

function split_JHU-ICBM-labels () {
    OUT_DIR=mask/JHU-ICBM-labels
    mkdir -p ${OUT_DIR}

    fslmaths JHU-ICBM-labels_to_FA -thr  1 -uthr  1 ${OUT_DIR}/01_Middle-cerebellar-peduncle
    fslmaths JHU-ICBM-labels_to_FA -thr  2 -uthr  2 ${OUT_DIR}/02_Pontine-crossing-tract
    fslmaths JHU-ICBM-labels_to_FA -thr  3 -uthr  3 ${OUT_DIR}/03_Genu-of-corpus-callosum
    fslmaths JHU-ICBM-labels_to_FA -thr  4 -uthr  4 ${OUT_DIR}/04_Body-of-corpus-callosum
    fslmaths JHU-ICBM-labels_to_FA -thr  5 -uthr  5 ${OUT_DIR}/05_Splenium-of-corpus-callosum
    fslmaths JHU-ICBM-labels_to_FA -thr  6 -uthr  6 ${OUT_DIR}/06_Fornix
    fslmaths JHU-ICBM-labels_to_FA -thr  7 -uthr  7 ${OUT_DIR}/07_Corticospinal-tract_R
    fslmaths JHU-ICBM-labels_to_FA -thr  8 -uthr  8 ${OUT_DIR}/08_Corticospinal-tract_L
    fslmaths JHU-ICBM-labels_to_FA -thr  9 -uthr  9 ${OUT_DIR}/09_Medial-lemniscus_R
    fslmaths JHU-ICBM-labels_to_FA -thr 10 -uthr 10 ${OUT_DIR}/10_Medial-lemniscus_L
    fslmaths JHU-ICBM-labels_to_FA -thr 11 -uthr 11 ${OUT_DIR}/11_Inferior-cerebellar-peduncle_R
    fslmaths JHU-ICBM-labels_to_FA -thr 12 -uthr 12 ${OUT_DIR}/12_Inferior-cerebellar-peduncle_L
    fslmaths JHU-ICBM-labels_to_FA -thr 13 -uthr 13 ${OUT_DIR}/13_Superior-cerebellar-peduncle_R
    fslmaths JHU-ICBM-labels_to_FA -thr 14 -uthr 14 ${OUT_DIR}/14_Superior-cerebellar-peduncle_L
    fslmaths JHU-ICBM-labels_to_FA -thr 15 -uthr 15 ${OUT_DIR}/15_Cerebral-peduncle_R
    fslmaths JHU-ICBM-labels_to_FA -thr 16 -uthr 16 ${OUT_DIR}/16_Cerebral-peduncle_L
    fslmaths JHU-ICBM-labels_to_FA -thr 17 -uthr 17 ${OUT_DIR}/17_Anterior-limb-of-internal-capsule_R
    fslmaths JHU-ICBM-labels_to_FA -thr 18 -uthr 18 ${OUT_DIR}/18_Anterior-limb-of-internal-capsule_L
    fslmaths JHU-ICBM-labels_to_FA -thr 19 -uthr 19 ${OUT_DIR}/19_Posterior-limb-of-internal-capsule_R
    fslmaths JHU-ICBM-labels_to_FA -thr 20 -uthr 20 ${OUT_DIR}/20_Posterior-limb-of-internal-capsule_L
    fslmaths JHU-ICBM-labels_to_FA -thr 21 -uthr 21 ${OUT_DIR}/21_Retrolenticular-part-of-internal-capsule_R
    fslmaths JHU-ICBM-labels_to_FA -thr 22 -uthr 22 ${OUT_DIR}/22_Retrolenticular-part-of-internal-capsule_L
    fslmaths JHU-ICBM-labels_to_FA -thr 23 -uthr 23 ${OUT_DIR}/23_Anterior-corona-radiata_R
    fslmaths JHU-ICBM-labels_to_FA -thr 24 -uthr 24 ${OUT_DIR}/24_Anterior-corona-radiata_L
    fslmaths JHU-ICBM-labels_to_FA -thr 25 -uthr 25 ${OUT_DIR}/25_Superior-corona-radiata_R
    fslmaths JHU-ICBM-labels_to_FA -thr 26 -uthr 26 ${OUT_DIR}/26_Superior-corona-radiata_L
    fslmaths JHU-ICBM-labels_to_FA -thr 27 -uthr 27 ${OUT_DIR}/27_Posterior-corona-radiata_R
    fslmaths JHU-ICBM-labels_to_FA -thr 28 -uthr 28 ${OUT_DIR}/28_Posterior-corona-radiata_L
    fslmaths JHU-ICBM-labels_to_FA -thr 29 -uthr 29 ${OUT_DIR}/29_Posterior-thalamic-radiation_R
    fslmaths JHU-ICBM-labels_to_FA -thr 30 -uthr 30 ${OUT_DIR}/30_Posterior-thalamic-radiation_L
    fslmaths JHU-ICBM-labels_to_FA -thr 31 -uthr 31 ${OUT_DIR}/31_Sagittal-stratum_R
    fslmaths JHU-ICBM-labels_to_FA -thr 32 -uthr 32 ${OUT_DIR}/32_Sagittal-stratum_L
    fslmaths JHU-ICBM-labels_to_FA -thr 33 -uthr 33 ${OUT_DIR}/33_External-capsule_R
    fslmaths JHU-ICBM-labels_to_FA -thr 34 -uthr 34 ${OUT_DIR}/34_External-capsule_L
    fslmaths JHU-ICBM-labels_to_FA -thr 35 -uthr 35 ${OUT_DIR}/35_Cingulum-cingulate-gyrus_R
    fslmaths JHU-ICBM-labels_to_FA -thr 36 -uthr 36 ${OUT_DIR}/36_Cingulum-cingulate-gyrus_L
    fslmaths JHU-ICBM-labels_to_FA -thr 37 -uthr 37 ${OUT_DIR}/37_Cingulum-hippocampus_R
    fslmaths JHU-ICBM-labels_to_FA -thr 38 -uthr 38 ${OUT_DIR}/38_Cingulum-hippocampus_L
    fslmaths JHU-ICBM-labels_to_FA -thr 39 -uthr 39 ${OUT_DIR}/39_Fornix-Stria-terminalis_R
    fslmaths JHU-ICBM-labels_to_FA -thr 40 -uthr 40 ${OUT_DIR}/40_Fornix-Stria-terminalis_L
    fslmaths JHU-ICBM-labels_to_FA -thr 41 -uthr 41 ${OUT_DIR}/41_Superior-longitudinal-fasciculus_R
    fslmaths JHU-ICBM-labels_to_FA -thr 42 -uthr 42 ${OUT_DIR}/42_Superior-longitudinal-fasciculus_L
    fslmaths JHU-ICBM-labels_to_FA -thr 43 -uthr 43 ${OUT_DIR}/43_Superior-fronto-occipital-fasciculus_R
    fslmaths JHU-ICBM-labels_to_FA -thr 44 -uthr 44 ${OUT_DIR}/44_Superior-fronto-occipital-fasciculus_L
    fslmaths JHU-ICBM-labels_to_FA -thr 45 -uthr 45 ${OUT_DIR}/45_Uncinate-fasciculus_R
    fslmaths JHU-ICBM-labels_to_FA -thr 46 -uthr 46 ${OUT_DIR}/46_Uncinate-fasciculus_L
    fslmaths JHU-ICBM-labels_to_FA -thr 47 -uthr 47 ${OUT_DIR}/47_Tapetum_R
    fslmaths JHU-ICBM-labels_to_FA -thr 48 -uthr 48 ${OUT_DIR}/48_Tapetum_L

    fsladd ${OUT_DIR}/51_Corticospinal-tract ${OUT_DIR}/07_Corticospinal-tract_R ${OUT_DIR}/08_Corticospinal-tract_L > /dev/null
    fsladd ${OUT_DIR}/52_Medial-lemniscus ${OUT_DIR}/09_Medial-lemniscus_R ${OUT_DIR}/10_Medial-lemniscus_L > /dev/null
    fsladd ${OUT_DIR}/53_Inferior-cerebellar-peduncle ${OUT_DIR}/11_Inferior-cerebellar-peduncle_R ${OUT_DIR}/12_Inferior-cerebellar-peduncle_L > /dev/null
    fsladd ${OUT_DIR}/54_Superior-cerebellar-peduncle ${OUT_DIR}/13_Superior-cerebellar-peduncle_R ${OUT_DIR}/14_Superior-cerebellar-peduncle_L > /dev/null
    fsladd ${OUT_DIR}/55_Cerebral-peduncle ${OUT_DIR}/15_Cerebral-peduncle_R ${OUT_DIR}/16_Cerebral-peduncle_L > /dev/null
    fsladd ${OUT_DIR}/56_Anterior-limb-of-internal-capsule ${OUT_DIR}/17_Anterior-limb-of-internal-capsule_R ${OUT_DIR}/18_Anterior-limb-of-internal-capsule_L > /dev/null
    fsladd ${OUT_DIR}/57_Posterior-limb-of-internal-capsule ${OUT_DIR}/19_Posterior-limb-of-internal-capsule_R ${OUT_DIR}/20_Posterior-limb-of-internal-capsule_L > /dev/null
    fsladd ${OUT_DIR}/58_Retrolenticular-part-of-internal-capsule ${OUT_DIR}/21_Retrolenticular-part-of-internal-capsule_R ${OUT_DIR}/22_Retrolenticular-part-of-internal-capsule_L > /dev/null
    fsladd ${OUT_DIR}/59_Anterior-corona-radiata ${OUT_DIR}/23_Anterior-corona-radiata_R ${OUT_DIR}/24_Anterior-corona-radiata_L > /dev/null
    fsladd ${OUT_DIR}/60_Superior-corona-radiata ${OUT_DIR}/25_Superior-corona-radiata_R ${OUT_DIR}/26_Superior-corona-radiata_L > /dev/null
    fsladd ${OUT_DIR}/61_Posterior-corona-radiata ${OUT_DIR}/27_Posterior-corona-radiata_R ${OUT_DIR}/28_Posterior-corona-radiata_L > /dev/null
    fsladd ${OUT_DIR}/62_Posterior-thalamic-radiation ${OUT_DIR}/29_Posterior-thalamic-radiation_R ${OUT_DIR}/30_Posterior-thalamic-radiation_L > /dev/null
    fsladd ${OUT_DIR}/63_Sagittal-stratum ${OUT_DIR}/31_Sagittal-stratum_R ${OUT_DIR}/32_Sagittal-stratum_L > /dev/null
    fsladd ${OUT_DIR}/64_External-capsule ${OUT_DIR}/33_External-capsule_R ${OUT_DIR}/34_External-capsule_L > /dev/null
    fsladd ${OUT_DIR}/65_Cingulum-cingulate-gyrus ${OUT_DIR}/35_Cingulum-cingulate-gyrus_R ${OUT_DIR}/36_Cingulum-cingulate-gyrus_L > /dev/null
    fsladd ${OUT_DIR}/66_Cingulum-hippocampus ${OUT_DIR}/37_Cingulum-hippocampus_R ${OUT_DIR}/38_Cingulum-hippocampus_L > /dev/null
    fsladd ${OUT_DIR}/67_Fornix-Stria-terminalis ${OUT_DIR}/39_Fornix-Stria-terminalis_R ${OUT_DIR}/40_Fornix-Stria-terminalis_L > /dev/null
    fsladd ${OUT_DIR}/68_Superior-longitudinal-fasciculus ${OUT_DIR}/41_Superior-longitudinal-fasciculus_R ${OUT_DIR}/42_Superior-longitudinal-fasciculus_L > /dev/null
    fsladd ${OUT_DIR}/69_Superior-fronto-occipital-fasciculus ${OUT_DIR}/43_Superior-fronto-occipital-fasciculus_R ${OUT_DIR}/44_Superior-fronto-occipital-fasciculus_L > /dev/null
    fsladd ${OUT_DIR}/70_Uncinate-fasciculus ${OUT_DIR}/45_Uncinate-fasciculus_R ${OUT_DIR}/46_Uncinate-fasciculus_L > /dev/null
    fsladd ${OUT_DIR}/71_Tapetum ${OUT_DIR}/47_Tapetum_R ${OUT_DIR}/48_Tapetum_L > /dev/null

    fsladd ${OUT_DIR}/81_Corpus-callosum ${OUT_DIR}/03_Genu-of-corpus-callosum ${OUT_DIR}/04_Body-of-corpus-callosum ${OUT_DIR}/05_Splenium-of-corpus-callosum > /dev/null
    fsladd ${OUT_DIR}/82_Internal-capsule ${OUT_DIR}/56_Anterior-limb-of-internal-capsule ${OUT_DIR}/57_Posterior-limb-of-internal-capsule ${OUT_DIR}/58_Retrolenticular-part-of-internal-capsule > /dev/null
    fsladd ${OUT_DIR}/83_Corona-radiata ${OUT_DIR}/59_Anterior-corona-radiata ${OUT_DIR}/60_Superior-corona-radiata ${OUT_DIR}/61_Posterior-corona-radiata > /dev/null
}

function split_JHU-ICBM-tracts-maxprob-thr25 () {
    OUT_DIR=mask/JHU-ICBM-tracts-maxprob-thr25
    mkdir -p ${OUT_DIR}

    fslmaths JHU-ICBM-tracts-maxprob-thr25_to_FA -thr  1 -uthr  1 ${OUT_DIR}/01_Anterior-thalamic_Radiation_L
    fslmaths JHU-ICBM-tracts-maxprob-thr25_to_FA -thr  2 -uthr  2 ${OUT_DIR}/02_Anterior-thalamic_Radiation_R
    fslmaths JHU-ICBM-tracts-maxprob-thr25_to_FA -thr  3 -uthr  3 ${OUT_DIR}/03_Corticospinal-tract_L
    fslmaths JHU-ICBM-tracts-maxprob-thr25_to_FA -thr  4 -uthr  4 ${OUT_DIR}/04_Corticospinal-tract_R
    fslmaths JHU-ICBM-tracts-maxprob-thr25_to_FA -thr  5 -uthr  5 ${OUT_DIR}/05_Cingulum-cingulate-gyrus_L
    fslmaths JHU-ICBM-tracts-maxprob-thr25_to_FA -thr  6 -uthr  6 ${OUT_DIR}/06_Cingulum-cingulate-gyrus_R
    fslmaths JHU-ICBM-tracts-maxprob-thr25_to_FA -thr  7 -uthr  7 ${OUT_DIR}/07_Cingulum-hippocampus_L
    fslmaths JHU-ICBM-tracts-maxprob-thr25_to_FA -thr  8 -uthr  8 ${OUT_DIR}/08_Cingulum-hippocampus_R
    fslmaths JHU-ICBM-tracts-maxprob-thr25_to_FA -thr  9 -uthr  9 ${OUT_DIR}/09_Forceps-major
    fslmaths JHU-ICBM-tracts-maxprob-thr25_to_FA -thr 10 -uthr 10 ${OUT_DIR}/10_Forceps-minor
    fslmaths JHU-ICBM-tracts-maxprob-thr25_to_FA -thr 11 -uthr 11 ${OUT_DIR}/11_Inferior-fronto-occipital-fasciculus_L
    fslmaths JHU-ICBM-tracts-maxprob-thr25_to_FA -thr 12 -uthr 12 ${OUT_DIR}/12_Inferior-fronto-occipital-fasciculus_R
    fslmaths JHU-ICBM-tracts-maxprob-thr25_to_FA -thr 13 -uthr 13 ${OUT_DIR}/13_Inferior_Longitudinal-fasciculus_L
    fslmaths JHU-ICBM-tracts-maxprob-thr25_to_FA -thr 14 -uthr 14 ${OUT_DIR}/14_Inferior_Longitudinal-fasciculus_R
    fslmaths JHU-ICBM-tracts-maxprob-thr25_to_FA -thr 15 -uthr 15 ${OUT_DIR}/15_Superior_Longitudinal-fasciculus_L
    fslmaths JHU-ICBM-tracts-maxprob-thr25_to_FA -thr 16 -uthr 16 ${OUT_DIR}/16_Superior_Longitudinal-fasciculus_R
    fslmaths JHU-ICBM-tracts-maxprob-thr25_to_FA -thr 17 -uthr 17 ${OUT_DIR}/17_Uncinate-fasciculus_L
    fslmaths JHU-ICBM-tracts-maxprob-thr25_to_FA -thr 18 -uthr 18 ${OUT_DIR}/18_Uncinate-fasciculus_R
    fslmaths JHU-ICBM-tracts-maxprob-thr25_to_FA -thr 19 -uthr 19 ${OUT_DIR}/19_Superior_Longitudinal-fasciculus-temporal-part_L
    fslmaths JHU-ICBM-tracts-maxprob-thr25_to_FA -thr 20 -uthr 20 ${OUT_DIR}/20_Superior_Longitudinal-fasciculus-temporal-part_R

    fsladd ${OUT_DIR}/21_Anterior-thalamic_Radiation ${OUT_DIR}/01_Anterior-thalamic_Radiation_L ${OUT_DIR}/02_Anterior-thalamic_Radiation_R > /dev/null
    fsladd ${OUT_DIR}/22_Corticospinal-tract ${OUT_DIR}/03_Corticospinal-tract_L ${OUT_DIR}/04_Corticospinal-tract_R > /dev/null
    fsladd ${OUT_DIR}/23_Cingulum-cingulate-gyrus ${OUT_DIR}/05_Cingulum-cingulate-gyrus_L ${OUT_DIR}/06_Cingulum-cingulate-gyrus_R > /dev/null
    fsladd ${OUT_DIR}/24_Cingulum-hippocampus ${OUT_DIR}/07_Cingulum-hippocampus_L ${OUT_DIR}/08_Cingulum-hippocampus_R > /dev/null
    fsladd ${OUT_DIR}/25_Forceps ${OUT_DIR}/09_Forceps-major ${OUT_DIR}/10_Forceps-minor > /dev/null
    fsladd ${OUT_DIR}/26_Inferior-fronto-occipital-fasciculus ${OUT_DIR}/11_Inferior-fronto-occipital-fasciculus_L ${OUT_DIR}/12_Inferior-fronto-occipital-fasciculus_R > /dev/null
    fsladd ${OUT_DIR}/27_Inferior_Longitudinal-fasciculus ${OUT_DIR}/13_Inferior_Longitudinal-fasciculus_L ${OUT_DIR}/14_Inferior_Longitudinal-fasciculus_R > /dev/null
    fsladd ${OUT_DIR}/28_Superior_Longitudinal-fasciculus ${OUT_DIR}/15_Superior_Longitudinal-fasciculus_L ${OUT_DIR}/16_Superior_Longitudinal-fasciculus_R > /dev/null
    fsladd ${OUT_DIR}/29_Uncinate-fasciculus ${OUT_DIR}/17_Uncinate-fasciculus_L ${OUT_DIR}/18_Uncinate-fasciculus_R > /dev/null
    fsladd ${OUT_DIR}/30_Superior_Longitudinal-fasciculus-temporal-part ${OUT_DIR}/19_Superior_Longitudinal-fasciculus-temporal-part_L ${OUT_DIR}/20_Superior_Longitudinal-fasciculus-temporal-part_R > /dev/null
}

function output_results () {
    [[ -d results ]] && \rm -rf results
    mkdir -p results

    for MAP in $(\ls -1 map/*.nii.gz | xargs -i basename -s ".nii.gz" {}); do
        for MASK in $(\ls -1 mask/JHU-ICBM-labels/*.nii.gz); do
            fslstats map/${MAP} -k ${MASK} -M >> results/JHU-ICBM-labels_${MAP}_mean.txt &
            fslstats map/${MAP} -k ${MASK} -S >> results/JHU-ICBM-labels_${MAP}_sd.txt &
            wait
        done
    done

    for MAP in $(\ls -1 map/*.nii.gz | xargs -i basename -s ".nii.gz" {}); do
        for MASK in $(\ls -1 mask/JHU-ICBM-tracts-maxprob-thr25/*.nii.gz); do
            fslstats map/${MAP} -k ${MASK} -M >> results/JHU-ICBM-tracts-maxprob-thr25_${MAP}_mean.txt &
            fslstats map/${MAP} -k ${MASK} -S >> results/JHU-ICBM-tracts-maxprob-thr25_${MAP}_sd.txt &
            wait
        done
    done
}

reg_FMRIB-Atlas_to_FA
split_JHU-ICBM-labels
split_JHU-ICBM-tracts-maxprob-thr25
output_results

Desikan Killiany AtlasでROI解析する方法

T1WI空間にあるDesikan Killiany AtlasをDWI空間に位置合わせをしてROI解析するスクリプト。

前提条件

「/opt/mrtrix3/share/mrtrix3/labelconvert/fs_default.txt」がある前提で走ります。

ディレクトリ構造

ディレクトリ構造は次の通り。

SUBJ_N
├── 3DT1.nii.gz
├── aparc+aseg.mgz ... FreeSurfer output
├── b0.nii.gz      ... b0 (ex: `fslroi eddy_result b0 0 1`)
├── wm.seg.mgz     ... FreeSurfer output
└── map
   ├── FA.nii.gz
   ├── MD.nii.gz
   └── uFA.nii.gz

$ ls -1
this_script.sh
SUBJ_1
SUBJ_2
SUBJ_3

実行方法

$ cd SUBJ_1
$ bash ../this_script.sh

ソースコード

#!/bin/bash

# SUBJ_N
# ├── 3DT1.nii.gz
# ├── aparc+aseg.mgz ... FreeSurfer output
# ├── b0.nii.gz      ... b0 (ex: `fslroi eddy_result b0 0 1`)
# ├── wm.seg.mgz     ... FreeSurfer output
# └── map
#     ├── FA.nii.gz
#     ├── MD.nii.gz
#     └── uFA.nii.gz
#
# $ ls -1
# this_script.sh
# SUBJ_1
# SUBJ_2
# SUBJ_3
#  :
# $ cd SUBJ_1
# $ bash ../this_script.sh

function create_nodes () {
    mkdir -p mask/DK

    fslreorient2std 3DT1 T1
    bet T1 T1_brain -B -f 0.1
    mri_label2vol --temp T1.nii.gz --seg aparc+aseg.mgz --regheader aparc+aseg.mgz --o aparc+aseg.nii.gz
    labelconvert aparc+aseg.nii.gz ${FREESURFER_HOME}/FreeSurferColorLUT.txt /opt/mrtrix3/share/mrtrix3/labelconvert/fs_default.txt nodes.nii.gz
    labelsgmfix nodes.nii.gz T1_brain.nii.gz /opt/mrtrix3/share/mrtrix3/labelconvert/fs_default.txt mask/nodes_sgmfixed.nii.gz -premasked
}

function split_nodes () {
    fslmaths mask/nodes_sgmfixed -thr  1 -uthr  1 mask/DK/01-lh-bankssts
    fslmaths mask/nodes_sgmfixed -thr  2 -uthr  2 mask/DK/02-lh-caudalanteriorcingulate
    fslmaths mask/nodes_sgmfixed -thr  3 -uthr  3 mask/DK/03-lh-caudalmiddlefrontal
    fslmaths mask/nodes_sgmfixed -thr  4 -uthr  4 mask/DK/04-lh-cuneus
    fslmaths mask/nodes_sgmfixed -thr  5 -uthr  5 mask/DK/05-lh-entorhinal
    fslmaths mask/nodes_sgmfixed -thr  6 -uthr  6 mask/DK/06-lh-fusiform
    fslmaths mask/nodes_sgmfixed -thr  7 -uthr  7 mask/DK/07-lh-inferiorparietal
    fslmaths mask/nodes_sgmfixed -thr  8 -uthr  8 mask/DK/08-lh-inferiortemporal
    fslmaths mask/nodes_sgmfixed -thr  9 -uthr  9 mask/DK/09-lh-isthmuscingulate
    fslmaths mask/nodes_sgmfixed -thr 10 -uthr 10 mask/DK/10-lh-lateraloccipital
    fslmaths mask/nodes_sgmfixed -thr 11 -uthr 11 mask/DK/11-lh-lateralorbitofrontal
    fslmaths mask/nodes_sgmfixed -thr 12 -uthr 12 mask/DK/12-lh-lingual
    fslmaths mask/nodes_sgmfixed -thr 13 -uthr 13 mask/DK/13-lh-medialorbitofrontal
    fslmaths mask/nodes_sgmfixed -thr 14 -uthr 14 mask/DK/14-lh-middletemporal
    fslmaths mask/nodes_sgmfixed -thr 15 -uthr 15 mask/DK/15-lh-parahippocampal
    fslmaths mask/nodes_sgmfixed -thr 16 -uthr 16 mask/DK/16-lh-paracentral
    fslmaths mask/nodes_sgmfixed -thr 17 -uthr 17 mask/DK/17-lh-parsopercularis
    fslmaths mask/nodes_sgmfixed -thr 18 -uthr 18 mask/DK/18-lh-parsorbitalis
    fslmaths mask/nodes_sgmfixed -thr 19 -uthr 19 mask/DK/19-lh-parstriangularis
    fslmaths mask/nodes_sgmfixed -thr 20 -uthr 20 mask/DK/20-lh-pericalcarine
    fslmaths mask/nodes_sgmfixed -thr 21 -uthr 21 mask/DK/21-lh-postcentral
    fslmaths mask/nodes_sgmfixed -thr 22 -uthr 22 mask/DK/22-lh-posteriorcingulate
    fslmaths mask/nodes_sgmfixed -thr 23 -uthr 23 mask/DK/23-lh-precentral
    fslmaths mask/nodes_sgmfixed -thr 24 -uthr 24 mask/DK/24-lh-precuneus
    fslmaths mask/nodes_sgmfixed -thr 25 -uthr 25 mask/DK/25-lh-rostralanteriorcingulate
    fslmaths mask/nodes_sgmfixed -thr 26 -uthr 26 mask/DK/26-lh-rostralmiddlefrontal
    fslmaths mask/nodes_sgmfixed -thr 27 -uthr 27 mask/DK/27-lh-superiorfrontal
    fslmaths mask/nodes_sgmfixed -thr 28 -uthr 28 mask/DK/28-lh-superiorparietal
    fslmaths mask/nodes_sgmfixed -thr 29 -uthr 29 mask/DK/29-lh-superiortemporal
    fslmaths mask/nodes_sgmfixed -thr 30 -uthr 30 mask/DK/30-lh-supramarginal
    fslmaths mask/nodes_sgmfixed -thr 31 -uthr 31 mask/DK/31-lh-frontalpole
    fslmaths mask/nodes_sgmfixed -thr 32 -uthr 32 mask/DK/32-lh-temporalpole
    fslmaths mask/nodes_sgmfixed -thr 33 -uthr 33 mask/DK/33-lh-transversetemporal
    fslmaths mask/nodes_sgmfixed -thr 34 -uthr 34 mask/DK/34-lh-insula
    fslmaths mask/nodes_sgmfixed -thr 35 -uthr 35 mask/DK/35-Left-Cerebellum-Cortex
    fslmaths mask/nodes_sgmfixed -thr 36 -uthr 36 mask/DK/36-Left-Thalamus-Proper
    fslmaths mask/nodes_sgmfixed -thr 37 -uthr 37 mask/DK/37-Left-Caudate
    fslmaths mask/nodes_sgmfixed -thr 38 -uthr 38 mask/DK/38-Left-Putamen
    fslmaths mask/nodes_sgmfixed -thr 39 -uthr 39 mask/DK/39-Left-Pallidum
    fslmaths mask/nodes_sgmfixed -thr 40 -uthr 40 mask/DK/40-Left-Hippocampus
    fslmaths mask/nodes_sgmfixed -thr 41 -uthr 41 mask/DK/41-Left-Amygdala
    fslmaths mask/nodes_sgmfixed -thr 42 -uthr 42 mask/DK/42-Left-Accumbens-area
    fslmaths mask/nodes_sgmfixed -thr 43 -uthr 43 mask/DK/43-Right-Thalamus-Proper
    fslmaths mask/nodes_sgmfixed -thr 44 -uthr 44 mask/DK/44-Right-Caudate
    fslmaths mask/nodes_sgmfixed -thr 45 -uthr 45 mask/DK/45-Right-Putamen
    fslmaths mask/nodes_sgmfixed -thr 46 -uthr 46 mask/DK/46-Right-Pallidum
    fslmaths mask/nodes_sgmfixed -thr 47 -uthr 47 mask/DK/47-Right-Hippocampus
    fslmaths mask/nodes_sgmfixed -thr 48 -uthr 48 mask/DK/48-Right-Amygdala
    fslmaths mask/nodes_sgmfixed -thr 49 -uthr 49 mask/DK/49-Right-Accumbens-area
    fslmaths mask/nodes_sgmfixed -thr 50 -uthr 50 mask/DK/50-rh-bankssts
    fslmaths mask/nodes_sgmfixed -thr 51 -uthr 51 mask/DK/51-rh-caudalanteriorcingulate
    fslmaths mask/nodes_sgmfixed -thr 52 -uthr 52 mask/DK/52-rh-caudalmiddlefrontal
    fslmaths mask/nodes_sgmfixed -thr 53 -uthr 53 mask/DK/53-rh-cuneus
    fslmaths mask/nodes_sgmfixed -thr 54 -uthr 54 mask/DK/54-rh-entorhinal
    fslmaths mask/nodes_sgmfixed -thr 55 -uthr 55 mask/DK/55-rh-fusiform
    fslmaths mask/nodes_sgmfixed -thr 56 -uthr 56 mask/DK/56-rh-inferiorparietal
    fslmaths mask/nodes_sgmfixed -thr 57 -uthr 57 mask/DK/57-rh-inferiortemporal
    fslmaths mask/nodes_sgmfixed -thr 58 -uthr 58 mask/DK/58-rh-isthmuscingulate
    fslmaths mask/nodes_sgmfixed -thr 59 -uthr 59 mask/DK/59-rh-lateraloccipital
    fslmaths mask/nodes_sgmfixed -thr 60 -uthr 60 mask/DK/60-rh-lateralorbitofrontal
    fslmaths mask/nodes_sgmfixed -thr 61 -uthr 61 mask/DK/61-rh-lingual
    fslmaths mask/nodes_sgmfixed -thr 62 -uthr 62 mask/DK/62-rh-medialorbitofrontal
    fslmaths mask/nodes_sgmfixed -thr 63 -uthr 63 mask/DK/63-rh-middletemporal
    fslmaths mask/nodes_sgmfixed -thr 64 -uthr 64 mask/DK/64-rh-parahippocampal
    fslmaths mask/nodes_sgmfixed -thr 65 -uthr 65 mask/DK/65-rh-paracentral
    fslmaths mask/nodes_sgmfixed -thr 66 -uthr 66 mask/DK/66-rh-parsopercularis
    fslmaths mask/nodes_sgmfixed -thr 67 -uthr 67 mask/DK/67-rh-parsorbitalis
    fslmaths mask/nodes_sgmfixed -thr 68 -uthr 68 mask/DK/68-rh-parstriangularis
    fslmaths mask/nodes_sgmfixed -thr 69 -uthr 69 mask/DK/69-rh-pericalcarine
    fslmaths mask/nodes_sgmfixed -thr 70 -uthr 70 mask/DK/70-rh-postcentral
    fslmaths mask/nodes_sgmfixed -thr 71 -uthr 71 mask/DK/71-rh-posteriorcingulate
    fslmaths mask/nodes_sgmfixed -thr 72 -uthr 72 mask/DK/72-rh-precentral
    fslmaths mask/nodes_sgmfixed -thr 73 -uthr 73 mask/DK/73-rh-precuneus
    fslmaths mask/nodes_sgmfixed -thr 74 -uthr 74 mask/DK/74-rh-rostralanteriorcingulate
    fslmaths mask/nodes_sgmfixed -thr 75 -uthr 75 mask/DK/75-rh-rostralmiddlefrontal
    fslmaths mask/nodes_sgmfixed -thr 76 -uthr 76 mask/DK/76-rh-superiorfrontal
    fslmaths mask/nodes_sgmfixed -thr 77 -uthr 77 mask/DK/77-rh-superiorparietal
    fslmaths mask/nodes_sgmfixed -thr 78 -uthr 78 mask/DK/78-rh-superiortemporal
    fslmaths mask/nodes_sgmfixed -thr 79 -uthr 79 mask/DK/79-rh-supramarginal
    fslmaths mask/nodes_sgmfixed -thr 80 -uthr 80 mask/DK/80-rh-frontalpole
    fslmaths mask/nodes_sgmfixed -thr 81 -uthr 81 mask/DK/81-rh-temporalpole
    fslmaths mask/nodes_sgmfixed -thr 82 -uthr 82 mask/DK/82-rh-transversetemporal
    fslmaths mask/nodes_sgmfixed -thr 83 -uthr 83 mask/DK/83-rh-insula
    fslmaths mask/nodes_sgmfixed -thr 84 -uthr 84 mask/DK/84-Right-Cerebellum-Cortex
}

function conbine_region () {

    #
    # Whole brain
    #

    fsladd mask/wholebrain $(imglob mask/DK/*)

    #
    # Cortical
    #

    # Frontal
    fsladd mask/cortical-frontal \
        mask/DK/27-lh-superiorfrontal \
        mask/DK/76-rh-superiorfrontal \
        mask/DK/26-lh-rostralmiddlefrontal \
        mask/DK/75-rh-rostralmiddlefrontal \
        mask/DK/03-lh-caudalmiddlefrontal \
        mask/DK/52-rh-caudalmiddlefrontal \
        mask/DK/17-lh-parsopercularis \
        mask/DK/66-rh-parsopercularis \
        mask/DK/18-lh-parsorbitalis \
        mask/DK/67-rh-parsorbitalis \
        mask/DK/19-lh-parstriangularis \
        mask/DK/68-rh-parstriangularis \
        mask/DK/11-lh-lateralorbitofrontal \
        mask/DK/13-lh-medialorbitofrontal \
        mask/DK/60-rh-lateralorbitofrontal \
        mask/DK/62-rh-medialorbitofrontal \
        mask/DK/23-lh-precentral \
        mask/DK/72-rh-precentral \
        mask/DK/16-lh-paracentral \
        mask/DK/65-rh-paracentral \
        mask/DK/31-lh-frontalpole \
        mask/DK/80-rh-frontalpole

    # Parietal
    fsladd mask/cortical-parietal \
        mask/DK/28-lh-superiorparietal \
        mask/DK/77-rh-superiorparietal \
        mask/DK/07-lh-inferiorparietal \
        mask/DK/56-rh-inferiorparietal \
        mask/DK/30-lh-supramarginal \
        mask/DK/79-rh-supramarginal \
        mask/DK/21-lh-postcentral \
        mask/DK/70-rh-postcentral \
        mask/DK/24-lh-precuneus \
        mask/DK/73-rh-precuneus

    # Temporal
    fsladd mask/cortical-temporal \
        mask/DK/29-lh-superiortemporal \
        mask/DK/78-rh-superiortemporal \
        mask/DK/14-lh-middletemporal \
        mask/DK/63-rh-middletemporal \
        mask/DK/08-lh-inferiortemporal \
        mask/DK/57-rh-inferiortemporal \
        mask/DK/01-lh-bankssts \
        mask/DK/50-rh-bankssts \
        mask/DK/06-lh-fusiform \
        mask/DK/55-rh-fusiform \
        mask/DK/33-lh-transversetemporal \
        mask/DK/82-rh-transversetemporal \
        mask/DK/05-lh-entorhinal \
        mask/DK/54-rh-entorhinal \
        mask/DK/32-lh-temporalpole \
        mask/DK/81-rh-temporalpole \
        mask/DK/15-lh-parahippocampal \
        mask/DK/64-rh-parahippocampal

    # Occipital
    fsladd mask/cortical-occipital \
        mask/DK/10-lh-lateraloccipital \
        mask/DK/59-rh-lateraloccipital \
        mask/DK/12-lh-lingual \
        mask/DK/61-rh-lingual \
        mask/DK/04-lh-cuneus \
        mask/DK/53-rh-cuneus \
        mask/DK/20-lh-pericalcarine \
        mask/DK/69-rh-pericalcarine

    # Cingulate
    fsladd mask/cortical-cingulate \
        mask/DK/25-lh-rostralanteriorcingulate \
        mask/DK/74-rh-rostralanteriorcingulate \
        mask/DK/02-lh-caudalanteriorcingulate \
        mask/DK/51-rh-caudalanteriorcingulate \
        mask/DK/22-lh-posteriorcingulate \
        mask/DK/71-rh-posteriorcingulate \
        mask/DK/09-lh-isthmuscingulate \
        mask/DK/58-rh-isthmuscingulate

    # Cortical
    fsladd mask/cortical \
        mask/cortical-frontal \
        mask/cortical-parietal \
        mask/cortical-temporal \
        mask/cortical-occipital \
        mask/cortical-cingulate

    #
    # Subcortical
    #

    fsladd mask/subcortical \
        mask/DK/36-Left-Thalamus-Proper \
        mask/DK/37-Left-Caudate \
        mask/DK/38-Left-Putamen \
        mask/DK/39-Left-Pallidum \
        mask/DK/42-Left-Accumbens-area \
        mask/DK/43-Right-Thalamus-Proper \
        mask/DK/44-Right-Caudate \
        mask/DK/45-Right-Putamen \
        mask/DK/46-Right-Pallidum \
        mask/DK/49-Right-Accumbens-area

    #
    # Gray matter
    #

    fsladd mask/graymatter mask/cortical mask/subcortical

    #
    # White matter
    #

    mri_label2vol --temp T1.nii.gz --seg wm.seg.mgz --regheader wm.seg.mgz --o wm.seg.nii.gz
    fslmaths wm.seg -bin mask/whitematter
}

function reg_maps_to_T1 () {
    bet b0 b0_brain -f 0.1 -m
    epi_reg --epi=b0_brain --t1=T1 --t1brain=T1_brain --out=BBR_DWI-to-T1
    for map in $(imglob map/* | sed "s|map/||g" | tr " " "\n"); do
        flirt -in map/${map} -ref T1 -out ${map}_T1 -init BBR_DWI-to-T1.mat -applyxfm
    done
}

function out_results () {
    [[ -d results ]] && rm -fr results
    mkdir results

    for map in $(imglob map/* | sed "s|map/||g" | tr " " "\n"); do
        for mask in $(imglob mask/DK/*); do
            fslstats ${map}_T1 -k ${mask} -M >> results/DK_${map}_mean.txt &
            fslstats ${map}_T1 -k ${mask} -S >> results/DK_${map}_sd.txt &
            wait
        done

        for mask in wholebrain graymatter whitematter cortical subcortical \
                    cortical-frontal cortical-parietal cortical-temporal cortical-occipital cortical-cingulate; do
            fslstats ${map}_T1 -k mask/${mask} -M >> results/DK_${map}_mean.txt &
            fslstats ${map}_T1 -k mask/${mask} -S >> results/DK_${map}_sd.txt &
            wait
        done
    done

}

create_nodes
split_nodes
conbine_region
reg_maps_to_T1
out_results

FreeSurferのwm.seg.nii.gzをDWI空間に位置合わせする方法

T1WI空間にあるFreeSurferのwm.seg.nii.gzををDWI空間に位置合わせするスクリプト。

必要なファイル

  • dwi.nii.gz (DWI 画像)
  • orig.mgz (T1 画像; T1空間)
  • wm.seg.mgz (White matter ラベル; T1空間)

ソースコード

# mgz を nii.gz に変換
mrconvert orig.mgz orig.nii.gz
mrconvert wm.seg.mgz wm.seg.nii.gz
fslmaths wm.seg.nii.gz -bin wm.seg.nii.gz
# reorient
fslreorient2std dwi.nii.gz dwi.nii.gz
fslreorient2std orig.nii.gz orig.nii.gz
fslreorient2std wm.seg.nii.gz wm.seg.nii.gz
# epi_reg で DWI→T1 の変換行列を計算し、逆行列 (T1→DWI) も計算
bet orig.nii.gz orig_brain.nii.gz -f 0.1 -B -m
fslroi dwi.nii.gz b0.nii.gz 0 1
epi_reg --epi=b0.nii.gz --t1=orig.nii.gz --t1brain=orig_brain.nii.gz --out=DWI_to_T1
convert_xfm -omat T1_to_DWI.mat -inverse DWI_to_T1.mat
# White matter ラベルを DWI 空間に移動
flirt -in wm.seg.nii.gz -ref b0.nii.gz -out wm.nii.gz -init T1_to_DWI.mat -applyxfm -interp nearestneighbour
# 確認
freeview dwi.nii.gz wm.nii.gz:colormap=jet