Skip to content

Commit

Permalink
* convert createSigTypes to typescript bq-utils.js
Browse files Browse the repository at this point in the history
  • Loading branch information
larrybabb committed Oct 1, 2024
1 parent fdc0932 commit 51c35ff
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 27 deletions.
8 changes: 8 additions & 0 deletions scripts/general/bq-createSigTypes-func.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE OR REPLACE FUNCTION `clinvar_ingest.createSigTypes`(nosig_count INTEGER, unc_count INTEGER, sig_count INTEGER)
RETURNS ARRAY<STRUCT<count INTEGER, percent NUMERIC>>
LANGUAGE js
OPTIONS (
library=['gs://clinvar-ingest/bq-tools/bq-utils.js'])
AS r"""
return createSigTypes(nosig_count, unc_count, sig_count);
""";
27 changes: 0 additions & 27 deletions scripts/temporal-table-procs/createSigType-func.sql

This file was deleted.

9 changes: 9 additions & 0 deletions scripts/temporal-table-procs/initialize-tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
40 changes: 40 additions & 0 deletions src/bq-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,43 @@ function normalizeAndKeyById(inputObject: JsonObjectWithId): Record<string, Json
if (typeof global !== 'undefined') {
(global as any).formatNearestMonth = formatNearestMonth;
}


type SigType = {
count: number;
percent: number;
};

/**
* Creates an array of SigType objects representing the count and percentage of each significance type.
*
* @param nosig_count - The count of non-significant items.
* @param unc_count - The count of uncertain items.
* @param sig_count - The count of significant items.
* @returns An array of SigType objects, each containing the count and percentage of the respective significance type.
*
* @remarks
* - If the total count of all types is zero, the function returns an array with zero counts and percentages.
* - Percentages are rounded to three decimal places.
*/
function createSigType(nosig_count: number, unc_count: number, sig_count: number): SigType[] {
// Check if the total count is zero to avoid division by zero
if ((nosig_count + unc_count + sig_count) === 0) {
return [
{ count: 0, percent: 0 },
{ count: 0, percent: 0 },
{ count: 0, percent: 0 }
];
}

// Calculate the total count
const total = nosig_count + unc_count + sig_count;

// Calculate percentages and return an array of SigType objects
// The returned array ORDINAL positions must be: 0 = nosig, 1 = unc, 2 = sig
return [
{ count: nosig_count, percent: Math.round((nosig_count / total) * 1000) / 1000 },
{ count: unc_count, percent: Math.round((unc_count / total) * 1000) / 1000 },
{ count: sig_count, percent: Math.round((sig_count / total) * 1000) / 1000 }
];
}
39 changes: 39 additions & 0 deletions test/bq-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,42 @@ test('normalizeAndKeyById should normalize and key input object correctly', () =
}
});
});
const createSigType = bqUtils.__get__('createSigType');

test('createSigType should return correct counts and percentages', () => {
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 }
]);
});

0 comments on commit 51c35ff

Please sign in to comment.