JuliaHealth / OMOPCDMCohortCreator.jl

Create cohorts from databases utilizing the OMOP CDM
https://juliahealth.org/OMOPCDMCohortCreator.jl/stable
Other
8 stars 7 forks source link

[FEATURE] Add Getter for Procedures Associated with a Visit #57

Closed TheCedarPrince closed 8 months ago

TheCedarPrince commented 9 months ago

The following code gets the procedures associated with someone's visit:

import FunSQL:
  From,
  Where,
  Select,
  Get,
  Fun,
  render,
"""
GetVisitProcedure(visit_ids, conn; tab = procedure_occurrence)

Given a list of visit IDs, find their corresponding procedures.

# Arguments:

- `visit_ids` - list of `visit_id`'s; each ID must be of subtype `Integer`

- `conn` - database connection using DBInterface

# Keyword Arguments:

- `tab` - the `SQLTable` representing the Condition Occurrence table; default `procedure_occurrence`

# Returns

- `df::DataFrame` - a two column `DataFrame` comprised of columns: `:visit_occurrence_id` and `:procedure_concept_id`
"""
function GetVisitProcedure(
    visit_ids,
    conn;
    tab=procedure_occurrence
)

    df = DBInterface.execute(conn, GetVisitProcedure(visit_ids; tab=tab)) |> DataFrame

    return df

end

"""
function GetVisitProcedure(df:DataFrame, conn; tab = procedure_occurrence)

Given a `DataFrame` with a `:visit_occurrence_id` column, return the `DataFrame` with an associated `:procedure_concept_id` for each `visit_occurrence_id` in the `DataFrame`

Multiple dispatch that accepts all other arguments like in `GetVisitProcedure(ids, conn; tab = procedure_occurrence)`
"""

function GetVisitProcedure(
    df::DataFrame,
    conn;
    tab=procedure_occurrence
)

    df_ids= df[:,"procedure_occurrence_id"]

    return outerjoin(GetVisitProcedure(df_ids, conn; tab=tab), df, on = :procedure_occurrence_id)

end

"""
GetVisitProcedure(visit_ids; tab = procedure_occurrence)

Produces SQL statement that, given a list of `visit_id`'s, finds the procedures associated with that visit.

# Arguments:

- `visit_ids` - list of `visit_id`'s; each ID must be of subtype `Integer`

# Keyword Arguments:

- `tab` - the `SQLTable` representing the Procedure Occurrence table; default `procedure_occurrence`

# Returns

- `df::DataFrame` - a two column `DataFrame` comprised of columns: `:visit_occurrence_id` and `:procedure_concept_id`
"""
function GetVisitProcedure(
    visit_ids;
    tab=procedure_occurrence
)

    sql =
        From(tab) |>
        Where(Fun.in(Get.visit_occurrence_id, visit_ids...)) |>
        Select(Get.visit_occurrence_id, Get.procedure_concept_id) |>
        q -> render(q, dialect=dialect)

    return String(sql)

end

I just haven't had the time yet to add this code into CohortCreator as a new feature. Would be nice to add it in. Maybe @Farreeda if you have time/interest?

TheCedarPrince commented 8 months ago

Closed by #59