Macro for Colocalization Analysis

macro
Tags: #<Tag:0x00007fb87c1aa478>

#1

I am an ImageJ macro newbie, but am trying my best to figure things out. Essentially, I am trying to write a macro which uses the “Colocalization” plugin to check the colocalization of my images, and then perform further analysis like Analyze Particles. The problem is that in the parameters of the “Colocalization” plugin, I have to write which images to choose as the two ones being analyzed. Here is the relevant part of the macro (rest at end of post):

imageTitle=getTitle();
run("Colocalization ",  "channel_1=[(imageTitle + " (red)")] channel_2=[(imageTitle + " (green)")]
ratio=50 threshold_channel_1=50 threshold_channel_2=50 display=255") ;

However, the parameters for the Colocalization plugin are in “quotations”, but I also need to put "(red)"and (green) in quotations.

So I am getting an error message which says unexpected (') in this line.

Any way that I can fix this problem? Thanks so much!!

Total code below:

// "BatchProcessFolders"
//
// This macro batch processes all the files in a folder and any
// subfolders in that folder. In this example, it runs the Subtract
// Background command of TIFF files. For other kinds of processing,
// edit the processFile() function at the end of this macro.

   requires("1.33s");
   dir = getDirectory("Choose a Directory ");
   setBatchMode(true);
   count = 0;
   countFiles(dir);
   n = 0;
   processFiles(dir);
   //print(count+" files processed");

   function countFiles(dir) {
      list = getFileList(dir);
      for (i=0; i<list.length; i++) {
          if (endsWith(list[i], "/"))
              countFiles(""+dir+list[i]);
          else
              count++;
      }
  }

   function processFiles(dir) {
      list = getFileList(dir);
      for (i=0; i<list.length; i++) {
          if (endsWith(list[i], "/"))
              processFiles(""+dir+list[i]);
          else {
             showProgress(n++, count);
             path = dir+list[i];
             processFile(path);
          }
      }
  }

  function processFile(path) {
       if (endsWith(path, ".tif")) {
           open(path);
                imageTitle=getTitle();
        run("Split Channels");
        run("Colocalization ",  "channel_1=[imageTitle + "(red)"]
channel_2=[imageTitle + '(green)'] ratio=50 threshold_channel_1=50
threshold_channel_2=50 display=255") ;
        run("Split Channels");
        setAutoThreshold("Default dark");
        //run("Threshold...");
        setAutoThreshold("Otsu dark");
        setOption("BlackBackground", false);
        run("Convert to Mask", "method=Otsu background=Dark calculate");
        run("Analyze Particles...", "size=10-1000 show=Nothing summarize stack");
        run("Analyze Particles...", "size=10-1000 show=Nothing summarize stack");


           save(path);
           close();
      }
  }

#2

Dear @jesse,

imageTitle is a variable that you are not using in your version of the macro. Instead, use the the following:

imageTitle=getTitle();
run("Colocalization ",  "channel_1=["+imageTitle+" (red)] channel_2=["+imageTitle+" (green)]";

This uses string concatenation (+) to concatenate e.g. "channel_1=[" and the contents of the variable imageTitle (see https://imagej.nih.gov/ij/developer/macro/macros.html#variables for some more information).

Best,
Stefan