From 6db6e8e35667fe35bfec6b039ee16303c01358ec Mon Sep 17 00:00:00 2001 From: Adam English Date: Tue, 7 Jan 2025 09:41:23 -0500 Subject: [PATCH] Doc & code clean Got the context manager working --- docs/api/truvari.package.rst | 104 +++++++++++++++++----------------- truvari/annotations/svinfo.py | 8 +-- truvari/matching.py | 35 +++++++++++- truvari/variant_file.py | 19 +++++-- 4 files changed, 103 insertions(+), 63 deletions(-) diff --git a/docs/api/truvari.package.rst b/docs/api/truvari.package.rst index a5805b47..4bbe2c11 100644 --- a/docs/api/truvari.package.rst +++ b/docs/api/truvari.package.rst @@ -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 @@ -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 ---- diff --git a/truvari/annotations/svinfo.py b/truvari/annotations/svinfo.py index 7688abbc..82498db9 100644 --- a/truvari/annotations/svinfo.py +++ b/truvari/annotations/svinfo.py @@ -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") diff --git a/truvari/matching.py b/truvari/matching.py index b71b90bf..5978dc6a 100644 --- a/truvari/matching.py +++ b/truvari/matching.py @@ -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", diff --git a/truvari/variant_file.py b/truvari/variant_file.py index d9a5b7a2..6081d2a6 100644 --- a/truvari/variant_file.py +++ b/truvari/variant_file.py @@ -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): @@ -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. @@ -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.