Skip to content

Commit

Permalink
resolving some nitpicks from pr #1245 (#1246)
Browse files Browse the repository at this point in the history
* resolving some nitpicks from pr #1245
  • Loading branch information
lbergelson authored Dec 21, 2018
1 parent 2473407 commit a4b7da8
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/main/java/htsjdk/samtools/SamStreams.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static boolean isBAMFile(final InputStream stream)
/**
* Checks whether the file is a gzipped sam file. Returns true if it
* is and false otherwise.
* @see @link IOUtil#isGZIPInputStream(InputStream)
* @deprecated use {@link IOUtil#isGZIPInputStream(InputStream)} instead
*/
@Deprecated
public static boolean isGzippedSAMFile(final InputStream stream) {
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/htsjdk/samtools/util/IOUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public class IOUtil {
public static final String COMPRESSED_VCF_FILE_EXTENSION = ".vcf.gz";
public static final String COMPRESSED_VCF_INDEX_EXTENSION = ".tbi";


/** Possible extensions for VCF files and related formats. */
public static final String[] VCF_EXTENSIONS = {VCF_FILE_EXTENSION, COMPRESSED_VCF_FILE_EXTENSION, BCF_FILE_EXTENSION};

Expand All @@ -110,6 +109,9 @@ public class IOUtil {

public static final Set<String> BLOCK_COMPRESSED_EXTENSIONS = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(".gz", ".gzip", ".bgz", ".bgzf")));

/** number of bytes that will be read for the GZIP-header in the function {@link #isGZIPInputStream(InputStream)} */
public static final int GZIP_HEADER_READ_LENGTH = 8000;

private static int compressionLevel = Defaults.COMPRESSION_LEVEL;
/**
* Sets the GZip compression level for subsequent GZIPOutputStream object creation.
Expand Down Expand Up @@ -1155,18 +1157,14 @@ public static List<Path> filesToPaths(Collection<File> files){
return files.stream().map(File::toPath).collect(Collectors.toList());
}

/** number of bytes that will be read for the GZIP-header in the function {@link #isGZIPInputStream(InputStream)} */
public static final int GZIP_HEADER_READ_LENGTH = 8000;

/**
* Test whether a input stream looks like a GZIP input.
* This identifies both gzip and bgzip streams as being GZIP.
* @param stream the input stream.
* @return true if `stream` starts with a gzip signature
* @return true if `stream` starts with a gzip signature.
* @throws IllegalArgumentException if `stream` cannot mark or reset the stream
* @see SamStreams#isGzippedSAMFile(InputStream)
*/
public static boolean isGZIPInputStream(final InputStream stream) {
/* this function was previously implemented in SamStreams.isGzippedSAMFile */
if (!stream.markSupported()) {
throw new IllegalArgumentException("isGZIPInputStream() : Cannot test a stream that doesn't support marking.");
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/htsjdk/variant/utils/VCFHeaderReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import htsjdk.samtools.SamStreams;
import htsjdk.samtools.cram.io.InputStreamUtils;
import htsjdk.samtools.seekablestream.SeekableStream;
import htsjdk.samtools.util.IOUtil;
import htsjdk.tribble.Feature;
import htsjdk.tribble.FeatureCodec;
import htsjdk.tribble.FeatureCodecHeader;
Expand Down Expand Up @@ -49,8 +50,8 @@ public static VCFHeader readHeaderFrom(final SeekableStream in) throws IOExcepti

private static InputStream bufferAndDecompressIfNecessary(final InputStream in) throws IOException {
BufferedInputStream bis = new BufferedInputStream(in);
// despite the name, SamStreams.isGzippedSAMFile looks for any gzipped stream (including block compressed)
return SamStreams.isGzippedSAMFile(bis) ? new GZIPInputStream(bis) : bis;
// IOUTil.isGZIPInputStream looks for any gzipped stream (including block compressed)
return IOUtil.isGZIPInputStream(bis) ? new GZIPInputStream(bis) : bis;
}

private static <FEATURE_TYPE extends Feature, SOURCE> VCFHeader readHeaderFrom(final InputStream in, final FeatureCodec<FEATURE_TYPE, SOURCE> featureCodec) throws IOException {
Expand Down
18 changes: 4 additions & 14 deletions src/main/java/htsjdk/variant/vcf/VCFIteratorBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,12 @@ public VCFIterator open(final InputStream in) throws IOException {
}
// wrap the input stream into a BufferedInputStream to reset/read a BCFHeader or a GZIP
// buffer must be large enough to contain the BCF header and/or GZIP signature
BufferedInputStream bufferedinput = new BufferedInputStream(in, Math.max(
BCF2Codec.SIZEOF_BCF_HEADER,
IOUtil.GZIP_HEADER_READ_LENGTH
));
BufferedInputStream bufferedinput = new BufferedInputStream(in, Math.max(BCF2Codec.SIZEOF_BCF_HEADER, IOUtil.GZIP_HEADER_READ_LENGTH));
// test for gzipped inputstream
if(IOUtil.isGZIPInputStream(bufferedinput)) {
// this is a gzipped input stream, wrap it into GZIPInputStream
// and re-wrap it into BufferedInputStream so we can test for the BCF header
bufferedinput = new BufferedInputStream(
new GZIPInputStream(bufferedinput),
BCF2Codec.SIZEOF_BCF_HEADER
);
bufferedinput = new BufferedInputStream(new GZIPInputStream(bufferedinput), BCF2Codec.SIZEOF_BCF_HEADER);
}

// try to read a BCF header
Expand Down Expand Up @@ -190,9 +184,7 @@ public VCFHeader getHeader() {

@Override
protected VariantContext advance() {
return this.lineIterator.hasNext() ?
this.codec.decode(this.lineIterator.next()) :
null;
return this.lineIterator.hasNext() ? this.codec.decode(this.lineIterator.next()) : null;
}

@Override
Expand Down Expand Up @@ -225,9 +217,7 @@ public VCFHeader getHeader() {

@Override
protected VariantContext advance() {
return this.codec.isDone(this.inputStream) ?
null :
this.codec.decode(this.inputStream);
return this.codec.isDone(this.inputStream) ? null : this.codec.decode(this.inputStream);
}

@Override
Expand Down
1 change: 1 addition & 0 deletions src/test/java/htsjdk/samtools/SamStreamsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class SamStreamsTest extends HtsjdkTest {
private static final File TEST_DATA_DIR = new File("src/test/resources/htsjdk/samtools");

@Test(dataProvider = "makeData")
@SuppressWarnings("deprecated") // we're testing a deprecated method here deliberately
public void testDataFormat(final String inputFile, final boolean isGzippedSAMFile, final boolean isBAMFile, final boolean isCRAMFile) throws Exception {
final File input = new File(TEST_DATA_DIR, inputFile);
try(final InputStream fis = new BufferedInputStream(new FileInputStream(input))) { //must be buffered or the isGzippedSAMFile will blow up
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/htsjdk/samtools/util/IOUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -733,8 +733,8 @@ private static byte[] gzipMessage(final byte[] message) throws IOException {

@DataProvider
public static Object[][] gzipTests() throws IOException {
final byte emptyMessage[]="".getBytes();
final byte message[]="Hello World".getBytes();
final byte[] emptyMessage = "".getBytes();
final byte[] message = "Hello World".getBytes();

// Compressed version of the messages
final byte[] gzippedMessage = gzipMessage(message);
Expand Down

0 comments on commit a4b7da8

Please sign in to comment.