Skip to content

Commit

Permalink
Doc & code clean
Browse files Browse the repository at this point in the history
Got the context manager working
  • Loading branch information
ACEnglish committed Jan 7, 2025
1 parent 5995137 commit 6db6e8e
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 63 deletions.
104 changes: 51 additions & 53 deletions docs/api/truvari.package.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,54 +22,46 @@ Variant Handling
.. autoclass:: VariantParams
:members:

Extra Methods
-------------
.. autofunction:: bed_ranges

.. autofunction:: best_seqsim

.. autofunction:: read_bed_tree

.. autofunction:: check_vcf_index

.. autofunction:: compress_index_vcf

.. autofunction:: get_gt

.. autofunction:: get_scalebin

.. autofunction:: get_sizebin

.. autofunction:: get_svtype
Objects
-------

.. autofunction:: msa2vcf
.. autoclass:: MatchResult
:members:

.. autofunction:: overlap_percent
.. autoclass:: GT
:members:

.. autofunction:: overlaps
.. autoclass:: SV
:members:

.. autofunction:: phab
.. autoclass:: Bench
:members:

.. autofunction:: reciprocal_overlap
.. autoclass:: BenchOutput
:members:

.. autofunction:: ref_ranges
.. autoclass:: StatsBox
:members:

.. autofunction:: roll_seqsim
.. autoclass:: LogFileStderr
:members:

.. autofunction:: seqsim
Extra Methods
-------------
.. autofunction:: bed_ranges

.. autofunction:: sizesim
.. autofunction:: benchdir_count_entries

.. autofunction:: unroll_seqsim
.. autofunction:: best_seqsim

.. autofunction:: vcf_ranges
.. autofunction:: read_bed_tree

Dev Methods
-----------
.. autofunction:: benchdir_count_entries
.. autofunction:: check_vcf_index

.. autofunction:: chunker

.. autofunction:: compress_index_vcf

.. autofunction:: cmd_exe

.. autofunction:: coords_within
Expand All @@ -80,45 +72,51 @@ Dev Methods

.. autofunction:: help_unknown_cmd

.. autofunction:: get_gt

.. autofunction:: get_scalebin

.. autofunction:: get_sizebin

.. autofunction:: get_svtype

.. autofunction:: make_temp_filename

.. autofunction:: msa2vcf

.. autofunction:: opt_gz_open

.. autofunction:: optimize_df_memory

.. autofunction:: overlap_percent

.. autofunction:: overlaps

.. autofunction:: performance_metrics

.. autofunction:: restricted_float
.. autofunction:: phab

.. autofunction:: restricted_int
.. autofunction:: reciprocal_overlap

.. autofunction:: setup_logging
.. autofunction:: restricted_float

.. autofunction:: vcf_to_df
.. autofunction:: restricted_int

Objects
-------
.. autofunction:: ref_ranges

.. autoclass:: Bench
:members:
.. autofunction:: roll_seqsim

.. autoclass:: BenchOutput
:members:
.. autofunction:: seqsim

.. autoclass:: StatsBox
:members:
.. autofunction:: setup_logging

.. autoclass:: GT
:members:
.. autofunction:: sizesim

.. autoclass:: LogFileStderr
:members:
.. autofunction:: unroll_seqsim

.. autoclass:: MatchResult
:members:
.. autofunction:: vcf_ranges

.. autoclass:: SV
:members:
.. autofunction:: vcf_to_df

Data
----
Expand Down
8 changes: 4 additions & 4 deletions truvari/annotations/svinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def svinfo_main(cmdargs):
args = parse_args(cmdargs)
vcf = truvari.VariantFile(args.input)
n_header = edit_header(vcf.header.copy())
out = truvari.VariantFile(args.output, 'w', header=n_header)
for entry in vcf:
add_svinfo(entry, args.minsize, n_header)
out.write(entry)
with truvari.VariantFile(args.output, 'w', header=n_header) as out:
for entry in vcf:
add_svinfo(entry, args.minsize, n_header)
out.write(entry)
logging.info("Finished svinfo")
35 changes: 34 additions & 1 deletion truvari/matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,40 @@
@total_ordering
class MatchResult(): # pylint: disable=too-many-instance-attributes
"""
A base/comp match holder
Holds results from a matching operation
Attributes
----------
.. list-table::
:header-rows: 1
* - Attribute
- Description
* - `base`
- The base (a.k.a. self) variant
* - `comp`
- The comp (a.k.a. other) variant
* - `state`
- Boolean of if variants match
* - `seqsim`
- Sequence similarity of variants
* - `sizesim`
- Size similarity of variants
* - `ovlpct`
- Reciprocal overlap ov variants
* - `sizediff`
- Base variant var_size minus comp variant var_size
* - `st_dist`
- Base variant start position minus comp variant start position
* - `ed_dist`
- Base variant end position minus comp variant end position
* - `gt_match`
- Boolean of if genotypes match
* - `score`
- TruScore of matches
* - `matid`
- Place to put MatchIds, not populated by `truvari.VariantRecord.match`
"""
__slots__ = ["base", "comp", "base_gt", "base_gt_count", "comp_gt", "comp_gt_count",
"state", "seqsim", "sizesim", "ovlpct", "sizediff", "st_dist", "ed_dist",
Expand Down
19 changes: 14 additions & 5 deletions truvari/variant_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@

class VariantFile:
"""
Wrapper around pysam.VariantFile with helper functions for iteration.
.. note::
The context manager functionality of pysam.VariantFile is not available with truvari.VariantFile.
Wrapper around `pysam.VariantFile` with helper functions for iteration.
"""

def __init__(self, filename, *args, params=None, **kwargs):
Expand All @@ -29,6 +26,19 @@ def __init__(self, filename, *args, params=None, **kwargs):
self.params = params
self._vcf = pysam.VariantFile(filename, *args, **kwargs)

def __enter__(self):
"""
Enter the context of the wrapped `pysam.VariantFile` object
"""
self._vcf.__enter__()
return self

def __exit__(self, exc_type, exc_value, traceback):
"""
Exit the context of the wrapped `pysam.VariantFile` object
"""
return self._vcf.__exit__(exc_type, exc_value, traceback)

def __getattr__(self, name):
"""
Delegate attribute access to the original VariantFile.
Expand Down Expand Up @@ -88,7 +98,6 @@ def fetch_bed(self, bed_fn, inside=True, with_region=False):
tree = truvari.read_bed_tree(bed_fn)
return self.fetch_regions(tree, inside, with_region)


def fetch_regions(self, tree, inside=True, with_region=False):
"""
Fetch variants from the VCF based on regions defined in a tree of chrom:IntervalTree.
Expand Down

0 comments on commit 6db6e8e

Please sign in to comment.