Rename files after BIDS Conversion with dcm2bids

Hey everyone. Does anyone has a bash script or something to rename the BIDS files once you do the conversion?

I need a different order renaming files, but I am wondering that dcm2bids has that option, that’s why I am asking for the possibility of a bash script (maybe manually?), but the thing is a lot of data!

e.g.:
I got from dcm2bids: “acq-MoCoHR_task-AIR_T2star”
I want to: “task-AIR_acq-MoCoHR_T2star”

Thank you so much!

Hi @dbg0899

I do not recommend doing this, as it will cause incompatibility with BIDS software. Why do you need this?

Best,
Steven

Because I am going to set a BOLD Pipeline and it seems to be very specific with the filename…

Not sure why dcm2bids lets you set entities in invalid orders, but the perl rename command is extremely useful for things like this.

rename 's/acq-MoCoHR_task-AIR/task-AIR_acq-MoCoHR/' **/*_acq-MoCoHR_*

See this StackOverflow for how to install it on different Unix-like OSs if you don’t already have it. I believe it’s installed by default on OSX. If you’re on Windows, I’m not sure what the best approach is. I’d probably try using WSL.

Dcm2bids should reorganize labels after the fact if you set the order incorrectly.

I guess the validator should be the arbiter here. In any case, task should come before acq.

Yeah, you are right. I just need to change the order of the echo and part (real/imaginary).
something like this:

sub-p01_echo-01_imaginary_task-AIR_acq-fullres_T2star

thank you in advance!!

I don’t think we can really work with “something like”. What filenames do you currently have, and what filenames do you need for your tool?

If you need to go from BIDS names to non-BIDS names, then it would probably make sense to copy your data into the form needed for your tool, rather than rename things in-place.

I have

sub-p01_task-AIR_acq-fullres_echo-01_imaginary_T2star

I need

sub-p01_echo-01_imaginary_task-AIR_acq-fullres_T2star

Thank you…

I would also encourage you to explore tools like ChatGPT to write scripts like this (LINK TO PROMPT). Here is one I just generated (since this has not been tested, I encourage you to test this on a COPY of your data so you do not make any irreversible mistakes).

#!/bin/bash

# Define the directory where BIDS subject folders are located
bids_dir="/path/to/BIDS/directory"

# Define the pattern to match files
pattern="_task-AIR_acq-fullres_echo-*-imaginary_T2star"

# Find files matching the pattern recursively within BIDS subject folders
find "$bids_dir" -type f \( -name "*$pattern.nii.gz" -o -name "*$pattern.json" \) | while IFS= read -r file; do
    if [[ -f $file ]]; then
        # Extract subject name and echo number from the file path
        subject=$(echo "$file" | grep -o 'sub-[^/]*' | head -1)
        echo_number=$(echo "$file" | grep -o 'echo-[0-9]*' | head -1)

        # Determine the file extension
        extension="${file##*.}"

        # Determine the new file name
        new_name="${file/$subject/sub-p01}"
        new_name="${new_name/$echo_number/echo-01_imaginary}"
        new_name="${new_name/_task-AIR_acq-fullres/}"
        new_name="${new_name/-imaginary_T2star/sub-p01_echo-01_imaginary_task-AIR_acq-fullres_T2star}"

        # Append the correct file extension
        new_name="${new_name%.*}.$extension"

        # Perform the rename
        mv "$file" "$new_name"
        echo "Renamed $file to $new_name"
    fi
done

Okay, so the way that rename works is that you have s/before/after/. So just copy the bits that you want to change into the before part and what you want them to say to the after part:

rename 's/task-AIR_acq-fullres_echo-01_imaginary/echo-01_imaginary_task-AIR_acq-fullres/' **/*

If you have patterns you want to do, you can use parentheses groups and references, like:

rename 's/(task-.*)_(echo-.._imaginary)/${2}_${1}/' **/*

Anyway, this is getting into regular expressions, so if you’re not comfortable with those, you’ll want to balance writing out more than you might need to and reading up on perlre - Perl regular expressions - Perldoc Browser.

We’re pretty firmly outside the realm of BIDS, at this point, as neither the input nor the output is BIDS. This is just basic file renaming.

Also you probably wanna tell the developers that if they may want to be BIDS compliant, they probably should change the expected inputs in terms of filenames.

1 Like

Here’s another possibility. Bidscoin has a nibabel2bids plugin that will simply take nifti’s as sourcedata and nifti’s as target data. Bidscoin will read all the source filenames and json sidecars, present the (shortlist of) datatypes (aka bidsmappings) in an editor for you to easily edit to your needs. Then bidscoin can automatically convert (i.e. rename) your datset, keeping everything BIDS compliant (scan.tsv file, IntendedFor values, etc)

1 Like

If more people are interested, I can even make a dedicated bids2bids template bidsmap, to make this process really smooth and easy

1 Like

Thank you all so much !

Sure thing! Thanks a lot. I remain attentive… I would need to see your tutorial for it :slight_smile:

I just made a new template, which will become available in the upcoming BIDScoin release. In the meantime, you can try it out by installing the development version, e.g. like this:

  1. It’s advised to install BIDScoin in a Python virtual environment, such as made by conda or venv
$ pip install --force-reinstall git+https://github.com/Donders-Institute/bidscoin
  1. Run the bidsmapper and use the automatically launched GUI to edit the output names to your liking (see screenshots below):
$ bidsmapper bids_inputfolder bids_outputfolder -p nibabel2bids -t bidsmap_bids2bids
  1. Then run the bidscoiner to convert (=rename) the data like this (the bidsmapper doesn’t change any data, it just does data discovery):
$ bidscoiner bids_inputfolder bids_outputfolder

Here’s a screenshot of the main window that you will (hopefully) get, showing all the run types in your bids input folder + the intended output:

And here is how it looks when you edit individual run types:

p.s. 1) This should work out of the box for all standard nifti BIDS data + nifti data in the extra_data folder (next to anat, func, etc). Editing other data would probably require more tweaking (consult me if needed).
2) In the bidseditor, besides renaming output files, you can change anything you like, such as B0Field tags, metadata for the sidecars, zipping data, etc

Hi Daniela,

Can you tell us which software asks your data to be named like this ?

Thank you
Arnaud