-
Notifications
You must be signed in to change notification settings - Fork 12
/
dwc_mapping.Rmd
468 lines (334 loc) · 8.77 KB
/
dwc_mapping.Rmd
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
---
title: "Darwin Core mapping"
subtitle: "For: my_dataset_title"
author:
- author_1
- author_2
date: "`r Sys.Date()`"
output:
html_document:
df_print: paged
number_sections: yes
toc: yes
toc_depth: 3
toc_float: yes
---
<!-- For examples, see https://github.com/trias-project/checklist-recipe/wiki/Examples -->
# Setup
```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = TRUE)
```
Install required libraries (only if the libraries have not been installed before):
```{r}
installed <- rownames(installed.packages())
required <- c("tidyverse", "tidylog", "magrittr", "here", "janitor", "readxl", "digest", "rgbif")
if (!all(required %in% installed)) {
install.packages(required[!required %in% installed])
}
```
Load libraries:
```{r message = FALSE}
library(tidyverse) # To do data science
library(tidylog) # To provide feedback on dplyr functions
library(magrittr) # To use %<>% pipes
library(here) # To find files
library(janitor) # To clean input data
library(readxl) # To read Excel files
library(digest) # To generate hashes
library(rgbif) # To use GBIF services
```
# Read source data
Create a data frame `input_data` from the source data:
```{r}
input_data <- read_excel(path = here("data", "raw", "checklist.xlsx"))
```
Preview data:
```{r}
input_data %>% head(n = 5)
```
# Process source data
## Tidy data
Clean data somewhat:
```{r}
input_data %<>% remove_empty("rows")
```
## Scientific names
Use the [GBIF nameparser](https://www.gbif.org/tools/name-parser) to retrieve nomenclatural information for the scientific names in the checklist:
```{r}
parsed_names <- input_data %>%
distinct(scientific_name) %>%
pull() %>% # Create vector from dataframe
parsenames() # An rgbif function
```
Show scientific names with nomenclatural issues, i.e. not of `type = SCIENTIFIC` or that could not be fully parsed. Note: these are not necessarily incorrect.
```{r}
parsed_names %>%
select(scientificname, type, parsed, parsedpartially, rankmarker) %>%
filter(!(type == "SCIENTIFIC" & parsed == "TRUE" & parsedpartially == "FALSE"))
```
Correct names and reparse:
```{r correct and reparse}
input_data %<>% mutate(scientific_name = recode(scientific_name,
"AseroÙ rubra" = "Asero rubra"
))
# Redo parsing
parsed_names <- input_data %>%
distinct(scientific_name) %>%
pull() %>%
parsenames()
# Show names with nomenclatural issues again
parsed_names %>%
select(scientificname, type, parsed, parsedpartially, rankmarker) %>%
filter(!(type == "SCIENTIFIC" & parsed == "TRUE" & parsedpartially == "FALSE"))
```
## Taxon ranks
The nameparser function also provides information about the rank of the taxon (in `rankmarker`). Here we join this information with our checklist. Cleaning these ranks will done in the Taxon Core mapping:
```{r}
input_data %<>% left_join(
parsed_names %>%
select(scientificname, rankmarker),
by = c("scientific_name" = "scientificname"))
```
## Taxon IDs
To link taxa with information in the extension(s), each taxon needs a unique and relatively stable `taxonID`. Here we create one in the form of `dataset_shortname:taxon:hash`, where `hash` is unique code based on scientific name and kingdom (that will remain the same as long as scientific name and kingdom remain the same):
```{r}
vdigest <- Vectorize(digest) # Vectorize digest function to work with vectors
input_data %<>% mutate(taxon_id = paste(
"my_dataset_shortname", # e.g. "alien-fishes-checklist"
"taxon",
vdigest(paste(scientific_name, kingdom), algo = "md5"),
sep = ":"
))
```
## Preview data
Show the number of taxa and distributions per kingdom and rank:
```{r}
input_data %>%
group_by(kingdom, rankmarker) %>%
summarize(
`# taxa` = n_distinct(taxon_id),
`# distributions` = n()
) %>%
adorn_totals("row")
```
Preview data:
```{r}
input_data %>% head()
```
# Taxon core
## Pre-processing
Create a dataframe with unique taxa only (ignoring multiple distribution rows):
```{r}
taxon <- input_data %>% distinct(taxon_id, .keep_all = TRUE)
```
## Term mapping
Map the data to [Darwin Core Taxon](http://rs.gbif.org/core/dwc_taxon_2015-04-24.xml).
Start with record-level terms which contain metadata about the dataset (which is generally the same for all records).
### language
```{r}
taxon %<>% mutate(dwc_language = "my_language") # e.g. "en"
```
### license
```{r}
taxon %<>% mutate(dwc_license = "my_license") # e.g. "http://creativecommons.org/publicdomain/zero/1.0/"
```
### rightsHolder
```{r}
taxon %<>% mutate(dwc_rightsHolder = "my_rights_holder") # e.g. "INBO"
```
### datasetID
```{r}
taxon %<>% mutate(dwc_datasetID = "my_dataset_doi") # e.g. "https://doi.org/10.15468/xvuzfh"
```
### institutionCode
```{r}
taxon %<>% mutate(dwc_institutionCode = "my_institution_code") # e.g. "INBO"
```
### datasetName
```{r}
taxon %<>% mutate(dwc_datasetName = "my_dataset_title") # e.g. "Checklist of non-native freshwater fishes in Flanders, Belgium"
```
The following terms contain information about the taxon:
### taxonID
```{r}
taxon %<>% mutate(dwc_taxonID = taxon_id)
```
### scientificName
```{r}
taxon %<>% mutate(dwc_scientificName = scientific_name)
```
### kingdom
Inspect values:
```{r}
taxon %>%
group_by(kingdom) %>%
count()
```
Map values:
```{r}
taxon %<>% mutate(dwc_kingdom = kingdom)
```
### taxonRank
Inspect values:
```{r}
taxon %>%
group_by(rankmarker) %>%
count()
```
Map values by recoding to the [GBIF rank vocabulary](http://rs.gbif.org/vocabulary/gbif/rank_2015-04-24.xml):
```{r}
taxon %<>% mutate(dwc_taxonRank = recode(rankmarker,
"agg." = "speciesAggregate",
"infrasp." = "infraspecificname",
"sp." = "species",
"var." = "variety",
.default = "",
.missing = ""
))
```
Inspect mapped values:
```{r}
taxon %>%
group_by(rankmarker, dwc_taxonRank) %>%
count()
```
### nomenclaturalCode
```{r}
taxon %<>% mutate(dwc_nomenclaturalCode = "my_nomenclaturalCode") # e.g. "ICZN"
```
## Post-processing
Only keep the Darwin Core columns:
```{r}
taxon %<>% select(starts_with("dwc_"))
```
Drop the `dwc_` prefix:
```{r}
colnames(taxon) <- str_remove(colnames(taxon), "dwc_")
```
Preview data:
```{r}
taxon %>% head()
```
Save to CSV:
```{r}
write_csv(taxon, here("data", "processed", "taxon.csv"), na = "")
```
# Distribution extension
## Pre-processing
Create a dataframe with all data:
```{r}
distribution <- input_data
```
## Term mapping
Map the data to [Species Distribution](http://rs.gbif.org/extension/gbif/1.0/distribution.xml).
### taxonID
```{r}
distribution %<>% mutate(dwc_taxonID = taxon_id)
```
### locality
Inspect values:
```{r}
distribution %>%
group_by(country_code, locality) %>%
count()
```
Map values to `locality` if provided, otherwise use the country name:
```{r}
distribution %<>% mutate(dwc_locality = case_when(
!is.na(locality) ~ locality,
country_code == "BE" ~ "Belgium",
country_code == "GB" ~ "United Kingdom",
country_code == "MK" ~ "Macedonia",
country_code == "NL" ~ "The Netherlands",
TRUE ~ "" # In other cases leave empty
))
```
Inspect mapped values:
```{r}
distribution %>%
group_by(country_code, locality, dwc_locality) %>%
count()
```
### countryCode
Inspect values:
```{r}
distribution %>%
group_by(country_code) %>%
count()
```
Map values:
```{r}
distribution %<>% mutate(dwc_countryCode = country_code)
```
### occurrenceStatus
Inspect values:
```{r}
distribution %>%
group_by(occurrence_status) %>%
count()
```
Map values:
```{r}
distribution %<>% mutate(dwc_occurrenceStatus = occurrence_status)
```
### threatStatus
Inspect values:
```{r}
distribution %>%
group_by(threat_status) %>%
count()
```
Map values by recoding to the [IUCN threat status vocabulary](http://rs.gbif.org/vocabulary/iucn/threat_status.xml):
```{r}
distribution %<>% mutate(dwc_threatStatus = recode(threat_status,
"endangered" = "EN",
"vulnerable" = "VU"
))
```
Inspect mapped values:
```{r}
distribution %>%
group_by(threat_status, dwc_threatStatus) %>%
count()
```
### source
Inspect values:
```{r}
distribution %>%
group_by(source) %>%
count() %>%
head() # Remove to show all values
```
Map values:
```{r}
distribution %<>% mutate(dwc_source = source)
```
### occurrenceRemarks
Inspect values:
```{r}
distribution %>%
group_by(remarks) %>%
count() %>%
head() # Remove to show all values
```
Map values:
```{r}
distribution %<>% mutate(dwc_occurrenceRemarks = remarks)
```
## Post-processing
Only keep the Darwin Core columns:
```{r}
distribution %<>% select(starts_with("dwc_"))
```
Drop the `dwc_` prefix:
```{r}
colnames(distribution) <- str_remove(colnames(distribution), "dwc_")
```
Preview data:
```{r}
distribution %>% head()
```
Save to CSV:
```{r}
write_csv(distribution, here("data", "processed", "distribution.csv"), na = "")
```