-
Notifications
You must be signed in to change notification settings - Fork 2
/
BatchConvertStacksToZProjJPGSRespectingChannels.txt
217 lines (185 loc) · 6.04 KB
/
BatchConvertStacksToZProjJPGSRespectingChannels.txt
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
// "BatchConvertStacksToZProjJPGSRespectingChannels"
//
// This macro batch processes a directory containing lsm files
// with associated metadata xml files
// It checks the order of the channels in the xml files
// and then saves jpgs in the order
// green, red, blue (ie 488, 568, 647)
// Can be passed 2 comma separated arguments on the command line
// inputPath, outputDir or inputDir, outputDir. In the later case
// it will iterate on all the files from the inputDir
// Adapted by Aaron Ostrovsky, Sebastian Cachero and
// Greg Jefferis from code at
// http://rsb.info.nih.gov/ij/macros/BatchProcessFolders.txt
requires("1.42k");
file = getArgument;
dir=""
outputDir=""
//print("file = "+file);
if (file!=""){
arg = split(file,",");
if (arg.length!=2) {
exit();
} else if(arg[0]=="" || arg[1]==""){
exit();
} else {
outputDir=arg[1];
if(!endsWith(outputDir,"/")) outputDir=outputDir+"/";
if(File.isDirectory(arg[0])) {
// we're dealing with a directory
dir=arg[0];
if(!endsWith(dir,"/")) dir=dir+"/";
} else {
// single file
dir=File.getParent(arg[0])+"/";
file=File.getName(arg[0]);
processFile(dir,outputDir,file);
exit();
}
}
}
if(dir=="") dir = getDirectory("Choose a stacks directory");
if(outputDir=="") outputDir = getDirectory("Choose output directory");
setBatchMode(true);
count = 0;
countFiles(dir);
print("Total files: "+count);
n = 0;
processFiles(dir, outputDir);
function countFiles(dir) {
list = getFileList(dir);
for (i=0; i<list.length; i++) {
if (endsWith(list[i], "/"))
countFiles(""+dir+list[i]);
else
count++;
}
}
function processFiles(dir,outputDir) {
list = getFileList(dir);
for (i=0; i<list.length; i++) {
if (endsWith(list[i], "/"))
processFiles(""+dir+list[i], outputDir);
else {
showProgress(n++, count);
processFile(dir,outputDir,list[i]);
}
}
}
function processFile(dir,outputDir,file) {
// if (endsWith(file, ".pic") || endsWith(file,".PIC")) {
// if(File.exists(outputDir+substring(file,0,indexOf(file,"_"))+"_C"+substring(file,lengthOf(file)-5,lengthOf(file)-4)+".jpg")==1) {
// print("image pic exist" + file);
// return 0;
// } else {
// print(outputDir+substring(file,0,indexOf(file,"_"))+"_C1.jpg");
// print(File.exists(outputDir+substring(file,0,indexOf(file,"_"))+"_C1.jpg"));
// }
// path = dir+list[i];
// open(path);
// processImage();
// saveAs("Jpeg", outputDir+substring(file,0,indexOf(file,"_"))+"_C"+substring(file,lengthOf(file)-5,lengthOf(file)-4));
// close();
// close();
// }
if (endsWith(file, ".lsm") || endsWith(file,".LSM")) {
print("Converting LSM to JPG: "+file);
// Skip if jpegs already exist
c1jpg=outputDir+substring(file,0,lengthOf(file)-4)+"_C1.jpg";
if(File.exists(c1jpg)) {
print("JPEG output files already exist for " + file);
return 0;
}
// This is the path of the xml meta data file
xmlpath=dir+substring(file,0,lengthOf(file)-4)+".xml";
if(!File.exists(xmlpath)){
print("Skipping file: "+file+" since no XML metadata available");
return;
}
// parse xml file to identify channel order
xmlstr=File.openAsString(xmlpath);
xmllines=split(xmlstr,"\n");
// get the number of channels from the xml file
noImages=0;
for(l=0;l<xmllines.length;l++){
if(indexOf(xmllines[l],"IMAGES_NUMBER_CHANNELS")>0){
noImages=parseInt(substring(xmllines[l],indexOf(xmllines[l],"\">")+2,indexOf(xmllines[l],"</")));
}
}
if( noImages==0 )
print("Image No not found in XML data for image: "+file);
// make an array to handle up to 4 channels
lsmchannels=newArray(5);
channelsfound=0;
for(l=0;l<xmllines.length;l++){
if(indexOf(xmllines[l],"DYE_NAME")>0){
if(indexOf(xmllines[l],"488")>0 || indexOf(xmllines[l],"FITC")>0 || indexOf(xmllines[l],"GFP")>0){
lsmchannels[++channelsfound]=1;
}
if(indexOf(xmllines[l],"568")>0 || indexOf(xmllines[l],"561")>0 || indexOf(xmllines[l],"543")>0){
lsmchannels[++channelsfound]=2;
}
if(indexOf(xmllines[l],"647")>0 || indexOf(xmllines[l],"633")>0){
lsmchannels[++channelsfound]=3;
}
}
}
if( (channelsfound) != noImages ){
print("exploring ILL_NAME");
channelsfound=0;
for(l=0;l<xmllines.length;l++){
if(indexOf(xmllines[l],"ILL_NAME")>0){
if(indexOf(xmllines[l],"488")>0){
lsmchannels[++channelsfound]=1;
}
if(indexOf(xmllines[l],"568")>0 || indexOf(xmllines[l],"561")>0 || indexOf(xmllines[l],"543")>0){
lsmchannels[++channelsfound]=2;
}
if(indexOf(xmllines[l],"647")>0 || indexOf(xmllines[l],"633")>0){
lsmchannels[++channelsfound]=3;
}
}
}
}
lsmpath = dir+file;
// Check that we have the same number of channels from the metadata file
// and the lsm file.
if( (channelsfound) != noImages ){
print("Skipping"+lsmpath+"\nNumber of channels extracted from metadata ("+channelsfound+") not equal to channels from lsm ("+noImages+")");
// Close open images so we don't end up running out of memory
// for(i=noImages;i>0;i--){
// selectImage(workingImage);
// close();
// workingImage++;
// }
return;
}
print("Trying to open LSM with with `open()` rather than with LOCI");
open(lsmpath);
run("Split Channels");
// run("Bio-Formats Importer", "open=" + lsmpath + " view=[Standard ImageJ] stack_order=Default split_channels");
// title=getTitle();
// noImages=parseInt(substring(title,(lengthOf(title)-1),lengthOf(title)));
workingImage=getImageID();
// noImages++;
for(i=noImages;i>0;i--){
selectImage(workingImage);
processImage();
// note that lsmchannels[i] will look up the corrected channel number
// for the ith channel in the original lsm order
jpegfile=substring(file,0,lengthOf(file)-4)+"_C"+lsmchannels[i];
saveAs("Jpeg", outputDir+jpegfile);
close();
print("Selecting Image " + workingImage);
selectImage(workingImage);
close();
workingImage++;
}
}
}
function processImage() {
run("Z Project...", "projection=[Standard Deviation]");
run("8-bit");
// run("Flip Vertically");
}