I am trying to create a for-loop in terminal to create the fieldmap with the topup function of fsl in all my participants. The base script I have for a single participant is this one:
The base script I have:
#!/bin/bash
# Directorio base
for i in {101,103,106}
do
base_dir="/home/jovyan/neurodesktop-storage/Agueda/BIDS/derivatives/CONN/dynamic_prueba/BIDS"
participant_dir="/home/jovyan/neurodesktop-storage/Agueda/BIDS/derivatives/CONN/dynamic_prueba/BIDS/sub-$i"
session_dir="/home/jovyan/neurodesktop-storage/Agueda/BIDS/derivatives/CONN/dynamic_prueba/BIDS/sub-$i/ses-1"
fmap_dir="/home/jovyan/neurodesktop-storage/Agueda/BIDS/derivatives/CONN/dynamic_prueba/BIDS/sub-$i/ses-1/fmap"
# Bucle a través de carpetas de participantes
for i in ${fmap_dir}sub-*/; do
# Bucle a través de archivos en la carpeta fmap
for i in sub-${i}-ses-1_task-rest_dir_**_fieldmap.nii.gz; do
# Obtener el nombre base del archivo sin extensión
base_name="${sub-${i}-ses-1_task-rest_dir_**_fieldmap.*}"
# Aplicar los comandos de FSL
fslmerge -t "${base_name}.nii.gz" "${base_name}"_dir-AP_fieldmap.nii.gz "${base_name}"_dir-PA_fieldmap.nii.gz
topup --imain="${participant_dir}${base_name}.nii.gz" --datain="${base_name}_acq_param.txt" --config=b02b0.cnf --out="${participant_dir}${base_name}_topup_results" --fout="${participant_dir}${base_name}_fieldmap" --iout="${participant_dir}${base_name}_magnitude"
done
# Volver al directorio original
cd -
fi
done
done
#!/bin/sh
# andrea_topup.sh
#
#
# Created by Agueda on 13/2/24.
#
If someone have done this before or have an example of a for-loop script that could share with me it would be helpful!
Additionally, SDCFlows (which is called internally by fMRIPrep) can be run as a standalone tool on the command line, which is good if your data are in BIDS format.
If it helps to someone, here i give you the script that i created basing it in the one from @jsein:
#!/bin/sh
#ml fsl #si no funciona escribir en el terminal
# Sesión 1
acq_parameters=/home/jovyan/neurodesktop-storage/Agueda/BIDS/derivatives/CONN/dynamic_prueba/BIDS/derivatives/task-rest_fieldmap_topup_acq_parameters.txt
for sub in 101 103 106
do
sub=sub-${sub}
echo -----starting subject $sub------
SES1DIR=/home/jovyan/neurodesktop-storage/Agueda/BIDS/derivatives/CONN/dynamic_prueba/BIDS/${sub}/ses-1
# Verificar si SES1DIR existe
if [ -d "$SES1DIR" ]; then
fslmerge -t "${SES1DIR}/fmap/${sub}_ses-1_AP_PA" "${SES1DIR}/fmap/${sub}_ses-1_task-rest_dir-AP_fieldmap.nii.gz" "${SES1DIR}/fmap/${sub}_ses-1_task-rest_dir-PA_fieldmap.nii.gz"
topup --imain="${SES1DIR}/fmap/${sub}_ses-1_AP_PA" --datain="${acq_parameters}" --config=b02b0.cnf --out="${SES1DIR}/fmap/${sub}_ses-1_topup_results" --fout="${SES1DIR}/fmap/${sub}_ses-1_fieldmap" --iout="${SES1DIR}/fmap/${sub}_ses-1_magnitude"
else
echo "El directorio $SES1DIR no existe. Saltando a la siguiente iteración."
fi
done
# Sesión 6
for sub in 101 103 106
do
sub=sub-${sub}
echo -----starting subject $sub------
SES6DIR=/home/jovyan/neurodesktop-storage/Agueda/BIDS/derivatives/CONN/dynamic_prueba/BIDS/${sub}/ses-6
# Verificar si SES6DIR existe
if [ -d "$SES6DIR" ]; then
fslmerge -t "${SES6DIR}/fmap/${sub}_ses-6_AP_PA" "${SES6DIR}/fmap/${sub}_ses-6_task-rest_dir-AP_fieldmap.nii.gz" "${SES6DIR}/fmap/${sub}_ses-6_task-rest_dir-PA_fieldmap.nii.gz"
topup --imain="${SES6DIR}/fmap/${sub}_ses-6_AP_PA" --datain="${acq_parameters}" --config=b02b0.cnf --out="${SES6DIR}/fmap/${sub}_ses-6_topup_results" --fout="${SES6DIR}/fmap/${sub}_ses-6_fieldmap" --iout="${SES6DIR}/fmap/${sub}_ses-6_magnitude"
else
echo "El directorio $SES6DIR no existe. Saltando a la siguiente iteración."
fi
done