Skip to content

Commit

Permalink
feat: Add only_pass parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapsssito committed Mar 30, 2022
1 parent 0405328 commit 39d7b41
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/vcf_to_bamsurgeon.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def variant_callback(var_type, variant_record):
output_file_sv.write(f'{variant_record.contig} {variant_record.pos} {variant_record.pos} {insertion_prefix}{op}\n')

print(f'Reading VCF file: {args.vcf_file}')
extractor = VariantExtractor()
extractor = VariantExtractor(only_pass=True)
for var_type, variant_record in extractor.read_vcf(args.vcf_file):
variant_callback(var_type, variant_record)

Expand Down
10 changes: 8 additions & 2 deletions src/variant_extractor/VariantExtractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@
from .utils import select_record, extract_bracket_sv, extract_shorthand_sv, extract_sgl_sv, permute_bracket_sv
from .variants import VariantType, VariantRecord


class VariantExtractor:
"""
Reads and extracts variants from VCF files. This class is designed to be
used in a pipeline, where the variants are ingested from VCF files and then used in downstream analysis.
"""

def __init__(self, ensure_pairs=True):
def __init__(self, only_pass=False, ensure_pairs=True):
"""
Parameters
----------
only_pass : bool, optional
If :code:`True`, only records with PASS filter will be considered.
ensure_pairs : bool, optional
If `True`, throws an exception if a breakend is missing a pair when all other were paired successfully.
If :code:`True`, throws an exception if a breakend is missing a pair when all other were paired successfully.
"""
self.ensure_pairs = ensure_pairs
self.only_pass = only_pass

def read_vcf(self, vcf_file):
"""Reads VCF file and extracts all variants.
Expand Down Expand Up @@ -71,6 +75,8 @@ def read_vcf(self, vcf_file):
return self.__variants

def __parse_record(self, rec):
if self.only_pass and 'PASS' not in rec.filter:
return
if len(rec.alts) != 1:
warnings.warn(f'WARNING: Skipping record with multiple alternate alleles ({rec})')
return
Expand Down

0 comments on commit 39d7b41

Please sign in to comment.