Skip to content

Commit f43dd86

Browse files
committed
Fix possible leakage when streaming SC vectors
1 parent 118658c commit f43dd86

3 files changed

Lines changed: 12 additions & 14 deletions

File tree

gemma-cli/src/main/java/ubic/gemma/apps/SingleCellDataWriterCli.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -295,16 +295,14 @@ private int aggregate( ExpressionExperiment ee, QuantitationType qt, @Nullable L
295295
if ( useStreaming ) {
296296
log.info( "Single-cell data will be streamed by batch of " + fetchSize + " vectors." );
297297
long numberOfVectors = singleCellExpressionExperimentService.getNumberOfSingleCellDataVectors( ee, qt );
298-
Stream<SingleCellExpressionDataVector> scVecs;
299-
if ( assays != null ) {
300-
scVecs = singleCellExpressionExperimentService.streamSingleCellDataVectors( ee, assays, qt, fetchSize, useCursorFetchIfSupported, true, config );
301-
} else {
302-
scVecs = singleCellExpressionExperimentService.streamSingleCellDataVectors( ee, qt, fetchSize, useCursorFetchIfSupported, true, config );
298+
try ( Stream<SingleCellExpressionDataVector> scVecs = assays != null ?
299+
singleCellExpressionExperimentService.streamSingleCellDataVectors( ee, assays, qt, fetchSize, useCursorFetchIfSupported, true, config ) :
300+
singleCellExpressionExperimentService.streamSingleCellDataVectors( ee, qt, fetchSize, useCursorFetchIfSupported, true, config ) ) {
301+
vecs = scVecs
302+
.peek( createStreamMonitor( ee, qt, getClass().getName(), 100, numberOfVectors ) )
303+
.map( createAggregator( aggregationMethod, cellLevelCharacteristics, aggregateUnknownCharacteristics ) )
304+
.collect( Collectors.toList() );
303305
}
304-
vecs = scVecs
305-
.peek( createStreamMonitor( ee, qt, getClass().getName(), 100, numberOfVectors ) )
306-
.map( createAggregator( aggregationMethod, cellLevelCharacteristics, aggregateUnknownCharacteristics ) )
307-
.collect( Collectors.toList() );
308306
} else {
309307
log.info( "Single-cell data will be loaded into memory. This process can use a lot of memory, press Ctrl-C at any time to interrupt." );
310308
Collection<SingleCellExpressionDataVector> scVecs;

gemma-core/src/main/java/ubic/gemma/core/analysis/service/ExpressionDataFileHelperService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public ExpressionDataDoubleMatrix getDataMatrix( ExpressionExperiment ee, @Nulla
118118
return matrix;
119119
}
120120

121-
public Stream<SingleCellExpressionDataVector> getSingleCellVectors( ExpressionExperiment ee, @Nullable List<BioAssay> samples, QuantitationType qt, Map<CompositeSequence, Set<Gene>> cs2gene, AtomicLong numVecs1, int fetchSize, boolean useCursorFetchIfSupported ) {
121+
public Stream<SingleCellExpressionDataVector> streamSingleCellVectors( ExpressionExperiment ee, @Nullable List<BioAssay> samples, QuantitationType qt, Map<CompositeSequence, Set<Gene>> cs2gene, AtomicLong numVecs1, int fetchSize, boolean useCursorFetchIfSupported ) {
122122
long numVecs = singleCellExpressionExperimentService.getNumberOfSingleCellDataVectors( ee, qt );
123123
if ( numVecs == 0 ) {
124124
throw new IllegalStateException( "There are no vectors for " + qt + " in " + ee + "." );
@@ -142,7 +142,7 @@ public Collection<SingleCellExpressionDataVector> getSingleCellVectors( Expressi
142142
return vectors;
143143
}
144144

145-
public Stream<SingleCellExpressionDataVector> getSingleCellVectors( ExpressionExperiment ee, @Nullable List<BioAssay> samples, QuantitationType qt, Map<CompositeSequence, Set<Gene>> cs2gene, AtomicLong numVecs1, Map<BioAssay, Long> nnzBySample, int fetchSize, boolean useCursorFetchIfSupported ) {
145+
public Stream<SingleCellExpressionDataVector> streamSingleCellVectors( ExpressionExperiment ee, @Nullable List<BioAssay> samples, QuantitationType qt, Map<CompositeSequence, Set<Gene>> cs2gene, AtomicLong numVecs1, Map<BioAssay, Long> nnzBySample, int fetchSize, boolean useCursorFetchIfSupported ) {
146146
long numVecs = singleCellExpressionExperimentService.getNumberOfSingleCellDataVectors( ee, qt );
147147
if ( numVecs == 0 ) {
148148
throw new IllegalStateException( "There are no vectors for " + qt + " in " + ee + "." );

gemma-core/src/main/java/ubic/gemma/core/analysis/service/ExpressionDataFileServiceImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ private int writeTabularSingleCellExpressionDataInternal( ExpressionExperiment e
547547
}
548548
if ( fetchSize > 0 ) {
549549
AtomicLong numVecs = new AtomicLong();
550-
try ( Stream<SingleCellExpressionDataVector> vectors = helperService.getSingleCellVectors( ee, samples, qt, cs2gene, numVecs, fetchSize, useCursorFetchIfSupported ) ) {
550+
try ( Stream<SingleCellExpressionDataVector> vectors = helperService.streamSingleCellVectors( ee, samples, qt, cs2gene, numVecs, fetchSize, useCursorFetchIfSupported ) ) {
551551
return matrixWriter.write( vectors.peek( createStreamMonitor( ee, qt, ExpressionDataFileServiceImpl.class.getName(), 100, numVecs.get() ) ), cs2gene, writer );
552552
}
553553
} else {
@@ -569,7 +569,7 @@ public int writeCellBrowserSingleCellExpressionData( ExpressionExperiment ee, Qu
569569
}
570570
if ( fetchSize > 0 ) {
571571
AtomicLong numVecs = new AtomicLong();
572-
try ( Stream<SingleCellExpressionDataVector> vectors = helperService.getSingleCellVectors( ee, null, qt, cs2gene, numVecs, fetchSize, useCursorFetchIfSupported ) ) {
572+
try ( Stream<SingleCellExpressionDataVector> vectors = helperService.streamSingleCellVectors( ee, null, qt, cs2gene, numVecs, fetchSize, useCursorFetchIfSupported ) ) {
573573
return matrixWriter.write( vectors.peek( createStreamMonitor( ee, qt, ExpressionDataFileServiceImpl.class.getName(), 100, numVecs.get() ) ), cs2gene, writer );
574574
}
575575
} else {
@@ -657,7 +657,7 @@ private int writeMexSingleCellExpressionDataInternal( ExpressionExperiment ee, @
657657
if ( fetchSize > 0 ) {
658658
Map<BioAssay, Long> nnzBySample = new HashMap<>();
659659
AtomicLong numVecs = new AtomicLong();
660-
try ( Stream<SingleCellExpressionDataVector> vectors = helperService.getSingleCellVectors( ee, samples, qt, cs2gene, numVecs, nnzBySample, fetchSize, useCursorFetchIfSupported ) ) {
660+
try ( Stream<SingleCellExpressionDataVector> vectors = helperService.streamSingleCellVectors( ee, samples, qt, cs2gene, numVecs, nnzBySample, fetchSize, useCursorFetchIfSupported ) ) {
661661
if ( Files.exists( destDir ) ) {
662662
log.info( destDir + " already exists, removing..." );
663663
PathUtils.deleteDirectory( destDir );

0 commit comments

Comments
 (0)