Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent record qualifier values from taking on array values #71

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 41 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: string, qualifier: string | string[]) {
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,25 @@ 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;
}

// avoid setting record qualifiers to its association qualifiers as
// that may result in an invalid TRAPI response
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
Loading