Skip to content

Commit

Permalink
Silence AsciiLineReader warning when creating a FASTA sequence index (#…
Browse files Browse the repository at this point in the history
…1559)

* Silence AsciiLineReader warning when creating a FASTA sequence index
  • Loading branch information
clintval authored Apr 27, 2022
1 parent 1449dec commit f461401
Showing 1 changed file with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@
import htsjdk.samtools.util.GZIIndex;
import htsjdk.samtools.util.IOUtil;
import htsjdk.tribble.readers.AsciiLineReader;
import htsjdk.tribble.readers.PositionalBufferedStream;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.zip.GZIPInputStream;

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

/**
* Wrap only non-GZIP input streams as a positional buffered input stream for use in {@link AsciiLineReader#from(InputStream)}
*
* @param input the input stream.
*
* @return the input stream which is either a GZIP input stream or a position buffered stream.
*/
private static InputStream optionallyWrapAsPositional(final InputStream input) {
if (input instanceof GZIPInputStream) {
return input;
} else {
return new PositionalBufferedStream(input);
}
}

/**
* Builds a FastaSequenceIndex on the fly from a FASTA file.
*
Expand All @@ -79,8 +97,8 @@ public static void create(final Path fastaFile, final boolean overwrite) throws
* @throws SAMException for formatting errors.
* @throws IOException if an IO error occurs.
*/
public static FastaSequenceIndex buildFromFasta(final Path fastaFile) throws IOException {
try(final AsciiLineReader in = AsciiLineReader.from(IOUtil.openFileForReading(fastaFile))) {
public static FastaSequenceIndex buildFromFasta(final Path fastaFile) throws IOException, SAMException {
try(final AsciiLineReader in = AsciiLineReader.from(optionallyWrapAsPositional(IOUtil.openFileForReading(fastaFile)))) {

// sanity check reference format:
// 1. Non-empty file
Expand Down

0 comments on commit f461401

Please sign in to comment.