Commit 317bb154 authored by Kenzo-Hugo Hillion's avatar Kenzo-Hugo Hillion
Browse files

allow retrieving fasta file from gene list

parent 74c3f349
Pipeline #32449 passed with stages
in 3 minutes and 25 seconds
......@@ -14,3 +14,4 @@ class GeneQueryParams(PaginatedQueryParams):
tax_id = fields.Integer()
function = fields.String()
source = fields.String()
fasta = fields.Boolean()
from io import StringIO
from django.http import HttpResponse
from drf_yasg.utils import swagger_auto_schema
from marshmallow.exceptions import ValidationError
from rest_framework.response import Response
from rest_framework.status import HTTP_422_UNPROCESSABLE_ENTITY
from metagenedb.apps.catalog.models import Gene
from metagenedb.api.catalog.filters import GeneFilter
......@@ -18,8 +24,26 @@ class GeneViewSet(BulkViewSet):
@swagger_auto_schema(
tags=['Genes'],
)
def list(self, *args, **kwargs):
return super().list(*args, **kwargs)
def list(self, request, *args, **kwargs):
try:
query_params = self.query_params_parser().load(request.query_params)
except ValidationError as validation_error:
error_message = validation_error.normalized_messages()
error_message.update({
'allowed_query_params': ', '.join(self.query_params_parser().declared_fields.keys())
})
return Response(error_message, status=HTTP_422_UNPROCESSABLE_ENTITY)
if query_params.get('fasta', False) is True:
queryset = self.filter_queryset(self.get_queryset())
fasta_file = StringIO()
for gene in queryset.iterator():
fasta_file.write(gene.fasta)
# generate the file
response = HttpResponse(fasta_file.getvalue(), content_type='text/fasta')
filename = 'metagenedb_sequences.fasta'
response['Content-Disposition'] = 'attachment; filename=%s' % filename
return response
return super().list(request, *args, **kwargs)
@swagger_auto_schema(
tags=['Genes'],
......
......@@ -28,6 +28,10 @@ class Gene(models.Model):
def __str__(self):
return self.gene_id
@property
def fasta(self):
return f">{self.gene_id}\n{self.sequence}\n"
class Meta:
ordering = ['-gene_id']
......
......@@ -21,7 +21,7 @@ services:
postgresql:
container_name: postgresql
image: postgres:11.4-alpine
shm_size: '2gb'
shm_size: '8gb'
ports:
- "5433:5432"
volumes:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment