Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolving some nitpicks from pr #1245 #1246

Merged
merged 2 commits into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 7 additions & 5 deletions src/main/java/htsjdk/samtools/util/IOUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public class IOUtil {
public static final String COMPRESSED_VCF_INDEX_EXTENSION = ".tbi";



Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why extra nl?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accident...


/** 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 +112,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 +1160,15 @@ 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
3 changes: 2 additions & 1 deletion 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 @@ -50,7 +51,7 @@ 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update comment

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return SamStreams.isGzippedSAMFile(bis) ? new GZIPInputStream(bis) : bis;
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