// F-ZOLA_TS.js script function by Christophe Leterrier // ZOLA localization file to proper ThunderSTORM file // 06/01/2018 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 acquired sequence pixel size in nm // sigma PSF SD in nm // xFactor dilatation in X to compensate cylindrical lens (measured on microscope) // zFactor factor for uncertainty in Z from uncertainty in XY function ZolaTS(inPath, outDir, xFactor) { // separators (csv files) var inSep = ","; var sep = ","; var idHeader = "id"; var fHeader = "frame"; var xHeader = "x [nm]"; var yHeader = "y [nm]"; var zHeader = "z [nm]"; var iHeader = "intensity"; var bHeader = "background"; var cHeader = "chi2"; var cxHeader = "crlbX"; var cyHeader = "crlbY"; var czHeader = "crlbZ"; // var dHeader = "detections"; // 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); // Get the header line and find index of Z, intensity, background and x coordinate columns var inHLine = br.readLine(); var inHeader = inHLine.split(inSep); var idIndex = arrayFind(inHeader, idHeader); var fIndex = arrayFind(inHeader, fHeader); var xIndex = arrayFind(inHeader, xHeader); var yIndex = arrayFind(inHeader, yHeader); var zIndex = arrayFind(inHeader, zHeader); var iIndex = arrayFind(inHeader, iHeader); var bIndex = arrayFind(inHeader, bHeader); var cIndex = arrayFind(inHeader, cHeader); var cxIndex = arrayFind(inHeader, cxHeader); var cyIndex = arrayFind(inHeader, cyHeader); var czIndex = arrayFind(inHeader, czHeader); // Generate output name and path, open file writer var outName = inName.replace("TS3D", "TS_TS3D"); if (outName == inName) outName = inNameExt[0] + "_TS3D." + inNameExt[1]; 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); // New header var idHTS = "id"; var fHTS = "frame"; var xHTS = "x [nm]"; var yHTS = "y [nm]"; var zHTS = "z [nm]"; var s1HTS = "sigma1 [nm]"; var s2HTS = "sigma2 [nm]"; var iHTS = "intensity [photon]"; var oHTS = "offset [photons]"; var bHTS = "bkgstd [photon]"; var uxyHTS = "uncertainty_xy [nm]"; var uzHTS = "uncertainty_z [nm]"; var dHTS = "detections"; var HTS = "" + idHTS +sep+ fHTS +sep+ xHTS +sep+ yHTS +sep+ zHTS +sep+ iHTS +sep+ bHTS +sep+ uxyHTS +sep+ uzHTS +sep ; bw.write(HTS); bw.newLine(); // Write the output file line by line while ((inLine = br.readLine()) != null) { var inCells = inLine.split(inSep); // var idOut = parseInt(inCells[idIndex]); // var fOut = parseInt(inCells[fIndex]); // correct distortion in X coordinate var xIn = Double.parseDouble(inCells[xIndex]); var xInC= xIn * xFactor; var xOut = xInC.toFixed(1); var yIn = Double.parseDouble(inCells[yIndex]); var yOut = yIn.toFixed(1); var zIn = Double.parseDouble(inCells[zIndex]); var zOut = zIn.toFixed(1); var iIn = Double.parseDouble(inCells[iIndex]); var iOut = iIn.toFixed(1); var bIn = Double.parseDouble(inCells[bIndex]); var bOut = bIn.toFixed(1); // Calculate uncertainty_xy as the harmonic average of crlbX and crlbY var cxIn = Double.parseDouble(inCells[cxIndex]); var cyIn = Double.parseDouble(inCells[cyIndex]); var uxyIn = Math.sqrt(cxIn * cyIn); var uxyOut = uxyIn.toFixed(1); var czIn = Double.parseDouble(inCells[czIndex]); var uzOut = czIn.toFixed(1); // Generate new line // outLine = "" + idOut +sep+ fOut +sep+ xOut +sep+ yOut +sep+ zOut +sep+ iOut +sep+ bOut +sep+ uxyOut +sep+ uzOut +sep ; outLine = "" + inCells[idIndex] +sep+ inCells[fIndex] +sep+ xOut +sep+ yOut +sep+ zOut +sep+ iOut +sep+ bOut +sep+ uxyOut +sep+ uzOut +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){ index = -1; for (var i = 0; i < a.length; i++) { if (a[i] == s) index = i; } return index; }