Search in sources :

Example 1 with SAMTextHeaderCodec

use of htsjdk.samtools.SAMTextHeaderCodec in project gatk by broadinstitute.

the class ReferenceUtils method loadFastaDictionary.

/**
     * Given an InputStream connected to a fasta dictionary, returns its sequence dictionary
     *
     * Note: does not close the InputStream it's passed
     *
     * @param fastaDictionaryStream InputStream connected to a fasta dictionary
     * @return the SAMSequenceDictionary from the fastaDictionaryStream
     */
public static SAMSequenceDictionary loadFastaDictionary(final InputStream fastaDictionaryStream) {
    // Don't close the reader when we're done, since we don't want to close the client's InputStream for them
    final BufferedLineReader reader = new BufferedLineReader(fastaDictionaryStream);
    final SAMTextHeaderCodec codec = new SAMTextHeaderCodec();
    final SAMFileHeader header = codec.decode(reader, fastaDictionaryStream.toString());
    return header.getSequenceDictionary();
}
Also used : SAMTextHeaderCodec(htsjdk.samtools.SAMTextHeaderCodec) BufferedLineReader(htsjdk.samtools.util.BufferedLineReader) SAMFileHeader(htsjdk.samtools.SAMFileHeader)

Example 2 with SAMTextHeaderCodec

use of htsjdk.samtools.SAMTextHeaderCodec in project gatk by broadinstitute.

the class SparkUtils method writeBAMHeaderToStream.

/**
     * Private helper method for {@link #convertHeaderlessHadoopBamShardToBam} that takes a SAMFileHeader and writes it
     * to the provided `OutputStream`, correctly encoded for the BAM format and preceded by the BAM magic bytes.
     *
     * @param samFileHeader SAM header to write
     * @param outputStream stream to write the SAM header to
     */
private static void writeBAMHeaderToStream(final SAMFileHeader samFileHeader, final OutputStream outputStream) {
    final BlockCompressedOutputStream blockCompressedOutputStream = new BlockCompressedOutputStream(outputStream, null);
    final BinaryCodec outputBinaryCodec = new BinaryCodec(new DataOutputStream(blockCompressedOutputStream));
    final String headerString;
    final Writer stringWriter = new StringWriter();
    new SAMTextHeaderCodec().encode(stringWriter, samFileHeader, true);
    headerString = stringWriter.toString();
    outputBinaryCodec.writeBytes(ReadUtils.BAM_MAGIC);
    // calculate and write the length of the SAM file header text and the header text
    outputBinaryCodec.writeString(headerString, true, false);
    // write the sequences binarily.  This is redundant with the text header
    outputBinaryCodec.writeInt(samFileHeader.getSequenceDictionary().size());
    for (final SAMSequenceRecord sequenceRecord : samFileHeader.getSequenceDictionary().getSequences()) {
        outputBinaryCodec.writeString(sequenceRecord.getSequenceName(), true, true);
        outputBinaryCodec.writeInt(sequenceRecord.getSequenceLength());
    }
    try {
        blockCompressedOutputStream.flush();
    } catch (final IOException ioe) {
        throw new RuntimeIOException(ioe);
    }
}
Also used : BinaryCodec(htsjdk.samtools.util.BinaryCodec) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) SAMTextHeaderCodec(htsjdk.samtools.SAMTextHeaderCodec) BlockCompressedOutputStream(htsjdk.samtools.util.BlockCompressedOutputStream) SAMSequenceRecord(htsjdk.samtools.SAMSequenceRecord) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException)

Aggregations

SAMTextHeaderCodec (htsjdk.samtools.SAMTextHeaderCodec)2 SAMFileHeader (htsjdk.samtools.SAMFileHeader)1 SAMSequenceRecord (htsjdk.samtools.SAMSequenceRecord)1 BinaryCodec (htsjdk.samtools.util.BinaryCodec)1 BlockCompressedOutputStream (htsjdk.samtools.util.BlockCompressedOutputStream)1 BufferedLineReader (htsjdk.samtools.util.BufferedLineReader)1 RuntimeIOException (htsjdk.samtools.util.RuntimeIOException)1