/* ExtractSpotPairs.txt N. Vischer 28.04.19, 11:56 https://sils.fnwi.uva.nl/bcb/objectj/examples/utils/ExtractSpotPairs/ Detects close fluorescent spot pairs BSpot and DSpot that are already marked in a Coli_Inspector project file Where BSpot and CSpot inside a cell are close, they are defined as a pair. A spot can only be part of one pair. The number of pairs per cell is shown in ObjectJ Results column 'nPairs' The pair distances per cell are shown in ObjectJ Results column 'dist' as space-separated strings. An unlinked table is created with one row per pair. */ macro "Detect Pairs"{ pairDist = 0.2;//maximum distance to call it a pair , default = 0.2 um print("\\Clear"); ojHideResults(); ojSetColumnProperty("*", "visible", 0); ojSetColumnProperty("Axis Dia *Spots", "visible", 1); ojDeleteColumn("nPairs dist"); ojInitColumn("nPairs"); ojInitTextColumn("dist"); ojSetColumnProperty("nPairs", "digits", 0); ojSetColumnProperty("nPairs dist", "color", 0x660088); pxSize = ojGetVoxelSize(1, "x"); for(obj = 1; obj <= ojNObjects(); obj++){ ojSelectObject(obj); nBSpots = ojNItems("BSpot"); nCSpots = ojNItems("CSpot"); nPairs = 0; distStr = "";//accumulates distances pairedC = newArray(nCSpots);//avoid double-usage for(bb = 1; bb <= nBSpots; bb++){ ojSelectItem("BSpot", bb); xb = ojXPos(1); yb = ojYPos(1); minD = 1e6; minCC = 0; for(cc = 1; cc <= nCSpots; cc++){ if(pairedC[cc-1]) continue; ojSelectItem("CSpot", cc); xc = ojXPos(1); yc = ojYPos(1); d = sqrt(pow(xc-xb, 2) + pow(yc-yb, 2)) * pxSize; if(d< minD){ minD = d; minCC = cc; } } //has this BSpot a neighbor? isClose = minD < pairDist; if(isClose){ nPairs++; distStr = distStr + d2s(minD, 2) + " "; //ojSetResult("dist", obj, minD); print("obj=", obj, " dist=", minD); pairedC[minCC-1] = true; } } ojSetResult("nPairs", obj, nPairs); ojSetResult("dist", obj, distStr); } ojRecalculate(); ojShowResults(); close("PairsTable.txt"); Table.create("PairsTable.txt"); row = 0; for(obj = 1; obj <= ojNObjects(); obj++){ s = ojResultString("dist", obj); if(s != ""){ s = split(s); for(jj = 0; jj < s.length; jj++){ Table.set("Obj", row, obj); Table.set("Dist", row++, s[jj]); } } } Table.update; }