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

#include <sorted_vcf_writer.hpp>

Classes

struct  BufferedRecordKey
 
struct  ReferenceContig
 A contig of the reference, for a ##contig line of the header. More...
 

Public Member Functions

 SortedVCFWriter (const string &sample_name)
 
bool add_variant (vcflib::Variant &var, size_t block=0, bool hidden=false)
 
void set_contigs (vector< ReferenceContig > declared, bool only_with_records=false)
 
void add_info_line (string line)
 
void add_format_line (string line)
 
void add_filter_line (string line)
 
void write_variants (ostream &out, const SnarlDecomposition *sites=nullptr, const HandleGraph *graph=nullptr, const function< string(const net_handle_t &)> &record_id_of_site={})
 
void set_nested (bool nested)
 Write the nesting INFO tags (LV, CH, PS, RC, RS, RD) to every record. More...
 
void set_gref_levels (map< string, int > levels)
 
void set_translation (const NodeTranslation *translation)
 Name nodes in record IDs and AT by translation instead of by their IDs. More...
 
const NodeTranslation * translation () const
 
const string & sample_name () const
 
void set_sample_names (vector< string > names)
 
void vcf_fixup (vcflib::Variant &var) const
 Run vcflib's vcffixup on a record, filling AC, AN and the like. More...
 

Static Public Member Functions

static string nesting_info_headers ()
 The nesting INFO header lines, for both vg call and vg deconstruct. More...
 

Static Public Attributes

static const int64_t max_vcf_line_length = 2000000000
 A record longer than this is refused. More...
 

Private Member Functions

string vcf_header (const unordered_set< string > *keep_contigs) const
 
unordered_set< string > get_output_contigs () const
 Return the reference contigs that have any records in the buffer. More...
 
void update_nesting_info_tags (const SnarlDecomposition &sites, const function< string(const net_handle_t &)> &record_id_of_site)
 

Private Attributes

string sample
 The name of the sample the writer was made for. More...
 
vcflib::VariantCallFile output_vcf
 
vector< ReferenceContig > contigs
 The contigs for the ##contig lines, in order. More...
 
bool only_contigs_with_records = false
 Whether the header declares only the contigs that have records. More...
 
vector< vector< pair< BufferedRecordKey, string > > > output_variants
 
vector< unordered_map< string, Region > > suppressed_ref_info
 
vector< string > info_lines
 The ##INFO lines added to the header. More...
 
vector< string > format_lines
 The ##FORMAT lines added to the header. More...
 
vector< string > filter_lines
 The ##FILTER lines added to the header. More...
 
const NodeTranslation * node_translation = nullptr
 The node translation that names nodes, or null to name them by ID. More...
 
bool include_nested = false
 Whether to write the nesting INFO tags. More...
 
map< string, int > gref_levels
 The gRef nesting level of each gRef contig, by contig name. Empty without a gRef cover. More...
 

Detailed Description

A sorted VCF, built up from records and written out at the end.

Records can be added from any thread. This class is responsible for buffering them until the end, giving them the nesting INFO tags, sorting them, and writing them.

Constructor & Destructor Documentation

◆ SortedVCFWriter()

vg::SortedVCFWriter::SortedVCFWriter ( const string &  sample_name)
explicit

Member Function Documentation

◆ add_filter_line()

void vg::SortedVCFWriter::add_filter_line ( string  line)

Add one ##FILTER line, without its newline, to the header. The added FILTER lines follow the PASS line, in the order they were added.

◆ add_format_line()

void vg::SortedVCFWriter::add_format_line ( string  line)

Add one ##FORMAT line, without its newline, to the header. The added FORMAT lines follow the GT line, in the order they were added.

◆ add_info_line()

void vg::SortedVCFWriter::add_info_line ( string  line)

Add one ##INFO line, without its newline, to the header. The added INFO lines follow the nesting lines, in the order they were added.

◆ add_variant()

bool vg::SortedVCFWriter::add_variant ( vcflib::Variant &  var,
size_t  block = 0,
bool  hidden = false 
)

Add a record to the output. The record is attached to the writer's VCF file, which gives it the writer's sample columns, and is then kept as its text.

A hidden record is not written. The writer keeps only its ID and the stretch of the reference its REF covers, so that a record nested inside it can still be placed on the reference when the nesting tags are computed.

Parameters
blockThe number of the difference block of its site that the record holds, or 0 for a record of a whole site. Two blocks of one site share an ID and can share a POS.
Returns
false, without adding the record, if its line is longer than max_vcf_line_length.

◆ get_output_contigs()

unordered_set< string > vg::SortedVCFWriter::get_output_contigs ( ) const
private

Return the reference contigs that have any records in the buffer.

◆ nesting_info_headers()

string vg::SortedVCFWriter::nesting_info_headers ( )
static

The nesting INFO header lines, for both vg call and vg deconstruct.

◆ sample_name()

const string & vg::SortedVCFWriter::sample_name ( ) const

◆ set_contigs()

void vg::SortedVCFWriter::set_contigs ( vector< ReferenceContig >  declared,
bool  only_with_records = false 
)

Set the contigs that the header declares, in order.

Parameters
only_with_recordsDeclare only the contigs that have records.

◆ set_gref_levels()

void vg::SortedVCFWriter::set_gref_levels ( map< string, int >  levels)

Set the gRef nesting level of each gRef contig, by contig name.

A record on one of these contigs gets at least its level as INFO/CH.

◆ set_nested()

void vg::SortedVCFWriter::set_nested ( bool  nested)

Write the nesting INFO tags (LV, CH, PS, RC, RS, RD) to every record.

◆ set_sample_names()

void vg::SortedVCFWriter::set_sample_names ( vector< string >  names)

Write the sample columns of the records for names, in order, instead of for the one sample the writer was made for.

◆ set_translation()

void vg::SortedVCFWriter::set_translation ( const NodeTranslation *  translation)

Name nodes in record IDs and AT by translation instead of by their IDs.

◆ translation()

const NodeTranslation * vg::SortedVCFWriter::translation ( ) const

◆ update_nesting_info_tags()

void vg::SortedVCFWriter::update_nesting_info_tags ( const SnarlDecomposition &  sites,
const function< string(const net_handle_t &)> &  record_id_of_site 
)
private

Add the nesting INFO tags to every buffered record, from the snarl tree sites, whose sites have the record IDs record_id_of_site gives.

◆ vcf_fixup()

void vg::SortedVCFWriter::vcf_fixup ( vcflib::Variant &  var) const

Run vcflib's vcffixup on a record, filling AC, AN and the like.

◆ vcf_header()

string vg::SortedVCFWriter::vcf_header ( const unordered_set< string > *  keep_contigs) const
private

Return the text of the whole header, each line ending in a newline. If keep_contigs is not null, only the contigs in it get ##contig lines.

The header starts with the fileformat line and the ##contig lines. The INFO, FORMAT and FILTER lines follow, grouped by type. Each group starts with the lines that the writer always writes, and then has the added lines. The ##SAMPLE lines and the column line come last.

◆ write_variants()

void vg::SortedVCFWriter::write_variants ( ostream &  out,
const SnarlDecomposition *  sites = nullptr,
const HandleGraph *  graph = nullptr,
const function< string(const net_handle_t &)> &  record_id_of_site = {} 
)

Write the header, then the records, sorted, and empty the buffer. Call it once, after every record and header line has been added.

With nesting on, each record first gets the nesting INFO tags, which say where its site lies in the snarl tree sites and where to find it on the reference. A record is matched to its site by its ID, so record_id_of_site must give each snarl's ID as its records carry it. When nesting is off, sites may be null.

Parameters
graphThe graph of sites, which the default record IDs read node IDs from. It may be null when record_id_of_site is given or nesting is off.
record_id_of_siteReturns the record ID of a snarl of sites, read the way the net handle traverses it. When empty, a snarl is named from its boundary nodes.

Member Data Documentation

◆ contigs

vector<ReferenceContig> vg::SortedVCFWriter::contigs
private

The contigs for the ##contig lines, in order.

◆ filter_lines

vector<string> vg::SortedVCFWriter::filter_lines
private

The ##FILTER lines added to the header.

◆ format_lines

vector<string> vg::SortedVCFWriter::format_lines
private

The ##FORMAT lines added to the header.

◆ gref_levels

map<string, int> vg::SortedVCFWriter::gref_levels
private

The gRef nesting level of each gRef contig, by contig name. Empty without a gRef cover.

◆ include_nested

bool vg::SortedVCFWriter::include_nested = false
private

Whether to write the nesting INFO tags.

◆ info_lines

vector<string> vg::SortedVCFWriter::info_lines
private

The ##INFO lines added to the header.

◆ max_vcf_line_length

const int64_t vg::SortedVCFWriter::max_vcf_line_length = 2000000000
static

A record longer than this is refused.

◆ node_translation

const NodeTranslation* vg::SortedVCFWriter::node_translation = nullptr
private

The node translation that names nodes, or null to name them by ID.

◆ only_contigs_with_records

bool vg::SortedVCFWriter::only_contigs_with_records = false
private

Whether the header declares only the contigs that have records.

◆ output_variants

vector<vector<pair<BufferedRecordKey, string> > > vg::SortedVCFWriter::output_variants
private

The buffered records, one buffer per thread, each a sort key and the zstd-compressed line. A vcflib::Variant is too big to keep in memory for every record.

◆ output_vcf

vcflib::VariantCallFile vg::SortedVCFWriter::output_vcf
private

The VCF file that the records belong to. Its sample names are the samples whose columns the records have, in order.

We take a deliberate shortcut with this file. vcflib expects a file to be opened with its header before any of its records exist. Here the records are printed to text as they are added, and the header is known only at the end. So while records are added, the file holds only the sample names. A record attached to the file copies them, and printing the record reads only those copies. The shortcut is safe because nothing reads the INFO or FORMAT types of the file through a record before the header is in place: a record is kept only as text once it is added. The file is opened with the real header just before anything is written.

◆ sample

string vg::SortedVCFWriter::sample
private

The name of the sample the writer was made for.

◆ suppressed_ref_info

vector<unordered_map<string, Region> > vg::SortedVCFWriter::suppressed_ref_info
private

The reference interval of each hidden record, by its ID, one table per thread like output_variants.

A hidden record is one of a site that had no variant to report. A record nested under such a site may have no other reference coordinate to point its RC, RS and RD at. This is common in gref graphs, where the parent of an island of non-reference sequence is often a large snarl that only the reference and its own gref copy span.


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