
/* OVERVIEW
 * 
 * This links trials to the first date each drug (indexed by NDA/ANDA etc) is
 * put on the market.
 * 
 * It takes 3 views to build up to it.
 * */

--Match trials to brands and ingredients
create or replace view public.match_trials_to_bn_in as
with trialncts as (
        SELECT DISTINCT nct_id  FROM history.trial_snapshots TS
    )
SELECT 
    bi.nct_id ,
    bi.downcase_mesh_term, 
    rr.tty2 ,
    rr.rxcui2 as bn_or_in_cui --brand or ingredient
    ,count(*)
FROM ctgov.browse_interventions bi
    left outer JOIN rxnorm_migrated.rxnorm_props AS rp
        on bi.downcase_mesh_term  = rp.propvalue1 --link names to drug cuis ()
    left outer join rxnorm_migrated.rxnorm_relations rr 
    	on rr.rxcui1 = rp.rxcui 
WHERE
    bi.nct_id in (
        SELECT nct_id  FROM trialncts
        )
    and
    bi.mesh_type='mesh-list'
    and rp.propname = 'Active_ingredient_name'
    and rr.tty2 in ('BN', 'IN', 'MIN')
group by bi.nct_id, bi.downcase_mesh_term , rr.tty2 ,rr.rxcui2 
order by bi.nct_id 
;
--running out of space.

-- get list of interventions assoicated with trials of interest
create temp table tmp_interventions as
select * from ctgov.browse_interventions bi 
where 
	bi.mesh_type ='mesh-list'
	and
	bi.nct_id in (select distinct nct_id from history.trial_snapshots)
;
select * from tmp_interventions;

--drop table tmp_join_interv_rxcui;
create temp table tmp_join_interv_rxcui as
select *
from
	tmp_interventions tint
	inner join
	rxnorm_migrated.rxnorm_props rp 
		on tint.downcase_mesh_term = rp.propvalue1 
where propname='RxNorm Name'
;-- get the rxcui for ingredients

select * from tmp_join_interv_rxcui;

--filter rxcui -> is human prescribable
create temp view tmp_view_prescribable as
select count(*) from rxnorm_migrated.rxnorm_props rp 
where 
	rp.propname = 'PRESCRIBABLE' 
	and 
	rp.propvalue1 = 'Y'
;

--link prescribable to brand ingredients or brand names.
	

--get relationships of IN -> BN
select *
from 
	rxnorm_migrated.rxnorm_relations rr 
where 
	rr.tty1 in ('IN','MIN')
	and rr.rxcui1 in (select distinct rxcui from tmp_join_interv_rxcui tjir)
	and rr.tty2 = 'BN'
;



--match trials to through brands NDC11
create or replace view public.match_trial_to_ndc11 as
select 
	mttbi.nct_id,
	ah.ndc,
	count(*)
from public.match_trials_to_bn_in as mttbi
    left outer join rxnorm_migrated.rxnorm_relations as rr 
    	on mttbi.bn_or_in_cui = rr.rxcui1
    left outer join rxnorm_migrated."ALLNDC_HISTORY" as ah 
        on rr.rxcui2 = ah.rxcui 
where 
    rr.tty1 = 'BN' 
    and
    rr.tty2 in ('SBD', 'BPCK')
    and 
    ah.sab='RXNORM' 
group by mttbi.nct_id, ah.ndc
order by mttbi.nct_id, ah.ndc
;



---associate trials to marketing start dates
create or replace view public.match_trial_to_marketing_start_date as
select 
	mttn.nct_id,
	n.application_number_or_citation,
	min(n.marketing_start_date )
from match_trial_to_ndc11 mttn 
	inner join spl.nsde n 
		on mttn.ndc = n.package_ndc11 
where 	
	n.product_type = 'HUMAN PRESCRIPTION DRUG'
	and
	n.marketing_category in ('NDA','ANDA','BLA', 'NDA authorized generic', 'NDA AUTHORIZED GENERIC')
group by mttn.nct_id,n.application_number_or_citation 
order by mttn.nct_id 
;


