You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
2.9 KiB
SQL
99 lines
2.9 KiB
SQL
/*
|
|
I started by creating a formularies schema,
|
|
then importing the usp - dc formulary data through DBeaver's csv import.
|
|
*/
|
|
|
|
-- DROP SCHEMA "Formularies";
|
|
|
|
CREATE SCHEMA "Formularies" AUTHORIZATION root;
|
|
|
|
-- "Formularies".usp_dc_2023 definition
|
|
|
|
-- Drop table
|
|
|
|
-- DROP TABLE "Formularies".usp_dc_2023;
|
|
|
|
CREATE TABLE "Formularies".usp_dc_2023 (
|
|
rxcui varchar(15) NULL, --yes even though this is a number, it is represented as a string elsewhere.
|
|
tty varchar(10) NULL,
|
|
"Name" varchar(256) NULL,
|
|
"Related BN" varchar(250) NULL,
|
|
"Related DF" varchar(25050) NULL,
|
|
"USP Category" varchar(250) NULL,
|
|
"USP Class" varchar(250) NULL,
|
|
"USP Pharmacotherapeutic Group" varchar(250) NULL,
|
|
"API Concept" varchar(250) NULL
|
|
);
|
|
|
|
/*
|
|
I then linked the data back on itself with a materialized view, using claude.ai for simplicity.
|
|
|
|
Claude.ai > I need a postres sql statement to create a materialized view that will take the following table and link from a given rxcui to the other rxcui's that share the same category and class
|
|
|
|
```sql
|
|
CREATE TABLE "Formularies".usp_dc_2023 (
|
|
rxcui int4 NULL,
|
|
tty varchar(10) NULL,
|
|
"Name" varchar(256) NULL,
|
|
"Related BN" varchar(250) NULL,
|
|
"Related DF" varchar(25050) NULL,
|
|
"USP Category" varchar(250) NULL,
|
|
"USP Class" varchar(250) NULL,
|
|
"USP Pharmacotherapeutic Group" varchar(250) NULL,
|
|
"API Concept" varchar(250) NULL
|
|
);
|
|
```
|
|
*/
|
|
CREATE MATERIALIZED VIEW "Formularies".rxcui_category_class_links AS
|
|
WITH base AS (
|
|
SELECT DISTINCT
|
|
a.rxcui as source_rxcui,
|
|
b.rxcui as linked_rxcui,
|
|
a."USP Category" as category,
|
|
a."USP Class" as class
|
|
FROM "Formularies".usp_dc_2023 a
|
|
JOIN "Formularies".usp_dc_2023 b
|
|
ON a."USP Category" = b."USP Category"
|
|
AND a."USP Class" = b."USP Class"
|
|
AND a.rxcui != b.rxcui
|
|
WHERE a.rxcui IS NOT NULL
|
|
AND b.rxcui IS NOT NULL
|
|
)
|
|
SELECT * FROM base;
|
|
|
|
-- Add indexes for better query performance
|
|
CREATE INDEX ON "Formularies".rxcui_category_class_links (source_rxcui);
|
|
CREATE INDEX ON "Formularies".rxcui_category_class_links (linked_rxcui);
|
|
|
|
/*
|
|
Next step is linking a given nct -> compounds -> formulary alternatives -> compounds -> brands/generics.
|
|
I'll' break this into two steps.
|
|
|
|
1. link formulary alternatives to compounds and brands,
|
|
2. link nct_id to formulary alternatives
|
|
*/
|
|
create materialized view "Formularies".rxcui_to_brand_through_uspdc AS
|
|
select
|
|
rccl.source_rxcui
|
|
,rccl.linked_rxcui
|
|
,rccl.category
|
|
,rccl."class"
|
|
,rr.tty1
|
|
,rr.tty2
|
|
,rr.rxcui2
|
|
from "Formularies".rxcui_category_class_links rccl
|
|
join rxnorm_migrated.rxnorm_relations rr on rr.rxcui1 = rccl.linked_rxcui
|
|
where rr.tty2 = 'BN'
|
|
;
|
|
|
|
|
|
create materialized view match_trial_compound_to_alternate_bn_rxcuis as
|
|
select distinct mttbi.nct_id, rtbtu.rxcui2
|
|
from match_trials_to_bn_in mttbi
|
|
join "Formularies".rxcui_to_brand_through_uspdc rtbtu
|
|
on mttbi.bn_or_in_cui = rtbtu.rxcui2
|
|
|
|
/*
|
|
Now I need to create a way to link
|
|
*/
|