Mercurial > repos > ecology > ecoregion_clara_cluster
annotate GeoNN.R @ 3:844b5bf84164 draft
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
author | ecology |
---|---|
date | Wed, 11 Sep 2024 09:19:33 +0000 |
parents | 24a81631a8b5 |
children |
rev | line source |
---|---|
2
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
1 #Date : 09/02/2024 |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
2 #Author : Seguineau Pauline |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
3 |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
4 #Load libraries |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
5 library(tidyr) |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
6 library(dplyr) |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
7 library(sf) |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
8 |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
9 #load arguments |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
10 args = commandArgs(trailingOnly=TRUE) |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
11 if (length(args)==0) |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
12 { |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
13 stop("This tool needs at least one argument") |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
14 }else{ |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
15 enviro <- args[1] |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
16 envlong <- as.numeric(args[2]) |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
17 envlat <- as.numeric(args[3]) |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
18 occu <- args[4] |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
19 occulat <- as.numeric(args[5]) |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
20 occulong <- as.numeric(args[6]) |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
21 } |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
22 |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
23 env = read.table(enviro, header = TRUE, sep="\t") |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
24 occ = read.table(occu, header = TRUE, sep = "\t") |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
25 |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
26 cols_env = c(names(env[envlong]),names(env[envlat])) |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
27 cols_occ = c(names(occ[occulong]),names(occ[occulat])) |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
28 |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
29 ###calculate distances### |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
30 #transform tables into sf object |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
31 |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
32 env_sf <- st_as_sf(env, coords = cols_env, crs = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs") |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
33 occ_sf <- st_as_sf(occ, coords = cols_occ, crs = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs") |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
34 |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
35 #Find the indices of env_sf entities closest to each point in occ_sf. |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
36 |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
37 nearest_indices <- st_nearest_feature(occ_sf, env_sf) |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
38 |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
39 nearest_points <- env[nearest_indices, ] |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
40 |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
41 # Calculate distances between env_sf and occ_sf points |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
42 distances <- st_distance(env_sf, occ_sf) |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
43 |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
44 #Extract the corresponding distances between occ and env |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
45 |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
46 nearest_distances <- numeric(length(nearest_indices)) |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
47 |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
48 for (i in 1:length(nearest_indices)) { |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
49 nearest_distances[i] <- st_distance(env_sf[nearest_indices[i],], occ_sf[i,]) |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
50 } |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
51 |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
52 #assemble occurrences and environmental parameters in the same file |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
53 |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
54 nearest_points <- nearest_points[, !names(nearest_points) %in% cols_env] #remove lat and long from env to clean data |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
55 new_occ = cbind(occ, nearest_points) |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
56 |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
57 #Save the file |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
58 |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
59 write.table(new_occ, file = "occurrence_env.tsv",sep ="\t",quote = F, row.names = F,col.names = T) |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
60 |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
61 #create an information file with the distances between the points of the two files |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
62 |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
63 distance_info <- data.frame( |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
64 occ_geometry = occ_sf$geometry, |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
65 env_geometry = env_sf$geometry[nearest_indices], |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
66 distance = nearest_distances |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
67 ) |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
68 |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
69 colnames(distance_info)[1] <- "occ_geometry" |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
70 colnames(distance_info)[2] <- "env_geometry" |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
71 colnames(distance_info)[3] <- "Distances (meters)" |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
72 #save the information file |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
73 |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
74 write.table(distance_info, file = "infos_file.tsv",sep ="\t",quote = F, row.names = F,col.names = T) |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
75 |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
76 |
24a81631a8b5
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff
changeset
|
77 |