Ejemplo n.º 1
0
class MoleculeViewSet(
                   FilterToUserMixIn,
                   mixins.RetrieveModelMixin,
                   # mixins.DestroyModelMixin,
                   GenericViewSet):
    queryset = Molecule.objects.order_by('id')
    serializer_class = MoleculeSerializer
    pagination_class = MoleculePagination
    owner_relation = 'providers__project__owner'

    def get_queryset(self):
        ret = super().get_queryset().distinct()

        if self.action in ('retrieve', 'list') and 'properties' in self.request.query_params:
            for prop in self.request.query_params['properties'].split(','):
                lookup = f"rdkit_prop_{prop}"
                prop_calculator = getattr(djrdkit, prop)
                ret = ret.annotate(**{ lookup: prop_calculator('entity__rdMol')})

        return ret

    properties = openapi.Parameter('properties', openapi.IN_QUERY, description="Attach specified physchem properties to the response. You should be able to use all properties listed here: https://django-rdkit.readthedocs.io/en/latest/functions.html", type=openapi.TYPE_ARRAY, items=openapi.Items(type=openapi.TYPE_STRING))
    @swagger_auto_schema(
        manual_parameters=[properties]
    )
    def retrieve(self, request, *args, **kwargs):
        return super().retrieve(request, *args, **kwargs)

    molset_id_param = openapi.Parameter('activity_set', openapi.IN_QUERY, description="Return only activities that belong to a certain activity set.", type=openapi.TYPE_NUMBER)
    @swagger_auto_schema(
        methods=['GET']
        , responses={200: ActivitySerializer(many=True)}
        , manual_parameters=[molset_id_param]
    )
    @action(detail=True, methods=['get'])
    def activities(self, request, pk=None):
        try:
            molecule = self.get_queryset().get(pk=pk)
        except Molecule.DoesNotExist:
            return Response({"error" : f"No molecule found: {pk}"}, status=status.HTTP_404_NOT_FOUND)
        activities = molecule.activities.filter(source__project__owner=self.request.user).distinct().order_by('id')
        activity_set = self.request.query_params.get('activity_set', None)
        if activity_set is not None:
            activities = activities.filter(source__pk=int(activity_set))
        # paginator = ActivityPagination()
        # page = paginator.paginate_queryset(activities, self.request, view=self)
        # if page is not None:
        #     serializer = ActivitySerializer(page, many=True)
        #     return paginator.get_paginated_response(serializer.data)
        # else:
        #     return Response({"error" : "You need to specify a valid page number."}, status=status.HTTP_400_BAD_REQUEST)
        return Response(ActivitySerializer(activities, many=True).data, status=status.HTTP_200_OK)
Ejemplo n.º 2
0
 def activities(self, request, pk=None):
     try:
         molecule = self.get_queryset().get(pk=pk)
     except Molecule.DoesNotExist:
         return Response({"error" : f"No molecule found: {pk}"}, status=status.HTTP_404_NOT_FOUND)
     activities = molecule.activities.filter(source__project__owner=self.request.user).distinct().order_by('id')
     activity_set = self.request.query_params.get('activity_set', None)
     if activity_set is not None:
         activities = activities.filter(source__pk=int(activity_set))
     # paginator = ActivityPagination()
     # page = paginator.paginate_queryset(activities, self.request, view=self)
     # if page is not None:
     #     serializer = ActivitySerializer(page, many=True)
     #     return paginator.get_paginated_response(serializer.data)
     # else:
     #     return Response({"error" : "You need to specify a valid page number."}, status=status.HTTP_400_BAD_REQUEST)
     return Response(ActivitySerializer(activities, many=True).data, status=status.HTTP_200_OK)
Ejemplo n.º 3
0
    def activities(self, request, pk=None):
        try:
            activity_set = self.get_queryset().get(pk=pk)
        except ActivitySet.DoesNotExist:
            return Response({"error" : f"No such set: {pk}"}, status=status.HTTP_404_NOT_FOUND)
        activities = Activity.objects.filter(source=activity_set)

        mols = self.request.query_params.get('mols', [])
        if mols:
            mols = mols.split(',')
            activities = activities.filter(molecule__in=mols)

        activities =  activities.order_by('id')
        paginator = ActivityPagination()
        page = paginator.paginate_queryset(activities, self.request, view=self)
        if page is not None:
            serializer = ActivitySerializer(page, many=True)
            return paginator.get_paginated_response(serializer.data)
        else:
            return Response({"error" : "You need to specify a valid page number."}, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 4
0
class ActivitySetViewSet(FilterToProjectMixIn, FilterToUserMixIn,
                         mixins.ListModelMixin, mixins.RetrieveModelMixin,
                         mixins.DestroyModelMixin, viewsets.GenericViewSet):
    queryset = ActivitySet.objects.order_by('-created')
    serializer_class = ActivitySetSerializer
    owner_relation = "project__owner"

    project_id_param = openapi.Parameter(
        'project_id',
        openapi.IN_QUERY,
        description="Return activity sets related to just this project.",
        type=openapi.TYPE_NUMBER)

    @swagger_auto_schema(
        operation_description=
        "List all activity sets. Can give a project ID to filter on."
        # , methods=['GET']
        ,
        manual_parameters=[project_id_param],
        responses={200: ActivitySetSerializer(many=True)})
    def list(self, request, *args, **kwargs):
        return super().list(request, *args, **kwargs)

    mols_param = openapi.Parameter(
        'mols',
        openapi.IN_QUERY,
        description="Only return activities for the given molecule IDs.",
        type=openapi.TYPE_ARRAY,
        items=openapi.Items(type=openapi.TYPE_INTEGER))

    @swagger_auto_schema(methods=['GET'],
                         manual_parameters=[mols_param],
                         responses={200: ActivitySerializer(many=True)})
    @action(detail=True, methods=['get'])
    def activities(self, request, pk=None):
        try:
            activity_set = self.get_queryset().get(pk=pk)
        except ActivitySet.DoesNotExist:
            return Response({"error": f"No such set: {pk}"},
                            status=status.HTTP_404_NOT_FOUND)
        activities = Activity.objects.filter(source=activity_set)

        mols = self.request.query_params.get('mols', [])
        if mols:
            mols = mols.split(',')
            activities = activities.filter(molecule__in=mols)

        activities = activities.order_by('id')
        paginator = ActivityPagination()
        page = paginator.paginate_queryset(activities, self.request, view=self)
        if page is not None:
            serializer = ActivitySerializer(page, many=True)
            return paginator.get_paginated_response(serializer.data)
        else:
            return Response(
                {"error": "You need to specify a valid page number."},
                status=status.HTTP_400_BAD_REQUEST)

    @swagger_auto_schema(
        methods=['GET'],
        responses={200: ActivitySetSummarySerializer(many=False)})
    @action(detail=True, methods=['get'])
    def summary(self, request, pk):
        try:
            activity_set = self.get_queryset().get(pk=pk)
        except ActivitySet.DoesNotExist:
            return Response({"error": f"No such set: {pk}"},
                            status=status.HTTP_404_NOT_FOUND)

        summary = activity_set.getSummary()
        serializer = ActivitySetSummarySerializer(summary)
        data = serializer.data
        return Response(data, status=status.HTTP_200_OK)