-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubmit_niche_analysis.R
executable file
·128 lines (108 loc) · 3.52 KB
/
submit_niche_analysis.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env Rscript
# Command-Line Interface (CLI) for submitting bee niche analyses through the
# utilization of plant interaction data on the
# [SPECIES platform](https://chilam.c3.unam.mx/en/projects/about-species).
# The script requires pre-existing bee files containing valid plant information
# in the designated data folder to function effectively.
library(argparse)
library(cli)
library(dplyr, warn.conflicts = FALSE)
library(tibble)
library(httr)
library(stringr)
options(scipen = 999)
FOOTPRINT <- 4 # Analysis over North America Region
config <- config::get()
parser <- ArgumentParser(description="Script to register bee-plant niche interaction
analysis in SPECIES platform")
parser$add_argument('bee_sp_code', type="character",
help=str_glue("Bee code based on {config$bee_sp_codes_file} list"))
args <- parser$parse_args()
sp_code <- args$bee_sp_code
cli_alert_info("Processing bee: {sp_code}")
BEE_SP_EXAMPLE <- args[1]
# BEE_SP_EXAMPLE <- "AGAOBL"
# Data loading and processing ----
# Bee sps
bee_sps_data <- readr::read_csv(config$bee_sp_codes_file,
show_col_types = FALSE)
bee_sp <- bee_sps_data |>
filter(ClaveSp == sp_code)
if (nrow(bee_sp) == 0) {
cli_abort("Could not find bee with code: {sp_code}")
}
bee_name <- paste(bee_sp["Género"], bee_sp["Especie"])
# Interaction bee-plant
interaction_data <- readr::read_csv(
fs::path_join(c(config$data_folder, str_glue("{sp_code}_valid.csv")))
)
bee_data <- tibble(
taxon_rank = "species",
value = bee_name
)
plant_data <- interaction_data |>
select(`Nombre Válido`, `Rango Taxonómico`) |>
rename(value=`Nombre Válido`) |>
distinct() |>
mutate(
rank = case_when(
`Rango Taxonómico` == "Familia" ~ "family",
`Rango Taxonómico` == "Género" ~ "genus",
`Rango Taxonómico` == "Especie" ~ "species",
.default = NA
),
type = 0,
level = "species"
) |>
filter(!is.na(rank)) |>
select(-`Rango Taxonómico`)
covariables <- tibble(
name = "plants",
biotic = TRUE,
group_item = 1,
merge_vars = list(plant_data)
)
# Create data analysis request ----
URL1 <- "http://species.conabio.gob.mx/api/dbdev/niche/countsTaxonsGroup"
# Body object
data <- list(
target_taxons = bee_data,
covariables = covariables,
apriori = FALSE,
mapa_prob = FALSE,
min_cells = 5,
fosil = TRUE,
date = TRUE,
grid_resolution = "32",
region = FOOTPRINT,
data_source = "gbif",
with_data_score_cell = TRUE,
with_data_freq = TRUE,
with_data_freq_cell = TRUE,
with_data_score_decil = TRUE,
iterations = 1,
tipo_procedencia = "API",
genTokenAndSaveResults = TRUE
)
# NOTE: To save the 'data' object to a file, you can use the following code
# data %>%
# jsonlite::write_json(
# pretty = TRUE,
# auto_unbox = TRUE,
# path = "testAnalysisCommunity.json"
# )
# For debugging purposes
resp_dataset_creation <- POST(URL1, body = data, encode = "json")
# NOTE: to explore the analysis results visit
# https://species.conabio.gob.mx/dbdev/geoportal_v0.1.html#link/?token=<token_value>
token <- content(resp_dataset_creation)$token
URL_RESULT <- "https://species.conabio.gob.mx/dbdev/geoportal_v0.1.html#link/?token="
url <- str_glue("{URL_RESULT}{token}")
result_data <- list(
"sp_code" = sp_code,
"token" = token
) %>% jsonlite::toJSON(auto_unbox = TRUE)
cli_alert_success(c("Analysis registered with information:\n",
"{result_data}\n",
"To view analysis visit:\n",
"{.url {url}}"))