Source:
void setupBuilder() throws IOException { final int numReads = 100; final String flowCellBarcode = "TESTBARCODE"; final String readName = flowCellBarcode; tempSamFile = File.createTempFile("CollectGcBias", ".bam", TEST_DIR); File tempSamFileUnsorted = File.createTempFile("CollectGcBias", ".bam", TEST_DIR); BufferedLineReader bufferedLineReader = null; try { bufferedLineReader = new BufferedLineReader(new FileInputStream(dict)); } catch (FileNotFoundException e) { e.printStackTrace(); } final SAMTextHeaderCodec codec = new SAMTextHeaderCodec(); final SAMFileHeader header = codec.decode(bufferedLineReader, dict.toString()); header.setSortOrder(SAMFileHeader.SortOrder.unsorted); setup(numReads, readName, 1, readGroupId1, readGroupRecord1, sample1, library1, header, setBuilder1); setup(numReads, readName, 2, readGroupId2, readGroupRecord2, sample1, library2, header, setBuilder2); setup(numReads, readName, 3, readGroupId3, readGroupRecord3, sample2, library3, header, setBuilder3); final SAMFileWriter writer = new SAMFileWriterFactory() .setCreateIndex(true).makeBAMWriter(header, false, tempSamFileUnsorted); for (final SAMRecord record : setBuilder1) { writer.addAlignment(record); } for (final SAMRecord record : setBuilder2) { writer.addAlignment(record); } for (final SAMRecord record : setBuilder3) { writer.addAlignment(record); } writer.close(); final SortSam sorter = new SortSam(); final String[] args = new String[]{"INPUT=" + tempSamFileUnsorted.getAbsolutePath(), "OUTPUT=" + tempSamFile.getAbsolutePath(), "SORT_ORDER=coordinate"}; sorter.instanceMain(args); IOUtil.deleteFiles(tempSamFileUnsorted); }

Target:
void setupBuilder() throws IOException { final int numReads = 100; final String flowCellBarcode = "TESTBARCODE"; final String readName = flowCellBarcode; tempSamFile = File.createTempFile("CollectGcBias", ".bam", TEST_DIR); File tempSamFileUnsorted = File.createTempFile("CollectGcBias", ".bam", TEST_DIR); tempSamFileUnsorted.deleteOnExit(); tempSamFile.deleteOnExit(); BufferedLineReader bufferedLineReader = null; try { bufferedLineReader = new BufferedLineReader(new FileInputStream(dict)); } catch (FileNotFoundException e) { e.printStackTrace(); } final SAMTextHeaderCodec codec = new SAMTextHeaderCodec(); final SAMFileHeader header = codec.decode(bufferedLineReader, dict.toString()); header.setSortOrder(SAMFileHeader.SortOrder.unsorted); setup(numReads, readName, 1, readGroupId1, readGroupRecord1, sample1, library1, header, setBuilder1); setup(numReads, readName, 2, readGroupId2, readGroupRecord2, sample1, library2, header, setBuilder2); setup(numReads, readName, 3, readGroupId3, readGroupRecord3, sample2, library3, header, setBuilder3); final SAMFileWriter writer = new SAMFileWriterFactory() .setCreateIndex(true).makeBAMWriter(header, false, tempSamFileUnsorted); for (final SAMRecord record : setBuilder1) { writer.addAlignment(record); } for (final SAMRecord record : setBuilder2) { writer.addAlignment(record); } for (final SAMRecord record : setBuilder3) { writer.addAlignment(record); } writer.close(); final SortSam sorter = new SortSam(); final String[] args = new String[]{"INPUT=" + tempSamFileUnsorted.getAbsolutePath(), "OUTPUT=" + tempSamFile.getAbsolutePath(), "SORT_ORDER=coordinate"}; sorter.instanceMain(args); }