Skip to content

Commit

Permalink
allow multiple input/output prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rjawesome committed Dec 5, 2023
1 parent 64e3a01 commit 80bdeb4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
5 changes: 4 additions & 1 deletion data/jq/utils.jq
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 5 additions & 4 deletions src/transformers/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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));
}
}
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down

0 comments on commit 80bdeb4

Please sign in to comment.