LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Running similar analysis in different subdirectories in loop (https://www.linuxquestions.org/questions/linux-newbie-8/running-similar-analysis-in-different-subdirectories-in-loop-4175618161/)

imaging_2018 11-22-2017 09:46 AM

Running similar analysis in different subdirectories in loop
 
Hi,

I have a folder with multiple sub-folders and files. They are all similar in name but the only difference is the patient's number

Example

Analysis/Patient_01/Brain/image_patient_01_merged.nii
Analysis/Patient_02/Brain/image_patient_02_merged.nii
Analysis/Patient_03/Brain/image_patient_03_merged.nii
.
.
.
and so on.

In the main folder (Analysis) I want to make a script to do the same thing for each .nii file in a loop. The issue is that there are many other .nii files in each subdirectory but I don't want to use them. Only the one that has the name image_patient_id_merged.nii


example

fslstats image_patient_01_merged.nii -M
fslstats image_patient_02_merged.nii -M
fslstats image_patient_03_merged.nii -M
.
.
.
.
In other words, I need to do the same analysis on the nii files in each subdirectory.

Thanks in advance,

TenTenths 11-22-2017 10:37 AM

So what have you tried so far?

joe_2000 11-22-2017 10:52 AM

Something like (untested)
Code:

find Analysis -type f -name "image_patient_*_merged.nii -exec fslstats {} -M \;
should do the job.

Note that this would also match files named e.g. image_patient_some_random_string_merged.nii

If you have false positives here you may want to refine the regex a little bit. E.g.

Code:

find Analysis -type f -name "image_patient_[0-9][0-9]_merged.nii -exec fslstats {} -M \;
Before running the actual command you can test the matches without the -exec stuff to only print the results
Code:

find Analysis -type f -name "image_patient_*_merged.nii"

imaging_2018 11-22-2017 11:47 AM

Thanks Guys,

I am very new to scripting. I have a .sh script that contains all my functions but I have to keep copying it inside each patient subfolder to perform my analysis.

Let me explain abit more. I have 12 patients till now and each of them has a directory named Brain. Inside Brain there are many .nii files (NIFTI files for medical images).

I want to use my script to run through all Patients folders and subfolders such that it finds the " image_patient_01_merged.nii" file and run the "fslstats image_patient_01_merged.nii -M" command, and then it repeats the same thing for all other patients.


I would be very grateful if I can get some help on this thing.

Regards and many thanks

joe_2000 11-22-2017 12:37 PM

Have you tried the commands I suggested?

imaging_2018 11-22-2017 02:20 PM

Thanks Joe,

I can see it looping through all the files required. However, I can not find where it saves the results.

joe_2000 11-22-2017 03:05 PM

That would depend on the behavior of fslstats

giis 11-24-2017 11:07 PM

As you mentioned you are new to scripting. I highly recommend you to check out find command http://www.softpanorama.org/Tools/Fi..._in_find.shtml tutorials. Its powerful and flexible command, will be helpful in long term.

imaging_2018 11-25-2017 12:58 AM

I have tried another command but still can not find where it saves the output files.

Is there anyway to define the output directory?

Also, can I define the name of the output file?

Example:

I want to save the output in Analysis/Brain/Patient_IDnumber (i.e IDnumber is the number of patient, which should be changing while executing the script).

The name of the output is the same as the input but with extra letters. Example (input Image_patient_IDnumber.nii and the output as Image_patient_IDnumber_new.nii )

The idea is that I need to keep the original files as well and not overwriting them.

Thanks in advance

joe_2000 11-25-2017 02:17 AM

Again. The output file depends on what fslstats does. Where does it store the output when you run it directly without any looping constructs?

imaging_2018 11-25-2017 02:34 AM

Thanks alot.

It should save it the current working directory. I do a lot of complicated image processing on daily basis but what I am asking about is how to use the script thing to automate my work.

I have hundreds of files to be analyzed. simply, I want soe help with a code that reads the file, execute the command and saves with similar name with extra letters to not overwrite the original one.

To do so,, I have made the same tree for each patient folder.

Analysis> patient name_along with its ID > Brain> image_file_along with patient ID.

joe_2000 11-25-2017 03:19 AM

I understood your usecase allright. The part I know nothing about is fslstats. Is that something you built yourself?

If yes, simply put the logic for output files in there. If not, check its documentation. Maybe it has a command line option to specify the output file. Maybe you can even have it write to stdout, then you could solve the problem with redirection in the find command I posted earlier.

If it really must write to the current working directory then the output would end up where you issued the find command. Which means the behavior you want will be difficult to achieve with a one-liner...

Last not least: As TenTens indicated initially: You should show some effort for yourself. Don't expect people to write scripts for you. It's ok to be stuck and post broken code (ideally using code tags). At least give it a try - that makes it easier for people to help...

allend 11-25-2017 05:13 PM

I have deja vu on this question. https://www.linuxquestions.org/quest...ry-4175581072/


All times are GMT -5. The time now is 05:26 PM.