Batch processing Bio-format and Merging Channels

Hi all,

I am pretty new with Fiji and LIF files so maybe someone out there might be of help.

I am trying to make a script to batch process lif files (with series, channels and z, no timestamp, 8bit), export the channels of all the series individually and finally merge 2 (out of 4) channels of each series into a composite.

My problem so far is that my script only saves one channel (C=3)

Here’s the 1st part of script.

path = File.openDialog("Select a File");
output = getDirectory("Select directory");

run("Bio-Formats Macro Extensions");
Ext.setId(path);
Ext.getCurrentFile(file);
Ext.getSeriesCount(seriesCount);

for (s=1; s<=seriesCount; s++) {
run("Bio-Formats Importer", "open=&path autoscale color_mode=Grayscale split_channels view=Hyperstack stack_order=XYCZT series_"+s);
//  the images come up but not all are in Grayscale for some reason
close();

// Exporting in TIFF in selected folder
for (i=0;i<nImages;i++) 
        selectImage(i+1);
        run("8-bit");
        title = getTitle;
        print(title);
        print(nImages)
        saveAs("tiff", output+title);
 
run("Close All");
  } 

Now the second part, for the composite image, where i need to merge channels C=0 and C=3, is a little dirtier. Gives me an error at the substring, probably due to wrong use of LastIndexOf. (I try to insert it to the for loop):

title=getTitle();
s=lastIndexOf(title, " - C=3");
s2=lastIndexOf(title, " - C=0");
subtitle=substring(title, 0, s);
subtitle2=substring(title, 0, s2);
run("Merge Channels...", "c2=["+ subtitle + " - C=3] c1=[" + subtitle2 + " - C=0] create"); 
run("8-bit");
run("Stack to RGB");
saveAs("tiff", output+title);

Thanks

Hi @PhiloVa,

For the first part you just need to add brackets around the for loop:

for (i=0;i<nImages;i++) {
        selectImage(i+1);
        run("8-bit");
        title = getTitle;
        print(title);
        print(nImages)
        saveAs("tiff", output+title);
}

For the second part, the subtitle should be the same for both so you only really need to replace the channel index:

title=getTitle();
s=lastIndexOf(title, " - C=");
subtitle=substring(title, 0, s);
run("Merge Channels...", "c2=["+ subtitle + " - C=3] c1=[" + subtitle + " - C=0] create"); 
run("8-bit");
run("Stack to RGB");
saveAs("tiff", output+title);
1 Like

Hey @dgault , thx for answering…

Well that first part was a definite beginners error. I noticed that it the script ran better closing brackets on the first for loop too before starting the second, as it didn’t give time to Fiji to load all the images before applying changes and saving.
However, does this bracket closing affect in any way the variable output = getDirectory at the start? I do get an error that output is not defined at the saveAs command. My only workaround so far is to bring the variable down some lines, but that won’t be very practical in long runs. Any ideas?

path = File.openDialog("Select a File");
//output = getDirectory("Select directory");

run("Bio-Formats Macro Extensions");
Ext.setId(path);
Ext.getCurrentFile(file);
Ext.getSeriesCount(seriesCount);

for (s=1; s<=seriesCount; s++) {
run("Bio-Formats Importer", "open=&path autoscale color_mode=Default split_channels view=Hyperstack stack_order=XYCZT series_"+s);
}

output = getDirectory("Select directory");

for (i=0;i<nImages;i++) {
        selectImage(i+1);
        run("8-bit");
        title = getTitle;
        print(nImages);
        saveAs("tiff", output+title);
}

For the second part, you are right, much clearer that way but I still get an error although the file exists and is the right channel:

Error: “EXP06_jan2019_DAPI_Phalloidin_DNMT1_ChAT4B1_tech1.lif - Y29_CTR_Brain 2 MB4 - C=3.tif” is not a valid choice for “c2” in line 4:

  run ( "Merge Channels..." , "c2=[" + subtitle + " - C=3.tif] c3=[" + subtitle + " - C=0.tif] create" <)> ; 

Maybe it is the way it looks for the files? I now have about 100 files and I only need to merge those with a same filename but ending with C=3 and C=0.
Code looks like this now:

for (i=0;i<nImages;i++) {
        selectImage(i+1);
        title=getTitle();
		s=lastIndexOf(title, " - C=");
		subtitle=substring(title, 0, s);
		run("Merge Channels...", "c2=["+ subtitle + " - C=3.tif] c3=[" + subtitle + " - C=0.tif] create"); 
		run("8-bit");
		run("Stack to RGB", "slices keep");
		saveAs("tiff", output+subtitle+comp);
}

Thx for your time!

The brackets should impact the output variable at all so that seems very odd. I tried running the macro exactly as in your post but using the original output placement and it ran without any issues so that is rather confusing.

I think for the second part if you are merging the channels before saving them then you wont need the tif extension, so it would be:
run("Merge Channels...", "c2=["+ subtitle + " - C=3] c3=[" + subtitle + " - C=0] create");

You’re right, I tried again and everything runs smooth. For some reason Merge Channels did need the .tif extension otherwise would not run.

I put the script here in case anyone needs something similar.

Thanks again!!!

// This macro will open all series of a lif file, with all channels. Turn them 8-Bit, save in Tiff format.
// It will also Merge Channels C=0 and C=3 in Blue and Green respectively. 

path = File.openDialog("Select a File");
output = getDirectory("Select directory");

run("Bio-Formats Macro Extensions");
Ext.setId(path);
Ext.getCurrentFile(file);
Ext.getSeriesCount(seriesCount);

for (s=1; s<=seriesCount; s++) {
run("Bio-Formats Importer", "open=&path autoscale color_mode=Default split_channels view=Hyperstack stack_order=XYCZT series_"+s);
}

setBatchMode(true); 
for (i=0;i<nImages;i++) {
        selectImage(i+1);
        run("8-bit");
        title = getTitle;
        print(title);
        print(nImages);
        saveAs("tiff", output+title);
}

for (i=0;i<nImages;i++) {
        selectImage(i+1);
        title=getTitle();
		s=lastIndexOf(title, " - C=");
		subtitle=substring(title, 0, s);
		run("Merge Channels...", "c2=["+ subtitle + " - C=3.tif] c3=[" + subtitle + " - C=0.tif] create keep"); 
		run("Stack to RGB", "slices keep");
		run("8-bit");
		saveAs("tiff", output+subtitle+"_comp.tif");
		close();
		close("Composite");
		close("Composite-1");
}

setBatchMode(false); 
print(nImages/4);
run("Close All");