/** * Read CRAM header from the given {@link InputStream}. * * @param inputStream input stream to read from * @return complete {@link CramHeader} object * @throws IOException as per java IO contract */ public static CramHeader readCramHeader(final InputStream inputStream) throws IOException { final CramHeader header = readFormatDefinition(inputStream); final SAMFileHeader samFileHeader = readSAMFileHeader(header.getVersion(), inputStream, new String(header.getId())); return new CramHeader(header.getVersion(), new String(header.getId()), samFileHeader); }
public SAMFileHeader getSAMFileHeader() { return cramHeader.getSamFileHeader(); }
public boolean fixHeaderInFile(File cramFile) throws IOException, MD5MismatchError { FileInputStream fis = new FileInputStream(cramFile); CramHeader cramHeader = CramIO.readCramHeader(fis); fixSequences(cramHeader.getSamFileHeader().getSequenceDictionary().getSequences()); String cmd = "fixheader"; String version = getClass().getPackage().getImplementationVersion(); addPG(cramHeader.getSamFileHeader(), "cramtools", cmd, version); CramHeader newHeader = cramHeader.clone(); return CramIO.replaceCramHeader(cramFile, newHeader); }
/** * Copy the CRAM header into a new {@link CramHeader} object. * @return a complete copy of the header */ @SuppressWarnings("CloneDoesntCallSuperClone") @Override public CramHeader clone() { final CramHeader clone = new CramHeader(); clone.version = version; System.arraycopy(id, 0, clone.id, 0, id.length); clone.samFileHeader = getSamFileHeader().clone(); return clone; }
/** * Writes CRAM header into the specified {@link OutputStream}. * * @param cramHeader the {@link CramHeader} object to write * @param outputStream the output stream to write to * @return the number of bytes written out * @throws IOException as per java IO contract */ public static long writeCramHeader(final CramHeader cramHeader, final OutputStream outputStream) throws IOException { // if (cramHeader.getVersion().major < 3) throw new RuntimeException("Deprecated CRAM version: " + cramHeader.getVersion().major); outputStream.write("CRAM".getBytes("US-ASCII")); outputStream.write(cramHeader.getVersion().major); outputStream.write(cramHeader.getVersion().minor); outputStream.write(cramHeader.getId()); for (int i = cramHeader.getId().length; i < 20; i++) outputStream.write(0); final long length = CramIO.writeContainerForSamFileHeader(cramHeader.getVersion().major, cramHeader.getSamFileHeader(), outputStream); return CramIO.DEFINITION_LENGTH + length; }
final RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw")) { final CramHeader header = readFormatDefinition(countingInputStream); final Container c = ContainerIO.readContainerHeader(header.getVersion().major, countingInputStream); final long pos = countingInputStream.getCount(); countingInputStream.close(); final Block block = Block.createRawFileHeaderBlock(toByteArray(newHeader.getSamFileHeader())); final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); block.write(newHeader.getVersion().major, byteArrayOutputStream); if (byteArrayOutputStream.size() > c.containerByteSize) { log.error("Failed to replace CRAM header because the new header does not fit.");
/** * Consume the entirety of the next container from the stream. * @param cramVersion * @param countingStream * @return The next Container from the stream. * @throws IOException */ protected Container containerFromStream(final Version cramVersion, final CountingInputStream countingStream) throws IOException { return ContainerIO.readContainer(cramHeader.getVersion(), countingStream); }
final String id = "testid"; final CramHeader cramHeader = new CramHeader(CramVersions.CRAM_v3, id, new SAMFileHeader()); Assert.assertTrue(cramHeader.getSamFileHeader().getSequenceDictionary().isEmpty()); final File file = File.createTempFile("test", ".cram"); file.deleteOnExit(); final FileOutputStream fos = new FileOutputStream(file); CramIO.writeCramHeader(cramHeader, fos); CramIO.issueEOF(cramHeader.getVersion(), fos); fos.close(); final long length = file.length(); samFileHeader.addSequence(sequenceRecord); final String id2 = "testid2"; final CramHeader cramHeader2 = new CramHeader(CramVersions.CRAM_v3, id2, samFileHeader); final boolean replaced = CramIO.replaceCramHeader(file, cramHeader2); Assert.assertTrue(replaced); Assert.assertEquals(cramHeader3.getVersion(), CramVersions.CRAM_v3); Assert.assertFalse(cramHeader3.getSamFileHeader().getSequenceDictionary().isEmpty()); Assert.assertNotNull(cramHeader3.getSamFileHeader().getSequenceDictionary().getSequence(0)); Assert.assertEquals(cramHeader3.getSamFileHeader().getSequence(sequenceRecord.getSequenceName()).getSequenceLength(), sequenceRecord.getSequenceLength()); file.delete();
@Test public void testCheckHeaderAndEOF_v3() throws IOException { final String id = "testid"; final CramHeader cramHeader = new CramHeader(CramVersions.CRAM_v3, id, new SAMFileHeader()); final File file = File.createTempFile("test", ".cram"); file.deleteOnExit(); final FileOutputStream fos = new FileOutputStream(file); CramIO.writeCramHeader(cramHeader, fos); CramIO.issueEOF(cramHeader.getVersion(), fos); fos.close(); Assert.assertTrue(CramIO.checkHeaderAndEOF(file)); file.delete(); }
private static CramHeader readFormatDefinition(final InputStream inputStream) throws IOException { for (final byte magicByte : CramHeader.MAGIC) { if (magicByte != inputStream.read()) throw new RuntimeException("Unknown file format."); } final Version version = new Version(inputStream.read(), inputStream.read(), 0); final CramHeader header = new CramHeader(version, null, null); final DataInputStream dataInputStream = new DataInputStream(inputStream); dataInputStream.readFully(header.getId()); return header; }
/** * Write a CRAM File header and a SAM Header to an output stream. * * @param cramVersion * @param outStream * @param samFileHeader * @param cramID * @return the offset in the stream after writing the headers */ public static long writeHeader(final Version cramVersion, final OutputStream outStream, final SAMFileHeader samFileHeader, String cramID) { final CramHeader cramHeader = new CramHeader(cramVersion, cramID, samFileHeader); return CramIO.writeCramHeader(cramHeader, outStream); }
private static void dist(File file, byte defaultQualityScore) throws IllegalArgumentException, IOException, IllegalAccessException { InputStream is = new FileInputStream(file); CramHeader header = CramIO.readCramHeader(is); Container c = null; ContainerParser parser = new ContainerParser(header.getSamFileHeader()); ArrayList<CramCompressionRecord> records = new ArrayList<CramCompressionRecord>(10000); long[] freq = new long[255]; while ((c = ContainerIO.readContainer(header.getVersion(), is)) != null && !c.isEOF()) { parser.getRecords(c, records, ValidationStringency.SILENT); CramNormalizer.restoreQualityScores(defaultQualityScore, records); for (CramCompressionRecord record : records) { for (byte b : record.qualityScores) freq[b & 0xFF]++; } records.clear(); } print(freq, defaultQualityScore, System.out); }
/** * Consume the entirety of the next container from the stream. * @param cramVersion * @param countingStream * @return The next Container from the stream. */ protected Container containerFromStream(final Version cramVersion, final CountingInputStream countingStream) { return ContainerIO.readContainer(cramHeader.getVersion(), countingStream); }
/** * Writes CRAM header into the specified {@link OutputStream}. * * @param cramHeader the {@link CramHeader} object to write * @param outputStream the output stream to write to * @return the number of bytes written out */ public static long writeCramHeader(final CramHeader cramHeader, final OutputStream outputStream) { // if (cramHeader.getVersion().major < 3) throw new RuntimeException("Deprecated CRAM version: " + cramHeader.getVersion().major); try { outputStream.write("CRAM".getBytes("US-ASCII")); outputStream.write(cramHeader.getVersion().major); outputStream.write(cramHeader.getVersion().minor); outputStream.write(cramHeader.getId()); for (int i = cramHeader.getId().length; i < 20; i++) outputStream.write(0); final long length = CramIO.writeContainerForSamFileHeader(cramHeader.getVersion().major, cramHeader.getSamFileHeader(), outputStream); return CramIO.DEFINITION_LENGTH + length; } catch (final IOException e) { throw new RuntimeIOException(e); } }
@Test public void testCheckHeaderAndEOF_v2() throws IOException { final String id = "testid"; final CramHeader cramHeader = new CramHeader(CramVersions.CRAM_v2_1, id, new SAMFileHeader()); final File file = File.createTempFile("test", ".cram"); file.deleteOnExit(); final FileOutputStream fos = new FileOutputStream(file); CramIO.writeCramHeader(cramHeader, fos); CramIO.issueEOF(cramHeader.getVersion(), fos); fos.close(); Assert.assertTrue(CramIO.checkHeaderAndEOF(file)); file.delete(); }
/** * Copy the CRAM header into a new {@link CramHeader} object. * @return a complete copy of the header */ @SuppressWarnings("CloneDoesntCallSuperClone") @Override public CramHeader clone() { final CramHeader clone = new CramHeader(); clone.version = version; System.arraycopy(id, 0, clone.id, 0, id.length); clone.samFileHeader = getSamFileHeader().clone(); return clone; }
private static CramHeader readFormatDefinition(final InputStream inputStream) throws IOException { for (final byte magicByte : CramHeader.MAGIC) { if (magicByte != inputStream.read()) throw new RuntimeException("Unknown file format."); } final Version version = new Version(inputStream.read(), inputStream.read(), 0); final CramHeader header = new CramHeader(version, null, null); final DataInputStream dataInputStream = new DataInputStream(inputStream); dataInputStream.readFully(header.getId()); return header; }
/** * Write a CRAM File header and a SAM Header to an output stream. * * @param cramVersion * @param outStream * @param samFileHeader * @param cramID * @return the offset in the stream after writing the headers */ public static long writeHeader(final Version cramVersion, final OutputStream outStream, final SAMFileHeader samFileHeader, String cramID) { final CramHeader cramHeader = new CramHeader(cramVersion, cramID, samFileHeader); try { return CramIO.writeCramHeader(cramHeader, outStream); } catch (final IOException e) { throw new RuntimeIOException(e); } }
throw new NullPointerException(); BAMRecordCodec codec = new BAMRecordCodec(header.getSamFileHeader()); Set<Integer> refSet = new HashSet<Integer>(); container = CramSerilization.convert(records, header.getSamFileHeader(), referenceSource, lossyOptions); } catch (IllegalArgumentException | IllegalAccessException | IOException e) { throw new RuntimeException(e); ContainerIO.writeContainer(header.getVersion(), container, baos); } catch (IOException e) { throw new RuntimeIOException();
public static void main(String[] args) throws IOException, IllegalArgumentException, IllegalAccessException { Log.setGlobalLogLevel(LogLevel.INFO); File cramFile = new File(args[0]); InputStream is = new BufferedInputStream(new FileInputStream(cramFile)); CramHeader header = CramIO.readCramHeader(is); Container c = null; while ((c = ContainerIO.readContainer(header.getVersion(), is)) != null && !c.isEOF()) { for (Slice slice : c.slices) { if (slice.sequenceId == Slice.MULTI_REFERENCE) { System.out.println("Read feature B detected."); System.exit(1); } } } } }