Skip to content

Commit e718cb4

Browse files
committed
Use get() to retrieve values from mapping reponse
1 parent 34ef722 commit e718cb4

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

src/mavedb/worker/jobs.py

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,10 @@ async def map_variants_for_score_set(
340340

341341
try:
342342
if mapping_results:
343-
if not mapping_results["mapped_scores"]:
343+
if not mapping_results.get("mapped_scores"):
344344
# if there are no mapped scores, the score set failed to map.
345345
score_set.mapping_state = MappingState.failed
346-
score_set.mapping_errors = {"error_message": mapping_results["error_message"]}
346+
score_set.mapping_errors = {"error_message": mapping_results.get("error_message")}
347347
else:
348348
# TODO(VariantEffect/dcd-mapping2#2) after adding multi target mapping support:
349349
# this assumes single-target mapping, will need to be changed to support multi-target mapping
@@ -352,9 +352,9 @@ async def map_variants_for_score_set(
352352
# TODO(VariantEffect/dcd-mapping2#3) after adding accession-based score set mapping support:
353353
# this also assumes that the score set is based on a target sequence, not a target accession
354354

355-
if mapping_results["computed_genomic_reference_sequence"]:
355+
if mapping_results.get("computed_genomic_reference_sequence"):
356356
target_sequence = mapping_results["computed_genomic_reference_sequence"]["sequence"]
357-
elif mapping_results["computed_protein_reference_sequence"]:
357+
elif mapping_results.get("computed_protein_reference_sequence"):
358358
target_sequence = mapping_results["computed_protein_reference_sequence"]["sequence"]
359359
else:
360360
raise NonexistentMappingReferenceError()
@@ -371,10 +371,10 @@ async def map_variants_for_score_set(
371371

372372
excluded_pre_mapped_keys = {"sequence"}
373373
if (
374-
mapping_results["computed_genomic_reference_sequence"]
375-
and mapping_results["mapped_genomic_reference_sequence"]
374+
mapping_results.get("computed_genomic_reference_sequence")
375+
and mapping_results.get("mapped_genomic_reference_sequence")
376376
):
377-
pre_mapped_metadata = mapping_results["computed_genomic_reference_sequence"]
377+
pre_mapped_metadata = mapping_results.get("computed_genomic_reference_sequence")
378378
target_gene.pre_mapped_metadata = cast(
379379
{
380380
"genomic": {
@@ -385,13 +385,13 @@ async def map_variants_for_score_set(
385385
JSONB,
386386
)
387387
target_gene.post_mapped_metadata = cast(
388-
{"genomic": mapping_results["mapped_genomic_reference_sequence"]}, JSONB
388+
{"genomic": mapping_results.get("mapped_genomic_reference_sequence")}, JSONB
389389
)
390390
elif (
391-
mapping_results["computed_protein_reference_sequence"]
392-
and mapping_results["mapped_protein_reference_sequence"]
391+
mapping_results.get("computed_protein_reference_sequence")
392+
and mapping_results.get("mapped_protein_reference_sequence")
393393
):
394-
pre_mapped_metadata = mapping_results["computed_protein_reference_sequence"]
394+
pre_mapped_metadata = mapping_results.get("computed_protein_reference_sequence")
395395
target_gene.pre_mapped_metadata = cast(
396396
{
397397
"protein": {
@@ -402,21 +402,18 @@ async def map_variants_for_score_set(
402402
JSONB,
403403
)
404404
target_gene.post_mapped_metadata = cast(
405-
{"protein": mapping_results["mapped_protein_reference_sequence"]}, JSONB
405+
{"protein": mapping_results.get("mapped_protein_reference_sequence")}, JSONB
406406
)
407407
else:
408408
raise NonexistentMappingReferenceError()
409409

410410
total_variants = 0
411411
successful_mapped_variants = 0
412-
for mapped_score in mapping_results["mapped_scores"]:
412+
for mapped_score in mapping_results.get("mapped_scores"):
413413
total_variants += 1
414-
variant_urn = mapped_score["mavedb_id"]
414+
variant_urn = mapped_score.get("mavedb_id")
415415
variant = db.scalars(select(Variant).where(Variant.urn == variant_urn)).one()
416416

417-
if mapped_score["pre_mapped"] and mapped_score["post_mapped"]:
418-
successful_mapped_variants += 1
419-
420417
# there should only be one current mapped variant per variant id, so update old mapped variant to current = false
421418
existing_mapped_variant = (
422419
db.query(MappedVariant)
@@ -428,18 +425,18 @@ async def map_variants_for_score_set(
428425
existing_mapped_variant.current = False
429426
db.add(existing_mapped_variant)
430427

431-
if mapped_score["pre_mapped"] and mapped_score["post_mapped"]:
428+
if mapped_score.get("pre_mapped") and mapped_score.get("post_mapped"):
432429
successful_mapped_variants += 1
433430

434431
mapped_variant = MappedVariant(
435-
pre_mapped=mapped_score["pre_mapped"] if mapped_score["pre_mapped"] else None,
436-
post_mapped=mapped_score["post_mapped"] if mapped_score["post_mapped"] else None,
432+
pre_mapped=mapped_score.get("pre_mapped", null()),
433+
post_mapped=mapped_score.get("post_mapped", null()),
437434
variant_id=variant.id,
438435
modification_date=date.today(),
439-
mapped_date=mapping_results["mapped_date_utc"],
440-
vrs_version=mapped_score["vrs_version"] if mapped_score["vrs_version"] else None,
441-
mapping_api_version=mapping_results["dcd_mapping_version"],
442-
error_message=mapped_score["error_message"] if mapped_score["error_message"] else None,
436+
mapped_date=mapping_results.get("mapped_date_utc", null()),
437+
vrs_version=mapped_score.get("vrs_version", null()),
438+
mapping_api_version=mapping_results.get("dcd_mapping_version", null()),
439+
error_message=mapped_score.get("error_message", null()),
443440
current=True,
444441
)
445442
db.add(mapped_variant)

0 commit comments

Comments
 (0)