-
Notifications
You must be signed in to change notification settings - Fork 0
/
micro-formats-extractor.js
262 lines (222 loc) · 8.06 KB
/
micro-formats-extractor.js
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
// Micro-Formats Extractor For Z Projections macro by Daniel Waiger and Tal Luigi
// Based on the Z-Stacks Projector and Christophe Leterrier's Leica LIF Extractor macros
// Tested with ImageJ 1.52p and BioFormats 6.6.0
// Version 1.0.1 28/07/2020
macro "Micro-Formats Extractor" {
// Get the folder name
var directoryPath = getDirectory("Select input folder");
print("\\Clear");
print("directoryPath: " + directoryPath);
// Get all file names
var allNames = getFileList(directoryPath);
var allExtensions = newArray(allNames.length);
// Create extensions array
for (var i = 0; i < allNames.length; i++) {
var currentNameLength = lengthOf(allNames[i]);
allExtensions[i] = substring(
allNames[i],
currentNameLength - 4,
currentNameLength
);
}
// Initialize choices variables
var backgroundArray = newArray("None", "25", "50", "100", "200", "500");
var filterArray = newArray("None", "Median", "Mean", "Gaussian", "Sigma");
var projectionArray = newArray(
"Average Intensity",
"Max Intensity",
"Min Intensity",
"Sum Slices",
"Standard Deviation",
"Median"
);
var saveArray = newArray(
"No, thanks",
"In the source folder",
"In a subfolder of the source folder",
"In a folder next to the source folder",
"In a custom folder"
);
// Create dialog box
Dialog.create("Micro-Formats Extractor");
Dialog.addMessage("\n");
Dialog.addChoice("Subtract background:", backgroundArray, "None");
Dialog.addChoice("Filter:", filterArray, "None");
Dialog.addChoice("Projection type:", projectionArray, "Sum Slices");
Dialog.addChoice(
"Save images?",
saveArray,
"In a subfolder of the source folder"
);
Dialog.addCheckbox("Reset spatial scales?", false);
Dialog.addCheckbox("Close result images (if saved)?", true);
Dialog.show();
// Capture values from dialog box choices in variables
var backgroundType = Dialog.getChoice();
var filterType = Dialog.getChoice();
var projectionType = Dialog.getChoice();
var saveType = Dialog.getChoice();
var resetScale = Dialog.getCheckbox();
var closeChoice = Dialog.getCheckbox();
setBatchMode(true);
// Adapt file format acorrding to your microscope.
// Loop on all .lei, .lsm, .czi or .lif extensions - change the file format below
for (var n = 0; n < allExtensions.length; n++) {
if (allExtensions[n] == ".lei" || allExtensions[n] == ".lif" || allExtensions[n] == ".lsm" || allExtensions[n] == ".czi") {
// Get the file path
var filePath = directoryPath + allNames[n];
// Store components of the file name
var fileName = File.getName(filePath);
var filePathLength = lengthOf(filePath);
var fileNameLength = lengthOf(fileName);
var fileDirectory = substring(filePath, 0, filePathLength - fileNameLength);
var fileExtension = substring(fileName, fileNameLength - 4, fileNameLength);
var fileShortname = substring(fileName, 0, fileNameLength - 4);
print("");
print("filePath: ", filePath);
print("fileName: ", fileName);
print("fileDirectory: ", fileDirectory);
print("fileExtension: ", fileExtension);
print("fileShortname: ", fileShortname);
// Localize or create the output folder
var outputDirectory = "Void";
if (saveType == "In the source folder") {
outputDirectory = fileDirectory;
} else if (saveType == "In a subfolder of the source folder") {
outputDirectory =
fileDirectory + fileShortname + "_ZStacks" + File.separator;
File.makeDirectory(outputDirectory);
} else if (saveType == "In a folder next to the source folder") {
outputDirectory = File.getParent(filePath);
outputDirectory =
outputDirectory + "_" + fileShortname + "_ZStacks" + File.separator;
File.makeDirectory(outputDirectory);
} else if (saveType == "In a custom folder") {
outputDirectory = getDirectory("Choose the save folder");
}
print("outputDirectory: " + outputDirectory);
print("");
// Start BioFormats and get series number in file
run("Bio-Formats Macro Extensions");
var seriesCount;
Ext.setId(filePath);
Ext.getSeriesCount(seriesCount);
var seriesNames = newArray(seriesCount);
print("seriesCount: " + seriesCount);
// Loop on all series in the file
for (var i = 0; i < seriesCount; i++) {
// Get series name and channels count
var channelCount;
Ext.setSeries(i);
Ext.getEffectiveSizeC(channelCount);
seriesNames[i] = "";
Ext.getSeriesName(seriesNames[i]);
var temporaryName = toLowerCase(seriesNames[i]);
print(
"seriesNames[" +
i +
"]: " +
seriesNames[i] +
" (temporaryName: " +
temporaryName +
")"
);
// Import the series (split channels)
run(
"Bio-Formats Importer",
"open=[" +
filePath +
"] " +
"split_channels " +
"view=[Standard ImageJ] " +
"stack_order=Default " +
"series_" +
d2s(i + 1, 0)
);
// Loop on each channel (each opened window)
for (var j = 0; j < channelCount; j++) {
// Construct window name
var temporaryChannel = d2s(j, 0);
// Window has Series Name in title only if more than one Series exists
var sourceWindowName;
if (seriesCount == 1) {
sourceWindowName = fileName + " - C=" + temporaryChannel;
} else {
sourceWindowName =
fileName + " - " + seriesNames[i] + " - C=" + temporaryChannel;
}
var type = "";
// Select source image and filter if asked
selectWindow(sourceWindowName);
if (backgroundType != "None") {
run(
"Subtract Background...",
"rolling=" + backgroundType + " sliding disable stack"
);
type = type + " Sub_BG";
}
if (filterType == "Median") {
run("Median...", "radius=1 stack");
} else if (filterType == "Mean") {
run("Mean...", "radius=1 stack");
} else if (filterType == "Gaussian") {
run("Gaussian Blur...", "radius=1 stack");
} else if (filterType == "Sigma") {
run("Sigma Filter Plus", "radius=2 use=2 minimum=0.2 outlier stack");
} else if (filterType != "None") {
type = type + " - " + substring(filterType, 0, 3) + "Filt";
}
// Reset spatial scale of the projection if the option is chosen
if (resetScale == true) {
run("Set Scale...", "distance=0 known=1 pixel=1 unit=pixel");
}
// Project and save
if (nSlices > 1) {
run("Z Project...", "projection=[" + projectionType + "]");
}
// Rename image according to processing
var newWindowName;
if (seriesCount == 1) {
newWindowName =
"C=" +
temporaryChannel +
" - " +
fileName +
" - " +
type +
" - " +
projectionType;
} else {
newWindowName =
"C=" +
temporaryChannel +
" - " +
fileName +
" - " +
type +
" - " +
projectionType +
" - " +
seriesNames[i];
}
rename(newWindowName);
print("newWindowName: " + newWindowName);
// Create output file path and save the output image
var outputPath = "Void";
if (saveType != "No, thanks") {
outputPath = outputDirectory + newWindowName + ".tif";
save(outputPath);
// Close output image if asked
if (closeChoice == true) {
close();
}
}
}
}
}
}
showMessage("Extraction Complete");
run("Close All");
run("Collect Garbage");
setBatchMode(false);
}