Macro bug with Bio-format importer in a loop

bio-formats
imagej
macro

#1

Hi,

I am currently trying to write a macro to convert in tiff images all images contain in a lif file (Leica format). As I have a bunch of lif files, I inserted that macro in a loop so it would repeat this operation on all my lif files.
What happens is that only the first image of all my lif files are processed and I can’t figure out what I did wrong. Any way one of you sees it?

Here is the code:

input = getDirectory("Dossier à traiter?");  
list = getFileList(input); 
output = getDirectory("Où enregistrer?");


for (i=0; i<list.length; i++)                                                      
{
    image = list[i];
    path = input + image;
    run("Bio-Formats Macro Extensions");
    Ext.setId(path);
    Ext.getCurrentFile(file);
    Ext.getSeriesCount(seriesCount);
    run("Bio-Formats Importer", "open=&path autoscale color_mode=Default view=Hyperstack stack_order=XYCZT series_");

    t=getTitle();
    date = substring(t, 0, 10);
    sub_output = output + date;
    File.makeDirectory(sub_output);                                                 
    close();

    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);
        title=getTitle();
        
        
        if (endsWith(title, "Snapshot1")) 
        {
            s=lastIndexOf(title, " - C=2");
            subtitle=substring(title, 0, s);
            close();
            run("Merge Channels...", "c1=["+ subtitle + " - C=0] c2=[" + subtitle + " - C=1] create");
        }
        
        else 
        {
            s=lastIndexOf(title, " - C=1");
            subtitle=substring(title, 0, s);
            run("Merge Channels...", "c1=["+ subtitle + " - C=1] c2=[" + subtitle + " - C=0] create");
        }

        rename(subtitle);
        saveAs("Tiff",  sub_output + "\\" + subtitle);
        close();
    }
}

Thanks a lot! :slightly_smiling:
Fanny


#2

Hey,
I finally figured out my problem. I’m posting the solution in case anyone runs into the same problem someday. I think the “if / else” conditions made the second loop bug. I took it out of it by coding a new function and it works perfectly now. Here is the new code:

input = getDirectory("Dossier à traiter?"); 
list = getFileList(input); 
output = getDirectory("Où enregistrer?");


for (i=0; i<list.length; i++)                                                          
{
    image = list[i];
    path = input + image;
    run("Bio-Formats Macro Extensions");
    Ext.setId(path);
    Ext.getCurrentFile(file);
    Ext.getSeriesCount(seriesCount);
    run("Bio-Formats Importer", "open=&path autoscale color_mode=Default view=Hyperstack stack_order=XYCZT series_");

    t=getTitle();
    date = substring(t, 0, 10);
    sub_output = output + date;
    File.makeDirectory(sub_output);                                                 
    close();

    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);  
        Merge_channels();

        subtitle=getTitle();
        saveAs("Tiff",  sub_output + "\\" + subtitle);
        run ("Close All");
    }
}


function Merge_channels()
{
    title=getTitle();
    
    if ((endsWith(title, "Snapshot1 - C=2")))    
    {
        print("Snapshot");
        s=lastIndexOf(title, " - C=2");
        subtitle=substring(title, 0, s);
        close();
        run("Merge Channels...", "c1=["+ subtitle + " - C=0] c2=[" + subtitle + " - C=1] create");
        rename(subtitle);
    }
        
    else 
    {
        print("Not a snapshot");
        s=lastIndexOf(title, " - C=1");
        subtitle=substring(title, 0, s);
        run("Merge Channels...", "c1=["+ subtitle + " - C=1] c2=[" + subtitle + " - C=0] create");
        rename(subtitle);
    }
}

Hope it’ll help someone someday!
Cheers,
Fanny


#3

Hi Fanny,

Thanks so much! It DID help me out today!!