Skip to content

Commit 6fbd958

Browse files
committed
refactor: use cached_property for _snp_effect_annotator in snp_frq.py
1 parent af991c9 commit 6fbd958

3 files changed

Lines changed: 11 additions & 44 deletions

File tree

malariagen_data/anoph/genome_sequence.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ def __init__(
2929
virtual_contigs = dict()
3030
self._virtual_contigs = virtual_contigs
3131

32-
# Initialize cache attributes.
33-
self._cache_genome = None
3432

3533
@property
3634
def contigs(self) -> Tuple[str, ...]:
@@ -62,13 +60,9 @@ def _genome_ref_id(self) -> str:
6260
def _genome_ref_name(self) -> str:
6361
return self.config["GENOME_REF_NAME"]
6462

65-
@_check_types
66-
@doc(
67-
summary="Open the reference genome zarr.",
68-
returns="Zarr hierarchy containing the reference genome sequence.",
69-
)
7063
def open_genome(self) -> zarr.hierarchy.Group:
71-
if self._cache_genome is None:
64+
"""Open the reference genome zarr."""
65+
if not hasattr(self, '_cache_genome') or self._cache_genome is None:
7266
path = f"{self._base_path}/{self._genome_zarr_path}"
7367
store = _init_zarr_store(fs=self._fs, path=path)
7468
self._cache_genome = zarr.open_consolidated(store=store)

malariagen_data/anoph/snp_frq.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from functools import cached_property
12
from typing import Optional, Dict, Union, Callable, List
23
import warnings
34

@@ -40,9 +41,6 @@ def __init__(
4041
# to the superclass constructor.
4142
super().__init__(**kwargs)
4243

43-
# Set up cache variables.
44-
self._cache_annotator = None
45-
4644
def _snp_df_melt(self, *, ds_snp: xr.Dataset) -> pd.DataFrame:
4745
"""Set up a dataframe with SNP site and filter data,
4846
melting each alternate allele into a separate row."""
@@ -81,13 +79,12 @@ def _snp_df_melt(self, *, ds_snp: xr.Dataset) -> pd.DataFrame:
8179

8280
return df_snps
8381

82+
@cached_property
8483
def _snp_effect_annotator(self):
8584
"""Set up variant effect annotator."""
86-
if self._cache_annotator is None:
87-
self._cache_annotator = veff.Annotator(
88-
genome=self.open_genome(), genome_features=self.genome_features()
89-
)
90-
return self._cache_annotator
85+
return veff.Annotator(
86+
genome=self.open_genome(), genome_features=self.genome_features()
87+
)
9188

9289
@_check_types
9390
@doc(
@@ -112,7 +109,7 @@ def snp_effects(
112109
df_snps = self._snp_df_melt(ds_snp=ds_snp)
113110

114111
# Setup variant effect annotator.
115-
ann = self._snp_effect_annotator()
112+
ann = self._snp_effect_annotator
116113

117114
# Add effects to the dataframe.
118115
ann.get_effects(transcript=transcript, variants=df_snps)
@@ -249,7 +246,7 @@ def snp_allele_frequencies(
249246

250247
if effects:
251248
# Add effect annotations.
252-
ann = self._snp_effect_annotator()
249+
ann = self._snp_effect_annotator
253250
ann.get_effects(
254251
transcript=transcript, variants=df_snps, progress=self._progress
255252
)
@@ -586,7 +583,7 @@ def snp_allele_frequencies_advanced(
586583
frequency = np.compress(loc_variant, frequency, axis=0)
587584

588585
# Set up variant effect annotator.
589-
ann = self._snp_effect_annotator()
586+
ann = self._snp_effect_annotator
590587

591588
# Add effects to the dataframe.
592589
ann.get_effects(
@@ -832,7 +829,7 @@ def snp_genotype_allele_counts(
832829
df_snps = pd.concat([df_snps, df_counts], axis=1)
833830

834831
# Add effect annotations.
835-
ann = self._snp_effect_annotator()
832+
ann = self._snp_effect_annotator
836833
ann.get_effects(
837834
transcript=transcript, variants=df_snps, progress=self._progress
838835
)

malariagen_data/plasmodium.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,13 @@ def _load_config(self, data_config):
5151
return config_content
5252

5353
def sample_metadata(self):
54-
"""Access sample metadata and return as pandas dataframe.
55-
Returns
56-
-------
57-
df : pandas.DataFrame
58-
A dataframe of sample metadata on the samples that were sequenced as part of this resource.
59-
Includes the time and place of collection, quality metrics, and accesion numbers.
60-
One row per sample.
61-
"""
6254
if self._cache_sample_metadata is None:
6355
path = os.path.join(self._path, self.CONF["metadata_path"])
6456
with self._fs.open(path) as f:
6557
self._cache_sample_metadata = pd.read_csv(f, sep="\t", na_values="")
6658
return self._cache_sample_metadata
6759

6860
def _open_variant_calls_zarr(self):
69-
"""Open variant calls zarr.
70-
71-
Returns
72-
-------
73-
root : zarr.hierarchy.Group
74-
Root of zarr containing information on variant calls.
75-
76-
"""
7761
if self._cache_variant_calls_zarr is None:
7862
path = os.path.join(self._path, self.CONF["variant_calls_zarr_path"])
7963
store = _init_zarr_store(fs=self._fs, path=path)
@@ -196,14 +180,6 @@ def variant_calls(self, extended=False, inline_array=True, chunks="native"):
196180
return ds
197181

198182
def open_genome(self):
199-
"""Open the reference genome zarr.
200-
201-
Returns
202-
-------
203-
root : zarr.hierarchy.Group
204-
Zarr hierarchy containing the reference genome sequence.
205-
206-
"""
207183
if self._cache_genome is None:
208184
path = os.path.join(self._path, self.CONF["reference_path"])
209185
store = _init_zarr_store(fs=self._fs, path=path)

0 commit comments

Comments
 (0)