# Library ####

library(tidyverse)
library(readxl)
library(gtsummary)
library(flextable)
library(scales)
library(DiagrammeR)
library(DiagrammeRsvg)
library(rsvg)
library(magick)
library(readxl)

# FIRST LOAD THE DATA BACK FROM EDITING IN EXCEL ####

DF.AE <- read_xlsx("06_data_update/DATA_SMT_AE.xlsx",sheet = 1)
  
DF.AE.TYPE <- read_xlsx("06_data_update/DATA_SMT_AE.xlsx",sheet = 2,na = "NA") %>% 
  mutate(FREQ.PARTICIP.AE=as.numeric(FREQ.PARTICIP.AE)) # manual change from xlsx

# Table 1 - descriptive ####

DF.AE %>% 
  tbl_summary(by=COLLECT.AE,
              include = -c(ID,Cov.ID,MENTION.SECTION,REPORT.AE,Year),
              percent = "row") %>% 
  add_overall() %>% 
  as_flex_table()

# Figure 1 - flowchart ####

## STATS

DF.AE %>% 
  group_by(MENTION.AE) %>%
  count()

DF.AE %>% 
  filter(MENTION.AE=="YES") %>% 
  group_by(COLLECT.AE) %>%
  count()

DF.AE %>% 
  filter(MENTION.AE=="YES",
         COLLECT.AE=="YES") %>% 
  group_by(REPORT.AE) %>%
  count()

F.flow<-
  grViz("
digraph ae_flowchart {

  # Default node style
  node [shape = box, fontname = Helvetica, fontsize = 14, style = filled, color = black]

  # Nodes
  root           [label = 'Included RCTs\\nn = 253', fillcolor = '#b6e3b6', width = 3.0, height = 1.4]
  no_ae          [label = 'No mentioning of AEs\\nn = 113', fillcolor = '#440154', fontcolor = white, width = 2.2, height = 1.1]
  yes_ae         [label = 'Mentioning of AEs\\nn = 140', fillcolor = '#b63679', fontcolor = white, width = 2.5, height = 1.2]
  not_collected  [label = 'Did not collect AE:\\nn = 4', fillcolor = '#800026', fontcolor = white, width = 2.3, height = 1.0]
  collected      [label = 'Collected AEs\\nn = 136', fillcolor = '#f98db7', fontcolor = black, width = 2.3, height = 1.0]

  no_results     [label = 'Studies not reporting AE results\\nn = 2', fillcolor = '#d8cf9b', width = 2.2, height = 0.9]
  zero_ae        [label = 'Studies reporting zero AEs\\nn = 60', fillcolor = '#fbe6a2', width = 2.0, height = 0.9]
  some_ae        [label = 'Studies reporting AEs\\nn = 74', fillcolor = '#fde725', width = 1.9, height = 0.9]

  # Edges
  root -> no_ae
  root -> yes_ae
  yes_ae -> collected
  yes_ae -> not_collected

  # Force left-to-right order
  collected -> no_results
  collected -> zero_ae
  collected -> some_ae

  { rank = same; no_results; zero_ae; some_ae }

  # Layout
  graph [layout = dot, rankdir = TB, nodesep = 0.45, ranksep = 0.55]
}
")


# Figure 2 - times data ####

DF.AE %>%
  select(ID,Year,COLLECT.AE,REPORT.AE) %>% 
  pivot_longer(-c(ID,Year)) %>% 
  group_by(Year,name,value) %>% 
  summarise(n=n()) %>% 
  group_by(Year,name) %>% 
  mutate(f_study=n/sum(n)) %>% 
  filter(!(name=="REPORT.AE" & value=="NO")) %>% 
  pivot_wider(names_from = c(name, value), values_from = c(n, f_study), values_fill = 0) %>% 
  mutate(
    F_rep = if_else(
      n_REPORT.AE_YES > 0 & n_COLLECT.AE_YES > 0,
      n_REPORT.AE_YES / n_COLLECT.AE_YES,
      f_study_REPORT.AE_YES  # fallback if one of them is missing
    )
  ) %>% 
  pivot_longer(cols=c(contains("n_"))) %>% 
  mutate(F_TOTAL=case_when(
    name=="n_COLLECT.AE_NO" ~ f_study_COLLECT.AE_NO,
    name=="n_COLLECT.AE_YES" ~ f_study_COLLECT.AE_YES,
    name=="n_REPORT.AE_YES" ~ f_study_REPORT.AE_YES,
  )) %>% 
  mutate(F_LABEL=case_when(
    name=="n_COLLECT.AE_NO" ~ f_study_COLLECT.AE_NO,
    name=="n_COLLECT.AE_YES" ~ f_study_COLLECT.AE_YES,
    name=="n_REPORT.AE_YES" ~ F_rep,
  )) %>% 
  select(Year, name, n=value,F_TOTAL, F_LABEL) %>% 
  mutate(name=fct_recode(name,
                         "Did not collect AEs" = "n_COLLECT.AE_NO",
                         "Collected AEs" = "n_COLLECT.AE_YES",
                         "Reported AEs"  = "n_REPORT.AE_YES")) %>% 
  mutate(label=paste0(n,"(",round(F_LABEL*100,0),"%)")) %>% 
  ggplot(aes(x=Year,y=F_TOTAL,group=name,col=name))+
  geom_line(size = 2) +
  geom_point(size = 3) +
  geom_label(aes(label = label), vjust = -0.8, size = 3, show.legend = FALSE,
             fill = "grey90", col = "black") +
  scale_y_continuous(labels = percent_format(accuracy = 1), limits = c(0, 1)) +
  scale_color_manual(
    values = c(
      "Did not collect AEs" = "#800026",
      "Collected AEs" = "#f98db7",
      "Reported AEs" = "#fde725"
    )
  ) +
  labs(y = "Proportion of total studies", color = "",x="") +
  theme_minimal(base_size = 10) +
  theme(legend.position = "bottom")


# Figure 3 - AE rep ####


## Data to be used for frequency repoting
TMP.comp<-
DF.AE.TYPE %>% 
  select(ID,Treatment) %>% 
  mutate(
    Treatment = case_when(
      Treatment %in% c("I1", "I2", "I3") ~ "Intervention group",
      Treatment == "NR" ~ "Study level",
      .default = "Comparator group"
    )
  ) %>% 
  unique() %>% 
  group_by(Treatment) %>% 
  summarise(TOTAL_N=n())

## Individual data points to be described

TMP.count<-
  DF.AE.TYPE %>% 
  select(ID, Treatment) %>% 
  mutate(
    Treatment = case_when(
      Treatment %in% c("I1", "I2", "I3") ~ "Intervention group",
      Treatment == "NR" ~ "Study level",
      .default = "Comparator group"
    )
  ) %>% 
  distinct() %>% 
  group_by(ID) %>% 
  summarise(combo = paste(sort(unique(Treatment)), collapse = " + ")) %>% 
  count(combo, name = "n") %>% 
  arrange(desc(n)) %>% 
  mutate(F=n/sum(n)*100)



## PLOT 

DF.AE.TYPE %>% 
  select(ID,Treatment,N.AE,FREQ.PARTICIP.AE,N.PARTICIP.AE)  %>% 
  unique() %>% 
  mutate(
    Treatment = case_when(
      Treatment %in% c("I1", "I2", "I3") ~ "Intervention group",
      Treatment == "NR" ~ "Study level",
      .default = "Comparator group"
    )
  ) %>%
  pivot_longer(
    cols = contains("AE"),
    names_to = "AE_REP",
    values_to = "value"
  ) %>%
  mutate(value = if_else(is.na(value), "NR", "REP")) %>% 
  #arrange(Cov.ID, Treatment, AE_REP, value) %>%
  distinct() %>% 
  group_by(Treatment,AE_REP,value) %>%
  summarise(n = n()) %>%
  full_join(TMP.comp) %>% 
  ungroup() %>% 
  mutate(total_Freq=n/TOTAL_N*100) %>% 
  mutate(
    label = paste0(n, "\n(", round(total_Freq, 1), "%)")
  ) %>%  
  filter(value=="REP") %>%
  mutate(Treatment=fct_relevel(Treatment,"Intervention group","Comparator group","Study level")) %>% 
  mutate(AE_REP = fct_recode(
    AE_REP,
    "Proportion reporting AEs" = "FREQ.PARTICIP.AE",
    "Count reporting AEs"      = "N.PARTICIP.AE",
    "Event-based reporting AEs"  = "N.AE"
  )) %>% 
  mutate(AE_REP = fct_relevel(
    AE_REP,
    "Event-based reporting AEs",
    "Proportion reporting AEs",
    "Count reporting AEs"     
  )) %>% 
  ggplot(aes(y = fct_rev(AE_REP), x = Treatment,fill=AE_REP)) +
  geom_point(aes(size = n), shape = 21, color = "black", alpha = 0.8) +
  geom_text(aes(label = label), vjust = 0.5, hjust = 0.5, size = 3) +
  scale_size_area(max_size = 30) +
  scale_fill_viridis_d(option = "turbo",begin=0.25,end=0.9)+
  labs(
    x = "",
    y = "",
    size = "Number of Studies",
    fill = "Reported"
  ) +
  theme_minimal(base_size = 12)+
  theme(legend.position = "none")


# FIgure 4 - AE types  ####

DF.AE.TYPE %>%
  # keep only real studies
  filter(!is.na(Cov.ID)) %>%
  # flag those that reported AE types
  mutate(
    TYPE.REPORT.AE = if_else(
      is.na(TYPE.REPORT.AE) & !is.na(TYPE.COUNT.AE),
      "YES",
      TYPE.REPORT.AE
    )
  ) %>%
  # narrow to the columns we need
  select(
    ID = Cov.ID,
    Treatment,
    TYPE.REPORT.AE,
    TYPE.COUNT.AE
  ) %>%
  # create your T.AE grouping
  mutate(
    T.AE = case_when(
      str_detect(Treatment, "I")        ~ "Intervention group",
      str_detect(Treatment, "C")        ~ "Comparator group",
      Treatment == "NR"                 ~ "Study level",
      TYPE.REPORT.AE == "NO"            ~ "No type reported"
    )
  ) %>%
  select(Cov.ID, T.AE, TYPE.COUNT.AE) %>%
  # fill missing TYPE.COUNT.AE
  mutate(
    TYPE.COUNT.AE = if_else(
      is.na(TYPE.COUNT.AE),
      "No type reported",
      TYPE.COUNT.AE
    )
  ) %>%
  # split comma‐separated lists into rows
  separate_rows(TYPE.COUNT.AE, sep = ",\\s*") %>%
  # recode all AE‐type labels
  mutate(
    TYPE.COUNT.AE = fct_recode(
      TYPE.COUNT.AE,
      "Other Body Pain"       = "Other body pain",
      "Aggravation Pain"      = "Aggrevation pain",
      "Headache/Facial Pain"  = "headache/facial pain",
      "Other AE Type"         = "other",
      "Stiffness"             = "stiffness",
      "Numbness/Tingling"     = "numbness/tingling",
      "Fatigue/Tiredness"     = "fatigue/tiredness",
      "Nausea/Vomiting"       = "nausea/vomiting",
      "Dizziness"             = "dizziness",
      "Gastrointestinal"      = "GI",
      "Not Specified"         = "No type reported",
      "Skin Irritation"       = "Skin irratation",
      "Not Specified"         = "Not specified",
      "Cardiovascular"        = "Cardiovascular",
      "Weakness"              = "weakness",
      "Problem Sleeping"      = "problem sleeping",
      "Difficulty Walking"    = "difficulty walking",
      "Death"                 = "Death",
      "Bruising"              = "Bruising"
    )
  ) %>%
    # count per group & AE type
  count(T.AE, TYPE.COUNT.AE) %>%
  ungroup() %>%
 
  # compute total for ordering y‐axis
  group_by(TYPE.COUNT.AE) %>%
  mutate(total = sum(n)) %>%
  ungroup() %>%
 
  # reorder levels by descending total
  mutate(TYPE.COUNT.AE = fct_reorder(TYPE.COUNT.AE, total)) %>%
  mutate(T.AE=fct_relevel(T.AE,"Intervention group","Comparator group","Study level")) %>%
  # 3) plot
  ggplot(aes(x = n, y = TYPE.COUNT.AE)) +
  geom_point(aes(size = n, color = T.AE), alpha = 0.9) +
  geom_text(aes(label = n),
            nudge_x = 0,       # zero because we've already offset n
            color    = "black",
            size     = 3,
            vjust    = 0.4
  ) +
  scale_color_viridis_d(begin = 0.5, option = "C") +
  scale_size(range = c(3, 10)) +
  guides(size = "none") +
  # ensure axis ticks are the true integers
  scale_x_continuous(
    breaks = scales::pretty_breaks(),
    labels = function(x) as.character(round(x))
  ) +
  labs(
    x     = "Number of studies",
    y     = "Adverse event type",
    color = "Group"
  ) +
  theme_minimal(base_size = 11) +
  theme(
    legend.position    = "bottom",
    axis.text.y        = element_text(size = 9),
    axis.text.x        = element_text(size = 9)
  )+
  facet_wrap(~T.AE)


# Figure 5 - AE cats ####

TMP.type.count<-
  DF.AE.TYPE %>%
  filter(!is.na(ID)) %>%
  select(Cov.ID, Treatment, contains("COUNT")) %>%
  mutate(
    Treatment = case_when(
      Treatment %in% c("I1", "I2", "I3") ~ "Intervention group",
      Treatment == "NR" ~ "Study level",
      TRUE ~ "Comparator group"
    ),
    Treatment = fct_relevel(Treatment, "Intervention group", "Comparator group", "Study level")
  ) %>%
  pivot_longer(cols = -c(ID,Cov.ID,Treatment,TYPE.COUNT.AE), names_to = "AE_type", values_to = "value") %>%
  separate_rows(value, sep = ",\\s*") %>% 
  mutate(
    value = case_when(
      value == "NR" ~ "Not reported",
      str_to_lower(value) == "other" ~ "Not able to classify",
      TRUE ~ str_to_sentence(value)
    ),
    AE_type = case_when(
      AE_type == "ADD.CARE.COUNT.AE" ~ "Additional care",
      AE_type == "SEVERITY.COUNT.AE" ~ "Severity",
      AE_type == "DURATION.COUNT.AE" ~ "Duration",
      AE_type == "ONSET.COUNT.AE" ~ "Onset",
      AE_type == "RELATED.COUNT.AE" ~ "Relatedness",
      TRUE ~ AE_type
    )
  ) %>%
  separate_rows(TYPE.COUNT.AE, sep = ",\\s*") %>%
  mutate(TYPE.COUNT.AE=replace_na(TYPE.COUNT.AE, "Not reported")) %>% 
  unique() %>%  
  # filter(Treatment=="Study level",
  #        AE_type=="Additional care") %>% 
  group_by(Treatment,TYPE.COUNT.AE,AE_type,value) %>%
  summarise(PER_TYPE_N=n()) %>%
  group_by(Treatment,AE_type,value) %>% 
  summarise(SUM=sum(PER_TYPE_N)) %>% 
  group_by(Treatment,AE_type) %>% 
  mutate(FREQ=SUM/sum(SUM)*100) %>% 
  mutate(label=paste0(SUM," (",round(FREQ,0),"%)"))  %>%
  ungroup()

TMP_all_vals <- TMP.type.count %>%
  distinct(AE_type, value)


expand_grid(
  Treatment = fct_relevel(factor(c("Intervention group", "Comparator group", "Study level")),
                          "Intervention group", "Comparator group", "Study level"),
  AE_type = unique(TMP.type.count$AE_type),
  value = unique(TMP.type.count$value)
) %>%
  semi_join(TMP_all_vals, by = c("AE_type", "value"))  %>%
  left_join(TMP.type.count, by = c("Treatment", "AE_type", "value")) %>%
  
  group_by(Treatment, AE_type) %>% 
  
  mutate(label=if_else(is.na(label),"0",label))%>% 
  
  mutate(rank = case_when(
    # Severity (1–9)
    AE_type == "Severity" & value == "Benign" ~ 1,
    AE_type == "Severity" & value == "Mild" ~ 2,
    AE_type == "Severity" & value == "Moderate" ~ 3,
    AE_type == "Severity" & value == "Severe" ~ 4,
    AE_type == "Severity" & value == "Not serious" ~ 5,
    AE_type == "Severity" & value == "Serious" ~ 6,
    AE_type == "Severity" & value == "Not specified" ~ 7,
    AE_type == "Severity" & value == "Not able to classify" ~ 8,
    AE_type == "Severity" & value == "Not reported" ~ 9,
    
    # Duration (10–19)
    AE_type == "Duration" & value == "< 1 hour" ~ 10,
    AE_type == "Duration" & value == "1-24 hours" ~ 11,
    AE_type == "Duration" & value == "Transient" ~ 12,
    AE_type == "Duration" & value == "1-7 days" ~ 13,
    AE_type == "Duration" & value == "Short-term" ~ 14,
    AE_type == "Duration" & value == "> 30 days" ~ 15,
    AE_type == "Duration" & value == "Permanent" ~ 16,
    AE_type == "Duration" & value == "Not specified" ~ 17,
    AE_type == "Duration" & value == "Not able to classify" ~ 18,
    AE_type == "Duration" & value == "Not reported" ~ 19,
    
    # Onset (20–29)
    AE_type == "Onset" & value == "Pre-treatment phase" ~ 20,
    AE_type == "Onset" & value == "During treatment" ~ 21,
    AE_type == "Onset" & value == "After treatment 0-4 hours" ~ 22,
    AE_type == "Onset" & value == "Within same day/24 hours" ~ 23,
    AE_type == "Onset" & value == "1 - 7 days" ~ 24,
    AE_type == "Onset" & value == "> 7 days" ~ 25,
    AE_type == "Onset" & value == "Follow-up phase" ~ 26,
    AE_type == "Onset" & value == "Not specified" ~ 27,
    AE_type == "Onset" & value == "Not able to classify" ~ 28,
    AE_type == "Onset" & value == "Not reported" ~ 29,
    
    # Additional care (30–38)
    AE_type == "Additional care" & value == "No additional care" ~ 30,
    AE_type == "Additional care" & value == "Visit to healthcare provider" ~ 31,
    AE_type == "Additional care" & value == "Non-specified further care" ~ 32,
    AE_type == "Additional care" & value == "Medication" ~ 33,
    AE_type == "Additional care" & value == "Hospitalization" ~ 34,
    AE_type == "Additional care" & value == "Emergency care" ~ 35,
    AE_type == "Additional care" & value == "Not specified" ~ 36,
    AE_type == "Additional care" & value == "Not able to classify" ~ 37,
    AE_type == "Additional care" & value == "Not reported" ~ 38,
    
    
    # Relatedness (39–47)
    AE_type == "Relatedness" & value == "Not related" ~ 39,
    AE_type == "Relatedness" & value == "Unlikely" ~ 40,
    AE_type == "Relatedness" & value == "Possible" ~ 41,
    AE_type == "Relatedness" & value == "Likely" ~ 42,
    AE_type == "Relatedness" & value == "Yes" ~ 43,
    AE_type == "Relatedness" & value == "Certain" ~ 44,
    AE_type == "Relatedness" & value == "Not specified" ~ 45,
    AE_type == "Relatedness" & value == "Not able to classify" ~ 46,
    AE_type == "Relatedness" & value == "Not reported" ~ 47,
    
    
    
    TRUE ~ NA_real_
  )) %>% 
  mutate(
    AE_type = fct_relevel(AE_type, c("Severity", "Duration", "Onset", "Additional care", "Relatedness"))
  ) %>% 
  mutate(
    value = factor(value, levels = value[order(rank)]),
    AE_type = fct_relevel(AE_type, c("Severity", "Duration", "Onset", "Additional care", "Relatedness"))
  ) %>%   
  ggplot(aes(x = Treatment, y = fct_reorder2(value, AE_type, rank), fill = FREQ))+
  geom_tile(color = "black", linewidth = 0.3) +
  geom_label(aes(label = label), size = 3, color = "white", fill = "black", alpha = 0.6)+
  facet_wrap(~AE_type, scales = "free_y", ncol = 1) +
  scale_fill_viridis_c(
    option = "E", direction = -1, begin = 0, end = 1,
    na.value = "white",
    limits = c(0, 100),
    breaks = c(0,25, 50, 75, 100),
    labels = scales::percent_format(scale = 1),
    guide = guide_colorbar(
      barheight = unit(0.6, "cm"),   # increase height
      barwidth = unit(10, "cm"),     # increase width
      title.position = "top",
      title.hjust = 0.5
    )
  )+
  labs(
    x = "",
    y = "Adverse event characteristic",
    fill = "Proportion of studies"
  ) +
  theme_minimal(base_size = 12) +
  theme(
    panel.grid = element_blank(),
    legend.position = "bottom"
  )


# OTher descriptives ####

DF.AE %>% 
  select(ID,MENTION.AE,COLLECT.AE,MENTION.SECTION) %>% 
  pivot_longer(cols=c(MENTION.AE,COLLECT.AE)) %>% 
  group_by(name,value,MENTION.SECTION) %>%
  count() %>% 
  na.omit() %>% 
  group_by(name) %>% 
  mutate(F=n/sum(n)*100)


