-
Notifications
You must be signed in to change notification settings - Fork 1
/
FOVTool.java
549 lines (485 loc) · 18.8 KB
/
FOVTool.java
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
package spacetx;
import loci.common.LogbackTools;
import loci.formats.*;
import loci.formats.in.DynamicMetadataOptions;
import loci.formats.in.MetadataOptions;
import loci.formats.ome.OMEXMLMetadata;
import loci.formats.out.OMETiffWriter;
import loci.formats.tiff.IFD;
import loci.formats.tools.ImageConverter;
import loci.formats.tools.ImageInfo;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.nio.file.Files;
import java.util.*;
import java.util.concurrent.*;
/**
* Main entry point for SpaceTx FOV generation.
*/
public class FOVTool {
private final static Logger LOGGER = LoggerFactory.getLogger(FOVTool.class);
/**
* Set to desired logging level
*/
private final static String LOGLEVEL = System.getProperty("spacetx.FOVTool.loglevel", "warn");
//
// PRIMARY INPUT ARGUMENTS
//
/**
* Path to the codebook which should be attached to the fileset.
* It will be copied into the output directory. If no codebook is
* provided, then the name "codebook.json" will be used.
*/
@Option(name="-c", usage="codebook to attach", metaVar="CODEBOOK")
private File codebook = new File("codebook.json");
/**
* Represents the first field-of-view in the <b>output fileset</b>
* regardless of the number of series in the input filesets.
*/
@Option(name="-f", usage="field of view", metaVar="FOV")
private int fov = 0;
@Option(name="-j", usage="concurrent threads", metaVar="THREADS")
private int threads = 1;
//
// PRIMARY OUTPUT ARGUMENTS
//
/**
* Non-extant directory which should be used to contain the
* SpaceTx output fileset.
*/
@Option(name="-o", usage="create & output to this directory", metaVar="OUTPUT")
private File out = null;
@Option(name="--guess", usage="guess a pattern file")
private boolean guess = false;
@Option(name="--info", usage="print information about the fileset and exit")
private boolean info = false;
//
// ADVANCED ARGUMENTS
//
/**
* Naming strategies for generating the names of files on disk.
*
* Currently only "standard" is supported.
*/
@Option(name="-n", usage="naming strategy ('standard')", metaVar="NAMING")
private Naming naming = Naming.standard;
/**
* Whether to skip generation of the OME-TIFFs and only produce the
* starfish json.
*/
@Option(name="--no-tiffs", usage="skip generation of OME-TIFFs")
private boolean noTiffs = false;
/**
* Options to pass to Bio-Formats.
* See https://docs.openmicroscopy.org/latest/bio-formats/formats/options.html?highlight=options
*/
@Option(name="--options", usage="Options of the form: 'k=v:k=v'")
private String options = null;
/**
* Options to pass to Bio-Formats.
* See https://docs.openmicroscopy.org/latest/bio-formats/formats/options.html?highlight=options
*/
@Option(name="--flags", usage="Flags of the form 'f1:f2' without hyphens")
private String flags = null; // TODO: these won't apply to --guess
//
// BIO-FORMATS INTERNALS
//
/**
* Chooses which series from given filesets will be included in the field-of-view.
*/
@Option(name="-s", usage="adv: series offset of image", metaVar="SERIES")
private int series = -1;
/**
*
*/
@Option(name="--format", usage="adv: specify a specific filetype", metaVar="FORMAT")
private String format = null;
/**
* Primary input files to Bio-Formats. Related files will be auto-detected.
*
* See https://docs.openmicroscopy.org/latest/bioformats/formats/dataset-table.html
* for more information.
*/
@Argument(required=true, metaVar="INPUT", usage="main input file for Bio-Formats")
private List<String> inputs = null;
//
// STATISTICS
//
/**
* Number of calls to write TIFFs
*/
int calls = 0;
/**
* Number of bytes written to TIFFs
*/
long bytes = 0;
/**
* Total time spent writing TIFFs
*/
long elapsed = 0;
//
// GLOBAL PARALLEL STATE
//
ExperimentWriter writer;
ExecutorService executor;
ExecutorCompletionService<Integer> ecs;
Queue<Future<Integer>> futures;
IFormatReader reader;
public static void main(String[] args) throws Exception {
System.exit(new FOVTool().doMain(args));
}
public int doMain(String[] args) throws IOException, FormatException {
CmdLineParser parser = new CmdLineParser(this);
parser.getProperties().withUsageWidth(80);
try {
parser.parseArgument(args);
executor = Executors.newFixedThreadPool(threads);
for (String input : inputs) {
if (!new File(input).exists()) {
Errors.doesNotExist.raise(input);
}
}
LogbackTools.setRootLevel(LOGLEVEL);
reader = createReader(format);
if (format != null && reader == null) {
Errors.unknownFormat.raise(format);
}
if (info) {
try {
List<String> infoArgs = new ArrayList<>();
infoArgs.add("-nopix");
infoArgs.add("-cache"); // TODO: should match options
addOptions(infoArgs);
if (format != null) {
infoArgs.add("-format");
infoArgs.add(format);
}
infoArgs.add(inputs.get(0));
ImageInfo.main(infoArgs.stream().toArray(String[]::new));
return 0;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
if (guess) {
// In the guess scenario, we don't want to create an output
// directory, but a single file which will be the input to
// a new execution. (i.e. EXIT EARLY)
inputs.sort(Comparator.naturalOrder());
FileStitcher stitcher = new FileStitcher(reader);
addOptions(stitcher); // TODO: perhaps not necessary
stitcher.setId(inputs.get(0));
String content = stitcher.getFilePattern().getPattern();
if (out == null) {
System.out.println(content);
} else {
if (!out.getAbsolutePath().endsWith(".pattern")) {
Errors.patternFiles.raise();
}
out.getParentFile().mkdirs();
Files.write(out.toPath(), content.getBytes());
System.out.println(String.format("Wrote %s to %s", content, out));
}
return 0;
}
if (out == null) {
Errors.needAction.raise();
} else if (out.exists()) {
Errors.outputExists.raise(out);
} else {
out.mkdirs();
}
if (fov < 0) {
Errors.fovIsPositive.raise(fov);
}
int loop = 0;
int rv = 0;
writer = new ExperimentWriter(naming, out);
ecs = new ExecutorCompletionService<>(executor);
futures = new ConcurrentLinkedQueue<>();
for (String input : inputs) {
final int inner = loop++;
futures.add(ecs.submit(() -> {
FOVParser fovParser = new FOVParser(createReader(format), input);
try {
return convert(fovParser, writer, inner);
} finally {
try {
writer.write();
} finally {
fovParser.close();
}
}
}
));
}
for (Future<Integer> future : futures) {
rv += future.get();
}
return rv;
} catch (CmdLineException | InterruptedException | ExecutionException hide) {
Exception copy = hide;
if (copy instanceof ExecutionException) {
Throwable t = copy.getCause();
if (t instanceof Errors.UsageException) {
copy = (Errors.UsageException) t;
} else {
copy.printStackTrace(); // These are hard to debug, so show the user the verbiage.
}
}
System.err.println("spacetx-writer [options...] arguments...");
parser.printUsage(System.err);
System.err.println();
int rc = Errors.usage.rc;
if (copy instanceof Errors.UsageException) {
rc = ((Errors.UsageException) copy).rc;
}
String line = String.join("", Collections.nCopies(60, "="));
System.err.println(line);
System.err.println(String.format(
"ERROR(%s): %s", rc, copy.getMessage()));
System.err.println(line);
return rc;
} finally {
if (executor != null) {
executor.shutdownNow();
}
if (reader != null) {
reader.close();
}
}
}
/**
* Reads an input file into a {@link ImageReader} in order to have all necessary metadata,
* then uses {@link ImageConverter} to produce the TIFF stacks, and finally uses {@link FOVWriter}
* to produce the necessary JSON.
*
* @return non-zero return code if anything went wrnog
* @throws IOException
* @throws FormatException
* @throws Errors.UsageException
*/
public int convert(FOVParser parser, ExperimentWriter writer, int loop)
throws IOException, FormatException, Errors.UsageException {
int rv = 0;
String input = parser.getInput();
int plateCount = parser.getPlateCount();
int seriesCount = parser.getSeriesCount();
IFormatReader reader = parser.getReader();
OMEXMLMetadata meta = parser.getMetadata();
if (plateCount > 0) {
if (inputs.size() > 1) {
// slightly unattractive (and late) way of detecting screening data
// but this prevents us from needing to load data more than once and/or
// keep all data in memory.
Errors.singleScreening.raise();
}
// We assume that a HCS dataset it more structured, having the same
// coordinate system for all the wells, therefore we can remove some
// of the restrictions around choosing FOV.
if (plateCount > 1) {
// however, to allow fov to choose the _well_, we abort if there
// are more than one plate.
Errors.tooManyPlates.raise(plateCount);
}
int wellCount = meta.getWellCount(0);
if (wellCount != 1) {
Errors.tooManyWells.raise(wellCount);
}
// This counting loop will need to be updated when/if multiple SPWs are supported
for (int i = 0; i < seriesCount; i++) {
if (threads <= 1) {
reader.setSeries(i);
rv += convertOne(reader, meta, input, writer, i + fov);
if (rv != 0) {
return rv;
}
} else {
// Only parallelizing in the SPW case if required due to memory constraints.
final int inner = i;
futures.add(ecs.submit(() -> {
FOVParser fovParser = new FOVParser(createReader(format), input);
fovParser.getReader().setSeries(inner);
try {
return convertOne(fovParser.getReader(), meta, input, writer,inner+fov);
} finally {
try {
writer.write();
} finally {
fovParser.close();
}
}
}
));
}
}
} else {
if (seriesCount > 1) {
if (series < 0) {
// User didn't choose a series
for (int i = 0; i < seriesCount; i++) {
LOGGER.error(String.format("series %s: '%s'", i, meta.getImageName(i)));
}
Errors.multipleImages.raise(input, seriesCount);
} else {
reader.setSeries(series);
}
}
rv += convertOne(reader, meta, input, writer, loop+fov);
}
return rv;
}
private int convertOne(IFormatReader reader, OMEXMLMetadata meta, String input, ExperimentWriter eWriter, int fov)
throws FormatException, IOException {
String companion = String.format("%s/%s", out, naming.getCompanionFilename(fov));
String tiffs = String.format("%s/%s", out, naming.getTiffPattern(fov));
ImageConverter converter = createConverter();
List<String> cmd = new ArrayList<>();
addOptions(cmd);
cmd.add("-series");
cmd.add(String.valueOf(reader.getSeries()));
cmd.add("-option");
cmd.add("ometiff.companion");
cmd.add(companion);
cmd.add("-validate");
cmd.add(input);
cmd.add(tiffs);
if (!noTiffs) {
try (FormatWriter writer = imageWriter()) {
if (!converter.testConvert(writer, cmd.stream().toArray(String[]::new))) {
System.out.println("Conversion failed!");
return 1;
}
}
}
// Now write out the spacetx json
FOVWriter writer = new FOVWriter(reader, meta, naming, fov, out);
writer.write();
eWriter.addFOV(fov);
return 0;
}
/*
* Deals with the fact that the ImageConverter constructor is package-private.
*/
private static ImageConverter createConverter() {
// TODO: this should be pushed upstream
try {
Constructor<ImageConverter> c = ImageConverter.class.getDeclaredConstructor();
c.setAccessible(true);
return c.newInstance();
} catch (Exception e) {
throw new RuntimeException("Something's weird in Java land", e);
}
}
/**
* If no format is passed, return an {@link ImageReader}. Otherwise, try to
* create an instance of the given format, first be prepending "loci.formats.in"
* and appending "Reader" and then by simply looking up the class. If no such
* class is found, return null.
*
* @param format possibly null
* @return possibly null {@link IFormatReader}
*/
private IFormatReader createReader(String format) throws Errors.UsageException {
IFormatReader reader = null;
if (format == null) {
reader = new ImageReader();
} else {
try {
Class c = Class.forName(String.format(String.format("loci.formats.in.%sReader", format)));
reader = (IFormatReader) c.newInstance();
} catch (Exception e) {
try {
Class c = Class.forName(String.format(format));
reader = (IFormatReader) c.newInstance();
} catch (Exception e2) {
return null; // EARLY EXIT!
}
}
}
reader = new Memoizer(reader);
addOptions(reader);
return reader;
}
/**
* Create a {@link FormatWriter} instance which will output status updates
* as TIFFs are saved.
*
* @return instance to be used by the {@link ImageConverter}. Never null.
*/
private FormatWriter imageWriter() {
final FOVTool tool = this;
return new OMETiffWriter() {
public void saveBytes(int no, byte[] buf, IFD ifd, int x, int y, int w, int h)
throws IOException, FormatException {
tool.calls++;
tool.bytes += buf.length;
long start = System.currentTimeMillis();
try {
super.saveBytes(no, buf, ifd, x, y, w, h);
} finally {
long stop = System.currentTimeMillis();
long elapsed = stop - start;
tool.elapsed += elapsed;
System.out.println(String.format(
"[%04d]\t%s\t%s\t%8d bytes\t%4d ms\t Avg. %5.3f MB/s",
calls, new Date(), currentId.substring(currentId.lastIndexOf(File.separatorChar)+1),
buf.length, elapsed, ((double) tool.bytes)/tool.elapsed/1000
));
}
}
};
}
/**
* Add each option to the given arguments list.
*/
private void addOptions(List<String> args) {
if (options != null) {
for (String option : options.split("[:;]")) {
args.add("-option");
for (String kOrV : option.split("=")) {
args.add(kOrV);
}
}
}
if (flags != null) {
for (String flag : flags.split("[:;]")) {
args.add("-"+flag);
}
}
}
/**
* Add each option to the given reader's metadata options.
*/
private void addOptions(IFormatReader reader) throws Errors.UsageException {
if (options != null) {
DynamicMetadataOptions opts = (DynamicMetadataOptions) reader.getMetadataOptions();
for (String option : options.split("[:;]")) {
String[] kv = option.split("=");
if (kv.length != 2) {
throw Errors.badOption.raise(option);
} else {
opts.set(kv[0], kv[1]);
}
}
}
if (flags != null) {
for (String flag : flags.split("[:;]")) {
switch (flag) {
case "noflat":
reader.setFlattenedResolutions(false);
break;
default:
throw Errors.badFlag.raise(flag);
}
}
}
}
}