-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBatchConvertLSMsToPICsRespectingChannelsLeft.txt
145 lines (133 loc) · 4.26 KB
/
BatchConvertLSMsToPICsRespectingChannelsLeft.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
// This macro batch processes all the lsm files in a folder
// saving them as pic files.
// Limitations/Specifics
// 1) Only works with Fiji
// 2) Channel order will be : red, green, blue (where present)
// this is identified from lsm xml metadata
// 3) Always flips vertically
// 4) Expects names to be in regular JLab format
// or in something where all it has to do is append the
// v01.pic v02.pic etc to the filestem
// 5) Cannot yet run headless because it uses LOCI to open LSMs
// 6) Can't yet figure out what kind of image this is
// which might help decide whether to flip/rotate etc
// Can be passed 2 comma separated arguments on the command line
// inputPath, outputDir
// 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;
//print("file = "+file);
if (file==""){
dir = getDirectory("Choose a stacks directory");
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++;
}
}
} else {
arg = split(file,",");
if (arg.length!=2) {
exit();
} else if(arg[0]=="" || arg[1]==""){
exit();
} else {
dir=File.getParent(arg[0])+"/";
file=File.getName(arg[0]);
outputDir=arg[1];
if(!endsWith(outputDir,"/")) outputDir=outputDir+"/";
processFile(dir,outputDir,file);
}
}
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) {
print("dir = "+dir,"; outputDir = "+outputDir,"; file = "+file);
if (endsWith(file, ".lsm") || endsWith(file,".LSM")) {
c1picpath=dir+substring(file,0,lengthOf(file)-4)+"01.pic";
if(File.exists(c1picpath)){
print("Skipping file: "+file+" since 01.pic already exists");
return;
}
// 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 at "+xmlpath);
return;
}
// parse xml file to identify channel order
xmlstr=File.openAsString(xmlpath);
xmllines=split(xmlstr,"\n");
// 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){
lsmchannels[++channelsfound]=2;
}
if(indexOf(xmllines[l],"568")>0 || indexOf(xmllines[l],"561")>0 || indexOf(xmllines[l],"543")>0){
lsmchannels[++channelsfound]=1;
}
if(indexOf(xmllines[l],"647")>0 || indexOf(xmllines[l],"633")>0){
lsmchannels[++channelsfound]=3;
}
}
}
lsmpath = dir+file;
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++;
// 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;
}
for(i=noImages;i>0;i--){
channel=lsmchannels[i];
selectImage(workingImage);
processImage();
run("Biorad ...","biorad=["+outputDir+substring(file,0,lastIndexOf(file,"."))+"_0"+channel+".pic]");
selectImage(workingImage);
close();
workingImage++;
}
}
}
function processImage() {
run("Rotate 90 Degrees Left");
// run("Flip Vertically", "stack");
// run("Z Project...", "projection=[Standard Deviation]");
// run("8-bit");
// run("Flip Vertically");
}