Skip to content

Commit

Permalink
Update input format
Browse files Browse the repository at this point in the history
  • Loading branch information
dglemos committed Nov 26, 2024
1 parent 2437c1b commit 687e8bd
Showing 1 changed file with 81 additions and 75 deletions.
156 changes: 81 additions & 75 deletions gene2phenotype_project/gene2phenotype_app/views/publication.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,13 @@ def post(self, request, stable_id):
serializer_list = LGDPublicationListSerializer(data=request.data)

if serializer_list.is_valid():
publications_data = serializer_list.validated_data.get('publications')
publications_data = serializer_list.validated_data.get('publications') # the pmids are mandatory
phenotypes_data = serializer_list.validated_data.get('phenotypes', None) # optional
variant_types_data = serializer_list.validated_data.get('variant_types', None) # optional
variant_descriptions_data = serializer_list.validated_data.get('variant_descriptions', None) # optional
mechanism_data = serializer_list.validated_data.get('molecular_mechanism', None) # optional
mechanism_synopsis_data = serializer_list.validated_data.get('mechanism_synopsis', None) # optional
mechanism_evidence_data = serializer_list.validated_data.get('mechanism_evidence', None) # optional

for publication in publications_data:
serializer_class = LGDPublicationSerializer(
Expand All @@ -223,82 +229,82 @@ def post(self, request, stable_id):
else:
response = Response({"errors": serializer_class.errors}, status=status.HTTP_400_BAD_REQUEST)

# Add extra data linked to the publication
if "phenotypes" in publication:
# Expected structure:
# { "phenotypes": [{ "accession": "HP:0003974", "publication": 1 }] }
for phenotype in publication["phenotypes"]:
hpo_terms = phenotype['hpo_terms']
for hpo in hpo_terms:
phenotype_data = {
"accession": hpo["accession"],
"publication": phenotype["pmid"]
}
try:
lgd_phenotype_serializer = LGDPhenotypeSerializer(
data = phenotype_data,
context = {'lgd': lgd}
)
# Validate the input data
if lgd_phenotype_serializer.is_valid():
# save() is going to call create()
lgd_phenotype_serializer.save()
else:
response = Response({"errors": lgd_phenotype_serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
except:
accession = phenotype_data["accession"]
return Response(
{"errors": f"Could not insert phenotype '{accession}' for ID '{stable_id}'"},
status=status.HTTP_400_BAD_REQUEST
)

if "variant_types" in publication:
for variant_type in publication["variant_types"]:
LGDVariantTypeSerializer(context={'lgd': lgd, 'user': user}).create(variant_type)

if "variant_descriptions" in publication:
for variant_type_desc in publication["variant_descriptions"]:
LGDVariantTypeDescriptionSerializer(context={'lgd': lgd}).create(variant_type_desc)

# Only mechanism "undetermined" can be updated
# If mechanism has to be updated, call method update_mechanism() and send new mechanism value
# plus the synopsis and the new evidence (if applicable)
# update_mechanism() updates the 'date_review' of the LGD record
if "molecular_mechanism" in publication:
lgd_serializer = LocusGenotypeDiseaseSerializer()
mechanism_obj = lgd.molecular_mechanism

if(mechanism_obj.mechanism.value != "undetermined" or
mechanism_obj.mechanism_support.value != "inferred"):
self.handle_no_update('molecular mechanism', stable_id)

# Build mechanism data
mechanism_data = {
"molecular_mechanism": publication["molecular_mechanism"]
# Add extra data linked to the publication - phenotypes
# Expected structure:
# { "phenotypes": [{ "accession": "HP:0003974", "publication": 1 }] }
for phenotype in phenotypes_data:
hpo_terms = phenotype['hpo_terms']
for hpo in hpo_terms:
phenotype_data = {
"accession": hpo["accession"],
"publication": phenotype["pmid"]
}
# Attach the synopsis to be updated (if applicable)
if "mechanism_synopsis" in publication:
mechanism_data["mechanism_synopsis"] = publication["mechanism_synopsis"]
# Attach the evidence to be updated (if applicable)
if "mechanism_evidence" in publication:
mechanism_data["mechanism_evidence"] = publication["mechanism_evidence"]

try:
lgd_serializer.update_mechanism(lgd, mechanism_data)
except Exception as e:
return self.handle_update_exception(e, "Error while updating molecular mechanism")

# If only the mechanism evidence is going to be updated, call method update_mechanism_evidence()
# update_mechanism_evidence() updates the 'date_review' of the LGD record
# TODO: but before adding evidence we have to check/update the mechanism support to "evidence"
elif "mechanism_evidence" in publication:
lgd_serializer = LocusGenotypeDiseaseSerializer()
try:
lgd_serializer.update_mechanism_evidence(lgd, publication["mechanism_evidence"])
except Exception as e:
return self.handle_update_exception(e, "Error while updating molecular mechanism evidence")

response = Response({'message': 'Publication added to the G2P entry successfully.'}, status=status.HTTP_201_CREATED)
lgd_phenotype_serializer = LGDPhenotypeSerializer(
data = phenotype_data,
context = {'lgd': lgd}
)
# Validate the input data
if lgd_phenotype_serializer.is_valid():
# save() is going to call create()
lgd_phenotype_serializer.save()
else:
response = Response({"errors": lgd_phenotype_serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
except:
accession = phenotype_data["accession"]
return Response(
{"errors": f"Could not insert phenotype '{accession}' for ID '{stable_id}'"},
status=status.HTTP_400_BAD_REQUEST
)

# Add extra data linked to the publication - variant types
for variant_type in variant_types_data:
LGDVariantTypeSerializer(context={'lgd': lgd, 'user': user}).create(variant_type)

# Add extra data linked to the publication - variant descriptions (HGVS)
for variant_type_desc in variant_descriptions_data:
LGDVariantTypeDescriptionSerializer(context={'lgd': lgd}).create(variant_type_desc)

# Only mechanism "undetermined" can be updated
# If mechanism has to be updated, call method update_mechanism() and send new mechanism value
# plus the synopsis and the new evidence (if applicable)
# update_mechanism() updates the 'date_review' of the LGD record
if mechanism_data:
lgd_serializer = LocusGenotypeDiseaseSerializer()
mechanism_obj = lgd.molecular_mechanism

# Check if it's possible to update the mechanism
if(mechanism_obj.mechanism.value != "undetermined" or
mechanism_obj.mechanism_support.value != "inferred"):
self.handle_no_update('molecular mechanism', stable_id)

# Build mechanism data
mechanism_data_input = {
"molecular_mechanism": mechanism_data
}
# Attach the synopsis to be updated (if applicable)
if mechanism_synopsis_data:
mechanism_data_input["mechanism_synopsis"] = mechanism_synopsis_data
# Attach the evidence to be updated (if applicable)
if mechanism_evidence_data:
mechanism_data_input["mechanism_evidence"] = mechanism_evidence_data

try:
lgd_serializer.update_mechanism(lgd, mechanism_data_input)
except Exception as e:
return self.handle_update_exception(e, "Error while updating molecular mechanism")

# If only the mechanism evidence is going to be updated, call method update_mechanism_evidence()
# update_mechanism_evidence() updates the 'date_review' of the LGD record
# TODO: but before adding evidence we have to check/update the mechanism support to "evidence"
elif mechanism_evidence_data:
lgd_serializer = LocusGenotypeDiseaseSerializer()
try:
lgd_serializer.update_mechanism_evidence(lgd, mechanism_evidence_data)
except Exception as e:
return self.handle_update_exception(e, "Error while updating molecular mechanism evidence")

response = Response({'message': 'Publication added to the G2P entry successfully.'}, status=status.HTTP_201_CREATED)

else:
response = Response({"errors": serializer_list.errors}, status=status.HTTP_400_BAD_REQUEST)
Expand Down

0 comments on commit 687e8bd

Please sign in to comment.