Macro - make substack based on user-defined strings (similar post was prematurely created)

Hello,
I am currently developing a macro to generate stacks with the order of slices being defined by the user. It’s intended to be used by users of a HCS microscope (IN Cell analyzer 2200, GE), which image titles are defined as “well of plate - field of view - fluorescence channel” (i.e. B - 01(fld 01 wv Cy3 - Cy3). So far, it correctly import an image sequence as stack and save it. It goes like this:

macro "IN Cell - Stack" {
setBatchMode(true);
  inputPath = getDirectory("Choose an input directory");
  outputPath = getDirectory("Choose an output directory");
  Dialog.create("IN Cell - image manipulator");
  itens=newArray("2","3","4");
  Dialog.addRadioButtonGroup("Number of channels", itens, 1, 3, 2);
  itens=newArray("FITC", "Texas Red", "Cy3", "Cy5", "dsRed", "CFP", "YFP", "DAPI", "Brightfield");
  Dialog.addRadioButtonGroup("First channel of stack", itens, 1, 9, 1);
  Dialog.addRadioButtonGroup("Second channel of stack", itens, 1, 9, 1);
  Dialog.addRadioButtonGroup("Third channel of stack", itens, 1, 9, 1);
  Dialog.addRadioButtonGroup("Fourth channel of stack", itens, 1, 9, 1);
  Dialog.show();
  nchannels=Dialog.getRadioButton();
  c1=Dialog.getRadioButton();
  c2=Dialog.getRadioButton();
  c3=Dialog.getRadioButton();
  c4=Dialog.getRadioButton();
  filelist = getFileList(inputPath);
  counter=0;
  for (i=0; i<filelist.length; i++)         
    if ( endsWith(filelist[i], "tif")) counter++;
  tiflist = newArray(counter);
  counter = 0;
  for (i=0; i<filelist.length; i++)          
    if  (endsWith(filelist[i], "tif")) {
      tiflist[counter] = filelist[i];
      counter++;
      i=index(tiflist, tiflist[i]);
    }
for (i=0; i<tiflist.length; i=i+nchannels) {
  run("Image Sequence...", "open=["+inputPath+"] number="+nchannels+" starting="+i+1+"");
parseFloat(counter/nchannels);
integ=parseInt(counter/nchannels);
    if (counter/nchannels != integ) {
exit("Error: uneven number of stacks. Please check whether your number of images and channels are really "+counter+" and "+nchannels+", respectively")
  }
  else { 
  filename=inputPath+tiflist[i];
  startindex=lastIndexOf(filename, "\\");
  finalindex=lastIndexOf(filename, "w");
  basename=substring(filename, startindex, finalindex);
  saveAs("Tiff", outputPath + basename+")");
  run("Close All");}
 function index(a, filenumber) { 
      for (i=0; i<a.length; i++) 
          if (a[i]==filenumber) return i; 
      return -1; 
  } 
}
print("\\Clear");
print("Finished macro. Please check the output directory for results");
setBatchMode(false);
}

I got stucked at the part of reordering the channels as requested by the user in the dialog box. As the “stack sorter” is not macro recordable, I thought about making a substack with channels ordered as per the user defined sequence. I believe I have to first add the channels to an array, then get their index numbers and reorder while making a substack. Also, it might be a bit more tricky than that because number varies from user to user. Any advice on how to perform such reordering of channels ?
Feel free to make any other suggestions related to the macro. It seems to be unnecessarily extensive :roll_eyes:
Thanks in advance.

ps.: here’s also some data for macro testing
macro (IN Cell - Stack, Iuri) test.rar (59.8 KB)

Hi @IuriCV,

I believe I can make it a bit easier for you by telling you that Image>Colors>Arrange Channels is macro-recordable.

Best,
Laura

1 Like

Hi, @lmurphy . Thanks for your reply.
I just tryed that, but Import>Image Sequence... generates a stack which slices are not recognized as channels. Am I missing something too trivial right here ?

Ok, I now see that Image>Hyperstacks>Stack to Hyperstack... has to be performed before arranging channels. I guess I’ll be able to finish the macro now. Thanks once again, @lmurphy !

1 Like

It seems that Arrange channels also requires the index numbers of channels within the stack. I guess I still have to get that info from an array function.

Oh sorry for commenting and dashing. I skimmed read your post a little but would be happy to look at it if you are still struggling (and noone else comes along)… does sound like you working it out though (?). I’m afraid I’m not good for simplifying macro queries as I am long winded, inefficient macro writer myself.

Hi, @lmurphy. I’ve actually struggled with the macro all day. I’m stuck at the same point. I would really appreciate your help once again.