diff --git a/package-lock.json b/package-lock.json index 22ac53de..23947843 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25,6 +25,7 @@ "colorette": "^2.0.20", "dom-to-svg": "^0.12.2", "isomorphic-fetch": "^3.0.0", + "jsonpath-plus": "^10.2.0", "react": "^18.2.0", "react-dom": "^18.2.0", "react-responsive-carousel": "^3.2.23", @@ -3872,6 +3873,30 @@ "@jridgewell/sourcemap-codec": "^1.4.14" } }, + "node_modules/@jsep-plugin/assignment": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/@jsep-plugin/assignment/-/assignment-1.3.0.tgz", + "integrity": "sha512-VVgV+CXrhbMI3aSusQyclHkenWSAm95WaiKrMxRFam3JSUiIaQjoMIw2sEs/OX4XifnqeQUN4DYbJjlA8EfktQ==", + "license": "MIT", + "engines": { + "node": ">= 10.16.0" + }, + "peerDependencies": { + "jsep": "^0.4.0||^1.0.0" + } + }, + "node_modules/@jsep-plugin/regex": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/@jsep-plugin/regex/-/regex-1.0.4.tgz", + "integrity": "sha512-q7qL4Mgjs1vByCaTnDFcBnV9HS7GVPJX5vyVoCgZHNSC9rjwIlmbXG5sUuorR5ndfHAIlJ8pVStxvjXHbNvtUg==", + "license": "MIT", + "engines": { + "node": ">= 10.16.0" + }, + "peerDependencies": { + "jsep": "^0.4.0||^1.0.0" + } + }, "node_modules/@kbox-labs/react-echarts": { "version": "1.4.0", "license": "MIT", @@ -7742,11 +7767,6 @@ "dev": true, "license": "MIT" }, - "node_modules/@types/jsonpath-plus": { - "version": "5.0.5", - "dev": true, - "license": "MIT" - }, "node_modules/@types/lodash": { "version": "4.17.9", "dev": true, @@ -17807,6 +17827,15 @@ "node": ">=12" } }, + "node_modules/jsep": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/jsep/-/jsep-1.4.0.tgz", + "integrity": "sha512-B7qPcEVE3NVkmSJbaYxvv4cHkVW7DQsZz13pUMrfS8z8Q/BuShN+gcTXrUlPiGqM2/t/EEaI030bpxMqY8gMlw==", + "license": "MIT", + "engines": { + "node": ">= 10.16.0" + } + }, "node_modules/jsesc": { "version": "2.5.2", "license": "MIT", @@ -17891,14 +17920,21 @@ "license": "MIT" }, "node_modules/jsonpath-plus": { - "version": "8.1.0", + "version": "10.2.0", + "resolved": "https://registry.npmjs.org/jsonpath-plus/-/jsonpath-plus-10.2.0.tgz", + "integrity": "sha512-T9V+8iNYKFL2n2rF+w02LBOT2JjDnTjioaNFrxRy0Bv1y/hNsqR/EBK7Ojy2ythRHwmz2cRIls+9JitQGZC/sw==", "license": "MIT", + "dependencies": { + "@jsep-plugin/assignment": "^1.3.0", + "@jsep-plugin/regex": "^1.0.4", + "jsep": "^1.4.0" + }, "bin": { "jsonpath": "bin/jsonpath-cli.js", "jsonpath-plus": "bin/jsonpath-cli.js" }, "engines": { - "node": ">=14.0.0" + "node": ">=18.0.0" } }, "node_modules/JSONStream": { @@ -34213,7 +34249,6 @@ "@types/fastestsmallesttextencoderdecoder": "^1.0.2", "@types/flat": "^5.0.3", "@types/isomorphic-fetch": "^0.0.36", - "@types/jsonpath-plus": "5.0.5", "@types/lodash": "^4.14.191", "@types/papaparse": "^5.3.14", "@types/uuid": "^9.0.0" @@ -34329,7 +34364,6 @@ "gray-matter": "^4.0.3", "jose": "^4.15.9", "js-cookie": "^3.0.5", - "jsonpath-plus": "^8.0.0", "mantine-react-table": "^2.0.0-beta.7", "mathjs": "^13.1.1", "minisearch": "^6.3.0", diff --git a/package.json b/package.json index ff8d1856..48fbeaea 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "@mantine/modals": "^7.13.3", "@mantine/dates": "^7.13.3", "@mantine/notifications": "^7.13.3", + "jsonpath-plus": "^10.2.0", "classnames": "^2.3.1", "colorette": "^2.0.20", "dom-to-svg": "^0.12.2", diff --git a/packages/core/package.json b/packages/core/package.json index 980a25d7..571bbbaf 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -48,7 +48,6 @@ "@types/fastestsmallesttextencoderdecoder": "^1.0.2", "@types/flat": "^5.0.3", "@types/isomorphic-fetch": "^0.0.36", - "@types/jsonpath-plus": "5.0.5", "@types/lodash": "^4.14.191", "@types/papaparse": "^5.3.14", "@types/uuid": "^9.0.0" diff --git a/packages/core/src/features/guppy/guppySlice.ts b/packages/core/src/features/guppy/guppySlice.ts index 1c20ec4b..b91cf17d 100644 --- a/packages/core/src/features/guppy/guppySlice.ts +++ b/packages/core/src/features/guppy/guppySlice.ts @@ -14,24 +14,32 @@ const statusEndpoint = '/_status'; const processHistogramResponse = ( data: Record, ): AggregationsData => { - const pathData = JSONPath({ + const valueData = JSONPath({ json: data, path: '$..histogram', - resultType: 'all', + resultType: 'value', }); - const results = pathData.reduce( - (acc: AggregationsData, element: Record) => { + + const pointerData = JSONPath({ + json: data, + path: '$..histogram', + resultType: 'pointer', + }); + + const results = pointerData.reduce( + (acc: AggregationsData, element: Record, idx: number) => { const key = element.pointer .slice(1) .replace(/\/histogram/g, '') .replace(/\//g, '.'); return { ...acc, - [key]: element.value, + [key]: valueData[idx], }; }, {} as AggregationsData, ); + console.log('processHistogramResponse', results); return results as AggregationsData; }; diff --git a/packages/core/src/features/guppy/tests/jsonpath.unit.test.ts b/packages/core/src/features/guppy/tests/jsonpath.unit.test.ts new file mode 100644 index 00000000..348add1f --- /dev/null +++ b/packages/core/src/features/guppy/tests/jsonpath.unit.test.ts @@ -0,0 +1,543 @@ +import { describe, it } from '@jest/globals'; +import { JSONPath } from 'jsonpath-plus'; +import { AggregationsData } from '../../../types'; + +const histogramData = { + programs_name: { + histogram: [ + { + key: 'tutorial', + count: 7518, + }, + { + key: 'idc', + count: -1, + }, + ], + }, + project_id: { + histogram: [ + { + key: 'tutorial-synthetic_data_set_open_access_1', + count: 2504, + }, + { + key: 'tutorial-synthetic_data_set_controlled_access_1', + count: 2504, + }, + { + key: 'tutorial-synthetic_data_set_1', + count: 2504, + }, + { + key: 'tutorial-study_level_test_1', + count: -1, + }, + { + key: 'idc-tcia-tcga-read', + count: -1, + }, + ], + }, + consent_codes: { + histogram: [ + { + key: 'open', + count: 5011, + }, + { + key: 'controlled', + count: 2510, + }, + ], + }, + studies_submitter_id: { + histogram: [ + { + key: 'public', + count: 2504, + }, + { + key: 'open', + count: 2504, + }, + { + key: 'controlled', + count: 2504, + }, + { + key: 'study_level_test_1', + count: -1, + }, + { + key: 'tcia-tcga-read', + count: -1, + }, + ], + }, + geographic_site: { + histogram: [ + { + key: 'no data', + count: 7521, + }, + ], + }, + data_type: { + histogram: [ + { + key: 'Simple Germline Variation', + count: 2504, + }, + { + key: 'Aligned Reads', + count: 2504, + }, + { + key: 'TXT', + count: -1, + }, + { + key: 'Imaging File Reference', + count: -1, + }, + { + key: 'Imaging File', + count: -1, + }, + { + key: 'no data', + count: 4968, + }, + ], + }, + data_format: { + histogram: [ + { + key: 'VCF', + count: 2504, + }, + { + key: 'CRAM', + count: 2504, + }, + { + key: 'TXT', + count: -1, + }, + { + key: 'TSV', + count: -1, + }, + { + key: 'NIFTI', + count: -1, + }, + { + key: 'no data', + count: 4968, + }, + ], + }, + annotated_sex: { + histogram: [ + { + key: 'female', + count: 3813, + }, + { + key: 'male', + count: 3699, + }, + { + key: 'no data', + count: -1, + }, + ], + }, + race: { + histogram: [ + { + key: '["asian"]', + count: 2979, + }, + { + key: '["black or african american"]', + count: 1983, + }, + { + key: '["white"]', + count: 1509, + }, + { + key: '["hispanic"]', + count: 1041, + }, + { + key: 'no data', + count: -1, + }, + ], + }, + ethnicity: { + histogram: [ + { + key: 'no data', + count: 7521, + }, + ], + }, + hispanic_subgroup: { + histogram: [ + { + key: 'no data', + count: 7521, + }, + ], + }, + subcohort: { + histogram: [ + { + key: 'no data', + count: 7521, + }, + ], + }, + current_smoker_baseline: { + histogram: [ + { + key: 'no data', + count: 7521, + }, + ], + }, + ever_smoker_baseline: { + histogram: [ + { + key: 'no data', + count: 7521, + }, + ], + }, + fasting_lipids: { + histogram: [ + { + key: 'no data', + count: 7521, + }, + ], + }, + lipid_lowering_medication: { + histogram: [ + { + key: 'no data', + count: 7521, + }, + ], + }, + carotid_plaque: { + histogram: [ + { + key: 'no data', + count: 7521, + }, + ], + }, + carotid_stenosis: { + histogram: [ + { + key: 'no data', + count: 7521, + }, + ], + }, + antihypertensive_meds: { + histogram: [ + { + key: 'no data', + count: 7521, + }, + ], + }, + vte_case_status: { + histogram: [ + { + key: 'no data', + count: 7521, + }, + ], + }, + vte_prior_history: { + histogram: [ + { + key: 'no data', + count: 7521, + }, + ], + }, +}; + +const results = { + programs_name: [ + { + key: 'tutorial', + count: 7518, + }, + { + key: 'idc', + count: -1, + }, + ], + project_id: [ + { + key: 'tutorial-synthetic_data_set_open_access_1', + count: 2504, + }, + { + key: 'tutorial-synthetic_data_set_controlled_access_1', + count: 2504, + }, + { + key: 'tutorial-synthetic_data_set_1', + count: 2504, + }, + { + key: 'tutorial-study_level_test_1', + count: -1, + }, + { + key: 'idc-tcia-tcga-read', + count: -1, + }, + ], + consent_codes: [ + { + key: 'open', + count: 5011, + }, + { + key: 'controlled', + count: 2510, + }, + ], + studies_submitter_id: [ + { + key: 'public', + count: 2504, + }, + { + key: 'open', + count: 2504, + }, + { + key: 'controlled', + count: 2504, + }, + { + key: 'study_level_test_1', + count: -1, + }, + { + key: 'tcia-tcga-read', + count: -1, + }, + ], + geographic_site: [ + { + key: 'no data', + count: 7521, + }, + ], + data_type: [ + { + key: 'Simple Germline Variation', + count: 2504, + }, + { + key: 'Aligned Reads', + count: 2504, + }, + { + key: 'TXT', + count: -1, + }, + { + key: 'Imaging File Reference', + count: -1, + }, + { + key: 'Imaging File', + count: -1, + }, + { + key: 'no data', + count: 4968, + }, + ], + data_format: [ + { + key: 'VCF', + count: 2504, + }, + { + key: 'CRAM', + count: 2504, + }, + { + key: 'TXT', + count: -1, + }, + { + key: 'TSV', + count: -1, + }, + { + key: 'NIFTI', + count: -1, + }, + { + key: 'no data', + count: 4968, + }, + ], + annotated_sex: [ + { + key: 'female', + count: 3813, + }, + { + key: 'male', + count: 3699, + }, + { + key: 'no data', + count: -1, + }, + ], + race: [ + { + key: '["asian"]', + count: 2979, + }, + { + key: '["black or african american"]', + count: 1983, + }, + { + key: '["white"]', + count: 1509, + }, + { + key: '["hispanic"]', + count: 1041, + }, + { + key: 'no data', + count: -1, + }, + ], + ethnicity: [ + { + key: 'no data', + count: 7521, + }, + ], + hispanic_subgroup: [ + { + key: 'no data', + count: 7521, + }, + ], + subcohort: [ + { + key: 'no data', + count: 7521, + }, + ], + current_smoker_baseline: [ + { + key: 'no data', + count: 7521, + }, + ], + ever_smoker_baseline: [ + { + key: 'no data', + count: 7521, + }, + ], + fasting_lipids: [ + { + key: 'no data', + count: 7521, + }, + ], + lipid_lowering_medication: [ + { + key: 'no data', + count: 7521, + }, + ], + carotid_plaque: [ + { + key: 'no data', + count: 7521, + }, + ], + carotid_stenosis: [ + { + key: 'no data', + count: 7521, + }, + ], + antihypertensive_meds: [ + { + key: 'no data', + count: 7521, + }, + ], + vte_case_status: [ + { + key: 'no data', + count: 7521, + }, + ], + vte_prior_history: [ + { + key: 'no data', + count: 7521, + }, + ], +}; + +describe('test json path plus with hisogram', () => { + it('should return histogram data with correct facet value', () => { + const valueData = JSONPath({ + json: histogramData, + path: '$..histogram', + resultType: 'value', + }); + + const pointerData = JSONPath({ + json: histogramData, + path: '$..histogram', + resultType: 'pointer', + }); + + // const pointer = JSONPath.toPointer(pathData); + const processedResults = pointerData.reduce( + (acc: AggregationsData, element: Record, idx: number) => { + const key = element + .slice(1) + .replace(/\/histogram/g, '') + .replace(/\//g, '.'); + return { + ...acc, + [key]: valueData[idx], + }; + }, + {} as AggregationsData, + ); + expect(processedResults).toEqual(results); + }); +}); diff --git a/packages/frontend/package.json b/packages/frontend/package.json index 4efa4896..5aece8fe 100644 --- a/packages/frontend/package.json +++ b/packages/frontend/package.json @@ -84,7 +84,6 @@ "gray-matter": "^4.0.3", "jose": "^4.15.9", "js-cookie": "^3.0.5", - "jsonpath-plus": "^8.0.0", "mantine-react-table": "^2.0.0-beta.7", "mathjs": "^13.1.1", "minisearch": "^6.3.0", diff --git a/packages/sampleCommons/.env.development b/packages/sampleCommons/.env.development index 7a923fa9..3c13c510 100644 --- a/packages/sampleCommons/.env.development +++ b/packages/sampleCommons/.env.development @@ -1,4 +1,4 @@ -GEN3_COMMONS_NAME=gen3 +GEN3_COMMONS_NAME=biodata NEXT_PUBLIC_GEN3_API=https://localhost:3010 ## For more fine grain control over the API uncomment the relevant lines