Skip to content

Commit 8f214c4

Browse files
committed
perf: use set for masked site lookup and validate sequence lengths
1 parent 7a1f467 commit 8f214c4

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

workflow/scripts/extract_afwdist_variants.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22

33
import logging
4-
from typing import List
4+
from typing import List, Set
55

66
import pandas as pd
77
from Bio import SeqIO
@@ -17,7 +17,7 @@ def read_monofasta(path: str) -> SeqRecord:
1717
return record
1818

1919

20-
def read_masked_sites(vcf_path: str, mask_classes: List[str]) -> List[int]:
20+
def read_masked_sites(vcf_path: str, mask_classes: List[str]) -> Set[int]:
2121
"""
2222
Parse a VCF containing positions for masking. Assumes the VCF file is
2323
formatted as in:
@@ -31,10 +31,10 @@ def read_masked_sites(vcf_path: str, mask_classes: List[str]) -> List[int]:
3131
comment="#",
3232
names=("CHROM", "POS", "ID", "REF", "ALT", "QUAL", "FILTER", "INFO")
3333
)
34-
return vcf.loc[vcf.FILTER.isin(mask_classes), "POS"].tolist()
34+
return set(vcf.loc[vcf.FILTER.isin(mask_classes), "POS"].unique())
3535

3636

37-
def build_ancestor_variant_table(ancestor: Seq, reference: Seq, reference_name: str, masked_positions: List[int]) -> pd.DataFrame:
37+
def build_ancestor_variant_table(ancestor: Seq, reference: Seq, reference_name: str, masked_positions: Set[int]) -> pd.DataFrame:
3838
pos = []
3939
alt = []
4040
for i in range(1, len(ancestor) + 1):
@@ -77,10 +77,11 @@ def build_ancestor_variant_table(ancestor: Seq, reference: Seq, reference_name:
7777
logging.info("Reading input FASTA files")
7878
# Case ancestor
7979
ancestor = read_monofasta(snakemake.input.ancestor)
80-
logging.info(f"Ancestor: '{ancestor.description}', length={len(ancestor.seq)}")
80+
logging.info(f"Ancestor: '{ancestor.description}', length={len(ancestor)}")
8181
# Alignment reference
8282
reference = read_monofasta(snakemake.input.reference)
83-
logging.info(f"Reference: '{reference.description}', length={len(reference.seq)}")
83+
logging.info(f"Reference: '{reference.description}', length={len(reference)}")
84+
assert len(ancestor) == len(reference)
8485

8586
logging.info("Processing ancestor variants")
8687
ancestor_table = build_ancestor_variant_table(ancestor.seq, reference.seq, reference.id, masked_sites)

0 commit comments

Comments
 (0)