Mercurial > repos > ecology > ecoregion_brt_analysis
annotate brt.R @ 5:3cc4b8bbce00 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:05 +0000 |
parents | a56c413f3a98 |
children |
rev | line source |
---|---|
1
fc621f3f8226
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 |
fc621f3f8226
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 |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
3 |
fc621f3f8226
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 |
fc621f3f8226
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
a56c413f3a98
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
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
7 |
fc621f3f8226
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 |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
9 |
fc621f3f8226
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) |
fc621f3f8226
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) |
fc621f3f8226
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) |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
13 |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
14 |
fc621f3f8226
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 |
fc621f3f8226
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) |
fc621f3f8226
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) |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
18 { |
fc621f3f8226
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") |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
20 }else{ |
fc621f3f8226
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] |
fc621f3f8226
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] |
fc621f3f8226
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] |
fc621f3f8226
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] |
fc621f3f8226
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] |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
26 } |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
27 |
fc621f3f8226
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 |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
29 |
fc621f3f8226
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") |
fc621f3f8226
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]] |
fc621f3f8226
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,",") |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
33 |
fc621f3f8226
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)) |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
35 |
fc621f3f8226
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)) { |
fc621f3f8226
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]) |
fc621f3f8226
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]} |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
39 |
fc621f3f8226
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 |
fc621f3f8226
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 |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
42 |
fc621f3f8226
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 |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
44 |
fc621f3f8226
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
a56c413f3a98
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="")) |
a56c413f3a98
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 |
a56c413f3a98
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 |
a56c413f3a98
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 ) { |
a56c413f3a98
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents:
1
diff
changeset
|
50 cat(".") |
a56c413f3a98
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)) |
a56c413f3a98
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 |
a56c413f3a98
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) ) { |
a56c413f3a98
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 ) { |
a56c413f3a98
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 |
a56c413f3a98
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) |
a56c413f3a98
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents:
1
diff
changeset
|
57 } |
a56c413f3a98
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents:
1
diff
changeset
|
58 } else { |
a56c413f3a98
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 |
a56c413f3a98
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) |
a56c413f3a98
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents:
1
diff
changeset
|
61 } |
a56c413f3a98
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents:
1
diff
changeset
|
62 |
a56c413f3a98
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 |
a56c413f3a98
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 |
a56c413f3a98
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) |
a56c413f3a98
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents:
1
diff
changeset
|
66 } |
1
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
67 #plot |
fc621f3f8226
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){ |
fc621f3f8226
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")) |
fc621f3f8226
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)) |
fc621f3f8226
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() |
fc621f3f8226
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) |
fc621f3f8226
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 |
fc621f3f8226
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 |
fc621f3f8226
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 |
fc621f3f8226
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 |
fc621f3f8226
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) |
fc621f3f8226
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)} |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
79 |
fc621f3f8226
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) |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
81 } |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
82 |
fc621f3f8226
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){ |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
84 #predictions |
fc621f3f8226
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") |
fc621f3f8226
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)) |
fc621f3f8226
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") |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
88 #carto |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
89 ggplot()+ |
fc621f3f8226
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))+ |
fc621f3f8226
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))+ |
fc621f3f8226
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")+ |
fc621f3f8226
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( |
fc621f3f8226
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, |
fc621f3f8226
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, |
fc621f3f8226
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, |
fc621f3f8226
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, |
fc621f3f8226
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", |
fc621f3f8226
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, |
fc621f3f8226
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", |
fc621f3f8226
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", |
fc621f3f8226
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", |
fc621f3f8226
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")+ |
fc621f3f8226
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"))+ |
fc621f3f8226
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))+ |
fc621f3f8226
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))+ |
fc621f3f8226
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))+ |
fc621f3f8226
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))+ |
fc621f3f8226
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))+ |
fc621f3f8226
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))+ |
fc621f3f8226
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))+ |
fc621f3f8226
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() |
fc621f3f8226
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")) |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
114 |
fc621f3f8226
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 |
fc621f3f8226
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
a56c413f3a98
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
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
118 } |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
119 |
fc621f3f8226
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 #### |
fc621f3f8226
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 |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
122 |
3
a56c413f3a98
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 |
a56c413f3a98
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%`) |
a56c413f3a98
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents:
1
diff
changeset
|
125 |
a56c413f3a98
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
fc621f3f8226
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
a56c413f3a98
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents:
1
diff
changeset
|
128 |
a56c413f3a98
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
fc621f3f8226
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") |
fc621f3f8226
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
a56c413f3a98
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents:
1
diff
changeset
|
132 |
a56c413f3a98
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
fc621f3f8226
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
a56c413f3a98
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents:
1
diff
changeset
|
135 |
a56c413f3a98
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
fc621f3f8226
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
a56c413f3a98
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'){ |
a56c413f3a98
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
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
140 } |
3
a56c413f3a98
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
ecology
parents:
1
diff
changeset
|
141 } |
a56c413f3a98
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
fc621f3f8226
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){ |
fc621f3f8226
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))) |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
145 } |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
146 } |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
147 |
3
a56c413f3a98
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
fc621f3f8226
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") |
fc621f3f8226
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") |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
151 |
fc621f3f8226
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 459ba1277acd7d8d4a02f90dbd7ff444bf8eac92
ecology
parents:
0
diff
changeset
|
152 |