/** * Create a CRAMFileReader from a file using the supplied reference source. * * @param cramFile CRAM file to read. Can not be null. * @param referenceSource a {@link htsjdk.samtools.cram.ref.CRAMReferenceSource source} of * reference sequences. May not be null. * @throws IllegalArgumentException if the {@code cramFile} or the {@code CRAMReferenceSource} is null */ public CRAMFileReader(final File cramFile, final CRAMReferenceSource referenceSource) { if (cramFile == null) throw new IllegalArgumentException("CRAM file cannot be null."); if (referenceSource == null) { throw new IllegalArgumentException("A reference is required for CRAM readers"); } this.cramFile = cramFile; this.referenceSource = referenceSource; mIndexFile = findIndexForFile(null, cramFile); getIterator(); }
/** * Create a CRAMFileReader from a file using the supplied reference source. * * @param cramFile CRAM file to read. Can not be null. * @param referenceSource a {@link htsjdk.samtools.cram.ref.CRAMReferenceSource source} of * reference sequences. May not be null. * @throws IllegalArgumentException if the {@code cramFile} or the {@code CRAMReferenceSource} is null */ public CRAMFileReader(final File cramFile, final CRAMReferenceSource referenceSource) { if (cramFile == null) throw new IllegalArgumentException("CRAM file cannot be null."); if (referenceSource == null) { throw new IllegalArgumentException("A reference is required for CRAM readers"); } this.cramFile = cramFile; this.referenceSource = referenceSource; mIndexFile = findIndexForFile(null, cramFile); getIterator(); }
/** * Create a CRAMFileReader from a file and optional index file using the supplied reference source. If index file * is supplied then random access will be available. * * @param cramFile CRAM file to read. May not be null. * @param indexFile index file to be used for random access. May be null. * @param referenceSource a {@link htsjdk.samtools.cram.ref.CRAMReferenceSource source} of * reference sequences. May not be null. * @throws IllegalArgumentException if the {@code cramFile} or the {@code CRAMReferenceSource} is null */ public CRAMFileReader(final File cramFile, final File indexFile, final CRAMReferenceSource referenceSource) { if (cramFile == null) throw new IllegalArgumentException("File is required."); if (referenceSource == null) { throw new IllegalArgumentException("A reference is required for CRAM readers"); } this.cramFile = cramFile; mIndexFile = findIndexForFile(indexFile, cramFile); this.referenceSource = referenceSource; getIterator(); }
/** * Create a CRAMFileReader from a file and optional index file using the supplied reference source. If index file * is supplied then random access will be available. * * @param cramFile CRAM file to read. May not be null. * @param indexFile index file to be used for random access. May be null. * @param referenceSource a {@link htsjdk.samtools.cram.ref.CRAMReferenceSource source} of * reference sequences. May not be null. * @throws IllegalArgumentException if the {@code cramFile} or the {@code CRAMReferenceSource} is null */ public CRAMFileReader(final File cramFile, final File indexFile, final CRAMReferenceSource referenceSource) { if (cramFile == null) throw new IllegalArgumentException("File is required."); if (referenceSource == null) { throw new IllegalArgumentException("A reference is required for CRAM readers"); } this.cramFile = cramFile; mIndexFile = findIndexForFile(indexFile, cramFile); this.referenceSource = referenceSource; getIterator(); }
private int getCRAMRecordCount(final File cramFile) { final CRAMFileReader cramReader = new CRAMFileReader(cramFile, (File)null, testReferenceSource); final Iterator<SAMRecord> it = cramReader.getIterator(); int recCount = 0; while (it.hasNext()) { it.next(); recCount++; } cramReader.close(); return recCount; }
@Test(expectedExceptions = CRAMException.class) public void testExceptionWhileReadingWithWrongReference() throws IOException { final CramTestCase test = new CramTestCase(); // try reading the CRAM file with the incorrect ref source that does not upper case bases: final CRAMFileReader reader = new CRAMFileReader(new ByteArrayInputStream(test.cramData), (File) null, test.referenceSourceMixedCase, ValidationStringency.STRICT); final SAMRecordIterator iterator = reader.getIterator(); // expect an exception here due to slice MD5 mismatch: iterator.hasNext(); }
private SAMRecordIterator getCramFileIterator(ValidationStringency valStringency) { final File refFile = new File("src/test/resources/htsjdk/samtools/cram/ce.fa"); final File cramFile = new File("src/test/resources/htsjdk/samtools/cram/ce#containsInvalidRecords.3.0.cram"); final ReferenceSource source = new ReferenceSource(refFile); final CRAMFileReader cramFileReader = new CRAMFileReader(cramFile, (SeekableStream) null, source); cramFileReader.setValidationStringency(valStringency); return cramFileReader.getIterator(); } }
@Test(dataProvider = "sortsFailCramWithoutIndex", expectedExceptions = IllegalStateException.class) public void testCramSortFail(String file, String fileReference, SAMFileHeader.SortOrder order) throws Exception { final CRAMFileReader cramFileReader = getCramFileReader(file, fileReference); final SAMRecordIterator samRecordIterator = cramFileReader.getIterator().assertSorted(order); Assert.assertTrue(samRecordIterator.hasNext()); while (samRecordIterator.hasNext()) { Assert.assertNotNull(samRecordIterator.next()); } }
@Test(expectedExceptions = CRAMException.class) public void testFailsMD5Check() throws IOException { // auxf.alteredForMD5test.fa has been altered slightly from the original reference // to cause the CRAM md5 check to fail final File CRAMFile = new File("src/test/resources/htsjdk/samtools/cram/auxf#values.3.0.cram"); final File refFile = new File("src/test/resources/htsjdk/samtools/cram/auxf.alteredForMD5test.fa"); ReferenceSource refSource = new ReferenceSource(refFile); CRAMFileReader reader = null; try { reader = new CRAMFileReader( CRAMFile, null, refSource, ValidationStringency.STRICT); Iterator<SAMRecord> it = reader.getIterator(); while (it.hasNext()) { it.next(); } } finally { if (reader != null) { reader.close(); } } }
@Test(dataProvider = "sortsCramWithoutIndex") public void testCramSort(String file, String fileReference, SAMFileHeader.SortOrder order) throws Exception { final CRAMFileReader cramFileReader = getCramFileReader(file, fileReference); final SAMRecordIterator samRecordIterator = cramFileReader.getIterator().assertSorted(order); Assert.assertTrue(samRecordIterator.hasNext()); while (samRecordIterator.hasNext()) { Assert.assertNotNull(samRecordIterator.next()); } }
@Test public void testCramIteratorWithoutCallingHasNextFirst() throws IOException { final SAMRecordSetBuilder builder = new SAMRecordSetBuilder(false, SAMFileHeader.SortOrder.unsorted); builder.addFrag("1", 0, 2, false); final CRAMFileReader reader = CRAMTestUtils.writeAndReadFromInMemoryCram(builder); final SAMRecordIterator iterator = reader.getIterator(); Assert.assertNotNull(iterator.next()); Assert.assertThrows(NoSuchElementException.class, iterator::next); } }
@Test public void testSliceMD5() throws IOException { final CramTestCase test = new CramTestCase(); // read the CRAM: final ByteArrayInputStream bais = new ByteArrayInputStream(test.cramData); final CramHeader cramHeader = CramIO.readCramHeader(bais); final Container container = ContainerIO.readContainer(cramHeader.getVersion(), bais); final Slice slice = container.slices[0]; Assert.assertEquals(slice.alignmentStart, 1); Assert.assertEquals(slice.alignmentSpan, test.referenceBases.length); // check the slice MD5 is the MD5 of upper-cased ref bases: final byte[] ucRefMD5 = SequenceUtil.calculateMD5(test.refBasesFromUCSource, 0, test.refBasesFromUCSource.length); Assert.assertEquals(slice.refMD5, ucRefMD5); // check the CRAM file reads: final CRAMFileReader reader = new CRAMFileReader(new ByteArrayInputStream(test.cramData), (File) null, test.referenceSourceUpperCased, ValidationStringency.STRICT); final SAMRecordIterator iterator = reader.getIterator(); Assert.assertTrue(iterator.hasNext()); Assert.assertEquals(iterator.next(), test.record); }
@Test public void testIteratorWholeFileSpan() throws IOException { CRAMFileReader reader = new CRAMFileReader( new ByteArraySeekableStream(cramBytes), new ByteArraySeekableStream(craiBytes), source, ValidationStringency.STRICT); final SAMFileSpan allContainers = reader.getFilePointerSpanningReads(); final CloseableIterator<SAMRecord> iterator = reader.getIterator(allContainers); Assert.assertTrue(iterator.hasNext()); long count = getIteratorCount(iterator); Assert.assertEquals(count, nofReads); }
private static void testSingleRecord(SAMRecord record, byte[] ref) throws IOException { CRAMFileReader cramFileReader = CRAMTestUtils.writeAndReadFromInMemoryCram(Collections.singletonList(record), ref); final SAMRecordIterator iterator = cramFileReader.getIterator(); Assert.assertTrue(iterator.hasNext()); SAMRecord s2 = iterator.next(); Assert.assertNotNull(s2); Assert.assertFalse(iterator.hasNext()); Assert.assertEquals(record.getFlags(), s2.getFlags()); Assert.assertEquals(record.getReadName(), s2.getReadName()); Assert.assertEquals(record.getReferenceName(), s2.getReferenceName()); Assert.assertEquals(record.getAlignmentStart(), s2.getAlignmentStart()); Assert.assertEquals(record.getReadBases(), s2.getReadBases()); Assert.assertEquals(record.getBaseQualities(), s2.getBaseQualities()); }
@Test public void testCRAIIndexerFromContainer() throws IOException { final File CRAMFile = new File("src/test/resources/htsjdk/samtools/cram/test2.cram"); final File refFile = new File("src/test/resources/htsjdk/samtools/cram/auxf.fa"); ReferenceSource refSource = new ReferenceSource(refFile); CRAMFileReader reader = new CRAMFileReader( CRAMFile, null, refSource, ValidationStringency.STRICT); SAMFileHeader samHeader = reader.getFileHeader(); Iterator<SAMRecord> it = reader.getIterator(); while(it.hasNext()) { SAMRecord samRec = it.next(); } reader.close(); FileInputStream fis = new FileInputStream(CRAMFile); CramContainerIterator cit = new CramContainerIterator(fis); ByteArrayOutputStream bos = new ByteArrayOutputStream(); CRAMCRAIIndexer craiIndexer = new CRAMCRAIIndexer(bos, samHeader); while (cit.hasNext()) { craiIndexer.processContainer(cit.next()); } craiIndexer.finish(); bos.close(); List<CRAIEntry> craiEntries = CRAMCRAIIndexer.readIndex(new ByteArrayInputStream(bos.toByteArray())).getCRAIEntries(); Assert.assertEquals(craiEntries.size(), 1); }
private static void testRecords(Collection<SAMRecord> records, byte[] ref) throws IOException { CRAMFileReader cramFileReader = CRAMTestUtils.writeAndReadFromInMemoryCram(records, ref); Iterator<SAMRecord> it; final SAMRecordIterator iterator = cramFileReader.getIterator(); Assert.assertTrue(iterator.hasNext()); it = records.iterator(); while (it.hasNext()) { SAMRecord record = it.next(); SAMRecord s2 = iterator.next(); Assert.assertNotNull(s2); Assert.assertEquals(record.getFlags(), s2.getFlags()); Assert.assertEquals(record.getReadName(), s2.getReadName()); Assert.assertEquals(record.getReferenceName(), s2.getReferenceName()); Assert.assertEquals(record.getAlignmentStart(), s2.getAlignmentStart()); Assert.assertEquals(record.getReadBases(), s2.getReadBases()); Assert.assertEquals(record.getBaseQualities(), s2.getBaseQualities()); } Assert.assertFalse(iterator.hasNext()); }
private void doTest(final List<SAMRecord> samRecords, final ByteArrayOutputStream outStream, final OutputStream indexStream) { final SAMFileHeader header = createSAMHeader(SAMFileHeader.SortOrder.coordinate); final ReferenceSource refSource = createReferenceSource(); final CRAMContainerStreamWriter containerStream = new CRAMContainerStreamWriter(outStream, indexStream, refSource, header, "test"); containerStream.writeHeader(header); for (SAMRecord record : samRecords) { containerStream.writeAlignment(record); } containerStream.finish(true); // finish and issue EOF // read all the records back in final CRAMFileReader cReader = new CRAMFileReader(null, new ByteArrayInputStream(outStream.toByteArray()), refSource); final SAMRecordIterator iterator = cReader.getIterator(); int count = 0; while (iterator.hasNext()) { SAMRecord actualRecord = iterator.next(); count++; } Assert.assertEquals(count, samRecords.size()); }
@Test public void test_roundtrip_tlen_preserved() throws IOException { SamReader reader = SamReaderFactory.make().open(new File("src/test/resources/htsjdk/samtools/cram_tlen_reads.sorted.sam")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ReferenceSource source = new ReferenceSource(new File("src/test/resources/htsjdk/samtools/cram_tlen.fasta")); CRAMFileWriter writer = new CRAMFileWriter(baos, source, reader.getFileHeader(), "test.cram"); SAMRecordIterator iterator = reader.iterator(); List<SAMRecord> records = new ArrayList<SAMRecord>(); while (iterator.hasNext()) { final SAMRecord record = iterator.next(); writer.addAlignment(record); records.add(record); } writer.close(); CRAMFileReader cramReader = new CRAMFileReader(new ByteArrayInputStream(baos.toByteArray()), (File) null, source, ValidationStringency.STRICT); iterator = cramReader.getIterator(); int i = 0; while (iterator.hasNext()) { SAMRecord record1 = iterator.next(); SAMRecord record2 = records.get(i++); Assert.assertEquals(record1.getInferredInsertSize(), record2.getInferredInsertSize(), record1.getReadName()); Assert.assertEquals(record1, record2, record1.getReadName()); } Assert.assertEquals(records.size(), i); }
@Test public void testIteratorFromFileSpan_WholeFile() throws IOException { CRAMFileReader reader = new CRAMFileReader(new ByteArraySeekableStream(cramBytes), new ByteArraySeekableStream(baiBytes), source, ValidationStringency.SILENT); reader.setValidationStringency(ValidationStringency.SILENT); final SAMFileSpan allContainers = reader.getFilePointerSpanningReads(); final CloseableIterator<SAMRecord> iterator = reader.getIterator(allContainers); Assert.assertTrue(iterator.hasNext()); int counter = 0; while (iterator.hasNext()) { iterator.next(); counter++; } Assert.assertEquals(counter, nofReads); }
private void validateRecords(final List<SAMRecord> expectedRecords, ByteArrayInputStream is, ReferenceSource referenceSource) { CRAMFileReader cReader = new CRAMFileReader(null, is, referenceSource); SAMRecordIterator iterator2 = cReader.getIterator(); int index = 0; while (iterator2.hasNext()) { SAMRecord actualRecord = iterator2.next(); SAMRecord expectedRecord = expectedRecords.get(index++); Assert.assertEquals(actualRecord.getReadName(), expectedRecord.getReadName()); Assert.assertEquals(actualRecord.getFlags(), expectedRecord.getFlags()); Assert.assertEquals(actualRecord.getAlignmentStart(), expectedRecord.getAlignmentStart()); Assert.assertEquals(actualRecord.getAlignmentEnd(), expectedRecord.getAlignmentEnd()); Assert.assertEquals(actualRecord.getReferenceName(), expectedRecord.getReferenceName()); Assert.assertEquals(actualRecord.getMateAlignmentStart(), expectedRecord.getMateAlignmentStart()); Assert.assertEquals(actualRecord.getMateReferenceName(), expectedRecord.getMateReferenceName()); Assert.assertEquals(actualRecord.getReadBases(), expectedRecord.getReadBases()); Assert.assertEquals(actualRecord.getBaseQualities(), expectedRecord.getBaseQualities()); } cReader.close(); }