I am trying to match MRI files with their volume files.


Link to files: https://wiki.cancerimagingarchive.net/pages/viewpage.action?pageId=68550661#68550661171ba531fc374829b21d3647e95f532c

The MRI files have the file path:

./manifest-1599764098812/Prostate-MRI-US-Biopsy/Prostate-MRI-US-Biopsy-0001/06-28-2009-NA-MRI PROSTATE W WO CONTRAST-51743/11.000000-t2spcrstaxial oblProstate-90221/1-01.dcm

The volume files have the file path with a long unique ID:


I have a list (mriIdList.txt) which contains all of the ID files so that patient 1's first MRI is on the first line and so on. One patient can have multiple MRIs and each MRI has its own ID. Also I want to label the parent directory of the .dcm files since I will be processing the file folder. The numbers in the MRI file names are meaningless. Note the spacing in the file names.

Attempted Solution:

find . -name "*{t2}*"
xargs -a ./mriIdList.txt -I {} -d'\n'
sed  's/.$1/.$1$2/'

This is my first script, so I'm sure it's messed up somewhere else, but my problem is that "find" goes through the directories in a computer-memory optimized way. I need to find MRI files going from left to right. (ie from Prostate-MRI-US-Biopsy-0001 to Prostate-MRI-US-Biopsy-0002). My general idea is to find directories labeled "t2", run through the ID list, and attach the ID to that directory. After that, I would use the ID to match up the MRIs with their volumes.


I created an example directory here: https://drive.google.com/drive/folders/17Gcl-IOjFzHKnIKEHVllLFDhcdR35E5c?usp=sharing

The first patient (Prostate-MRI-US-Biopsy-0001) has 1 MRI. The file path should changed to something like

/Prostate-MRI-US-Biopsy/Prostate-MRI-US-Biopsy-0001/06-28-2009-NA-MRI PROSTATE W WO CONTRAST-51743/11.000000-t2spcrstaxial oblProstate-90221_1.

The second patient has no MRIs, so he should be skipped.

The third patient has 3 MRIs. The first MRI should be

Prostate-MRI-US-Biopsy/Prostate-MRI-US-Biopsy-0003/05-01-2006-NA-MRI PROSTATE W WO CONTRAST-62671/3.000000-t2spcrstaxialp2-33258_1.

The second:

Prostate-MRI-US-Biopsy/Prostate-MRI-US-Biopsy-0003/09-21-2008-NA-MRI PROSTATE W WO CONTRAST-86577/9.000000-t2spcrstaxial oblProstate-31608_1.

The third:

Prostate-MRI-US-Biopsy/Prostate-MRI-US-Biopsy-0003/10-17-2009-NA-MRI PROSTATE W WO CONTRAST-68997/9.000000-t2spcrstaxial oblProstate-97825_1.

After that, I could pass in the MRI and STL files into a python script to extract image features since they have the same ID. It would be 1 matching MRI file and 1 STL file at a time. I'll take any and all advice on that operation too.

Any help or suggestions would be greatly appreciated.

  • Please edit your question and include a full, reproducible example we can use to test our solutions. We would need a few example file names, as you have provided, but also the corresponding lines from ./mriIdList.txt. Everything will depend on how that file is formatted. Make sure the example lines include all possible cases (e.g. one file per patient, multiple different files per patient and anything else the script will need to be able to handle).
    – terdon
    Oct 13 at 10:04
  • Thank you for the help! I added an example. The text file is from an excel column, so I am flexible with the format. Oct 13 at 12:00

I didn't bother with renaming because I have a 1:1 match to the ID and can get to the volume files now, but this worked!

From MRI file parent directory:

tree -dfi > dir.txt;
grep "t2" dir.txt > mriFiles.txt

Tree kept the order of the directories. Grep found the directories of interest.

  • Yay! I'm glad you found a solution and thank you for taking the time to post it!
    – terdon
    Oct 14 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.