Skip to content

Commit

Permalink
refact(call): break apart confusing t-tuple for call_locus
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlougheed committed Jun 10, 2024
1 parent 20859c4 commit 963f25c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
17 changes: 7 additions & 10 deletions strkit/call/call_locus.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,11 @@ def _nested_ndarray_serialize(x: Iterable) -> list[list[Union[int, np.int_]]]:

def call_locus(
t_idx: int,
t: tuple,
contig: str,
left_coord: int,
right_coord: int,
motif: str,
# ---
n_alleles: int,
bf: STRkitBAMReader,
ref: FastaFile,
Expand Down Expand Up @@ -770,23 +774,17 @@ def call_locus(

rng = np.random.default_rng(seed=seed)

contig: str = t[0]
read_contig = normalize_contig(contig, read_file_has_chr)
ref_contig = normalize_contig(contig, ref_file_has_chr)

motif: str = t[-1]
motif_size = len(motif)

left_coord = int(t[1])
right_coord = int(t[2])

left_flank_coord = left_coord - flank_size
right_flank_coord = right_coord + flank_size

ref_total_seq: str = ""
ref_left_flank_seq: str = ""
ref_right_flank_seq: str = ""
ref_right_flank_seq_plus_1: str = ""
ref_seq: str = ""
raised: bool = False

Expand All @@ -813,11 +811,10 @@ def call_locus(
ref_seq_offset_r = right_coord - left_flank_coord

try:
ref_total_seq = ref.fetch(ref_contig, left_flank_coord, right_flank_coord + 1)
ref_total_seq = ref.fetch(ref_contig, left_flank_coord, right_flank_coord + 1) # plus one for use with realign

ref_left_flank_seq = ref_total_seq[:ref_seq_offset_l]
ref_right_flank_seq_plus_1 = ref_total_seq[ref_seq_offset_r:]
ref_right_flank_seq = ref_right_flank_seq_plus_1[:-1]
ref_right_flank_seq = ref_total_seq[ref_seq_offset_r:-1]
ref_seq = ref_total_seq[ref_seq_offset_l:ref_seq_offset_r]
except IndexError:
logger_.warning(
Expand Down
28 changes: 19 additions & 9 deletions strkit/call/call_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,27 +117,39 @@ def locus_worker(
lg.debug(f"worker {worker_id} encountered queue.Empty")
break

t_idx, t, n_alleles, locus_seed = td
t_idx, contig, left_coord, right_coord, motif, n_alleles, locus_seed = td

if current_contig is None:
current_contig = t[0]
current_contig = contig

# String representation of locus for logging purposes
locus_log_str: str = (
f"[w{worker_id}] {sample_id or ''}{' ' if sample_id else ''}locus {t_idx}: {t[0]}:{t[1]}-{t[2]}"
f"[w{worker_id}] {sample_id or ''}{' ' if sample_id else ''}locus {t_idx}: "
f"{contig}:{left_coord}-{right_coord}"
)

lg.debug(f"{locus_log_str} - working on locus")

try:
res = call_locus(
t_idx, t, n_alleles, bf, ref, params,
t_idx,
contig,
left_coord,
right_coord,
motif,
# ---
n_alleles,
bf,
ref,
params,
# ---
phase_set_lock,
phase_set_counter,
phase_set_remap,
phase_set_synonymous,
snv_genotype_update_lock,
snv_genotype_cache,
# ---
seed=locus_seed,
logger_=lg,
locus_log_str=locus_log_str,
Expand All @@ -150,9 +162,7 @@ def locus_worker(

except Exception as e:
res = None
lg.error(
f"{locus_log_str} - encountered exception while genotyping ({t_idx=}, {t[:3]=}, {n_alleles=}): "
f"{repr(e)}")
lg.error(f"{locus_log_str} - encountered exception while genotyping ({t_idx=}, {n_alleles=}): {repr(e)}")
lg.error(f"{locus_log_str} - {traceback.format_exc()}")

locus_counter_lock.acquire(timeout=30)
Expand Down Expand Up @@ -296,8 +306,8 @@ def call_sample(

# We use locus-specific random seeds for replicability, no matter which order
# the loci are yanked out of the queue / how many processes we have.
# Tuple of (1-indexed locus index, locus data, locus-specific random seed)
locus_queue.put((t_idx, t, n_alleles, get_new_seed(rng)))
# Tuple of (1-indexed locus index, contig, left coord, right coord, motif, locus-specific random seed)
locus_queue.put((t_idx, contig, int(t[1]), int(t[2]), t[-1], n_alleles, get_new_seed(rng)))
num_loci += 1

if num_loci > last_none_append_n_loci: # have progressed since the last None-append
Expand Down

0 comments on commit 963f25c

Please sign in to comment.