while (itLeft.hasCurrent()) {
if (!itRight.hasCurrent()) {
// Exhausted right side. See if any of the remaining left reads match
// any of the saved right reads.
for( ; itLeft.hasCurrent(); itLeft.advance()) {
final SAMRecord left = itLeft.getCurrent();
final SAMRecord right = rightUnmatched.remove(left.getReadName());
if (right == null) {
++missingRight;
} else {
tallyAlignmentRecords(left, right);
}
}
break;
}
// Don't assume stability of order beyond the coordinate. Therefore grab all the
// reads from the left that has the same coordinate.
final SAMRecord left = itLeft.getCurrent();
final Map<String, SAMRecord> leftCurrentCoordinate = new HashMap<String, SAMRecord>();
leftCurrentCoordinate.put(left.getReadName(), left);
while (itLeft.advance()) {
final SAMRecord nextLeft = itLeft.getCurrent();
if (compareAlignmentCoordinates(left, nextLeft) == 0) {
leftCurrentCoordinate.put(nextLeft.getReadName(), nextLeft);
} else {
break;
}
}
// Advance the right iterator until it is >= the left reads that have just been grabbed
while (itRight.hasCurrent() && compareAlignmentCoordinates(left, itRight.getCurrent()) > 0) {
final SAMRecord right = itRight.getCurrent();
rightUnmatched.put(right.getReadName(), right);
itRight.advance();
}
// For each right read that has the same coordinate as the current left reads,
// see if there is a matching left read. If so, process and discard. If not,
// save the right read for later.
for (;itRight.hasCurrent() && compareAlignmentCoordinates(left, itRight.getCurrent()) == 0; itRight.advance()) {
final SAMRecord right = itRight.getCurrent();
final SAMRecord matchingLeft = leftCurrentCoordinate.remove(right.getReadName());
if (matchingLeft != null) {
ret = tallyAlignmentRecords(matchingLeft, right) && ret;
} else {
rightUnmatched.put(right.getReadName(), right);
}
}
// Anything left in leftCurrentCoordinate has not been matched
for (final SAMRecord samRecord : leftCurrentCoordinate.values()) {
leftUnmatched.put(samRecord.getReadName(), samRecord);
}
}
// The left iterator has been exhausted. See if any of the remaining right reads
// match any of the saved left reads.
for( ; itRight.hasCurrent(); itRight.advance()) {
final SAMRecord right = itRight.getCurrent();
final SAMRecord left = leftUnmatched.remove(right.getReadName());
if (left != null) {
tallyAlignmentRecords(left, right);
} else {
++missingLeft;
}
}
// Look up reads that were unmatched from left, and see if they are in rightUnmatched.
// If found, remove from rightUnmatched and tally.
for (final Map.Entry<String, SAMRecord> leftEntry : leftUnmatched.entrySet()) {
final String readName = leftEntry.getKey();
final SAMRecord left = leftEntry.getValue();
final SAMRecord right = rightUnmatched.remove(readName);
if (right == null) {
++missingRight;
continue;
}
tallyAlignmentRecords(left, right);