Skip to content

Commit 1b17ec2

Browse files
authored
Merge branch 'master' into GH919-fix-cnv-variant-query
2 parents 6fe09d4 + 6d711b7 commit 1b17ec2

7 files changed

Lines changed: 335 additions & 72 deletions

File tree

malariagen_data/anoph/describe.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import inspect
2+
from typing import Optional
3+
4+
import pandas as pd
5+
from numpydoc_decorator import doc # type: ignore
6+
7+
from .base import AnophelesBase
8+
9+
10+
class AnophelesDescribe(AnophelesBase):
11+
"""Mixin class providing API introspection and discovery functionality."""
12+
13+
@doc(
14+
summary="""
15+
List all available public API methods with their descriptions.
16+
""",
17+
returns="""
18+
A dataframe with one row per public method, containing the method
19+
name, a short summary description, and its category (data access,
20+
analysis, or plotting).
21+
""",
22+
parameters=dict(
23+
category="""
24+
Optional filter to show only methods of a given category.
25+
Supported values are "data", "analysis", "plot", or None to
26+
show all methods.
27+
""",
28+
),
29+
)
30+
def describe_api(
31+
self,
32+
category: Optional[str] = None,
33+
) -> pd.DataFrame:
34+
methods_info = []
35+
36+
# Walk through all public methods on this instance.
37+
for name in sorted(dir(self)):
38+
# Skip private/dunder methods.
39+
if name.startswith("_"):
40+
continue
41+
42+
attr = getattr(type(self), name, None)
43+
if attr is None:
44+
continue
45+
46+
# Only include callable methods and non-property attributes.
47+
if isinstance(attr, property):
48+
continue
49+
if not callable(attr):
50+
continue
51+
52+
# Extract the docstring summary.
53+
summary = self._extract_summary(attr)
54+
55+
# Determine category.
56+
method_category = self._categorize_method(name)
57+
58+
methods_info.append(
59+
{
60+
"method": name,
61+
"summary": summary,
62+
"category": method_category,
63+
}
64+
)
65+
66+
df = pd.DataFrame(methods_info)
67+
68+
# Apply category filter if specified.
69+
if category is not None:
70+
valid_categories = {"data", "analysis", "plot"}
71+
if category not in valid_categories:
72+
raise ValueError(
73+
f"Invalid category: {category!r}. "
74+
f"Must be one of {valid_categories}."
75+
)
76+
df = df[df["category"] == category].reset_index(drop=True)
77+
78+
return df
79+
80+
@staticmethod
81+
def _extract_summary(method) -> str:
82+
"""Extract the first line of the docstring as a summary."""
83+
docstring = inspect.getdoc(method)
84+
if not docstring:
85+
return ""
86+
# Take the first non-empty line as the summary.
87+
for line in docstring.strip().splitlines():
88+
line = line.strip()
89+
if line:
90+
return line
91+
return ""
92+
93+
@staticmethod
94+
def _categorize_method(name: str) -> str:
95+
"""Categorize a method based on its name."""
96+
if name.startswith("plot_"):
97+
return "plot"
98+
data_prefixes = (
99+
"sample_",
100+
"snp_",
101+
"hap_",
102+
"cnv_",
103+
"genome_",
104+
"open_",
105+
"lookup_",
106+
"read_",
107+
"general_",
108+
"sequence_",
109+
"cohorts_",
110+
"aim_",
111+
"gene_",
112+
)
113+
if name.startswith(data_prefixes):
114+
return "data"
115+
return "analysis"

malariagen_data/anoph/fst.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def plot_fst_gwss_track(
235235
height=height,
236236
toolbar_location="above",
237237
x_range=x_range,
238-
y_range=(0, 1),
238+
y_range=(clip_min, 1),
239239
output_backend=output_backend,
240240
)
241241

@@ -252,7 +252,7 @@ def plot_fst_gwss_track(
252252

253253
# tidy up the plot
254254
fig.yaxis.axis_label = "Fst"
255-
fig.yaxis.ticker = [0, 1]
255+
fig.yaxis.ticker = sorted(set([clip_min, 0, 1]))
256256
self._bokeh_style_genome_xaxis(fig, contig)
257257

258258
if show: # pragma: no cover

malariagen_data/anopheles.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from .anoph.phenotypes import AnophelesPhenotypeData
4545
from .mjn import _median_joining_network, _mjn_graph
4646
from .anoph.hapclust import AnophelesHapClustAnalysis
47+
from .anoph.describe import AnophelesDescribe
4748
from .anoph.dipclust import AnophelesDipClustAnalysis
4849
from .util import (
4950
CacheMiss,
@@ -95,6 +96,7 @@ class AnophelesDataResource(
9596
AnophelesSampleMetadata,
9697
AnophelesGenomeFeaturesData,
9798
AnophelesGenomeSequenceData,
99+
AnophelesDescribe,
98100
AnophelesBase,
99101
AnophelesPhenotypeData,
100102
):

poetry.lock

Lines changed: 44 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ numpy = "==1.26.4"
2020
numba = ">=0.60.0"
2121
llvmlite = "*"
2222
scipy = "*"
23-
pandas = "*"
23+
pandas = "2.3.2"
2424
zarr = ">=2.18.3, <3.0.0"
2525
numcodecs = ">=0.13, <0.16"
2626
dask = {version="*", extras=["array"]}

0 commit comments

Comments
 (0)