Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added initial scripts for LiFE #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions 000_view_preprocessing_results
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
Check_preprocessing () {

subj=$1

low_res=/home/auguser2016/dMRI_DATA/PREPROCESSED_DATA/1.5mm_iso/$subj
high_res=/home/auguser2016/dMRI_DATA/PREPROCESSED_DATA/0.75mm_iso/$subj

# View denoising maps. The less anatomical structures are visible the better
mrview $low_res/Temporal/Distortions/$subj\_series*

# Compare DWI before and after denoising for each encoding direction
mrview $low_res/Temporal/*AP.mif $low_res/Temporal/*AP_denoised.mif
mrview $low_res/Temporal/*PA.mif $low_res/Temporal/*PA_denoised.mif

# Compare mrtrix gradient for corrected DWI with original gradient table
mrinfo $low_res/Temporal/$subj\_*AP_denoised.mif -export_grad_fsl $low_res/Temporal/bvecs $low_res/Temporal/bvals
gedit $low_res/Temporal/bvecs &
gedit $low_res/Temporal/Distortions/$subj\_gradients &
gedit /home/auguser2016/dMRI_DATA/RAW_DATA/$subj*/s*/*ep2d*/*.bvec
rm $low_res/Temporal/bvecs $low_res/Temporal/bvals

# Compare DWI before and after dwipreproc
mrview $low_res/Temporal/*AP.mif $low_res/Temporal/*PA.mif $low_res/Temporal/$subj\_denoised_preproc.mif

# View mask used for bias field correction
mrview $low_res/Temporal/$subj\_denoised_preproc.mif -overlay.load $low_res/$subj\_clean_150mm_mask.mif -overlay.opacity 0.2 -overlay.colourmap 3

# View bias field
mrview $low_res/Temporal/Distortions/$subj\_bias_field.mif

# View DWI before and after bias correction
mrview $low_res/Temporal/$subj\_denoised_preproc.mif $low_res/$subj\_clean_150mm.mif

# View normal and upsampled images
mrview $low_res/$subj\_clean_150mm.mif $high_res/$subj\_clean_075mm.mif

# View upsampled mask
mrview $high_res/$subj\_clean_075mm.mif -overlay.load $high_res/$subj\_clean_075mm_mask.mif -overlay.opacity 0.2 -overlay.colourmap 3

# View SFR estimation voxels
mrview $high_res/$subj\_clean_075mm.mif -overlay.load $high_res/Temporal/$subj\_clean_075mm_SFR_voxels.mif

# View SFR
shview $high_res/$subj\_clean_075mm_SFR.txt

# View anatomical image overlaid on upsampled FOD
mrview $high_res/$subj\_clean_075mm.mif $high_res/$subj\_075mm_anatomical_coreg2nodif_undist_2.nii.gz

# View results of bet2
mrview $high_res/$subj\_anatomical.nii.gz -overlay.load $high_res/Temporal/$subj\_anatomical_brain.nii.gz -overlay.opacity 0.3

# View segmentation
mrview $high_res/$subj\_075mm_anatomical_coreg2nodif_undist_2.nii.gz -overlay.load $high_res/$subj\_075mm_5tt.nii.gz -overlay.opacity 0.3

# Correct WM segmentation in chiasm - modify opened ROI and close mrview. Modified WM segmentation image will be shown - repeat previous steps until satisfactory result is obtained. Terminate console to finish process
mrview $high_res/$subj\_075mm_5tt.nii.gz -colourmap 3 -overlay.load $high_res/$subj\_075mm_anatomical_coreg2nodif_undist_2.nii.gz -overlay.colourmap 0 -overlay.opacity 0.8 -roi.load $high_res/$subj\_5tt_patch.mif

5ttedit $high_res/$subj\_075mm_5tt.nii.gz -wm $high_res/$subj\_5tt_patch.mif $high_res/$subj\_5tt_for_act_modified.nii.gz -force

while true; do
mrview $high_res/$subj\_5tt_for_act_modified.nii.gz -colourmap 3 -overlay.load $high_res/$subj\_075mm_anatomical_coreg2nodif_undist_2.nii.gz -overlay.colourmap 0 -overlay.opacity 0.8 -roi.load $high_res/$subj\_5tt_patch.mif
5ttedit $high_res/$subj\_075mm_5tt.nii.gz -wm $high_res/$subj\_5tt_patch.mif $high_res/$subj\_5tt_for_act_modified.nii.gz -force
done

}

for i in /home/auguser2016/dMRI_DATA/PREPROCESSED_DATA/1.5mm_iso/*; do Check_preprocessing ${i: -4}; done
117 changes: 117 additions & 0 deletions 00_fsl_preprocess.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Initializing

# Folder with raw and preprocessed data
data=/home/auguser2016/dMRI_DATA

Preparation () {

# 2 resolutions (1.5 mm and 0.75mm), inside each folder all preprocessed data sets of corresponding resolution with masks, map of distortions etc.
a=${1::-6}

low_res=PREPROCESSED_DATA/1.5mm_iso/$a
high_res=PREPROCESSED_DATA/0.75mm_iso/$a

mkdir $data/$low_res
mkdir $data/$low_res/Temporal
mkdir $data/$low_res/Temporal/Distortions

mkdir $data/$high_res
mkdir $data/$high_res/Temporal


cd $data/RAW_DATA/${1::-1}/s*/

# Raw data files - DWI and anatomy - unpacked with MRIcron (dcm2nii) as a 4D Nifti images. Raw data goes: RAW_DATA/data_set_name/study.../studies_X/ where important are those with ep2d
# Transforming Nifti into .mif files and denoising them. Denoising is the very first that should be done - it's results can be viewed.

for b in *ep2d*; do
cd $b
mrconvert *a001.nii -stride -1,2,3,4 -fslgrad *.bvec *.bval $data/$low_res/Temporal/$a\_$b\.mif
dwidenoise $data/$low_res/Temporal/$a\_$b.mif $data/$low_res/Temporal/$a\_$b\_denoised.mif -noise $data/$low_res/Temporal/Distortions/$a\_$b\_noise.mif -debug
cd ..
done

# Script from MRtrix interacting with fsl. Here version for correcting DWI with 2 phase encoding directions is used
dwipreproc AP $data/$low_res/Temporal/*AP_denoised.mif -rpe_all $data/$low_res/Temporal/*PA_denoised.mif $data/$low_res/Temporal/$a\_denoised_preproc.mif -export_grad_mrtrix $data/$low_res/Temporal/Distortions/$a\_gradients

# Masking preprocessed data. Results should be checked
dwi2mask $data/$low_res/Temporal/$a\_denoised_preproc.mif $data/$low_res/$a\_clean_150mm_mask.mif

# Masked is used for bias field correction using ANTS software
dwibiascorrect -ants $data/$low_res/Temporal/$a\_denoised_preproc.mif $data/$low_res/$a\_clean_150mm.mif -mask $data/$low_res/$a\_clean_150mm_mask.mif -bias $data/$low_res/Temporal/Distortions/$a\_bias_field.mif

# Preprocessing of 1.5 mm iso data sets done. Moving to upsampled resolution

# Upsampling by factor of 2 to 0.75mm. Justified step, in FBA even factor 3 can be recommended
mrresize $data/$low_res/$a\_clean_150mm.mif -scale 2.0 $data/$high_res/$a\_clean_075mm.mif

# Calculating mask again
dwi2mask $data/$high_res/$a\_clean_075mm.mif $data/$high_res/$a\_clean_075mm_mask.mif

# Estimating single fibre response (SFR) for default lmax
dwi2response tournier $data/$high_res/$a\_clean_075mm.mif $data/$high_res/$a\_clean_075mm_SFR.txt -shell 1600 -lmax 8 -mask $data/$high_res/$a\_clean_075mm_mask.mif -voxels $data/$high_res/Temporal/$a\_clean_075mm_SFR_voxels.mif

# Estimating FOD for previously obtained SFR
dwiextract $data/$high_res/$a\_clean_075mm.mif - | dwi2fod msmt_csd - $data/$high_res/$a\_clean_075mm_SFR.txt $data/$high_res/$a\_clean_075mm_FOD.mif -mask $data/$high_res/$a\_clean_075mm_mask.mif

# Performing coregistration of anatomical image to DWI images and segmenting it. It's easier approach as gradient table is not rotated this way. However for cross-modal studies it would be better to register DWI to anatomy

cd *MPRAGE*iso

# Converting DWI image from .mif to .nii.gz as FSL likes. This step should be checked as well.
mrconvert $data/$high_res/$a\_clean_075mm.mif $data/$high_res/Temporal/$a\_clean_075mm.nii.gz

# Coping anatomical image to corresponding folder
cp o* $data/$high_res/$a\_anatomical.nii.gz

# Extracting clean brain from anatomical image. Check this step
bet2 $data/$high_res/$a\_anatomical.nii.gz $data/$high_res/Temporal/$a\_anatomical_brain.nii.gz -f 0.6

# Corregister DWI to anatomy
epi_reg --epi=$data/$high_res/Temporal/$a\_clean_075mm.nii.gz --t1=$data/$high_res/$a\_anatomical.nii.gz --t1brain=$data/$high_res/Temporal/$a\_anatomical_brain.nii.gz --out=$data/$high_res/Temporal/$a\_nodif2mprage_2 --echospacing=0.00023 --pedir=-y

# Reverse corregistration matrix
convert_xfm -inverse $data/$high_res/Temporal/$a\_nodif2mprage_2.mat -omat $data/$high_res/Temporal/$a\_mprage2nodif_2.mat

# Apply reverted matrix to corregister anatomy to DWI
flirt -in $data/$high_res/$a\_anatomical.nii.gz -ref $data/$high_res/Temporal/$a\_clean_075mm.nii.gz -out $data/$high_res/$a\_075mm_anatomical_coreg2nodif_undist_2 -applyxfm -init $data/$high_res/Temporal/$a\_mprage2nodif_2.mat -interp sinc -sincwidth 7 -sincwindow hanning

# Perform segmentation into WM, GM, subcortical GM, CSF and possible pathological tissue using FIRST from FSL
5ttgen fsl $data/$high_res/$a\_075mm_anatomical_coreg2nodif_undist_2.nii.gz $data/$high_res/$a\_075mm_5tt.nii.gz

}

cd $data/RAW_DATA

# List of all data_sets used in studies
list=(fe21_1325/ hw91_0844/ ib57_0731/ kw99_0633/ lw37_0977/ nb30_1185/ ow93_0974/ ps94_1516/ rx88_1234/ sj22_1218/ ta14_1065/ tq63_1214/ xs62_1217/ xn78_1085/ uh47_1309/ uf97_1072/ ow93_0974/ )
#list=(fe21_1325/)

# This way 4 sets are processed at once - we can't process all at once due to RAM usage, however preprocessing one at a time is ineffective because e.g. eddy from dwipreproc is using only one core. There is a version of eddy operating on multicore processors, but it's not running on this machine

for i in ${list[@]:0:3}; do Preparation $i & done
wait
for i in ${list[@]:3:6}; do Preparation $i & done
wait
for i in ${list[@]:6:9}; do Preparation $i & done
wait
for i in ${list[@]:9:12}; do Preparation $i & done
wait
for i in ${list[@]:12:15}; do Preparation $i & done
wait
for i in ${list[@]:15:18}; do Preparation $i & done
wait
for i in ${list[@]:18:21}; do Preparation $i & done
wait
for i in ${list[@]:21:24}; do Preparation $i & done
wait
for i in ${list[@]:24:27}; do Preparation $i & done
wait
for i in ${list[@]:27:30}; do Preparation $i & done
wait
for i in ${list[@]:30:33}; do Preparation $i & done
wait

# Returning to original folder
cd /home/auguser2016/Scripts/

117 changes: 117 additions & 0 deletions 00_fsl_preprocess.sh~
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Initializing

# Folder with raw and preprocessed data
data=/home/auguser2016/dMRI_DATA

Genral_preprocessing () {

# 2 resolutions (1.5 mm and 0.75mm), inside each folder all preprocessed data sets of corresponding resolution with masks, map of distortions etc.
a=${1::-6}

low_res=PREPROCESSED_DATA/1.5mm_iso/$a
high_res=PREPROCESSED_DATA/0.75mm_iso/$a

mkdir $data/$low_res
mkdir $data/$low_res/Temporal
mkdir $data/$low_res/Temporal/Distortions

mkdir $data/$high_res
mkdir $data/$high_res/Temporal


cd $data/RAW_DATA/${1::-1}/s*/

# Raw data files - DWI and anatomy - unpacked with MRIcron (dcm2nii) as a 4D Nifti images. Raw data goes: RAW_DATA/data_set_name/study.../studies_X/ where important are those with ep2d
# Transforming Nifti into .mif files and denoising them. Denoising is the very first that should be done - it's results can be viewed.

for b in *ep2d*; do
cd $b
mrconvert *a001.nii -stride -1,2,3,4 -fslgrad *.bvec *.bval $data/$low_res/Temporal/$a\_$b\.mif
dwidenoise $data/$low_res/Temporal/$a\_$b.mif $data/$low_res/Temporal/$a\_$b\_denoised.mif -noise $data/$low_res/Temporal/Distortions/$a\_$b\_noise.mif -debug
cd ..
done

# Script from MRtrix interacting with fsl. Here version for correcting DWI with 2 phase encoding directions is used
dwipreproc AP $data/$low_res/Temporal/*AP_denoised.mif -rpe_all $data/$low_res/Temporal/*PA_denoised.mif $data/$low_res/Temporal/$a\_denoised_preproc.mif -export_grad_mrtrix $data/$low_res/Temporal/Distortions/$a\_gradients

# Masking preprocessed data. Results should be checked
dwi2mask $data/$low_res/Temporal/$a\_denoised_preproc.mif $data/$low_res/$a\_clean_150mm_mask.mif

# Masked is used for bias field correction using ANTS software
dwibiascorrect -ants $data/$low_res/Temporal/$a\_denoised_preproc.mif $data/$low_res/$a\_clean_150mm.mif -mask $data/$low_res/$a\_clean_150mm_mask.mif -bias $data/$low_res/Temporal/Distortions/$a\_bias_field.mif

# Preprocessing of 1.5 mm iso data sets done. Moving to upsampled resolution

# Upsampling by factor of 2 to 0.75mm. Justified step, in FBA even factor 3 can be recommended
mrresize $data/$low_res/$a\_clean_150mm.mif -scale 2.0 $data/$high_res/$a\_clean_075mm.mif

# Calculating mask again
dwi2mask $data/$high_res/$a\_clean_075mm.mif $data/$high_res/$a\_clean_075mm_mask.mif

# Estimating single fibre response (SFR) for default lmax
dwi2response tournier $data/$high_res/$a\_clean_075mm.mif $data/$high_res/$a\_clean_075mm_SFR.txt -shell 1600 -lmax 8 -mask $data/$high_res/$a\_clean_075mm_mask.mif -voxels $data/$high_res/Temporal/$a\_clean_075mm_SFR_voxels.mif

# Estimating FOD for previously obtained SFR
dwiextract $data/$high_res/$a\_clean_075mm.mif - | dwi2fod msmt_csd - $data/$high_res/$a\_clean_075mm_SFR.txt $data/$high_res/$a\_clean_075mm_FOD.mif -mask $data/$high_res/$a\_clean_075mm_mask.mif

# Performing coregistration of anatomical image to DWI images and segmenting it. It's easier approach as gradient table is not rotated this way. However for cross-modal studies it would be better to register DWI to anatomy

cd *MPRAGE*iso

# Converting DWI image from .mif to .nii.gz as FSL likes. This step should be checked as well.
mrconvert $data/$high_res/$a\_clean_075mm.mif $data/$high_res/Temporal/$a\_clean_075mm.nii.gz

# Coping anatomical image to corresponding folder
cp o* $data/$high_res/$a\_anatomical.nii.gz

# Extracting clean brain from anatomical image. Check this step
bet2 $data/$high_res/$a\_anatomical.nii.gz $data/$high_res/Temporal/$a\_anatomical_brain.nii.gz -f 0.6

# Corregister DWI to anatomy
epi_reg --epi=$data/$high_res/Temporal/$a\_clean_075mm.nii.gz --t1=$data/$high_res/$a\_anatomical.nii.gz --t1brain=$data/$high_res/Temporal/$a\_anatomical_brain.nii.gz --out=$data/$high_res/Temporal/$a\_nodif2mprage_2 --echospacing=0.00023 --pedir=-y

# Reverse corregistration matrix
convert_xfm -inverse $data/$high_res/Temporal/$a\_nodif2mprage_2.mat -omat $data/$high_res/Temporal/$a\_mprage2nodif_2.mat

# Apply reverted matrix to corregister anatomy to DWI
flirt -in $data/$high_res/$a\_anatomical.nii.gz -ref $data/$high_res/Temporal/$a\_clean_075mm.nii.gz -out $data/$high_res/$a\_075mm_anatomical_coreg2nodif_undist_2 -applyxfm -init $data/$high_res/Temporal/$a\_mprage2nodif_2.mat -interp sinc -sincwidth 7 -sincwindow hanning

# Perform segmentation into WM, GM, subcortical GM, CSF and possible pathological tissue using FIRST from FSL
5ttgen fsl $data/$high_res/$a\_075mm_anatomical_coreg2nodif_undist_2.nii.gz $data/$high_res/$a\_075mm_5tt.nii.gz

}

cd $data/RAW_DATA

# List of all data_sets used in studies
list=(fe21_1325/ hw91_0844/ ib57_0731/ kw99_0633/ lw37_0977/ nb30_1185/ ow93_0974/ ps94_1516/ rx88_1234/ sj22_1218/ ta14_1065/ tq63_1214/ xs62_1217/ xn78_1085/ uh47_1309/ uf97_1072/ ow93_0974/ )
#list=(fe21_1325/)

# This way 4 sets are processed at once - we can't process all at once due to RAM usage, however preprocessing one at a time is ineffective because e.g. eddy from dwipreproc is using only one core. There is a version of eddy operating on multicore processors, but it's not running on this machine

for i in ${list[@]:0:3}; do Preparation $i & done
wait
for i in ${list[@]:3:6}; do Preparation $i & done
wait
for i in ${list[@]:6:9}; do Preparation $i & done
wait
for i in ${list[@]:9:12}; do Preparation $i & done
wait
for i in ${list[@]:12:15}; do Preparation $i & done
wait
for i in ${list[@]:15:18}; do Preparation $i & done
wait
for i in ${list[@]:18:21}; do Preparation $i & done
wait
for i in ${list[@]:21:24}; do Preparation $i & done
wait
for i in ${list[@]:24:27}; do Preparation $i & done
wait
for i in ${list[@]:27:30}; do Preparation $i & done
wait
for i in ${list[@]:30:33}; do Preparation $i & done
wait

# Returning to original folder
cd /home/auguser2016/Scripts/

Loading