|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import logging |
| 4 | +from typing import List |
| 5 | + |
| 6 | +import pandas as pd |
| 7 | +from Bio import SeqIO |
| 8 | +from Bio.SeqRecord import SeqRecord |
| 9 | +from Bio.Seq import Seq |
| 10 | + |
| 11 | + |
| 12 | +def read_monofasta(path: str) -> SeqRecord: |
| 13 | + fasta = SeqIO.parse(path, "fasta") |
| 14 | + record = next(fasta) |
| 15 | + if next(fasta, None) is not None: |
| 16 | + logging.warning(f"There are unread records left in '{path}'") |
| 17 | + return record |
| 18 | + |
| 19 | + |
| 20 | +def read_masked_sites(vcf_path: str, mask_classes: List[str]) -> List[int]: |
| 21 | + """ |
| 22 | + Parse a VCF containing positions for masking. Assumes the VCF file is |
| 23 | + formatted as in: |
| 24 | + github.com/W-L/ProblematicSites_SARS-CoV2/blob/master/problematic_sites_sarsCov2.vcf |
| 25 | + with a "mask" or "caution" recommendation in column 7. |
| 26 | + Masked sites are specified with params. |
| 27 | + """ |
| 28 | + vcf = pd.read_csv( |
| 29 | + vcf_path, |
| 30 | + sep="\s+", |
| 31 | + comment="#", |
| 32 | + names=("CHROM", "POS", "ID", "REF", "ALT", "QUAL", "FILTER", "INFO") |
| 33 | + ) |
| 34 | + return vcf.loc[vcf.FILTER.isin(mask_classes), "POS"].tolist() |
| 35 | + |
| 36 | + |
| 37 | +def build_ancestor_variant_table(ancestor: Seq, reference: Seq, reference_name: str, masked_positions: List[int]) -> pd.DataFrame: |
| 38 | + pos = [] |
| 39 | + alt = [] |
| 40 | + for i in range(1, len(ancestor) + 1): |
| 41 | + if i not in masked_positions and ancestor[i-1] != reference[i-1]: |
| 42 | + pos.append(i) |
| 43 | + alt.append(reference[i-1]) |
| 44 | + df = pd.DataFrame({snakemake.params.position_col: pos, snakemake.params.sequence_col: alt}) |
| 45 | + df[snakemake.params.frequency_col] = 1 # As a reference genome, we assume all positions have fixed alleles |
| 46 | + df[snakemake.params.sample_col] = reference_name |
| 47 | + return df |
| 48 | + |
| 49 | + |
| 50 | +if __name__ == "__main__": |
| 51 | + |
| 52 | + logging.basicConfig(filename=snakemake.log[0], format=snakemake.config["LOG_PY_FMT"], level=logging.INFO) |
| 53 | + |
| 54 | + colnames = { |
| 55 | + snakemake.params.sample_col: "sample", |
| 56 | + snakemake.params.position_col: "position", |
| 57 | + snakemake.params.sequence_col: "sequence", |
| 58 | + snakemake.params.frequency_col: "frequency" |
| 59 | + } |
| 60 | + |
| 61 | + logging.info("Reading input tables") |
| 62 | + # Variants |
| 63 | + variants = pd.read_table(snakemake.input.variants, sep="\t") |
| 64 | + logging.info(f"Read {len(variants)} variant records") |
| 65 | + # VCF with sites to mask |
| 66 | + masked_sites = read_masked_sites(snakemake.input.mask_vcf, snakemake.params.mask_class) |
| 67 | + logging.info(f"Read {len(masked_sites)} masked positions") |
| 68 | + |
| 69 | + logging.info("Reading input FASTA files") |
| 70 | + # Case ancestor |
| 71 | + ancestor = read_monofasta(snakemake.input.ancestor) |
| 72 | + logging.info(f"Ancestor: '{ancestor.description}', length={len(ancestor.seq)}") |
| 73 | + # Alignment reference |
| 74 | + reference = read_monofasta(snakemake.input.reference) |
| 75 | + logging.info(f"Reference: '{reference.description}', length={len(reference.seq)}") |
| 76 | + |
| 77 | + logging.info("Processing ancestor variants") |
| 78 | + ancestor_table = build_ancestor_variant_table(ancestor.seq, reference.seq, reference.id, masked_sites) |
| 79 | + logging.info(f"Ancestor has {len(ancestor_table)} variants") |
| 80 | + all_variants = pd.concat([variants, ancestor_table], ignore_index=True) |
| 81 | + logging.info(f"Combined table has {len(all_variants)} variants") |
| 82 | + |
| 83 | + logging.info("Renaming and selecting columns") |
| 84 | + output = all_variants.rename(columns=colnames)[list(colnames.values())] |
| 85 | + logging.info("Filtering sites") |
| 86 | + output = output[~output.position.isin(masked_sites)] |
| 87 | + logging.info(f"There are {len(output)} rows left") |
| 88 | + |
| 89 | + logging.info("Writing results") |
| 90 | + output.to_csv(snakemake.output.variants, index=False) |
0 commit comments