Skip to content

Instantly share code, notes, and snippets.

@lacan
Last active February 24, 2024 12:27
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lacan/16e12482b52f539795e49cb2122060cc to your computer and use it in GitHub Desktop.
Save lacan/16e12482b52f539795e49cb2122060cc to your computer and use it in GitHub Desktop.
Extract multi-series file and resave as TIFF stacks or slices #Fiji #ImageJ #Macro #BIOP
/*
* Complex Format EXPORT MACRO
* By Olivier Burri @ EPFL - SV - PTECH - BIOP
* Given a folder, extracts all series inside all multi-file files with given extension in new folders
* Last edit: 13.02.2017
*/
////////////////////// SET PARAMETERS //////////////////////
////////////////////////////////////////////////////////////
// Set the extension you would like this macro to work with.
// Do not add a . at the beggining
extension = "czi"; //eg "lif", "vsi", etc...
// Set to true if you want all planes of the image to be saved individually
// if set to false, it will save each series as a stack.
is_save_individual_planes = true; // set to either true or false
// Padding for the naming of the series if you want to save all
// Images individually
pad = 3; // 0 means no padding. 2 means '01', '02' etc...
//////////////////// END SET PARAMETERS ////////////////////
////////////////////////////////////////////////////////////
// Beggining of macro. You should now have anything to edit after this line.
dir = getDirectory("Select a directory containing one or several ."+extension+" files.");
files = getFileList(dir);
setBatchMode(true);
k=0;
n=0;
run("Bio-Formats Macro Extensions");
for(f=0; f<files.length; f++) {
if(endsWith(files[f], "."+extension)) {
k++;
id = dir+files[f];
Ext.setId(id);
Ext.getSeriesCount(seriesCount);
print(seriesCount+" series in "+id);
n+=seriesCount;
for (i=0; i<seriesCount; i++) {
run("Bio-Formats Importer", "open=["+id+"] color_mode=Default view=Hyperstack stack_order=XYCZT series_"+(i+1));
fullName = getTitle();
dirName = substring(fullName, 0,lastIndexOf(fullName, "."+extension));
fileName = substring(fullName, lastIndexOf(fullName, " - ")+3, lengthOf(fullName));
File.makeDirectory(dir+File.separator+dirName+File.separator);
print("Saving "+fileName+" under "+dir+File.separator+dirName);
getDimensions(x,y,c,z,t);
if(is_save_individual_planes) {
save_string = getSaveString(pad);
print(dir+File.separator+dirName+File.separator+fileName+"_"+save_string+".tif");
run("Image Sequence... ", "format=TIFF name=["+fileName+"] digits="+pad+" save=["+dir+File.separator+dirName+File.separator+"]");
} else {
saveAs("tiff", dir+File.separator+dirName+File.separator+fileName+"_"+(i+1)+".tif");
}
run("Close All");
}
}
}
Ext.close();
setBatchMode(false);
showMessage("Done with "+k+" files and "+n+" series!");
function getSaveString(pad) {
str ="";
getDimensions(x,y,c,z,t);
if(t > 1) str+="t_"+IJ.pad(1,pad);
if(z > 1) str+="z_"+IJ.pad(1,pad);
if(c > 1) str+="c_"+IJ.pad(1,pad);
return str;
}
@VVSBN
Copy link

VVSBN commented Feb 1, 2023

Hi,
thanks for the code! I wanted to ask if there is a way to specify exactly which TIFFs to combine?
Eg, I have the following set: _

A01_T001_L01_C01
A01_T002_L01_C01
A01_T003_L01_C01
A01_T001_L02_C01
A01_T002_L02_C01
A01_T003_L02_C01
A01_T001_L01_C02
A01_T002_L01_C02
A01_T003_L01_C02
A01_T001_L02_C02
A01_T002_L02_C02
A01_T003_L02_C02

I want him to combine the tiffs from the same well, the same location and a single channel into one superstack (so only combining the timeline into one stack, if the other parameters such as well, location or channel change, I want him to make a new superstack).

Thanks! :)

@lacan
Copy link
Author

lacan commented Feb 1, 2023

This is the opposite of what this script does. Thes script creates many files from a single multi-series file.

You can ask your question on the https://forum.image.sc website, where myself or other people can answer.

The simplest would be to use File > Import > Image Sequence...

And use some parameters like this for the regex : (A01_T\d{3}_L01_C01)
image

@af21009
Copy link

af21009 commented Aug 7, 2023

Hi,

As a beginner to ImageJ macros, your script has been very helpful for creating TIFFs from a multi-series file, thank you!

However, I have had a slight issue with my LIF - it has 20 series, and when the macro runs it only works for the first 11 series - after that an error occurs:

beginIndex>endIndex

for the line - dirName = substring ( fullName , 0 , lastIndexOf ( fullName , "." + extension ) <)> ;

Do you have any suggestions for how I could fix this? Thank you

@lacan
Copy link
Author

lacan commented Aug 8, 2023

I would surmise that after series 11, the name of your files changes? The script assumes that there is a' .lif' at the end of every series name, which is what BioFormats usually does.

Can you provide the output of the following code?

run("Bio-Formats Macro Extensions");

id = File.openDialog("Problematic LIF File");
Ext.setId(id);
Ext.getSeriesCount(seriesCount);
print(seriesCount+" series in "+id);
n+=seriesCount;
for (i=0; i<seriesCount; i++) {
	run("Bio-Formats Importer", "open=["+id+"] color_mode=Default view=Hyperstack stack_order=XYCZT series_"+(i+1));
	fullName	= getTitle();
	print("Name: "+fullName);
}

@af21009
Copy link

af21009 commented Aug 8, 2023

Hi,

Thank you for your quick reply! I've just ran the code using the lif as the input, however this error code came up -

Error: Unrecognized Ext function in line 2:

	Ext . <setId> ( id ) ; 

@lacan
Copy link
Author

lacan commented Aug 8, 2023

Argh typo on my part, please try the updated code from the original reply.

@af21009
Copy link

af21009 commented Aug 9, 2023

Worked this time, thanks!

This was the output:

20 series in C:\Users\af21009\Downloads\MCF10A Starve baf stained 11th july 2023, imaged 18th july 2023.lif
Name: MCF10A Starve baf stained 11th july 2023, imaged 18th july 2023.lif - MCF10A control DAPI, 488 LAMP2, 568 LC3B, 647 P62
Name: MCF10A Starve baf stained 11th july 2023, imaged 18th july 2023.lif - MCF10A control DAPI, 488 LAMP2, 568 LC3B, 647 P62 2
Name: MCF10A Starve baf stained 11th july 2023, imaged 18th july 2023.lif - MCF10A control DAPI, 488 LAMP2, 568 LC3B, 647 P62 3
Name: MCF10A Starve baf stained 11th july 2023, imaged 18th july 2023.lif - MCF10A control DAPI, 488 LAMP2, 568 LC3B, 647 P62 4
Name: MCF10A Starve baf stained 11th july 2023, imaged 18th july 2023.lif - MCF10A control DAPI, 488 LAMP2, 568 LC3B, 647 P62 5
Name: MCF10A Starve baf stained 11th july 2023, imaged 18th july 2023.lif - MCF10A baf 50nm 2 hr DAPI, 488 LAMP2, 568 LC3B, 647 P62
Name: MCF10A Starve baf stained 11th july 2023, imaged 18th july 2023.lif - MCF10A baf 50nm 2 hr DAPI, 488 LAMP2, 568 LC3B, 647 P62 2
Name: MCF10A Starve baf stained 11th july 2023, imaged 18th july 2023.lif - MCF10A baf 50nm 2 hr DAPI, 488 LAMP2, 568 LC3B, 647 P62 3
Name: MCF10A Starve baf stained 11th july 2023, imaged 18th july 2023.lif - MCF10A baf 50nm 2 hr DAPI, 488 LAMP2, 568 LC3B, 647 P62 4
Name: MCF10A Starve baf stained 11th july 2023, imaged 18th july 2023.lif - MCF10A baf 50nm 2 hr DAPI, 488 LAMP2, 568 LC3B, 647 P62 5
Name: MCF10A Starve baf stained 11th july 2023, imaged 18th july 2023.lif - MCF10A starve HBSS 2 hr DAPI, 488 LAMP2, 568 LC3B, 647 P62
Name: MCF10A Starve baf stained 11th july 2023, imaged 18th july 202...- MCF10A starve HBSS 2 hr DAPI, 488 LAMP2, 568 LC3B, 647 P62 2
Name: MCF10A Starve baf stained 11th july 2023, imaged 18th july 202...- MCF10A starve HBSS 2 hr DAPI, 488 LAMP2, 568 LC3B, 647 P62 3
Name: MCF10A Starve baf stained 11th july 2023, imaged 18th july 202...- MCF10A starve HBSS 2 hr DAPI, 488 LAMP2, 568 LC3B, 647 P62 4
Name: MCF10A Starve baf stained 11th july 2023, imaged 18th july 202...- MCF10A starve HBSS 2 hr DAPI, 488 LAMP2, 568 LC3B, 647 P62 5
Name: MCF10A Starve baf stained 11th july 2023, imaged 18th july 202...arve HBSS AND BAF 50NM 2 hr DAPI, 488 LAMP2, 568 LC3B, 647 P62
Name: MCF10A Starve baf stained 11th july 2023, imaged 18th july 202...ve HBSS AND BAF 50NM 2 hr DAPI, 488 LAMP2, 568 LC3B, 647 P62 2
Name: MCF10A Starve baf stained 11th july 2023, imaged 18th july 202...ve HBSS AND BAF 50NM 2 hr DAPI, 488 LAMP2, 568 LC3B, 647 P62 3
Name: MCF10A Starve baf stained 11th july 2023, imaged 18th july 202...ve HBSS AND BAF 50NM 2 hr DAPI, 488 LAMP2, 568 LC3B, 647 P62 4
Name: MCF10A Starve baf stained 11th july 2023, imaged 18th july 202...ve HBSS AND BAF 50NM 2 hr DAPI, 488 LAMP2, 568 LC3B, 647 P62 5

@JennyLuong
Copy link

Is it possible to do split channels with this script? I tried added that option to the script but it messed up the file names.

@cfausto
Copy link

cfausto commented Feb 22, 2024

Hello!

Thank you so much for making this script! It has helped me tremendously in processing large datasets. I recently updated FIJI to the newest version, and am now getting an error. I was wondering if you can help me address this?

Specifically in line 54:
dirName = substring(fullName, 0,lastIndexOf(fullName, "."+extension));

I am now getting this error message attached below, with the following debug window. I am using ImageJ version 2.14.0/1.54f Build: c89e8500e4

Screenshot 2024-02-22 at 2 37 55 PM
Screenshot 2024-02-22 at 2 38 12 PM

@lacan
Copy link
Author

lacan commented Feb 23, 2024

perhaps you put a dot when specifying the extension? or the image is the wrong extension?

@cfausto
Copy link

cfausto commented Feb 23, 2024

@lacan I appreciate that you are still active on this post!

I was just able to find the issue when manually saving at tifs last night, my .lif file name was too long and it created ellipsis! I guess FIJI does have a character limit for file naming. I was able to get it running after making the file name shorter. Good to know!

image

@lacan
Copy link
Author

lacan commented Feb 24, 2024

good to know. the ellipsis might me talked to a Mac file path issue, as Fiji is just reading the filename in the macro. thanks for the report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment