InterleaveFastq implements Callable<Integer> { @Override public Integer call() throws Exception { BufferedReader firstReader = null; BufferedReader secondReader = null; PrintWriter pairedWriter = null; PrintWriter unpairedWriter = null; try { firstReader = reader(firstFastqFile); secondReader = reader(secondFastqFile); pairedWriter = writer(pairedFile); unpairedWriter = writer(unpairedFile); final PrintWriter pw = pairedWriter; final PrintWriter uw = unpairedWriter; final FastqWriter pairedFastqWriter = new SangerFastqWriter(); final FastqWriter unpairedFastqWriter = new SangerFastqWriter(); PairedEndFastqReader.streamPaired(firstReader, secondReader, new PairedEndAdapter() { @Override public void paired(final Fastq left, final Fastq right) { try { pairedFastqWriter.append(pw, left); pairedFastqWriter.append(pw, right); } catch (IOException e) { throw new RuntimeException("could not write paired end reads", e); } } @Override public void unpaired(final Fastq unpaired) { try { unpairedFastqWriter.append(uw, unpaired); } catch (IOException e) { throw new RuntimeException("could not write unpaired read", e); } } }); return 0; } finally { try { firstReader.close(); } catch (Exception e) { } try { secondReader.close(); } catch (Exception e) { } try { pairedWriter.close(); } catch (Exception e) { } try { unpairedWriter.close(); } catch (Exception e) { } } } InterleaveFastq(final File firstFastqFile, final File secondFastqFile, final File pairedFile, final File unpairedFile); @Override Integer call(); static void main(final String[] args);  }
@Test public void testInterleaveFastqBackslash() throws Exception { copyResource("paired_backslash_R1.fq.gz", firstFastqFile); copyResource("paired_backslash_R2.fq.gz", secondFastqFile); new InterleaveFastq(firstFastqFile, secondFastqFile, pairedFile, unpairedFile).call(); assertEquals(16, countFastq(pairedFile)); assertEquals(0, countFastq(unpairedFile)); }
@Test public void testInterleaveFastqUnpaired() throws Exception { copyResource("unpaired_R1.fq.gz", firstFastqFile); copyResource("unpaired_R2.fq.gz", secondFastqFile); new InterleaveFastq(firstFastqFile, secondFastqFile, pairedFile, unpairedFile).call(); assertEquals(4, countFastq(pairedFile)); assertEquals(2, countFastq(unpairedFile)); }