Skip to content

Commit

Permalink
Merge pull request #65 from EBI-Metagenomics/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
atarkowska authored Jul 3, 2018
2 parents 7613ded + de5c6ac commit e56cf94
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 44 deletions.
26 changes: 24 additions & 2 deletions emgapi/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ def filter_include(self, qs, name, value):
class Meta:
model = emg_models.Sample
fields = (
'accession',
'experiment_type',
'biome_name',
'lineage',
Expand All @@ -448,6 +449,21 @@ class Meta:

class RunFilter(django_filters.FilterSet):

accession = filters.ModelMultipleChoiceFilter(
queryset=emg_models.Run.objects,
to_field_name='accession',
method='filter_accession',
distinct=True,
label='Run accession',
help_text='Run accession',
widget=QueryArrayWidget
)

def filter_accession(self, qs, name, values):
if values:
qs = qs.available(self.request).filter(accession__in=values)
return qs

experiment_type = filters.ModelMultipleChoiceFilter(
queryset=emg_models.ExperimentType.objects,
to_field_name='experiment_type',
Expand Down Expand Up @@ -580,13 +596,19 @@ def filter_include(self, qs, name, value):
class Meta:
model = emg_models.Run
fields = (
'accession',
'experiment_type',
'biome_name',
'lineage',
'experiment_type',
'species',
'instrument_platform',
'instrument_model',
'species',
'metadata_key',
'metadata_value_gte',
'metadata_value_lte',
'metadata_value',
'sample_accession',
'study_accession',
'include',
)

Expand Down
69 changes: 30 additions & 39 deletions emgapi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
from __future__ import unicode_literals

from django.db import models
from django.db.models import Sum, Count, Case, When
from django.db.models import CharField, IntegerField, Value
from django.db.models import Count
from django.db.models import CharField, Value
from django.db.models.functions import Concat, Cast
from django.db.models import Q
from django.db.models import Prefetch
Expand Down Expand Up @@ -106,7 +106,8 @@ def available(self, request=None):
_instance = _query_filters[self.__class__.__name__]
if isinstance(self, self.__class__):
if request is not None and request.user.is_authenticated():
q.extend(_instance['authenticated'])
if not request.user.is_superuser:
q.extend(_instance['authenticated'])
else:
q.extend(_instance['all'])
return self.distinct().filter(*q)
Expand Down Expand Up @@ -156,12 +157,20 @@ class PipelineManager(models.Manager):

def get_queryset(self):
return PipelineQuerySet(self.model, using=self._db) \
.annotate(analyses_count=Count('analyses', distinct=True)) \
.annotate(samples_count=Sum(Case(
When(analyses__sample__is_public=1, then=1),
default=0,
output_field=IntegerField()
)))
.annotate(
analyses_count=Count(
'analyses', filter=(
Q(analyses__analysis_status_id=3) |
Q(analyses__analysis_status_id=6)
), distinct=True)
) \
.annotate(
samples_count=Count(
'analyses__sample', filter=(
Q(analyses__analysis_status_id=3) |
Q(analyses__analysis_status_id=6)
), distinct=True)
)


class Pipeline(models.Model):
Expand Down Expand Up @@ -231,11 +240,7 @@ class BiomeManager(models.Manager):

def get_queryset(self):
return BiomeQuerySet(self.model, using=self._db) \
.annotate(samples_count=Sum(Case(
When(samples__is_public=1, then=1),
default=0,
output_field=IntegerField()
)))
.annotate(samples_count=Count('samples', distinct=True))


class Biome(models.Model):
Expand Down Expand Up @@ -275,16 +280,8 @@ class PublicationManager(models.Manager):

def get_queryset(self):
return PublicationQuerySet(self.model, using=self._db) \
.annotate(studies_count=Sum(Case(
When(studies__is_public=1, then=1),
default=0,
output_field=IntegerField()
))) \
.annotate(samples_count=Sum(Case(
When(studies__samples__is_public=1, then=1),
default=0,
output_field=IntegerField()
)))
.annotate(studies_count=Count('studies', distinct=True)) \
.annotate(samples_count=Count('studies__samples', distinct=True))


class Publication(models.Model):
Expand Down Expand Up @@ -520,11 +517,7 @@ def get_queryset(self):
# TODO: remove biome when schema updated
return StudyQuerySet(self.model, using=self._db) \
.defer('biome') \
.annotate(samples_count=Sum(Case(
When(samples__is_public=1, then=1),
default=0,
output_field=IntegerField()
)))
.annotate(samples_count=Count('samples'))

def available(self, request):
return self.get_queryset().available(request)
Expand Down Expand Up @@ -799,16 +792,14 @@ class ExperimentTypeManager(models.Manager):

def get_queryset(self):
return ExperimentTypeQuerySet(self.model, using=self._db) \
.annotate(runs_count=Sum(Case(
When(runs__status_id=4, then=1),
default=0,
output_field=IntegerField()
))) \
.annotate(samples_count=Sum(Case(
When(runs__sample__is_public=1, then=1),
default=0,
output_field=IntegerField()
)))
.annotate(
samples_count=Count(
'runs__sample', filter=Q(runs__status_id=4), distinct=True)
) \
.annotate(
runs_count=Count(
'runs', filter=Q(runs__status_id=4), distinct=True)
)


class ExperimentType(models.Model):
Expand Down
2 changes: 1 addition & 1 deletion emgapi/views_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def get_queryset(self):
release_version=self.kwargs[self.lookup_field])
queryset = emg_models.Sample.objects \
.available(self.request, prefetch=True) \
.filter(analysis__pipeline=pipeline)
.filter(analyses__pipeline=pipeline)
if 'runs' in self.request.GET.get('include', '').split(','):
_qs = emg_models.Run.objects.available(self.request)
queryset = queryset.prefetch_related(
Expand Down
2 changes: 1 addition & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# run pip install -r requirements-test

pytest
pytest-django
pytest-django~=3.2.1

model_mommy
mock_services
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
_requirements = os.path.join(_base, 'requirements.txt')
_requirements_test = os.path.join(_base, 'requirements-test.txt')

version = "1.3.0"
version = "1.3.1"

install_requirements = []
with open(_requirements) as f:
Expand Down

0 comments on commit e56cf94

Please sign in to comment.