Skip to content

Commit

Permalink
fix: modify init and reverse record logic
Browse files Browse the repository at this point in the history
avoid setting record qualifiers to its association qualifiers as that may result in an invalid TRAPI response
  • Loading branch information
NeuralFlux committed Oct 2, 2024
1 parent 38811c0 commit 551c37a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
65 changes: 39 additions & 26 deletions src/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/trapi_transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 551c37a

Please sign in to comment.