// Get_by_Conditions.js script by Christophe Leterrier // Takes a results table (with image name in a column, containing a string for the condition) // Outputs one of the columns as multiple columns, one by condition // 15-03-23 importClass(Packages.ij.io.OpenDialog) importClass(Packages.java.io.File) importClass(Packages.ij.IJ); importClass(Packages.ij.gui.GenericDialog); importClass(Packages.java.io.File); importClass(Packages.ij.measure.ResultsTable); // Default variables fixRepN_def = false; repN_def = 10; regexC_def = "C\\d"; // Dialog var od = new OpenDialog("Choose a Results txt file", ""); var inDirectory = od.getDirectory(); var inName = od.getFileName(); var inPath = inDirectory + inName; IJ.log("\nInput file path:" + inPath); // Open the file var inFile = new File(inPath); var inString = IJ.openAsString(inPath); // Get the columns titles var headerArray = getHeader(inString); var nameTitle_def = headerArray[3]; var resTitle_def = headerArray[13]; // Store the file as an array var inArray = getArray(inString); // Options dialog var gd = new GenericDialog("Get by Conditions: options"); gd.addCheckbox("Fixed number of replicates", fixRepN_def); gd.addNumericField(" ", repN_def, 0, 2, " replicates"); gd.addChoice("Image Name column", headerArray, nameTitle_def); gd.addStringField("Condition regex (escaped)", regexC_def); gd.addChoice("Results column", headerArray, resTitle_def); gd.showDialog(); var fixRepN = gd.getNextBoolean(); var repN = gd.getNextNumber(); var nameTitle = gd.getNextChoice(); var nameIndex = getIndex(headerArray, nameTitle); var regexC = gd.getNextString(); var resTitle = gd.getNextChoice(); var resIndex = getIndex(headerArray, resTitle); if (gd.wasOKed()) { IJ.log("Image Name column: " + nameTitle + " (column #" + nameIndex + ")"); IJ.log("Results column: " + resTitle + " (column #" + resIndex + ")"); var rgx = new RegExp(regexC); var nameCol = getCol(inArray, nameIndex); var resCol = getCol(inArray, resIndex); var rt = new ResultsTable(); var p = 0; for (var i = 0; i < nameCol.length; i++) { var currMatch = nameCol[i].match(rgx)[0]; if (i > 0) { prevMatch = nameCol[i-1].match(rgx)[0]; if (currMatch != prevMatch) p = 0; } rt.setValue(currMatch, p, resCol[i]); p++; } for (var c = 0; c < rt.getLastColumn() + 1; c++) { l = rt.size() - 1; while (rt.getValue(c , l) == 0) { rt.setValue(c, l, Number.NaN); l--; } } // show the Profiles Table rt.showRowNumbers(false); outName = inName.replace(".txt", "_" + resTitle + ".txt"); rt.show(outName); } function getHeader(st){ var rows = st.split("\n"); var h = rows[0].split("\t"); return h; } function getArray(st) { var rows = st.split("\n"); var nrows = rows.length; var cells = rows[1].split("\t"); var ncols = cells.length; var mt = new Array (nrows-1); for (var i = 1; i < nrows; i++) { var rowArray = new Array (ncols); cells = rows[i].split("\t"); for (var j = 0; j < ncols; j++) { // Other columns are numbers that are parsed rowArray[j] = cells[j]; } mt[i-1] = rowArray; } return mt; } function getIndex(arr, str) { for (var f = 0; f < arr.length; f++) { if (arr[f] == str) return f; } return -1; } function getCol(a, j) { var nrows = a.length; var col = new Array(nrows); for (var i = 0; i < nrows; i++) { col[i] = a[i][j]; } return col; }