from datasets_api.permissions import get_instance_timestamp_etag
from django.utils.decorators import method_decorator
from django.views.decorators.http import etag
from query_base.query_base import QueryBaseView
from rest_framework import status
from rest_framework.request import Request
from rest_framework.response import Response
from dae.genomic_resources.histogram import NumberHistogram
[docs]
class GenomicScoresView(QueryBaseView):
"""View for genomic scores database for the instance."""
[docs]
@method_decorator(etag(get_instance_timestamp_etag))
def get(self, _request: Request) -> Response:
"""List all genomic scores used by the GPF instance."""
res = []
registry = self.gpf_instance.genomic_scores
for score_id, score in registry.get_scores():
if isinstance(score.hist, NumberHistogram):
res.append({
"score": score_id,
"name": score.name,
"desc": score.description,
"bars": score.hist.bars,
"bins": score.hist.bins,
"xscale":
"log" if score.hist.config.x_log_scale else "linear",
"yscale":
"log" if score.hist.config.y_log_scale else "linear",
"range": score.hist.view_range,
"help": score.help,
"small_values_desc": score.small_values_desc,
"large_values_desc": score.large_values_desc,
})
else:
res.append({
"score": score_id,
"name": score.name,
"desc": score.name,
"bars": None,
"bins": None,
"xscale": None,
"yscale": None,
"range": None,
"help": score.description,
"small_values_desc": None,
"large_values_desc": None,
})
return Response(res)
[docs]
class GenomicScoreDescsView(QueryBaseView):
"""View for accessing inner genomic scores data from federations."""
[docs]
@method_decorator(etag(get_instance_timestamp_etag))
def get(
self, _request: Request, score_id: str | None = None,
) -> Response:
"""Convert all genomic score descs into a JSON list."""
registry = self.gpf_instance.genomic_scores
res = []
if score_id is not None:
if score_id not in registry:
return Response(status=status.HTTP_404_NOT_FOUND)
scores = [
(score_id, registry[score_id]),
]
else:
scores = registry.get_scores()
for _, score in scores:
res.append(score.to_json())
return Response(res)