Source:
protected int doWork() { IOUtil.assertFileIsReadable(INPUT); IOUtil.assertFileIsReadable(REFERENCE); IOUtil.assertFileIsWritable(OUTPUT); final SamReader in = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(INPUT); ReferenceSequenceFile reference = ReferenceSequenceFileFactory.getReferenceSequenceFile(REFERENCE); SAMSequenceDictionary refDict = reference.getSequenceDictionary(); if (refDict == null) { log.error("No reference sequence dictionary found. Aborting. You can create a sequence dictionary for the reference fasta using CreateSequenceDictionary.jar."); CloserUtil.close(in); return 1; } printDictionary("SAM/BAM/CRAM file", in.getFileHeader().getSequenceDictionary()); printDictionary("Reference", refDict); Map<Integer, Integer> newOrder = buildSequenceDictionaryMap(refDict, in.getFileHeader().getSequenceDictionary()); SAMFileHeader outHeader = in.getFileHeader().clone(); outHeader.setSequenceDictionary(refDict); log.info("Writing reads..."); if (in.hasIndex()) { final SAMFileWriter out = new SAMFileWriterFactory().makeWriter(outHeader, true, OUTPUT, REFERENCE_SEQUENCE); for (final SAMSequenceRecord contig : refDict.getSequences()) { final SAMRecordIterator it = in.query(contig.getSequenceName(), 0, 0, false); writeReads(out, it, newOrder, contig.getSequenceName()); } writeReads(out, in.queryUnmapped(), newOrder, "unmapped"); out.close(); } else { SAMFileWriter out = new SAMFileWriterFactory().makeWriter(outHeader, false, OUTPUT, REFERENCE_SEQUENCE); writeReads(out, in.iterator(), newOrder, "All reads"); out.close(); } CloserUtil.close(in); return 0; }

Target:
protected int doWork() { IOUtil.assertFileIsReadable(INPUT); IOUtil.assertFileIsReadable(REFERENCE_SEQUENCE); IOUtil.assertFileIsWritable(OUTPUT); final SamReader in = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(INPUT); ReferenceSequenceFile reference = ReferenceSequenceFileFactory.getReferenceSequenceFile(REFERENCE_SEQUENCE); SAMSequenceDictionary refDict = reference.getSequenceDictionary(); if (refDict == null) { log.error("No reference sequence dictionary found. Aborting. You can create a sequence dictionary for the reference fasta using CreateSequenceDictionary.jar."); CloserUtil.close(in); return 1; } printDictionary("SAM/BAM/CRAM file", in.getFileHeader().getSequenceDictionary()); printDictionary("Reference", refDict); Map<Integer, Integer> newOrder = buildSequenceDictionaryMap(refDict, in.getFileHeader().getSequenceDictionary()); SAMFileHeader outHeader = in.getFileHeader().clone(); outHeader.setSequenceDictionary(refDict); log.info("Writing reads..."); if (in.hasIndex()) { final SAMFileWriter out = new SAMFileWriterFactory().makeWriter(outHeader, true, OUTPUT, REFERENCE_SEQUENCE); for (final SAMSequenceRecord contig : refDict.getSequences()) { final SAMRecordIterator it = in.query(contig.getSequenceName(), 0, 0, false); writeReads(out, it, newOrder, contig.getSequenceName()); } writeReads(out, in.queryUnmapped(), newOrder, "unmapped"); out.close(); } else { SAMFileWriter out = new SAMFileWriterFactory().makeWriter(outHeader, false, OUTPUT, REFERENCE_SEQUENCE); writeReads(out, in.iterator(), newOrder, "All reads"); out.close(); } CloserUtil.close(in); return 0; }