LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-22-2017, 09:46 AM   #1
imaging_2018
LQ Newbie
 
Registered: Nov 2017
Posts: 5

Rep: Reputation: Disabled
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,
 
Old 11-22-2017, 10:37 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,185

Rep: Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377
So what have you tried so far?
 
Old 11-22-2017, 10:52 AM   #3
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
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"
 
Old 11-22-2017, 11:47 AM   #4
imaging_2018
LQ Newbie
 
Registered: Nov 2017
Posts: 5

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-22-2017, 12:37 PM   #5
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Have you tried the commands I suggested?
 
Old 11-22-2017, 02:20 PM   #6
imaging_2018
LQ Newbie
 
Registered: Nov 2017
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks Joe,

I can see it looping through all the files required. However, I can not find where it saves the results.
 
Old 11-22-2017, 03:05 PM   #7
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
That would depend on the behavior of fslstats
 
Old 11-24-2017, 11:07 PM   #8
giis
Member
 
Registered: Nov 2013
Location: Third Rock from Moon
Distribution: RPM/DEB based and LFS
Posts: 73

Rep: Reputation: Disabled
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.
 
Old 11-25-2017, 12:58 AM   #9
imaging_2018
LQ Newbie
 
Registered: Nov 2017
Posts: 5

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-25-2017, 02:17 AM   #10
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Again. The output file depends on what fslstats does. Where does it store the output when you run it directly without any looping constructs?
 
Old 11-25-2017, 02:34 AM   #11
imaging_2018
LQ Newbie
 
Registered: Nov 2017
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.
 
Old 11-25-2017, 03:19 AM   #12
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
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...

Last edited by joe_2000; 11-25-2017 at 03:20 AM. Reason: typo
 
Old 11-25-2017, 05:13 PM   #13
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,635

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
2 similar tcl files, but 1 is running properly while other shows error.please explain Niraj Mishra Linux - Newbie 1 11-24-2014 10:56 AM
Nested while loop - similar strings tlegend33 Linux - Newbie 4 09-17-2014 06:04 PM
[SOLVED] Looking for good loop-based audio-program similar to Windows' Acid Pro kkoistinen Linux - Software 1 07-17-2012 07:07 AM
Problem running bash script recursively over a number of subdirectories using osx ghadley_00 Linux - Software 2 09-06-2011 10:38 AM
bash: two very similar errors, one ends a while loop, the other doesn't openSauce Programming 2 12-02-2009 09:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration