// Batch_3Ddrift_ZOLA macro by Christophe Leterrier // Process ThunderSTORM csv localization files // Drift correction in ZOLA, export, then reconversion into TS format macro "Batch 3D Drift ZOLA" { // 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_SUFFIX1 = ".csv"; LOC_SUFFIX2 = ".xls"; OUT_PARAM1 = "proc1"; OUT_PARAM2 = "proc2"; // Camera setup variables CAM_SIZE = 160; CG = 12.48; // camera gain new images EM = 100; // EM gain new images GPU = false; // use GPU // File chooser CHOOSE_DEF = false; CHOOSE_STRING_DEF = ""; EXC_DEF = true; EXC_STRING_DEF = "_ZR_"; COUNT_DEF = true; // Drift correction CORR_DRIFT_DEF = true; BIN_DEF = 7; // number of frames per sub-reconstruction used for autocorrelation MAG_DEF = 50; // pixel size (nm) of sub-reconstructions used for autocorrelation SM_DEF = 1; // Maximal drift (um) //*************** 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 3D Drift ZOLA 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.addCheckbox("Correct drift", CORR_DRIFT_DEF); Dialog.addNumber("Number of bins for sub-images", BIN_DEF, 0, 5, ""); Dialog.addNumber("Pixel size for autocorrelation", MAG_DEF, 0, 2, "nm"); Dialog.addNumber("Maximum drift", SM_DEF, 3, 5, "µm"); Dialog.show(); // Feeding variables from dialog choices CHOOSE = Dialog.getCheckbox(); CHOOSE_STRING = Dialog.getString(); EXC = Dialog.getCheckbox(); EXC_STRING = Dialog.getString(); CORR_DRIFT = Dialog.getCheckbox(); BIN = Dialog.getNumber(); MAG = Dialog.getNumber(); SM = Dialog.getNumber(); } // called from macro: // arguments (INPUT_DIR, CHOOSE, CHOOSE_STRING, EXC, EXC_STRING, CORR_DRIFT, BIN, MAG, SM) else { CHOOSE = argarray[1]; CHOOSE_STRING = argarray[2]; EXC = argarray[3]; EXC_STRING = argarray[4]; RESET = argarray[5]; CORR_DRIFT = argarray[6]; BIN = argarray[7]; MAG = argarray[8]; SM = argarray[9]; } //*************** 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); OUTPUT_DIR = File.getParent(INPUT_DIR); OUTPUT_NAME = File.getName(INPUT_DIR); OUTPUT_DIR1 = OUTPUT_DIR + File.separator + OUTPUT_NAME + " " + OUT_PARAM1 + File.separator; OUTPUT_DIR2 = OUTPUT_DIR + File.separator + OUTPUT_NAME + " " + OUT_PARAM2 + File.separator; if (File.isDirectory(OUTPUT_DIR1) == false) { File.makeDirectory(OUTPUT_DIR1); } if (File.isDirectory(OUTPUT_DIR2) == false) { File.makeDirectory(OUTPUT_DIR2); } print("Output folder 1: " + OUTPUT_DIR1); print("Output folder 2: " + OUTPUT_DIR2); run("EMCCD", "emccd_camera_adu=" + CG + " emccd_camera_gain=" + EM + " emccd_camera_offset=100"); //*************** 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_SUFFIX1) == true) { LOC_SUFFIX = LOC_SUFFIX1; ALL_TS[n] = true; ALL_TYPES[n] = "[CSV (comma separated)]"; } if (endsWith(ALL_NAMES[n], LOC_SUFFIX2) == true) { LOC_SUFFIX = LOC_SUFFIX2; ALL_TS[n] = true; ALL_TYPES [n] = "[XLS (tab 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); OUT_TITLE = FILE_NAME; // Open the loc file run("Import table", "file_path=[" + FILE_PATH + "]"); if (CORR_DRIFT == true) { if (GPU == true) GPU_STRING = "run_on_gpu"; else GPU_STRING = ""; run("3D Drift correction", GPU_STRING + " cross-correlation_pixel_size=" + MAG + " number=" + BIN + " maximum_drift=" + SM + " localization_table_attached=[]"); } // Export the corrected locs into an output file OUT_PATH1 = OUTPUT_DIR1 + FILE_NAME; run("Export table", "file_path=[" + OUTPATH1 +""]"); print(" ZOLA drift correction saved in proc1"); close(); selectWindow("Z_drift"); close(); selectWindow("Y_drift"); close(); selectWindow("X_drift"); close(); selectWindow("2D color histogram 20.0nm per px"); close(); // One pass in ThunderSTORM to remove ZOLA columns // run("Camera setup", "offset=100.0 quantumefficiency=1.0 isemgain=true photons2adu=12.48 gainem=100.0 pixelsize=160.0"); run("Import results", "detectmeasurementprotocol=false filepath=[" + OUTPATH1 + "] fileformat=[CSV (comma separated)] livepreview=false rawimagestack= startingframe=1 append=false"); OUT_PATH2 = OUTPUT_DIR2 + FILE_NAME; run("Export results", "floatprecision=1 filepath=[" + OUTPATH2 + "] fileformat=[CSV (comma separated)] chi2=false offset=false occurrencemerging=false saveprotocol=false drifty=false uncertainty_xy=true driftz=false intensity=true driftx=false background=true x=true uncertainty_z=true y=true z=true id=false crlbx=false crlby=false crlbz=false frame=true"); print(" ThunderSTORM file saved in proc2"); } // end of IF loop on include/exclude names } // end of IF loop on extensions } // end of FOR loop on n extensions //*************** Cleanup and end *************** // Restore settings restoreSettings(); showStatus("Batch Process Localizations finished"); //Time counter stopTime = getTime(); Time = stopTime - startTime; print(""); print("*** Batch 3D Drift ZOLA end after " + Time / 1000 + " s ***\n\n\n"); return OUTPUT_DIR; }