29
29
import htsjdk .samtools .util .GZIIndex ;
30
30
import htsjdk .samtools .util .IOUtil ;
31
31
import htsjdk .tribble .readers .AsciiLineReader ;
32
+ import htsjdk .tribble .readers .PositionalBufferedStream ;
32
33
33
34
import java .io .IOException ;
35
+ import java .io .InputStream ;
34
36
import java .nio .file .Files ;
35
37
import java .nio .file .Path ;
38
+ import java .util .zip .GZIPInputStream ;
36
39
37
40
/**
38
41
* Static methods to create an {@link FastaSequenceIndex}.
@@ -65,6 +68,21 @@ public static void create(final Path fastaFile, final boolean overwrite) throws
65
68
index .write (indexFile );
66
69
}
67
70
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
+
68
86
/**
69
87
* Builds a FastaSequenceIndex on the fly from a FASTA file.
70
88
*
@@ -79,8 +97,8 @@ public static void create(final Path fastaFile, final boolean overwrite) throws
79
97
* @throws SAMException for formatting errors.
80
98
* @throws IOException if an IO error occurs.
81
99
*/
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 ) ))) {
84
102
85
103
// sanity check reference format:
86
104
// 1. Non-empty file
0 commit comments