final Map<SAMReadGroupRecord, FastqWriters> writerMap = new HashMap<SAMReadGroupRecord, FastqWriters>();
final FastqWriters fastqWriters;
if (!OUTPUT_PER_RG) {
IOUtil.assertFileIsWritable(FASTQ);
final FastqWriter firstOfPairWriter = factory.newWriter(FASTQ);
final FastqWriter secondOfPairWriter;
if (INTERLEAVE) {
secondOfPairWriter = firstOfPairWriter;
} else if (SECOND_END_FASTQ != null) {
IOUtil.assertFileIsWritable(SECOND_END_FASTQ);
secondOfPairWriter = factory.newWriter(SECOND_END_FASTQ);
} else {
secondOfPairWriter = null;
}
/** Prepare the writer that will accept unpaired reads. If we're emitting a single fastq - and assuming single-ended reads -
* then this is simply that one fastq writer. Otherwise, if we're doing paired-end, we emit to a third new writer, since
* the other two fastqs are accepting only paired end reads. */
final FastqWriter unpairedWriter = UNPAIRED_FASTQ == null ? firstOfPairWriter : factory.newWriter(UNPAIRED_FASTQ);
fastqWriters = new FastqWriters(firstOfPairWriter, secondOfPairWriter, unpairedWriter);
// For all read groups we may find in the bam, register this single set of writers for them.
writerMap.put(null, fastqWriters);
for (final SAMReadGroupRecord rg : samReadGroupRecords) {
writerMap.put(rg, fastqWriters);
}
} else {
// When we're creating a fastq-group per readgroup, by convention we do not emit a special fastq for unpaired reads.
for (final SAMReadGroupRecord rg : samReadGroupRecords) {
final FastqWriter firstOfPairWriter = factory.newWriter(makeReadGroupFile(rg, "_1"));
// Create this writer on-the-fly; if we find no second-of-pair reads, don't bother making a writer (or delegating,
// if we're interleaving).
final Lazy<FastqWriter> lazySecondOfPairWriter = new Lazy<FastqWriter>(new Lazy.LazyInitializer<FastqWriter>() {
@Override
public FastqWriter make() {