samtools / htsjdk

A Java API for high-throughput sequencing data (HTS) formats.
http://samtools.github.io/htsjdk/
283 stars 242 forks source link

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

Open d-cameron opened 4 years ago

d-cameron commented 4 years ago

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.
lbergelson commented 4 years ago

@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.