Skip to content

Commit

Permalink
refact(call): more partial/itemgetter usage
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlougheed committed Oct 2, 2024
1 parent b8cc5d8 commit 6fd7ac0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
5 changes: 3 additions & 2 deletions strkit/call/call_locus.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
from .types import (
VCFContigFormat, AssignMethod, AssignMethodWithHP, ConsensusMethod, ReadDict, ReadDictExtra, CalledSNV, LocusResult
)
from .utils import idx_0_getter, find_pair_by_ref_pos, normalize_contig, get_new_seed, calculate_seq_with_wildcards
from .utils import (
idx_0_getter, cn_getter, find_pair_by_ref_pos, normalize_contig, get_new_seed, calculate_seq_with_wildcards
)


__all__ = [
Expand Down Expand Up @@ -85,7 +87,6 @@


# property getters & other partials
cn_getter = operator.itemgetter("cn")
weight_getter = operator.itemgetter("w")
not_snv_out_of_range_char = functools.partial(operator.ne, SNV_OUT_OF_RANGE_CHAR)
eq_0 = functools.partial(operator.eq, 0)
Expand Down
17 changes: 11 additions & 6 deletions strkit/call/output/vcf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
# from os.path import commonprefix
from pathlib import Path
from pysam import FastaFile, VariantFile, VariantHeader, VariantRecord
from typing import Optional
from typing import Iterable, Optional

from strkit.utils import cat_strs, is_none
from ..allele import get_n_alleles
from ..params import CallParams
from ..utils import idx_0_getter
from ..utils import idx_0_getter, cn_getter

__all__ = [
"build_vcf_header",
Expand Down Expand Up @@ -39,6 +39,11 @@
VT_SNV = "snv"


def iter_to_upper(x: Iterable[str]) -> Iterable[str]:
# noinspection PyTypeChecker
return map(str.upper, x)


def build_vcf_header(sample_id: str, reference_file: str) -> VariantHeader:
vh = VariantHeader() # automatically sets VCF version to 4.2

Expand Down Expand Up @@ -134,16 +139,16 @@ def output_contig_vcf_lines(
logger.error(f"Encountered None in results[{result_idx}].peaks.start_anchor_seqs: {peak_start_anchor_seqs}")
continue

seqs: tuple[str, ...] = tuple(map(str.upper, peak_seqs))
seqs_with_anchors = tuple(zip(seqs, tuple(map(str.upper, peak_start_anchor_seqs))))
seqs: tuple[str, ...] = tuple(iter_to_upper(peak_seqs))
seqs_with_anchors = tuple(zip(seqs, tuple(iter_to_upper(peak_start_anchor_seqs))))

if 0 < len(seqs) < n_alleles:
seqs = tuple([seqs[0]] * n_alleles)
seqs_with_anchors = tuple([seqs_with_anchors[0]] * n_alleles)

seq_alts = sorted(
set(filter(lambda c: not (c[0] == ref_seq and c[1] == ref_start_anchor), seqs_with_anchors)),
key=lambda x: x[0]
key=idx_0_getter
)

# common_suffix_idx = -1 * len(commonprefix(tuple(map(_reversed_str, (ref_seq, *seqs)))))
Expand Down Expand Up @@ -204,7 +209,7 @@ def output_contig_vcf_lines(
lambda pair: ":".join(map(str, pair)),
sorted(
Counter(
map(lambda r: r["cn"], filter(lambda r: r.get("p") == pi, res_reads.values()))
map(cn_getter, filter(lambda r: r.get("p") == pi, res_reads.values()))
).items()
)
)
Expand Down
3 changes: 3 additions & 0 deletions strkit/call/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
__all__ = [
"idx_0_getter",
"idx_1_getter",
"cn_getter",
"neq_blank",
"find_pair_by_ref_pos",
"normalize_contig",
Expand All @@ -19,8 +20,10 @@
]


# index/property getters and other partials
idx_0_getter = operator.itemgetter(0)
idx_1_getter = operator.itemgetter(1)
cn_getter = operator.itemgetter("cn")
neq_blank = partial(operator.ne, "")


Expand Down

0 comments on commit 6fd7ac0

Please sign in to comment.