-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenLifAndSaveTiffs.ijm
268 lines (211 loc) · 9.26 KB
/
openLifAndSaveTiffs.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
// Opens a lif file, gets all tilescan merges and closes the single files,
// then saves a tiff with all channels and tiffs of the individual green and red channels.
// Importer for Files with several series such as Leica .lif files. Copied from openLifTilescans.ijm
// The script opens all series and then closes all images that are not TileScan merges
// TODO: If user uses Custom Channel names, they should be written into the metadata of the files.
// Create a prompt window to select the file
loggerSanitize = false;
logger = false;
DEFAULT_DIR = "J:/2023 Screen/";
// DEFAULT_DIR = "\\\\192.168.11.13\\Old Photo\\TEPASS LAB\\2023 Screen\\Xpd RNAi\\2023 09 15 Xpd x Crb DAPI phal.lif";
DEFAULT_CHANNELS = true;
DEFAULT_CHANNEL_NAMES = false;
BATCH_MODE = false;
OS = getInfo("os.name");
Dialog.create("Browse .lif file");
Dialog.addFile("File:", DEFAULT_DIR);
// Dialog.addFile("File:","G:/2023 Screen/aPKC RNAi/2023 05 May aPKC 4 GFP.lif");
// Dialog.addDirectory("Save to:","G:/2023 Screen/aPKC RNAi/2023 05 May aPKC 4 GFP");
//Dialog.addFile("File:","\\\\mesawest/Old Photo/TEPASS LAB/2023 Screen/aPKC RNAi/2023 05 May aPKC 4 GFP.lif");
Dialog.addMessage("OPTIONAL:");
Dialog.addCheckbox("Save Channels Seperately", DEFAULT_CHANNELS);
Dialog.addCheckbox("Custom Channel Names:", DEFAULT_CHANNEL_NAMES);
Dialog.addMessage("Channels will be named automatically after LUTs used, but you can change them here:");
Dialog.addString("Channel 1 Name:","DAPI");
Dialog.addString("Channel 2 Name:","GFP");
Dialog.addString("Channel 3 Name:","RFP");
Dialog.addString("Channel 4 Name:","YFP");
Dialog.show();
dialogFile = Dialog.getString();
dialogOptionSeperateChannels = Dialog.getCheckbox();
dialogOptionCustomChannelNames = Dialog.getCheckbox();
channelNames = newArray(4);
for (i=0; i < 4; i++) {
channelNames[i] = Dialog.getString();
}
function getIncrementedBaseName(baseName, directory) {
// Check for incremented base names, starting from 01
for (i = 1; i <= 99; i++) { // Assuming a max of 99 incremented files
// Format the number with a leading zero if needed
if (i < 10) {num = "0" + i;} else {num = "" + i;}
indexedBaseName = baseName + "-" + num;
if (logger){
print("num:" + num);
print("indexedBaseName:" + indexedBaseName);
}
if (!File.exists(directory + indexedBaseName)) {
if (logger){
print("File not found. Creating.");
print("directory+indexedBaseName:"+directory + indexedBaseName);
}
return indexedBaseName;
}
}
// If we reach here, we have more than 99 incremented files. Handle appropriately.
return null;
}
function sanitizeFileName(fileName) {
if (loggerSanitize) {
print("sanitizeFileName has been called.");
print("Initial fileName: " + fileName);
}
illegalChars = "<>:\"/\\|?*";
for (i = 0; i < illegalChars.length(); i++) {
char = substring(illegalChars, i, i+1); // Use substring instead of charAt
fileName = replace(fileName, char, "_"); // Replace without checking, it's ok for short strings
if (loggerSanitize) {print("Replaced (if present) " + char + " with _. New fileName: " + fileName);}
}
if (loggerSanitize) {print("Final sanitized fileName: " + fileName);}
print("Returning from sanitizeFileName: " + fileName);
return fileName;
}
function getChannelNumber() {
var channelNumber = Property.getNumber(" SizeC");
if (isNaN(channelNumber)) {
var width, height, channels, slices, frames;
getDimensions(width, height, channels, slices, frames);
channelNumber = channels;
}
number = toString(channelNumber);
return number;
}
function saveChannelTiffs(channelNumber, indexedBaseName, fullDirImageName, channelNames) {
// TODO: DAPI is 4th somehow GFP is 3rd - "red". Red is green 2nd and yellow is blue.
for (j=1; j<=channelNumber; j++) {
if (logger) { print("Starting looking at Channel " + j);}
// channelTitle = "C"+j+"-"+indexedBaseName + ".tif";
channelTitle = "C"+j+"-"+indexedBaseName;
if (logger) { print("Looking for: " + channelTitle); }
if (isOpen(channelTitle)){
selectImage(channelTitle);
}
else{
print("Image: " + channelTitle + " not found!");
Dialog.createNonBlocking("Image not found!");
Dialog.addMessage("Image: " + channelTitle + " not found!");
Dialog.addImageChoice("Find *Channel " + j + " Image", channelTitle+"*");
Dialog.show();
channelTitle = Dialog.getImageChoice();
selectImage(channelTitle);
}
LUTNameVar = isolatedImageName + " Image #0|ChannelDescription #" + (j-1) +"|LUTName";
if (logger) {print("LUTNAME = " + LUTNameVar);}
LUTName = getInfo(LUTNameVar);
// print("Wavelength: " + wavelength +"\n LUTname: " + LUTName);
// fullDirImageSaveName = fullDirImageName + " - C" + j + " "+ channelNames[j-1] + ".tif";
// fullDirImageSaveName = fullDirImageName + " - C" + j + " "+ LUTName + " " + wavelength + ".tif";
// Override LUTName with channelNames if dialogOptionCustomChannelNames is true
if (dialogOptionCustomChannelNames) {
LUTName = channelNames[j-1];
}
fullDirImageSaveName = fullDirImageName + " - C" + j + " "+ LUTName + ".tif";
saveAs("Tiff", fullDirImageSaveName);
if (logger) { print("Saved: " + fullDirImageSaveName);}
close();
}
}
// Script start running here
setBatchMode(BATCH_MODE);
run("Bio-Formats Importer", "open=["+dialogFile+"] color_mode=Composite open_all_series rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");
dir = getInfo("image.directory");
lifFileName = getInfo("image.filename");
// name of file = foldername
//folderName = sanitizeFileName(substring(lifFileName , 0, indexOf(lifFileName, ".lif")));
//folderNameDirty Removes the .lif from the folder name
folderNameDirty = substring(lifFileName , 0, indexOf(lifFileName, ".lif"));
//Cleans the foldername from illegal characters
folderName = sanitizeFileName(folderNameDirty);
// folderName:2023 09 15 Xpd x Crb DAPI phal
exportDir = dir+folderName+"\\";
if (OS == "Windows 10") {
exportDir = dir+folderName+"\\";
} else {
exportDir = dir+folderName+"/";
}
if (logger) {
print("lifFileName:" + lifFileName);
print("folderNameDirty: " +folderNameDirty);
print("folderName:" + folderName);
}
if (!File.isDirectory(exportDir)) {
File.makeDirectory(exportDir);
}
// get a list of all files open
// Will print the list out here
listMerges = getList("image.titles");
print(listMerges.length + " stacks found:");
for (list=0; list<listMerges.length; list++){
print(" ---" + listMerges[list]);
}
for (i=0; i<listMerges.length; i++) {
if (logger) {print("i ="+(i+1)+"/"+ listMerges.length +" looking at "+listMerges[i]);}
selectWindow(listMerges[i]);
//Gives the whole title: "2023 05 May aPKC 4 GFP.lif - TileScan 2/Position 2"
imageWholeTitle = getInfo("image.title");
// Get everything after the last dash in the title: "TileScan 2/Position 2"
lastDash = lastIndexOf(imageWholeTitle, "-");
if (lastDash != -1) {
isolatedImageName = trim(imageWholeTitle.substring(lastDash + 1)); // trim to remove any extra spaces
//isolatedImageName = "TileScan 2/Position 2"
} else {
print("PROBLEM WITH FINDING IMAGE NAME!");
isolatedImageName = imageWholeTitle;
}
//If isolatedImageName has some illegal characters, get rid of those.
cleanImageName = sanitizeFileName(isolatedImageName);
selectWindow(listMerges[i]);
channelNumber = getChannelNumber();
//Make the folder where the file will be located
fullDirImageName = exportDir+folderName+" - "+cleanImageName;
if (logger) {
print("Isolated Image Name: " + isolatedImageName);
print("cleanImageName:" + cleanImageName);
print("channelNumber: " + channelNumber);
print("fullDirImageName :" + fullDirImageName);
// Isolated Image Name: Series002
// cleanImageName:Series002
// channelNumber: 3
// fullDirImageName :Z:\TEPASS LAB\2023 Screen\Xpd RNAi\2023 09 15 Xpd x Crb DAPI phal\2023 09 15 Xpd x Crb DAPI phal - Series002
}
// Save the file that has all channels?
if (!File.isDirectory(fullDirImageName +".tif")) {
saveAs("Tiff", fullDirImageName +" - All Channels.tif");
savedAllChannelTitle = getTitle();
if (logger) {print("Saved: " + savedAllChannelTitle);}
indexedBaseName = folderName+" - "+cleanImageName; // "2023 09 15 Xpd x Crb DAPI phal"+" - "+"Series002" = "2023 09 15 Xpd x Crb DAPI phal - Series002"
selectImage(savedAllChannelTitle);
rename(indexedBaseName);
} else {
indexedBaseName = folderName+" - "+getIncrementedBaseName(isolatedImageName, exportDir); //
rename(indexedBaseName);
}
// real name = "C2-2023 05 May aPKC 4 GFP - TileScan 2_Position 1.tif
// channeltitle = "C1-2023 05 May aPKC 4 GFP - TileScan 2_Position 1"
selectImage(indexedBaseName);
//run("Split Channels");
if (channelNumber > 1) {
runMacro("SplitChannels.ijm","false");
print("Split channels for " + indexedBaseName + " With "+channelNumber+ " channels.");
if (dialogOptionSeperateChannels == true) {
saveChannelTiffs(channelNumber, indexedBaseName, fullDirImageName, channelNames);
}
} else {
close();
}
print("Trying to close: " + listMerges[i]);
// close();
}
print("Done!");
//C4-2023 08 25 aPKC RNAI x Crb DAPI GFP Baz647 Casp3555.lif - Series009.tif
//2023 08 25 aPKC RNAI x Crb DAPI GFP Baz647 Casp3555 - Series009 - C3 Red.tif
//C4-2023 08 25 aPKC RNAI x Crb DAPI GFP Baz647 Casp3555 - Series009.tif