################################################################################ # August 2019 # Corresponding author: Giulia Francesca Azzurra Donati, giulia.donati@usys.ethz.ch # # Main Script used in: # Donati et al. 2019: "A process-based-model supports an association between # dispersal and the prevalence of species traits in tropical reef fish assemblages" # accepted in Ecography, August 2019 # # Script input: simulated and observed data of dipersal-related ecological traits for reef fish species # Script output: Spatial congruences of simulations vs. observations: # geographic distribution of the areas where predominantly low dispersing or high # dispersing species were expected based on the simulations and the ones where # low-or high-dispersal- related trait values were observed for the investigated # traits. ################################################################################ ### Library loading library(rgdal) ### Functions loading source("https://www.envidat.ch/dataset/6b06ad48-bd85-47a8-b478-0850b6def1a4/resource/eed003d7-c661-42ab-bafb-c2fd2290d94e/download/donati_et_al_dispersal_global_prevalence_traits_functions.r") #source("Donati_et_al_Dispersal_Global_Prevalence_Traits_Functions.R") ### Data loading spt_gaspar <- read.csv(url("https://www.envidat.ch/dataset/6b06ad48-bd85-47a8-b478-0850b6def1a4/resource/5c3edf6e-b9e7-478e-8e65-15e8ee9ab20b/download/sp_ecological_trait_data.csv"),h=T) #dispersal-related trait data for reef fish species #spt_gaspar <- read.csv("sp_ecological_trait_data.csv",h=T) spm <- read.table(url("https://www.envidat.ch/dataset/6b06ad48-bd85-47a8-b478-0850b6def1a4/resource/471350ea-cbf8-409e-a8cc-55a84fe4a8cd/download/matrix_sim_sprichness.txt/matrix_Sim_SpRichness.txt")) # lat long and species richness per simulation #spm<-read.table("matrix_Sim_SpRichness.txt") spt <- read.table(url("https://www.envidat.ch/dataset/6b06ad48-bd85-47a8-b478-0850b6def1a4/resource/f895d8b6-3eff-4479-892b-8cc86b476a22/download/d_param_Sim.txt")) # dispersal parameter ranges from 1.75-5.45 for each simulation of sp #spt <- read.table("d_param_Sim.txt") spm_gaspar <- read.csv2(url("https://www.envidat.ch/dataset/6b06ad48-bd85-47a8-b478-0850b6def1a4/resource/f554c199-621c-4c60-9566-e3ae1d9479b0/download/fishnet_gaspar_code.csv", sep=";", dec=",")) # gaspar code and corresponding fishnet id #spm_gaspar <- read.csv2("Fishnet_Gaspar_code.csv",h=T) grid<- readOGR("./", "Grid_4326")# insert the path to the unzipped folder "Coral reef grid cells 5x5?? pL <- SpatialPoints(spm[,1:2],proj4string=CRS(proj4string(grid))) IDs <- over(pL, grid) IDs$Lid <- 1:dim(IDs)[1] pL <- SpatialPointsDataFrame(spm[,1:2], data=as.data.frame(IDs)) pL <- pL[which(!is.na(pL$FISHNET_ID)),] #filter only matching points ### Extract fishnet_ID FnID <- unique(pL$FISHNET_ID) FnID <- FnID[-which(FnID == 198)] coordinates <- cbind(coordinates(grid),grid$FISHNET_ID) colnames(coordinates)<-c("long","lat","FnID") # Extracting trait data per fishnet_ID ------------------------------------ Vect_traits <- c("Max_length","Home_Range","Schooling","vect_mPLD","vect_PLD_raw","PLD_range_mean","Activity") Res_list <- lapply(1:length(Vect_traits), function(z){ get_traits_data(FnID,var=Vect_traits [z],matPAgasp=spm_gaspar,trait_gasp=spt_gaspar) }) names(Res_list) <- c("size_gaspar","mobl_gaspar","school_gaspar","mPLD_gaspar", "PLD_gaspar_raw","PLD_range_gaspar","Activity") ### simulation dispersal trait (size_simu) size_simu <- lapply(FnID, function(x) { sel <- pL@data[which(pL@data$FISHNET_ID ==x),] dat_temp <- spm[sel$Lid, 3:dim(spm)[2]] sel_sp <- colSums(dat_temp) size <- spt$x[which(sel_sp > 0)] }) names(size_simu)=FnID ###======================= Simulation parameters calculation ===============### Par_sim <- get_traits_prop(spt=spt$x,size_simu=size_simu,probs=c(0.1,0.9)) ###======================= Observed parameters calculation =================### ### For continuous traits Obs_size <- get_traits_prop(spt=spt_gaspar$Max_length,size_simu=Res_list$size_gaspar,probs=c(0.1,0.9)) # observed size mPLD <- get_traits_prop(spt=spt_gaspar$vect_mPLD,size_simu=Res_list$mPLD_gaspar,probs=c(0.2,0.8)) # mPLD median PLD_raw <- get_traits_prop(spt=spt_gaspar$vect_PLD_raw,size_simu=Res_list$PLD_gaspar_raw,probs=c(0.2,0.8)) # PLD raw data (luiz et al. 2013) PLD_resampled <- get_traits_prop(spt=spt_gaspar$PLD_range_mean,size_simu=Res_list$PLD_range_gaspar,probs=c(0.2,0.8)) # resampled PLD over genus range ### For categorical parameters ric_gaspar_schooling <-sapply(Res_list$school_gaspar,length) ric_gaspar_activity <-sapply(Res_list$Activity,length) ric_gaspar_home_range <- sapply(Res_list$mobl_gaspar,length) mob_gaspar <- sapply(Res_list$mobl_gaspar, function (x) length(x[which(x=="VMob")])) sed_gaspar <- sapply(Res_list$mobl_gaspar, function (x) length(x[which(x %in% c("Mob", "Sed"))])) solpair_gaspar <- sapply(Res_list$school_gaspar, function (x) length(x[which(x %in% c("Sol", "Pair"))])) medlargeG_gaspar <- sapply(Res_list$school_gaspar, function (x) length(x[which(x %in% c("MedG", "LargeG"))])) day_activity <- sapply(Res_list$Activity, function (x) length(x[which(x=="Day")])) night_activity <- sapply(Res_list$Activity, function (x) length(x[which(x=="Night")])) prop_sed_gaspar <- sed_gaspar/ric_gaspar_home_range*100 prop_mob_gaspar <- mob_gaspar/ric_gaspar_home_range*100 prop_solpair_gaspar <- solpair_gaspar/ric_gaspar_schooling*100 prop_medtolargeG_gaspar <- medlargeG_gaspar/ric_gaspar_schooling*100 prop_day <- day_activity/ric_gaspar_activity*100 prop_night <- night_activity/ric_gaspar_activity*100 ###======================= Creating an attribute matrix ===================### mat <- do.call(rbind, lapply(FnID, function(x) { ricsimu <- Par_sim$ric[which(names(Par_sim$ric) == as.factor(x))] ricobs <- Obs_size$ric[which(names(Obs_size$ric) == as.factor(x))] ricpld <- mPLD$ric[which(names(mPLD$ric)== as.factor(x))] ricpldraw <- PLD_raw$ric[which(names(PLD_raw$ric)== as.factor(x))] ricpldrange <-PLD_resampled$ric[which(names(PLD_resampled$ric)== as.factor(x))] psmallsim <- Par_sim$prop_small[which(names(Par_sim$prop_small) == as.factor(x))] plargesim <- Par_sim$prop_large[which(names(Par_sim$prop_large) == as.factor(x))] psmallobs10 <- Obs_size$prop_small[which(names(Obs_size$prop_small) == as.factor(x))] plargeobs90 <- Obs_size$prop_large[which(names(Obs_size$prop_large) == as.factor(x))] pshort_pld <- mPLD$prop_small[which(names(mPLD$prop_small) == as.factor(x))] plong_pld <- mPLD$prop_large[which(names(mPLD$prop_large) == as.factor(x))] pshort_pldrange <- PLD_resampled$prop_small[which(names(PLD_resampled$prop_small) == as.factor(x))] plong_pldrange <- PLD_resampled$prop_large[which(names(PLD_resampled$prop_large) == as.factor(x))] pshort_pld_raw <- PLD_raw$prop_small[which(names(PLD_raw$prop_small) == as.factor(x))] plong_pld_raw <- PLD_raw$prop_large[which(names(PLD_raw$prop_large) == as.factor(x))] psedobs <- prop_sed_gaspar[which(names(prop_sed_gaspar) == as.factor(x))] pmobobs <- prop_mob_gaspar[which(names(prop_mob_gaspar) == as.factor(x))] psolpairobs <- prop_solpair_gaspar[which(names(prop_solpair_gaspar) == as.factor(x))] pmedtolargeG_gaspar <- prop_medtolargeG_gaspar[which(names(prop_medtolargeG_gaspar) == as.factor(x))] pprop_day <- prop_day[which(names(prop_day) == as.factor(x))] pprop_night <- prop_day[which(names(prop_night) == as.factor(x))] c(x,ricsimu,ricobs,ricpld,ricpldraw,ricpldrange, psmallsim,plargesim, psmallobs10,plargeobs90, pshort_pld,plong_pld, pshort_pldrange,plong_pldrange,pshort_pld_raw,plong_pld_raw, psedobs, pmobobs,psolpairobs, pmedtolargeG_gaspar, pprop_day,pprop_night) })) colnames(mat) = c("FnID","ricsimu","ricobs","ricpld","ricpldraw","ricpldrange","psmallsim","plargesim", "psmallobs10","plargeobs90","pshort_pld","plong_pld","pshort_pldrange","plong_pldrange", "pshort_pld_raw","plong_pld_raw","psedobs","pmobobs","psolpairobs","pmedtolargeG_gaspar", "pprop_day","pprop_night") grid_pl <- grid[which(grid$FISHNET_ID %in% FnID),] mat_data <- do.call(rbind,lapply(grid_pl$FISHNET_ID,function(x){mat[which(mat[,1]==x),]})) ###==================== Combining the matrix to the grid ===================### mat_data <- merge(coordinates, mat_data,by="FnID") mat_data_cong <- mat_data[-c(116,128),] ###========================= Save the data =================================### save(mat_data_cong,file="cong_sim_obs_trait_prop.Rdata") grid_pl@data = cbind(grid_pl@data, mat_data) write.csv(grid_pl@data,file="grid_pl_sim_obs_trait_prop_cong.csv") ################################################################################ ######################### End of the script ################################## ################################################################################ ## Next script : Donati_et_al_Dispersal_Global_Prevalence_Traits_Figures_main_text.R