-
Notifications
You must be signed in to change notification settings - Fork 0
/
DK 16bit 2P 4-channel combine.ijm
171 lines (155 loc) · 5.32 KB
/
DK 16bit 2P 4-channel combine.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
//This macro will aide in combining the four detector channels from the Two-Photon microscope
// Currently, this version only works when both 930nm and 1040nm lasers are used with upper and lower detectors for each.
// Created by Eritano AS., Wang YC., Kelly DJ.
// contact: dougkelly88@gmail.com
setOption("ExpandableArrays", true);
// user parameters
//stack_order = "xyctz";
function filterByFileType(files, extension){
// filter file array according to file extension
filtered = newArray();
for (idx = 0; idx < files.length; idx++)
{
if (endsWith(files[idx], extension)){
filtered[filtered.length] = files[idx];
}
}
return filtered;
}
function combineChannels(file_name, channel_name, channel_number1, channel_number2, slices, frames, out_path)
{
// sum channels into a single stack
file_string = split(file_name, ".");
file_string = file_string[0];
print("filename = " + file_name);
print("channel_name = " + channel_name);
print("channel_number1 = " + channel_number1);
print("channel_number2 = " + channel_number2);
print("slices = " + slices);
print("frames = " + frames);
selectWindow(file_name);
run("Make Substack...", "channels=" + channel_number1 + " slices=1-" + slices + " frames=1-" + frames + "");
rename("channel" + channel_number1);
selectWindow(file_name);
run("Make Substack...", "channels=" + channel_number2 + " slices=1-" + slices + " frames=1-" + frames + "");
rename("channel" + channel_number2);
imageCalculator("Add create stack", "channel" + channel_number1, "channel" + channel_number2);
run("Grays");
if ((frames > 1) || (slices > 1))
{
Stack.getStatistics(area, mean, min, max);
}
else
{
getStatistics(area, mean, min, max);
}
setMinAndMax(min, max);
run("16-bit");
rename(channel_name);
run("Duplicate...", "duplicate");
saveAs("tiff", out_path + File.separator + file_string + " - " + channel_name);
}
// get the files
data_root = getDirectory("Choose a folder containing data...");
if (!endsWith(data_root, File.separator))
{
data_root = data_root + File.separator;
}
files = getFileList(data_root);
// get the file-related parameters
Dialog.create("Looped 2p combiner");
Dialog.addChoice("File format:", newArray(".oir",".tif"));
Dialog.addMessage("Please name the output folder:");
Dialog.addString("File Name", "output");
Dialog.show();
file_extension = Dialog.getChoice();
output_folder = Dialog.getString();
output_path = data_root + output_folder;
// set default channel parameters based on file format
if (file_extension == ".tif")
{
default_channels = newArray(1, 2, 3, 4);
}
else if (file_extension == ".oir")
{
default_channels = newArray(1, 3, 2, 4);
}
// get the channel-related parameters
Dialog.create("Channel definitions...")
Dialog.addString("Red Channel Name", "myomK");
Dialog.addNumber("Red Channel", default_channels[0]);
Dialog.addNumber("Red Channel", default_channels[1]);
Dialog.addString("Green Channel Name", "evemNG");
Dialog.addNumber("Green Channel", default_channels[2]);
Dialog.addNumber("Green Channel", default_channels[3]);
Dialog.show();
red_channel_name = Dialog.getString();
red_channel_number1 = Dialog.getNumber();
red_channel_number2 = Dialog.getNumber();
green_channel_name = Dialog.getString();
green_channel_number1 = Dialog.getNumber();
green_channel_number2 = Dialog.getNumber();
// loop over files, combining channels
filtered_files = filterByFileType(files, file_extension);
if (filtered_files.length > 0)
{
File.makeDirectory(output_path);
}
for (fidx = 0; fidx < filtered_files.length; fidx++)
{
data_path = data_root + filtered_files[fidx];
subfolder_name = split(filtered_files[fidx], ".");
subfolder_name = subfolder_name[0];
output_subfolder = output_path + File.separator + subfolder_name;
File.makeDirectory(output_subfolder);
print(data_path);
run("Bio-Formats", "open=[" + data_path + "]autoscale color_mode=Default");
file_name = getInfo("image.filename");
selectWindow(file_name);
getDimensions(w, h, channels, slices, frames);
if (channels == 2)
{
red_channel_number1_temp = 1;
red_channel_number2_temp = 2;
}
else
{
red_channel_number1_temp = red_channel_number1;
red_channel_number2_temp = red_channel_number2;
}
combineChannels(file_name, red_channel_name, red_channel_number1_temp, red_channel_number2_temp, slices, frames, output_subfolder);
if (channels > 2)
{
combineChannels(file_name, green_channel_name, green_channel_number1, green_channel_number2, slices, frames, output_subfolder);
selectWindow(green_channel_name);
// no idea why this triple repeat is necessary, seems ridiculous:
run("Merge Channels...", "c1=" + red_channel_name + " c2=" + green_channel_name + " create keep");
run("Merge Channels...", "c1=" + red_channel_name + " c2=" + green_channel_name + " create keep");
run("Merge Channels...", "c1=" + red_channel_name + " c2=" + green_channel_name + " create keep");
if (isOpen("Merged"))
{
selectWindow("Merged");
}
if (isOpen("Composite"))
{
selectWindow("Composite");
}
if (isOpen("Merged") | isOpen("Composite"))
{
saveAs("tiff", output_subfolder + File.separator + subfolder_name +"_merged");
}
else
{
print("WARNING: no merged file saved for input file " + file_name + "!");
}
}
run("Close All");
}
if (filtered_files.length == 0)
{
waitForUser("No files of the selected format exist in the selected folder!");
}
else
{
waitForUser("Congratulations, you are done!");
}