-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnFR_ROI_Finder.ijm
229 lines (210 loc) · 5.66 KB
/
SnFR_ROI_Finder.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
//script to find ROIs in a Stack with clear defined peaks
// you need to have two plug-ins "Find Peaks" and "Read and Write to excel"
// get them by clicking on help->update, than "manage update sites" tick there
// BAR and Result to Excel
//Open a stack to be analyzed and run the script :)
// Clear everything to make sure no errors occur
run("Clear Results");
nRois=roiManager("count");
if(nRois>0){
roiManager("reset");
}
//Remember Stack Title and get dimensions, measure mean overall intensity
Dialog.create("Setting your parameters");
Dialog.addNumber("Lenght of the sides of the ROI", 7);
Dialog.addNumber("Intensity increase upon an event (e.g. 10%=0.1)", 0.1);
Dialog.addNumber("Signal to Noise Ratio", 3);
Dialog.addNumber("Minimal number of expected Peaks", 10);
Dialog.show();
dir=getDirectory("Choose where your Rois shall be stored");
SizeofRoi=Dialog.getNumber();
IncreaseFactor=Dialog.getNumber();
SNFactor=Dialog.getNumber();
ExpectedPeaks=Dialog.getNumber();
run("Subtract Background...", "rolling=50 stack");
T=getTitle();
getDimensions(width, height, channels, slices, frames);
Width= width/SizeofRoi;
Height= height/SizeofRoi;
selectWindow(T);
run("Select All");
run("Measure");
MeanofAll= getResult("Mean",0);
run("Clear Results");
//Enter x,y run
u=0;
xRoi=0;
yRoi=0;
Roicount=0;
t=0;
while(t<Height){
while(u<Width){
selectWindow(T);
makeRectangle(xRoi, yRoi, SizeofRoi, SizeofRoi);
roiManager("add");
run("Measure");
MeanofRoi= getResult("Mean", 0);
run("Clear Results");
//test if mean intensity in current Roi is lower than mean overall intensity
//go to next iteration if so
if(MeanofRoi<=MeanofAll){
roiManager("select", Roicount);
roiManager("delete");
xRoi=xRoi+SizeofRoi;
u=u+1;
}
else{
selectWindow(T);
roiManager("select", Roicount);
roiManager("Multi Measure");
selectWindow("Results");
Table.sort("Mean1");
n=0;
o=nResults-1;
Max=0;
Min=0;
Maximum=0;
Minimum=0;
meanMax=0;
meanMin=0;
Amplitude=0;
while(n<ExpectedPeaks){
Max= getResult("Mean1", o);
n= n+1;
Maximum= Maximum+Max;
o=o-1;
}
v=0;
while(v<30){
Min= getResult("Mean1", v);
v=v+1;
Minimum= Minimum+Min;
}
meanMax= (Maximum)/ExpectedPeaks;
meanMin= (Minimum)/30;
Amplitude= (meanMax-meanMin)*0.5;
MinimumIncrease= meanMin*IncreaseFactor;
if(Amplitude<MinimumIncrease){
roiManager("select", Roicount);
roiManager("delete");
xRoi=xRoi+SizeofRoi;
u=u+1;
close("Results");
}
else{
//clean up
close("Resutls");
//determine bin with highest counts in intensity to not fall into a mean value that is artificially high due to good signals
selectWindow(T);
roiManager("select", Roicount);
roiManager("Multi Measure");
run("Distribution...", "parameter=Mean1 or=16 and=0-0");
Plot.getValues(bin, count);
Array.show("Results",bin, count);
selectWindow("Results");
Table.sort("count");
close("Mean1 Distribution");
RealMean=getResult("bin", 15);
Amplitude=meanMax-RealMean;
//do not use Rois with an peak amplitude that is smaller than 10 % of mean intensity
Mini=RealMean*IncreaseFactor;
if(Amplitude<Mini){
roiManager("select", Roicount);
roiManager("delete");
xRoi=xRoi+SizeofRoi;
u=u+1;
close("Results");
}
else{
MeanToMin=(RealMean-meanMin)*SNFactor;
if(Amplitude<MeanToMin){
roiManager("select", Roicount);
roiManager("delete");
xRoi=xRoi+SizeofRoi;
u=u+1;
close("Results");
}
else{
roiManager("select", Roicount);
roiManager("Multi Measure");
roiManager("select", Roicount);
Plot.create("Plot of Results", "x", "Mean1");
Plot.add("Line", Table.getColumn("Mean1", "Results"));
Plot.setStyle(0, "blue,#A0A0FF,1.0,Line");
Plot.show();
SearchAmplitude=Amplitude*0.6;
run("Find Peaks", "min._peak_amplitude=SearchAmplitude min._peak_distance=0 min._value=[] max._value=[] list");
IJ.renameResults("Plot Values", "Results");
MinimumNrPeaks=ExpectedPeaks/3;
Test=0;
Test=getResult("Y1", MinimumNrPeaks);
// AP 230808 changed to provide no 2nd argument in getResult below (gives last value in column)
// 2nd argument was, somewhat inexplicably, '50' before.
Test2=0;
Test2=getResult("Y1");
if(isNaN(Test)){
roiManager("select", Roicount);
roiManager("delete");
xRoi=xRoi+SizeofRoi;
u=u+1;
close("Results");
close("Peaks in Plot of Results");
close("Plot of Results");
}
else{
if(isNaN(Test2)){
close("Peaks in Plot of Results");
roiManager("select", Roicount);
rName= Roi.getName();
selectWindow("Plot of Results");
Plot.freeze(true);
rename(rName);
Roicount=Roicount+1;
xRoi=xRoi+SizeofRoi;
u=u+1;
close("Results");
run("Tile");
}
else{
roiManager("select", Roicount);
roiManager("delete");
xRoi=xRoi+SizeofRoi;
u=u+1;
close("Results");
close("Peaks in Plot of Results");
close("Plot of Results");
}
}// end of else if(Amplitude<MeantoMin)
}//end of else (isNaN(Test))
}//end of else (Test is a real Number, so Image contains sufficient peaks
}
}//end of else (MeanofRoi<=MeanofALL)
} //end of u<width while loop
u=0;
t=t+1;
xRoi=0;
yRoi=yRoi+SizeofRoi;
}//end of t<height while loop
//User should have a look on the tiled traces and manually delete bad ones
title="Decision Time";
message="Inspect tiled traces for outliers, if you found one, check the name of the plot to find it in the Roi Manager and delete it there. Click OK when finished";
close("Results");
waitForUser(title, message);
//run through all rois sequentially and multi measure over stack, write each measurement to excel
nRoi= roiManager("count");
p=0;
run("Set Measurements...", "mean redirect=None decimal=3");
selectWindow(T);
for(p=0; p<nRoi; p++){
roiManager("Select", p);
roiManager("Multi Measure");
run("Read and Write Excel", "no_count_column");
}
roiManager("Show All without labels");
selectWindow(T);
saveAs("Tiff", dir+"/overlay"+T+".tiff");
run("Close All");
roiManager("Deselect");
roiManager("Save", dir+"/RoiSet"+T+".zip");
roiManager("Deselect");
roiManager("delete");