From 551c37acca3fe5ed0f0295708e2e5fedde83723b Mon Sep 17 00:00:00 2001 From: NeuralFlux <40491005+NeuralFlux@users.noreply.github.com> Date: Wed, 2 Oct 2024 16:49:33 -0400 Subject: [PATCH] fix: modify init and reverse record logic avoid setting record qualifiers to its association qualifiers as that may result in an invalid TRAPI response --- src/record.ts | 65 ++++++++++++++++----------- src/transformers/trapi_transformer.ts | 2 +- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/src/record.ts b/src/record.ts index 7d29294..22bd2df 100644 --- a/src/record.ts +++ b/src/record.ts @@ -169,6 +169,32 @@ export class Record { } } + reverseQualifierEntry(qualifierType, qualifier) { + let newQualifierType: string = qualifierType; + let newQualifier: string | string[] = qualifier; + if (qualifierType.includes("predicate")) { + if (Array.isArray(qualifier)) { + newQualifier = qualifier.map( + (str: string) => + `biolink:${this.qEdge.getReversedPredicate( + str.replace("biolink:", ""), + )}`, + ); + } else { + newQualifier = `biolink:${this.qEdge.getReversedPredicate( + qualifier.replace("biolink:", ""), + )}`; + } + } + if (qualifierType.includes("subject")) { + newQualifierType = qualifierType.replace("subject", "object"); + } + if (qualifierType.includes("object")) { + newQualifierType = qualifierType.replace("object", "subject"); + } + return [newQualifierType, newQualifier]; + } + reverse() { const frozen = { ...this.freezeVerbose() }; const reversedAPIEdge: Association = { ...frozen.association }; @@ -183,36 +209,23 @@ export class Record { if (reversedAPIEdge.qualifiers) { const reversedQualifiers = Object.fromEntries( Object.entries(reversedAPIEdge.qualifiers).map( - ([qualifierType, qualifier]) => { - let newQualifierType: string = qualifierType; - let newQualifier: string | string[] = qualifier; - if (qualifierType.includes("predicate")) { - if (Array.isArray(qualifier)) { - newQualifier = qualifier.map( - (str: string) => - `biolink:${this.qEdge.getReversedPredicate( - str.replace("biolink:", ""), - )}`, - ); - } else { - newQualifier = `biolink:${this.qEdge.getReversedPredicate( - qualifier.replace("biolink:", ""), - )}`; - } - } - if (qualifierType.includes("subject")) { - newQualifierType = qualifierType.replace("subject", "object"); - } - if (qualifierType.includes("object")) { - newQualifierType = qualifierType.replace("object", "subject"); - } - return [newQualifierType, newQualifier]; - }, + ([qualifierType, qualifier]) => this.reverseQualifierEntry( + qualifierType, qualifier + ), ), ); reversedAPIEdge.qualifiers = reversedQualifiers; - frozen.qualifiers = reversedQualifiers; + } + + if (frozen.qualifiers) { + frozen.qualifiers = Object.fromEntries( + Object.entries(frozen.qualifiers).map( + ([qualifierType, qualifier]) => this.reverseQualifierEntry( + qualifierType, qualifier + ), + ), + ); } // frozen.predicate = 'biolink:' + predicate; frozen.association = reversedAPIEdge; diff --git a/src/transformers/trapi_transformer.ts b/src/transformers/trapi_transformer.ts index a79985d..0e3ce21 100644 --- a/src/transformers/trapi_transformer.ts +++ b/src/transformers/trapi_transformer.ts @@ -95,7 +95,7 @@ export default class TRAPITransformer extends BaseTransformer { return [qualifier.qualifier_type_id, qualifier.qualifier_value]; }), ) - : undefined, + : {}, // set to empty to avoid record init from association association: this.edge.association, qEdge: this.edge.reasoner_edge, mappedResponse: {