Source:
<code>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()) { <START> final SAMFileWriter out = new SAMFileWriterFactory().makeWriter(outHeader, true, OUTPUT, REFERENCE_SEQUENCE); <END> 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; }</code><technical_language>For some reason (probably a convenient override optional attribute for REFERENCE for this tool) ReorderSam a duplicate command line arg for reference. is in ReorderSam class called REFERENCE, inherited CommandLineProgram called REFERENCE_SEQUENCE. of in tool code, REFERENCE is properly populated command line parser. This code fixed variable usage is consistent. remove local REFERENCE variable rely inherited REFERENCE_SEQUENCE one, code throw if argument is populated. I retain behavior remove ambiguity</technical_language>

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; }