final SAMFileHeader outHeader = inHeader.clone();
if (null == SORT_ORDER) this.SORT_ORDER = inHeader.getSortOrder(); // same as the input
outHeader.setSortOrder(SORT_ORDER);
SAMFileWriterFactory.setDefaultCreateIndexWhileWriting(CREATE_INDEX);
SAMFileWriterFactory.setDefaultCreateMd5File(CREATE_MD5_FILE);
final SAMFileWriter out = new SAMFileWriterFactory().makeSAMOrBAMWriter(outHeader, false, OUTPUT);
// Iterate over the records, revert original base qualities, and push them into a SortingCollection by queryname
final SortingCollection<SAMRecord> sorter = SortingCollection.newInstance(SAMRecord.class, new BAMRecordCodec(outHeader),
new SAMRecordQueryNameComparator(), MAX_RECORDS_IN_RAM);
final ProgressLogger revertingProgress = new ProgressLogger(log, 1000000, " reverted OQs");