# Process the disease data 

# Load packages 
library(readxl)
library(dplyr)
library(purrr)
library(countrycode)

## PIGGOTT ET AL

# Load data 
CL <- read_excel("CL_final_dataset.xlsx")
VL <- read_excel("VL_final_dataset.xlsx")

# Convert to list for formatting 
list1 <- list(CL, VL)
disease <- c('cl', 'vl')
names(list1) <- disease

list1 <- map(list1, ~ .x %>% 
               select(X, Y, YEAR, COUNTRY, DISEASE) %>% 
               filter(X > -31.4 & X < 62 & Y > 34.4 & Y < 82) %>%
               rename("x" = X, "y" = Y, "year" = YEAR, "adm0" = COUNTRY, "disease" = DISEASE) %>%
               mutate(disease = 1))

data1 <- bind_rows(list1, .id = "type")
data1 <- data1 %>% distinct()
data1 <- data1 %>%
  tidyr::pivot_wider(names_from = type, values_from = disease, values_fill = list(disease = 0))

# Attach to ISO3 codes 
data1$ISO3 <- countrycode(data1$adm0, "country.name", "iso3c")

# Have a column for if either one is present for a combined visceral and cutaneous model 
data1 <- data1 %>% mutate(either = if_else(vl == 0 & cl == 0, 0, 1))

# Save 
readr::write_csv(data1, "piggott_leish.csv")

# Plot where the points are 
nuts <- read_sf("NUTS_RG_20M_2024_4326.shp", stringsAsFactors = F)
nuts <- nuts %>% filter(LEVL_CODE == 1)
data1 <- data1 %>% tidyr::gather(disease, occurance, 5:6)
data1 <- data1 %>% mutate(disease = if_else(disease == "cl", "Cutaneous", "Visceral"))
data1$year <- as.numeric(data1$year)

ggplot() + 
  geom_point(data = data1 %>% filter(occurance == 1), aes(x = x, y = y, color = disease, alpha = year)) + 
  geom_sf(data = nuts, fill = NA) + 
  theme_minimal() + 
  facet_wrap(~disease) + 
  xlim(-20, 54) + 
  ylim(34.4, 71) + 
  labs(alpha = "Year", color = "Disease",) + 
  scale_color_manual(values = c("#FF6F91", "#FF8C42")) + 
  theme(text = element_text(face = "bold", size = 15),
        legend.position = "top") +
  guides(
    color = guide_legend(override.aes = list(size = 4)),
    alpha = guide_legend(override.aes = list(size = 4))
  )

data1 <- data1 %>% group_by(year, disease) %>% mutate(occurance = sum(occurance, na.rm = T)) %>% ungroup()

ggplot(data1, aes(x = year, y = occurance, color = disease)) + geom_line(size = 1) + 
  theme_minimal() + 
  scale_color_manual(values = c("#FF6F91", "#FF8C42")) + 
  theme(text = element_text(face = "bold", size = 15),
        legend.position = "top") +
  guides(
    color = guide_legend(override.aes = list(size = 4)),
    alpha = guide_legend(override.aes = list(size = 4))
  ) + 
  labs(x = "Year", color = "Disease", y = "Point Occurances")

data1 <- data1 %>% group_by(disease, adm0) %>% mutate(occurance = sum(occurance, na.rm = T)) %>% ungroup()

ggplot(data1, aes(x = reorder(adm0, occurance), y = occurance, color = disease, fill = disease)) + 
  geom_bar(stat = "identity") + 
  theme_minimal() + coord_flip() + 
  scale_color_manual(values = c("#FF6F91", "#FF8C42")) + 
  scale_fill_manual(values = c("#FF6F91", "#FF8C42")) + 
  theme(text = element_text(face = "bold", size = 15),
        legend.position = "top") +
  guides(
    color = guide_legend(override.aes = list(size = 4)),
    alpha = guide_legend(override.aes = list(size = 4))
  ) + 
  labs(x = "Country", color = "Disease", y = "Point Occurances", fill = "Disease")

## WHO GHO

# Load the data 
vl <- read_delim("occurance_vl.csv", 
                           delim = ";", escape_double = FALSE, trim_ws = TRUE)
cl <- read_delim("occurance_cl.csv", 
                 delim = ";", escape_double = FALSE, trim_ws = TRUE)

# Tidy the data and transform to a binary outcome 
cl <- cl %>% select(SpatialDimValueCode, Period, Value) %>% 
  rename("ISO3" = SpatialDimValueCode, "year" = Period, "cl" = Value) %>% 
  mutate(cl = if_else(cl > 0, 1, 0))

vl <- vl %>% select(SpatialDimValueCode, Period, Value) %>% 
  rename("ISO3" = SpatialDimValueCode, "year" = Period, "vl" = Value) %>% 
  mutate(vl = if_else(vl > 0, 1, 0))

# Join to one dataframe 
leish <- left_join(cl, vl)
leish <- leish %>% distinct()

# Transform NAs to zero 
leish <- leish %>%
  mutate_all(~replace(., is.na(.), 0))

# Have a column for if either one is present for a combined visceral and cutaneous model 
leish <- leish %>% mutate(either = if_else(vl == 0 & cl == 0, 0, 1))

# Merge to only countries in Europe 
leish$CNTR_CODE <- countrycode(leish$ISO3, "iso3c", "iso2c")
nuts <- nuts %>% select(CNTR_CODE) %>% st_drop_geometry() %>% distinct() 
nuts$in_euro <- 1
leish <- leish %>% mutate(CNTR_CODE = if_else(CNTR_CODE == "GR", "EL", CNTR_CODE))
leish <- left_join(leish, nuts)
leish <- na.omit(leish)
leish$in_euro <- NULL

write_csv(leish, "who_leish.csv")

# Plot 
nuts <- read_sf("NUTS_RG_20M_2024_4326.shp", stringsAsFactors = F)
nuts <- nuts %>% filter(LEVL_CODE == 0)
leish <- leish %>% tidyr::gather(disease, occurance, 3:4)
leish <- leish %>% mutate(disease = if_else(disease == "cl", "Cutaneous", "Visceral"))
leish <- leish %>% group_by(disease, CNTR_CODE) %>% mutate(occurance = sum(occurance, na.rm = T)) %>%
  ungroup() %>% select(disease, CNTR_CODE, occurance) %>% distinct()

leish2 <- leish %>% filter(disease == "Cutaneous")
leish <- leish %>%filter(disease == "Visceral")
nuts1 <- left_join(nuts, leish)
nuts2 <- left_join(nuts, leish2)

p2 <- 
ggplot(nuts2) + 
  geom_sf(aes(fill = occurance)) + 
  theme_minimal() + 
  xlim(-20, 54) + 
  ylim(34.4, 71) + 
  labs(fill = "Occurance", title = "Cutaneous") + 
  scale_fill_distiller(palette = "Purples", direction = 1) +
  theme(text = element_text(face = "bold", size = 15))

ggpubr::ggarrange(p1,p2,ncol = 2)




