unmappedWriter.close();
final File alignedSam = File.createTempFile("aligned.", ".sam");
alignedSam.deleteOnExit();
final SAMFileReader dictReader = new SAMFileReader(sequenceDict);
header.setSequenceDictionary(dictReader.getFileHeader().getSequenceDictionary());
dictReader.close();
final SAMFileWriter alignedWriter = factory.makeSAMWriter(header, false, alignedSam);
String expectedFirstPrimarySequence = null;
int expectedFirstPrimaryAlignmentStart = -1;
String expectedSecondPrimarySequence = null;
int expectedSecondPrimaryAlignmentStart = -1;
// Semi-randomly make the reads align to forward or reverse strand.
boolean reverse = false;
for (final MostDistantStrategyAlignmentSpec spec: firstEndSpecs) {
addAlignmentForMostStrategy(alignedWriter, firstUnmappedRead, spec, reverse);
reverse = !reverse;
if (spec.expectedPrimary) {
expectedFirstPrimarySequence = spec.sequence;
expectedFirstPrimaryAlignmentStart = spec.alignmentStart;
}
}
for (final MostDistantStrategyAlignmentSpec spec: secondEndSpecs) {
addAlignmentForMostStrategy(alignedWriter, secondUnmappedRead, spec, reverse);
reverse = !reverse;
if (spec.expectedPrimary) {
expectedSecondPrimarySequence = spec.sequence;
expectedSecondPrimaryAlignmentStart = spec.alignmentStart;
}
}
alignedWriter.close();
final File output = File.createTempFile("testMostDistantStrategy." + testName, ".sam");
output.deleteOnExit();
doMergeAlignment(unmappedSam, Collections.singletonList(alignedSam),
null, null, null, null,
false, true, false, 1,
"0", "1.0", "align!", "myAligner",
true, fasta, output,
SamPairUtil.PairOrientation.FR, MergeBamAlignment.PrimaryAlignmentStrategy.MostDistant,
null, includeSecondary);
final SAMFileReader reader = new SAMFileReader(output);
int numFirstRecords = 0;
int numSecondRecords = 0;
String firstPrimarySequence = null;
int firstPrimaryAlignmentStart = -1;
String secondPrimarySequence = null;
int secondPrimaryAlignmentStart = -1;
for (final SAMRecord rec: reader) {
Assert.assertTrue(rec.getReadPairedFlag());
if (rec.getFirstOfPairFlag()) ++numFirstRecords;
else if (rec.getSecondOfPairFlag()) ++ numSecondRecords;
else Assert.fail("unpossible!");
if (!rec.getReadUnmappedFlag() && !rec.getNotPrimaryAlignmentFlag()) {
if (rec.getFirstOfPairFlag()) {
Assert.assertEquals(firstPrimaryAlignmentStart, -1);
firstPrimarySequence = rec.getReferenceName();
firstPrimaryAlignmentStart = rec.getAlignmentStart();
} else {
Assert.assertEquals(secondPrimaryAlignmentStart, -1);
secondPrimarySequence = rec.getReferenceName();
secondPrimaryAlignmentStart = rec.getAlignmentStart();
}
} else if (rec.getNotPrimaryAlignmentFlag()) {
Assert.assertTrue(rec.getMateUnmappedFlag());
}
}
reader.close();
Assert.assertEquals(firstPrimarySequence, expectedFirstPrimarySequence);
Assert.assertEquals(firstPrimaryAlignmentStart, expectedFirstPrimaryAlignmentStart);
Assert.assertEquals(secondPrimarySequence, expectedSecondPrimarySequence);
Assert.assertEquals(secondPrimaryAlignmentStart, expectedSecondPrimaryAlignmentStart);
if (!includeSecondary) {