private int compareCoordinates(final SAMRecord record1, final SAMRecord record2) { final int seqIndex1 = record1.getReferenceIndex(); final int seqIndex2 = record2.getReferenceIndex(); if (seqIndex1 == -1) { return ((seqIndex2 == -1) ? 0 : -1); } else if (seqIndex2 == -1) { return 1; } int result = seqIndex1 - seqIndex2; if (result != 0) { return result; } result = record1.getAlignmentStart() - record2.getAlignmentStart(); return result; }
private void checkPassesFilter( final boolean expected, final SAMRecord record, final String sequence, final int startPos, final int endPos, final boolean contained) { final boolean passes = passesFilter(record, sequence, startPos, endPos, contained); if (passes != expected) { System.out.println( "Error: Record erroneously " + (passes ? "passed" : "failed") + " filter."); System.out.println(" Record: " + record.getSAMString()); System.out.println( " Filter: " + sequence + ":" + startPos + "-" + endPos + " (" + (contained ? "contained" : "overlapping") + ")"); assertEquals(passes, expected); } }
private boolean passesFilter( final SAMRecord record, final String sequence, final int startPos, final int endPos, final boolean contained) { if (record == null) { return false; } if (!safeEquals(record.getReferenceName(), sequence)) { return false; } final int alignmentStart = record.getAlignmentStart(); int alignmentEnd = record.getAlignmentEnd(); if (alignmentStart <= 0) { assertTrue(record.getReadUnmappedFlag()); return false; } if (alignmentEnd <= 0) { // For indexing-only records, treat as single base alignment. assertTrue(record.getReadUnmappedFlag()); alignmentEnd = alignmentStart; } if (contained) { if (startPos != 0 && alignmentStart < startPos) { return false; } if (endPos != 0 && alignmentEnd > endPos) { return false; } } else { if (startPos != 0 && alignmentEnd < startPos) { return false; } if (endPos != 0 && alignmentStart > endPos) { return false; } } return true; }
private int runQueryTest( final URL bamURL, final String sequence, final int startPos, final int endPos, final boolean contained) { verbose("Testing query " + sequence + ":" + startPos + "-" + endPos + " ..."); final SamReader reader1 = SamReaderFactory.makeDefault() .disable(SamReaderFactory.Option.EAGERLY_DECODE) .open(SamInputResource.of(bamURL).index(BAM_INDEX_FILE)); final SamReader reader2 = SamReaderFactory.makeDefault() .disable(SamReaderFactory.Option.EAGERLY_DECODE) .open(SamInputResource.of(bamURL).index(BAM_INDEX_FILE)); final Iterator<SAMRecord> iter1 = reader1.query(sequence, startPos, endPos, contained); final Iterator<SAMRecord> iter2 = reader2.iterator(); // Compare ordered iterators. // Confirm that iter1 is a subset of iter2 that properly filters. SAMRecord record1 = null; SAMRecord record2 = null; int count1 = 0; int count2 = 0; int beforeCount = 0; int afterCount = 0; while (true) { if (record1 == null && iter1.hasNext()) { record1 = iter1.next(); count1++; } if (record2 == null && iter2.hasNext()) { record2 = iter2.next(); count2++; } // System.out.println("Iteration:"); // System.out.println(" Record1 = " + ((record1 == null) ? "null" : record1.format())); // System.out.println(" Record2 = " + ((record2 == null) ? "null" : record2.format())); if (record1 == null && record2 == null) { break; } if (record1 == null) { checkPassesFilter(false, record2, sequence, startPos, endPos, contained); record2 = null; afterCount++; continue; } assertNotNull(record2); final int ordering = compareCoordinates(record1, record2); if (ordering > 0) { checkPassesFilter(false, record2, sequence, startPos, endPos, contained); record2 = null; beforeCount++; continue; } assertTrue(ordering == 0); checkPassesFilter(true, record1, sequence, startPos, endPos, contained); checkPassesFilter(true, record2, sequence, startPos, endPos, contained); assertEquals(record1.getReadName(), record2.getReadName()); assertEquals(record1.getReadString(), record2.getReadString()); record1 = null; record2 = null; } CloserUtil.close(reader1); CloserUtil.close(reader2); verbose("Checked " + count1 + " records against " + count2 + " records."); verbose("Found " + (count2 - beforeCount - afterCount) + " records matching."); verbose("Found " + beforeCount + " records before."); verbose("Found " + afterCount + " records after."); return count1; }