Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add endpoints to support bulk updates of disease #91

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions gene2phenotype_project/gene2phenotype_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ def perform_create(self, serializer):
path('lgd/<str:stable_id>/comment/', views.LGDEditComment.as_view(), name="lgd_comment"),
# Delete LGD record. Action: UPDATE
path('lgd/<str:stable_id>/delete/', views.LocusGenotypeDiseaseDelete.as_view(), name="lgd_delete"),
# Update disease IDs for LGD records. Action: POST
path('lgd_disease_updates/', views.LGDUpdateDisease.as_view(), name="lgd_disease_updates"),

### Endpoints to update other data ###
path('update/diseases/', views.UpdateDisease.as_view(), name="update_diseases"),

### Curation endpoints ###
path('add/curation/', views.AddCurationData.as_view(), name="add_curation_data"),
Expand Down
3 changes: 2 additions & 1 deletion gene2phenotype_project/gene2phenotype_app/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

from .locus import LocusGene, LocusGeneSummary, GeneFunction

from .disease import GeneDiseaseView, DiseaseDetail, DiseaseSummary, AddDisease
from .disease import (GeneDiseaseView, DiseaseDetail, DiseaseSummary, AddDisease,
UpdateDisease, LGDUpdateDisease)

from .curation import (AddCurationData, ListCurationEntries, CurationDataDetail,
UpdateCurationData, PublishRecord, DeleteCurationData)
Expand Down
108 changes: 104 additions & 4 deletions gene2phenotype_project/gene2phenotype_app/views/disease.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from rest_framework import permissions
from rest_framework import permissions, status
from rest_framework.response import Response
from django.db.models import Q
from django.shortcuts import get_object_or_404

from gene2phenotype_app.serializers import (GeneDiseaseSerializer,
DiseaseDetailSerializer,
CreateDiseaseSerializer)

from gene2phenotype_app.models import (AttribType, Locus, OntologyTerm,
DiseaseOntologyTerm, Disease,
LocusAttrib, GeneDisease)
LocusAttrib, GeneDisease, LocusGenotypeDisease)

from ..utils import clean_omim_disease
from .base import BaseView, BaseAdd
from .base import BaseView, BaseAdd, IsSuperUser

class GeneDiseaseView(BaseView):
"""
Expand Down Expand Up @@ -147,4 +148,103 @@ def list(self, request, *args, **kwargs):
"""
class AddDisease(BaseAdd):
serializer_class = CreateDiseaseSerializer
permission_classes = [permissions.IsAuthenticated]
permission_classes = [IsSuperUser]

### Update data
class UpdateDisease(BaseAdd):
http_method_names = ['post', 'options']
permission_classes = [IsSuperUser]

def post(self, request):
diseases = request.data # list of diseases {'id': ..., 'name': ...}

if not isinstance(diseases, list):
return Response({"error": "Request should be a list"}, status=status.HTTP_400_BAD_REQUEST)

updated_diseases = []
errors = []

for disease_data in diseases:
disease_id = disease_data.get("id")
new_name = disease_data.get("name")

if not disease_id or not new_name:
errors.append({"error": "Both 'id' and 'name' are required."})
continue

# Fetch disease or return 404 if not found
disease = get_object_or_404(Disease, id=disease_id)

# Ensure the new name is unique
check_disease = Disease.objects.filter(name=new_name).exclude(id=disease_id)
if check_disease:
errors.append({
"id": disease_id,
"name": new_name,
"existing_id": check_disease[0].id,
"error": f"A disease with the name '{new_name}' already exists."
})
continue

# Update and save
disease.name = new_name
disease.save()
updated_diseases.append({"id": disease_id, "name": new_name})
Comment on lines +179 to +192
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
check_disease = Disease.objects.filter(name=new_name).exclude(id=disease_id)
if check_disease:
errors.append({
"id": disease_id,
"name": new_name,
"existing_id": check_disease[0].id,
"error": f"A disease with the name '{new_name}' already exists."
})
continue
# Update and save
disease.name = new_name
disease.save()
updated_diseases.append({"id": disease_id, "name": new_name})
check_disease = Disease.objects.filter(name=new_name).exclude(id=disease_id)
if check_disease:
# If new disease name already exists then flag error
errors.append({
"id": disease_id,
"name": new_name,
"existing_id": check_disease[0].id,
"error": f"A disease with the name '{new_name}' already exists."
})
else:
# Else update disease name and save
disease.name = new_name
disease.save()
updated_diseases.append({"id": disease_id, "name": new_name})


response_data = {}
if updated_diseases:
response_data["updated"] = updated_diseases

if errors:
response_data["errors"] = errors

return Response(response_data, status=status.HTTP_200_OK if updated_diseases else status.HTTP_400_BAD_REQUEST)

class LGDUpdateDisease(BaseAdd):
http_method_names = ['post', 'options']
permission_classes = [IsSuperUser]

def post(self, request):
data_to_update = request.data

if not isinstance(data_to_update, list):
return Response({"error": "Request should be a list"}, status=status.HTTP_400_BAD_REQUEST)

updated_records = []
errors = []

for disease_to_update in data_to_update:
current_disease_id = disease_to_update.get("disease_id")
new_disease_id = disease_to_update.get("new_disease_id")

if not current_disease_id or not new_disease_id:
errors.append({"error": "Both 'disease_id' and 'new_disease_id' are required."})
continue

# Get records that use the disease id
lgd_list = LocusGenotypeDisease.objects.filter(disease_id=current_disease_id, is_deleted=0)

for lgd_obj in lgd_list:
# Check if there is another LGD record linked to the new disease id
try:
existing_lgd_obj = LocusGenotypeDisease.objects.get(
locus_id = lgd_obj.locus_id,
disease_id = new_disease_id,
genotype_id = lgd_obj.genotype_id,
mechanism_id = lgd_obj.mechanism_id
)
errors.append({"disease_id": current_disease_id, "error": f"Found a different record with same locus, genotype, disease and mechanism: '{existing_lgd_obj.stable_id.stable_id}'"})
except:
# Update record with new disease id
lgd_obj.disease_id = new_disease_id
lgd_obj.save()
updated_records.append({"g2p_id": lgd_obj.stable_id.stable_id, "lgd_id": lgd_obj.id})

response_data = {}
if updated_records:
response_data["Updated records"] = updated_records

if errors:
response_data["Errors"] = errors

return Response(response_data, status=status.HTTP_200_OK if updated_records else status.HTTP_400_BAD_REQUEST)