-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lyso_distance.ijm
236 lines (208 loc) · 6.7 KB
/
Lyso_distance.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
/*
* LYSOSOME DISTANCE TO NUCLEUS v1.0
* ---------------------------------
*
* This macro calculates the distance from each lysosome (or similar puncta)
* to the center and average edge of the nucleus.
*
* Cell and nuclei segmentation is performed manually (via manual ROIs).
* Lysosome segmentation is automated via a pre-defined threshold.
*
* A multichannel calibrated image is required
* (one channel to segment cell, another to segment lysosomes).
*
* If the image is a z-stack, a Maximal Intensity Projection will be calculated.
* (the script is optimized for single z-planes, though).
*
* The script has been optimized for in vivo immunostaining, where lysosomal labelling can be noisy.
* Hence, the macro includes a Difference of Gaussians filter to enhance contrast.
* (I will make this optional in the future).
*
*
* Federico N. Soria (January 2021)
* federico.soria@achucarro.org
*
*/
//INITIALIZATION
run("Collect Garbage");
Overlay.remove;
if (nImages==0) {
exit("No images open. Please open an image");
}
name=getTitle();
getDimensions(width, height, channels, slices, frames);
if (channels==1) {
exit("Image is not multichannel. Please open a multichannel image");
}
getPixelSize(unit, pw, ph);
run("Set Measurements...", "area centroid fit redirect=None decimal=2");
setBackgroundColor(0, 0, 0);
setForegroundColor(255, 255, 255);
//GUI
ch_list=newArray(channels);
for (i=0; i<channels; i++){
ch_list[i]=""+i+1+"";
}
thres_list = getList("threshold.methods");
Dialog.create("Lysosome distance to Nucleus for FIJI");
Dialog.addChoice("Channel for reference ROI (cell)", ch_list, "1");
Dialog.addString("Name for ref channel", "TH");
Dialog.addMessage("\n");
Dialog.addChoice("Channel for lysosome labelling", ch_list, "2");
Dialog.addString("Name for coloc channel", "LAMP2");
Dialog.addChoice("Threshold for coloc channel", thres_list, "Default");
Dialog.addMessage("\n");
Dialog.addNumber("Minimum sigma for DifGaus filter", 1);
Dialog.addNumber("Maximum sigma for DifGaus filter", 50);
Dialog.addCheckbox("Save binary images, values an ROIs to disk?", false);
Dialog.addMessage("\n(c) Federico N. Soria (federico.soria@achucarro.org)\nJan 2021");
Dialog.show();
cell_ch=Dialog.getChoice();
cell_name=Dialog.getString();
lyso_ch=Dialog.getChoice();
lyso_name=Dialog.getString();
thres_lyso=Dialog.getChoice();
gmin=Dialog.getNumber();
gmax=Dialog.getNumber();
save_files=Dialog.getCheckbox();
//DIRECTORY CREATION
if (save_files==true) {
dir=getDirectory("Choose a folder to save Result files.");
//DIRECTORY FOR IMAGES
dir_im = dir + "Images" + File.separator;
if (File.exists(dir_im)==false) {
File.makeDirectory(dir_im);
}
//DIRECTORY FOR VALUES
dir_val = dir + "Values" + File.separator;
if (File.exists(dir_val)==false) {
File.makeDirectory(dir_val);
}
//DIRECTORY FOR ROIS
dir_roi = dir + "ROIs" + File.separator;
if (File.exists(dir_roi)==false) {
File.makeDirectory(dir_roi);
}
}
//MIP CREATION
print("Analyzing "+ name + " ...");
run("Select None");
if (slices>1) {
run("Z Project...", "projection=[Max Intensity]");
print("MIP created.");
}
run("Duplicate...", "title=TEMP duplicate");
//PREPROCESSING
run("Grays");
difgaus(gmin, gmax, "DifGaus");
selectWindow("DifGaus");
run("Split Channels");
n=0;
do{
n=n+1;
//CLEAN FROM PREVIOUS
roiManager("reset");
roiManager("Show None");
run("Select None");
run("Clear Results");
//CELL ROI
selectWindow("C"+cell_ch+"-DifGaus");
run("Enhance Contrast", "saturated=0.35");
setTool("polygon");
waitForUser("Draw a selection precisely around the cell soma and press OK");
roiManager("Add");
roiManager("select", 0);
roiManager("rename", "cell soma");
run("Select None");
//NUCLEAR ROI
waitForUser("Now draw a selection precisely around the nucleus and press OK");
run("Fit Spline"); //To calculate max and min radius it will consider it an ellipse
roiManager("Add");
roiManager("Select", 1);
roiManager("rename", "cell nucleus");
run("Measure");
r_major = getResult("Major")/2; //Major radius of the fitted ellipse
r_minor = getResult("Minor")/2; //Minor radius of the fitted ellipse
radius = (r_major+r_minor)/2; //Average radius, used to calculate distance to border
x0 = (getResult("X"))*(1/pw);
y0 = (getResult("Y"))*(1/ph);
//LYSOSOME SEGMENTATION
selectWindow("C"+lyso_ch+"-DifGaus");
run("Duplicate...", " ");
rename(name+"_"+lyso_name);
puncta ("Default dark", 0, 10);
//LYSOSOME DISTANCE
ROIcount=roiManager("count");
counter=1;
for (i=2;i<ROIcount;i++) {
roiManager("select", i);
getSelectionBounds(x1, y1, width1, height1);
dx=(x0 - x1)*pw;
dy=(y0 - y1)*ph;
d_to_center = sqrt(dx*dx + dy*dy); //euclidean distance to center
d_to_border = d_to_center - radius; //distance to border of nucleus
Overlay.drawLine(x0, y0, (x1+width1/2), (y1+height1/2));
Overlay.show;
Overlay.setStrokeWidth(2);
myTable(name,counter,cell_name,lyso_name,unit,d_to_center,d_to_border);
counter=counter+1;
}
//SAVE SEGMENTED IMAGE AND ROIs
if (save_files==true) {
selectWindow(name+"_"+lyso_name);
saveAs("tiff", dir_im+File.separator+"BIN_"+name+"_"+lyso_name+"_"+n);
roiManager("save", dir_roi+"ROI_"+name+"_"+n+".zip");
}
cont=getBoolean("Do you want to analyze another cell?");
}while(cont==true);
print((counter-1)+" lysosomes analysed");
run("Tile");
//SAVE RESULTS
if (save_files==true) {
selectWindow("Quantification");
saveAs("Text", dir_val+File.separator+"DISTANCES_"+name+".csv");
print("Result files saved in "+dir);
}
//EXIT
close_images = getBoolean("Close all images?");
if (close_images==true) {
run("Close All");
}
print("DONE!");
print("");
//FUNCTION: DoG FILTERING
function difgaus (min, max, finalname){
run("Duplicate...", "title=min duplicate");
run("Gaussian Blur...", "sigma="+min+" stack");
run("Duplicate...", "title=max duplicate");
run("Gaussian Blur...", "sigma="+max+" stack");
imageCalculator("Subtract stack", "min","max");
selectWindow("max");
close();
selectWindow("min");
rename(finalname);
}
//FUNCTION: PUNCTA SEGMENTATION
function puncta (autothres, ROI, size){
setAutoThreshold(autothres);
run("Convert to Mask");
roiManager("Select", ROI);
setBackgroundColor(0, 0, 0);
run("Clear Outside");
run("Select None");
run("Watershed");
run("Analyze Particles...", "size="+size+"-Infinity pixel add");
}
//FUNCTION: CUSTOM TABLE
function myTable(a,b,c,d,e,f,g){
title1="Quantification";
title2="["+title1+"]";
if (isOpen(title1)){
print(title2, a+"\t"+b+"\t"+c+"\t"+d+"\t"+e+"\t"+f+"\t"+g);
}
else{
run("Table...", "name="+title2+" width=800 height=400");
print(title2, "\\Headings:File\tLyso#\tCell type\tLyso marker\tUnit of D\tD to nucleus center\tD to nucleus border");
print(title2, a+"\t"+b+"\t"+c+"\t"+d+"\t"+e+"\t"+f+"\t"+g);
}
}