htsjdk
htsjdk copied to clipboard
Unnecessary IllegalStateException "no sequences were added to the reference" in FastaReferenceWriter
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:
- Never throw the exception.
- Add a set method to FastaReferenceWriterFactory to allow for the creation of empty fasta files.
@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.