comparison brt.R @ 0:5cde56683579 draft

planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
author ecology
date Thu, 21 Mar 2024 14:05:01 +0000
parents
children 36637718c51d
comparison
equal deleted inserted replaced
-1:000000000000 0:5cde56683579
1 #16/02/2023
2 ## Analyse BRT data
3
4 ### Clean environment
5 rm(list = ls(all.names = TRUE))
6 options(warn=-1)
7
8 ### load packages
9
10 library(dismo, warn.conflicts = FALSE)
11 library(gbm, warn.conflicts = FALSE)
12 library(ggplot2, warn.conflicts = FALSE)
13
14
15 #load arguments
16 args = commandArgs(trailingOnly=TRUE)
17 if (length(args)==0)
18 {
19 stop("This tool needs at least one argument")
20 }else{
21 enviro <- args[1]
22 species_files <- args[2]
23 abio_para <- args[3]
24 dec_env <- args[8]
25 dec_species <- args[9]
26 }
27
28 ### load data
29
30 env = read.table(enviro, dec = dec_env, header = TRUE, sep="\t", na.strings = "-9999")
31 pred_vars = strsplit(abio_para, ",")[[1]]
32 data_files = strsplit(species_files,",")
33
34 pred.vars <- character(length(pred_vars))
35
36 for (i in seq_along(pred_vars)) {
37 pred_var_col <- as.numeric(pred_vars[i])
38 pred.vars[i] <- names(env)[pred_var_col]}
39
40 #environemental parameters
41 #Carbo,Grav,Maxbearing,Maxmagnit,Meancurmag,Meansal,Meantheta,Mud,Prof,Rugosity,Sand,Seaice_prod,Sili,Slope,Standcurmag,Standsal,Standtheta
42
43 #Load functions
44
45 make.brt <- function(spe,data,pred.vars,env,nb_file){
46 brt_step <- gbm.step(data= data, gbm.x = pred.vars, gbm.y = spe, family = "bernoulli", tree.complexity = 2, learning.rate = 0.0001,max.trees = 10000,plot.main = F)
47 #plot
48 if (is.null(brt_step)==FALSE){
49 pdf(file = paste("BRT-",spe,".pdf"))
50 gbm.plot(brt_step, write.title = T,show.contrib = T, y.label = "fitted function",plot.layout = c(3,3))
51 dev.off()
52 #total deviance explained as (Leathwick et al., 2006)
53 total_deviance <- brt_step$self.statistics$mean.null
54 cross_validated_residual_deviance <- brt_step$cv.statistics$deviance.mean
55 total_deviance_explained <- (total_deviance - cross_validated_residual_deviance)/total_deviance
56 #Validation file
57 valid = cbind(spe,brt_step$cv.statistics$discrimination.mean,brt_step$gbm.call$tree.complexity,total_deviance_explained)
58 write.table(valid, paste(nb_file,"_brts_validation_ceamarc.tsv",sep=""), quote=FALSE, dec=".",sep="\t" ,row.names=F, col.names=F,append = T)}
59
60 return(brt_step)
61 }
62
63 make.prediction.brt <- function(brt_step){
64 #predictions
65 preds <- predict.gbm(brt_step,env,n.trees=brt_step$gbm.call$best.trees, type="response")
66 preds <- as.data.frame(cbind(env$lat,env$long,preds))
67 colnames(preds) <- c("lat","long","Prediction.index")
68 #carto
69 ggplot()+
70 geom_raster(data = preds , aes(x = long, y = lat, fill = Prediction.index))+
71 geom_raster(data = preds , aes(x = long, y = lat, alpha = Prediction.index))+
72 scale_alpha(range = c(0,1), guide = "none")+
73 scale_fill_viridis_c(
74 alpha = 1,
75 begin = 0,
76 end = 1,
77 direction = -1,
78 option = "D",
79 values = NULL,
80 space = "Lab",
81 na.value = "grey50",
82 guide = "colourbar",
83 aesthetics = "fill")+
84 xlab("Longitude") + ylab("Latitude")+ ggtitle(paste(spe,"Plot of BRT predictions"))+
85 theme(plot.title = element_text(size = 10))+
86 theme(axis.title.y = element_text(size = 10))+
87 theme(axis.title.x = element_text(size = 10))+
88 theme(axis.text.y = element_text(size = 10))+
89 theme(axis.text.x = element_text(size = 10))+
90 theme(legend.text = element_text(size = 10))+
91 theme(legend.title = element_text(size = 10))+
92 coord_quickmap()
93 output_directory <- ggsave(paste("BRT-",spe,"_pred_plot.png"))
94
95 #Write prediction in a file
96 preds <- cbind(preds,spe)
97 write.table(preds, paste(nb_file,"_brts_pred_ceamarc.tsv",sep=""), quote=FALSE, dec=".", row.names=F, col.names=T,append = T,sep="\t")
98 }
99
100 #### RUN BRT ####
101 nb_file = 0
102
103 for (file in data_files[[1]]) {
104 species_data <- read.table(file, dec = dec_species, sep = "\t", header = TRUE, na.strings = "NA", colClasses = "numeric")
105 nb_file = nb_file + 1
106 `%!in%` <- Negate(`%in%`)
107 sp = list()
108 for (n in names(species_data)) {
109 if (n %!in% names(env) && n != 'station'){
110 sp = cbind(sp,n)
111 }
112 }
113 for (spe in sp){
114 try(make.prediction.brt(make.brt(spe,species_data,pred.vars,env,nb_file)))
115 }
116 }
117
118 cat("Here is the list of your abiotic parameters:\n")
119 cat(paste(pred.vars, collapse = ", "), "\n")
120
121