Skip to content

Dicom to BIDS

Alejandra López Castro edited this page Oct 7, 2025 · 9 revisions

Ir a como usar dcm2bids en Don Clusterio

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.

Create the bids directories

#!/bin/bash

#Set the path where the directories will be created
base_dir="/misc/directory/alopez/patients/bids_project"

# Create directories for subjects 16, 17, and 18
for subject in 16 17 18; do
  for session in 01 02; do
     mkdir -p "$base_dir"/sub-"$subject"
     mkdir -p "$base_dir"/sub-"$subject"/ses-"$session"
     mkdir -p "$base_dir"/sub-"$subject"/ses-"$session"/anat
     mkdir -p "$base_dir"/sub-"$subject"/ses-"$session"/func
     mkdir -p "$base_dir"/sub-"$subject"/ses-"$session"/fmap
     mkdir -p "$base_dir"/sub-"$subject"/ses-"$session"/dwi
  done
done

Convert your dicom archives to nifti and put it in the bids format

#!/bin/bash

conda activate dcm2bids

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

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

# Subjects list 
subjects=("01")

# 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