Finding files based on a substring

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Finding files based on a substring

ChrisW
Hi,

Apologies if this is a dumb question, I am completely new to programming,
but I am enjoying what I have learnt so far.

I have several thousand images that are either background or stained images.
I am trying to write a batch macro that will match the stained image with
the correct background image. Ultimately, I want to subtract the background
from the stain.
I thought this could be done by defining a substring of the file name that
is shared between the background and stained images, and then opening all
the images that match with file names that match the substring (which should
only be two. For example, all the file names follow the same format:


 [marker name or background(bg)]_[fluorochrome]_[timepoint]_[position].tiff

I have worked out how to define the substring I want, using:

inputFolder=getDirectory("Choose input folder");
bglist=getFileList(inputFolder);

for (i=0; i<bglist.length; i++) {
        if(startsWith(bglist[i],"bg")) { // I use the background images to define
the string as they all start with bg.
                str = bglist[i];
                sx = indexOf(str, "_");
                dotx= indexOf(str, ".");
                sub = substring(str, sx, dotx);


However, I have no idea how to then open files based on the substring.
Everything I have tried just returns the first background image. Any help
would be greatly appreciated!

Many thanks,
ChrisW




--
Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Finding files based on a substring

Michael Schmid-3
Hi Chris,

probably it would be easier to start with the non-background images and
then create the name of the corresponding background image out of it.
Roughly like this:

if(!startsWith(list[i],"bg")) {
   mainImageName = list[i];
   sx = indexOf(mainImageName, "_");
   sub = substring(mainImageName, sx);
   bgImageName = "bg"+sub;


Michael
________________________________________________________________
On 29/06/2018 14:00, ChrisW wrote:

> Hi,
>
> Apologies if this is a dumb question, I am completely new to programming,
> but I am enjoying what I have learnt so far.
>
> I have several thousand images that are either background or stained images.
> I am trying to write a batch macro that will match the stained image with
> the correct background image. Ultimately, I want to subtract the background
> from the stain.
> I thought this could be done by defining a substring of the file name that
> is shared between the background and stained images, and then opening all
> the images that match with file names that match the substring (which should
> only be two. For example, all the file names follow the same format:
>
>
>   [marker name or background(bg)]_[fluorochrome]_[timepoint]_[position].tiff
>
> I have worked out how to define the substring I want, using:
>
> inputFolder=getDirectory("Choose input folder");
> bglist=getFileList(inputFolder);
>
> for (i=0; i<bglist.length; i++) {
> if(startsWith(bglist[i],"bg")) { // I use the background images to define
> the string as they all start with bg.
> str = bglist[i];
> sx = indexOf(str, "_");
> dotx= indexOf(str, ".");
> sub = substring(str, sx, dotx);
>
>
> However, I have no idea how to then open files based on the substring.
> Everything I have tried just returns the first background image. Any help
> would be greatly appreciated!
>
> Many thanks,
> ChrisW
>
>
>
>
> --
> Sent from: http://imagej.1557.x6.nabble.com/
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Finding files based on a substring

Stoyan Pavlov
Hi Chris,
Another approach is (given your background images match in number and name
your stained images) to separate them both in two folders and use a batch
to subtract corresponding files from both folders,like this:

---

//subtract dir1 from dir2
inputDir1 = getDirectory("Choose minuend image directory! ");
inputDir2 = getDirectory("Choose subtrahend image directory! ");
outputDir = getDirectory("Choose Output image directory! ");
fileList1 = getFileList(inputDir1);
fileList2 = getFileList(inputDir2);
if (fileList1.length != fileList2.length) {
  exit("Input directories must have same number of files.");
}
setBatchMode(true);
for (i = 0; i < fileList1.length; i++) {
  file1 = fileList1[i];
  file2 = fileList2[i];
  open(inputDir1+file1);
  id1 = getImageID();
  open(inputDir2+file2);
  id2 = getImageID();
  imageCalculator("Subtract create ", id1, id2);
  outName ="Top-Hat_"+file2;
  saveAs("Tiff", outputDir + "/" + outName);
close();
close(file1);
close(file2);
}
print("Finished");

---

Please note that for this to work the Ith file in folder 1 must match the
Ith file in folder 2. It is however relatively easier to batch rename the
files to follow this policy instead of recursively searching through one
folder for matching files inside an ImageJ macro.

Best wishes!
Stoyan

---
Assoc.Prof. Stoyan P. Pavlov, MD, PhD
Departament of Anatomy, Histology and Embryology
Medical University "Prof. Dr. Paraskev Stoyanov", Varna
Prof. Marin Drinov Str.55
9002 Varna
 Bulgaria
Tel: +359 (0) 52 - 677 - 052
e-mail: [hidden email]
           [hidden email]

2018-06-29 18:30 GMT+03:00 Michael Schmid <[hidden email]>:

> Hi Chris,
>
> probably it would be easier to start with the non-background images and
> then create the name of the corresponding background image out of it.
> Roughly like this:
>
> if(!startsWith(list[i],"bg")) {
>   mainImageName = list[i];
>   sx = indexOf(mainImageName, "_");
>   sub = substring(mainImageName, sx);
>   bgImageName = "bg"+sub;
>
>
> Michael
> ________________________________________________________________
>
> On 29/06/2018 14:00, ChrisW wrote:
>
>> Hi,
>>
>> Apologies if this is a dumb question, I am completely new to programming,
>> but I am enjoying what I have learnt so far.
>>
>> I have several thousand images that are either background or stained
>> images.
>> I am trying to write a batch macro that will match the stained image with
>> the correct background image. Ultimately, I want to subtract the
>> background
>> from the stain.
>> I thought this could be done by defining a substring of the file name that
>> is shared between the background and stained images, and then opening all
>> the images that match with file names that match the substring (which
>> should
>> only be two. For example, all the file names follow the same format:
>>
>>
>>   [marker name or background(bg)]_[fluorochrome]
>> _[timepoint]_[position].tiff
>>
>> I have worked out how to define the substring I want, using:
>>
>> inputFolder=getDirectory("Choose input folder");
>> bglist=getFileList(inputFolder);
>>
>> for (i=0; i<bglist.length; i++) {
>>         if(startsWith(bglist[i],"bg")) { // I use the background images
>> to define
>> the string as they all start with bg.
>>                 str = bglist[i];
>>                 sx = indexOf(str, "_");
>>                 dotx= indexOf(str, ".");
>>                 sub = substring(str, sx, dotx);
>>
>>
>> However, I have no idea how to then open files based on the substring.
>> Everything I have tried just returns the first background image. Any help
>> would be greatly appreciated!
>>
>> Many thanks,
>> ChrisW
>>
>>
>>
>>
>> --
>> Sent from: http://imagej.1557.x6.nabble.com/
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html