-
Notifications
You must be signed in to change notification settings - Fork 0
/
smFISH-compute-apical-basal-ROIs-for-batch.ijm
executable file
·118 lines (97 loc) · 4.67 KB
/
smFISH-compute-apical-basal-ROIs-for-batch.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
clean_up_workspace();
half_cell_height = 7.5; // in microns; half the height of the outer bud epithelial cells
//inputFolder = getDirectory("Choose the folder containing images to process:");
inputFolder = getArgument();
setBatchMode(true);
//setBatchMode(false);
fList = getFileList(inputFolder);
for (i=0; i<fList.length; i++) {
f = fList[i];
if ( endsWith(f, File.separator) || startsWith(f, ".") || startsWith(f, "~") || startsWith(f, "_") ) { continue; } //skip if f is a folder, or system files
filenamePrefix = getFilenamePrefix(f);
// epiMask = ROIfolder + filenamePrefix + "-epiMask.tif";
basalMask = inputFolder + filenamePrefix + "-basalMask.tif";
apicalMask = inputFolder + filenamePrefix + "-apicalMask.tif";
if ( endsWith(f, "-epiMask.tif") ) {
if ( File.exists(basalMask) && File.exists(apicalMask) ) { continue; } //skip if already processed
open(inputFolder + f); rename("epi"); id0 = getImageID();
id_expanded = expand_ROI(id0);// expand image to all directions and fill with mirror images
id_shrunken_1 = shrink_ROI(id_expanded, half_cell_height);
selectImage(id_shrunken_1); rename("shrunken_1");
cell_height = half_cell_height*2;
id_shrunken_2 = shrink_ROI(id_expanded, cell_height);
selectImage(id_shrunken_2); rename("shrunken_2");
imageCalculator("Subtract create", "epi", "shrunken_1"); id_basal = getImageID();
selectImage(id_basal); saveAs("tiff", basalMask);
imageCalculator("Subtract create", "shrunken_1", "shrunken_2"); id_apical = getImageID();
selectImage(id_apical); saveAs("tiff", apicalMask);
run("Close All"); run("Collect Garbage"); // Release occupied memory
roiManager("reset");
}
}
setTool("rectangle");
//showMessage("Find saved ROIs in the \"ROIs\" folder inside the output folder.");
function shrink_ROI(id, shrinkWidth) {
// shrink an mirror-explanded binary image to prevent shrinking of boundary-touching edges
selectImage(id); run("Duplicate...", " "); id_shrunken = getImageID();
selectImage(id_shrunken); setThreshold(255, 255); setOption("BlackBackground", false); run("Convert to Mask");
run("Create Selection"); run("Enlarge...", "enlarge=-"+shrinkWidth);
setBackgroundColor(255, 255, 255); run("Clear Outside"); run("Select None");
getDimensions(width, height, channels, slices, frames);
new_w = width / 3;
new_h = height / 3;
run("Canvas Size...", "width="+new_w+" height="+new_h+" position=Center zero");
return id_shrunken;
}
function expand_ROI(id) {
// expand image to all directions and fill with mirror images
selectImage(id); run("Duplicate...", " "); rename("center");
selectWindow("center"); run("Duplicate...", " "); run("Flip Horizontally"); rename("left");
selectWindow("left"); run("Duplicate...", " "); rename("right");
run("Combine...", "stack1=left stack2=center"); rename("left_center");
run("Combine...", "stack1=left_center stack2=right"); rename("middle");
selectWindow("middle"); run("Duplicate...", " "); run("Flip Vertically"); rename("top");
selectWindow("top"); run("Duplicate...", " "); rename("bottom");
run("Combine...", "stack1=top stack2=middle combine"); rename("top_middle");
run("Combine...", "stack1=top_middle stack2=bottom combine"); rename("expanded"); id_expanded = getImageID();
return id_expanded;
}
function clean_up_workspace() {
// Clean up workspace
run("Close All"); run("Collect Garbage"); // Release occupied memory
if ( isOpen("Synchronize Windows") ) { selectWindow("Synchronize Windows"); run("Close"); }
if ( isOpen("Log") ) { selectWindow("Log"); run("Close"); }
if ( isOpen("Debug") ) { selectWindow("Debug"); run("Close"); }
if ( isOpen("Results") ) { selectWindow("Results"); run("Close"); }
if ( isOpen("ROI Manager") ) { roiManager("reset"); selectWindow("ROI Manager"); run("Close"); }
setTool("rectangle");
setForegroundColor(255, 255, 255);
setBackgroundColor(0, 0, 0);
}
function getFilenamePrefix(filename) {
// this one takes just the file name without folder path
temp = split(filename, ".");
return temp[0];
}
function getPathFilenamePrefix(pathFileOrFolder) {
// this one takes full path of the file of folder
temp = split(pathFileOrFolder, File.separator);
temp = temp[temp.length-1];
temp = split(temp, ".");
return temp[0];
}
function getPath(pathFileOrFolder) {
// this one takes full path of the file (input can also be a folder)
temp = split(pathFileOrFolder, File.separator);
if ( File.separator == "/" ) {
// Mac and unix system
pathTemp = File.separator;
for (i=0; i<temp.length-1; i++) {pathTemp = pathTemp + temp[i] + File.separator;}
}
if ( File.separator == "\\" ) {
// Windows system
pathTemp = temp[0] + File.separator;
for (i=1; i<temp.length-1; i++) {pathTemp = pathTemp + temp[i] + File.separator;}
}
return pathTemp;
}