annotate brt.R @ 4:2152f424e7a2 draft default tip

planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit c41939f1cdc03331ec021d47495576a6b0c5fd14
author ecology
date Wed, 16 Oct 2024 11:46:35 +0000
parents 18c4cd785a74
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
1 #16/02/2023
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
2 ## Analyse BRT data
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
3
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
4 ### Clean environment
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
5 rm(list = ls(all.names = TRUE))
3
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
6 options(warn=1)
1
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
7
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
8 ### load packages
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
9
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
10 library(dismo, warn.conflicts = FALSE)
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
11 library(gbm, warn.conflicts = FALSE)
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
12 library(ggplot2, warn.conflicts = FALSE)
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
13
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
14
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
15 #load arguments
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
16 args = commandArgs(trailingOnly=TRUE)
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
17 if (length(args)==0)
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
18 {
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
19 stop("This tool needs at least one argument")
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
20 }else{
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
21 enviro <- args[1]
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
22 species_files <- args[2]
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
23 abio_para <- args[3]
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
24 dec_env <- args[8]
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
25 dec_species <- args[9]
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
26 }
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
27
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
28 ### load data
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
29
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
30 env = read.table(enviro, dec = dec_env, header = TRUE, sep="\t", na.strings = "-9999")
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
31 pred_vars = strsplit(abio_para, ",")[[1]]
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
32 data_files = strsplit(species_files,",")
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
33
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
34 pred.vars <- character(length(pred_vars))
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
35
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
36 for (i in seq_along(pred_vars)) {
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
37 pred_var_col <- as.numeric(pred_vars[i])
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
38 pred.vars[i] <- names(env)[pred_var_col]}
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
39
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
40 #environemental parameters
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
41 #Carbo,Grav,Maxbearing,Maxmagnit,Meancurmag,Meansal,Meantheta,Mud,Prof,Rugosity,Sand,Seaice_prod,Sili,Slope,Standcurmag,Standsal,Standtheta
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
42
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
43 #Load functions
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
44
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
45 make.brt <- function(spe,data,pred.vars,env,nb_file){
3
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
46 cat(paste(" ", spe,":\n -> optimising BRT model ",sep=""))
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
47 lr <- 0.05
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
48 no.trees <- 0
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
49 while ( no.trees < 1000 & lr > 0.0005 ) {
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
50 cat(".")
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
51 try(brt_step <- gbm.step(data= data, gbm.x = pred.vars, gbm.y = spe, family = "bernoulli", tree.complexity = 2, learning.rate = lr,max.trees = 10000, plot.main = F))
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
52 # if the gbm does not converge, the return object is null or of size 0
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
53 if (!is.null(brt_step) ) {
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
54 if (object.size(brt_step) > 0 ) {
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
55 no.trees <- brt_step$gbm.call$best.trees
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
56 print(no.trees)
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
57 }
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
58 } else {
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
59 no.trees <- 0
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
60 print(no.trees)
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
61 }
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
62
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
63 # decrease the learning rate
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
64 lr <- lr / 2
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
65 print(lr)
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
66 }
1
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
67 #plot
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
68 if (is.null(brt_step)==FALSE){
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
69 pdf(file = paste("BRT-",spe,".pdf"))
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
70 gbm.plot(brt_step, write.title = T,show.contrib = T, y.label = "fitted function",plot.layout = c(3,3))
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
71 dev.off()
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
72 #total deviance explained as (Leathwick et al., 2006)
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
73 total_deviance <- brt_step$self.statistics$mean.null
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
74 cross_validated_residual_deviance <- brt_step$cv.statistics$deviance.mean
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
75 total_deviance_explained <- (total_deviance - cross_validated_residual_deviance)/total_deviance
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
76 #Validation file
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
77 valid = cbind(spe,brt_step$cv.statistics$discrimination.mean,brt_step$gbm.call$tree.complexity,total_deviance_explained)
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
78 write.table(valid, paste(nb_file,"_brts_validation_ceamarc.tsv",sep=""), quote=FALSE, dec=".",sep="\t" ,row.names=F, col.names=F,append = T)}
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
79
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
80 return(brt_step)
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
81 }
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
82
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
83 make.prediction.brt <- function(brt_step){
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
84 #predictions
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
85 preds <- predict.gbm(brt_step,env,n.trees=brt_step$gbm.call$best.trees, type="response")
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
86 preds <- as.data.frame(cbind(env$lat,env$long,preds))
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
87 colnames(preds) <- c("lat","long","Prediction.index")
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
88 #carto
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
89 ggplot()+
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
90 geom_raster(data = preds , aes(x = long, y = lat, fill = Prediction.index))+
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
91 geom_raster(data = preds , aes(x = long, y = lat, alpha = Prediction.index))+
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
92 scale_alpha(range = c(0,1), guide = "none")+
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
93 scale_fill_viridis_c(
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
94 alpha = 1,
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
95 begin = 0,
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
96 end = 1,
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
97 direction = -1,
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
98 option = "D",
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
99 values = NULL,
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
100 space = "Lab",
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
101 na.value = "grey50",
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
102 guide = "colourbar",
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
103 aesthetics = "fill")+
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
104 xlab("Longitude") + ylab("Latitude")+ ggtitle(paste(spe,"Plot of BRT predictions"))+
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
105 theme(plot.title = element_text(size = 10))+
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
106 theme(axis.title.y = element_text(size = 10))+
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
107 theme(axis.title.x = element_text(size = 10))+
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
108 theme(axis.text.y = element_text(size = 10))+
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
109 theme(axis.text.x = element_text(size = 10))+
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
110 theme(legend.text = element_text(size = 10))+
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
111 theme(legend.title = element_text(size = 10))+
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
112 coord_quickmap()
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
113 output_directory <- ggsave(paste("BRT-",spe,"_pred_plot.png"))
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
114
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
115 #Write prediction in a file
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
116 preds <- cbind(preds,spe)
3
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
117 write.table(preds, paste(nb_file,"_brts_pred_ceamarc.tsv",sep=""), quote=FALSE, dec=".", row.names=F, col.names=!file.exists(paste(nb_file,"_brts_pred_ceamarc.tsv",sep="")),append = T,sep="\t")
1
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
118 }
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
119
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
120 #### RUN BRT ####
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
121 nb_file = 0
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
122
3
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
123 # Creating the %!in% operator
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
124 `%!in%` <- Negate(`%in%`)
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
125
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
126 # Data file browsing
1
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
127 for (file in data_files[[1]]) {
3
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
128
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
129 # Reading the file
1
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
130 species_data <- read.table(file, dec = dec_species, sep = "\t", header = TRUE, na.strings = "NA", colClasses = "numeric")
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
131 nb_file = nb_file + 1
3
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
132
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
133 # List to store species to predict
1
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
134 sp = list()
3
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
135
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
136 # Selection of columns that are not in 'env' and that are not coordinates or stations
1
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
137 for (n in names(species_data)) {
3
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
138 if (n %!in% names(env) && n != 'station' && n != 'decimalLatitude' && n != 'decimalLongitude' && n!='lat' && n!='long'){
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
139 sp = c(sp,n)
1
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
140 }
3
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
141 }
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
142 # Making predictions for each species
1
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
143 for (spe in sp){
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
144 try(make.prediction.brt(make.brt(spe,species_data,pred.vars,env,nb_file)))
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
145 }
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
146 }
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
147
3
18c4cd785a74 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents: 1
diff changeset
148 #Display of abiotic parameters
1
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
149 cat("Here is the list of your abiotic parameters:\n")
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
150 cat(paste(pred.vars, collapse = ", "), "\n")
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
151
9dc992f80c25 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents: 0
diff changeset
152