From f26f7877fa51d03cf376d3310ea22249e236c6ad Mon Sep 17 00:00:00 2001 From: Prabhat Rayapati Date: Fri, 9 Sep 2016 16:14:59 -0400 Subject: [PATCH 1/3] Adult patients and corresponding hospital admissions This MIMIC-III V1.3 --- cookbook/postgres/AdultPatients.sql | 65 +++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 cookbook/postgres/AdultPatients.sql diff --git a/cookbook/postgres/AdultPatients.sql b/cookbook/postgres/AdultPatients.sql new file mode 100644 index 0000000..a9c016c --- /dev/null +++ b/cookbook/postgres/AdultPatients.sql @@ -0,0 +1,65 @@ +-- ------------------------------------------------------------- +-- Title: Identify Adult Patients/Hospital Admissions (>18 years of age) +-- Description: This query create a table of Adult patients and their corresponding hospital admission ID, adult +-- is definied as age greater than or equal to 18 years. The table produced by this script called "adultpatients" contains the subject_id +-- and the corresponding hadm_id of adult patients. +-- MIMIC version: MIMIC-III v1.3 +-- This query does not specify a schema, and can be directly run on your data +-- Script Author- Prabhat Rayapati +-- contact- pr2sn@virginia.edu +--------------------------------------------------------------- + +-- create a copy of icustays called icustays_1 +CREATE TABLE icustays_1 (LIKE icustays); +INSERT INTO icustays_1 +SELECT * +FROM icustays; + +-- add the dob column to icustays_1, copy the dob data from the patients table +ALTER TABLE icustays_1 +ADD dob date; + +UPDATE icustays_1 +SET dob=patients.dob +FROM patients +WHERE icustays_1.subject_id=patients.subject_id; + +--Now lets calculate the age of the patients at the time of icu admission + +ALTER TABLE icustays_1 +ADD age int; + +UPDATE icustays_1 +SET age=date_part('year',age(cast(intime as date),dob)); + +-- the age of people over 89 years is scwered +-- so replace the age of anyone over 89years with 91, since the median age +-- of shifted age is 91.4 + +UPDATE icustays_1 +SET age=91 +WHERE age>89; + + -- Now lets create a table of adult patients only, and their corresponding hospital admissions + CREATE TABLE icustays_2 (LIKE icustays_1); + + INSERT INTO icustays_2 + SELECT * + FROM icustays_1; + DELETE FROM icustays_2 + WHERE age<18; + +-- creating a table with the distinct adult hospital admissions + CREATE TABLE adultpatients( + hadm_id int); + INSERT INTO adultpatients + SELECT distinct hadm_id + FROM icustays_2; + +-- create the table that contains the adult patients subject id and their hospital admission id + ALTER TABLE adultpatients + ADD subject_id int; + UPDATE adultpatients + SET subject_id=admissions.subject_id + FROM admissions + WHERE adultpatients.hadm_id=admissions.hadm_id; \ No newline at end of file From 6ad4597d8d7a1975baa023f21e48141a352cfa13 Mon Sep 17 00:00:00 2001 From: Prabhat Rayapati Date: Fri, 9 Sep 2016 18:54:55 -0400 Subject: [PATCH 2/3] suspected Infection identifying patients with suspected infection and the time of start of infection --- cookbook/postgres/Infections.sql | 346 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 346 insertions(+) create mode 100644 cookbook/postgres/Infections.sql diff --git a/cookbook/postgres/Infections.sql b/cookbook/postgres/Infections.sql new file mode 100644 index 0000000..05ad7f5 --- /dev/null +++ b/cookbook/postgres/Infections.sql @@ -0,0 +1,346 @@ +-------------------------------------------------------------------------------------------------- +-- Title: Identify adult patients with an Infection and time of suspected infection +-- +-- Description: This script produces a table view of patients with infection, +-- their corresponding hospital admission id and start time of the infection. +-- The definition followed to identify patients with infection here is the popular definition used in +-- many science journals, patients who meet the criteria of having a blood culture acquisition in sequence +-- with antibiotics administration within 24 hours prior to the culture through 72 hours after the culture are +-- identified as having infection. For each episode of antibiotic administration and blood culture acquisition +-- the earlier time was considered as the time of infection onset. This definition is usually used in sepsis papers. +-- +-- Reference: Assessment of clinical criteria for sepsis -christopher w. seymour, et.al. +-- MIMIC version- MIMIC-III V1.3 +-- This query script uses the adultpatients view generated by the AdultPatients.sql file +-- +-- Author: Prabhat Rayapati +-- contact: pr2sn@virginia.edu +----------------------------------------------------------------------------------------------- + +------------------------------------------------------------------------------- +-- Identify antibiotics in the prescriptions table -------------------------- +------------------------------------------------------------------------------- + +--lets create duplicate of prescriptions table with only entries for the patients +-- in adultpatients table + +CREATE TABLE prescriptions_2 (LIKE prescriptions); +INSERT INTO prescriptions_2 +SELECT prescriptions.* +FROM prescriptions, adultpatients +WHERE prescriptions.hadm_id = adultpatients.hadm_id; + +-- extract the records of the patients who got antibiotics +-- prescription into the table antibiotics_prescriptions + +CREATE TABLE antibiotics_prescriptions (LIKE prescriptions_2); +INSERT INTO antibiotics_prescriptions +SELECT * +FROM prescriptions_2 +WHERE (lower(drug) like '%ampicillin-sulbactam%' or + lower(drug) like '%amoxicillin-clavulanic%' or + lower(drug) like '%amikacin%' or + lower(drug) like '%augmentin suspension%' or + lower(drug) like '%azithromycin%' or + lower(drug) like '%aztreonam%' or + lower(drug) like '%bactrim%' or + lower(drug) like '%cefepime%' or + lower(drug) like '%cefixime%' or + lower(drug) like '%cefotaxime %' or + lower(drug) like '%cefotetan%' or + lower(drug) like '%cefoxitin%' or + lower(drug) like '%cefpodoxime %' or + lower(drug) like '%ceftazidime%' or + lower(drug) like '%ceftriaxone%' or + lower(drug) like '%cefuroxime%' or + lower(drug) like '%ciprofloxacin%' or + lower(drug) like '%clarithromycin%' or + lower(drug) like '%clindamycin%' or + lower(drug) like '%colistin%' or + lower(drug) like '%daptomycin%' or + lower(drug) like '%doxycycline %' or + lower(drug) like '%ertapenem%' or + lower(drug) like '%erythromycin%' or + lower(drug) like '%gatifloxacin%' or + lower(drug) like '%gentamicin%' or + lower(drug) like '%imipenem%' or + lower(drug) like '%levofloxacin%' or + lower(drug) like '%linezolid%' or + lower(drug) like '%meropenem%' or + lower(drug) like '%metronidazole%' or + lower(drug) like '%minocycline%' or + lower(drug) like '%moxifloxacin%' or + lower(drug) like '%piperacillin%' or + lower(drug) like '%quinupristin%' or + lower(drug) like '%synercid%' or + lower(drug) like '%tetracycline%' or + lower(drug) like '%tigecycline%' or + lower(drug) like '%timentin%' or + lower(drug) like '%tobramycin%' or + lower(drug) like '%trimethoprim%' or + lower(drug) like '%unasyn%' or + lower(drug) like '%vancomycin%' or + lower(drug) like '%abacavir%' or + lower(drug) like '%lamivudine%' or + lower(drug) like '%zidovudine%' or + lower(drug) like '%acyclovir%' or + lower(drug) like '%amikacin%' or + lower(drug) like '%amoxicillin%' or + lower(drug) like '%amoxicillin-pot%' or + lower(drug) like '%amphotericin%' or + lower(drug) like '%ampicillin%' or + lower(drug) like '%ampicillin%' or + lower(drug) like '%sulbactam%' or + lower(drug) like '%atazanavir%' or + lower(drug) like '%atovaquone%' or + lower(drug) like '%atovaquone%' or + lower(drug) like '%proguanil%' or + lower(drug) like '%azithromycin%' or + lower(drug) like '%aztreonam%' or + lower(drug) like '%chloroquine%' or + lower(drug) like '%ciprofloxacin%' or + lower(drug) like '%ciprofloxacin%' or + lower(drug) like '%ciproflox%' or + lower(drug) like '%clarithromycin%' or + lower(drug) like '%clindamycin%' or + lower(drug) like '%dapsone%' or + lower(drug) like '%daptomycin%' or + lower(drug) like '%darunavir%' or + lower(drug) like '%dicloxacillin%' or + lower(drug) like '%doxycycline%' or + lower(drug) like '%doxycycline-eyelid%' or + lower(drug) like '%doxycycline-multivit/minerals%' or + lower(drug) like '%doxycycline-suncreen-sal%' or + lower(drug) like '%emtricitabine-tenofovir%' or + lower(drug) like '%erythromycin%' or + lower(drug) like '%erythromycin-sulfisoxazole%' or + lower(drug) like '%ethambutol%' or + lower(drug) like '%etravirine%' or + lower(drug) like '%fluconazole%' or + lower(drug) like '%fosfomycin%' or + lower(drug) like '%gentamicin%' or + lower(drug) like '%hepatitis b immune globulin%' or + lower(drug) like '%hydroxychloroquine%' or + lower(drug) like '%immune globulin%' or + lower(drug) like '%isoniazid%' or + lower(drug) like '%isoniazid-rifampin%' or + lower(drug) like '%isoniazid-rifamp-pyrazinamide%' or + lower(drug) like '%itraconazole%' or + lower(drug) like '%ketoconazole%' or + lower(drug) like '%lamivudine-zidovudine%' or + lower(drug) like '%ledipasvir%' or + lower(drug) like '%ledipasvir-sofosbuvir%' or + lower(drug) like '%linezolid%' or + lower(drug) like '%metronidazole%' or + lower(drug) like '%minocycline%' or + lower(drug) like '%moxifloxacin%' or + lower(drug) like '%nafcillin%' or + lower(drug) like '%neomycin%' or + lower(drug) like '%neomycin-polymyxin%' or + lower(drug) like '%nitrofurantoin%' or + lower(drug) like '%nystatin%' or + lower(drug) like '%nystatin-hydrocort-zinc%' or + lower(drug) like '%oseltamivir%' or + lower(drug) like '%palivizumab%' or + lower(drug) like '%penicillin%' or + lower(drug) like '%pentamidine%' or + lower(drug) like '%piperacillin-tazobactam%' or + lower(drug) like '%posaconazole%' or + lower(drug) like '%pyrimethamine%' or + lower(drug) like '%rabies immune globulin%' or + lower(drug) like '%raltegravir%' or + lower(drug) like '%ribavirin%' or + lower(drug) like '%ribavirin-interferon%' or + lower(drug) like '%rifampin%' or + lower(drug) like '%rifaximin%' or + lower(drug) like '%ritonavir%' or + lower(drug) like '%sulfamethoxazole%' or + lower(drug) like '%trimethoprim%' or + lower(drug) like '%tenofovir%'); + +-- SANITY CHECK: Just to make sure that only distinct records exist in the antibiotics_prescriptions table, lets +-- insert distinct records into antibiotics_prescriptions_table + +CREATE TABLE antibiotics_prescriptions_table (LIKE antibiotics_prescriptions); +INSERT INTO antibiotics_prescriptions_table +SELECT DISTINCT * +FROM antibiotics_prescriptions; + +----------------------------------------------------------------------------- +--- BLOOD CULTURES information from the tables: microbiologyevents, chartevents, +--- procedureevents_mv and labevents +------------------------------------------------------------------------------ + +CREATE TABLE microbiology_adult_admissions (LIKE microbiologyevents); +INSERT INTO microbiology_adult_admissions +SELECT microbiologyevents.* +FROM microbiologyevents, adultpatients +WHERE microbiologyevents.hadm_id=adultpatients.hadm_id; + +-- lets update the NULL values in charttime column with values in column chartdate + +UPDATE microbiology_adult_admissions +SET charttime=chartdate +WHERE charttime ISNULL; + +-- lets start by creating a blood culture tables for records of interest in each table +-- that contains the information subject_id, hadm_id, itemid, and charttime +-- lets create one table for each table and then merge them + +CREATE TABLE blood_culture_mv( +Subject_id int, +Hadm_id int, +Itemid int, +Charttime timestamp(0)); + +-- lets only input the blood culture items from microbiology_adult_admissions +-- into blood_culture_mv, spec_item=70012 is the blood cult item in microbiologyevents table +INSERT INTO blood_culture_mv +SELECT subject_id, hadm_id,spec_itemid,charttime +FROM microbiology_adult_admissions +WHERE spec_itemid=70012; + +-- The blood_culture has duplicates because blood culture is performed for diff bacteria at the same time in +-- microbiology so we add only the distinct records from blood_culture_mv to the final blood_culture_events table + +CREATE TABLE blood_culture_events(LIKE blood_culture_mv); +INSERT INTO blood_culture_events +SELECT DISTINCT * +FROM blood_culture_mv; + +-- next, lets create the adult records for chartevents table +CREATE TABLE chartevents_adult_admissions (LIKE chartevents); +INSERT INTO chartevents_adult_admissions +SELECT chartevents.* +FROM chartevents, adultpatients +WHERE chartevents.hadm_id=adultpatients.hadm_id; + +----------------------------------------------------- +----------------------------------------------------- +-- lets index the chartevents_adult_admissions table, this will be useful later when calculating +-- sofa score, itll speed up the process + +drop index IF EXISTS CHARTEVENTS_adult_admissions_idx01; +CREATE INDEX CHARTEVENTS_adult_admissions_idx01 + ON chartevents_adult_admissions (SUBJECT_ID); + +drop index IF EXISTS CHARTEVENTS_adult_admissions_idx02; +CREATE INDEX CHARTEVENTS_adult_admissions_idx02 + ON chartevents_adult_admissions (ITEMID); + +drop index IF EXISTS CHARTEVENTS_adult_admissions_idx04; +CREATE INDEX CHARTEVENTS_adult_admissions_idx04 + ON chartevents_adult_admissions (HADM_ID); + +------------------------------------------------------- +------------------------------------------------------- + +-- Next create a blood_culture_chart table like blood_culture_mv +-- and insert the blood culture records from +-- chartevents_adult_admissions, +-- before entering into blood_culture_events + +CREATE TABLE blood_culture_chart (LIKE blood_culture_mv); +INSERT INTO blood_culture_chart +SELECT subject_id, hadm_id,itemid,charttime +FROM chartevents_adult_admissions +WHERE itemid in (3333,938,942); +-- only 6 blood culture records + +INSERT INTO blood_culture_events +SELECT DISTINCT * +FROM blood_culture_chart; + +-- next do the same on procedureevnets_mv table +CREATE TABLE procedureevents_mv_adult_admissions (LIKE procedureevents_mv); +INSERT INTO procedureevents_mv_adult_admissions +SELECT procedureevents_mv.* +FROM procedureevents_mv,adultpatients +WHERE procedureevents_mv.hadm_id=adultpatients.hadm_id; + +-- Next create a blood_culture_proceduremv table like blood_culture_mv +-- and insert the blood culture records +-- from procedureevents_mv_adult_admissions, +-- before entering into blood_culture_events + +CREATE TABLE blood_cul_proceduremv (LIKE blood_culture_mv); +INSERT INTO blood_cul_proceduremv +SELECT subject_id,hadm_id,itemid,starttime +FROM procedureevents_mv_adult_admissions +WHERE itemid=225401; + +INSERT INTO blood_culture_events +SELECT DISTINCT * +FROM blood_cul_proceduremv; + +-- next do the same on labevents +CREATE TABLE labevents_adult_admissions (LIKE labevents); +INSERT INTO labevents_adult_admissions +SELECT labevents.* +FROM labevents, adultpatients +WHERE labevents.hadm_id=adultpatients.hadm_id; + +-- Next create a blood_culture_labevents table like blood_culture_mv +-- and insert the blood culture records from +-- labevents_adult_admissions, +-- before entering into blood_culture_events + +CREATE TABLE blood_culture_labevents (LIKE blood_culture_mv); +INSERT INTO blood_culture_labevents +SELECT subject_id,hadm_id,itemid,charttime +FROM labevents_adult_admissions +WHERE itemid=50886; + +INSERT INTO blood_culture_events +SELECT DISTINCT * +FROM blood_culture_labevents; + +------------------------------------------------------------------------------ +------------------ Identify patients with infections ------------------------- +------------------------------------------------------------------------------ +--- definition: antibiotics-24hrs-blood culture-72hrs-antibiotics -- + +-- lets add a column to antibiotics prescriptions table +-- this column contains the time diff in hours between prescription time +-- and blood culture time + +ALTER TABLE antibiotics_prescriptions_table +ADD time_antibio_bloodculture double precision; + +UPDATE antibiotics_prescriptions_table +SET time_antibio_bloodculture = date_part('day',antibiotics_prescriptions_table.startdate-blood_culture_events.charttime)*24 + date_part('hour', antibiotics_prescriptions_table.startdate-blood_culture_events.charttime) +FROM blood_culture_events +WHERE antibiotics_prescriptions_table.hadm_id=blood_culture_events.hadm_id; + +-- Delete any record that has no entry in the time_antibio_bloodculture column +DELETE FROM antibiotics_prescriptions_table +WHERE time_antibio_bloodculture ISNULL; + +-- lets delete any record where the time diff does not meet the definition of infection +DELETE FROM antibiotics_prescriptions_table +WHERE time_antibio_bloodculture<-24 and time_antibio_bloodculture>72; + +-- lets add the start time of infection +ALTER TABLE antibiotics_prescriptions_table +ADD infection_starttime timestamp(0); + +UPDATE antibiotics_prescriptions_table +SET infection_starttime=blood_culture_events.charttime +FROM blood_culture_events +WHERE antibiotics_prescriptions_table.hadm_id=blood_culture_events.hadm_id; + +UPDATE antibiotics_prescriptions_table +SET infection_starttime=startdate +WHERE time_antibio_bloodculture<0; + +-- now lets create a table infection_patients, +-- this table contains subject_id, hadm_id and the infection start time + +CREATE TABLE infection_patients( +subject_id int, +hadm_id int, +infection_starttime timestamp(0)); + +INSERT INTO infection_patients +SELECT subject_id,hadm_id,infection_starttime +FROM antibiotics_prescriptions_table; \ No newline at end of file From 1e8bd3bedfe034a5997c962d4fc179fbe7e6ad25 Mon Sep 17 00:00:00 2001 From: Prabhat Rayapati Date: Tue, 13 Sep 2016 19:49:51 -0400 Subject: [PATCH 3/3] Delete Infections.sql --- cookbook/postgres/Infections.sql | 346 --------------------------------------- 1 file changed, 346 deletions(-) delete mode 100644 cookbook/postgres/Infections.sql diff --git a/cookbook/postgres/Infections.sql b/cookbook/postgres/Infections.sql deleted file mode 100644 index 05ad7f5..0000000 --- a/cookbook/postgres/Infections.sql +++ /dev/null @@ -1,346 +0,0 @@ --------------------------------------------------------------------------------------------------- --- Title: Identify adult patients with an Infection and time of suspected infection --- --- Description: This script produces a table view of patients with infection, --- their corresponding hospital admission id and start time of the infection. --- The definition followed to identify patients with infection here is the popular definition used in --- many science journals, patients who meet the criteria of having a blood culture acquisition in sequence --- with antibiotics administration within 24 hours prior to the culture through 72 hours after the culture are --- identified as having infection. For each episode of antibiotic administration and blood culture acquisition --- the earlier time was considered as the time of infection onset. This definition is usually used in sepsis papers. --- --- Reference: Assessment of clinical criteria for sepsis -christopher w. seymour, et.al. --- MIMIC version- MIMIC-III V1.3 --- This query script uses the adultpatients view generated by the AdultPatients.sql file --- --- Author: Prabhat Rayapati --- contact: pr2sn@virginia.edu ------------------------------------------------------------------------------------------------ - -------------------------------------------------------------------------------- --- Identify antibiotics in the prescriptions table -------------------------- -------------------------------------------------------------------------------- - ---lets create duplicate of prescriptions table with only entries for the patients --- in adultpatients table - -CREATE TABLE prescriptions_2 (LIKE prescriptions); -INSERT INTO prescriptions_2 -SELECT prescriptions.* -FROM prescriptions, adultpatients -WHERE prescriptions.hadm_id = adultpatients.hadm_id; - --- extract the records of the patients who got antibiotics --- prescription into the table antibiotics_prescriptions - -CREATE TABLE antibiotics_prescriptions (LIKE prescriptions_2); -INSERT INTO antibiotics_prescriptions -SELECT * -FROM prescriptions_2 -WHERE (lower(drug) like '%ampicillin-sulbactam%' or - lower(drug) like '%amoxicillin-clavulanic%' or - lower(drug) like '%amikacin%' or - lower(drug) like '%augmentin suspension%' or - lower(drug) like '%azithromycin%' or - lower(drug) like '%aztreonam%' or - lower(drug) like '%bactrim%' or - lower(drug) like '%cefepime%' or - lower(drug) like '%cefixime%' or - lower(drug) like '%cefotaxime %' or - lower(drug) like '%cefotetan%' or - lower(drug) like '%cefoxitin%' or - lower(drug) like '%cefpodoxime %' or - lower(drug) like '%ceftazidime%' or - lower(drug) like '%ceftriaxone%' or - lower(drug) like '%cefuroxime%' or - lower(drug) like '%ciprofloxacin%' or - lower(drug) like '%clarithromycin%' or - lower(drug) like '%clindamycin%' or - lower(drug) like '%colistin%' or - lower(drug) like '%daptomycin%' or - lower(drug) like '%doxycycline %' or - lower(drug) like '%ertapenem%' or - lower(drug) like '%erythromycin%' or - lower(drug) like '%gatifloxacin%' or - lower(drug) like '%gentamicin%' or - lower(drug) like '%imipenem%' or - lower(drug) like '%levofloxacin%' or - lower(drug) like '%linezolid%' or - lower(drug) like '%meropenem%' or - lower(drug) like '%metronidazole%' or - lower(drug) like '%minocycline%' or - lower(drug) like '%moxifloxacin%' or - lower(drug) like '%piperacillin%' or - lower(drug) like '%quinupristin%' or - lower(drug) like '%synercid%' or - lower(drug) like '%tetracycline%' or - lower(drug) like '%tigecycline%' or - lower(drug) like '%timentin%' or - lower(drug) like '%tobramycin%' or - lower(drug) like '%trimethoprim%' or - lower(drug) like '%unasyn%' or - lower(drug) like '%vancomycin%' or - lower(drug) like '%abacavir%' or - lower(drug) like '%lamivudine%' or - lower(drug) like '%zidovudine%' or - lower(drug) like '%acyclovir%' or - lower(drug) like '%amikacin%' or - lower(drug) like '%amoxicillin%' or - lower(drug) like '%amoxicillin-pot%' or - lower(drug) like '%amphotericin%' or - lower(drug) like '%ampicillin%' or - lower(drug) like '%ampicillin%' or - lower(drug) like '%sulbactam%' or - lower(drug) like '%atazanavir%' or - lower(drug) like '%atovaquone%' or - lower(drug) like '%atovaquone%' or - lower(drug) like '%proguanil%' or - lower(drug) like '%azithromycin%' or - lower(drug) like '%aztreonam%' or - lower(drug) like '%chloroquine%' or - lower(drug) like '%ciprofloxacin%' or - lower(drug) like '%ciprofloxacin%' or - lower(drug) like '%ciproflox%' or - lower(drug) like '%clarithromycin%' or - lower(drug) like '%clindamycin%' or - lower(drug) like '%dapsone%' or - lower(drug) like '%daptomycin%' or - lower(drug) like '%darunavir%' or - lower(drug) like '%dicloxacillin%' or - lower(drug) like '%doxycycline%' or - lower(drug) like '%doxycycline-eyelid%' or - lower(drug) like '%doxycycline-multivit/minerals%' or - lower(drug) like '%doxycycline-suncreen-sal%' or - lower(drug) like '%emtricitabine-tenofovir%' or - lower(drug) like '%erythromycin%' or - lower(drug) like '%erythromycin-sulfisoxazole%' or - lower(drug) like '%ethambutol%' or - lower(drug) like '%etravirine%' or - lower(drug) like '%fluconazole%' or - lower(drug) like '%fosfomycin%' or - lower(drug) like '%gentamicin%' or - lower(drug) like '%hepatitis b immune globulin%' or - lower(drug) like '%hydroxychloroquine%' or - lower(drug) like '%immune globulin%' or - lower(drug) like '%isoniazid%' or - lower(drug) like '%isoniazid-rifampin%' or - lower(drug) like '%isoniazid-rifamp-pyrazinamide%' or - lower(drug) like '%itraconazole%' or - lower(drug) like '%ketoconazole%' or - lower(drug) like '%lamivudine-zidovudine%' or - lower(drug) like '%ledipasvir%' or - lower(drug) like '%ledipasvir-sofosbuvir%' or - lower(drug) like '%linezolid%' or - lower(drug) like '%metronidazole%' or - lower(drug) like '%minocycline%' or - lower(drug) like '%moxifloxacin%' or - lower(drug) like '%nafcillin%' or - lower(drug) like '%neomycin%' or - lower(drug) like '%neomycin-polymyxin%' or - lower(drug) like '%nitrofurantoin%' or - lower(drug) like '%nystatin%' or - lower(drug) like '%nystatin-hydrocort-zinc%' or - lower(drug) like '%oseltamivir%' or - lower(drug) like '%palivizumab%' or - lower(drug) like '%penicillin%' or - lower(drug) like '%pentamidine%' or - lower(drug) like '%piperacillin-tazobactam%' or - lower(drug) like '%posaconazole%' or - lower(drug) like '%pyrimethamine%' or - lower(drug) like '%rabies immune globulin%' or - lower(drug) like '%raltegravir%' or - lower(drug) like '%ribavirin%' or - lower(drug) like '%ribavirin-interferon%' or - lower(drug) like '%rifampin%' or - lower(drug) like '%rifaximin%' or - lower(drug) like '%ritonavir%' or - lower(drug) like '%sulfamethoxazole%' or - lower(drug) like '%trimethoprim%' or - lower(drug) like '%tenofovir%'); - --- SANITY CHECK: Just to make sure that only distinct records exist in the antibiotics_prescriptions table, lets --- insert distinct records into antibiotics_prescriptions_table - -CREATE TABLE antibiotics_prescriptions_table (LIKE antibiotics_prescriptions); -INSERT INTO antibiotics_prescriptions_table -SELECT DISTINCT * -FROM antibiotics_prescriptions; - ------------------------------------------------------------------------------ ---- BLOOD CULTURES information from the tables: microbiologyevents, chartevents, ---- procedureevents_mv and labevents ------------------------------------------------------------------------------- - -CREATE TABLE microbiology_adult_admissions (LIKE microbiologyevents); -INSERT INTO microbiology_adult_admissions -SELECT microbiologyevents.* -FROM microbiologyevents, adultpatients -WHERE microbiologyevents.hadm_id=adultpatients.hadm_id; - --- lets update the NULL values in charttime column with values in column chartdate - -UPDATE microbiology_adult_admissions -SET charttime=chartdate -WHERE charttime ISNULL; - --- lets start by creating a blood culture tables for records of interest in each table --- that contains the information subject_id, hadm_id, itemid, and charttime --- lets create one table for each table and then merge them - -CREATE TABLE blood_culture_mv( -Subject_id int, -Hadm_id int, -Itemid int, -Charttime timestamp(0)); - --- lets only input the blood culture items from microbiology_adult_admissions --- into blood_culture_mv, spec_item=70012 is the blood cult item in microbiologyevents table -INSERT INTO blood_culture_mv -SELECT subject_id, hadm_id,spec_itemid,charttime -FROM microbiology_adult_admissions -WHERE spec_itemid=70012; - --- The blood_culture has duplicates because blood culture is performed for diff bacteria at the same time in --- microbiology so we add only the distinct records from blood_culture_mv to the final blood_culture_events table - -CREATE TABLE blood_culture_events(LIKE blood_culture_mv); -INSERT INTO blood_culture_events -SELECT DISTINCT * -FROM blood_culture_mv; - --- next, lets create the adult records for chartevents table -CREATE TABLE chartevents_adult_admissions (LIKE chartevents); -INSERT INTO chartevents_adult_admissions -SELECT chartevents.* -FROM chartevents, adultpatients -WHERE chartevents.hadm_id=adultpatients.hadm_id; - ------------------------------------------------------ ------------------------------------------------------ --- lets index the chartevents_adult_admissions table, this will be useful later when calculating --- sofa score, itll speed up the process - -drop index IF EXISTS CHARTEVENTS_adult_admissions_idx01; -CREATE INDEX CHARTEVENTS_adult_admissions_idx01 - ON chartevents_adult_admissions (SUBJECT_ID); - -drop index IF EXISTS CHARTEVENTS_adult_admissions_idx02; -CREATE INDEX CHARTEVENTS_adult_admissions_idx02 - ON chartevents_adult_admissions (ITEMID); - -drop index IF EXISTS CHARTEVENTS_adult_admissions_idx04; -CREATE INDEX CHARTEVENTS_adult_admissions_idx04 - ON chartevents_adult_admissions (HADM_ID); - -------------------------------------------------------- -------------------------------------------------------- - --- Next create a blood_culture_chart table like blood_culture_mv --- and insert the blood culture records from --- chartevents_adult_admissions, --- before entering into blood_culture_events - -CREATE TABLE blood_culture_chart (LIKE blood_culture_mv); -INSERT INTO blood_culture_chart -SELECT subject_id, hadm_id,itemid,charttime -FROM chartevents_adult_admissions -WHERE itemid in (3333,938,942); --- only 6 blood culture records - -INSERT INTO blood_culture_events -SELECT DISTINCT * -FROM blood_culture_chart; - --- next do the same on procedureevnets_mv table -CREATE TABLE procedureevents_mv_adult_admissions (LIKE procedureevents_mv); -INSERT INTO procedureevents_mv_adult_admissions -SELECT procedureevents_mv.* -FROM procedureevents_mv,adultpatients -WHERE procedureevents_mv.hadm_id=adultpatients.hadm_id; - --- Next create a blood_culture_proceduremv table like blood_culture_mv --- and insert the blood culture records --- from procedureevents_mv_adult_admissions, --- before entering into blood_culture_events - -CREATE TABLE blood_cul_proceduremv (LIKE blood_culture_mv); -INSERT INTO blood_cul_proceduremv -SELECT subject_id,hadm_id,itemid,starttime -FROM procedureevents_mv_adult_admissions -WHERE itemid=225401; - -INSERT INTO blood_culture_events -SELECT DISTINCT * -FROM blood_cul_proceduremv; - --- next do the same on labevents -CREATE TABLE labevents_adult_admissions (LIKE labevents); -INSERT INTO labevents_adult_admissions -SELECT labevents.* -FROM labevents, adultpatients -WHERE labevents.hadm_id=adultpatients.hadm_id; - --- Next create a blood_culture_labevents table like blood_culture_mv --- and insert the blood culture records from --- labevents_adult_admissions, --- before entering into blood_culture_events - -CREATE TABLE blood_culture_labevents (LIKE blood_culture_mv); -INSERT INTO blood_culture_labevents -SELECT subject_id,hadm_id,itemid,charttime -FROM labevents_adult_admissions -WHERE itemid=50886; - -INSERT INTO blood_culture_events -SELECT DISTINCT * -FROM blood_culture_labevents; - ------------------------------------------------------------------------------- ------------------- Identify patients with infections ------------------------- ------------------------------------------------------------------------------- ---- definition: antibiotics-24hrs-blood culture-72hrs-antibiotics -- - --- lets add a column to antibiotics prescriptions table --- this column contains the time diff in hours between prescription time --- and blood culture time - -ALTER TABLE antibiotics_prescriptions_table -ADD time_antibio_bloodculture double precision; - -UPDATE antibiotics_prescriptions_table -SET time_antibio_bloodculture = date_part('day',antibiotics_prescriptions_table.startdate-blood_culture_events.charttime)*24 + date_part('hour', antibiotics_prescriptions_table.startdate-blood_culture_events.charttime) -FROM blood_culture_events -WHERE antibiotics_prescriptions_table.hadm_id=blood_culture_events.hadm_id; - --- Delete any record that has no entry in the time_antibio_bloodculture column -DELETE FROM antibiotics_prescriptions_table -WHERE time_antibio_bloodculture ISNULL; - --- lets delete any record where the time diff does not meet the definition of infection -DELETE FROM antibiotics_prescriptions_table -WHERE time_antibio_bloodculture<-24 and time_antibio_bloodculture>72; - --- lets add the start time of infection -ALTER TABLE antibiotics_prescriptions_table -ADD infection_starttime timestamp(0); - -UPDATE antibiotics_prescriptions_table -SET infection_starttime=blood_culture_events.charttime -FROM blood_culture_events -WHERE antibiotics_prescriptions_table.hadm_id=blood_culture_events.hadm_id; - -UPDATE antibiotics_prescriptions_table -SET infection_starttime=startdate -WHERE time_antibio_bloodculture<0; - --- now lets create a table infection_patients, --- this table contains subject_id, hadm_id and the infection start time - -CREATE TABLE infection_patients( -subject_id int, -hadm_id int, -infection_starttime timestamp(0)); - -INSERT INTO infection_patients -SELECT subject_id,hadm_id,infection_starttime -FROM antibiotics_prescriptions_table; \ No newline at end of file