R/genomics.R

Defines functions genomics

Documented in genomics

#' Create a dataframe of genomic data from a tumor registry that can be combined into a storyboard
#' @description
#' `genomics()` wrangles data from the Genoimcs form of tumor registries to produce a dataframe of details about genomic data from tumors or blood, which can then be incorporated into a Patient Storyboard
#' @param data is a data frame which contains the data for which you want to create a storyboard
#' @return A data frame with five variables ("record_id", "description", "value", "date", and "hover" that can be combined with others dfs with the same five variables to form a storyboard
#' @export
#' @examples
#' # Test with embedded data set "storyboard_dataset"
#' storyboard_dataset %>%
#'   genomics()
#'

genomics <- function(data){
  ##########################################################################################################################
  # Load Data
  ##########################################################################################################################
  dt <- data
  ##########################################################################################################################
  # Replace values with strings
  ##########################################################################################################################
  dt$genomics_tissue_type <- replace(dt$genomics_tissue_type, dt$genomics_tissue_type == 1, "Primary Cutaneous")
  dt$genomics_tissue_type <- replace(dt$genomics_tissue_type, dt$genomics_tissue_type == 2, "Metastases")
  dt$genomics_tissue_type <- replace(dt$genomics_tissue_type, dt$genomics_tissue_type == 3, "MCCUP")
  dt$genomics_tissue_type <- replace(dt$genomics_tissue_type, dt$genomics_tissue_type == 4, "Local Recurrence")
  dt$genomics_tissue_type <- replace(dt$genomics_tissue_type, dt$genomics_tissue_type == 5, "Liquid Biopsy")

  dt$genomics_platform <- replace(dt$genomics_platform, dt$genomics_platform == 1, "BWH OncoPanel")
  dt$genomics_platform <- replace(dt$genomics_platform, dt$genomics_platform == 2, "MGH SNaPshot")
  dt$genomics_platform <- replace(dt$genomics_platform, dt$genomics_platform == 3, "MSK Impact")
  dt$genomics_platform <- replace(dt$genomics_platform, dt$genomics_platform == 4, "Foundation One CDx")
  dt$genomics_platform <- replace(dt$genomics_platform, dt$genomics_platform == 5, "Tempus xT Gene Panel")
  dt$genomics_platform <- replace(dt$genomics_platform, dt$genomics_platform == 6, "Guardant 360")

  ##########################################################################################################################
  ##########################################################################################################################
  ################################################# New Data Frame  ########################################################
  ##########################################################################################################################
  ##########################################################################################################################

  ##########################################################################################################################
  # Create a new dataframe of relevant variables for Gene Variants
  ##########################################################################################################################
  # Create DFs of gene variants
  variants.gene <- dt %>%
    select(record_id,
           lesion_tag_genomics,
           genomics_tissue_type,
           redcap_repeat_instance,
           contains("variant_gene"))

  ##########################################################################################################################
  ##########################################################################################################################
  ################################################# New Data Frame  ########################################################
  ##########################################################################################################################
  ##########################################################################################################################

  ##########################################################################################################################
  # Create a new dataframe of of the long form of Gene Variants
  ##########################################################################################################################
  variants.gene.g <- variants.gene %>%
    gather(key = "variable",
           value = "gene",
           -record_id,
           -lesion_tag_genomics,
           -genomics_tissue_type,
           -redcap_repeat_instance) %>%
    drop_na("gene")
  ##########################################################################################################################
  # Drop those rows with cfDNA b/c there is a numeric in the column "gene", which is not what we want
  ##########################################################################################################################
  variants.gene.g <- variants.gene.g %>%
    filter(!stringr::str_detect(string = variants.gene.g$variable,
                               pattern = "cfdna"))


  ##########################################################################################################################
  ##########################################################################################################################
  ################################################# New Data Frame  ########################################################
  ##########################################################################################################################
  ##########################################################################################################################

  ##########################################################################################################################
  # Separate "variable" isolate the numeric suffix, we will come back to this data frame
  ##########################################################################################################################
  variants.gene.g.split <-  tidyr::separate(
    data = variants.gene.g,
    col = variable,
    into = c("text","number"),
    sep = "variant_gene_"
    ) %>%
    select(-text) ## let's get rid of this column as it is blank

  ##########################################################################################################################
  ##########################################################################################################################
  ################################################# New Data Frame  ########################################################
  ##########################################################################################################################
  ##########################################################################################################################

  ##########################################################################################################################
  # Create a new dataframe of the relevant variables for Protein Variants
  ##########################################################################################################################
  variants.protein <- dt %>%
    select(record_id,
           lesion_tag_genomics,
           genomics_tissue_type,
           redcap_repeat_instance,
           contains("variant_protein")
           )
  ##########################################################################################################################
  ##########################################################################################################################
  ################################################# New Data Frame  ########################################################
  ##########################################################################################################################
  ##########################################################################################################################

  ##########################################################################################################################
  # Create a new dataframe of of the long form of Protein Variants
  ##########################################################################################################################
  variants.protein.g <- tidyr::gather(
    data = variants.protein,
    key = "variable",
    value = "protein",
    -record_id,
    -lesion_tag_genomics,
    -genomics_tissue_type,
    -redcap_repeat_instance) %>%
    drop_na("protein")
  ##########################################################################################################################
  # Separate "variable" isolate the numeric suffix, we will come back to this data frame
  ##########################################################################################################################
  variants.protein.g.split <- tidyr::separate(
    data = variants.protein.g,
    col = variable,
    into = c("text","number"),
    sep = "variant_protein_"
    ) %>%
    select(-text) ## let's get rid of this column as it is blank

  ##########################################################################################################################
  ##########################################################################################################################
  ################################################# New Data Frame  ########################################################
  ##########################################################################################################################
  ##########################################################################################################################

  ##########################################################################################################################
  # Create a new dataframe of the relevant variables for Nucleotide Variants
  ##########################################################################################################################
  variants.nucleotide <- dt %>%
    select(record_id,
           lesion_tag_genomics,
           genomics_tissue_type,
           redcap_repeat_instance,
           contains("variant_nucleotide"))
  ##########################################################################################################################
  ##########################################################################################################################
  ################################################# New Data Frame  ########################################################
  ##########################################################################################################################
  ##########################################################################################################################

  ##########################################################################################################################
  # Create a new dataframe of of the long form of Nucleotide Variants
  ##########################################################################################################################
  variants.nucleotide.g <- tidyr::gather(
    data = variants.nucleotide,
    key = "variable",
    value = "nucleotide",
    -record_id,
    -lesion_tag_genomics,
    -genomics_tissue_type,
    -redcap_repeat_instance) %>%
    drop_na("nucleotide")
  ##########################################################################################################################
  # Separate "variable" isolate the numeric suffix, we will come back to this data frame
  ##########################################################################################################################
  variants.nucleotide.g.split <- tidyr::separate(
    data = variants.nucleotide.g,
    col = variable,
    into = c("text","number"),
    sep = "variant_nucleotide_"
  ) %>%
    select(-text) ## let's get rid of this column as it is blank

  ##########################################################################################################################
  ##########################################################################################################################
  ################################################# New Data Frame  ########################################################
  ##########################################################################################################################
  ##########################################################################################################################

  ##########################################################################################################################
  # Create a table of metadata
  ##########################################################################################################################
  genomics.meta <- dt %>%
    dplyr::select(record_id,
                  lesion_tag_genomics,
                  genomics_tissue_type,
                  genomics_platform,
                  redcap_repeat_instance) %>%
    drop_na(lesion_tag_genomics)

  ##########################################################################################################################
  ##########################################################################################################################
  ################################################# New Data Frame  ########################################################
  ##########################################################################################################################
  ##########################################################################################################################

  ##########################################################################################################################
  # Combine the above tables with gene, protein and nucelotide data
  ##########################################################################################################################
  snv <- dplyr::left_join(variants.gene.g.split,
                          variants.protein.g.split,
                          by =c("record_id" = "record_id",
                                "lesion_tag_genomics" = "lesion_tag_genomics",
                                "genomics_tissue_type" = "genomics_tissue_type",
                                "number" = "number",
                                "redcap_repeat_instance" = "redcap_repeat_instance"))
  snv <- left_join(snv,
                   variants.nucleotide.g.split,
                   by =c("record_id" = "record_id",
                         "lesion_tag_genomics" = "lesion_tag_genomics",
                         "genomics_tissue_type" = "genomics_tissue_type",
                         "number" = "number",
                         "redcap_repeat_instance" = "redcap_repeat_instance"))
  ##########################################################################################################################
  # arrange snv by gene
  ##########################################################################################################################
  snv <- snv %>%
    arrange(gene)
  ##########################################################################################################################
  # Delete everything before the period in the nucleotide column
  ##########################################################################################################################
  snv$nucleotide  <- gsub("^.*\\.",
                          "",
                          snv$nucleotide)
  ##########################################################################################################################
  # Delete everything before the period in the protein column
  ##########################################################################################################################
  # Delete everything before the period in the protein column
  snv$protein  <- gsub("^.*\\.",
                       "",
                       snv$protein)
  ##########################################################################################################################
  #   #Drop the number column
  ##########################################################################################################################
  snv <- snv %>%
    select(-contains("number"))

  ##########################################################################################################################
  ##########################################################################################################################
  ################################################# New Data Frame  ########################################################
  ##########################################################################################################################
  ##########################################################################################################################

  ##########################################################################################################################
  # New data frame to count the number of copy number variations
  ##########################################################################################################################
  cnv <- dt %>%
    select(record_id,
           lesion_tag_genomics,
           genomics_tissue_type,
           redcap_repeat_instance,
           contains("cnv_gene"))

  cnv.g <- cnv %>%
    gather(key = "variable",
           value = "gene",
           -record_id,
           -lesion_tag_genomics,
           -genomics_tissue_type,
           -redcap_repeat_instance) %>%
    drop_na("gene")
  ##########################################################################################################################
  #Add a vector for "Yes" to help ID which genes had CNVs as well
  ##########################################################################################################################
  cnv.g$cnv_yn <- "Yes"
  # drop variable
  cnv.g <- cnv.g %>% select(-variable)

  ##########################################################################################################################
  ##########################################################################################################################
  ################################################# New Data Frame  ########################################################
  ##########################################################################################################################
  ##########################################################################################################################

  ##########################################################################################################################
  # Full join CNV.g to SNV
  ##########################################################################################################################
  genomics.full_join <- dplyr::full_join(snv,
                                         cnv.g,
                                         by = c("record_id" = "record_id",
                                                "lesion_tag_genomics" = "lesion_tag_genomics",
                                                "genomics_tissue_type" = "genomics_tissue_type",
                                                "gene" = "gene",
                                                "redcap_repeat_instance" = "redcap_repeat_instance")) %>%
    dplyr::arrange(gene)  # were using a full join b/c we want to be able to see rows for CNV only
  ##########################################################################################################################
  # Replace NA with "No" for cnv_yn
  ##########################################################################################################################
  genomics.full_join$cnv_yn[is.na(genomics.full_join$cnv_yn)] <- "No"

  ##########################################################################################################################
  ##########################################################################################################################
  ################################################# New Data Frame  ########################################################
  ##########################################################################################################################
  ##########################################################################################################################

  ##########################################################################################################################
  # Full join genomics.full_join with meta data and select relevant variables
  ##########################################################################################################################
  genomics.full_join.meta <- dplyr::full_join(genomics.full_join,
                                              genomics.meta) %>%
    select(record_id,
           lesion_tag_genomics,
           genomics_tissue_type,
           genomics_platform,
           redcap_repeat_instance,
           gene)
  ##########################################################################################################################
  ##########################################################################################################################
  ################################################# New Data Frame  ########################################################
  ##########################################################################################################################
  ##########################################################################################################################

  ##########################################################################################################################
  # New Df to obtain Date of the genomics test
  ##########################################################################################################################
  genomics.date <- dt %>%
    select(record_id,
           lesion_tag_genomics,
           genomics_date,
           redcap_repeat_instance) %>%
    drop_na(lesion_tag_genomics)

  ##########################################################################################################################
  ##########################################################################################################################
  ################################################# New Data Frame  ########################################################
  ##########################################################################################################################
  ##########################################################################################################################

  ##########################################################################################################################
  # Full Join of genomics.full_join.meta and genomics.date
  ##########################################################################################################################
  genomics.full_join.meta.date <- dplyr::left_join(genomics.full_join.meta,
                                                   genomics.date,
                                                   by = c("record_id",
                                                          "lesion_tag_genomics",
                                                          "redcap_repeat_instance"))

  ##########################################################################################################################
  ##########################################################################################################################
  ################################################# New Data Frame  ########################################################
  ##########################################################################################################################
  ##########################################################################################################################

  ##########################################################################################################################
  # New DF creating a new variable that pastes the genes from a given test into one cell
  ##########################################################################################################################
  genomics.pivot.wider <- genomics.full_join.meta.date %>%
    group_by(record_id,
             lesion_tag_genomics,
             genomics_date,
             redcap_repeat_instance) %>%
    mutate(genomic_alterations = paste0(gene,
                                        collapse = ", ")) %>%
    ungroup()
  ##########################################################################################################################
  # If there were no alterations detected, replace with "None Detected"
  ##########################################################################################################################
  genomics.pivot.wider$genomic_alterations[genomics.pivot.wider$genomic_alterations == "NA"] <- "None Detected"

  ##########################################################################################################################
  ##########################################################################################################################
  ################################################# New Data Frame  ########################################################
  ##########################################################################################################################
  ##########################################################################################################################

  ##########################################################################################################################
  # New DF slicing to give one row for each unique test run
  ##########################################################################################################################
  genomics.pivot.wider.slice <- genomics.pivot.wider %>%
    group_by(record_id,
             lesion_tag_genomics,
             genomics_date,
             redcap_repeat_instance) %>%
    slice_head() %>%
    ungroup()
  ##########################################################################################################################
  # Create the relevant variables that will allow us to combine with other Storyboard Data Frames
  ## "date", "description", "value" and "hover"
  ##########################################################################################################################
  genomics.pivot.wider.slice <- genomics.pivot.wider.slice %>%
    mutate(date = as.Date(genomics_date))

  genomics.pivot.wider.slice$description <- "genomics"
  genomics.pivot.wider.slice$value <- "Genomics"

  genomics.pivot.wider.slice$hover.a <- paste("<b>Lesion Assessed:</b>", genomics.pivot.wider.slice$lesion_tag_genomics)
  genomics.pivot.wider.slice$hover.b <- paste("<b>Platform:</b>", genomics.pivot.wider.slice$genomics_platform)
  genomics.pivot.wider.slice$hover.c <- paste("<b>Genetic Alterations:</b>", genomics.pivot.wider.slice$genomic_alterations)
  genomics.pivot.wider.slice$hover.d <- paste("<b>Date:</b>",genomics.pivot.wider.slice$date)
  genomics.pivot.wider.slice$hover <- paste(genomics.pivot.wider.slice$hover.a,
                                            genomics.pivot.wider.slice$hover.b,
                                            genomics.pivot.wider.slice$hover.c,
                                            genomics.pivot.wider.slice$hover.d,
                                            sep = "<br>")

  ##########################################################################################################################
  ##########################################################################################################################
  ################################################# New Data Frame  ########################################################
  ##########################################################################################################################
  ##########################################################################################################################

  ##########################################################################################################################
  # Select the relevant variables for the final dataframe
  ##########################################################################################################################
  genomics.final <- genomics.pivot.wider.slice %>%
  select(record_id,
         description,
         value,
         date,
         hover)
  ##########################################################################################################################
  # Return the final df which is intended to be combined with other Storyboard DFs
  ##########################################################################################################################
  return(genomics.final)

  }
TheMillerLab/StoryboardR documentation built on Jan. 12, 2023, 11:24 a.m.