comparison brt.R @ 0:32849c52aa54 draft

planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 2a2ae892fa2dbc1eff9c6a59c3ad8f3c27c1c78d
author ecology
date Wed, 18 Oct 2023 09:59:19 +0000
parents
children edb8d19735a6
comparison
equal deleted inserted replaced
-1:000000000000 0:32849c52aa54
1 #16/02/2023
2 ## Analyse BRT data Ceamarc
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 }
25
26 ### load data
27
28 env = read.table(enviro, header = TRUE, dec = ".", na.strings = "-9999")
29 pred.vars = strsplit(abio_para, ",")[[1]]
30 data_files = strsplit(species_files,",")
31
32 #environemental parameters
33 #Carbo,Grav,Maxbearing,Maxmagnit,Meancurmag,Meansal,Meantheta,Mud,Prof,Rugosity,Sand,Seaice_prod,Sili,Slope,Standcurmag,Standsal,Standtheta
34
35 #Load functions
36
37 make.brt <- function(spe,data,pred.vars,env,nb_file){
38 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)
39 #plot
40 if (is.null(brt_step)==FALSE){
41 pdf(file = paste("BRT-",spe,".pdf"))
42 gbm.plot(brt_step, write.title = T,show.contrib = T, y.label = "fitted function",plot.layout = c(3,3))
43 dev.off()
44 #total deviance explained as (Leathwick et al., 2006)
45 total_deviance <- brt_step$self.statistics$mean.null
46 cross_validated_residual_deviance <- brt_step$cv.statistics$deviance.mean
47 total_deviance_explained <- (total_deviance - cross_validated_residual_deviance)/total_deviance
48 #Validation file
49 valid = cbind(spe,brt_step$cv.statistics$discrimination.mean,brt_step$gbm.call$tree.complexity,total_deviance_explained)
50 write.table(valid, paste(nb_file,"_brts_validation_ceamarc.tsv",sep=""), quote=FALSE, dec=".",sep="\t" ,row.names=F, col.names=F,append = T)}
51
52 return(brt_step)
53 }
54
55 make.prediction.brt <- function(brt_step){
56 #predictions
57 preds <- predict.gbm(brt_step,env,n.trees=brt_step$gbm.call$best.trees, type="response")
58 preds <- as.data.frame(cbind(env$lat,env$long,preds))
59 colnames(preds) <- c("lat","long","Prediction.index")
60 #carto
61 ggplot()+
62 geom_raster(data = preds , aes(x = long, y = lat, fill = Prediction.index))+
63 geom_raster(data = preds , aes(x = long, y = lat, alpha = Prediction.index))+
64 scale_alpha(range = c(0,1), guide = "none")+
65 scale_fill_viridis_c(
66 alpha = 1,
67 begin = 0,
68 end = 1,
69 direction = -1,
70 option = "D",
71 values = NULL,
72 space = "Lab",
73 na.value = "grey50",
74 guide = "colourbar",
75 aesthetics = "fill")+
76 xlab("Longitude") + ylab("Latitude")+ ggtitle(paste(spe,"Plot of BRT predictions"))+
77 theme(plot.title = element_text(size = 10))+
78 theme(axis.title.y = element_text(size = 10))+
79 theme(axis.title.x = element_text(size = 10))+
80 theme(axis.text.y = element_text(size = 10))+
81 theme(axis.text.x = element_text(size = 10))+
82 theme(legend.text = element_text(size = 10))+
83 theme(legend.title = element_text(size = 10))+
84 coord_quickmap()
85 output_directory <- ggsave(paste("BRT-",spe,"_pred_plot.png"))
86
87 #Write prediction in a file
88 preds <- cbind(preds,spe)
89 write.table(preds, paste(nb_file,"_brts_pred_ceamarc.txt",sep=""), quote=FALSE, dec=".", row.names=F, col.names=T,append = T)
90 }
91
92 #### RUN BRT ####
93 nb_file = 0
94
95 for (file in data_files[[1]]) {
96 species_data <- read.table(file, dec = ",", sep = ";", header = TRUE, na.strings = "na", colClasses = "numeric")
97 nb_file = nb_file + 1
98 `%!in%` <- Negate(`%in%`)
99 sp = list()
100 for (n in names(species_data)) {
101 if (n %!in% names(env) && n != 'station'){
102 sp = cbind(sp,n)
103 }
104 }
105
106 for (spe in sp){
107 try(make.prediction.brt(make.brt(spe,species_data,pred.vars,env,nb_file)))
108 }
109 }