#---------------------------------------------------------
# Scriptname:  "0.descriptive_cases.R"
#
# Purpose of script: 
#
# To perform descriptive analyses for the temporal and 
# spatial distribution of WNND cases and reproduce Fig 1. 
#
#  ------------------------------------------------------
#  ! This script is run on real data:                   !
#  !                                                    !
#  ! - "monthly_cases.csv"                              !
#  ! - "cumulative_cases.csv"                           !
#  !                                                    !
#  ! In the manuscript Fig.1 the time series are        !
#  ! at weekly level, here are at monthly level.        !
#  ------------------------------------------------------
#
# Author(s): Giovenale Moirano
# Date Created: 05 May 2024
# Email: giovenale.moirano@bsc.es
#
#---------------------------------------------------------
# 1. Clean Environment
#---------------------------------------------------------

rm(list = ls())

#------------------------------------------------------------
# 2.Set WD
#------------------------------------------------------------

# setwd("mydirectory/")

#---------------------------------------------------------
# 3. Loadpackages
#---------------------------------------------------------

packages <- c( 
  "sf", "dplyr", "dplyr",
  "ggplot2", "ggpubr", "grid"
)
install.packages(setdiff(packages, rownames(installed.packages())))
lapply(packages, require, character.only = TRUE)


#------------------------------------------------------------
# 4. Load monthly data and plot it
#------------------------------------------------------------

# 4a. Import Human Monthly Data
data <- read.csv("data/1.monthly_cases.csv")

# 4b. Plot monthly time series of cases 

plot_a <- data %>%
  ggplot(aes(x = my, y = n, group = country)) +
  geom_line() +
  labs(x = "Year-Week", y = "Number of Cases", title = "A") +
  theme_minimal() +
  scale_x_discrete(breaks = c("2014-01", "2015-01",
                              "2016-01", "2017-01",
                              "2018-01", "2019-01",
                              "2020-01", "2021-01",
                              "2022-01"),
                   labels = c(2014:2022)) +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.title.x = element_text(size = 12),
        axis.title.y = element_text(size = 12),
        axis.text.x = element_text(size = 10),
        axis.text.y = element_text(size = 10),
        axis.ticks = element_line()) +
  theme(panel.grid.minor = element_blank(),
        axis.line = element_line(colour = "black")) +
  facet_wrap(~ country, ncol = 1)

plot_a
#------------------------------------------------------------
# 5. Load shapefile, cumulative cases and plot Incidence Rates
#------------------------------------------------------------

#5a. Load NUTS3 level European Shapefile with population (ref year 2018)
Europe_NUTS <- st_read("shapefiles/Europe_pop.shp", quiet = TRUE)

# Bounding box
min_lat <- 29.5 
max_lat <- 65.5  
min_lon <- -12  
max_lon <- 45  

#crop shapefile 
bbox <- st_bbox(c(xmin = min_lon, xmax = max_lon, ymin = min_lat, ymax = max_lat), crs = st_crs(Europe_NUTS))
Europe_NUTS <- st_crop(Europe_NUTS, bbox)

#Multiply population for the 9 years of the study 
Europe_NUTS$pop <- Europe_NUTS$X2018*9


#5b. Load wnnd cumulative cases (2014-2022) per NUTS3 level
data_cum <- read.csv("data/2.cumulative_cases.csv")

#5c. Merge Data_cum and European Shapefile and compute incidence rates 

Europe_NUTS <- Europe_NUTS %>% left_join(data_cum, by = "NUTS_ID")
Europe_NUTS$n[is.na(Europe_NUTS$n)] <- 0
Europe_NUTS$inc <- (Europe_NUTS$n / (Europe_NUTS$pop)) * 100000
Europe_NUTS$inc[is.na(Europe_NUTS$inc)] <- 0
Europe_NUTS$IR <- cut(Europe_NUTS$inc, breaks = c(-0.001, 0.0001, 0.5, 1, 2, 4, 8),
                      labels = c("No cases", "0.01 - 0.49", "0.50 -0.99", "1.00 - 1.99", "2.00 - 3.99", "4.00 - 7.99"))


 
#5d. Plot the Map 
Europe_NUTS$IR[Europe_NUTS$CNTR_CODE %in% c("UK", "CH", NA)] <- NA
palette <- colorRampPalette(c("white", "darkred"))(6)

plot_b <- ggplot() +
  geom_sf(data = Europe_NUTS, aes(fill = IR)) +
  theme_minimal() +
  scale_fill_manual(values = palette, labels = c(levels(Europe_NUTS$IR), "Not Available")) +
  theme(legend.title = element_text(size = 14)) +
  labs(x = "Lon", y = "Lat", title = "B") +
  theme(plot.title = element_text(hjust = 0.5))

plot_b

#------------------------------------------------------------
# 6. Combine Graphs 
#------------------------------------------------------------

ggarrange(plot_a, plot_b, ncol = 2, heights = c(1, 1), widths = c(1, 2))

