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

Various JQ fixes #59

Merged
merged 7 commits into from
Dec 5, 2023
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
3 changes: 2 additions & 1 deletion __test__/biothings_transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ describe("test biothings transformer", () => {
const res = await tf.transform();
expect(res).toHaveLength(27);
expect(res[0]).not.toHaveProperty("ref_pmid");
expect(res[0]).toHaveProperty("publications", ["PMID:21873635"]);
expect(res[0].mappedResponse).toHaveProperty("pubmed", 21873635);
// expect(res[0]).toHaveProperty("publications", ["PMID:21873635"]);
});
});

Expand Down
18 changes: 9 additions & 9 deletions __test__/data/biothings/mychem_example_edge.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"input": ["DB00188", "DB00184", "DB0000"],
"query_operation": {
"_params": {
"params": {
"fields": "drugbank.enzymes"
},
"_requestBody": {
"requestBody": {
"body": {
"q": "{inputs[0]}",
"scopes": "drugbank.id"
},
"header": "application/x-www-form-urlencoded"
},
"_supportBatch": true,
"_inputSeparator": ",",
"_method": "post",
"_pathParams": [],
"_server": "https://mychem.info/v1",
"_path": "/query",
"_tags": ["chemical", "drug", "annotation", "query", "translator", "biothings"]
"supportBatch": true,
"inputSeparator": ",",
"method": "post",
"pathParams": [],
"server": "https://mychem.info/v1",
"path": "/query",
"tags": ["chemical", "drug", "annotation", "query", "translator", "biothings"]
},
"association": {
"input_id": "DRUGBANK",
Expand Down
19 changes: 10 additions & 9 deletions __test__/data/biothings/mygene_example_edge.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
{
"input": "NCBIGene:1017",
"query_operation": {
"_params": {
"params": {
"fields": "go.BP"
},
"_requestBody": {
"requestBody": {
"body": {
"q": "{inputs[0]}",
"scopes": "entrezgene"
},
"header": "application/x-www-form-urlencoded"
},
"_supportBatch": true,
"_inputSeparator": ",",
"_method": "post",
"_pathParams": [],
"_server": "https://mygene.info/v3",
"_path": "/query",
"_tags": ["gene", "annotation", "query", "translator", "biothings"]
"supportBatch": true,
"inputSeparator": ",",
"method": "post",
"pathParams": [],
"server": "https://mygene.info/v3",
"path": "/query",
"tags": ["gene", "annotation", "query", "translator", "biothings"]
},
"association": {
"input_id": "NCBIGene",
Expand Down
23 changes: 12 additions & 11 deletions data/jq/pair/biothings.jq
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
if $edge.query_operation._method == "post" then
if $edge.query.method == "post" then
# if response is not an array, then use response.hits
if (.response | type) == "array" then .response else .response.hits end |
reduce .[] as $item ({};
# if the item is notfound, then proceed to next item & keep current object
if ($item | keys | contains(["notfound"])) then
.
else
if $edge.input | type == "object" then
generateCurieWithInputs($edge.association.input_id; $item.query; $edge.input.queryInputs) as $curie | .[$curie] = .[$curie] + [$item]
else
generateCurieWithInputs($edge.association.input_id; $item.query; $edge.input | toArray) as $curie | .[$curie] = .[$curie] + [$item]
end
generateCurieWithInputs(
$edge.input.id;
$item.query;
$edge.input.curies
| toArray
| map(. | split(":") | last)
) as $curie | .[$curie] += [$item]
end
)
else
if ($edge.input | type) == "object" then
.response as $res | generateCurie($edge.association.input_id; $edge.input.queryInputs) as $curie | {} | .[$curie] = [$res]
else
.response as $res | generateCurie($edge.association.input_id; ($edge.input | toArray)[0]) as $curie | {} | .[$curie] = [$res]
end
.response as $res | generateCurie(
$edge.input.id;
($edge.input.curies | toArray | map(. | split(":") | last) | first)
) as $curie | {} | .[$curie] = [$res]
end
35 changes: 13 additions & 22 deletions data/jq/pair/ctd.jq
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
reduce .response[] as $item (
{}; .[(
if ($edge.input | type) == "object" then
($edge.input.queryInputs | toArray)[]
else
($edge.input | toArray)[]
end
)
| select(($item.Input | ascii_upcase | split(":") | last) == (. | ascii_upcase))
| generateCurie($edge.association.input_id; .)
] = []
+ .[(
if ($edge.input | type) == "object" then
($edge.input.queryInputs | toArray)[]
else
($edge.input | toArray)[]
end
)
| select(($item.Input | ascii_upcase | split(":") | last) == (. | ascii_upcase))
| generateCurie($edge.association.input_id; .)
]
+ [$item]
(reduce .response[].Input as $id (
{}; if (. as $mapping | $id | in($mapping) | not)
then
($edge.input.curies | find((. | ascii_upcase) == ($id | split(":") | last | ascii_upcase))) as $fixedID
| .[$id] = $fixedID
end
)) as $idMapping # maps response[].Input -> appropriate $edge.input.curies
| reduce .response[] as $item (
{}; if ($idMapping[$item.Input])
then
(generateCurie($edge.input.id; $idMapping[$item.Input])) as $curie
| .[$curie] += [$item]
end
) | map_values([.])
3 changes: 3 additions & 0 deletions data/jq/utils.jq
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ def list_filter_all(filter_strs): if (. | type) == "array" then [.[] | if . | al

# filters the list that is supplied on any conditions
def list_filter_any(filter_strs): if (. | type) == "array" then [.[] | if . | any_filter(filter_strs) then . else empty end] else empty end;

# essential Array.find()
def find(cond): . as $iter | 0 | until((. == ($iter | length)) or ($iter[.] | cond); . + 1) | if (. == ($iter | length)) then false else $iter[.] end;
16 changes: 7 additions & 9 deletions src/jq_utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { JQVariable, BTEKGOperationObject } from "./types";
import Path from "path";
import fs from "fs";
import { toArray } from "./utils";

const functions = fs.readFileSync(Path.resolve(`${__dirname}/../data/jq/utils.jq`), { encoding: "utf8" });
const functions = fs.readFileSync(
Path.resolve(`${__dirname}/../data/jq/utils.jq`),
{ encoding: "utf8" },
);

function generateVariables(variables: JQVariable[]) {
let variableString = "";
Expand All @@ -12,12 +16,6 @@ function generateVariables(variables: JQVariable[]) {
return variableString;
}

export function generateFilterString(filterString: string, edge: BTEKGOperationObject) {
const variables = [
{
name: "$edge",
value: JSON.stringify(edge),
},
];
return `${functions}\n${generateVariables(variables)}${filterString}`;
export function generateFilterString(filterString: string) {
return `${functions}\n${filterString}`;
}
60 changes: 35 additions & 25 deletions src/transformers/jq_transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Path from "path";

import * as jq from "node-jq"; // If converted to import, ts compile breaks it (imports default as undefined)
import { JSONDoc, PairedResponse } from "../json_transform/types";
import { toArray } from "../utils";

// Get prefab JQ strings from data/jq
const filterStringsWrap = Object.fromEntries(
Expand All @@ -24,33 +25,42 @@ const filterStringsPair = Object.fromEntries(
export default class JQTransformer extends BaseTransformer {
// TODO more specific typing?
async wrap(res: JSONDoc | JSONDoc[]): Promise<JSONDoc> {
if (this.config.wrap)
res = JSON.parse(
(await jq.run(generateFilterString(this.config.wrap, this.edge), res, { input: "json" })) as string,
);
else if (filterStringsWrap[this.config.type])
res = JSON.parse(
(await jq.run(generateFilterString(filterStringsWrap[this.config.type], this.edge), res, {
input: "json",
})) as string,
);
else res = super.wrap(res);

return res;
let filterString: string | undefined = this.config.wrap ?? filterStringsWrap[this.config.type];
if (typeof filterString === "undefined") return super.wrap(res);
filterString = generateFilterString(filterString);
return JSON.parse(
(await jq.run(filterString, res, {
input: "json",
})) as string,
);
}

async pairCurieWithAPIResponse(): Promise<PairedResponse> {
if (this.config.pair)
return JSON.parse(
(await jq.run(generateFilterString(this.config.pair, this.edge), this.data, { input: "json" })) as string,
);
else if (filterStringsPair[this.config.type])
return JSON.parse(
(await jq.run(generateFilterString(filterStringsPair[this.config.type], this.edge), this.data, {
input: "json",
})) as string,
);

return super.pairCurieWithAPIResponse();
let filterString: string | undefined = this.config.pair ?? filterStringsPair[this.config.type];
if (typeof filterString === "undefined") return super.pairCurieWithAPIResponse();
const data = {
response: this.data.response,
edge: {
query: {
method: this.edge.query_operation.method,
},
input: {
id: this.edge.association.input_id,
type: this.edge.association.input_type,
// input is array or is object with queryInputs
curies:
Array.isArray(this.edge.input) || typeof this.edge.input === "string"
? toArray(this.edge.input)
: toArray(this.edge.input.queryInputs),
},
predicate: this.edge.association.predicate,
output: {
id: this.edge.association.output_id,
type: this.edge.association.output_type,
},
},
};
filterString = `.edge as $edge | ${generateFilterString(filterString)}`;
return JSON.parse((await jq.run(filterString, data, { input: "json" })) as string);
}
}
Loading