// F-TranslateABL_TS.js script function by Christophe Leterrier // Translate localizations between Abbelight text file and ThunderSTORM csv importClass(Packages.java.io.File); importClass(Packages.java.io.FileReader); importClass(Packages.java.io.FileWriter); importClass(Packages.java.io.BufferedReader); importClass(Packages.java.io.BufferedWriter); importClass(Packages.ij.IJ); importClass(Packages.java.lang.Double); // Parameters // inPath: input file path // outDir: output directory path // pxSize: size of pixel on raw image sequence (for coordinates) // ppc: photons per count to translate camera ADU to photons (for intensity) function TranslateABLTS(inPath, outDir, pxSize, ppc){ // Separators var inSep = " "; // input separator var sep = ","; // output separator // Fields of the input header var inHeaderList = ["x_EPI (subpix)","y_EPI (subpix)","Wx_EPI","Wy_EPI","Intensity_EPI","Background_EPI"]; // var outHeader2DList = ["\"frame\"","\"x [nm]\"","\"y [nm]\"","\"sigma [nm]\"","\"intensity [photon]\"","\"offset [photon]\"","\"bkgstd [photon]\"","\"uncertainty_xy [nm]\"", "\"chi2\"", "\"detections\""]; var outHeader2DList = ["\"frame\"","\"x [nm]\"","\"y [nm]\"","\"sigma [nm]\"","\"intensity [photon]\"","\"bkgstd [photon]\""]; // Correspondance /* 2D case: x [nm] = x_EPI (subpix) [0] * pxSize y [nm] = y_EPI (subpix) [1] * pxSize sigma [nm] = average(Wx_EPI, Wy_EPI) in nm = sqrt([2]^2 +[3]^2) * pxSize , if Wx are SD - if FWHM need to divide by Wx and Wy by 2.35 uncertainty_xy [nm] should be calculated from Intensity_EPI [4] but how? bkgstd [photon] can be obtained form Background_EPI [5]? but now it's constant at 1. */ // Define input files, folder, open it etc. var inFile = new File(inPath); var inName = inFile.getName(); var inNameExt = getExt("" + inName); var br = new BufferedReader(new FileReader(inFile)); IJ.log(" inName: " + inName); // Pass the header lines (####) for (var i = 0; i < 11; i++) { br.readLine(); } // Columns header (right now labels are separated by coma+space ", " with "# " at the beginning 0_0) var inHeader = br.readLine(); //IJ.log("inHeader: " + inHeader); // hard coding the column numbers for used variables var xIndex = 0; var yIndex = 1; var sdxIndex = 2; var sdyIndex = 3; var intIndex = 4; var bgIndex = 5; // Define output extension and header for 2D var outSuffix = "_TS2D"; var outHeaderList = outHeader2DList; // Generate output name and path, open file writer var outName = inName.replace("." + inNameExt[1], outSuffix + ".csv"); if (outName == inName) outName = inNameExt[0] + outSuffix + "." + inNameExt[1]; // in the case we are processing CSV files var outPath = outDir + outName; var outFile = new File(outPath); if (!outFile.exists()) { outFile.createNewFile(); } var bw = new BufferedWriter(new FileWriter(outFile)); IJ.log(" outName: " + outName); // Write the header var outHeader = makeLineFromArray(outHeaderList, sep); bw.write(outHeader); bw.newLine(); // Write the output file line by line while ((inLine = br.readLine()) != null) { // Processing of individual values from input file lines, split by inSep var inCells = inLine.split(inSep); var fOut = 1; // no frame info in file var xOut = (parseFloat(inCells[xIndex]) * pxSize).toFixed(1); var yOut = (parseFloat(inCells[yIndex]) * pxSize).toFixed(1); var sigma1Out = parseFloat(inCells[sdxIndex] * pxSize); var sigma2Out = parseFloat(inCells[sdyIndex] * pxSize); var sigmaOut = (Math.sqrt((sigma1Out * sigma1Out) + (sigma2Out * sigma2Out))).toFixed(1); var intOut = (parseFloat(inCells[intIndex]) * ppc).toFixed(0); var bgstdOut = (parseFloat(inCells[bgIndex]) * ppc).toFixed(1); // Assemble output line var outLineArray = [fOut, xOut, yOut, sigmaOut, intOut, bgstdOut]; var outLine = makeLineFromArray(outLineArray, sep); // Write new line bw.write(outLine); bw.newLine(); } br.close(); bw.close(); } function getExt(filestring){ var namearray = filestring.split("."); var shortname = ""; for (var f = 0; f < namearray.length - 1; f++) { shortname = shortname + namearray[f]; } return [shortname, namearray[namearray.length - 1]]; } function arrayFind(a, s){ for (var i = 0; i < a.length; i++) { testS = a[i]; if (testS.indexOf(s)>-1 && testS.indexOf(s)<3) return i; } return -1; } function makeLineFromArray(ar, se) { ol = "" + ar[0]; for (t = 1; t < ar.length; t++) { ol = ol + se + ar[t]; } return ol; }