htsjdk icon indicating copy to clipboard operation
htsjdk copied to clipboard

Unnecessary IllegalStateException "no sequences were added to the reference" in FastaReferenceWriter

Open d-cameron opened this issue 3 years ago • 1 comments

Description of the issue:

FastaReferenceWriter throws an exception if no sequences were written:

Exception in thread "main" java.lang.IllegalStateException: no sequences were added to the reference
	at htsjdk.samtools.reference.FastaReferenceWriter.close(FastaReferenceWriter.java:635)

Steps to reproduce

try (FastaReferenceWriter writer = new FastaReferenceWriterBuilder().setFastaFile(new File("test.fa".toPath()).build()) {
}

Expected behaviour

An empty test.fa is created.

Actual behaviour

An exception is thrown.

For my particular use case, I am writing the sequences of all viruses detected in a sample. In this use case, an empty fasta file is a valid output.

I see the following possible resolutions to this issue:

  1. Never throw the exception.
  2. Add a set method to FastaReferenceWriterFactory to allow for the creation of empty fasta files.

d-cameron avatar Aug 11 '20 07:08 d-cameron

@d-cameron Relaxing this restriction seems like a good idea. I don't remember why we made this particular writer so strict about empty files, I suspect it was inspired by an issue someone ran into. Making it configurable would be a good solution so it doesn't change things for people who are expecting it.

lbergelson avatar Sep 24 '20 19:09 lbergelson