Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
tokebe committed Dec 5, 2023
2 parents 208c4cc + df854b3 commit 878e4b8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/graph/kg_edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class KGEdge {
};
publications: Set<string>;
qualifiers: {
[qualifier_type_id: string]: string;
[qualifier_type_id: string]: string | string[];
};
attributes: {
[attribute_type_id: string]: Set<string> | TrapiAttribute[];
Expand Down Expand Up @@ -105,7 +105,7 @@ export default class KGEdge {
});
}

addQualifier(name: string, value: string): void {
addQualifier(name: string, value: string | string[]): void {
this.qualifiers[name] = value;
}

Expand Down
4 changes: 3 additions & 1 deletion src/inferred_mode/inferred_mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ export default class InferredQueryHandler {
return Object.fromEntries(
qualifierSetObj.qualifier_set.map(({ qualifier_type_id, qualifier_value }) => [
qualifier_type_id.replace('biolink:', ''),
qualifier_value.replace('biolink:', ''),
Array.isArray(qualifier_value)
? qualifier_value.map((string) => string.replace('biolink:', ''))
: qualifier_value.replace('biolink:', ''),
]),
) as CompactQualifiers;
});
Expand Down
37 changes: 27 additions & 10 deletions src/query_edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,25 @@ export default class QEdge {
return {
qualifier_set: qualifierSetObj.qualifier_set.map(({ qualifier_type_id, qualifier_value }) => {
const new_qualifier_values = qualifier_type_id.includes('predicate')
? Array.from(
new Set(
biolink
.getDescendantPredicates(utils.removeBioLinkPrefix(qualifier_value))
.map((item) => `biolink:${utils.removeBioLinkPrefix(item)}`),
),
)
: Array.from(new Set(biolink.getDescendantQualifiers(utils.removeBioLinkPrefix(qualifier_value))));
? Array.isArray(qualifier_value)
? Array.from(
qualifier_value.reduce((set: Set<string>, predicate: string) => {
biolink
.getDescendantPredicates(utils.removeBioLinkPrefix(predicate))
.forEach((item) => set.add(`biolink:${utils.removeBioLinkPrefix(item)}`));
return set;
}, new Set()),
)
: Array.from(
new Set(
biolink
.getDescendantPredicates(utils.removeBioLinkPrefix(qualifier_value))
.map((item) => `biolink:${utils.removeBioLinkPrefix(item)}`),
),
)
: Array.from(
new Set(biolink.getDescendantQualifiers(utils.removeBioLinkPrefix(qualifier_value as string))),
);

return {
qualifier_type_id,
Expand All @@ -180,7 +191,11 @@ export default class QEdge {
let newQualifierType = qualifier_type_id;
let newQualifierValue = qualifier_value;
if (qualifier_type_id.includes('predicate')) {
newQualifierValue = `biolink:${this.getReversedPredicate(qualifier_value.replace('biolink:', ''))}`;
if (Array.isArray(qualifier_value)) {
newQualifierValue = qualifier_value.map((str) => `biolink:${str.replace('biolink', '')}`);
} else {
newQualifierValue = `biolink:${qualifier_value.replace('biolink:', '')}`;
}
}
if (qualifier_type_id.includes('subject')) {
newQualifierType = qualifier_type_id.replace('subject', 'object');
Expand All @@ -204,7 +219,9 @@ export default class QEdge {
return Object.fromEntries(
qualifierSetObj.qualifier_set.map(({ qualifier_type_id, qualifier_value }) => [
qualifier_type_id.replace('biolink:', ''),
qualifier_value.replace('biolink:', ''),
Array.isArray(qualifier_value)
? qualifier_value.map((string) => string.replace('biolink:', ''))
: qualifier_value.replace('biolink:', ''),
]),
);
});
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export interface TrapiAttribute {

export interface TrapiQualifier {
qualifier_type_id: string;
qualifier_value: string;
qualifier_value: string | string[];
}

export interface TrapiQualifierConstraint {
Expand Down
9 changes: 5 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ export function getUnique<Type>(input: Type[]): Type[] {
return Array.from(new Set(input));
}

export function removeBioLinkPrefix(input: string): string {
if (input && input.startsWith('biolink:')) {
return input.slice(8);
export function removeBioLinkPrefix<T extends string | string[]>(input: T): T {
if (Array.isArray(input)) {
return input.map((str: string) => str.replace('biolink:', '')) as T;
} else {
return input.replace('biolink:', '') as T;
}
return input;
}

// This gets the intersection of two sets.
Expand Down

0 comments on commit 878e4b8

Please sign in to comment.