vg
tools for working with variation graphs
Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
vg::HTSAlignmentEmitter Class Reference

#include <hts_alignment_emitter.hpp>

Inheritance diagram for vg::HTSAlignmentEmitter:
vg::io::AlignmentEmitter vg::SplicedHTSAlignmentEmitter

Public Member Functions

 HTSAlignmentEmitter (const string &filename, const string &format, const map< string, int64_t > &path_length, size_t max_threads)
 
 ~HTSAlignmentEmitter ()
 Tear down an HTSAlignmentEmitter and destroy HTSlib structures. More...
 
 HTSAlignmentEmitter (const HTSAlignmentEmitter &other)=delete
 
HTSAlignmentEmitteroperator= (const HTSAlignmentEmitter &other)=delete
 
 HTSAlignmentEmitter (HTSAlignmentEmitter &&other)=delete
 
HTSAlignmentEmitteroperator= (HTSAlignmentEmitter &&other)=delete
 
virtual void emit_singles (vector< Alignment > &&aln_batch)
 Emit a batch of Alignments. More...
 
virtual void emit_mapped_singles (vector< vector< Alignment >> &&alns_batch)
 
virtual void emit_pairs (vector< Alignment > &&aln1_batch, vector< Alignment > &&aln2_batch, vector< int64_t > &&tlen_limit_batch)
 Emit a batch of pairs of Alignments. More...
 
virtual void emit_mapped_pairs (vector< vector< Alignment >> &&alns1_batch, vector< vector< Alignment >> &&alns2_batch, vector< int64_t > &&tlen_limit_batch)
 
- Public Member Functions inherited from vg::io::AlignmentEmitter
virtual void emit_single (Alignment &&aln)
 Emit a single Alignment. More...
 
virtual void emit_mapped_single (vector< Alignment > &&alns)
 Emit a single Alignment with secondaries. All secondaries must have is_secondary set already. More...
 
virtual void emit_pair (Alignment &&aln1, Alignment &&aln2, int64_t tlen_limit=0)
 
virtual void emit_mapped_pair (vector< Alignment > &&alns1, vector< Alignment > &&alns2, int64_t tlen_limit=0)
 
virtual ~AlignmentEmitter ()=default
 Allow destruction through base class pointer. More...
 

Private Member Functions

virtual void convert_alignment (const Alignment &aln, vector< pair< int, char >> &cigar, bool &pos_rev, int64_t &pos, string &path_name) const
 Describe the given alignment as a CIGAR and start position. More...
 
void convert_unpaired (Alignment &aln, bam_hdr_t *header, vector< bam1_t * > &dest)
 
void convert_paired (Alignment &aln1, Alignment &aln2, bam_hdr_t *header, int64_t tlen_limit, vector< bam1_t * > &dest)
 
void save_records (bam_hdr_t *header, vector< bam1_t * > &records, size_t thread_number)
 
bam_hdr_t * ensure_header (const Alignment &sniff, size_t thread_number)
 
void initialize_sam_file (bam_hdr_t *header, size_t thread_number, bool keep_header=false)
 

Private Attributes

unique_ptr< ofstream > out_file
 If we are doing output to a file, this will hold the open file. Otherwise (for stdout) it will be empty. More...
 
vg::io::StreamMultiplexer multiplexer
 
string format
 This holds our format name, for later error messages. More...
 
map< string, int64_t > path_length
 Store the path length map until the header can be made. More...
 
vector< hFILE * > backing_files
 
vector< samFile * > sam_files
 
atomic< bam_hdr_t * > atomic_header
 We need a header. More...
 
string sam_header
 
mutex header_mutex
 If the header isn't present when we want to write, we need a mutex to control creating it. More...
 
bool output_is_bgzf
 
string hts_mode
 Remember the HTSlib mode string we need to open our files. More...
 

Static Private Attributes

static const size_t BGZF_FOOTER_LENGTH = 28
 We hack about with htslib's BGZF EOF footers, so we need to know how long they are. More...
 

Detailed Description

Emit Alignments to a stream in SAM/BAM/CRAM format. Thread safe.

Constructor & Destructor Documentation

◆ HTSAlignmentEmitter() [1/3]

vg::HTSAlignmentEmitter::HTSAlignmentEmitter ( const string &  filename,
const string &  format,
const map< string, int64_t > &  path_length,
size_t  max_threads 
)

Create an HTSAlignmentEmitter writing to the given file (or "-") in the given HTS format ("SAM", "BAM", "CRAM"). path_length must map from contig name to length to include in the header. Sample names and read groups for the header will be guessed from the first reads. HTSlib positions will be read from the alignments' refpos, and the alignments must be surjected.

◆ ~HTSAlignmentEmitter()

vg::HTSAlignmentEmitter::~HTSAlignmentEmitter ( )

Tear down an HTSAlignmentEmitter and destroy HTSlib structures.

◆ HTSAlignmentEmitter() [2/3]

vg::HTSAlignmentEmitter::HTSAlignmentEmitter ( const HTSAlignmentEmitter other)
delete

◆ HTSAlignmentEmitter() [3/3]

vg::HTSAlignmentEmitter::HTSAlignmentEmitter ( HTSAlignmentEmitter &&  other)
delete

Member Function Documentation

◆ convert_alignment()

void vg::HTSAlignmentEmitter::convert_alignment ( const Alignment aln,
vector< pair< int, char >> &  cigar,
bool &  pos_rev,
int64_t &  pos,
string &  path_name 
) const
privatevirtual

Describe the given alignment as a CIGAR and start position.

Reimplemented in vg::SplicedHTSAlignmentEmitter.

◆ convert_paired()

void vg::HTSAlignmentEmitter::convert_paired ( Alignment aln1,
Alignment aln2,
bam_hdr_t *  header,
int64_t  tlen_limit,
vector< bam1_t * > &  dest 
)
private

Convert a paired alignment to HTS format. Header must have been created already.

◆ convert_unpaired()

void vg::HTSAlignmentEmitter::convert_unpaired ( Alignment aln,
bam_hdr_t *  header,
vector< bam1_t * > &  dest 
)
private

Convert an unpaired alignment to HTS format. Header must have been created already.

◆ emit_mapped_pairs()

void vg::HTSAlignmentEmitter::emit_mapped_pairs ( vector< vector< Alignment >> &&  alns1_batch,
vector< vector< Alignment >> &&  alns2_batch,
vector< int64_t > &&  tlen_limit_batch 
)
virtual

Emit the mappings of a batch of pairs of Alignments. All secondaries must have is_secondary set already.

Implements vg::io::AlignmentEmitter.

◆ emit_mapped_singles()

void vg::HTSAlignmentEmitter::emit_mapped_singles ( vector< vector< Alignment >> &&  alns_batch)
virtual

Emit a batch of Alignments with secondaries. All secondaries must have is_secondary set already.

Implements vg::io::AlignmentEmitter.

◆ emit_pairs()

void vg::HTSAlignmentEmitter::emit_pairs ( vector< Alignment > &&  aln1_batch,
vector< Alignment > &&  aln2_batch,
vector< int64_t > &&  tlen_limit_batch 
)
virtual

Emit a batch of pairs of Alignments.

Implements vg::io::AlignmentEmitter.

◆ emit_singles()

void vg::HTSAlignmentEmitter::emit_singles ( vector< Alignment > &&  aln_batch)
virtual

Emit a batch of Alignments.

Implements vg::io::AlignmentEmitter.

◆ ensure_header()

bam_hdr_t * vg::HTSAlignmentEmitter::ensure_header ( const Alignment sniff,
size_t  thread_number 
)
private

Make sure that the HTS header has been written, and the samFile* in sam_files has been created for the given thread.

If the header has not been written, blocks until it has been written.

If we end up being the thread to write it, sniff header information from the given alignment.

Returns the header pointer, so we don't have to do another atomic read later.

◆ initialize_sam_file()

void vg::HTSAlignmentEmitter::initialize_sam_file ( bam_hdr_t *  header,
size_t  thread_number,
bool  keep_header = false 
)
private

Given a header and a thread number, make sure the samFile* for that thread is initialized and ready to have alignments written to it. If true, actually writes the given header into the output file created by the multiplexer. If the samFile* was already initialized, flushes it out and makes a breakpoint.

◆ operator=() [1/2]

HTSAlignmentEmitter& vg::HTSAlignmentEmitter::operator= ( const HTSAlignmentEmitter other)
delete

◆ operator=() [2/2]

HTSAlignmentEmitter& vg::HTSAlignmentEmitter::operator= ( HTSAlignmentEmitter &&  other)
delete

◆ save_records()

void vg::HTSAlignmentEmitter::save_records ( bam_hdr_t *  header,
vector< bam1_t * > &  records,
size_t  thread_number 
)
private

Write and deallocate a bunch of BAM records. Takes care of locking the file. Header must have been written already.

Member Data Documentation

◆ atomic_header

atomic<bam_hdr_t*> vg::HTSAlignmentEmitter::atomic_header
private

We need a header.

◆ backing_files

vector<hFILE*> vg::HTSAlignmentEmitter::backing_files
private

To back our samFile*s, we need the hFILE* objects wrapping our C++ streams. We need to manually flush these after HTS headers are written, since bgzf_flush, which samtools calls, closes a BGZF block and sends the data to the hFILE* but does not actually flush the hFILE*. These will be pointers to the hFILE* for each thread's samFile*. We may only use them while the samFile* they belong to is still open; closing the samFile* will free the hFILE* but not null it out of this vector.

◆ BGZF_FOOTER_LENGTH

const size_t vg::HTSAlignmentEmitter::BGZF_FOOTER_LENGTH = 28
staticprivate

We hack about with htslib's BGZF EOF footers, so we need to know how long they are.

◆ format

string vg::HTSAlignmentEmitter::format
private

This holds our format name, for later error messages.

◆ header_mutex

mutex vg::HTSAlignmentEmitter::header_mutex
private

If the header isn't present when we want to write, we need a mutex to control creating it.

◆ hts_mode

string vg::HTSAlignmentEmitter::hts_mode
private

Remember the HTSlib mode string we need to open our files.

◆ multiplexer

vg::io::StreamMultiplexer vg::HTSAlignmentEmitter::multiplexer
private

This holds a StreamMultiplexer on the output stream, for sharing it between threads.

◆ out_file

unique_ptr<ofstream> vg::HTSAlignmentEmitter::out_file
private

If we are doing output to a file, this will hold the open file. Otherwise (for stdout) it will be empty.

◆ output_is_bgzf

bool vg::HTSAlignmentEmitter::output_is_bgzf
private

Remember if we are outputting BGZF-compressed data or not. If we are, we trim off spurious EOF markers and append our own.

◆ path_length

map<string, int64_t> vg::HTSAlignmentEmitter::path_length
private

Store the path length map until the header can be made.

◆ sam_files

vector<samFile*> vg::HTSAlignmentEmitter::sam_files
private

We make one samFile* per thread, on each thread's output stream form the multiplexer. As soon as we create them, we show them the header, so they are initialized properly. If they have not yet been filled in (because the header is not ready yet), they are null.

◆ sam_header

string vg::HTSAlignmentEmitter::sam_header
private

We also need a header string. Not atomic, because by the time we read it we know the header is ready and nobody is writing to it.


The documentation for this class was generated from the following files: