Skip to content

Commit

Permalink
Fix pysam.VariantFile attribute setting
Browse files Browse the repository at this point in the history
Minor code cleaning and docs
  • Loading branch information
ACEnglish committed Jan 7, 2025
1 parent 6db6e8e commit 8b053ea
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
12 changes: 8 additions & 4 deletions truvari/bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,10 @@ def __init__(self, bench, params):
with open(os.path.join(self.m_bench.outdir, 'params.json'), 'w') as fout:
json.dump(param_dict, fout)

b_vcf = truvari.VariantFile(self.m_bench.base_vcf, params=self.m_params)
c_vcf = truvari.VariantFile(self.m_bench.comp_vcf, params=self.m_params)
b_vcf = truvari.VariantFile(
self.m_bench.base_vcf, params=self.m_params)
c_vcf = truvari.VariantFile(
self.m_bench.comp_vcf, params=self.m_params)
self.n_headers = {'b': edit_header(b_vcf),
'c': edit_header(c_vcf)}

Expand Down Expand Up @@ -407,6 +409,7 @@ def close_outputs(self):
self.stats_box.write_json(os.path.join(
self.m_bench.outdir, "summary.json"))


class Bench():
"""
Object to perform operations of truvari bench
Expand Down Expand Up @@ -524,6 +527,7 @@ def compare_chunk(self, chunk):
logging.debug("Comparing chunk %s", chunk_id)
result = self.compare_calls(
chunk_dict["base"], chunk_dict["comp"], chunk_id)
# Not checking BNDs as part of refine_candidates because they can't be refined.
self.check_refine_candidate(result)
# Check BNDs separately
if self.params.bnddist != -1 and (chunk_dict['base_BND'] or chunk_dict['comp_BND']):
Expand Down Expand Up @@ -619,8 +623,8 @@ def check_refine_candidate(self, result):
# min(10, self.matcher.chunksize) need to make sure the refine covers the region
buf = 10
start = max(0, min(*pos) - buf)
self.refine_candidates.append(
f"{chrom}\t{start}\t{max(*pos) + buf}")
end = max(*pos) + buf
self.refine_candidates.append(f"{chrom}\t{start}\t{end}")

#################
# Match Pickers #
Expand Down
24 changes: 18 additions & 6 deletions truvari/matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,25 @@ def __repr__(self):

def file_zipper(*start_files):
"""
Zip files to yield the entries in order.
Each file must be sorted in the same order.
start_files is a tuple of ('key', iterable)
where key is the identifier (so we know which file the yielded entry came from)
and iterable is usually a truvari.VariantFile
Zip multiple files to yield their entries in order.
yields key, truvari.VariantRecord
The function takes as input tuples of (`key`, `iterable`), where:
- `key` is an identifier (used to track which file the yielded entry comes from).
- `iterable` is an iterable object, typically a `truvari.VariantFile`.
The function iterates through all input files in a coordinated manner, yielding the entries in order.
:param start_files: A variable-length argument list of tuples (`key`, `iterable`).
:type start_files: tuple
:yields: A tuple of (`key`, `truvari.VariantRecord`), where `key` is the file identifier and the second element is the next record from the corresponding file.
:rtype: tuple
:raises StopIteration: Raised when all input files have been exhausted.
**Logs**:
- Logs a summary of the zipping process after all files have been processed.
"""
markers = [] # list of lists: [name, file_handler, top_entry]
file_counts = Counter()
Expand Down
19 changes: 16 additions & 3 deletions truvari/variant_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

RC = str.maketrans("ATCG", "TAGC")


class VariantRecord:
"""
Wrapper around pysam.VariantRecords with helper functions of variant properties and basic comparisons
Expand All @@ -35,6 +34,21 @@ def __getattr__(self, name):
"""
return getattr(self._record, name)

def __setattr__(self, name, value):
"""
Attempt to delegate attribute setting to the original VariantRecord first
"""
if name.startswith("_") or not hasattr(self, "_record"):
# Directly set internal attributes or during __init__ before _record is set
super().__setattr__(name, value)
else:
try:
# Try to set the attribute on the wrapped _record
setattr(self._record, name, value)
except AttributeError:
# If the wrapped object does not have the attribute, set it on self
super().__setattr__(name, value)

def __str__(self):
return str(self._record)

Expand Down Expand Up @@ -348,8 +362,7 @@ def is_present(self, sample=0, allow_missing=True):
gt = self.gt(sample)
if allow_missing:
return 1 in gt
# Hemi...
return truvari.get_gt(gt)[truvari.GT.HET, truvari.GT.HOM]
return truvari.get_gt(gt) in [truvari.GT.HET, truvari.GT.HOM]

def is_single_bnd(self):
"""
Expand Down

0 comments on commit 8b053ea

Please sign in to comment.