## ----set, include = FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.retina = 1
)

## ----lib, echo=FALSE----------------------------------------------------------
library(gDRplots)
library(gDRutils)

## ----sa_data, warning = FALSE, message = FALSE--------------------------------
mae <- get_synthetic_data("combo_matrix")
sa_name <- get_supported_experiments("sa")
se_sa <- mae[[sa_name]]

dt_norm_sa <-
  convert_se_assay_to_dt(se = se_sa,
                         assay_name =  "Normalized",
                         unify_metadata = TRUE,
                         merge_additional_variables = TRUE)
dt_metrics_sa <-
  convert_se_assay_to_dt(se = se_sa,
                         assay_name = "Metrics",
                         unify_metadata = TRUE,
                         merge_additional_variables = TRUE)
dt_average_sa <-
  convert_se_assay_to_dt(se = se_sa,
                         assay_name = "Averaged",
                         unify_metadata = TRUE,
                         merge_additional_variables = TRUE)
dt_metrics_sa_capped <-
  cap_assay_infinities(conc_assay_dt = dt_average_sa,
                       assay_dt = dt_metrics_sa,
                       experiment_name = sa_name,
                       col = "xc50",
                       capping_fold = 5)

## ----cd_data, warning = FALSE, message = FALSE--------------------------------
mae <- get_synthetic_data("combo_codilution_small")
cd_name <- get_supported_experiments("cd")
se_cd <- mae[[cd_name]]

dt_norm_cd <-
  convert_se_assay_to_dt(se = se_cd,
                         assay_name =  "Normalized",
                         unify_metadata = TRUE,
                         merge_additional_variables = TRUE)
dt_metrics_cd <-
  convert_se_assay_to_dt(se = se_cd,
                         assay_name = "Metrics",
                         unify_metadata = TRUE,
                         merge_additional_variables = TRUE)
dt_average_cd <-
  convert_se_assay_to_dt(se = se_cd,
                         assay_name = "Averaged",
                         unify_metadata = TRUE,
                         merge_additional_variables = TRUE)

## ----combo_data, warning = FALSE, message = FALSE-----------------------------
mae <- get_synthetic_data("combo_matrix")
combo_name <- get_supported_experiments("combo")
se_combo <- mae[[combo_name]]

dt_norm_combo <-
  convert_se_assay_to_dt(se = se_combo,
                         assay_name =  "Normalized",
                         unify_metadata = TRUE,
                         merge_additional_variables = TRUE)
dt_metrics_combo <-
  convert_se_assay_to_dt(se = se_combo,
                         assay_name = "Metrics",
                         unify_metadata = TRUE,
                         merge_additional_variables = TRUE)
dt_average_combo <-
  convert_se_assay_to_dt(se = se_combo,
                         assay_name = "Averaged",
                         unify_metadata = TRUE,
                         merge_additional_variables = TRUE)
dt_scores <-
  convert_se_assay_to_dt(se = se_combo,
                         assay_name = "scores",
                         unify_metadata = TRUE,
                         merge_additional_variables = TRUE)
dt_excess <-
  convert_se_assay_to_dt(se = se_combo,
                         assay_name = "excess",
                         unify_metadata = TRUE,
                         merge_additional_variables = TRUE)
dt_isobolograms <-
  convert_se_assay_to_dt(se = se_combo,
                         assay_name = "isobolograms",
                         unify_metadata = TRUE,
                         merge_additional_variables = TRUE)

## ----sa_prism_data, warning = FALSE, message = FALSE--------------------------
mae_prism <- get_synthetic_data("prism")
sa_name <- get_supported_experiments("sa")
se_sa_prism <- mae_prism[[sa_name]]

dt_norm_prism_sa <-
  convert_se_assay_to_dt(se = se_sa_prism,
                         assay_name =  "Normalized")
dt_metrics_prism_sa <-
  convert_se_assay_to_dt(se = se_sa_prism,
                         assay_name = "Metrics")
dt_average_prism_sa <-
  convert_se_assay_to_dt(se = se_sa_prism,
                         assay_name = "Averaged")
dt_metrics_prism_sa_capped <-
  cap_assay_infinities(conc_assay_dt = dt_average_prism_sa,
                       assay_dt = dt_metrics_prism_sa,
                       experiment_name = sa_name,
                       col = "xc50",
                       capping_fold = 5)

## ----plot_dose_response_sa, warning = FALSE, message = FALSE------------------
plot_dose_response_sa(
  dt_metrics = dt_metrics_sa,
  dt_average = dt_average_sa,
  selection_name = "drug_002",
  group_var = "CellLineName",
  group_names = c("cellline_BC", "cellline_FD", "cellline_JE"))

## ----plot_dose_response_combo, warning = FALSE, message = FALSE---------------
plot_dose_response_combo(
  dt_average = dt_average_combo,
  drug1_name = "drug_011",
  drug2_name = "drug_021",
  cl_name = "cellline_NE")

## ----pheatmap_with_anno_sa, warning = FALSE, message = FALSE------------------
annotation_manual_col <-
  unique(dt_metrics_sa[, c("CellLineName", "Tissue"), with = FALSE])
annotation_manual_row <-
  unique(dt_metrics_sa[, c("DrugName", "drug_moa"), with = FALSE])

output <- pheatmap_with_anno_sa(
  dt_metrics = dt_metrics_sa,
  dt_metrics_capped = dt_metrics_sa_capped,
  annotation_row = annotation_manual_row,
  annotation_col = annotation_manual_col)
hm <- output[["heatmap"]]
patchwork::wrap_elements(hm[["gtable"]])

knitr::kable(output[["data"]][["matrix"]], escape = FALSE)

## ----plot_boxplot_metric_sa_by_CLs, warning = FALSE, message = FALSE----------
plot_boxplot_metric_sa_by_CLs(dt_metrics_sa_capped)

## ----plot_boxplot_metric_sa_by_drugs, warning = FALSE, message = FALSE--------
plot_boxplot_metric_sa_by_drugs(dt_metrics_sa_capped)

## ----plot_boxplot_metric_sa_by_CLs_group, warning = FALSE, message = FALSE----
plot_boxplot_metric_sa_by_CLs(dt_metrics_sa_capped,
                              grouped_flag = TRUE)

## ----plot_boxplot_metric_sa_by_drugs_group, warning = FALSE, message = FALSE----
plot_boxplot_metric_sa_by_drugs(dt_metrics_sa_capped,
                                grouped_flag = TRUE)

## ----plot_boxplot_metric_sa_by_CLs_pts, warning = FALSE, message = FALSE------
plot_boxplot_metric_sa_by_CLs(dt_metrics_sa_capped,
                              colored_pts_flag = TRUE)

## ----plot_boxplot_metric_sa_by_drugs_pts, warning = FALSE, message = FALSE----
plot_boxplot_metric_sa_by_drugs(dt_metrics_sa_capped,
                                colored_pts_flag = TRUE)

## ----plot_boxplot_metric_sa_by_grp_d, warning = FALSE, message = FALSE--------
plot_boxplot_metric_sa_by_grp(dt_metrics_sa_capped,
                              selection_var = "DrugName",
                              selection_name = "drug_002",
                              group_var = "Tissue",
                              named_n = 3)

## ----plot_boxplot_metric_sa_by_grp_cl, warning = FALSE, message = FALSE-------
plot_boxplot_metric_sa_by_grp(dt_metrics_sa_capped,
                              selection_var = "CellLineName",
                              selection_name = "cellline_BC",
                              group_var = "drug_moa",
                              named_n = 3)

## ----pheatmap_with_anno_cd, warning = FALSE, message = FALSE------------------
annotation_manual_col <-
  unique(dt_metrics_cd[, c("CellLineName", "Tissue"), with = FALSE])
annotation_manual_row <-
  unique(dt_metrics_cd[, c("DrugName", "DrugName_2", "Concentration_2",
                           "drug_moa", "drug_moa_2"), with = FALSE])

output <- pheatmap_with_anno_cd(
  dt_metrics = dt_metrics_cd,
  annotation_row = annotation_manual_row,
  annotation_col = annotation_manual_col)
hm <- output[["heatmap"]]
patchwork::wrap_elements(hm[["gtable"]])

knitr::kable(output[["data"]][["matrix"]], escape = FALSE)

## ----pheatmap_with_anno_combo, warning = FALSE, message = FALSE---------------
annotation_manual_col <-
  unique(dt_scores[, c("CellLineName", "Tissue"), with = FALSE])
annotation_manual_row <-
  unique(dt_scores[, c("DrugName", "DrugName_2",
                       "drug_moa", "drug_moa_2"), with = FALSE])

output <- pheatmap_with_anno_combo(
  dt_scores = dt_scores,
  annotation_row = annotation_manual_row,
  annotation_col = annotation_manual_col)
hm <- output[["heatmap"]]
patchwork::wrap_elements(hm[["gtable"]])

knitr::kable(output[["data"]][["matrix"]], escape = FALSE)

## ----plot_boxplot_metric_combo_by_CLs, warning = FALSE, message = FALSE-------
plot_boxplot_metric_combo_by_CLs(dt_scores)

## ----plot_boxplot_metric_combo_by_drugs, warning = FALSE, message = FALSE-----
plot_boxplot_metric_combo_by_drugs(dt_scores)

## ----plot_boxplot_metric_combo_by_grp_d, warning = FALSE, message = FALSE-----
plot_boxplot_metric_combo_by_grp(dt_scores,
                                 selection_var = "DrugName",
                                 selection_name = c("drug_002", "drug_026"),
                                 group_var = "Tissue",
                                 named_n = 3)

## ----plot_boxplot_metric_combo_by_grp_cl, warning = FALSE, message = FALSE----
plot_boxplot_metric_combo_by_grp(dt_scores,
                                 selection_var = "CellLineName",
                                 selection_name = "cellline_BC",
                                 group_var = "drug_moa_2",
                                 named_n = 3)

## ----heatmap_combo_metrics_panel, warning = FALSE, message = FALSE------------
heatmap_combo_metrics(dt_excess = dt_excess,
                      dt_isobolograms = dt_isobolograms,
                      drug1_name = "drug_001",
                      drug2_name = "drug_026",
                      cl_name = "cellline_NE",
                      iso_levels = "0.5",
                      metric = "smooth")

heatmap_combo_metrics(dt_excess = dt_excess,
                      dt_isobolograms = dt_isobolograms,
                      drug1_name = "drug_001",
                      drug2_name = "drug_026",
                      cl_name = "cellline_NE",
                      iso_levels = "0.5",
                      metric = "hsa_excess")

heatmap_combo_metrics(dt_excess = dt_excess,
                      dt_isobolograms = dt_isobolograms,
                      drug1_name = "drug_001",
                      drug2_name = "drug_026",
                      cl_name = "cellline_NE",
                      iso_levels = "0.5",
                      metric = "bliss_excess")

## ----plot_combination_index, warning = FALSE, message = FALSE-----------------
plot_combination_index(dt_excess = dt_excess,
                       dt_isobolograms = dt_isobolograms,
                       drug1_name = "drug_001",
                       drug2_name = "drug_026",
                       cl_name = "cellline_NE",
                       iso_levels = c("0.2", "0.5", "0.8"))

## ----heatmap_combo_with_isoref, warning = FALSE, message = FALSE--------------
heatmap_combo_with_isoref(dt_excess = dt_excess,
                          dt_isobolograms = dt_isobolograms,
                          drug1_name = "drug_001",
                          drug2_name = "drug_026",
                          cl_name = "cellline_NE",
                          iso_levels = c("0.2", "0.5", "0.8"))

## ----plot_plate, warning = FALSE, message = FALSE, eval = FALSE---------------
# # Load test data using gDRimport
# td <- gDRimport::get_test_data()
# 
# # Load data from manifest, template, and results files
# l_tbl <- gDRimport::load_data(
#   manifest_file = gDRimport::manifest_path(td),
#   df_template_files = gDRimport::template_path(td),
#   results_file = gDRimport::result_path(td)
# )
# 
# # Merge the manifest, treatments, and raw data
# merged_data <- gDRcore::merge_data(
#   l_tbl$manifest,
#   l_tbl$treatments,
#   l_tbl$data
# )
# 
# # Visualize the plate design of the experiment
# plate_stack_vis <- plot_plate_stack_info(merged_data)
# plate_vis <- plot_plate(merged_data, "ReadoutValue")
# plate_stack_vis[["201904197a"]]
# plate_vis[["201904197a"]]

## ----heatmap_control_mapping_qc, warning = FALSE, message = FALSE-------------
dt_treat <-
  convert_se_assay_to_dt(se_sa, assay_name = "RawTreated")
dt_controls <-
  convert_se_assay_to_dt(se_sa, assay_name = "Controls")

hm <- heatmap_control_mapping_qc(dt_treat = dt_treat,
                                 dt_controls = dt_controls)
patchwork::wrap_elements(hm[["gtable"]])

dt_treat <-
  convert_se_assay_to_dt(se_combo, assay_name = "RawTreated")
dt_controls <-
  convert_se_assay_to_dt(se_combo, assay_name = "Controls")

hm <- heatmap_control_mapping_qc(dt_treat = dt_treat,
                                 dt_controls = dt_controls)
patchwork::wrap_elements(hm[["gtable"]])

# simulate missing controls for two cell lines
cellline_name <- get_env_identifiers("cellline_name")
dt_controls_missing <-
  dt_controls[!get(cellline_name) %in% c("cellline_BC", "cellline_JE")]
hm <- heatmap_control_mapping_qc(dt_treat = dt_treat,
                                 dt_controls = dt_controls_missing)
patchwork::wrap_elements(hm[["gtable"]])

## ----plot_var_distribution_qc, warning = FALSE, message = FALSE---------------
plot_var_distribution_qc(dt_assay = dt_norm_sa,
                         cl_name = "cellline_NE")
plot_var_distribution_qc(dt_assay = dt_norm_cd,
                         cl_name = "cellline_BA")
plot_var_distribution_qc(dt_assay = dt_norm_combo,
                         cl_name = "cellline_NE")

## ----pheatmap_qc_sa, warning = FALSE, message = FALSE-------------------------
hm_sa <- pheatmap_qc(dt_average = dt_average_sa)
patchwork::wrap_elements(hm_sa[["gtable"]])

## ----pheatmap_qc_cd, warning = FALSE, message = FALSE-------------------------
hm_cd <- pheatmap_qc(dt_average = dt_average_cd)
patchwork::wrap_elements(hm_cd[["gtable"]])

## ----pheatmap_qc_combo, warning = FALSE, message = FALSE----------------------
hm_combo <- pheatmap_qc(dt_average = dt_average_combo)
patchwork::wrap_elements(hm_combo[["gtable"]])

## ----plot_dose_response_sa_qc, warning = FALSE, message = FALSE---------------
plot_dose_response_sa_qc(dt_metrics = dt_metrics_sa,
                         dt_average = dt_average_sa,
                         cl_name = "cellline_AA",
                         d_name = "drug_001")

## ----plot_var_stat_qc, warning = FALSE, message = FALSE-----------------------
plot_var_stat_qc(dt_assay = dt_metrics_sa,
                 cl_name = "cellline_AA",
                 metric = "x_mean")

## ----plot_fitting_acc, warning = FALSE, message = FALSE-----------------------
plot_fitting_acc(dt_assay = dt_metrics_sa,
                 cl_name = "cellline_EA")

## ----sel_drug, warning = FALSE, message = FALSE-------------------------------
sel_drug <- unique(dt_norm_prism_sa$DrugName)[1]
print(sel_drug)

## ----prism_dt_res, warning = FALSE, message = FALSE---------------------------
dt_response <- prep_dt_response_metric_sa(
  dt_metrics = dt_metrics_prism_sa,
  d_name = sel_drug,
  normalization_type = "RV",
  metric = "x_mean"
)

## ----obj_meta, warning = FALSE, message = FALSE-------------------------------
meta_data_path <- 
  system.file("depmap_data/Model.csv.gz", package = "gDRtestData")

meta_col <- "OncotreeLineage"
obj_depmap_meta <- prep_dt_depmap_meta(
    meta_data_path = meta_data_path, 
    metadata_col = meta_col)

## ----obj_feat, warning = FALSE, message = FALSE-------------------------------
feat_data_path <- system.file("depmap_data", package = "gDRtestData")

feature_set <- "OmicsCNGene"
obj_depmap_feat <- prep_dt_depmap_feat(
    feat_data_path = feat_data_path,
    meta_data_path = meta_data_path,
    feature_set = feature_set)

feature_set_2 <- "OmicsSomaticMutationsMatrixDamaging"
obj_depmap_feat_2 <- prep_dt_depmap_feat(
    feat_data_path = feat_data_path,
    meta_data_path = meta_data_path,
    feature_set = feature_set_2,
    with_decoding = TRUE)

## ----obj_assoc, warning = FALSE, message = FALSE------------------------------
obj_assoc_meta <- prep_dt_assoc(
  dt_response = dt_response,
  dt_depmap = obj_depmap_meta$dt_depmap,
  selected_feat_meta_col = obj_depmap_meta$selected_feat_meta_col)

obj_assoc_feta <- prep_dt_assoc(
  dt_response = dt_response,
  dt_depmap = obj_depmap_feat$dt_depmap,
  selected_feat_meta_col = obj_depmap_feat$selected_feat_meta_col)

## ----plot_vol_meta, warning = FALSE, message = FALSE--------------------------
plot_volcano_assoc(
  dt_assoc = obj_assoc_meta$dt_assoc,
  selected_feat_meta_col = obj_assoc_meta$selected_feat_meta_col,
  selected_metric = obj_assoc_meta$selected_metric,
  condition_info = obj_assoc_meta$condition_info)

## ----plot_vol_feat, warning = FALSE, message = FALSE--------------------------
plot_volcano_assoc(
  dt_assoc = obj_assoc_feta$dt_assoc,
  selected_feat_meta_col = obj_assoc_feta$selected_feat_meta_col,
  selected_metric = obj_assoc_feta$selected_metric,
  condition_info = obj_assoc_feta$condition_info)

## ----plot_vol_meta_panel, warning = FALSE, message = FALSE--------------------
obj_res <- plot_volcano_assoc_panel(
  dt_response = dt_response,
  dt_depmap = obj_depmap_meta$dt_depmap,
  selected_metric = "RV_gDR_x_mean",
  selected_feat_meta_col = obj_depmap_meta$selected_feat_meta_col)

obj_res[["panel"]]

generate_datatable(
  obj_res[["assoc_data"]],
  col_to_round = c("rho", "q_value", "neglog_q_value"))

## ----plot_vol_feat_panel, warning = FALSE, message = FALSE--------------------
obj_res <- plot_volcano_assoc_panel(
  dt_response = dt_response,
  dt_depmap = obj_depmap_feat$dt_depmap,
  selected_metric = "RV_gDR_x_mean",
  selected_feat_meta_col = obj_depmap_feat$selected_feat_meta_col)

obj_res[["panel"]]

generate_datatable(
  obj_res[["assoc_data"]],
  col_to_round = c("rho", "q_value", "neglog_q_value"))

## ----plot_scatt, warning = FALSE, message = FALSE-----------------------------
plot_scatter_with_corr(
  dt_response = dt_response,
  dt_depmap = obj_depmap_feat$dt_depmap,
  selected_feat = names(obj_depmap_feat$dt_depmap)[42],
  selected_feat_meta_col = obj_depmap_feat$selected_feat_meta_col)

## ----plot_scatt_panel, warning = FALSE, message = FALSE-----------------------
plot_scatter_with_corr_panel(
  dt_response = dt_response,
  dt_depmap = obj_depmap_feat$dt_depmap,
  selected_feat = names(obj_depmap_feat$dt_depmap)[12:17],
  selected_feat_meta_col = obj_depmap_feat$selected_feat_meta_col,
  ncol = 3)

## ----plot_box_num_meta, warning = FALSE, message = FALSE----------------------
plot_boxplot_num(
  dt_response = dt_response,
  dt_depmap = obj_depmap_meta$dt_depmap,
  selected_feat = names(obj_depmap_meta$dt_depmap)[10],
  selected_feat_meta_col = obj_depmap_meta$selected_feat_meta_col)

## ----plot_box_meta_panel, warning = FALSE, message = FALSE--------------------
plot_boxplot_num_panel(
  dt_response = dt_response,
  dt_depmap = obj_depmap_meta$dt_depmap,
  selected_feat = names(obj_depmap_meta$dt_depmap)[10:13],
  selected_feat_meta_col = obj_depmap_meta$selected_feat_meta_col,
  ncol = 2)

## ----plot_box_meta, warning = FALSE, message = FALSE--------------------------
plot_boxplot_meta(
  dt_response = dt_response,
  dt_depmap = obj_depmap_meta$dt_depmap,
  selected_feat_meta_col = obj_depmap_meta$selected_feat_meta_col)

## ----sessionInfo--------------------------------------------------------------
sessionInfo()

