Niifti conversion to BIDS?

A colleague will be sending me a series of nifti files converted with dcm2niix, along with their .json side-cars. These would be single participant, single session. Does there exist a tool that organizes nifti+json pairs into a bids structure, or would this be something I simply need to do manually?

From a colleague in Paris:

1 Like

Here is a list of software available for BIDS conversion : https://bids.neuroimaging.io/benefits#mri-and-pet-converters
https://github.com/benoitberanger/niix2bids has different objective : it is made for fast BIDS conversion but has to cut some corners. And It only works for Siemens magnets for now. I made this tool in order for our lab to convert lors of old datasets without any configuration config file requirement.

2 Likes

For one-offs or small datasets I usually do the conversion semi-manually, with an R script like below. Part of why I prefer this manual style is to be able to double-check the conversion (having an explicit record of which scans were assigned which names can be invaluable). Some programs create records along these lines, but for me, the time needed to configure and verify the programs can be longer than manual scripting for small datasets.

Here’s a bit of a script to give the idea (an old, but complete script is here; I can share updated versions if there’s interest):

sub.id <- "9998";  # subject ID
in.path <- paste0("C:/BIDS/dicoms/", sub.id, "/scans/");       # top-level path to source dicoms
ds.path <- paste0("C:/BIDS/bids/sub-", sub.id, "/ses-1/");   # top-level path to where session will be written in BIDS format
dcm.path <- "C:/Users//MRIcroGL_windows/MRIcroGL/Resources/dcm2niix.exe";   # https://www.nitrc.org/projects/mricrogl/

# function to rename the created .nii.gz and .json from the name given them by dcm2niix to proper BIDS.
# The function prints TRUE if everything worked properly, an error message if not.
# img.num <- 9;  # first number in the dicom scans directory name, e.g., 9_T1 MPRAGE_sag_p2 iso 1_176
# new.fname <- paste0("sub-", sub.id, "_ses-1_T1w");   # desired BIDS name for this file
do.rename <- function(img.num, new.fname) {
  file.rename(from=list.files(out.path, pattern=paste0("_", img.num, ".json"), full.names=TRUE), to=paste0(out.path, new.fname, ".json"));  
  file.rename(from=list.files(out.path, pattern=paste0("_", img.num, ".nii.gz"), full.names=TRUE), to=paste0(out.path, new.fname, ".nii.gz"));  
}

#######  anat (T1 and T2 anatomy)
out.path <- paste0(ds.path, "anat/");
if (!dir.exists(out.path)) { dir.create(out.path); }

system2(dcm.path, args=paste0("-b y -z y -v y -o ", out.path, ' -f "%t_%p_%s" "', in.path, '23_T1 MPRAGE_sag_p2 iso 1_176"'), stdout=TRUE);
do.rename(23, paste0("sub-", sub.id, "_ses-1_T1w"));

system2(dcm.path, args=paste0("-b y -z y -v y -o ", out.path, ' -f "%t_%p_%s" "', in.path, '24_t2_spc_sag_p2_iso 1"'), stdout=TRUE);
do.rename(24, paste0("sub-", sub.id, "_ses-1_T2w"));

####### func (bold & sbref)
out.path <- paste0(ds.path, "func/");
if (!dir.exists(out.path)) { dir.create(out.path); }

system2(dcm.path, args=paste0("-b y -z y -v y -o ", out.path, ' -f "%t_%p_%s" "', in.path, '11_tfMRI_IAPS1_3p0FA66_AP_SBRef"'), stdout=TRUE);
do.rename(11, paste0("sub-", sub.id, "_ses-1_task-IAPS_run-1_sbref"));

system2(dcm.path, args=paste0("-b y -z y -v y -o ", out.path, ' -f "%t_%p_%s" "', in.path, '12_tfMRI_IAPS1_3p0FA66_AP"'), stdout=TRUE);
do.rename(12, paste0("sub-", sub.id, "_ses-1_task-IAPS_run-1_bold"));

# etc.

@benoitberanger the dcm2niix source code contains some hints on deriving modality from a Siemens MRI using the DICOM sequence name (0018,0024) which is stored as SequenceName in the BIDS JSON:

  • ep_b: dwi
  • epfid2d: perf
  • epfid2d: bold
  • epfid3d1_15: swi
  • epse2d: dwi (when b-vals specified)
  • epse2d: fmap (spin echo, e.g. TOPUP, nb could also be extra B=0 for DWI sequence)
  • fl2d: localizer
  • fl3d1r_t: angio
  • fl3d1r_tm: angio
  • fl3d1r: angio
  • fl3d1r: swi
  • fl3d1r: ToF
  • fm2d: fmap (gradient echo, e.g. FUGUE)
  • spc3d: T2
  • spcir: flair (dark fluid)
  • spcR: PD
  • tfl3d: T1
  • tfl_me3d5_16ns: T1 (ME-MPRAGE)
  • tir2d: flair
  • tse2d: PD (short TE)
  • tse2d: T2 (long TE)
  • tse3d: T2
1 Like

@neurolabusc thanks, this list will be useful for the decision tree niix2bids/config_file/siemens.py

@benoitberanger I will add your repo to the list of “converters” on the BIDS website.

@Remi-Gau The converter is not tested intensively. Is it ok to wait before adding it on the website ? In the next weeks/months I should have access to some large datasets from other MRI labs. This will help me check the robustness of the code.

@benoitberanger I have archived Siemens data that you might find useful. Most of my archives are designed to examine specific issues rather than typical datasets. However, you may find them useful. You can look at my OSF datasets, archival data, and many of my dcm_qa validation datasets e.g. dcm_qa_fmap, dcm_qa_stc, dcm_qa_nih, dcm_qa, dcm_qa_asl, etc.

oh yeah of course.
just ping me when you feel OK having it listed there.

The nibabel plugin from BIDScoin can (easily) convert nifti datasets to BIDS:
https://bidscoin.readthedocs.io/en/stable/options.html#nibabel2bids-plugin