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

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

FreeSurferの結果のまとめ方 ~aparc, aseg, wmparc, brainstem~

f:id:AIProgrammer:20200508150125p:plain

目的

被験者ごとのFreeSurferの結果を、一つのエクセルファイルにまとめる。

フォルダ構造

フォルダ構造は以下の通り。 基本的には、FreeSurfer後のフォルダを一つのフォルダにまとめ、そのフォルダで作業する。 FreeSurferの出力であるstatsフォルダさえあれば動作する。

$ tree

.
├── CC001
│   ├── mri
│   │   └── brainstemSsVolumes.v10.txt
│   └── stats
│   ├── aseg.stats
│   ├── lh.BA.stats
│   ├── lh.BA.thresh.stats
│   ├── lh.aparc.DKTatlas40.stats
│   ├── lh.aparc.a2009s.stats
│   ├── lh.aparc.stats
│   ├── lh.curv.stats
│   ├── lh.entorhinal_exvivo.stats
│   ├── lh.w-g.pct.stats
│   ├── rh.BA.stats
│   ├── rh.BA.thresh.stats
│   ├── rh.aparc.DKTatlas40.stats
│   ├── rh.aparc.a2009s.stats
│   ├── rh.aparc.stats
│   ├── rh.curv.stats
│   ├── rh.entorhinal_exvivo.stats
│   ├── rh.w-g.pct.stats
│   └── wmparc.stats
├── CC002
└── CC003

aparcとasegのまとめ方

aparcとasegの結果をまとめる場合、aparcstats2tableasegstats2tableを実行。

  • --subjects : subject folderを指定
  • --delimiter : 区切り文字を指定
  • --table(file) : 出力ファイル名の指定
  • --meas : 計測対象を指定。皮質厚の場合はthickness (#aparcstats2tableのみのオプション) SUBJECTS_DIRの設定を忘れずに。
export SUBJECTS_DIR=$PWD

# aseg
asegstats2table \
--subjects $(ls -F |grep / |cut -d / -f1) \
--delimiter=comma \
--tablefile aseg.vol.csv 

# aparc
## lh
aparcstats2table --hemi lh \
--subjects $(ls -F |grep / |cut -d / -f1) \
--delimiter=comma \
--meas volume \
--table lh.aparc.volume.csv

## rh
aparcstats2table --hemi rh \
--subjects $(ls -F |grep / |cut -d / -f1) \
--delimiter=comma \
--meas volume \
--table rh.aparc.volume.csv

wmparcのまとめ方

FreeSurferのコマンドでaparcstats2tableやasegstats2tableのようなコマンドが見当たらなかったのでPythonで実装した。

import os
import pandas as pd
import numpy as np

def get_wmname(sub):
    with open(sub + "/stats/wmparc.stats") as f:
        result = [row.strip () for i, row in enumerate(f.readlines()) if i >= 63 ]
    wmname = []
    for row in result:
        tmp_val = []
        for val in row.split(" "):
            if val != "":
                tmp_val.append(val)
        wmname.append(tmp_val[4])
    return wmname

def get_wmvol(sub):
    with open(sub + "/stats/wmparc.stats") as f:
        result = [row.strip () for i, row in enumerate(f.readlines()) if i >= 63 ]
        wmvol = []
    for row in result:
        tmp_val = []
        for val in row.split(" "):
            if val != "":
                tmp_val.append(val)
        wmvol.append(tmp_val[3])
    return wmvol

# list folders
files = os.listdir(".")
folders = [f for f in files if os.path.isdir(f)]
sub_id, all_wmname, all_wmvol = [], [], []

for i, sub in enumerate(folders):
    if i == 1:
        sub_id.append(sub)
        all_wmname = get_wmname(sub)
        all_wmvol.append(get_wmvol(sub))
    else:
        sub_id.append(sub)
        all_wmvol.append(get_wmvol(sub))        

df = pd.DataFrame(all_wmvol, index=sub_id, columns=all_wmname)
df.sort_index().to_csv("wmseg.csv")

Brain Stemの計測

FreeSurferではBrain-Stemの計測もできる。

recon-all -all -s bert -brainstem-structures 

すでに、recon-all -allが終わっている場合。

recon-all -s bert -brainstem-structures 

各被験者のBrain-Stem volume結果をまとめるには次のコマンドを使用。

ただし、FreeSurfer versionによっては「brainstemSsVolumes.v10.txt」のファイル名が異なる。

echo "" >> brainstem_vol.txt
echo Medulla >> brainstem_vol.txt
echo Pons >> brainstem_vol.txt
echo SCP >> brainstem_vol.txt
echo Midbrain >> brainstem_vol.txt
echo Whole_brainstem >> brainstem_vol.txt

for k in $(ls -F |grep / |cut -d / -f1);do
    echo $k > tmp1
    cat $k/mri/brainstemSsVolumes.v10.txt | cut -d " " -f2 >> tmp1
    paste brainstem_vol.txt tmp1 > tmp2
    cat tmp2 > brainstem_vol.txt
done

rm tmp1 tmp2

出力結果

aparc (lhのみ)

lh.aparc.volume lh_bankssts_volume lh_caudalanteriorcingulate_volume lh_caudalmiddlefrontal_volume lh_cuneus_volume lh_entorhinal_volume lh_fusiform_volume lh_inferiorparietal_volume lh_inferiortemporal_volume lh_isthmuscingulate_volume lh_lateraloccipital_volume lh_lateralorbitofrontal_volume lh_lingual_volume lh_medialorbitofrontal_volume lh_middletemporal_volume lh_parahippocampal_volume lh_paracentral_volume lh_parsopercularis_volume lh_parsorbitalis_volume lh_parstriangularis_volume lh_pericalcarine_volume lh_postcentral_volume lh_posteriorcingulate_volume lh_precentral_volume lh_precuneus_volume lh_rostralanteriorcingulate_volume lh_rostralmiddlefrontal_volume lh_superiorfrontal_volume lh_superiorparietal_volume lh_superiortemporal_volume lh_supramarginal_volume lh_frontalpole_volume lh_temporalpole_volume lh_transversetemporal_volume lh_insula_volume
CC001 2318 1802 5756 2654 1476 7944 10552 10073 2404 9799 7142 5479 4294 8916 2237 3267 4286 2156 3420 2435 9525 2876 13489 9070 2719 13135 17687 12328 10130 8677 654 2568 1135 5921
CC002 2228 1464 5130 2359 1252 10454 9426 8519 2760 9473 6898 5715 4917 9127 1704 3423 5596 1777 3196 2458 7681 2890 11703 8323 2306 11127 18289 11653 10041 9281 505 2253 1103 5970
CC003 1949 1178 4583 1948 1589 9473 10751 10139 2439 7841 6288 4407 4322 7663 2314 2872 3641 1573 2725 1331 8929 2521 11441 8670 1969 10178 18529 11355 9141 7869 567 2451 886 5892

aseg

Measure:volume Left-Lateral-Ventricle Left-Inf-Lat-Vent Left-Cerebellum-White-Matter Left-Cerebellum-Cortex Left-Thalamus-Proper Left-Caudate Left-Putamen Left-Pallidum 3rd-Ventricle 4th-Ventricle Brain-Stem Left-Hippocampus Left-Amygdala CSF Left-Accumbens-area Left-VentralDC Left-vessel Left-choroid-plexus Right-Lateral-Ventricle Right-Inf-Lat-Vent Right-Cerebellum-White-Matter Right-Cerebellum-Cortex Right-Thalamus-Proper Right-Caudate Right-Putamen Right-Pallidum Right-Hippocampus Right-Amygdala Right-Accumbens-area Right-VentralDC Right-vessel Right-choroid-plexus 5th-Ventricle WM-hypointensities Left-WM-hypointensities Right-WM-hypointensities non-WM-hypointensities Left-non-WM-hypointensities Right-non-WM-hypointensities Optic-Chiasm CC_Posterior CC_Mid_Posterior CC_Central CC_Mid_Anterior CC_Anterior BrainSegVol BrainSegVolNotVent BrainSegVolNotVentSurf lhCortexVol rhCortexVol CortexVol lhCorticalWhiteMatterVol rhCorticalWhiteMatterVol CorticalWhiteMatterVol SubCortGrayVol TotalGrayVol SupraTentorialVol SupraTentorialVolNotVent SupraTentorialVolNotVentVox MaskVol BrainSegVol-to-eTIV MaskVol-to-eTIV lhSurfaceHoles rhSurfaceHoles SurfaceHoles EstimatedTotalIntraCranialVol
CC001 37058.4 1011.5 16164.4 38981.6 6843 3338.3 5360.1 1110.2 1872.6 1800.4 16125.3 3742.2 1604.7 1896.4 411.5 3217.3 123.7 1545.9 32916.5 1167.8 14133.9 38528.6 5500 3579.4 4640.5 1533 3777.3 1791 485.9 3377.6 78.8 2250.4 3.2 3994.9 0 0 45.7 0 0 317.3 1088.5 292.8 333.8 358.1 959.4 1083033 1001404 1000251.316 207264.634 202815.0315 410079.6655 214103.7471 218492.9033 432596.6504 51317 537801.6655 971737.3159 895513.3159 894975 1491464 0.708591 0.975813 12 14 26 1528431.47
CC002 13223.5 788.7 16425.8 40452 9575.9 2850.7 3605.3 720.9 1301.8 2185.2 21441.9 3400.9 1760.7 1733.1 437.9 3835.2 87.5 1677.5 12254.4 380.7 16553.9 43973.6 7719.5 2902.6 3093.5 1090.6 4185.4 1880.1 562.7 3705.1 27.9 1973.1 15.5 2032.4 0 0 28.2 0 0 172.1 1042.6 366.9 416.7 422.4 853.2 1051867 1016551 1016029.561 198464.6049 201233.972 399698.5769 221079.9779 228023.0058 449102.9836 52151 534712.5769 932305.5605 902100.5605 901346 1484953 0.696805 0.983701 35 16 51 1509557.937
CC003 18422.9 873.6 14908.2 41111.2 8277 2982.7 4649.6 1417.1 2492.8 1497.9 17573.2 3362.6 1574.2 1342.6 345.8 3907.2 92.4 1713.7 19819.9 939.3 15407.7 41400.7 6487.8 2988.3 4805.9 1635.3 3503.2 1521.3 436.6 3935.3 53.8 2268.7 0 12013.7 0 0 371.5 0 0 212 775.2 337.8 370.3 388.7 641.1 983765 934645 933889.1318 187178.4982 186464.2799 373642.7781 197476.6 199021.7536 396498.3537 52891 507882.7781 869181.1318 825200.1318 823469 1339837 0.778802 1.060688 38 36 74 1263176.796

wmparc

Measure:volume wm-lh-bankssts wm-lh-caudalanteriorcingulate wm-lh-caudalmiddlefrontal wm-lh-cuneus wm-lh-entorhinal wm-lh-fusiform wm-lh-inferiorparietal wm-lh-inferiortemporal wm-lh-isthmuscingulate wm-lh-lateraloccipital wm-lh-lateralorbitofrontal wm-lh-lingual wm-lh-medialorbitofrontal wm-lh-middletemporal wm-lh-parahippocampal wm-lh-paracentral wm-lh-parsopercularis wm-lh-parsorbitalis wm-lh-parstriangularis wm-lh-pericalcarine wm-lh-postcentral wm-lh-posteriorcingulate wm-lh-precentral wm-lh-precuneus wm-lh-rostralanteriorcingulate wm-lh-rostralmiddlefrontal wm-lh-superiorfrontal wm-lh-superiorparietal wm-lh-superiortemporal wm-lh-supramarginal wm-lh-frontalpole wm-lh-temporalpole wm-lh-transversetemporal wm-lh-insula wm-rh-bankssts wm-rh-caudalanteriorcingulate wm-rh-caudalmiddlefrontal wm-rh-cuneus wm-rh-entorhinal wm-rh-fusiform wm-rh-inferiorparietal wm-rh-inferiortemporal wm-rh-isthmuscingulate wm-rh-lateraloccipital wm-rh-lateralorbitofrontal wm-rh-lingual wm-rh-medialorbitofrontal wm-rh-middletemporal wm-rh-parahippocampal wm-rh-paracentral wm-rh-parsopercularis wm-rh-parsorbitalis wm-rh-parstriangularis wm-rh-pericalcarine wm-rh-postcentral wm-rh-posteriorcingulate wm-rh-precentral wm-rh-precuneus wm-rh-rostralanteriorcingulate wm-rh-rostralmiddlefrontal wm-rh-superiorfrontal wm-rh-superiorparietal wm-rh-superiortemporal wm-rh-supramarginal wm-rh-frontalpole wm-rh-temporalpole wm-rh-transversetemporal wm-rh-insula Left-UnsegmentedWhiteMatter Right-UnsegmentedWhiteMatter
CC001 2925 2762.7 6601.8 2336.3 579.4 5657.4 9285.9 5771.2 4040 8779.4 6602.2 4990.2 3065.7 5261.5 1520.6 3335.3 3277.8 883.4 3049.5 3329.7 7734.1 4228.5 13701.9 9239.6 2662.9 11357.6 15431.4 12371.3 6273.3 7390.8 141.7 576.6 1105.2 8191.2 2799 2680.2 5440.3 2439.1 430.4 5110.1 11592.2 4709.9 4187.3 8267.3 6254.9 6909.4 3311.9 5331.2 1478.3 4632.1 3602.6 1045.2 2980.8 3908.2 6865.2 4190.5 11798.2 10210.4 2057 12338.4 15838.8 11781.2 6749.2 8375.8 250.8 651.9 646.7 8237.9 29358.3 30753.8
CC002 2550.3 2551 5794.4 2271.9 592 7762.7 9147.5 5679.2 4133.5 9755.3 6559.1 6138.7 3698.5 4925.1 1382.7 3790 4914.5 742.4 3597.5 3723.8 6478.3 4766.7 12476.3 8710.1 2560.6 11304.2 16989.6 12974.7 8548.1 8630.8 152.6 731.3 958.4 7857.9 2243 3502.7 5766 1996.7 562.1 7317.3 11037.8 5585.4 3902.7 9115.5 6192.2 6739.3 3663.1 5487 1436.4 5018.9 3032.7 1181.1 3937.5 4195.1 7653 5025.2 12929.3 8695.7 2353.7 11923.5 15849.5 13578.8 8221.4 8953.5 267.8 644 563.4 9315.8 25862.5 28235.8
CC003 2791.5 3067.6 5542.2 2001.4 705.3 6605 9246 5889 3455.8 6859.9 6289.1 4614.7 3036.7 5115.9 1588.5 4051.4 2720.2 738.3 2281.9 2393.7 7252.4 3741.7 12757 8773 2983.4 9870.5 15184.3 10613.1 6126.2 7300.9 177.1 824.3 796.2 9027.7 2502.4 2630.2 4438.6 2399.9 640.1 6858 10302 5133.4 2860.7 8340.2 5191.2 4649.2 2949.5 5792.5 1542.6 3950.9 3000.1 962.8 3059.2 2391.3 7117.3 3555.7 13722.1 9428.6 1871.3 10707.4 15004.4 10056.3 5532 8236 236.4 566.4 518.8 8760.9 22167.4 23004.2

brainstem

Sub001 Sub002 Sub003
Medulla 5055.255047 3525.950826 4963.488455
Pons 14902.281191 9690.186276 14694.493219
SCP 228.536551 154.9365 273.623867
Midbrain 6336.645556 3991.233358 5651.538489
Whole_brainstem 26522.718345 17362.30696 25583.14403



頑張れ!喝!!の代わりにB!ブックマークを押していただけるとただただうれしいです(^^)! ↓