0
votes

To start off with, I'm a biochemist with pretty much no experience with any kind of coding so this is probably why struggling. I have recently been trying to automate my image analysis process on FIJI/ImageJ and I've hit a wall. My current code is as below:

waitForUser("Are you ready to start?")


open("C:/Users/laure/OneDrive/Documents/MSc Molecular Biology & Biotechnology/Masters Project - Dombrowski Lab/Project Data/LD005 CX5/CellHealthProfiling.V4_03-06-20_09;46;02/CEM-133432_200229080001/CEM-133432_200229080001_B02f00d0.C01");

rename("CEM-133432_200229080001_B02f00d0.C01 - Well B02, Field #00") //for automation I kept the same image name

// define variables for each window
x="CEM-133432_200229080001_B02f00d0.C01 - Well B02, Field #00" //picture name
y="c:1/3 - Well B02, Field #00" //DAPI
a="c:2/3 - Well B02, Field #00" //Olig2
b="c:3/3 - Well B02, Field #00" //MBP
c="Result of c:1/3 - Well B02, Field #00" //DAPI, Olig2 overlap
d="Result of Result of c:1/3 - Well B02, Field #00" //DAPI, Olig2, MBP
e="Drawing of c:1/3 - Well B02, Field #00" // draws all counted cells with numbers (excludes too big/small particles)
f="Drawing of c:2/3 - Well B02, Field #00" // same for Olig2
g="Drawing of c:3/3 - Well B02, Field #00" //same for MBP
h="Drawing of Result of c:1/3 - Well B02, Field #00" // same for DAPI, Olig2
i="Drawing of Result of Result of c:1/3 - Well B02, Field #00" // same for DAPI, Olig2, MBP


//DAPI
selectWindow(x);
run("Stack to Images");
selectWindow(y);
run("8-bit");
setThreshold(45, 255); //was 45 in windows
run("Convert to Mask"); //converts everything between 45 and 255 to a mask for further analysis
//--> binary image: each pixel can only have one of two values to to indicate whether it is inside the threshold or not!
//--> black: inside threshold, white: outside threshold

//analyze particles feature
run("Analyze Particles...", "size=30-350 show=Outlines clear summarize add");


//OLIG2
selectWindow(a);
run("8-bit");
setThreshold(25, 255); //was 80 in windows
run("Convert to Mask");
//analyze particles feature
run("Analyze Particles...", "size=30-250 show=Outlines clear summarize add");


//MBP
selectWindow(b);
run("8-bit");
setThreshold(52, 255); //was 150 in Windows
run("Convert to Mask");
//analyze particles feature
run("Analyze Particles...", "size=30.00-250.0 show=Outlines clear summarize add");


//measure overlay DAPI + Olig2
imageCalculator("AND create", y, a); //overlay DAPI and Olig2 
//(other options are: "add","subtract","multiply","divide", "and", "or", "xor", "min", "max", "average", "difference" or "copy")
selectWindow(c);
run("Analyze Particles...", "size=30-250 show=Outlines clear summarize add");


//measure overlay DAPI + Olig2 + MBP
imageCalculator("AND create", c, b); //overlay DAPI, Olig2 and MBP
selectWindow(d);
run("Analyze Particles...", "size=30-250 show=Outlines clear summarize add");

close(y);
close(a);
close(b);
close(c);
close(d);
close(e);
close(f);
close(g);
close(h);
close(i);

This macro works but it will only analyse one stack (three images individually analysed and then layered as a composite to quantify any overlap of each of the signals at each wavelength), whereas I'm trying to loop it to do ten "stacks" at a time and feed me back the information but when I try to do this, the macro gets stuck on the file name e.g. CEM-133432_200229080001_B02f00d0.C01, which is one of three , the other two being CEM-133432_200229080001_B02f00d1.C01, CEM-133432_200229080001_B02f00d2.C01 . The next stack in the series would then be CEM-133432_200229080001_B02f01d0.C01,CEM-133432_200229080001_B02f01d1.C01 & CEM-133432_200229080001_B02f01d2.C01 and so on.

For reference the images are from a 96 well plate, the CX5 microscope takes 25 stacks of images per well, I'm aiming to analyse ten stacks to then average out the double and triple positive signals per well. When I try looping it just says it doesn't recognise the file name.

Any solutions or suggestions would be greatly appreciated

1

1 Answers

0
votes

From your code, you have two issues to overcome (which you've already identified). How to loop over all your files and how to deal with changing names. Below I have pasted an outline ImageJ macro that will get you started. This will find the C01 files and work on them one-by-one. Note that the names are irrelevant. When you're programming, you can use variables for things that you don't know or that will need to change. That brings us to the second problem. You have set up some string variables that are hard-coded. They will work for one image but as soon as you are working on a different image the code will fail. These strings need to be dynamically assigned within the loop.

macro "Process My Files" {
    dir1 = getDirectory("Choose Source Directory ");
    dir2 = getDirectory("Choose Destination Directory ");
    list = getFileList(dir1);

    // Make an array of C01 files only
    C01list = newArray(0);
    for (i=0; i<list.length; i++) {
        if (endsWith(list[i], ".C01")) {
            C01list = append(C01list, list[i]);
        }
    }

    setBatchMode(true);
    for (i=0; i<C01list.length; i++) {
        showProgress(i+1, C01list.length);
        // your code goes here - an example is shown
        s = "open=["+dir1+C01list[i]+"] autoscale color_mode=Composite rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT";
        run("Bio-Formats Importer", s);
        saveAs("tiff", dir2+replace(C01list[i],".C01",".tif"));
        close();
        // and ends here
    }
    setBatchMode(false);
}

function append(arr, value) {
    arr2 = newArray(arr.length+1);
    for (i=0; i<arr.length; i++)
        arr2[i] = arr[i];
        arr2[arr.length] = value;
    return arr2;
}

This code example will open each C01 file you have and save as a tiff. You need to customise it for what you need to do. Your application is too specific to get more help at this point. Try and roll your analysis into this wrapper. Use a generic method to name the windows that you are making, you will close them all before the loop starts again so it is safe to call them result1 or result2 etc. You can specify this in many operations or you can rename the window(s). Finally, note the square brackets in my example. They allow special characters in file paths (you were possibly also encountering this problem).