# Creating ADOE

# Introduction

This article describes creating an ADOE ADaM with Ophthalmology Exam Analysis data for ophthalmology endpoints. It is to be used in conjunction with the article on creating a BDS dataset from SDTM. As such, derivations and processes that are not specific to ADOE are absent, and the user is invited to consult the aforementioned article for guidance.

As the current release of `{admiralophtha}` does not contain any functionality specific to ADOE, this article only showcases how to map parameters from OE in order to set up the basic structure of ADOE. For the following steps, the user is invited to consult the above-linked article on BDS datasets. This article will be updated for future releases to showcase and explain any `{admiralophtha}`-specific functionality.

Note: All examples assume CDISC SDTM and/or ADaM format as input unless otherwise specified.

## Dataset Contents

`{admiralophtha}` suggests to populate ADOE with general miscellaneous ophthalmology parameters. Any efficacy endpoint-related parameters (eg. BCVA tests) should be placed in separate datasets (eg. ADBCVA).

## Required Packages

The examples of this vignette require the following packages.

    library(dplyr)
    library(admiral)
    library(pharmaversesdtm)
    library(admiraldev)
    library(admiralophtha)

# Programming Workflow

- Initial Set Up of ADOE
- Further Derivations of Standard BDS Variables
- Example Script

## Initial set up of ADOE

As with all BDS ADaM datasets, one should start from the OE SDTM, where only the general ophthalmology records are of interest. For the purposes of the next two sections, we shall be using the `{admiral}` OE and ADSL test data. We will also require a lookup table for the mapping of parameter codes.

Note: to simulate an ophthalmology study, we add a randomly generated `STUDYEYE` variable to ADSL, but in practice `STUDYEYE` will already have been derived using `derive_var_studyeye()`.

    data("oe_ophtha")
    data("admiral_adsl")

    # Add STUDYEYE to ADSL to simulate an ophtha dataset
    adsl <- admiral_adsl %>%
      as.data.frame() %>%
      mutate(STUDYEYE = sample(c("LEFT", "RIGHT"), n(), replace = TRUE)) %>%
      convert_blanks_to_na()

    oe <- convert_blanks_to_na(oe_ophtha) %>%
      ungroup()

    # ---- Lookup table ----

    # Assign PARAMCD, PARAM, and PARAMN
    param_lookup <- tibble::tribble(
      ~OETESTCD, ~OECAT, ~OESCAT, ~AFEYE, ~PARAMCD, ~PARAM, ~PARAMN,
      "CSUBTH", "OPHTHALMIC ASSESSMENTS", "SD-OCT CST SINGLE FORM", "Study Eye", "SCSUBTH", "Study Eye Center Subfield Thickness (um)", 1, # nolint
      "CSUBTH", "OPHTHALMIC ASSESSMENTS", "SD-OCT CST SINGLE FORM", "Fellow Eye", "FCSUBTH", "Fellow Eye Center Subfield Thickness (um)", 2, # nolint
      "DRSSR", "OPHTHALMIC ASSESSMENTS", "SD-OCT CST SINGLE FORM", "Study Eye", "SDRSSR", "Study Eye Diabetic Retinopathy Severity", 3, # nolint
      "DRSSR", "OPHTHALMIC ASSESSMENTS", "SD-OCT CST SINGLE FORM", "Fellow Eye", "FDRSSR", "Fellow Eye Diabetic Retinopathy Severity", 4, # nolint
    )

Following this setup, the programmer can start constructing ADOE. The first step is to subset OE to only general ophthalmology parameters. Then, one can merge the resulting dataset with ADSL. This is required for two reasons: firstly, `STUDYEYE` is crucial in the mapping of `AFEYE` and `PARAMCD`’s. Secondly, the treatment start date (`TRTSDT`) is also a prerequisite for the derivation of variables such as Analysis Day (`ADY`).

    adsl_vars <- exprs(TRTSDT, TRTEDT, TRT01A, TRT01P, STUDYEYE)

    adoe <- oe %>%
      filter(
        OETESTCD %in% c("CSUBTH", "DRSSR")
      ) %>%
      derive_vars_merged(
        dataset_add = adsl,
        new_vars = adsl_vars,
        by_vars = exprs(STUDYID, USUBJID)
      )

The next item of business is to derive `AVAL`, `AVALU`, and `DTYPE`. In this example, due to the small number of parameters their derivation is trivial. `AFEYE` is also created in this step using the function `derive_var_afeye()`. To determine the affected eye, this function compares `OELAT` to the `STUDYEYE` variable created from the previous step.

    adoe <- adoe %>%
      # Calculate AVAL, AVALC, AVALU and DTYPE
      mutate(
        AVAL = OESTRESN,
        AVALC = OESTRESC,
        AVALU = OESTRESU,
        DTYPE = NA_character_
      ) %>%
      # Derive AFEYE needed for PARAMCD derivation
      derive_var_afeye(loc_var = OELOC, lat_var = OELAT, loc_vals = c("EYE", "RETINA"))

## Further Derivations of Standard BDS Variables

The user is invited to consult the article on creating a BDS dataset from SDTM to learn how to add standard BDS variables to ADOE.

## Example Script

ADaM | Sample Code
--- | ---
ADOE | ad_adoe.R