// Batch_FRC macro by Christophe Leterrier // Process ThunderSTORM csv localization files // Makes two sets of image pairs for FRC analysis // See https://github.com/cleterrier/ChriSTORM/blob/master/Readme.md macro "Batch FRC" { // Save Settings saveSettings(); //*************** Initialize variables *************** // Detect if called from macro arg = getArgument(); if (lengthOf(arg)>0) { called = true; argarray = split(arg, ","); } else { called = false; } LOC_SUFFIX = ".csv"; OUT_PARAM = "FRC"; // Camera setup variables // CAM_SIZE = 160; // CG = 12.48; // camera gain new images // EM = 100; // EM gain new images // File chooser CHOOSE_DEF = false; CHOOSE_STRING_DEF = ""; EXC_DEF = false; EXC_STRING_DEF = "_ZR_"; // Split mode SPLIT_ARRAY = newArray("localizations", "frame", "100 frames"); SPLIT_DEF = SPLIT_ARRAY[2]; // Keep split localization files KEEP_DEF = true; // Cam image size (in pixels) 0, 5, CAMX_DEF = 256; CAMY_DEF = 256; // Magnification (size of pixel on widefield image / size of pixel on paired SR images) PIX_DEF = 40; // Compute FRC FRC_DEF = false; //*************** Get input folder *************** // Get input directory (dialog or argument) if (called == false) { INPUT_DIR = getDirectory("Select a source folder"); } else { INPUT_DIR = argarray[0]; } print("\n\n\n*** Batch Process Localizations started ***"); print(""); print("Input folder: " + INPUT_DIR); //*************** Dialog *************** if (called == false) { //Creation of the dialog box Dialog.create("Batch process localizations: options"); Dialog.addCheckbox("Choose files based on name", CHOOSE_DEF); Dialog.addString("Name contains", CHOOSE_STRING_DEF); Dialog.addCheckbox("Exclude files based on name", EXC_DEF); Dialog.addString("Name contains", EXC_STRING_DEF); Dialog.addMessage(""); Dialog.addChoice("Split mode", SPLIT_ARRAY, SPLIT_DEF); Dialog.addCheckbox("Keep split locs files", KEEP_DEF); Dialog.addNumber ("Cam image width:", CAMX_DEF, 0, 5, "pixels"); Dialog.addNumber ("Cam image height:", CAMY_DEF, 0, 5, "pixels"); Dialog.addNumber("Magnification (compared to cam pixel size)", PIX_DEF, 0, 5, "X"); Dialog.addCheckbox("Compute FRC", FRC_DEF); Dialog.show(); // Feeding variables from dialog choices CHOOSE = Dialog.getCheckbox(); CHOOSE_STRING = Dialog.getString(); EXC = Dialog.getCheckbox(); EXC_STRING = Dialog.getString(); SPLIT_MODE = Dialog.getChoice(); KEEP = Dialog.getCheckbox(); CAMX = Dialog.getNumber(); CAMY = Dialog.getNumber(); PIX = Dialog.getNumber(); FRC = Dialog.getCheckbox(); } // called from macro: // arguments (INPUT_DIR, CHOOSE, CHOOSE_STRING, EXC, EXC_STRING, SPLIT_MODE, KEEP, PIX, CAMX, CAMY, FRC) else { CHOOSE = argarray[1]; CHOOSE_STRING = argarray[2]; EXC = argarray[3]; EXC_STRING = argarray[4]; SPLIT_MODE = argarray[5]; KEEP = argarray[6]; PIX = argarray[7]; CAMX = argarray[8]; CAMY = argarray[9]; FRC = argarray[10]; } //*************** Prepare processing *************** //Time counter startTime = getTime(); // Get all file names ALL_NAMES = getFileList(INPUT_DIR); Array.sort(ALL_NAMES); ALL_TS = newArray(ALL_NAMES.length); ALL_TYPES = newArray(ALL_NAMES.length); // Make output folder OUTPUT_DIR = File.getParent(INPUT_DIR); OUTPUT_NAME = File.getName(INPUT_DIR); OUTPUT_DIR = OUTPUT_DIR + File.separator + OUTPUT_NAME + " " + OUT_PARAM + File.separator; if (File.isDirectory(OUTPUT_DIR) == false) { File.makeDirectory(OUTPUT_DIR); } print("Output folder: " + OUTPUT_DIR); // Prepare filtering strings to split localizations if (SPLIT_MODE == "localizations") { FILT_STRING1 = "id%2=0"; FILT_STRING2 = "id%2=1"; } else if (SPLIT_MODE == "frames") { FILT_STRING1 = "frame%2=0"; FILT_STRING2 = "frame%2=1"; } else if (SPLIT_MODE == "100 frames") { FILT_STRING1 = "frame%200<100"; FILT_STRING2 = "frame%200>99"; } // run("Camera setup", "isemgain=true pixelsize=" + CAM_SIZE + " gainem=" + CG +" offset=85 photons2adu=" + EM); //*************** Process loc files *************** // Detect number of files FileTotal = 0; for (n = 0; n < ALL_NAMES.length; n++) { ALL_TS[n] = false; ALL_TYPES[n] = "not TS"; if (endsWith(ALL_NAMES[n], LOC_SUFFIX) == true) { ALL_TS[n] = true; ALL_TYPES[n] = "[CSV (comma separated)]"; } if (ALL_TS[n] == true) { if ((CHOOSE == false || indexOf(ALL_NAMES[n], CHOOSE_STRING) > -1) && (EXC == false || indexOf(ALL_NAMES[n], EXC_STRING) == -1)) { FileTotal++; } } } // print(FileTotal); // Loop on all TS loc files FileCount = 0; for (n = 0; n < ALL_NAMES.length; n++) { if (ALL_TS[n] == true) { if ((CHOOSE == false || indexOf(ALL_NAMES[n], CHOOSE_STRING) > -1) && (EXC == false || indexOf(ALL_NAMES[n], EXC_STRING) == -1)) { // Image counter FileCount++; // Get the file path FILE_PATH = INPUT_DIR + ALL_NAMES[n]; // Store components of the file name FILE_NAME = File.getName(FILE_PATH); print(" Input file #" + FileCount + "/" + FileTotal + ": " + FILE_NAME); // Open the loc file run("Import results", "append=false startingframe=1 rawimagestack= filepath=[" + FILE_PATH + "] livepreview=false fileformat=" + ALL_TYPES[n]); // Filter even localizations (file 1) run("Show results table", "action=filter formula=[" + FILT_STRING1 + "]"); // Count the nLoc number nLocs = eval("script", "importClass(Packages.cz.cuni.lf1.lge.ThunderSTORM.results.IJResultsTable); var rt = IJResultsTable.getResultsTable(); rows = rt.getRowCount();"); nLocK = round(nLocs / 1000); // Rename file 1 OUT_TITLE1 = FILE_NAME; OUT_TITLE1 = replace(OUT_TITLE1, "([0-9])+K_", "_FRCa_" + nLocK + "K_"); if (OUT_TITLE1 == FILE_NAME) { OUT_TITLE1 = replace(OUT_TITLE1, ".csv", "_FRCa.csv"); } // Save loc file 1 if (KEEP == true) { OUT_PATH1 = OUTPUT_DIR + OUT_TITLE1; run("Export results", "filepath=[" + OUT_PATH1 + "] fileformat=[CSV (comma separated)] saveprotocol=false"); print(" Output loc file 1:" + OUT_TITLE1); } // Generate image 1 run("Visualization", "imleft=0.0 imtop=0.0 imwidth=" + CAMX + " imheight=" + CAMY + " renderer=[Normalized Gaussian] dxforce=false pickedlut=[Rainbow RGB] magnification=" + PIX + " colorize=true dx=5.0 threed=false dzforce=false"); IM1_ID = getImageID(); // Save image 1 OUT_TITLEIM1 = replace(OUT_TITLE1, ".csv", ".tif"); OUT_PATHIM1 = OUTPUT_DIR + OUT_TITLEIM1; rename(OUT_TITLEIM1); save(OUT_PATHIM1); print(" Output image 1:" + OUT_TITLEIM1); // Close image 1 if (FRC == false) close(); // Filter odd localizations (file 2) run("Show results table", "action=reset"); run("Show results table", "action=filter formula=[" + FILT_STRING2 + "]"); // Count the nLoc number nLocs = eval("script", "importClass(Packages.cz.cuni.lf1.lge.ThunderSTORM.results.IJResultsTable); var rt = IJResultsTable.getResultsTable(); rows = rt.getRowCount();"); nLocK = round(nLocs / 1000); // Rename file 2 OUT_TITLE2 = FILE_NAME; OUT_TITLE2 = replace(OUT_TITLE2, "([0-9])+K_", "_FRCb_" + nLocK + "K_"); if (OUT_TITLE2 == FILE_NAME) { OUT_TITLE2 = replace(OUT_TITLE2, ".csv", "_FRCb.csv"); } // Save loc file 2 if (KEEP == true) { OUT_PATH2 = OUTPUT_DIR + OUT_TITLE2; run("Export results", "filepath=[" + OUT_PATH2 + "] fileformat=[CSV (comma separated)] saveprotocol=false"); print(" Output loc file 2:" + OUT_TITLE2); } // Generate image 2 run("Visualization", "imleft=0.0 imtop=0.0 imwidth=" + CAMX + " imheight=" + CAMY + " renderer=[Normalized Gaussian] dxforce=false pickedlut=[Rainbow RGB] magnification=" + PIX + " colorize=true dx=5.0 threed=false dzforce=false"); IM2_ID = getImageID(); // Save image 2 OUT_TITLEIM2 = replace(OUT_TITLE2, ".csv", ".tif"); OUT_PATHIM2 = OUTPUT_DIR + OUT_TITLEIM2; rename(OUT_TITLEIM2); save(OUT_PATHIM2); print(" Output image 2:" + OUT_TITLEIM2); // Close image 1 if (FRC == false) close(); if (FRC == true) { run("FRC Calculation...", "image_1=" + OUT_TITLEIM1 + " image_2=" + OUT_TITLEIM2 + " resolution=[Fixed 1/7] display"); selectImage(IM1_ID); close(); selectImage(IM2_ID); close(); } } // end of IF loop on include/exclude names } // end of IF loop on extensions } // end of FOR loop on n extensions //*************** Cleanup and end *************** if (FRC == true && FileCount > 1) { run("Images to Stack", "name=[FRC curves stack] title=[FRC Of] use"); save(OUTPUT_DIR + "FRC_Curves.tif"); close(); selectWindow("FRC Results"); saveAs("results", OUTPUT_DIR + "FRC_Results.xls"); run("Close"); } // Restore settings restoreSettings(); showStatus("Batch FRC finished"); //Time counter stopTime = getTime(); Time = stopTime - startTime; print(""); print("*** Batch FRC ends after " + Time / 1000 + " s ***\n\n\n"); if (called == true) return OUTPUT_DIR; }