Skip to content

Dicom to BIDS

Alejandra López Castro edited this page Aug 13, 2024 · 9 revisions

How to convert DICOM files to BIDS format

You'll need to have dcm2niix and dcm2bids in your computer or as a conda environment in your clusters. Installation instructions

Another option is to install MRIcroGL to install dcm2niix. MRIcroGL

Then you'll need to have: config.json file necessary for the conversion:

{
    "descriptions": [
        {
            "dataType": "anat",
            "suffix": "T1w",
            "criteria": {
                "in": {"SeriesDescription": "sT1W_3D_TFE_32"}
            }
        },
        {
            "dataType": "dwi",
            "suffix": "dwi",
            "criteria": {
                "in": {"SeriesDescription": "DTI_136_2_SHELLS_AP"}
            }
        },       
        {
            "dataType": "func",
            "suffix": "bold",
            "customLabels": "task-rest",
            "criteria": {
                "equal": {"SeriesDescription": "FE_EPI_32chSHC_AP"}
            }
	}
    ]
}

This config file would have the information of the raw directories and the metadata that distinguish every sequence. An easy way to access to metadata of the DICOMs is to obtain tmp archives from dcm2bids with dcm2bids_helper -d command. tmp_dcm2bids/helper

Small script

#!/bin/bash

root=/media/egarza/INP_MRI_Backup/projects/INP/addimex_tms/data/mri
dcmdir=/media/egarza/INP_MRI_Backup/projects/INP/addimex_tms/data/mri/raw_dicom
bidsdir=/media/egarza/INP_MRI_Backup/projects/INP/addimex_tms/data/mri/bids

for i in 001 002 003 004 005 006 007 008 009; do 

subject=$i

echo "Subject ${subject}"

dcm2bids -d ${dcmdir}/${subject}/t0 -p ${subject} -s t0 -c ${root}/code/config.json
dcm2bids -d ${dcmdir}/${subject}/t1 -p ${subject} -s t1 -c ${root}/code/config.json
dcm2bids -d ${dcmdir}/${subject}/t1-4 -p ${subject} -s t14 -c ${root}/code/config.json
dcm2bids -d ${dcmdir}/${subject}/t2 -p ${subject} -s t2 -c ${root}/code/config.json

sleep 10

done

If you are working with a SLURM cluster and have dcm2bids as an environment. You could want to use the next script.

#!/bin/bash

conda activate dcm2bids

# Set the base directory where the DICOM files are located
base_dir="/misc/tesla1/alopez/nobackup2/patients/bids_project/sourcedata"

# Set the output directory for the BIDS format
bids_dir="/misc/tesla1/alopez/nobackup2/patients/bids_project"

# Subjects list 
subjects=("22")

# Session list
sessions = ("01" "02")


# Convert DICOM to NIfTI for each subject
for subject in "${subjects[@]}"; do
  for session in "${sessions[@]"; do
    fsl_sub -T 2 -l "bids_dir"/tmp_dcm2bids/log dcm2bids -d "$base_dir" -p "$subject" -s "$session" --config "$bids_dir"/code/config.json -o "$bids_dir"/sub-"$subject"/ses-"$session" -m y -v y -z y  --force_dcm2bids -l DEBUG 
  done
done

Clone this wiki locally