-
Notifications
You must be signed in to change notification settings - Fork 0
/
CRaQ_MultiData_MaxMin.ijm
311 lines (265 loc) · 10.7 KB
/
CRaQ_MultiData_MaxMin.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
//####### CRaQ VERSION
version = "v1.21";
//####### //####### //####### //####### //####### //####### //####### //####### //####### //####### //####### //####### //####### //####### //####### //####### //#######
run("Set Measurements...", "area mean min center feret's redirect=None decimal=3");
run("Colors...", "foreground=black background=black selection=green");
choices=newArray("Unprojected DV files: fast acquisition mode","Unprojected DV files: slow acquisition mode","Projected files","TIF files","Deconvolved DVs");
Dialog.create("Set Channels");
Dialog.addString("Data channel number (use commas (,) to separate multiple inputs","1,2,3",1);
Dialog.addNumber("Reference channel number",1,0,0,"");
Dialog.addNumber("DAPI channel number",99,0,0,"large number --> last channel");
Dialog.addNumber("Total channels",0,0,0," use 0 to auto-detect");
Dialog.addMessage("");
Dialog.addNumber("Camera Bitdepth",12,0,0,"usually 12 or 16");
Dialog.addMessage("");
Dialog.addCheckbox("Chromatic aberration correction using MultiStackReg",1);
Dialog.addCheckbox("Change default parameter settings",0);
Dialog.addCheckbox("Cropped cells",0);
Dialog.addMessage("");
Dialog.show();
DataCh=Dialog.getString();
RefCh=Dialog.getNumber();
DapiCh=Dialog.getNumber();
TotCh=Dialog.getNumber();
CameraBitDepth=Dialog.getNumber();
AutoChromAbCorr=Dialog.getCheckbox();
Change=Dialog.getCheckbox();
CroppedCells=Dialog.getCheckbox();
if (RefCh == DapiCh){
exit("Reference and Data channels should be different from DAPI channel");
}
SatPixVal = 1;
for (b = 0; b < CameraBitDepth; b++) {
SatPixVal *= 2;
}
DataSplit = split(DataCh, ",,"); // using two commas to avoid errors when string ends with comma or consecutive commas are used
DataChArray = newArray(DataSplit.length);
for (i = 0; i < DataSplit.length; i++) {
DataChArray[i] = parseInt(DataSplit[i]);
if (isNaN(DataChArray[i])){
exit("\"" + DataSplit[i] + "\" detected as input for Data channel\n Only integers are allowed in this field\n Multiple inputs can be separated by using a comma (,)");
}
}
Ch = Array.concat(newArray(RefCh,DapiCh),DataChArray);
RMD = newArray(Ch.length); RMD[0] = "Ref"; RMD[1] = "Mask";
for (i = 2; i < RMD.length; i++) {
RMD[i] = "Data_"+d2s(i-1,0);
}
Dialog.create("Change parameter settings");
Dialog.addNumber("Square size",7,0,0,"pixels");
Dialog.addNumber("Minimum Circularity",0.95,2,4,"a.u.");
Dialog.addNumber("Max Feret's Diameter",7,1,3,"pixels");
Dialog.addNumber("Min Centromere Size",4,0,2,"pixel");
Dialog.addNumber("Max Centromere Size",35,0,2,"pixel");
Dialog.addNumber("Threshold Factor",1.11,2,4,"pixel intensity");
Dialog.addNumber("Pixels are considered saturated at: ",92,0,2,"% of camera saturation");
Dialog.addMessage("\nIf known, set the chromatic aberration of the reference channel compared to the data channel.");
Dialog.addMessage("If automatic corrections are used, this should be left at (0,0).");
Dialog.addNumber("Chromatic aberration (horizontal): ",0,0,2,"pixels to right");
Dialog.addNumber("Chromatic aberration (vertical): ",0,0,2,"pixels down");
if (Change == 1)
Dialog.show(); //####################keeps defaults if "Change default" is unchecked
SquareSize=Dialog.getNumber(); corner=(SquareSize-1)/2;
MinCirc=Dialog.getNumber();
MaxFeret=Dialog.getNumber();
MinCentro=Dialog.getNumber();
MaxCentro=Dialog.getNumber();
OtsuUp=Dialog.getNumber();
Saturation = Dialog.getNumber() * SatPixVal / 100;
xCor=Dialog.getNumber();
yCor=Dialog.getNumber();
if (MinCirc >= 1) exit("Minimum circularity was set to" + MinCirc + ".\n Please enter a value between 0 and 1");
if (MinCentro > MaxCentro) exit("Minimum centromere size ("+MinCentro+") may not be larger than maximum centromere size ("+MaxCentro+")");
run("Close All");
roiManager("reset");
dir = getDirectory("Choose Base Directory ");
outf="_OUTPUT";
out=dir+outf+File.separator;
File.makeDirectory(out);
INITIATING_FUNCTION(dir);
close("B&C");
//close("Log");
close("ROI Manager");
run("Close All");
waitForUser("CRaQ done");
///////////////////////////FUNCTIONS///////////////////////////FUNCTIONS///////////////////////////FUNCTIONS///////////////////////////FUNCTIONS///////////////////////////
function INITIATING_FUNCTION(dir) {
print("\\Clear");
if(File.exists(getDirectory("macros")+File.separator+"PrintDateTime.txt")==1) runMacro("PrintDateTime");
else{
getDateAndTime(y, m, dW, dM, h, min, s, ms);
if(min<10) min="0"+min;
print(y,m,dM+"\n"+h+":"+min);
}
print("CRaQ_ Macro version: "+version);
print("Updates will be published on:\nhttps://github.com/DaniBodor/CRaQ/");
print("ImageJ version: "+getVersion);
print("Base Directory: ", dir);
print("Data Channel(s): "+DataCh);
print("Reference Channel: "+RefCh);
print("DAPI Channel: "+DapiCh);
print("");
print ("Square Size: ", SquareSize);
print ("Minimum Circularity: ", MinCirc);
print ("Maximum Ferets Diameter: ", MaxFeret);
print ("Minimum Centromere Size: ", MinCentro);
print ("Maximum Centromere Size: ", MaxCentro);
print ("Threshold Factor: ", OtsuUp);
if (AutoChromAbCorr == 1) answer="Yes"; else answer="No";
print ("Automatic correction of chromatic aberration using MultiStackReg: "+answer);
print ("User-defined chromatic aberration correction: ("+xCor+","+yCor+") [(x,y) difference of reference compared to data]");
print ("CameraBitDepth: ", CameraBitDepth);
print ("Pixel Saturation at: ", Saturation, "arbitrary intensity units");
selectWindow("Log");
saveAs("Text",out+"__logfile.txt");
list = getFileList(dir);
for (i=0; i<list.length; i++) {
if (endsWith(list[i], "/") && indexOf(list[i],outf)<0){
sdir= dir+list[i];
DIRname=substring(list[i],0,lengthOf(list[i])-1);
Table.create("DataTable");
rowOffset = 0;
slist= getFileList(sdir);
for (j=0; j<slist.length; j++) {
if (endsWith(slist[j], ".dv") || endsWith(slist[j], ".tif")){
ImageLoc = sdir+slist[j];
IMname = slist[j];
run("Open...", "open=["+ImageLoc+"] view=[Standard ImageJ] stack_order=Default");
roiManager("reset");
if (TotCh == 0) Stack.getDimensions(WIDTH,HEIGHT, TotCh ,Zdepth,Tframes); // read total chanel info from Metadata; everything else unused
Deco=indexOf(getTitle,"D3D");
TotSlice=nSlices;
run("Properties...", "unit=pixel pixel_width=1 pixel_height=1");
run("Rename...", "title=Image");
if(nSlices>TotCh) run("Z Project...", "projection=[Max Intensity]");
else run("Duplicate...", "title=PRJ duplicate");
run("Rename...", "title=PRJ");
// auto-chromatic aberration correction
if (AutoChromAbCorr == 1){
run("MultiStackReg", "stack_1=PRJ action_1=Align file_1=[] stack_2=None action_2=Ignore file_2=[] transformation=Translation");
run("Z Project...", "projection=[Min Intensity]");
MinPRJ = getTitle();
doWand(0,0);
run("Make Inverse");
selectWindow("PRJ");
run("Restore Selection");
run("Crop");
close(MinPRJ);
}
close("Image");
for (k=0; k<Ch.length; k++){
if(Ch[k]>0){
selectWindow("PRJ");
Stack.setChannel(Ch[k]);
run("Duplicate...", "title="+RMD[k]);
run("Brightness/Contrast...");
resetMinAndMax();
}
}
selectWindow("PRJ");
saveAs("Tiff", out+slist[j]+"__PRJ.tif");
close();
//print("\n=="+slist[j]);
rowOffset = MEASURE_FUNCTION(rowOffset);
if (roiManager("count")>0) {
roiManager("Save",out+slist[j]+"__ROI.zip")
roiManager("reset");
}
run("Close All");
}
}
/* selectWindow("Log");
saveAs("Text",out+"_"+DIRname+".txt");
print("\\Clear") ;
*/
selectWindow("DataTable");
saveAs("Results",out+"_"+DIRname+".csv");
// waitForUser(Table.title);
close("_"+DIRname+".csv");
// waitForUser("_"+DIRname+".csv");
}
}
}
function MEASURE_FUNCTION(rowOffset){
count=1;
if(DapiCh != 0 && CroppedCells == 0){
selectWindow("Mask");
run("Duplicate...", "title=blur");
run("Gaussian Blur...", "sigma=75");
imageCalculator("Subtract", "Mask","blur");
selectWindow("blur");
close();
run("Invert");
getStatistics(AREA,MEAN,MIN,MAX);
for(i=0;AREA>=getWidth*getHeight;i+=10){
makeRectangle(0,0,0,0);
setThreshold(MIN,MAX-i);
for(j=0;j<getHeight;j+=100) doWand(0,j);
getStatistics(AREA,a,b,c);
}
run("Convert to Mask");
run("Fill Holes");
run("16-bit");
run("Multiply...", "value=257.000");
}
if( DapiCh != 0 && CroppedCells == 1){
selectWindow("Mask");
setAutoThreshold("Default");
getThreshold(AAA,BBB);
setThreshold(AAA, BBB*2/3);
run("Convert to Mask");
run("16-bit");
run("Multiply...", "value=257.000");
run("Invert");
}
selectWindow("Ref");
run("Bandpass Filter...", "filter_large=10 filter_small=1 suppress=None tolerance=5 autoscale");
if(DapiCh != 0) imageCalculator("AND", "Ref","Mask");
run("Invert");
if(is("Inverting LUT")) run("Invert LUT");
setAutoThreshold("Default");
run("Analyze Particles...", "size="+MinCentro+"-"+MaxCentro+" circularity="+MinCirc+"-1.00 show=Nothing exclude clear");
for (l=0;l<nResults;l++) {
if (getResult("Feret", l)<MaxFeret){
selectWindow("Ref");
x=round(getResult("XM", l));
y=round(getResult("YM", l));
cx=x-xCor;cy=y-yCor;
makeRectangle(x-corner, y-corner, SquareSize, SquareSize);
getStatistics(area, no, minRef, no);
if (minRef>0 && area==(SquareSize*SquareSize)){
makeRectangle(cx-corner, cy-corner, SquareSize, SquareSize);
fillRect(cx-corner, cy-corner, SquareSize, SquareSize); //########## puts black box over spots, these are then disregarded in the next cycle due to "if(min>0)"
makeRectangle(cx-corner, cy-corner, SquareSize, SquareSize);
roiManager("Add");
}
}
}
roiNumber = roiManager("count");
for (data_channels = 0; data_channels < DataChArray.length; data_channels++) {
columnName = "Ch" + DataChArray[data_channels];
resArray = newArray(0);
selectWindow(RMD[data_channels+2]);
selectWindow("DataTable");
for (roi = 0; roi < roiNumber; roi++) {
row = roi + rowOffset;
Table.set("Image",row, IMname);
Table.set("ROI", row, roi+1);
roiManager("select", roi);
getStatistics(no, DataMean, DataMin, DataMax);
spot_value = DataMax - DataMin;
if (DataMax > Saturation){
Table.set(columnName, row, "Saturated Pixel");
} else {
Table.set(columnName, row, spot_value);
}
}
Table.update;
}
rowOffset += roiNumber;
return rowOffset;
}
//####### VERSION UPDATES //####### VERSION UPDATES //####### VERSION UPDATES //####### VERSION UPDATES //####### VERSION UPDATES //#######
// Check github initial commit for version updates from published version