From 80bdeb42193620aec1724150b09eece2d5a1d29a Mon Sep 17 00:00:00 2001 From: Rohan Juneja Date: Mon, 4 Dec 2023 17:13:49 -0800 Subject: [PATCH] allow multiple input/output prefixes --- data/jq/utils.jq | 5 ++++- src/transformers/transformer.ts | 9 +++++---- src/types.ts | 3 +++ src/utils.ts | 2 +- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/data/jq/utils.jq b/data/jq/utils.jq index 220755c..3f4abdd 100644 --- a/data/jq/utils.jq +++ b/data/jq/utils.jq @@ -16,8 +16,11 @@ def getfirst: if (. | type) == "array" then .[0] else . end; # generates a curie from a type and id def generateCurie(idType; id): id | getfirst | split(":") | last | idType + ":" + .; +# removes prefix if there +def rmPrefix: . | split(":") | last; + # generates a curie from a type and id [string] (by checking queryInputs) -def generateCurieWithInputs(idType; id; queryInputs): (id | getfirst) as $id | reduce (queryInputs | toArray | .[]) as $input (""; if ($id | ascii_upcase | contains($input | ascii_upcase)) then $input else . end) | idType + ":" + .; +def generateCurieWithInputs(idType; id; queryInputs): (id | getfirst) as $id | reduce (queryInputs | toArray | .[] | rmPrefix) as $input (""; if ($id | ascii_upcase | contains($input | ascii_upcase)) then $input else . end) | idType + ":" + .; # getting a nested field from inputted object (seperated by ., ie. drugcentral.bioactivity) def get_nested_field(field): diff --git a/src/transformers/transformer.ts b/src/transformers/transformer.ts index a07b4a4..7d8f438 100644 --- a/src/transformers/transformer.ts +++ b/src/transformers/transformer.ts @@ -193,7 +193,7 @@ export default class BaseTransformer { _removeNonEdgeData(mappedResponse: any) { delete mappedResponse["@type"]; - delete mappedResponse[this.edge.association.output_id]; + delete mappedResponse["output"]; delete mappedResponse["input_name"]; delete mappedResponse["output_name"]; return mappedResponse; @@ -241,6 +241,7 @@ export default class BaseTransformer { async transform() { let transformedRecords = []; let responses = await this.pairCurieWithAPIResponse(); + console.log((this.edge.input as any).queryInputs) await async.eachSeries(Object.entries(responses), async ([curie, curieResponses]) => { if (Array.isArray(curieResponses) && curieResponses.length > 0) { @@ -267,10 +268,10 @@ export default class BaseTransformer { */ extractObjectIDs(mappedResponse: object) { const output_id_type = this.edge.association.output_id; - if (!(output_id_type in mappedResponse)) { + if (!('output' in mappedResponse)) { return []; } - mappedResponse[output_id_type] = toArray(mappedResponse[output_id_type]); - return mappedResponse[output_id_type].map((id: string) => generateCurie(output_id_type, id)); + mappedResponse.output = toArray(mappedResponse['output']); + return (mappedResponse['output'] as any[]).map((id: string) => generateCurie(output_id_type, id)); } } diff --git a/src/types.ts b/src/types.ts index 6c24101..74b5e89 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3,7 +3,10 @@ import { JSONDoc } from "./json_transform/types"; interface KGAssociationObject { input_id?: string; input_type: string; + input_name_field?: string; output_id?: string; + output_id_field: string; + output_name_field?: string; output_type: string; predicate: string; source?: string; diff --git a/src/utils.ts b/src/utils.ts index ffa9b24..67f66c5 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -8,7 +8,7 @@ export function generateCurie(idType: string, id: string | string[]) { return idType + ":" + id; } -export function toArray(item) { +export function toArray(item): any[] { if (!Array.isArray(item)) { return [item]; }