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”
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.
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 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:
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.
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)
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:
It’s advised to install BIDScoin in a Python virtual environment, such as made by conda or venv
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