-
Notifications
You must be signed in to change notification settings - Fork 1
/
CountSpotsInCells.ijm
159 lines (156 loc) · 5.06 KB
/
CountSpotsInCells.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
run("OMERO Extensions");
// Enter details for connection to OMERO and select Macro to run on Dataset
#@ String (label = "username", value = "public") user
#@ String (label = "password", style="password", value = "public") pass
#@ String (label = "server", value = "camdu.warwick.ac.uk") host
#@ Integer (label = "port", value = 4064) port
#@ Integer (label = "Dataset ID", value = 10000) dataset_id
#@ String (label = "Image name contains", value="2.5x") file_contains
// Connect to server and apply macro to each image in dataset
connected = Ext.connectToOMERO(host,port,user,pass);
table_name="NumberSpotsInCells.csv";
if (connected=="true"){
imageList = Ext.list("images", "dataset", dataset_id);
image=split(imageList, ",");
print(dataset_id);
for (i = 0; i < image.length; i++) {
image_name=Ext.getName("image",image[i]);
if (indexOf(image_name, file_contains)>=0) {
print("start download");
Ext.getImage(image[i]);
print(image[i]);
title=getTitle();
processImage(title,image[i],table_name);
roiManager("reset");
}
}
selectWindow("Spots Per Cell");
// Save results to OMERO
csv_file = getDir("temp") + table_name;
selectWindow("Spots Per Cell");
saveAs("Results", csv_file);
file_id = Ext.addFile("Dataset",dataset_id, csv_file);
deleted=File.delete(csv_file);
Ext.disconnect();
print("Finished");
}
function processImage(title, image, table_name){
// open image
run("Select None");
original = title;
// Segment all cells from channel 2
Stack.setPosition(2, 1, 1);
run("Median...", "radius=10 slice");
run("Directional Filtering", "type=Max operation=Erosion line=60 direction=32");
run("Morphological Segmentation");
// wait for window to load");
wait(1000);
call("inra.ijpb.plugins.MorphologicalSegmentation.segment", "tolerance=50.0", "calculateDams=true", "connectivity=4");
call("inra.ijpb.plugins.MorphologicalSegmentation.setDisplayFormat", "Catchment basins");
wait(1000);
call("inra.ijpb.plugins.MorphologicalSegmentation.createResultImage");
run("Remove Border Labels", "left right top bottom");
run("Label Size Filtering", "operation=Greater_Than size=2000");
cellsImage=getTitle();
setThreshold(1.0000, 1000000000000000000000000000000.0000);
run("Analyze Particles...", "add");
if (roiManager("count") > 0){
selectImage(original);
// Identify spots in channel 1
original=getTitle();
Stack.setPosition(1, 1, 1);
run("Duplicate...", "title=copy duplicate channels=1");
run("Median...", "radius=2");
roiManager("Deselect");
roiManager("Combine");
run("Find Maxima...", "prominence=200 output=[Point Selection]");
selectImage(original);
run("Restore Selection");
roiManager("add");
selectWindow("copy");
close();
// Delete rois except points
roiManager("Deselect");
regions=roiManager("count");
roiManager("Select", Array.getSequence(regions-1));
roiManager("Delete");
run("Select None");
// Count number of spots Per cell
selectImage(cellsImage);
run("Set Measurements...", "modal redirect=None decimal=3");
roiManager("Measure");
modes=Table.getColumn("Mode");
Array.getStatistics(modes, min, max, mean, stdDev);
run("Distribution...", "parameter=Mode or="+max+1+" and=0-"+max+1);
selectWindow("Mode Distribution");
Plot.getValues(values,counts);
// Format Results
Array.sort(counts,values);
curr_number_of_spots=Array.deleteValue(counts, 0);
num_cells=lengthOf(curr_number_of_spots);
Array.reverse(values);
curr_cell_id=Array.trim(values, num_cells);
curr_cell_id=Array.reverse(curr_cell_id);
curr_image_id = newArray(num_cells);
Array.fill(curr_image_id, image);
if (isOpen("Spots Per Cell")){
selectWindow("Spots Per Cell");
prev_image_id=Table.getColumn("image_ID");
image_ID=Array.concat(prev_image_id,curr_image_id);
prev_cell_id=Table.getColumn("cell_ID");
cell_ID=Array.concat(prev_cell_id,curr_cell_id);
prev_spots=Table.getColumn("number_of_spots");
number_of_spots=Array.concat(prev_spots,curr_number_of_spots);
run("Close");
} else {
image_ID=curr_image_id;
cell_ID=curr_cell_id;
number_of_spots=curr_number_of_spots;
}
Array.show("Spots Per Cell", image_ID, cell_ID, number_of_spots);
// Close windows
selectWindow("Mode Distribution");
run("Close");
selectWindow("Results");
run("Close");
// Create ROIs for individual cells
selectImage(cellsImage);
run("Label Morphological Filters", "operation=Erosion radius=1 from_any_label");
setThreshold(1.0000, 1000000000000000000000000000000.0000);
roiManager("Deselect");
run("Select None");
run("Analyze Particles...", "add");
selectImage(original);
Stack.setPosition(2, 1, 1);
// select channel 2
for (i = 1; i <= roiManager("count"); i++) {
roiManager("Select", 1);
run("Enlarge...", "enlarge=1 pixel");
roiManager("Add");
roiManager("Select", 1);
roiManager("Delete");
}
// Rename cells with corresponding ID
selectImage(cellsImage);
for (i = 1; i < num_cells; i++) {
roiManager("Select", i);
run("Measure");
mode=Table.get("Mode", 0);
mode=Math.ceil(mode);
roiManager("Rename",mode);
selectWindow("Results");
run("Close");
}
// Close additional windows
selectImage(original);
// Save ROIs to OMERO
Ext.saveROIs(image, "");
}
roiManager("reset");
selectImage(original);
close("\\Others");
selectWindow("Log");
run("Close");
selectImage(original);
close();
}