final boolean includeSecondary, final int expectedFirstMapq,
final int expectedSecondMapq) throws Exception {
final File unmappedSam = File.createTempFile("unmapped.", ".sam");
unmappedSam.deleteOnExit();
final SAMFileWriterFactory factory = new SAMFileWriterFactory();
final SAMFileHeader header = new SAMFileHeader();
header.setSortOrder(SAMFileHeader.SortOrder.queryname);
final String readName = "theRead";
final SAMRecord firstUnmappedRead = new SAMRecord(header);
firstUnmappedRead.setReadName(readName);
firstUnmappedRead.setReadString("ACGTACGTACGTACGT");
firstUnmappedRead.setBaseQualityString("5555555555555555");
firstUnmappedRead.setReadUnmappedFlag(true);
firstUnmappedRead.setMateUnmappedFlag(true);
firstUnmappedRead.setReadPairedFlag(true);
firstUnmappedRead.setFirstOfPairFlag(true);
final SAMRecord secondUnmappedRead = new SAMRecord(header);
secondUnmappedRead.setReadName(readName);
secondUnmappedRead.setReadString("TCGAACGTTCGAACTG");
secondUnmappedRead.setBaseQualityString("6666666666666666");
secondUnmappedRead.setReadUnmappedFlag(true);
secondUnmappedRead.setMateUnmappedFlag(true);
secondUnmappedRead.setReadPairedFlag(true);
secondUnmappedRead.setSecondOfPairFlag(true);
final SAMFileWriter unmappedWriter = factory.makeSAMWriter(header, false, unmappedSam);
unmappedWriter.addAlignment(firstUnmappedRead);
unmappedWriter.addAlignment(secondUnmappedRead);
unmappedWriter.close();
final File alignedSam = File.createTempFile("aligned.", ".sam");
alignedSam.deleteOnExit();
final String sequence = "chr1";
// Populate the header with SAMSequenceRecords
header.getSequenceDictionary().addSequence(new SAMSequenceRecord(sequence, 1000000));
final SAMFileWriter alignedWriter = factory.makeSAMWriter(header, false, alignedSam);
addAlignmentsForBestFragmentMapqStrategy(alignedWriter, firstUnmappedRead, sequence, firstMapQs);
addAlignmentsForBestFragmentMapqStrategy(alignedWriter, secondUnmappedRead, sequence, secondMapQs);