private static void writeTerminatorBlock(final OutputStream out, final SAMFormat samOutputFormat) throws IOException { if (SAMFormat.CRAM == samOutputFormat) { CramIO.issueEOF(CramVersions.DEFAULT_CRAM_VERSION, out); // terminate with CRAM EOF container } else { out.write(BlockCompressedStreamConstants.EMPTY_GZIP_BLOCK); // add the BGZF terminator } }
/** * Write a CRAM file header and SAM header to the stream. * @param header SAMFileHeader to write */ public void writeHeader(final SAMFileHeader header) { // TODO: header must be written exactly once per writer life cycle. offset = CramIO.writeHeader(cramVersion, outputStream, header, cramID); }
@Override protected CramHeader readHeader(InputStream is) throws IOException { try { return CramIO.readCramHeader(is); } catch (IOException e) { throw e; } catch (Exception ee) { return null; } } };
/** * Check if the file: 1) contains proper CRAM header. 2) given the version info from the header check the end of file marker. * * @param file the CRAM file to check * @return true if the file is a valid CRAM file and is properly terminated with respect to the version. * @throws IOException as per java IO contract */ public static boolean checkHeaderAndEOF(final File file) throws IOException { final SeekableStream seekableStream = new SeekableFileStream(file); final CramHeader cramHeader = readCramHeader(seekableStream); return checkEOF(cramHeader.getVersion(), seekableStream); }
file.deleteOnExit(); final FileOutputStream fos = new FileOutputStream(file); CramIO.writeCramHeader(cramHeader, fos); CramIO.issueEOF(cramHeader.getVersion(), fos); fos.close(); final long length = file.length(); 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(file.length(), length); Assert.assertTrue(CramIO.checkHeaderAndEOF(file)); final CramHeader cramHeader3 = CramIO.readCramHeader(new FileInputStream(file)); Assert.assertEquals(cramHeader3.getVersion(), CramVersions.CRAM_v3); Assert.assertFalse(cramHeader3.getSamFileHeader().getSequenceDictionary().isEmpty());
@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(); }
ValidationStringency.SILENT, null); CramHeader cramHeader = new CramHeader(CramVersions.CRAM_v3, new File(args[0]).getName(), samFileHeader); CramIO.writeCramHeader(cramHeader, cramOutputStream); CramIO.issueEOF(cramHeader.getVersion(), cramOutputStream); cramOutputStream.close();
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); }
/** * 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); }
/** * 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); }
try (final CountingInputStream countingInputStream = new CountingInputStream(new FileInputStream(file)); 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);
/** * Check if the {@link SeekableStream} is properly terminated with a end-of-file marker. * * @param version CRAM version to assume * @param seekableStream the stream to read from * @return true if the stream ends with a correct EOF marker, false otherwise * @throws IOException as per java IO contract */ @SuppressWarnings("SimplifiableIfStatement") private static boolean checkEOF(final Version version, final SeekableStream seekableStream) throws IOException { if (version.compatibleWith(CramVersions.CRAM_v3)) return streamEndsWith(seekableStream, ZERO_F_EOF_MARKER); if (version.compatibleWith(CramVersions.CRAM_v2_1)) return streamEndsWith(seekableStream, ZERO_B_EOF_MARKER); return false; }
@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(); }
/** * Check if the file: 1) contains proper CRAM header. 2) given the version info from the header check the end of file marker. * * @param file the CRAM file to check * @return true if the file is a valid CRAM file and is properly terminated with respect to the version. */ public static boolean checkHeaderAndEOF(final File file) { try (final SeekableStream seekableStream = new SeekableFileStream(file)) { final CramHeader cramHeader = readCramHeader(seekableStream); return checkEOF(cramHeader.getVersion(), seekableStream); } catch (final IOException e) { throw new RuntimeIOException(e); } }
CramHeader cramHeader = CramIO.readCramHeader(fis); if (!CramIO.replaceCramHeader(params.cramFile, newHeader)) { log.error("Failed to replace the header."); System.exit(1);
/** * Read CRAM header from the given {@link InputStream}. * * @param inputStream input stream to read from * @return complete {@link CramHeader} object */ public static CramHeader readCramHeader(final InputStream inputStream) { try { 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); } catch (final IOException e) { throw new RuntimeIOException(e); } }
/** * 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); } }
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 ExposedByteArrayOutputStream byteArrayOutputStream = new ExposedByteArrayOutputStream(); block.write(newHeader.getVersion().major, byteArrayOutputStream);
/** * Check if the {@link SeekableStream} is properly terminated with a end-of-file marker. * * @param version CRAM version to assume * @param seekableStream the stream to read from * @return true if the stream ends with a correct EOF marker, false otherwise * @throws IOException as per java IO contract */ @SuppressWarnings("SimplifiableIfStatement") private static boolean checkEOF(final Version version, final SeekableStream seekableStream) throws IOException { if (version.compatibleWith(CramVersions.CRAM_v3)) return streamEndsWith(seekableStream, ZERO_F_EOF_MARKER); if (version.compatibleWith(CramVersions.CRAM_v2_1)) return streamEndsWith(seekableStream, ZERO_B_EOF_MARKER); return false; }
public CraiIndexer(InputStream cramInputStream, OutputStream craiOutputStream) throws FileNotFoundException, IOException { this.is = new CountingInputStream(cramInputStream); cramHeader = CramIO.readCramHeader(this.is); index = new CramIndex(); os = new GZIPOutputStream(new BufferedOutputStream(craiOutputStream)); }