gene.py 1.39 KB
Newer Older
1
from django_filters import rest_framework as filters
2
from metagenedb.apps.catalog.models import Function, Gene, Taxonomy
3
4
5


class GeneFilter(filters.FilterSet):
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    has_taxonomy = filters.BooleanFilter(
        field_name='taxonomy', lookup_expr="isnull", exclude=True
    )
    has_functions = filters.BooleanFilter(
        field_name='functions', lookup_expr="isnull", distinct=True, exclude=True
    )
    taxonomy_rank = filters.ChoiceFilter(
        choices=Taxonomy.RANK_CHOICES, field_name='taxonomy__rank'
    )
    name = filters.CharFilter(
        field_name='name', lookup_expr='icontains'
    )
    length__gt = filters.NumberFilter(
        field_name='length', lookup_expr='gt'
    )
    length__lt = filters.NumberFilter(
        field_name='length', lookup_expr='lt'
    )
24
25
    tax_id = filters.CharFilter(
        method='filter_annotated_tax'
26
    )
27
28
29
    source = filters.CharFilter(
        field_name="source__slug"
    )
30
31
32
33
34
    function = filters.ModelMultipleChoiceFilter(
        queryset=Function.objects.all(),
        field_name='functions__function_id',
        to_field_name='function_id'
    )
35

36
37
38
39
40
    def filter_annotated_tax(self, queryset, name, value):
        tax_rank = Taxonomy.objects.get(tax_id=value).rank
        lookup = f"taxonomy__hierarchy__{tax_rank}__tax_id"
        return queryset.filter(**{lookup: value})

41
42
    class Meta:
        model = Gene
43
        fields = ['length', 'name']