Skip to content

Commit

Permalink
Merge pull request #21 from akikuno:develop-v1.0.3
Browse files Browse the repository at this point in the history
Develop-v1.0.3
  • Loading branch information
akikuno authored Oct 4, 2023
2 parents adb7030 + 96bb0d4 commit a5a46f1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "cstag"
version = "1.0.2"
version = "1.0.3"
description = "Python module to manipulate the minimap2's CS tag"
authors = ["Akihiro Kuno <[email protected]>"]
homepage = "https://github.com/akikuno/cstag"
Expand Down
10 changes: 6 additions & 4 deletions src/cstag/to_vcf.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def replace_mutation_to_atmark(cs_tags: str) -> str:
return "".join(cs if cs in {"A", "C", "G", "T"} else "@" for cs in cs_tags)


def call_reference_depth(variant_annotations, cs_tags_list, positions_list) -> dict[int, int]:
def call_reference_depth(variant_annotations, cs_tags_list, positions_list) -> dict[tuple(str, int), int]:
cs_tags_normalized_length = normalize_read_lengths(cs_tags_list, positions_list)
cs_replaced = [replace_mutation_to_atmark(cs_tags) for cs_tags in cs_tags_normalized_length]

Expand All @@ -204,19 +204,21 @@ def call_reference_depth(variant_annotations, cs_tags_list, positions_list) -> d
v_idx = v.pos - min(positions_list)
for cs in cs_replaced:
if v.ref == cs[v_idx : v_idx + len(v.ref)]:
reference_depth[v.pos] += 1
reference_depth[(v.ref, v.pos)] += 1

return dict(reference_depth)


def add_vcf_fields(variant_annotations: list[Vcf], chrom: str, reference_depth: dict[int, int]) -> list[Vcf]:
def add_vcf_fields(
variant_annotations: list[Vcf], chrom: str, reference_depth: dict[tuple(str, int), int]
) -> list[Vcf]:
"""Add Chrom and VCF info (AD, RD, DP, and VAF) to immutable Vcf dataclass"""
variant_counter = Counter((v.pos, v.ref, v.alt) for v in variant_annotations)

updated_annotations = []
for v in set(variant_annotations):
ad = variant_counter[(v.pos, v.ref, v.alt)]
rd = reference_depth.get(v.pos, 0)
rd = reference_depth.get((v.ref, v.pos), 0)
dp = rd + ad
vaf = round(ad / dp, 3) if dp else 0

Expand Down
4 changes: 2 additions & 2 deletions tests/test_to_vcf.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def test_call_reference_depth():
]
cs_tags_list = ["=A*cg=G+aa=T-ac=G", "*cg=G+aa=T-ac=G", "=G-t=ACG"]
positions_list = [10, 11, 12]
expected_output = {12: 1}
expected_output = {("G", 12): 1}

result = call_reference_depth(variant_annotations, cs_tags_list, positions_list)
assert result == expected_output, f"Expected {expected_output}, but got {result}"
Expand All @@ -186,7 +186,7 @@ def test_add_vcf_fields():
Vcf(chrom=None, pos=2, ref="G", alt="C", info=VcfInfo(dp=None, rd=None, ad=None, vaf=None)),
]
sample_chrom = "chr1"
sample_reference_depth = {1: 10, 2: 5}
sample_reference_depth = {("A", 1): 10, ("G", 2): 5}
result = add_vcf_fields(sample_variant_annotations, sample_chrom, sample_reference_depth)
result = sorted(result, key=lambda x: (chrom_sort_key(x.chrom), x.pos))
assert result[0].chrom == "chr1"
Expand Down

0 comments on commit a5a46f1

Please sign in to comment.