Skip to content

Commit 00953ec

Browse files
authored
Merge pull request #404 from VariantEffect/feature/bencap/additional-statistics-endpoints
Additional Statistics Endpoints
2 parents 5bbd315 + 4deebf0 commit 00953ec

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/mavedb/routers/score_sets.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,37 @@ def search_score_sets(
138138
return fetch_superseding_score_set_in_search_result(score_sets, user_data, search)
139139

140140

141+
@router.get("/score-sets/mapped-genes", status_code=200, response_model=dict[str, list[str]])
142+
def score_set_mapped_gene_mapping(
143+
db: Session = Depends(deps.get_db), user_data: UserData = Depends(get_current_user)
144+
) -> Any:
145+
"""
146+
Get a mapping of score set URNs to mapped gene symbols.
147+
"""
148+
save_to_logging_context({"requested_resource": "mapped-genes"})
149+
150+
score_sets_with_mapping_metadata = db.execute(
151+
select(ScoreSet, TargetGene.post_mapped_metadata)
152+
.join(ScoreSet)
153+
.where(TargetGene.post_mapped_metadata.is_not(None))
154+
).all()
155+
156+
mapped_genes: dict[str, list[str]] = {}
157+
for score_set_item, post_mapped_metadata in score_sets_with_mapping_metadata:
158+
if not has_permission(user_data, score_set_item, Action.READ).permitted:
159+
continue
160+
161+
sequence_genes = [
162+
*post_mapped_metadata.get("genomic", {}).get("sequence_genes", []),
163+
*post_mapped_metadata.get("protein", {}).get("sequence_genes", []),
164+
]
165+
166+
if sequence_genes:
167+
mapped_genes.setdefault(score_set_item.urn, []).extend(sequence_genes)
168+
169+
return mapped_genes
170+
171+
141172
@router.post(
142173
"/me/score-sets/search",
143174
status_code=200,

src/mavedb/routers/statistics.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,38 @@ def record_counts(model: RecordNames, group: Optional[GroupBy] = None, db: Sessi
277277
return OrderedDict(sorted(grouped.items()))
278278

279279

280+
@router.get("/record/score-set/variant/count", status_code=200, response_model=dict[str, int])
281+
def record_variant_counts(db: Session = Depends(get_db)) -> dict[str, int]:
282+
"""
283+
Returns a dictionary of counts for the number of published and distinct variants in the database contained
284+
within a given record.
285+
"""
286+
variants = db.execute(
287+
select(PublishedVariantsMV.score_set_urn, func.count(PublishedVariantsMV.variant_id))
288+
.group_by(PublishedVariantsMV.score_set_urn)
289+
.order_by(PublishedVariantsMV.score_set_urn)
290+
).all()
291+
292+
grouped = {urn: sum(c for _, c in g) for urn, g in itertools.groupby(variants, lambda t: t[0])}
293+
return OrderedDict(sorted(filter(lambda item: item[1] > 0, grouped.items())))
294+
295+
296+
@router.get("/record/score-set/mapped-variant/count", status_code=200, response_model=dict[str, int])
297+
def record_mapped_variant_counts(db: Session = Depends(get_db)) -> dict[str, int]:
298+
"""
299+
Returns a dictionary of counts for the number of published and distinct mapped variants in the database contained
300+
within a given record.
301+
"""
302+
variants = db.execute(
303+
select(PublishedVariantsMV.score_set_urn, func.count(PublishedVariantsMV.mapped_variant_id))
304+
.group_by(PublishedVariantsMV.score_set_urn)
305+
.order_by(PublishedVariantsMV.score_set_urn)
306+
).all()
307+
308+
grouped = {urn: sum(c for _, c in g) for urn, g in itertools.groupby(variants, lambda t: t[0])}
309+
return OrderedDict(sorted(filter(lambda item: item[1] > 0, grouped.items())))
310+
311+
280312
########################################################################################
281313
# Target statistics
282314
########################################################################################

0 commit comments

Comments
 (0)