CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pyvcf

A VCFv4.0 and 4.1 parser for Python

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

vcf-writing.mddocs/

VCF File Writing

Functionality for writing VCF records to files while preserving metadata integrity and VCF format compliance.

Capabilities

VCF Writer

Write VCF records to files with proper formatting and metadata preservation.

class Writer:
    def __init__(self, stream, template, lineterminator="\n"):
        """
        Initialize VCF writer.
        
        Parameters:
        - stream: file-like object, output stream
        - template: Reader object, provides header metadata and format
        - lineterminator: str, line ending character(s)
        """
        
    def write_record(self, record):
        """
        Write a variant record to the output file.
        
        Parameters:
        - record: _Record object to write
        """
        
    def flush(self):
        """Flush the output stream."""
        
    def close(self):
        """Close the output stream."""

Backwards Compatibility

class VCFWriter:
    """Alias for Writer class for backwards compatibility."""
    pass

Usage Examples

import vcf

# Basic VCF writing
reader = vcf.Reader(filename='input.vcf')
writer = vcf.Writer(open('output.vcf', 'w'), reader)

for record in reader:
    # Optional: modify record
    if record.QUAL and record.QUAL > 30:
        writer.write_record(record)

writer.close()

# Writing filtered records
reader = vcf.Reader(filename='input.vcf')
with open('filtered.vcf', 'w') as output_file:
    writer = vcf.Writer(output_file, reader)
    
    for record in reader:
        # Filter by variant type
        if record.is_snp:
            writer.write_record(record)
    
    writer.flush()

# Copy with modifications
reader = vcf.Reader(filename='input.vcf')
writer = vcf.Writer(open('modified.vcf', 'w'), reader)

for record in reader:
    # Add INFO field
    record.INFO['PROCESSED'] = True
    writer.write_record(record)

writer.close()

Install with Tessl CLI

npx tessl i tessl/pypi-pyvcf

docs

constants.md

genotype-analysis.md

index.md

sample-filtering.md

utils.md

variant-records.md

vcf-filtering.md

vcf-parsing.md

vcf-writing.md

tile.json