// Batch_Localize_Phasor macro by Christophe Leterrier // Process stacks with p3D-SMLM option of ThunderSTORM // 12/01/2017 // Todo: possibility to trim part of the sequence (beginning) // Todo: option to export acquired stacks to IJtif or bigtiff (optional processing) // Todo: add drift correction and merging (to stick to the Nikon workflow)? macro "Batch Localize Phasor" { // Save Settings saveSettings(); //*************** Initialize variables *************** // Detect if called from macro arg = getArgument(); if (lengthOf(arg)>0) { called = true; argarray = split(arg, ","); } else { called = false; } // Naming parameters input_ext1 = ".nd2"; input_ext2 = ".tif"; output_ext = ".csv"; // File chooser choose_file_def = false; choose_string_def = ""; except_file_def = false; except_string_def = ""; // Camera setup parameters // run("Camera setup", "offset=0.0 quantumefficiency=1.0 isemgain=true photons2adu=12.48 gainem=100.0 pixelsize=160.0"); offset_def = 100; isemgain_def = true; photons2adu_def = 12.48; gainem_def = 100; pixelsize_def = 160; // Run Analysis parameters // run("Run analysis", "filter=[Wavelet filter (B-Spline)] scale=0.9 order=3 detector=[Local maximum] connectivity=8-neighbourhood threshold=4*std(Wave.F1) estimator=[Phasor Fitting] astigmatism=false fitradius=1 calibrationpath=[path_to_yaml] renderer=[No Renderer]"); wave_scale_def = 0.9; wave_order_def = 3; detec_thresh_def = 4; phasor_radius_def = 1; //3D case // run("Run analysis", "filter=[Wavelet filter (B-Spline)] scale=0.9 order=3 detector=[Local maximum] connectivity=8-neighbourhood threshold=4*std(Wave.F1) estimator=[Phasor Fitting] astigmatism=true fitradius=1 calibrationpath=[path_to_yaml] renderer=[No Renderer]"); astigmatism_def = false; calib_path_def = File.directory; // Export parameters // run("Export results", "floatprecision=1 filepath=[path_to_csv] fileformat=[CSV (comma separated)] intensity=true offset=true saveprotocol=false x=true sigma2=false y=true sigma1=false z=true bkgstd=true id=true frame=true"); //*************** Get input folder *************** print("\n\n\n*** Batch Localize Phasor started ***"); print(""); //*************** Dialogs *************** if (called == false) { // Get input directory input_dir = getDirectory("Select a source folder"); //Creation of the dialog box Dialog.create("Batch Localize Phasor: options"); Dialog.addCheckbox("Choose files based on name", choose_file_def); Dialog.addString("Name contains", choose_string_def); Dialog.addCheckbox("Exclude files based on name", except_file_def); Dialog.addString("Name contains", except_string_def); Dialog.addMessage(""); Dialog.addNumber("Acquired sequence pixel size", pixelsize_def, 0, 5, "nm"); Dialog.addNumber("Gain before EM gain", photons2adu_def, 3, 4, "photons/ADU"); Dialog.addNumber("Camera offset", offset_def, 0, 5, "ADU"); Dialog.addCheckbox("EMCCD", isemgain_def); Dialog.addNumber("EM Gain", gainem_def, 0, 5, ""); Dialog.addMessage(""); Dialog.addNumber("Wavelet order", wave_order_def, 0, 5, ""); Dialog.addNumber("Wavelet scale", wave_scale_def, 3, 1, ""); Dialog.addNumber("Predetection threshold", detec_thresh_def, 0, 5, "*std(Wave.F1)"); Dialog.addNumber("Phasor radius", phasor_radius_def, 0, 5, "pixels"); Dialog.addMessage(""); Dialog.addCheckbox("3D processing", astigmatism_def); Dialog.show(); // Feeding variables from dialog choices choose_file = Dialog.getCheckbox(); choose_string = Dialog.getString(); except_file = Dialog.getCheckbox(); except_string = Dialog.getString(); pixelsize = Dialog.getNumber(); photons2adu = Dialog.getNumber(); offset = Dialog.getNumber(); isemgain = Dialog.getCheckbox(); gainem = Dialog.getNumber(); wave_order = Dialog.getNumber(); wave_scale = Dialog.getNumber(); detec_thresh = Dialog.getNumber(); phasor_radius = Dialog.getNumber(); astigmatism = Dialog.getCheckbox(); if (astigmatism == true) { calib_path = File.openDialog("Select a phasor .yaml calibration file"); } else calib_path = calib_path_def; } // called from macro: else { input_dir = argarray[0]; choose_file = argarray[1]; choose_string = argarray[2]; except_file = argarray[3]; except_string = argarray[4]; pixelsize = argarray[5]; photons2adu = argarray[6]; offset = argarray[7]; isemgain = argarray[8]; gainem = argarray[9]; wave_order = argarray[10]; wave_scale = argarray[11]; detec_thresh = argarray[12]; phasor_radius = argarray[13]; astigmatism = argarray[14]; calib_path = argarray[15]; } print("Input folder: " + input_dir); //*************** Prepare processing *************** //Time counter start_time = getTime(); // Get all file names all_names = getFileList(input_dir); Array.sort(all_names); output_dir = File.getParent(input_dir); output_name = File.getName(input_dir); output_dir = output_dir + File.separator + output_name + " phasor(W" + wave_order + "-" + wave_scale + "," + "T" + detec_thresh + "," + "R" + phasor_radius + ")" + File.separator; if (File.isDirectory(output_dir) == false) { File.makeDirectory(output_dir); } print("Output folder: " + output_dir); //*************** Setup camera parameters *************** run("Camera setup", "offset=" + offset + " quantumefficiency=1.0 isemgain=" + isemgain + " photons2adu=" + photons2adu + " gainem=" + gainem + " pixelsize=" + pixelsize); //*************** Process loc files *************** // Detect number of files to process file_number = 0; for (n = 0; n < all_names.length; n++) { if (endsWith(all_names[n], input_ext1) == true || endsWith(all_names[n], input_ext2) == true) { if ((choose_file == false || indexOf(all_names[n], choose_string) > -1) && (except_file == false || indexOf(all_names[n], except_string) == -1)) { file_number++; } } } // Setup BioFormats macro extension run("Bio-Formats Macro Extensions"); // Loop on all files file_count = 0; for (n = 0; n < all_names.length; n++) { if (endsWith(all_names[n], input_ext1) == true || endsWith(all_names[n], input_ext2) == true) { if ((choose_file == false || indexOf(all_names[n], choose_string) > -1) && (except_file == false || indexOf(all_names[n], except_string) == -1)) { // increment file counter file_count++; // Get the file path file_path = input_dir + all_names[n]; // Store components of the file name file_name = File.getName(file_path); file_namearray = getFileExtension(file_name); file_shortname = file_namearray[0]; print(" Input file #" + file_count + "/" + file_number + ": " + file_name); out_title = file_name; // Open the image file Ext.setGroupFiles("false"); Ext.setId(file_path); Ext.getEffectiveSizeC(channel_count); print("Bio-Formats Id Set"); run("Bio-Formats Importer", "open=[" + file_path + "] " + "view=[Standard ImageJ]" + " color_mode=Grayscale stack_order=Default"); print("Bio-Formats Importer launched"); file_title = getTitle(); file_id = getImageID(); if (endsWith(file_shortname, "3D") == true && astigmatism == true) { is3D = true; TS_dim = "3D"; } else { is3D = false; TS_dim = "2D"; } // Localization run("Run analysis", "filter=[Wavelet filter (B-Spline)] scale=" + wave_scale + " order=" + wave_order + " detector=[Local maximum] connectivity=8-neighbourhood threshold=" + detec_thresh + "*std(Wave.F1) estimator=[Phasor Fitting] astigmatism=" + is3D + " fitradius=" + phasor_radius + " calibrationpath=[" + calib_path + "] renderer=[No Renderer]"); // Count the new Loc number n_Locs = eval("script", "importClass(Packages.cz.cuni.lf1.lge.ThunderSTORM.results.IJResultsTable); var rt = IJResultsTable.getResultsTable(); rows = rt.getRowCount();"); n_LocK = round(n_Locs / 1000); // Export locaization table to ThunderSTORM csv file output_title = file_shortname + "_" + n_LocK + "K_TS" + TS_dim + output_ext; output_path = output_dir + output_title; if (is3D == false) { run("Export results", "floatprecision=1 filepath=[" + output_path + "] fileformat=[CSV (comma separated)] intensity=true offset=true saveprotocol=false x=true sigma2=false y=true sigma1=false z=false bkgstd=true id=false frame=true"); } else if (is3D == true) { run("Export results", "floatprecision=1 filepath=[" + output_path + "] fileformat=[CSV (comma separated)] intensity=true offset=true saveprotocol=false x=true sigma2=false y=true sigma1=false z=true bkgstd=true id=false frame=true"); } print(" Output file:" + output_title); selectImage(file_id); close(); Ext.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 *************** // Restore settings restoreSettings(); showStatus("Batch Localize Phasor finished"); //Time counter stop_time = getTime(); total_time = stop_time - start_time; print(""); print("*** Batch Localize Phasor end after " + total_time / 1000 + " s ***\n\n\n"); return output_dir; } //*************** Functions *************** function getFileExtension(Name) { nameparts = split(Name, "."); shortname = nameparts[0]; if (nameparts.length > 2) { for (k = 1; k < nameparts.length - 1; k++) { shortname += "." + nameparts[k]; } } extname = "." + nameparts[nameparts.length - 1]; namearray = newArray(shortname, extname); return namearray; }