-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBatchConvertFlycircuitLSMsToNrrds.txt
172 lines (155 loc) · 4.44 KB
/
BatchConvertFlycircuitLSMsToNrrds.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
// "BatchConvertLSMsToPICs"
//
// This macro batch processes all the lsm files in a folder
// saving them as nrrd or pic files.
// Limitations/Specifics
// 1) Only works with Fiji
// 2) No attempt is made to reorder channels
// 3) Uses file locking when called with a directory as input, but NOT when
// called with a specific input file
// (in which case we assume that someone else is handling this issue)
//
// (Slightly) Adapted by Greg Jefferis from code at
// http://rsb.info.nih.gov/ij/macros/BatchProcessFolders.txt
// jefferis@gmail.com
requires("1.42k");
file = getArgument;
dir=""
outputDir=""
outputformat="nrrd" // or pic
useLocksAndCheckOutput = true; // by default
// Set up for headless image opening
run("Bio-Formats Macro Extensions");
setBatchMode(true);
// 1) Check what arguments we've received
// --------------------------------------
//print("file = "+file);
if (file!=""){
arg = split(file,",");
if (arg.length<2 || arg.length>4) {
exit();
}
if (arg.length==3){
outputformat=arg[2];
if( ! (outputformat == "pic" || outputformat == "nrrd")){
exit("Output format must be either pic or nrrd");
}
}
// ... now deal with directories
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
useLocksAndCheckOutput = false;
dir=File.getParent(arg[0])+"/";
file=File.getName(arg[0]);
processFile(dir,outputDir,file);
exit();
}
}
}
// 2) Ask for in/output dirs if required
// --------------------------------------
if(dir=="") dir = getDirectory("Choose a stacks directory");
if(outputDir=="") outputDir = getDirectory("Choose output directory");
// 3) Process files in the chosen directories
// --------------------------------------
count = 0;
countFiles(dir);
print("Total files: "+count);
n = 0;
processFiles(dir, outputDir);
// 4) Function definitions
// --------------------------------------
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);
shuffle(list);
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, ".lsm") || endsWith(file,".LSM")) {
path = dir+file;
if(useLocksAndCheckOutput){
// Check if output already exists
c1outpath=outputDir+substring(file,0,lengthOf(file)-4)+"_01."+outputformat;
if(File.exists(c1outpath)){
print("Skipping file: " + file + " since " + c1outpath + " already exists");
return;
}
// Check if someone is already working on this
lockpath=path+'.lock';
if(File.exists(lockpath)){
print("Skipping file: " + file + " since someone else is working on it");
return;
}
File.saveString("",lockpath);
}
imagesAlreadyOpen=nImages;
Ext.openImagePlus(path);
noImages=nImages-imagesAlreadyOpen;
if(noImages==1){
run("Split Channels");
noImages=nImages-imagesAlreadyOpen;
}
if(noImages!=3){
IJ.log("Warning there appear to be "+noImages+" channels (rather than 3).");
}
for(i=noImages;i>0;i--){
processImage();
if(outputformat=='pic'){
run("Biorad ...","biorad=["+outputDir+substring(file,0,lastIndexOf(file,"."))+"_0"+i+".pic]");
} else {
setKeyDown("alt");
run("Nrrd ... ", "nrrd=["+outputDir+substring(file,0,lastIndexOf(file,"."))+"_0"+i+".nrrd]");
setKeyDown("none");
}
close();
}
if(useLocksAndCheckOutput){
File.delete(lockpath);
}
}
}
function processImage() {
// run("Flip Vertically", "stack");
// run("Z Project...", "projection=[Standard Deviation]");
// run("8-bit");
// run("Flip Vertically");
}
function shuffle(array) {
n = array.length; // The number of items left to shuffle (loop invariant).
while (n > 1) {
k = randomInt(n); // 0 <= k < n.
n--; // n is now the last pertinent index;
temp = array[n]; // swap array[n] with array[k] (does nothing if k==n).
array[n] = array[k];
array[k] = temp;
}
}
// returns a random number, 0 <= k < n
function randomInt(n) {
return n * random();
}