@Test public void testBamIntegers() throws IOException { final List<String> errorMessages = new ArrayList<String>(); final SamReader bamReader = SamReaderFactory.makeDefault().open(BAM_INPUT); final File bamOutput = File.createTempFile("test", ".bam"); final File samOutput = File.createTempFile("test", ".sam"); final SAMFileWriter samWriter = new SAMFileWriterFactory().makeWriter(bamReader.getFileHeader(), true, samOutput, null); final SAMFileWriter bamWriter = new SAMFileWriterFactory().makeWriter(bamReader.getFileHeader(), true, bamOutput, null); final SAMRecordIterator iterator = bamReader.iterator(); while (iterator.hasNext()) { try { final SAMRecord rec = iterator.next(); samWriter.addAlignment(rec); bamWriter.addAlignment(rec); } catch (final Throwable e) { System.out.println(e.getMessage()); errorMessages.add(e.getMessage()); } } CloserUtil.close(bamReader); samWriter.close(); bamWriter.close(); Assert.assertEquals(errorMessages.size(), 0); bamOutput.deleteOnExit(); samOutput.deleteOnExit(); }
private int countRecords(final SamReader reader) { int count = 0; try (final SAMRecordIterator iter = reader.iterator()) { while (iter.hasNext()) { iter.next(); count++; } } return count; }
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; }