From 35e013ed7e32dd64f1fe9fdc44497b180271c305 Mon Sep 17 00:00:00 2001 From: Tom White Date: Mon, 21 Jan 2019 14:48:33 +0000 Subject: [PATCH] StreamBasedTabixIndexCreator --- .../tabix/StreamBasedTabixIndexCreator.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/main/java/htsjdk/tribble/index/tabix/StreamBasedTabixIndexCreator.java diff --git a/src/main/java/htsjdk/tribble/index/tabix/StreamBasedTabixIndexCreator.java b/src/main/java/htsjdk/tribble/index/tabix/StreamBasedTabixIndexCreator.java new file mode 100644 index 0000000000..c638fc9cf1 --- /dev/null +++ b/src/main/java/htsjdk/tribble/index/tabix/StreamBasedTabixIndexCreator.java @@ -0,0 +1,55 @@ +package htsjdk.tribble.index.tabix; + +import htsjdk.samtools.BinningIndexContent; +import htsjdk.samtools.SAMSequenceDictionary; +import htsjdk.samtools.util.BlockCompressedOutputStream; +import htsjdk.tribble.index.Index; +import htsjdk.tribble.util.LittleEndianOutputStream; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.Path; +import java.util.List; + +public class StreamBasedTabixIndexCreator extends TabixIndexCreator { + + static class StreamBasedTabixIndex extends TabixIndex { + private final OutputStream out; + + StreamBasedTabixIndex( + TabixFormat formatSpec, + List sequenceNames, + BinningIndexContent[] indices, + OutputStream out) { + super(formatSpec, sequenceNames, indices); + this.out = out; + } + + @Override + public void writeBasedOnFeaturePath(final Path featurePath) throws IOException { + try (final LittleEndianOutputStream los = + new LittleEndianOutputStream(new BlockCompressedOutputStream(out, (Path) null))) { + write(los); + } + } + } + + private final OutputStream out; + + public StreamBasedTabixIndexCreator( + SAMSequenceDictionary sequenceDictionary, TabixFormat formatSpec, OutputStream out) { + super(sequenceDictionary, formatSpec); + this.out = out; + } + + @Override + public Index finalizeIndex(long finalFilePosition) { + Index index = super.finalizeIndex(finalFilePosition); + TabixIndex tabixIndex = (TabixIndex) index; + return new StreamBasedTabixIndex( + tabixIndex.getFormatSpec(), + tabixIndex.getSequenceNames(), + tabixIndex.getIndices(), + out); + } +}