Skip to content

Commit

Permalink
Add CRAIIndexMerger
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwhite committed Jun 18, 2019
1 parent febcf0f commit f18aa28
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/main/java/htsjdk/samtools/cram/CRAIIndexMerger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package htsjdk.samtools.cram;

import htsjdk.samtools.IndexMerger;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream;

public class CRAIIndexMerger extends IndexMerger<CRAIIndex> {

private GZIPOutputStream compressedOut;
private long offset;

public CRAIIndexMerger(final OutputStream out, final long headerLength) throws IOException {
super(out, headerLength);
this.compressedOut = new GZIPOutputStream(new BufferedOutputStream(out));
this.offset = headerLength;
}

@Override
public void processIndex(CRAIIndex index, long partLength) {
index.getCRAIEntries()
.forEach(e -> shift(e, offset).writeToStream(compressedOut));
offset += partLength;
}

private static CRAIEntry shift(CRAIEntry entry, long offset) {
return new CRAIEntry(entry.getSequenceId(), entry.getAlignmentStart(), entry.getAlignmentSpan(), entry.getContainerStartByteOffset() + offset, entry.getSliceByteOffsetFromCompressionHeaderStart(), entry.getSliceByteSize());
}

@Override
public void finish(long dataFileLength) throws IOException {
compressedOut.flush();
compressedOut.close();
}
}

0 comments on commit f18aa28

Please sign in to comment.