-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path03_prepare_fieldmaps.sh
More file actions
129 lines (123 loc) · 6.71 KB
/
Copy path03_prepare_fieldmaps.sh
File metadata and controls
129 lines (123 loc) · 6.71 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash
echo '
# ----------------------------------------------------------------------------------------------------------------------------------------------
# Script name: 03_prepare_fieldmaps.sh
#
# Version: 1
#
# Version Date: July 10th, 2024
#
# Version Notes: None
#
# Description: This script prepares the fieldmaps for use in FEAT or MELODIC.
#
# Authors: Justin W. Andrushko, PhD, Vice-Chancellor Fellow & Assistant Professor, Department of Sport, Exercise and Rehabilitation, Northumbria University
#
# Intended For: Raw nifti and JSON files
#
# User Guide: N/A
#
# Disclaimer: Use scripts at own risk, the authors do not take responsibility for any errors or typos
# that may exist in the scripts original or edited form.
# ----------------------------------------------------------------------------------------------------------------------------------------------'
#--------------------------------------#
# Add FSL Oxford Server Specific #
#--------------------------------------#
module add fsl
#--------------------------------------#
# Define Directories #
#--------------------------------------#
WDIR=/home/fs0/yzg018/scratch/pfcl_study_X # Set this to your top level BIDS formatted directory
rawdata=$WDIR/rawdata # BIDS formatted data directory
derivatives=$WDIR/derivatives
# Function to expand wildcards and store filenames in an array
expand_files() {
local pattern="$1"
local -n arr_ref="$2" # Use nameref for array variable
arr_ref=() # Initialize the array
for file in $pattern; do
[ -e "$file" ] && arr_ref+=("$file") # Check if file exists and add to array
done
}
#--------------------------------------#
# For Loop to Run the Script #
#--------------------------------------#
cd $rawdata
for subject in sub-* ; do
if [ -d "$rawdata/$subject" ] ; then
echo $subject
cd $rawdata/$subject
for session in ses-03 ; do
echo $session
cd $rawdata/$subject/$session
echo "Starting fieldmap prep for:" $subject $session
fmap_dir="$rawdata/$subject/$session/fmap"
# Expand wildcards and assign to arrays
expand_files "$fmap_dir/*fmap_magnitude1.nii" mag1_files
expand_files "$fmap_dir/*fmap_magnitude1.nii.gz" temp_files
mag1_files+=("${temp_files[@]}")
expand_files "$fmap_dir/*fmap_magnitude2.nii" mag2_files
expand_files "$fmap_dir/*fmap_magnitude2.nii.gz" temp_files
mag2_files+=("${temp_files[@]}")
expand_files "$fmap_dir/*fmap_phasediff.nii" phasediff_files
expand_files "$fmap_dir/*fmap_phasediff.nii.gz" temp_files
phasediff_files+=("${temp_files[@]}")
# Ensure all arrays have the same length
len=${#mag1_files[@]}
if [[ ${#mag2_files[@]} -ne $len || ${#phasediff_files[@]} -ne $len ]]; then
echo "Error: File arrays have different lengths"
exit 1
fi
# Loop through the files
for ((i = 0; i < len; i++)); do
mag1=${mag1_files[$i]}
echo "$mag1"
mag2=${mag2_files[$i]}
echo "$mag2"
phasediff=${phasediff_files[$i]}
echo "$phasediff"
if [ -f "$mag1" ] ; then
if [[ "$mag1" == *.nii.gz ]]; then
mag1_name=$(basename "$mag1" .nii.gz)
mag1_name_noext="${mag1_name%%.*}"
mag2_name=$(basename "$mag2" .nii.gz)
mag2_name_noext="${mag2_name%%.*}"
phasediff_name=$(basename "$phasediff" .nii.gz)
phasediff_name_noext="${phasediff_name%%.*}"
else
mag1_name=$(basename "$mag1" .nii)
mag1_name_noext="${mag1_name%.*}"
mag2_name=$(basename "$mag2" .nii)
mag2_name_noext="${mag2_name%.*}"
phasediff_name=$(basename "$phasediff" .nii)
phasediff_name_noext="${phasediff_name%.*}"
fi
fi
echo "Processing files:"
echo "Magnitude1: $subject $session $mag1_name"
echo "Magnitude2: $subject $session $mag2_name"
echo "PhaseDiff: $subject $session $phasediff_name"
# fieldmap prep and store in derivatives
mkdir -p $derivatives/$subject/$session/fmap/
if [ ! -f "$derivatives/$subject/$session/fmap/${subject}_${session}_fmap_rads.nii.gz" ] ; then
#calculate the mean of magnitude1 and magnitude2
fslmaths $rawdata/$subject/$session/fmap/$mag1_name -add $rawdata/$subject/$session/fmap/$mag2_name -div 2 $derivatives/$subject/$session/fmap/${subject}_${session}_fmap_mean_magnitude.nii.gz
#brain extract and errode
bet $derivatives/$subject/$session/fmap/${subject}_${session}_fmap_mean_magnitude.nii.gz $derivatives/$subject/$session/fmap/${subject}_${session}_fmap_mean_magnitude_brain.nii.gz
fslmaths $derivatives/$subject/$session/fmap/${subject}_${session}_fmap_mean_magnitude_brain.nii.gz -kernel boxv 3.8 -ero $derivatives/$subject/$session/fmap/${subject}_${session}_fmap_mean_magnitude_brain.nii.gz
#Extract TE for magnitude1 and magnitude2 from the JSON files
magnitude1_TE=$(awk -F ': ' '/"EchoTime"/ {gsub(/[^0-9.]/, "", $2); print $2}' $rawdata/$subject/$session/fmap/${mag1_name_noext}.json)
echo "Magnitude1 Echo Time: $magnitude1_TE"
magnitude2_TE=$(awk -F ': ' '/"EchoTime"/ {gsub(/[^0-9.]/, "", $2); print $2}' $rawdata/$subject/$session/fmap/${mag2_name_noext}.json)
echo "Magnitude2 Echo Time: $magnitude2_TE"
#calculate deltaTE (required for fsl_prepare_fieldmaps input)
deltaTE=$(awk -v v1=$magnitude1_TE -v v2=$magnitude2_TE 'BEGIN {print (v2 - v1) * 1000}')
echo "Calculated deltaTE for" $subject $session $deltaTE
#run fsl_prepare_fieldmaps
fsl_prepare_fieldmap SIEMENS $rawdata/$subject/$session/fmap/$phasediff_name $derivatives/$subject/$session/fmap/${subject}_${session}_fmap_mean_magnitude_brain.nii.gz $derivatives/$subject/$session/fmap/${subject}_${session}_fmap_rads.nii.gz $deltaTE # 1.02 for PFCL tACS
echo "fielmap preparation is complete for:" $subject $session
fi
done
done
fi
done