annotate GeoNN.R @ 1:36637718c51d 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:58 +0000
parents 5cde56683579
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
5cde56683579 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
5cde56683579 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
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
3
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
4 #Load libraries
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
5 library(tidyr)
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
6 library(dplyr)
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
7 library(sf)
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
8
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
9 #load arguments
5cde56683579 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)
5cde56683579 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)
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
12 {
5cde56683579 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")
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
14 }else{
5cde56683579 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]
5cde56683579 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])
5cde56683579 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])
5cde56683579 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]
5cde56683579 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])
5cde56683579 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])
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
21 }
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
22
5cde56683579 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")
5cde56683579 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")
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
25
5cde56683579 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]))
5cde56683579 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]))
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
28
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
29 ###calculate distances###
5cde56683579 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
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
31
5cde56683579 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")
5cde56683579 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")
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
34
5cde56683579 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.
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
36
5cde56683579 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)
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
38
5cde56683579 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, ]
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
40
5cde56683579 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
5cde56683579 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)
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
43
5cde56683579 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
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
45
5cde56683579 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))
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
47
5cde56683579 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)) {
5cde56683579 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,])
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
50 }
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
51
5cde56683579 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
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
53
5cde56683579 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
5cde56683579 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)
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
56
5cde56683579 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
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
58
5cde56683579 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)
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
60
5cde56683579 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
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
62
5cde56683579 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(
5cde56683579 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,
5cde56683579 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],
5cde56683579 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
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
67 )
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
68
5cde56683579 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"
5cde56683579 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"
5cde56683579 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)"
5cde56683579 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
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
73
5cde56683579 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)
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
75
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
76
5cde56683579 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 5d48df67919fbc9d77b98a8243d438c397f61a0e
ecology
parents:
diff changeset
77