Skip to content

Commit

Permalink
GATKBAMIndex calls buffer.length() on every read. This is causing mu…
Browse files Browse the repository at this point in the history
…ch pain.

 Optimized by getting the read of the file upon opening the index-file and using that instead.
  • Loading branch information
Yossi Farjoun committed Apr 29, 2013
1 parent c5701a9 commit 0e7e6d3
Showing 1 changed file with 10 additions and 6 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@


package org.broadinstitute.sting.gatk.datasources.reads; package org.broadinstitute.sting.gatk.datasources.reads;


import net.sf.samtools.Bin;
import net.sf.samtools.GATKBin;
import net.sf.samtools.GATKChunk;
import net.sf.samtools.LinearIndex;
import net.sf.samtools.seekablestream.SeekableBufferedStream; import net.sf.samtools.seekablestream.SeekableBufferedStream;
import net.sf.samtools.seekablestream.SeekableFileStream; import net.sf.samtools.seekablestream.SeekableFileStream;

import net.sf.samtools.*;

import org.broadinstitute.sting.utils.exceptions.ReviewedStingException; import org.broadinstitute.sting.utils.exceptions.ReviewedStingException;
import org.broadinstitute.sting.utils.exceptions.StingException;
import org.broadinstitute.sting.utils.exceptions.UserException; import org.broadinstitute.sting.utils.exceptions.UserException;


import java.io.*; import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.util.ArrayList; import java.util.ArrayList;
Expand Down Expand Up @@ -86,6 +87,7 @@ public class GATKBAMIndex {


private SeekableFileStream fileStream; private SeekableFileStream fileStream;
private SeekableBufferedStream bufferedStream; private SeekableBufferedStream bufferedStream;
private Long fileLength;


public GATKBAMIndex(final File file) { public GATKBAMIndex(final File file) {
mFile = file; mFile = file;
Expand Down Expand Up @@ -307,6 +309,7 @@ private void openIndexFile() {
try { try {
fileStream = new SeekableFileStream(mFile); fileStream = new SeekableFileStream(mFile);
bufferedStream = new SeekableBufferedStream(fileStream,BUFFERED_STREAM_BUFFER_SIZE); bufferedStream = new SeekableBufferedStream(fileStream,BUFFERED_STREAM_BUFFER_SIZE);
fileLength=bufferedStream.length();
} }
catch (IOException exc) { catch (IOException exc) {
throw new ReviewedStingException("Unable to open index file (" + exc.getMessage() +")" + mFile, exc); throw new ReviewedStingException("Unable to open index file (" + exc.getMessage() +")" + mFile, exc);
Expand All @@ -317,6 +320,7 @@ private void closeIndexFile() {
try { try {
bufferedStream.close(); bufferedStream.close();
fileStream.close(); fileStream.close();
fileLength=null;
} }
catch (IOException exc) { catch (IOException exc) {
throw new ReviewedStingException("Unable to close index file " + mFile, exc); throw new ReviewedStingException("Unable to close index file " + mFile, exc);
Expand Down Expand Up @@ -368,7 +372,7 @@ private void read(final ByteBuffer buffer) {
// We have a rigid expectation here to read in exactly the number of bytes we've limited // We have a rigid expectation here to read in exactly the number of bytes we've limited
// our buffer to -- if there isn't enough data in the file, the index // our buffer to -- if there isn't enough data in the file, the index
// must be truncated or otherwise corrupt: // must be truncated or otherwise corrupt:
if(bytesRequested > bufferedStream.length() - bufferedStream.position()){ if(bytesRequested > fileLength - bufferedStream.position()){
throw new UserException.MalformedFile(mFile, String.format("Premature end-of-file while reading BAM index file %s. " + throw new UserException.MalformedFile(mFile, String.format("Premature end-of-file while reading BAM index file %s. " +
"It's likely that this file is truncated or corrupt -- " + "It's likely that this file is truncated or corrupt -- " +
"Please try re-indexing the corresponding BAM file.", "Please try re-indexing the corresponding BAM file.",
Expand Down

0 comments on commit 0e7e6d3

Please sign in to comment.