From 51c35ffa629b8c6013602a2ffbb8b2f970d35537 Mon Sep 17 00:00:00 2001 From: Lawrence Babb Date: Mon, 30 Sep 2024 21:51:43 -0400 Subject: [PATCH] * convert createSigTypes to typescript bq-utils.js --- scripts/general/bq-createSigTypes-func.sql | 8 ++++ .../createSigType-func.sql | 27 ------------- .../initialize-tables.sql | 9 +++++ src/bq-utils.ts | 40 +++++++++++++++++++ test/bq-utils.test.js | 39 ++++++++++++++++++ 5 files changed, 96 insertions(+), 27 deletions(-) create mode 100644 scripts/general/bq-createSigTypes-func.sql delete mode 100644 scripts/temporal-table-procs/createSigType-func.sql diff --git a/scripts/general/bq-createSigTypes-func.sql b/scripts/general/bq-createSigTypes-func.sql new file mode 100644 index 0000000..57bad18 --- /dev/null +++ b/scripts/general/bq-createSigTypes-func.sql @@ -0,0 +1,8 @@ +CREATE OR REPLACE FUNCTION `clinvar_ingest.createSigTypes`(nosig_count INTEGER, unc_count INTEGER, sig_count INTEGER) +RETURNS ARRAY> +LANGUAGE js + OPTIONS ( + library=['gs://clinvar-ingest/bq-tools/bq-utils.js']) +AS r""" + return createSigTypes(nosig_count, unc_count, sig_count); +"""; \ No newline at end of file diff --git a/scripts/temporal-table-procs/createSigType-func.sql b/scripts/temporal-table-procs/createSigType-func.sql deleted file mode 100644 index ad8124a..0000000 --- a/scripts/temporal-table-procs/createSigType-func.sql +++ /dev/null @@ -1,27 +0,0 @@ - --- agg_sig_type sig|unc|nosig as 2|1|0 so ---- 7 = 111 or yes to all 3. (sig conflict) ---- 6 = 110 or yes to sig and unc only (sig conflict) ---- 5 = 101 or yes to sig and nosig only (sig conflict) ---- 4 = 100 or yes to sig only. (no conflict - sig) ---- 3 = 011 or yes to unc and no sig (nosig conflict*) ---- 2 = 010. or yes to unc only (no conflict - nosig) ---- 1 = 001. or yes to nosig only (no conflict - nosig) - - - - -CREATE OR REPLACE FUNCTION `clinvar_ingest.createSigType`(nosig_count INTEGER, unc_count INTEGER, sig_count INTEGER) -RETURNS ARRAY> -AS ( - IF( - (nosig_count + unc_count + sig_count ) = 0, - ARRAY[STRUCT(0,0),STRUCT(0,0),STRUCT(0,0)], - -- order is significant, OFFSET 0=no-sig, 1=uncertain, 2=sig - ARRAY[ - STRUCT( nosig_count, CAST(ROUND(nosig_count/(sig_count + unc_count + nosig_count ),3) as NUMERIC)), - STRUCT( unc_count, CAST(ROUND(unc_count/(sig_count + unc_count + nosig_count ),3) as NUMERIC)), - STRUCT( sig_count, CAST(ROUND(sig_count/(sig_count + unc_count + nosig_count ),3) as NUMERIC)) - ] - ) -); \ No newline at end of file diff --git a/scripts/temporal-table-procs/initialize-tables.sql b/scripts/temporal-table-procs/initialize-tables.sql index 7de1871..169cefb 100644 --- a/scripts/temporal-table-procs/initialize-tables.sql +++ b/scripts/temporal-table-procs/initialize-tables.sql @@ -415,4 +415,13 @@ CREATE OR REPLACE TABLE `clinvar_ingest.clinvar_gc_scvs` end_release_date DATE, deleted_release_date DATE, deleted_count INT DEFAULT 0 +); + + +-- ***************** clinvar_var_scv_change ***************** +CREATE OR REPLACE TABLE `clinvar_ingest.clinvar_var_scv_change` +( + variation_id STRING NOT NULL, + start_release_date DATE, + end_release_date DATE ); \ No newline at end of file diff --git a/src/bq-utils.ts b/src/bq-utils.ts index 40477ce..d084700 100644 --- a/src/bq-utils.ts +++ b/src/bq-utils.ts @@ -210,3 +210,43 @@ function normalizeAndKeyById(inputObject: JsonObjectWithId): Record { + expect(createSigType(0, 0, 0)).toEqual([ + { count: 0, percent: 0 }, + { count: 0, percent: 0 }, + { count: 0, percent: 0 } + ]); + + expect(createSigType(10, 0, 0)).toEqual([ + { count: 10, percent: 1 }, + { count: 0, percent: 0 }, + { count: 0, percent: 0 } + ]); + + expect(createSigType(0, 10, 0)).toEqual([ + { count: 0, percent: 0 }, + { count: 10, percent: 1 }, + { count: 0, percent: 0 } + ]); + + expect(createSigType(0, 0, 10)).toEqual([ + { count: 0, percent: 0 }, + { count: 0, percent: 0 }, + { count: 10, percent: 1 } + ]); + + expect(createSigType(10, 10, 10)).toEqual([ + { count: 10, percent: 0.333 }, + { count: 10, percent: 0.333 }, + { count: 10, percent: 0.333 } + ]); + + expect(createSigType(5, 10, 15)).toEqual([ + { count: 5, percent: 0.167 }, + { count: 10, percent: 0.333 }, + { count: 15, percent: 0.5 } + ]); +}); \ No newline at end of file