@Override protected SAMFileHeader readHeader(InputStream is) throws IOException { try { BAMFileReader reader = new BAMFileReader(is, null, false, false, ValidationStringency.SILENT, null); return reader.getFileHeader(); } catch (IOException e) { throw e; } catch (Exception e) { return null; } } },
/** * Reads through the header and sequence records to find the virtual file offset of the first record in the BAM file. * The caller is responsible for closing the stream. */ static long findVirtualOffsetOfFirstRecord(final SeekableStream seekableStream) throws IOException { final BAMFileReader reader = new BAMFileReader(seekableStream, (SeekableStream) null, false, false, ValidationStringency.SILENT, new DefaultSAMRecordFactory()); return reader.mFirstRecordPointer; }
/** * Reads through the header and sequence records to find the virtual file offset of the first record in the BAM file. * The caller is responsible for closing the stream. */ static long findVirtualOffsetOfFirstRecord(final SeekableStream seekableStream) throws IOException { final BAMFileReader reader = new BAMFileReader(seekableStream, (SeekableStream) null, false, false, ValidationStringency.SILENT, new DefaultSAMRecordFactory()); return reader.mFirstRecordPointer; }
/** * Reads through the header and sequence records to find the virtual file offset of the first record in the BAM file. * The caller is responsible for closing the stream. */ static long findVirtualOffsetOfFirstRecord(final SeekableStream seekableStream) throws IOException { final BAMFileReader reader = new BAMFileReader(seekableStream, (SeekableStream) null, false, false, ValidationStringency.SILENT, new DefaultSAMRecordFactory()); return reader.mFirstRecordPointer; }
/** Reads through the header and sequence records to find the virtual file offset of the first record in the BAM file. */ static long findVirtualOffsetOfFirstRecord(final File bam) throws IOException { final BAMFileReader reader = new BAMFileReader(bam, null, false, false, ValidationStringency.SILENT, new DefaultSAMRecordFactory()); final long offset = reader.mFirstRecordPointer; reader.close(); return offset; }
/** Reads through the header and sequence records to find the virtual file offset of the first record in the BAM file. */ static long findVirtualOffsetOfFirstRecord(final File bam) throws IOException { final BAMFileReader reader = new BAMFileReader(bam, null, false, false, ValidationStringency.SILENT, new DefaultSAMRecordFactory()); final long offset = reader.mFirstRecordPointer; reader.close(); return offset; }
/** Reads through the header and sequence records to find the virtual file offset of the first record in the BAM file. */ static long findVirtualOffsetOfFirstRecord(final File bam) throws IOException { final BAMFileReader reader = new BAMFileReader(bam, null, false, ValidationStringency.SILENT, new DefaultSAMRecordFactory()); final long offset = reader.mFirstRecordPointer; reader.close(); return offset; }
/** Reads through the header and sequence records to find the virtual file offset of the first record in the BAM file. */ static long findVirtualOffsetOfFirstRecord(final File bam) throws IOException { final BAMFileReader reader = new BAMFileReader(bam, null, false, false, ValidationStringency.SILENT, new DefaultSAMRecordFactory()); final long offset = reader.mFirstRecordPointer; reader.close(); return offset; }
@BeforeTest public void init() throws IOException { bamFileReaderBAI = new BAMFileReader(bamFile, baiFileIndex, true, false, ValidationStringency.DEFAULT_STRINGENCY, DefaultSAMRecordFactory.getInstance()); bamFileReaderCSI = new BAMFileReader(bamFile, csiFileIndex, true, false, ValidationStringency.DEFAULT_STRINGENCY, DefaultSAMRecordFactory.getInstance()); bamFileReaderWrong = new BAMFileReader(bamFile, iiiFileIndex, true, false, ValidationStringency.DEFAULT_STRINGENCY, DefaultSAMRecordFactory.getInstance()); bamFileReaderNull = new BAMFileReader(bamFile, null, true, false, ValidationStringency.DEFAULT_STRINGENCY, DefaultSAMRecordFactory.getInstance()); }
public static void main(String[] args) throws IOException { Log.setGlobalLogLevel(LogLevel.INFO); File bamFile = new File(args[0]); File outCramFile = new File(args[1]); ReferenceSource source = new ReferenceSource(new File(args[2])); int maxThreads = Integer.valueOf(args[3]); BAMFileReader reader = new BAMFileReader(bamFile, null, false, false, ValidationStringency.SILENT, new DefaultSAMRecordFactory()); OutputStream os = new FileOutputStream(outCramFile); CRAMFileWriter writer = new CRAMFileWriter(os, source, reader.getFileHeader(), outCramFile.getName(), maxThreads); CloseableIterator<SAMRecord> iterator = reader.getIterator(); while (iterator.hasNext()) { SAMRecord record = iterator.next(); writer.addAlignment(record); } writer.close(); reader.close(); }
private void init(final SeekableStream strm, final File indexFile, final boolean eagerDecode, final ValidationStringency validationStringency) { try { if (streamLooksLikeBam(strm)) { mIsBinary = true; mReader = new BAMFileReader(strm, indexFile, eagerDecode, validationStringency, this.samRecordFactory); } else { throw new SAMFormatException("Unrecognized file format: " + strm); } setValidationStringency(validationStringency); } catch (final IOException e) { throw new RuntimeIOException(e); } }
private void init(final SeekableStream strm, final SeekableStream indexStream, final boolean eagerDecode, final ValidationStringency validationStringency) { try { if (streamLooksLikeBam(strm)) { mIsBinary = true; mReader = new BAMFileReader(strm, indexStream, eagerDecode, validationStringency, this.samRecordFactory); } else { throw new SAMFormatException("Unrecognized file format: " + strm); } setValidationStringency(validationStringency); } catch (final IOException e) { throw new RuntimeIOException(e); } }
if (file == null || !file.isFile()) { mReader = new BAMFileReader(bufferedStream, indexFile, eagerDecode, validationStringency, this.samRecordFactory); } else { bufferedStream.close(); mReader = new BAMFileReader(file, indexFile, eagerDecode, validationStringency, this.samRecordFactory);
@Test public void exhaustivelyTestIndexTest () throws IOException { BAMFileReader bamFileReader1 = new BAMFileReader(BAM_FILE, BAI_FILE, true, false, ValidationStringency.DEFAULT_STRINGENCY, new DefaultSAMRecordFactory()); bamFileReader1.enableIndexCaching(true); BAMFileReader bamFileReader2 = new BAMFileReader(BAM_FILE, CSI_FILE, true, false, ValidationStringency.DEFAULT_STRINGENCY, new DefaultSAMRecordFactory()); final SamReader samFileReader1 = new SamReader.PrimitiveSamReaderToSamReaderAdapter(bamFileReader1, null); final SamReader samFileReader2 = new SamReader.PrimitiveSamReaderToSamReaderAdapter(bamFileReader2, null); int baiCount = BamIndexValidator.exhaustivelyTestIndex(samFileReader1); int csiCount = BamIndexValidator.exhaustivelyTestIndex(samFileReader2); Assert.assertEquals(baiCount, csiCount); }
final BAMFileReader bam = new BAMFileReader(inputBamFile, null, false, ValidationStringency.SILENT, new DefaultSAMRecordFactory()); if (!bam.hasIndex()) { throw new SAMException("No index for bam file " + inputBamFile);
final BAMFileReader bam = new BAMFileReader(inputBamFile, null, false, false, ValidationStringency.SILENT, new DefaultSAMRecordFactory()); if (!bam.hasIndex()) { throw new SAMException("No index for bam file " + inputBamFile);
final BAMFileReader bam = new BAMFileReader(inputBamFile, null, false, false, ValidationStringency.SILENT, new DefaultSAMRecordFactory()); if (!bam.hasIndex() || bam.getIndexType() == null) { throw new SAMException("No index for bam file " + inputBamFile);
@Test public void setAttributeOnBamRecord() throws IOException { final SAMRecordSetBuilder builder = new SAMRecordSetBuilder(true, SAMFileHeader.SortOrder.coordinate); builder.addPair("test", 0, 100, 150, false, false, null, null, true, false, 30); //encode as BAM into ByteArray final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (final BAMFileWriter writer = new BAMFileWriter(baos, null)) { writer.setHeader(builder.getHeader()); builder.forEach(r -> r.setAttribute("xx", "Testing123")); builder.forEach(writer::addAlignment); } //read from ByteArray final BAMFileReader reader = new BAMFileReader(new ByteArrayInputStream(baos.toByteArray()), null, false, false, ValidationStringency.SILENT, new DefaultSAMRecordFactory()); for (final SAMRecord rec : (Iterable<SAMRecord>) reader::getIterator) { //clear attribute before explicitly accessing cigar or attributes rec.setAttribute("xx", null); Assert.assertNull(rec.getAttribute("xx")); } }
@Test(dataProvider = "longCigarsData") public void testClearAttributesDoesntVoidLongCigar(final int numOps) throws Exception { final SAMRecordSetBuilder builder = new SAMRecordSetBuilder(true, SAMFileHeader.SortOrder.coordinate); final Cigar cigar = Cigar.fromCigarOperators(getCigarOperatorsForTest(numOps)); builder.addFrag("frag1", 0, 1, false, false, cigar.toString(), null, 30); //encode as BAM into ByteArray final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (final BAMFileWriter writer = new BAMFileWriter(baos, null)) { writer.setHeader(builder.getHeader()); builder.getRecords().forEach(writer::addAlignment); } //read from ByteArray final BAMFileReader reader = new BAMFileReader(new ByteArrayInputStream(baos.toByteArray()), null, false, false, ValidationStringency.SILENT, new DefaultSAMRecordFactory()); final CloseableIterator<SAMRecord> iterator = reader.getIterator(); iterator.hasNext(); final SAMRecord recordFromBAM = iterator.next(); //clear attributes before explicitly accessing cigar or attributes recordFromBAM.clearAttributes(); // see that cigar is unscathed Assert.assertNotNull(recordFromBAM.getCigar()); Assert.assertFalse(BAMRecord.isSentinelCigar(recordFromBAM.getCigar(), recordFromBAM.getReadLength())); }
/** * A test to check that BAM changes read bases according with {@link SequenceUtil#toBamReadBasesInPlace}. */ @Test public void testBAMReadBases() throws IOException { final SAMFileHeader header = new SAMFileHeader(); header.addSequence(new SAMSequenceRecord("1", SequenceUtil.getIUPACCodesString().length())); header.addReadGroup(new SAMReadGroupRecord("rg1")); final SAMRecord originalSAMRecord = new SAMRecord(header); originalSAMRecord.setReadName("test"); originalSAMRecord.setReferenceIndex(0); originalSAMRecord.setAlignmentStart(1); originalSAMRecord.setReadBases(SequenceUtil.getIUPACCodesString().getBytes()); originalSAMRecord.setCigarString(originalSAMRecord.getReadLength() + "M"); originalSAMRecord.setBaseQualities(SAMRecord.NULL_QUALS); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (final BAMFileWriter writer = new BAMFileWriter(baos, null)) { writer.setHeader(header); writer.addAlignment(originalSAMRecord); } final BAMFileReader reader = new BAMFileReader(new ByteArrayInputStream(baos.toByteArray()), null, true, false, ValidationStringency.SILENT, new DefaultSAMRecordFactory()); final CloseableIterator<SAMRecord> iterator = reader.getIterator(); iterator.hasNext(); final SAMRecord recordFromBAM = iterator.next(); Assert.assertNotEquals(recordFromBAM.getReadBases(), originalSAMRecord.getReadBases()); Assert.assertEquals(recordFromBAM.getReadBases(), SequenceUtil.toBamReadBasesInPlace(originalSAMRecord.getReadBases())); }