Last active
July 11, 2024 17:49
-
-
Save RyanZurrin/190b3c9e6f04d860438aa55b9cac2d16 to your computer and use it in GitHub Desktop.
renameToBIDSAnySession is a bash script designed to create symlinks for neuroimaging data files in a BIDS (Brain Imaging Data Structure) format. The script processes structural, diffusion, and rfMRI files, creating the appropriate directory structure and symlinks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Initialize variables | |
| input_dir="/path/to/input/directory" | |
| bids_root="/path/to/BIDS/rawdata/" | |
| log_file="logs/$(date +%Y%m%d_%H%M%S)_renameToBIDSAnySession.log" | |
| cases="" | |
| case_list="" | |
| # Function to display help message | |
| usage() { | |
| echo "Usage: $0 -i input_dir -o bids_root [-l log_file] [-c cases] [-f case_list]" | |
| echo " -i input_dir : Input directory (e.g., '/path/to/Protocol/*')" | |
| echo " -o bids_root : BIDS root directory" | |
| echo " -l log_file : Log file (optional)" | |
| echo " -c cases : Specific cases to process (e.g., '5017_MR1 5018_MR2') (optional)" | |
| echo " -f case_list : Path to a file containing a list of cases to process (optional)" | |
| exit 1 | |
| } | |
| # Parse command line arguments | |
| while getopts "i:o:l:c:f:h" opt; do | |
| case ${opt} in | |
| i) input_dir=${OPTARG} ;; | |
| o) bids_root=${OPTARG} ;; | |
| l) log_file=${OPTARG} ;; | |
| c) cases=${OPTARG} ;; | |
| f) case_list=${OPTARG} ;; | |
| h) usage ;; | |
| *) usage ;; | |
| esac | |
| done | |
| # Check for required arguments | |
| if [ -z "${input_dir}" ] || [ -z "${bids_root}" ]; then | |
| usage | |
| fi | |
| # Create logs directory if it does not exist | |
| if [ ! -d "logs" ]; then | |
| mkdir logs | |
| fi | |
| # Create log file | |
| touch ${log_file} | |
| # Change to BIDS root directory | |
| cd ${bids_root} || { echo "Error: Cannot change to BIDS root directory ${bids_root}"; exit 1; } | |
| # Function to create symlinks and handle broken links | |
| create_symlink() { | |
| local from=$1 | |
| local to=$2 | |
| if [ -f ${from} ]; then | |
| ln -s ${from} ${to} | |
| echo "moving ${from}" | |
| echo "new file is $(ls ${to})" | |
| else | |
| echo "Warning: Source file ${from} does not exist. Logging to ${log_file}" | |
| echo "$(date +%Y-%m-%d\ %H:%M:%S) - Broken symlink: ${from} -> ${to}" >> ${log_file} | |
| fi | |
| # Check if the symlink is broken | |
| if [ -L ${to} ] && [ ! -e ${to} ]; then | |
| echo "Warning: Symlink ${to} is broken. Logging to ${log_file}" | |
| echo "$(date +%Y-%m-%d\ %H:%M:%S) - Broken symlink: ${to}" >> ${log_file} | |
| fi | |
| } | |
| # Function to process each case | |
| process_case() { | |
| local case=$1 | |
| echo -e '\n\n' | |
| echo "Processing ${case}" | |
| local sub_dir="sub-${case%_MR*}" | |
| local ses_dir="ses-${case#????_MR}" | |
| mkdir -p ${sub_dir}/${ses_dir}/anat | |
| mkdir -p ${sub_dir}/${ses_dir}/dwi | |
| mkdir -p ${sub_dir}/${ses_dir}/func | |
| # Process Structural files | |
| find ${input_dir}/${case}/unprocessed/3T/ -type f \( -name "*T1w*" -o -name "*T2w*" \) | while read -r from; do | |
| base_name=$(basename ${from}) | |
| ext="${base_name##*.}" | |
| modality=$(echo ${base_name} | grep -oP '(T1w|T2w)') | |
| to=${sub_dir}/${ses_dir}/anat/${sub_dir}_${ses_dir}_${modality}.${ext} | |
| create_symlink ${from} ${to} | |
| done | |
| # Process Diffusion files | |
| find ${input_dir}/${case}/unprocessed/3T/Diffusion/ -type f \( -name "*.bval" -o -name "*.bvec" -o -name "*.nii.gz" -o -name "*.json" \) | while read -r from; do | |
| base_name=$(basename ${from}) | |
| ext="${base_name##*.}" | |
| dir=$(echo ${base_name} | grep -oP '(dir\d+)') | |
| phase=$(echo ${base_name} | grep -oP '(AP|PA)') | |
| to=${sub_dir}/${ses_dir}/dwi/${sub_dir}_${ses_dir}_acq-${phase}_${dir}_dwi.${ext} | |
| create_symlink ${from} ${to} | |
| done | |
| # Process rfMRI files | |
| find ${input_dir}/${case}/unprocessed/3T/ -type f \( -name "*rfMRI*" -o -name "*REST*" \) | while read -r from; do | |
| base_name=$(basename ${from}) | |
| ext="${base_name##*.}" | |
| run=$(echo ${base_name} | grep -oP '(REST\d+)') | |
| phase=$(echo ${base_name} | grep -oP '(AP|PA)') | |
| to=${sub_dir}/${ses_dir}/func/${sub_dir}_${ses_dir}_task-rest_acq-${phase}_run-${run#REST}_bold.${ext} | |
| create_symlink ${from} ${to} | |
| done | |
| } | |
| # Function to read cases from a file | |
| read_cases_from_file() { | |
| local file=$1 | |
| while IFS= read -r line; do | |
| cases="${cases} ${line}" | |
| done < ${file} | |
| } | |
| # Process specified cases, read cases from file, or find all cases if not specified | |
| if [ -n "${case_list}" ]; then | |
| read_cases_from_file ${case_list} | |
| fi | |
| if [ -n "${cases}" ]; then | |
| # Split the case list into individual cases | |
| IFS=' ' read -r -a case_array <<< "${cases}" | |
| for case in "${case_array[@]}"; do | |
| process_case ${case} | |
| done | |
| else | |
| find ${input_dir} -mindepth 1 -maxdepth 1 -type d -exec basename {} \; | while read -r case; do | |
| process_case ${case} | |
| done | |
| fi |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
renameToBIDSAnySession
Overview
renameToBIDSAnySessionis a bash script designed to create symlinks for neuroimaging data files in a BIDS (Brain Imaging Data Structure) format. The script processes structural, diffusion, and rfMRI files, creating the appropriate directory structure and symlinks.Features
Usage
Parameters
-i input_dir: The input directory containing the neuroimaging data (e.g.,/path/to/Protocol/*).-o bids_root: The BIDS root directory where the symlinks will be created.-l log_file(optional) : The path to the log file. Defaults tologs/$(date +%Y%m%d_%H%M%S)_renameToBIDSAnySession.log.-c cases(optional) : Specific cases to process, provided as a quoted string with space-separated case identifiers (e.g.,'5017_MR1 5018_MR2').-f case_list(optional) : Path to a file containing a list of cases to process, with one case per line.Examples
Process Specific Cases
To process specific cases provided as command-line arguments:
Process Cases from a File
To process cases listed in a file (e.g.,
case_list.txt):./renameToBIDSAnySession.sh -i /rfanfs/pnl-zorro/projects/Neuroprogression/Protocol/* -o /rfanfs/pnl-zorro/projects/Neuroprogression/data_processing/BIDS2/rawdata/ -f /path/to/case_list.txtProcess All Cases
To process all cases found in the input directory:
./renameToBIDSAnySession.sh -i /rfanfs/pnl-zorro/projects/Neuroprogression/Protocol/* -o /rfanfs/pnl-zorro/projects/Neuroprogression/data_processing/BIDS2/rawdata/Logging
The script logs any broken symlink attempts to the specified log file. If no log file is specified, it defaults to
logs/$(date +%Y%m%d_%H%M%S)_renameToBIDSAnySession.log. Ensure thelogsdirectory exists or will be created by the script.Script Details
Function:
create_symlinkCreates a symlink from the source file to the target location. Logs any broken symlink attempts.
Function:
process_caseProcesses a single case, creating the appropriate directory structure and symlinks for structural, diffusion, and rfMRI files.
Function:
read_cases_from_fileReads cases from a specified file and appends them to the cases variable.
Handling Command Line Arguments
The script handles command line arguments using
getopts, supporting both specific cases and case list files.Requirements
License
This project is licensed under the MIT License. See the
LICENSEfile for details.