diff --git a/java/src/org/broadinstitute/sting/alignment/bwa/bwt/BWT.java b/java/src/org/broadinstitute/sting/alignment/bwa/bwt/BWT.java index 93ccf2a16..6a4103068 100644 --- a/java/src/org/broadinstitute/sting/alignment/bwa/bwt/BWT.java +++ b/java/src/org/broadinstitute/sting/alignment/bwa/bwt/BWT.java @@ -2,7 +2,6 @@ package org.broadinstitute.sting.alignment.bwa.bwt; import org.broadinstitute.sting.alignment.bwa.packing.PackUtils; import org.broadinstitute.sting.utils.StingException; -import org.broadinstitute.sting.utils.Pair; /** * Represents the Burrows-Wheeler Transform of a reference sequence. diff --git a/java/src/org/broadinstitute/sting/alignment/bwa/bwt/BWTReader.java b/java/src/org/broadinstitute/sting/alignment/bwa/bwt/BWTReader.java index 31e25bbb2..cf5b2b4a8 100644 --- a/java/src/org/broadinstitute/sting/alignment/bwa/bwt/BWTReader.java +++ b/java/src/org/broadinstitute/sting/alignment/bwa/bwt/BWTReader.java @@ -17,7 +17,7 @@ public class BWTReader { /** * Input stream from which to read BWT data. */ - private InputStream inputStream; + private FileInputStream inputStream; /** * Create a new BWT reader. @@ -25,7 +25,7 @@ public class BWTReader { */ public BWTReader( File inputFile ) { try { - this.inputStream = new BufferedInputStream(new FileInputStream(inputFile)); + this.inputStream = new FileInputStream(inputFile); } catch( FileNotFoundException ex ) { throw new StingException("Unable to open input file", ex); diff --git a/java/src/org/broadinstitute/sting/alignment/bwa/bwt/SuffixArrayReader.java b/java/src/org/broadinstitute/sting/alignment/bwa/bwt/SuffixArrayReader.java index f478a42d1..3f6b47cce 100644 --- a/java/src/org/broadinstitute/sting/alignment/bwa/bwt/SuffixArrayReader.java +++ b/java/src/org/broadinstitute/sting/alignment/bwa/bwt/SuffixArrayReader.java @@ -17,7 +17,7 @@ public class SuffixArrayReader { /** * Input stream from which to read suffix array data. */ - private InputStream inputStream; + private FileInputStream inputStream; /** * BWT to use to fill in missing data. @@ -31,7 +31,7 @@ public class SuffixArrayReader { */ public SuffixArrayReader(File inputFile, BWT bwt) { try { - this.inputStream = new BufferedInputStream(new FileInputStream(inputFile)); + this.inputStream = new FileInputStream(inputFile); this.bwt = bwt; } catch( FileNotFoundException ex ) { @@ -57,7 +57,7 @@ public class SuffixArrayReader { intPackedInputStream.read(occurrences); // Throw away the suffix array size in bytes and use the occurrences table directly. suffixArrayInterval = intPackedInputStream.read(); - suffixArray = new int[occurrences[occurrences.length-1]+1/suffixArrayInterval]; + suffixArray = new int[(occurrences[occurrences.length-1]+suffixArrayInterval-1)/suffixArrayInterval]; intPackedInputStream.read(suffixArray); } catch( IOException ex ) { diff --git a/java/src/org/broadinstitute/sting/alignment/bwa/packing/IntPackedInputStream.java b/java/src/org/broadinstitute/sting/alignment/bwa/packing/IntPackedInputStream.java index bdc04d7e2..27aba4460 100644 --- a/java/src/org/broadinstitute/sting/alignment/bwa/packing/IntPackedInputStream.java +++ b/java/src/org/broadinstitute/sting/alignment/bwa/packing/IntPackedInputStream.java @@ -3,6 +3,7 @@ package org.broadinstitute.sting.alignment.bwa.packing; import java.io.*; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.nio.channels.FileChannel; /** * Read a set of integers packed into @@ -14,12 +15,22 @@ public class IntPackedInputStream { /** * Ultimate target for the occurrence array. */ - private final InputStream targetInputStream; + private final FileInputStream targetInputStream; + + /** + * Target channel from which to pull file data. + */ + private final FileChannel targetInputChannel; /** * The byte order in which integer input data appears. */ - private final ByteBuffer buffer; + private final ByteOrder byteOrder; + + /** + * How many bytes are required to store an integer? + */ + private final int bytesPerInteger = PackUtils.bitsInType(Integer.class)/PackUtils.BITS_PER_BYTE; /** * Create a new PackedIntInputStream, writing to the given target file. @@ -36,9 +47,10 @@ public class IntPackedInputStream { * @param inputStream Input stream from which to read ints. * @param byteOrder Endianness to use when writing a list of integers. */ - public IntPackedInputStream(InputStream inputStream, ByteOrder byteOrder) { + public IntPackedInputStream(FileInputStream inputStream, ByteOrder byteOrder) { this.targetInputStream = inputStream; - this.buffer = ByteBuffer.allocate(PackUtils.bitsInType(Integer.class)/ PackUtils.BITS_PER_BYTE).order(byteOrder); + this.targetInputChannel = inputStream.getChannel(); + this.byteOrder = byteOrder; } /** @@ -69,11 +81,15 @@ public class IntPackedInputStream { * @throws IOException if an I/O error occurs. */ public void read( int[] data, int offset, int length ) throws IOException { - for(int i = offset; i < offset+length; i++) { - targetInputStream.read(buffer.array()); - data[i] = buffer.getInt(); - buffer.rewind(); - } + ByteBuffer readBuffer = ByteBuffer.allocate(bytesPerInteger*length).order(byteOrder); + + targetInputChannel.read(readBuffer,targetInputChannel.position()); + readBuffer.flip(); + targetInputChannel.position(targetInputChannel.position()+readBuffer.remaining()); + + int i = 0; + while(i < length) + data[offset+i++] = readBuffer.getInt(); } /**