0
votes

I've been combing this forum looking at other examples of code similar to mine but I still have not been able to solve the issue.

I am processing images in Fiji/imageJ using a macro code. The code is meant to recursively search a given directory for image files (in this case .vsi files) and convert them to .tiff using the built in bioformats functions.

I have cobbled the following code together from several examples (I am not a programmer, I have a biology degree), and I am returning an error that the specified file does not exist just as I am about to actually process a file.

All of my images have the following general directory structure:

~/Brain_01/Slice_01/Slice_01_01.vsi

An actual example is as follows for the second image in this set:

~/K259_tlx3cre/K259_1of3_02/K259_1of3_01_02.vsi

I really, really appreciate all the help you guys provide! I am completely stuck right now and am just banging my head against the keyboard at this point. Also, I think I've followed all the rules for posting. If not please just point out the error of my ways.

Finally here is the macro code I have written so far:

Dialog.create("File type");
Dialog.addString("File suffix: ", ".vsi", 5);
Dialog.show();
suffix = Dialog.getString();

inDir = getDirectory("Choose Directory Containing " + suffix + " Files ");
outDir = getDirectory("Choose Directory for TIFF Output ");
setBatchMode(true);
processFiles(inDir, outDir, "");
print("Done!");

function processFiles(inBase, outBase, sub) {
  flattenFolders = false; // this flag controls output directory structure
  print("Processing folder: " + sub);
  list = getFileList(inBase + sub);
  if (!flattenFolders) File.makeDirectory(outBase + sub);
  for (i=0; i<list.length; i++) {
    path = sub + list[i];
    //upath = toUpperCase(path);
    upath = path; //the previous line cause some problems
    if (endsWith(path, "/")) {
      processFiles(inBase, outBase, path);

    }
    else if (endsWith(upath, suffix)) {
        print("Importing " + suffix + " = " + list[i]);

            run("Bio-Formats Windowless Importer", "open=path color_mode=Default view=Hyperstack stack_order=XYCZT");

            print("Blue...");
            selectImage(1);
            title1 = getTitle();
            run("Blue");

            print("Green...");
            selectImage(2);
            title2 = getTitle();
            run("Green");

            print("Red...");
            selectImage(3);
            title3 = getTitle();
            run("Red");

            print("Merging Channels...");
            run("Merge Channels...", "red=&title3 green=&title2 blue=&title1 gray=*None* cyan=*None* magenta=*None* yellow=*None* create keep");

            print("Converting to RGB");
            run("RGB Color");

            saveAs("Tiff", path);
            run("Close All");
    }
  }
}

Thanks to some great help from Jan, and the people on the ImageJ mailing list, I was able to get my code working well enough to do what I need to do! The code below is working well enough to batch convert a directory full of .vsi files to .tiff using the functions I need. I haven't tried changing my code to Michael's suggestion to change endsWith(path, "/"... if i get that working in the future I will post it also.

Dialog.create("File type");
Dialog.addString("File suffix: ", ".vsi", 5);
Dialog.show();
suffix = Dialog.getString();

inDir = getDirectory("Choose Directory Containing " + suffix + " Files ");
outDir = getDirectory("Choose Directory for TIFF Output ");
setBatchMode(true);
processFiles(inDir, outDir, "");
print("Done!");

function processFiles(inBase, outBase, sub) {
  flattenFolders = true; // this flag controls output directory structure
  print("Processing folder: " + sub);
  list = getFileList(inBase + sub);
  if (!flattenFolders) File.makeDirectory(outBase + sub);
  for (i=0; i<list.length; i++) {
    path = sub + list[i];
    //upath = toUpperCase(path); only need if the file extension is case sensitive
    upath = path; //avoids the previous line
    if (endsWith(path, "/")) {
      processFiles(inBase, outBase, path);

    }
    else if (endsWith(upath, suffix)) {

        print("Importing " + suffix + " = " + list[i]);

        run("Bio-Formats Windowless Importer", "open=["+inBase + path+"] color_mode=Default view=Hyperstack stack_order=XYCZT");

        print("Stack to Images...");
        run("Stack to Images");

        print("Blue...");
        selectImage(1);
        title1 = getTitle();
        run("Blue");

        print("Green...");
        selectImage(2);
        title2 = getTitle();
        run("Green");

        print("Red...");
        selectImage(3);
        title3 = getTitle();
        run("Red");

        print("Merging Channels...");
        run("Merge Channels...", "red=&title3 green=&title2 blue=&title1 gray=*None* cyan=*None* magenta=*None* yellow=*None* create keep");

        print("Converting to RGB");
        run("RGB Color");

        saveAs("Tiff", outBase + path);
        run("Close All");
    }
  }
}
1

1 Answers

0
votes

By looking at the error message you get, you should be able to spot the issue: while your file sits at e.g. inBase + sub + list[i], your path variable contains just sub + list[i]. So you should use something like:

run("Bio-Formats Windowless Importer", "open=[" + inBase + path + "] color_mode=Default view=Hyperstack stack_order=XYCZT");

for opening the file, and:

saveAs("Tiff", outBase + path);

for saving the output file.

If you also want to use the flattenFolders flag in your code, you should build your output path dependent on the state of flattenFolders, such as:

outputPath = outBase;
if (!flattenFolders) outputPath += sub;
outputPath += list[i];

By the way, there's no need to convert your path to uppercase with toUpperCase(path), unless you want to use a case-insensitive file suffix. In that case, you can write:

if (endsWith(toUpperCase(path), toUpperCase(suffix))) {
    // process your file here
}

to allow both .vsi and .VSI as an input.

Note: The script editor of the Fiji distribution of ImageJ offers a macro template to process folders recursively, you can open it via Templates > Macros > Process Folder in the editor.