Skip to content
Snippets Groups Projects
Commit 51a93fef authored by AndiMajore's avatar AndiMajore
Browse files

try to fix tissue expression route

parent 2aa76c6b
No related branches found
No related tags found
No related merge requests found
Pipeline #63010 canceled
......@@ -19,7 +19,7 @@ from django.urls import path
from drugstone.views import map_nodes, tasks_view, result_view, \
graph_export, TissueView, query_tissue_proteins, TaskView, \
adjacent_drugs, adjacent_disorders, fetch_edges, create_network, load_network, get_license, get_datasets, \
get_max_tissue_expression, get_tissue_expression
get_max_tissue_expression
# cache time is 6 hours
urlpatterns = [
......@@ -33,7 +33,7 @@ urlpatterns = [
path('query_tissue_proteins/', query_tissue_proteins),
path('adjacent_drugs/', adjacent_drugs),
path('adjacent_disorders/', adjacent_disorders),
path('tissue_expression/', get_tissue_expression),
path('tissue_expression/', TissueExpressionView.as_view()),
path('tissue_max_expression/', get_max_tissue_expression),
path('tissues/', TissueView.as_view()),
path('admin/', admin.site.urls),
......
......@@ -653,49 +653,56 @@ class TissueView(APIView):
return Response(TissueSerializer(many=True).to_representation(tissues))
@api_view(['POST', 'GET'])
def get_tissue_expression(request) -> Response:
tissue = Tissue.objects.get(id=request.query_params.get('tissue'))
class TissueExpressionView(APIView):
"""
Expression of host proteins in tissues.
"""
if request.query_params.get('proteins'):
ids = json.loads(request.query_params.get('proteins'))
proteins = list(Protein.objects.filter(id__in=ids).all())
elif request.query_params.get('token'):
proteins = []
task = Task.objects.get(token=request.query_params['token'])
result = task_result(task)
network = result['network']
node_attributes = result.get('node_attributes')
if not node_attributes:
node_attributes = {}
node_types = node_attributes.get('node_types')
if not node_types:
node_types = {}
parameters = json.loads(task.parameters)
seeds = parameters['seeds']
nodes = network['nodes']
for node in nodes + seeds:
node_type = node_types.get(node)
details = None
if node_type == 'protein':
if details:
proteins.append(details)
else:
try:
prot = Protein.objects.get(uniprot_code=node)
if prot not in proteins:
proteins.append(Protein.objects.get(uniprot_code=node))
except Protein.DoesNotExist:
pass
pt_expressions = {}
for protein in proteins:
try:
expression_level = ExpressionLevel.objects.get(protein=protein, tissue=tissue)
pt_expressions[
ProteinSerializer().to_representation(protein)['drugstone_id']] = expression_level.expression_level
except ExpressionLevel.DoesNotExist:
pt_expressions[ProteinSerializer().to_representation(protein)['drugstone_id']] = None
def get(self, request) -> Response:
return self.post(request)
def post(self, request) -> Response:
tissue = Tissue.objects.get(id=request.data.get('tissue'))
if request.data.get('proteins'):
ids = json.loads(request.data.get('proteins'))
proteins = list(Protein.objects.filter(id__in=ids).all())
elif request.data.get('token'):
proteins = []
task = Task.objects.get(token=request.data['token'])
result = task_result(task)
network = result['network']
node_attributes = result.get('node_attributes')
if not node_attributes:
node_attributes = {}
node_types = node_attributes.get('node_types')
if not node_types:
node_types = {}
parameters = json.loads(task.parameters)
seeds = parameters['seeds']
nodes = network['nodes']
for node in nodes + seeds:
node_type = node_types.get(node)
details = None
if node_type == 'protein':
if details:
proteins.append(details)
else:
try:
prot = Protein.objects.get(uniprot_code=node)
if prot not in proteins:
proteins.append(Protein.objects.get(uniprot_code=node))
except Protein.DoesNotExist:
pass
pt_expressions = {}
for protein in proteins:
try:
expression_level = ExpressionLevel.objects.get(protein=protein, tissue=tissue)
pt_expressions[
ProteinSerializer().to_representation(protein)['drugstone_id']] = expression_level.expression_level
except ExpressionLevel.DoesNotExist:
pt_expressions[ProteinSerializer().to_representation(protein)['drugstone_id']] = None
return Response(pt_expressions)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment