-
Notifications
You must be signed in to change notification settings - Fork 0
/
GRatioOfManualOuter_MayelinatedAxons.ijm
359 lines (334 loc) · 11.6 KB
/
GRatioOfManualOuter_MayelinatedAxons.ijm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/* GRatioOfManualOuter_MayelinatedAxons.ijm
*
* By: Ofra Golani, March 2017
* For: Anya Vainshtein, Christina Katanov, Ori Peles Lab
* Usage Instructions
* ==================
* This macro is the Third step for semi-manual g-ratio Quantification:
* For the opened image file, assuming:
* 1) Inner Mayelinated areas were automatically found using "CountmayelinatedAxons.ijm",
* and hence related Roi (FileName"_InnerOuterRoiSet.zip") file and scaled file are saved under the Results subfolder
* 2) Outer Rois were manually added to the RoiManager and saved in Results\FileName"_InnerOuterRoiSet.zip"
* see: ManualEditingInstructions_ForGRatio.txt
*
* Workflow
* =========
* - Look under Results subfolder and open FileName_InnerOuterRoiSet.zip
* - Go over Rois and count the inner / outer ones: inner Rois start with I-
* for each Inner Roi - find the index of the Roi in the Count Mask table
* For each outer Roi
* - find the matching Inner Roi
* - measure Inner area, outer area area ratio, calculate effective radius + radius ratio
*
* Output
* =========
* 1) Table of results with the following values for each axon
* Label of Inner & Outer Rois,
* Area of Inner and Outer Roi
* effective diameter of Inner and Outer Roi
* G-Ratio based on Area and on effective diameter (inner/outer)
* 2) Roi file with final Inner/Outer Rois - FileName"_FinalInnerOuterRoiSet.zip"
* 3) Overlay of the Final Rois - FinalName"_FinalInnerOuterOverlay.tif"
* Inner Roi is colored in cyan by default (controlled by InnerRoiColor)
* Outer Roi is colored in red by default (controlled by OuterRoiColor)
*/
//-------------------------------------------------------------------------------------//
// Parameters
//-------------------------------------------------------------------------------------//
defaultKnownDist = 2;
defaultKnownUnit = "umunit"
InnerRoiColor= "cyan";
OuterRoiColor= "red";
cleanup_flag = 1;
//-------------------------------------------------------------------------------------//
// Initialization
//-------------------------------------------------------------------------------------//
if (isOpen("Results"))
{
selectWindow("Results");
run("Close");
}
roiManager("reset");
if (isOpen("Summary"))
{
selectWindow("Summary");
run("Close"); // To close non-image window
}
if (isOpen("Log"))
{
selectWindow("Log");
run("Close"); // To close non-image window
}
setTool("hand"); // To make sure the user doesn't create another ROI
//-------------------------------------------------------------------------------------//
// Open related files
//-------------------------------------------------------------------------------------//
origIm=getImageID();
orig_name = getTitle();
orig_name_no_ext = replace(orig_name, ".tif", "");
orig_name_no_ext = replace(orig_name_no_ext, " ", "_");
orig_name_no_ext = replace(orig_name_no_ext, ".", "_");
origFolder = getInfo("image.directory");
resDir = origFolder + File.separator + "Results" + File.separator;
// scale the original image
scaleImage(orig_name, origIm, resDir, defaultKnownDist, defaultKnownUnit);
roiManager("Open", resDir+orig_name_no_ext+"_InnerOuterRoiSet.zip");
//-------------------------------------------------------------------------------------//
// Create label mask using updated inner ROIs only , reload all ROis afterward
//-------------------------------------------------------------------------------------//
nRoi = roiManager("Count");
for (n = nRoi-1; n >= 0; n--)
{
roiManager("Select",n);
roiName=call("ij.plugin.frame.RoiManager.getName", n);
if (startsWith(roiName, "O-"))
{
roiManager("Delete");
}
}
createLabelMaskFromRoiManager(origIm);
rename("InnerRoiCountMask");
labelMaskIm=getImageID();
roiManager("reset");
roiManager("Open", resDir+orig_name_no_ext+"_InnerOuterRoiSet.zip");
roiManager("Measure");
//-------------------------------------------------------------------------------------//
// Find Matched ROIs of Myelin (Outer ROI) and Axon (Inner ROI)
//-------------------------------------------------------------------------------------//
selectImage(labelMaskIm);
roiManager("Deselect");
roiManager("Show None");
getStatistics(areaCM, meanCM, minCM, maxCM);
nRoi = roiManager("Count");
roiTypeA = newArray(nRoi); // 0=inner, 1=outer
roiNameA = newArray(nRoi); // name of each Roi
roiNewNameA = newArray(nRoi); // new name of each Roi - with prefix of Rnnnn_ where nnnn stands for the pair number
roiActiveA = newArray(nRoi); // is Roi Used either inner/outer
countMaskValA = newArray(nRoi); // countMask value of inner idx
roiAreaA = newArray(nRoi); // Roi Area
outerRoiIdx = newArray(nRoi); // Indexes of all outer Rois only
innerRoiIdx = newArray(nRoi); // Indexes of all inner Rois only
nInnerRoi = 0;
nOuterRoi = 0;
for (n = 0; n < nRoi; n++)
{
roiManager("Select",n);
roiName=call("ij.plugin.frame.RoiManager.getName", n);
roiNameA[n] = roiName;
if (startsWith(roiName, "I-"))
{
roiTypeA[n] = 0; // Inner
innerRoiIdx[nInnerRoi] = n;
nInnerRoi++;
} else // Outer Roi
{
roiTypeA[n] = 1; // Outer
outerRoiIdx[nOuterRoi] = n;
nOuterRoi++;
}
// find most frequent non-zero value of the countMask within the given outer Roi
nBins = maxCM + 10;
getHistogram(values, counts, nBins, 0, nBins);
maxCount = 0;
maxVal = -1; // to enable checking for validity
for (j = 1; j < nBins; j++)
{
if (counts[j] > maxCount)
{
maxCount = counts[j];
maxVal = values[j];
}
}
countMaskValA[n] = maxVal; // the +1 is needed as we started the hist from 1
roiAreaA[n] = getResult("Area", n);
}
//-------------------------------------------------------------------------------------//
// Print out GRatio for all outer Rois and save the table
//-------------------------------------------------------------------------------------//
saveAs("Results", resDir+orig_name_no_ext+"_AllRoiResults.csv");
IJ.renameResults("Results",orig_name_no_ext+"_AllRoiResults.csv"); // rename results table for saving it from being overwritten by other "Results"
nPairs = 0;
for (n = 0; n < nOuterRoi; n++)
{
outerIdx = outerRoiIdx[n];
outCountMaskVal = countMaskValA[outerIdx];
found = 0;
if (outCountMaskVal >= 0)
{
m = 0;
innerIdx = 0;
do {
id = innerRoiIdx[m];
inCountMaskVal = countMaskValA[id];
if (inCountMaskVal == outCountMaskVal)
{
innerIdx = id;
found = 1;
}
m++;
} while ( (m < nInnerRoi) && (found==0))
}
if (found==1)
{
nPairs++;
prefix = "R"+pad(nPairs,4,0)+"-";
roiNewNameA[outerIdx] = prefix + roiNameA[outerIdx];
roiNewNameA[innerIdx] = prefix + roiNameA[innerIdx];
roiActiveA[outerIdx] = 1;
roiActiveA[innerIdx] = 1;
outerAreaRoiGR = roiAreaA[innerIdx] / roiAreaA[outerIdx];
effeciveOuterDiameter = 2 * sqrt(roiAreaA[outerIdx] / PI);
effeciveInnerDiameter = 2 *sqrt(roiAreaA[innerIdx] / PI);
outerDiameterRoiGR = effeciveInnerDiameter / effeciveOuterDiameter;
print(outerIdx, innerIdx, prefix,", NewName:Outer=",roiNewNameA[outerIdx],", NewName:Inner=", roiNewNameA[innerIdx]);
outName=roiNewNameA[outerIdx];
inName=roiNewNameA[innerIdx];
setResult("OuterLabel", nResults, outName);
setResult("InnerLabel", nResults-1, inName);
setResult("outerCountMaskValue", nResults-1, countMaskValA[outerIdx]);
setResult("innerCountMaskValue", nResults-1, countMaskValA[innerIdx]);
setResult("InnerArea", nResults-1, roiAreaA[innerIdx]);
setResult("OuterArea", nResults-1, roiAreaA[outerIdx]);
setResult("AreaGRatio", nResults-1, outerAreaRoiGR);
setResult("eInnerDiameter", nResults-1, effeciveInnerDiameter);
setResult("eOuterDiameter", nResults-1, effeciveOuterDiameter);
setResult("DiameterGRatio", nResults-1, outerDiameterRoiGR);
}
else
{
roiActiveA[outerIdx] = 0;
}
}
saveAs("Results", resDir+orig_name_no_ext+"_FinalResults.csv");
//-------------------------------------------------------------------------------------//
// Delete all non-relevant Rois, and save the Rois of inner/Outer only
//-------------------------------------------------------------------------------------//
for (n = nRoi-1; n >= 0; n--)
{
roiManager("Select",n);
if (roiActiveA[n] == 0)
{
roiManager("Delete");
}
else
{
if (startsWith(roiNameA[n], "I-"))
{
roiManager("Set Color", InnerRoiColor);
roiManager("Set Line Width", 2);
}
else // Outer Roi
{
roiManager("Set Color", OuterRoiColor);
roiManager("Set Line Width", 2);
}
roiManager("rename", roiNewNameA[n]);
}
}
// sort the Rois which have new names now, to put inner and outer Rois together
roiManager("sort");
roiManager("Save", resDir+orig_name_no_ext+"_FinalInnerOuterRoiSet.zip");
selectImage(origIm);
roiManager("Deselect");
roiManager("Show None");
roiManager("Show All without labels");
run("Flatten");
saveAs("Tiff", resDir+orig_name_no_ext+"_FinalInnerOuterOverlay.tif");
flatIm = getImageID();
//-------------------------------------------------------------------------------------//
// Close newly opened images and tables
//-------------------------------------------------------------------------------------//
if (cleanup_flag == 1)
{
if (isOpen(orig_name_no_ext+"_AllRoiResults.csv"))
{
selectWindow(orig_name_no_ext+"_AllRoiResults.csv");
run("Close"); // To close non-image window
}
if (isOpen("Summary"))
{
selectWindow("Summary");
run("Close"); // To close non-image window
}
if (isOpen("Results"))
{
//print("Results table will be closed");
selectWindow("Results");
run("Close");
}
selectImage(labelMaskIm);
close();
selectImage(flatIm);
close();
roiManager("reset");
}
// =========================== Helper Functions ======================================================
// scaleImage : read a scale image if exist, otherwise ask the user to draw a line to scale the image
function scaleImage(origName, origIm, resDir, defaultKnownDist, defaultKnownUnit)
{
origName_noExt = replace(origName, ".tif", "");
scaledName = origName_noExt+"_scaled.tif";
if (File.exists(resDir+scaledName))
{
// Read scaled image
open(resDir+scaledName);
scaledOrigIm=getImageID();
// get the scale
getVoxelSize(pixelWidth, pixelHeight, pixelDepth, pixelUnit);
selectImage(origIm);
setVoxelSize(pixelWidth, pixelHeight, pixelDepth, pixelUnit);
selectImage(scaledOrigIm);
close();
} else
{
// Scale the image
selectImage(origIm);
run("Set Scale...", "distance=0 known=0 pixel=1 unit=pixel");
setTool("line");
waitForUser("Measure Scalebar Done ?");
getLine(x1, y1, x2, y2, lineWidth);
getPixelSize(unit, pw, ph);
x1*=pw; y1*=ph; x2*=pw; y2*=ph;
dx = x2-x1; dy = y2-y1;
length = sqrt(dx*dx+dy*dy);
print("dx:", dx, ", Length:", length, unit);
// get the known dist
UserKnownDist = getNumber("Known Distance (um)?", defaultKnownDist);
run("Set Scale...", "distance="+dx+" known="+UserKnownDist+" pixel=1 unit="+defaultKnownUnit);
run("Select None");
setTool("hand");
// Save scaled image
run("Duplicate...", "title="+scaledName);
saveAs("Tiff", resDir + scaledName);
close();
}
} // end of scaleImage
// createLabelMaskFromRoiManager - Create Labeled Image based on ROI Manager, apply scaling of the original image
function createLabelMaskFromRoiManager(origIm)
{
selectImage(origIm);
getVoxelSize(width, height, depth, unit);
newImage("Labeling", "16-bit black", getWidth(), getHeight(), 1);
for (index = 0; index < roiManager("count"); index++) {
roiManager("select", index);
setColor(index+1);
fill();
}
// apply scaling of original image
setVoxelSize(width, height, depth, unit);
resetMinAndMax();
run("glasbey");
}
// Add leading zeros to number
function pad(a, left, right)
{
while (lengthOf(""+a)<left) a="0"+a;
if (right > 0)
{
separator=".";
while (lengthOf(""+separator)<=right) separator=separator+"0";
return ""+a+separator;
}
else
return ""+a;
}