Skip to content

Commit f461401

Browse files
authored
Silence AsciiLineReader warning when creating a FASTA sequence index (#1559)
* Silence AsciiLineReader warning when creating a FASTA sequence index
1 parent 1449dec commit f461401

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/main/java/htsjdk/samtools/reference/FastaSequenceIndexCreator.java

+20-2
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@
2929
import htsjdk.samtools.util.GZIIndex;
3030
import htsjdk.samtools.util.IOUtil;
3131
import htsjdk.tribble.readers.AsciiLineReader;
32+
import htsjdk.tribble.readers.PositionalBufferedStream;
3233

3334
import java.io.IOException;
35+
import java.io.InputStream;
3436
import java.nio.file.Files;
3537
import java.nio.file.Path;
38+
import java.util.zip.GZIPInputStream;
3639

3740
/**
3841
* Static methods to create an {@link FastaSequenceIndex}.
@@ -65,6 +68,21 @@ public static void create(final Path fastaFile, final boolean overwrite) throws
6568
index.write(indexFile);
6669
}
6770

71+
/**
72+
* Wrap only non-GZIP input streams as a positional buffered input stream for use in {@link AsciiLineReader#from(InputStream)}
73+
*
74+
* @param input the input stream.
75+
*
76+
* @return the input stream which is either a GZIP input stream or a position buffered stream.
77+
*/
78+
private static InputStream optionallyWrapAsPositional(final InputStream input) {
79+
if (input instanceof GZIPInputStream) {
80+
return input;
81+
} else {
82+
return new PositionalBufferedStream(input);
83+
}
84+
}
85+
6886
/**
6987
* Builds a FastaSequenceIndex on the fly from a FASTA file.
7088
*
@@ -79,8 +97,8 @@ public static void create(final Path fastaFile, final boolean overwrite) throws
7997
* @throws SAMException for formatting errors.
8098
* @throws IOException if an IO error occurs.
8199
*/
82-
public static FastaSequenceIndex buildFromFasta(final Path fastaFile) throws IOException {
83-
try(final AsciiLineReader in = AsciiLineReader.from(IOUtil.openFileForReading(fastaFile))) {
100+
public static FastaSequenceIndex buildFromFasta(final Path fastaFile) throws IOException, SAMException {
101+
try(final AsciiLineReader in = AsciiLineReader.from(optionallyWrapAsPositional(IOUtil.openFileForReading(fastaFile)))) {
84102

85103
// sanity check reference format:
86104
// 1. Non-empty file

0 commit comments

Comments
 (0)