-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRenderImage.java
More file actions
370 lines (308 loc) · 14 KB
/
Copy pathRenderImage.java
File metadata and controls
370 lines (308 loc) · 14 KB
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
package cmd;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import data.STData;
import data.STDataStatistics;
import data.STDataUtils;
import filter.FilterFactory;
import filter.GaussianFilterFactory;
import filter.GaussianFilterFactory.WeightType;
import filter.MeanFilterFactory;
import filter.MedianFilterFactory;
import filter.SingleSpotRemovingFilterFactory;
import gui.STDataAssembly;
import gui.bdv.AddedGene;
import gui.bdv.AddedGene.Rendering;
import ij.IJ;
import ij.ImageJ;
import ij.ImagePlus;
import ij.ImageStack;
import ij.process.ImageProcessor;
import imglib2.ImgLib2Util;
import io.SpatialDataContainer;
import io.SpatialDataIO;
import net.imglib2.Interval;
import net.imglib2.IterableRealInterval;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.RealRandomAccessible;
import net.imglib2.converter.RealFloatConverter;
import net.imglib2.img.display.imagej.ImageJFunctions;
import net.imglib2.realtransform.AffineGet;
import net.imglib2.realtransform.AffineTransform2D;
import net.imglib2.realtransform.RealViews;
import net.imglib2.type.numeric.real.DoubleType;
import net.imglib2.util.Intervals;
import net.imglib2.util.Pair;
import net.imglib2.util.Util;
import net.imglib2.util.ValuePair;
import net.imglib2.view.Views;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import render.MaxDistanceParam;
import render.Render;
import org.apache.logging.log4j.Logger;
import util.LoggerUtil;
@Command(name = "st-render", mixinStandardHelpOptions = true, version = "0.3.2-SNAPSHOT", description = "Spatial Transcriptomics as IMages project - render ST data as images in Fiji/ImageJ")
public class RenderImage implements Callable<Void> {
private static final Logger logger = LoggerUtil.getLogger();
// st-render -i /Users/preibischs/Documents/BIMSB/Publications/imglib2-st/slide-seq/raw/slide-seq.n5 -d Puck_180531_22.n5,Puck_180531_23.n5 -g Malat1
// -dm Gauss -bMin 0.0 -bMax 0.1579 -rf 2.0922 --ffSingleSpot 1.25 --scale 0.10557775847089489
@Option(names = {"-i", "--input"}, required = true, description = "input file or N5 container path, e.g. -i /home/ssq.n5.")
private String inputPath = null;
@Option(names = {"-g", "--genes"}, required = true, description = "comma separated list of one or more genes, e.g. -g 'Calm2,Hpca,Ptgds'")
private String genes = null;
@Option(names = {"-o", "--output"}, required = false, description = "output folder for saving rendered images as TIFF, e.g. -o /home/export (default: display with ImageJ)")
private String output = null;
@Option(names = {"-d", "--datasets"}, required = false, description = "if --container is given: comma separated list of datasets, e.g. -d 'Puck_180528_20,Puck_180528_22' (default: open all datasets)")
private String datasets = null;
@Option(names = {"-s", "--scale"}, required = false, description = "scaling of the image, e.g. -s 0.5 (default: 0.05)")
private double scale = 0.05;
@Option(names = {"-bmin", "--brightnessMin"}, required = false, description = "min initial brightness relative to the maximal value + overall min intensity (default: 0.0)")
private double brightnessMin = 0.0;
@Option(names = {"-bmax", "--brightnessMax"}, required = false, description = "max initial brightness relative to the maximal value (default: 0.5)")
private double brightnessMax = 0.5;
@Option(names = {"--rendering"}, required = false, description = "initial rendering type (Gauss, Mean, NearestNeighbor, Linear), e.g --rendering Gauss (default: Gauss)")
private Rendering rendering = Rendering.Gauss;
@Option(names = {"-rf", "--renderingFactor"}, required = false, description = "factor for the amount of filtering or radius used for rendering, corresponds to smoothness for Gauss, e.g -rf 2.0 (default: 1.5)")
private double renderingFactor = 1.5;
@Option(names = {"--ffSingleSpot"}, required = false, description = "filter single spots using the median distance between all spots as threshold, e.g. --ffSingleSpot 1.5 (default: no filtering)")
private Double ffSingleSpot = null;
@Option(names = {"--ffMedian"}, required = false, description = "median-filter all spots using a given radius, e.g --ffMedian 5.0 (default: no filtering)")
private Double ffMedian = null;
@Option(names = {"--ffGauss"}, required = false, description = "Gauss-filter all spots using a given radius, e.g --ffGauss 2.0 (default: no filtering)")
private Double ffGauss = null;
@Option(names = {"--ffMean"}, required = false, description = "mean/avg-filter all spots using a given radius, e.g --ffMean 2.5 (default: no filtering)")
private Double ffMean = null;
@Option(names = {"--border"}, required = false, description = "extra empty border around spatial sequencing locations, e.g. -b 100 (default: 20)")
private int border = 20;
@Option(names = {"--ignoreTransforms"}, required = false, description = "ignore the transforms stored in the metadata when rendering (default: false)")
private boolean ignoreTransforms = false;
@Override
public Void call() throws Exception {
if (! SpatialDataContainer.exists(inputPath)) {
logger.error("Container / dataset '{}' does not exist. Stopping.", inputPath);
return null;
}
final ExecutorService service = Executors.newFixedThreadPool(8);
final ArrayList<Pair<String, SpatialDataIO>> iodata = new ArrayList<>();
if (SpatialDataContainer.isCompatibleContainer(inputPath)) {
SpatialDataContainer container = SpatialDataContainer.openForReading(inputPath, service);
final List<String> datasetNames;
if (datasets != null && !datasets.isEmpty())
datasetNames = Arrays.asList(datasets.split(","));
else {
logger.info("Opening all datasets in '{}':", inputPath);
datasetNames = container.getDatasets();
}
for (String dataset : datasetNames) {
logger.info("Opening dataset '{}' in '{}' ...", dataset, inputPath);
iodata.add( new ValuePair<>( dataset.trim(), container.openDatasetReadOnly(dataset.trim())));
}
}
else {
logger.info("Opening dataset '{}' ...", inputPath);
iodata.add( new ValuePair<>( inputPath, SpatialDataIO.openReadOnly(inputPath, service)));
}
if (genes == null || genes.isEmpty()) {
logger.error("No genes available. stopping.");
return null;
}
String[] geneList = genes.split(",");
final List<Pair<STData, AffineTransform2D>> dataToVisualize = new ArrayList<>();
for (final Pair<String, SpatialDataIO> entry : iodata ) {
final STDataAssembly stAssembly = entry.getB().readData();
if (stAssembly != null) {
logger.info("Assigning transform to {}", entry.getA());
AffineTransform2D transform = ignoreTransforms ? new AffineTransform2D() : stAssembly.transform();
dataToVisualize.add(new ValuePair<>(stAssembly.data(), transform));
logger.debug(transform);
}
}
if (dataToVisualize.isEmpty())
{
logger.error( "No datasets that contain sequencing data. stopping." );
return null;
}
final List<FilterFactory<DoubleType, DoubleType>> filterFactories =
assembleFilterFactories(
new STDataStatistics( dataToVisualize.get( 0 ).getA() ),
ffSingleSpot, ffMedian, ffGauss, ffMean );
if ( output == null )
new ImageJ();
else
if ( !new File( output ).exists() )
new File( output ).mkdirs();
for ( final String gene : geneList )
{
logger.info("Rendering gene {}", gene);
//ImagePlus imp = AlignTools.visualizeList( dataToVisualize, scale, gene, true );// filterFactories );
ImagePlus imp = visualizeList( dataToVisualize, scale, brightnessMin, brightnessMax, gene, rendering, renderingFactor, border, filterFactories );
imp.setTitle( gene );
if ( output == null )
{
imp.show();
}
else
{
final String file = new File( output, gene + ".tif" ).getAbsolutePath();
logger.info("Saving as {}", file);
IJ.saveAsTiff( imp, file );
imp.close();
}
}
service.shutdown();
return null;
}
public static ImagePlus visualizeList(
final List< Pair< STData, AffineTransform2D > > data,
final double scale,
final double brightnessMin,
final double brightnessMax,
final String gene,
final Rendering renderType,
final double renderingFactor,
final int border,
final List< FilterFactory< DoubleType, DoubleType > > filterFactories )
{
// visualize result using the global transform
final AffineTransform2D tS = new AffineTransform2D();
tS.scale( scale );
final Interval interval =
STDataUtils.getCommonIterableInterval(
data.stream().map( entry ->
ImgLib2Util.transformInterval(
entry.getA().getRenderInterval(),
entry.getB().copy().preConcatenate( tS ) )
).collect( Collectors.toList() ) );
final Interval finalInterval = Intervals.expand( interval, border );
logger.info("Rendering interval: {}", Util.printInterval(finalInterval));
final ImageStack stack = new ImageStack( (int)finalInterval.dimension( 0 ), (int)finalInterval.dimension( 1 ) );
double minDisplay = Double.MAX_VALUE;
double maxDisplay = -Double.MAX_VALUE;
for ( Pair< STData, AffineTransform2D > pair : data )
{
final AffineTransform2D tA = pair.getB().copy();
tA.preConcatenate( tS );
final RandomAccessibleInterval<DoubleType> vis =
display(
pair.getA(),
new STDataStatistics( pair.getA() ),
pair.getB().copy().preConcatenate( tS ),
renderType,
renderingFactor,
filterFactories,
gene,
finalInterval );
logger.info("rendering {}", pair.getA().toString());
final double[] minmax = AddedGene.minmax( pair.getA().getExprData( gene ) );
minDisplay = Math.min( minDisplay, AddedGene.getDisplayMin( minmax[ 0 ], minmax[ 1 ], brightnessMin ) );
maxDisplay = Math.max( maxDisplay, AddedGene.getDisplayMax( minmax[ 1 ], brightnessMax ) );
final ImageProcessor ip = ImageJFunctions.wrapFloat( vis, new RealFloatConverter<>(), pair.getA().toString(), null ).getProcessor();
stack.addSlice(pair.getA().toString(), ip );
}
final ImagePlus imp = new ImagePlus("all", stack );
//imp.setDimensions(data.size(), 1, 1 );
//imp.resetDisplayRange();
imp.setDisplayRange(minDisplay, maxDisplay);
return imp;
}
public static ArrayList<FilterFactory<DoubleType, DoubleType>> assembleFilterFactories(
STDataStatistics stats,
Double ffSingleSpot,
Double ffMedian,
Double ffGauss,
Double ffMean)
{
final DoubleType outOfBounds = new DoubleType( 0 );
final ArrayList<FilterFactory<DoubleType, DoubleType>> filterFactories = new ArrayList<>();
if ( ffSingleSpot != null && ffSingleSpot > 0 )
{
logger.debug("Using single-spot filtering, radius={}", ffSingleSpot);
filterFactories.add( new SingleSpotRemovingFilterFactory<>( outOfBounds, stats.getMedianDistance() * ffSingleSpot ) );
}
if ( ffMedian != null && ffMedian > 0.0 )
{
logger.debug("Using median filtering, radius={}", ffMedian);
filterFactories.add( new MedianFilterFactory<>( outOfBounds, stats.getMedianDistance() * ffMedian ) );
}
if ( ffGauss != null && ffGauss > 0.0 )
{
logger.debug("Using Gauss filtering, radius={}", ffGauss);
filterFactories.add( new GaussianFilterFactory<>( outOfBounds, stats.getMedianDistance() * ffGauss ) );
}
if ( ffMean != null && ffMean > 0.0 )
{
logger.debug("Using mean/avg filtering, radius={}", ffMean);
filterFactories.add( new MeanFilterFactory<>( outOfBounds, stats.getMedianDistance() * ffMean ) );
}
return filterFactories;
}
public static RealRandomAccessible< DoubleType > createRRA(
final IterableRealInterval< DoubleType > data,
final double medianDistance,
final Rendering renderType,
final double renderingFactor )
{
final RealRandomAccessible< DoubleType > renderRRA;
if ( renderType == Rendering.Gauss )
{
renderRRA = Render.render( data, new GaussianFilterFactory<>( new DoubleType( 0 ), medianDistance*renderingFactor, WeightType.PARTIAL_BY_SUM_OF_WEIGHTS ) );
}
else if ( renderType == Rendering.NN )
{
renderRRA = Render.renderNN( data, new DoubleType( 0 ), new MaxDistanceParam( medianDistance*renderingFactor ) );
}
else if ( renderType == Rendering.Mean )
{
renderRRA = Render.render( data, new MeanFilterFactory<>( new DoubleType( 0 ), medianDistance*renderingFactor ) );
}
else // LINEAR
{
renderRRA = Render.renderLinear( data, 5, 3.0, new DoubleType( 0 ), new MaxDistanceParam( medianDistance*renderingFactor ) );
}
return renderRRA;
}
public static RandomAccessibleInterval< DoubleType > display(
final STData stdata,
final STDataStatistics stStats,
final AffineGet coordinateTransform,
final Rendering renderType,
final double renderingFactor,
final List< FilterFactory< DoubleType, DoubleType > > filterFactories,
final String gene,
final Interval renderInterval )
{
// we work at full resolution so rendering and filter parameters are independent of the scale
final IterableRealInterval< DoubleType > data = Render.getRealIterable( stdata, null, gene, filterFactories );
return display(data, stStats.getMedianDistance(), coordinateTransform, renderType, renderingFactor, renderInterval );
/*
final RealRandomAccessible< DoubleType > renderRRA =
createRRA( data, stStats.getMedianDistance(), renderType, renderingFactor );
return Views.interval( RealViews.affine( renderRRA, coordinateTransform ), renderInterval );*/
}
public static RandomAccessibleInterval< DoubleType > display(
final IterableRealInterval< DoubleType > data,
final double medianDistance,
final AffineGet coordinateTransform,
final Rendering renderType,
final double renderingFactor,
final Interval renderInterval )
{
final RealRandomAccessible< DoubleType > renderRRA =
createRRA( data, medianDistance, renderType, renderingFactor );
return Views.interval( RealViews.affine( renderRRA, coordinateTransform ), renderInterval );
}
public static void main(final String... args) {
final CommandLine cmd = new CommandLine(new RenderImage());
cmd.execute(args);
}
}