library(adegenet)
library(ade4)
library(dplyr)
library(data.table)
library(ggplot2)
library(gridExtra)
library(ggpubr)
library(reshape2)
library(hierfstat)
library(pegas)
library(poppr)
library(tidyr)
library(stringi)
library(ggtree)
library(treeio)
library(tidytree)
library(RColorBrewer)
library(lemon)
library(treemap)
library(rgdal)
library(rgeos) 
library(maptools)
library(scales)
library(maps)
library(stringr)
library(svglite)
library(gridBase)
library(grid)
library(ggpubr)
library(data.table)
library(randomForest)
library(synbreed)
library(geosphere)
library(RandomFields)
library(INLA)
library(SPASIBA)
library(dplyr)
library(devtools)
library(raster)
library(sp)
library(PerformanceAnalytics)
setwd("~/Documents/Cedrela/Cedrela_SNP_assay_2018/")

#load data
spa_results<-read.csv("20190407_spa_results.csv",header=1)

names(spa_results)
## [1] "sampleID"  "long_true" "lat_true"  "long_est"  "lat_est"   "dist_hav" 
## [7] "dist_km"   "data"      "imp"
spa_results$index<-1:nrow(spa_results)

#separate sampling locations and estimated origins
samp_locs<-spa_results[c(1:3,8,10)]
pred_locs<-spa_results[c(1,4:5,8,10)]

#load WorldClim Data
r <- getData("worldclim",var="bio",res=10)
r <- r[[c(1,12)]] #we are focused only on the mean annual temperature (MAT) and annual precipitation (AP). 
names(r) <- c("MAT","AP")

#make coordinates file with long and lat columns. 
samp_coords<-samp_locs[c(2,3)]
pred_coords<-pred_locs[c(2,3)]
#find MAT and AP values corresponding to coordinates. 
samp_points <- SpatialPoints(samp_coords, proj4string = r@crs)
samp_values <- extract(r,samp_points)
pred_points <- SpatialPoints(pred_coords, proj4string = r@crs)
pred_values <- extract(r,samp_points)
#combine MAT and AP values with coordinates
samp_env<- cbind.data.frame(coordinates(samp_points),samp_values)
samp_env$index<-1:nrow(samp_env)
names(samp_env)[c(3:4)]<-c("MAT_true","AP_true")
pred_env<- cbind.data.frame(coordinates(pred_points),pred_values)
pred_env$index<-1:nrow(pred_env)
names(pred_env)[c(3:4)]<-c("MAT_est","AP_est")
#combine with other meta data
spa_results_env<-inner_join(samp_env,pred_env)
## Joining, by = "index"
spa_results<-inner_join(spa_results,spa_results_env)
## Joining, by = c("long_true", "lat_true", "long_est", "lat_est", "index")
#find climate difference
spa_results<-mutate(spa_results,MAT_diff = (MAT_true - MAT_est))
spa_results<-mutate(spa_results,AP_diff = (AP_true - AP_est))

summary(spa_results)
##        sampleID      long_true         lat_true           long_est     
##  CEOD_42   :  57   Min.   :-80.13   Min.   :-22.3667   Min.   :-80.13  
##  CEFI_281  :  56   1st Qu.:-75.83   1st Qu.:-11.1865   1st Qu.:-76.02  
##  CEODO_1291:  56   Median :-73.70   Median : -6.4325   Median :-73.17  
##  CEOD_285  :  54   Mean   :-72.90   Mean   : -7.8404   Mean   :-72.62  
##  CEOD_151  :  53   3rd Qu.:-69.29   3rd Qu.: -3.8000   3rd Qu.:-69.20  
##  CESA_75   :  53   Max.   :-61.24   Max.   :  0.2364   Max.   :-61.24  
##  (Other)   :5671                                                       
##     lat_est            dist_hav          dist_km         data     imp     
##  Min.   :-22.3667   Min.   :   6672   Min.   :   6.672   o:3000   i:3000  
##  1st Qu.:-11.6599   1st Qu.: 180853   1st Qu.: 180.853   r:3000   n:3000  
##  Median : -9.2807   Median : 628522   Median : 628.522                    
##  Mean   : -8.7478   Mean   : 758869   Mean   : 758.869                    
##  3rd Qu.: -4.5011   3rd Qu.:1111346   3rd Qu.:1111.346                    
##  Max.   :  0.2364   Max.   :3033755   Max.   :3033.755                    
##                                                                           
##      index         MAT_true        AP_true        MAT_est     
##  Min.   :   1   Min.   : 70.0   Min.   : 671   Min.   : 70.0  
##  1st Qu.:1501   1st Qu.:213.0   1st Qu.:1454   1st Qu.:213.0  
##  Median :3000   Median :251.0   Median :2084   Median :251.0  
##  Mean   :3000   Mean   :234.4   Mean   :2116   Mean   :234.4  
##  3rd Qu.:4500   3rd Qu.:263.0   3rd Qu.:2752   3rd Qu.:263.0  
##  Max.   :6000   Max.   :269.0   Max.   :4315   Max.   :269.0  
##                                                               
##      AP_est        MAT_diff    AP_diff 
##  Min.   : 671   Min.   :0   Min.   :0  
##  1st Qu.:1454   1st Qu.:0   1st Qu.:0  
##  Median :2084   Median :0   Median :0  
##  Mean   :2116   Mean   :0   Mean   :0  
##  3rd Qu.:2752   3rd Qu.:0   3rd Qu.:0  
##  Max.   :4315   Max.   :0   Max.   :0  
##