-
Notifications
You must be signed in to change notification settings - Fork 0
/
05_occurrence_indicators_preprocessing.Rmd
696 lines (561 loc) · 15.8 KB
/
05_occurrence_indicators_preprocessing.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
---
title: "Occurrence indicators: pre-processing"
author:
- Damiano Oldoni
- Toon Van Daele
- Tim Adriaens
date: "`r Sys.Date()`"
output:
html_document:
toc: true
toc_depth: 3
toc_float: true
number_sections: true
---
This document describes the pre-processing of occurrence data of alien species. The outputs of this pipeline will be used as input for modelling.
# Setup
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE)
```
Load libraries:
```{r load_libraries, message = FALSE}
library(dplyr) # To do data science
library(readr)
library(magrittr)
library(purrr)
library(stringr)
library(tidyr)
library(tidylog) # To provide feedback on dplyr functions
library(progress) # To add progress bars
library(here) # To find files
library(lubridate) # To work with dates
library(rgbif) # To get taxa from publisehd unified checklist
```
# Get data
We get occurrence data as obtained in `trias-project/occ-processing` repository.
## Occurrence data
### Alien species
We load occurrence data related to alien species in Belgium:
```{r read_data_cube, message = FALSE, warning = FALSE}
df <- read_csv(
file = "https://zenodo.org/records/10527772/files/be_alientaxa_cube.csv?download=1",
col_types = cols(
year = col_double(),
eea_cell_code = col_character(),
taxonKey = col_double(),
n = col_double(),
min_coord_uncertainty = col_double()
),
na = ""
) %>%
write_csv("./data/output/UAT_processing/be_alientaxa_cube.csv")
```
Nota: these data have been also published on [Zenodo](https://doi.org/10.5281/zenodo.3632749).
We remove columns we will never use:
```{r remove_coords_uncertainty}
df <-
df %>%
select(-min_coord_uncertainty)
```
and rename column `n` to `obs`:
```{r rename_n_to_obs}
df <-
df %>%
rename(obs = n)
```
### Baseline
We get occurrence data of non alien species grouped at class level, which will be used for correcting the research bias effort:
```{r get_data_baseline}
df_bl <- read_csv(
file = "https://zenodo.org/records/10527772/files/be_classes_cube.csv?download=1",
col_types = cols(
year = col_double(),
eea_cell_code = col_character(),
classKey = col_double(),
n = col_double(),
min_coord_uncertainty = col_double()
),
na = ""
)
```
Nota: these data have been also published on [Zenodo](https://doi.org/10.5281/zenodo.3632749).
We remove columns we will never use:
```{r remove_coords_uncertainty_baseline}
df_bl <-
df_bl %>%
select(-min_coord_uncertainty)
```
and rename column `n` to `cobs`:
```{r rename_n_to_obs_bl}
df_bl <-
df_bl %>%
rename(cobs = n)
```
## Checklist data
We read input data based on [Global Register of Introduced and Invasive Species - Belgium](https://www.gbif.org/dataset/6d9e952f-948c-4483-9807-575348147c7e), typically referred to as the _unified checklist_. These data contain taxonomic information as well distribution, description and species profiles. We limit to taxa introduced in Belgium (`locationId` : `ISO_3166:BE`)
```{r unified_checklist_tidy_df}
data_file <- here::here(
"data",
"output",
"UAT_processing",
"data_input_checklist_indicators.tsv"
)
taxa_df <-
read_tsv(data_file,
na = "",
guess_max = 5000
)
taxa_df <-
taxa_df %>%
filter(locationId == "ISO_3166:BE")
```
## Remove some taxa
### Taxa in unified checklist without occurrences
There is a group of taxa defined in the unified checklist which are not present in the occurrence data, i.e. the presence of these taxa, although confirmed by authoritative's checklists, is still not sustained by observations:
```{r taxa_without_occs}
alien_taxa_without_occs <-
taxa_df %>%
filter(!nubKey %in% df$taxonKey) %>%
distinct(
key,
nubKey,
canonicalName,
first_observed,
last_observed,
class,
kingdom
) %>%
arrange(nubKey)
alien_taxa_without_occs
```
where `key` is the unique GBIF identifier of the taxon as published in the unified checklist, while `nubKey` is the unique GBIF identifier of the taxon in the GBIF Backbone Taxonomy.
We save this data.frame as `alien_taxa_without_occurrences.tsv` in `data/output`:
```{r save_alien_taxa_without_occs}
write_tsv(
alien_taxa_without_occs,
here::here("data", "output", "alien_taxa_without_occs.tsv"),
na = ""
)
```
## Grid cells intersecting protected areas
We get information about intersection of grid cells with Belgian protected areas by reading file `intersect_EEA_ref_grid_protected_areas.tsv` in `data/output`:
```{r read_protected_areas}
df_prot_areas <- read_tsv(
here::here(
"data",
"interim",
"intersect_EEA_ref_grid_protected_areas.tsv"
),
na = ""
)
```
Preview:
```{r preview_prot_areas}
df_prot_areas %>%
head()
```
We are interested to areas included in Natura2000. We remove columns we are not interested to:
```{r select_cols_prot_areas}
df_prot_areas <-
df_prot_areas %>%
select(
CELLCODE,
natura2000
)
```
# Pre-processing
## Add informative columns
For better interpretation of the data, we retrieve scientific names (without authorship) of the species whose data we are going to analyze. We also retrieve the class of each alien species to link occurrence data with the related baseline data:
```{r get_canonical_names}
taxon_key <-
df %>%
distinct(taxonKey) %>%
pull()
pb <- progress_bar$new(total = length(taxon_key))
spec_names <- map_df(
taxon_key,
function(k) {
pb$tick()
name_usage(key = k)$data
}
) %>%
select(
taxonKey = key,
canonicalName,
scientificName,
kingdomKey, classKey
) %>%
mutate(canonicalName = ifelse(
is.na(canonicalName), scientificName, canonicalName
))
```
In the rare case multiple taxa share the same scientific name, authorship is added:
```{r add_authorship_if_needed}
spec_names <-
spec_names %>%
group_by(canonicalName) %>%
add_tally() %>%
ungroup() %>%
mutate(canonicalName = if_else(n > 1,
scientificName,
canonicalName)) %>%
select(-c(n, scientificName))
```
We also retrieve kingdom and class:
```{r get_kingodm_class}
class_key <-
spec_names %>%
distinct(classKey) %>%
filter(!is.na(classKey)) %>%
pull()
pb <- progress_bar$new(total = length(class_key))
kingdom_class <- map_df(
class_key,
function(x) {
pb$tick()
name_usage(key = x)$data
}
) %>%
select(classKey, class, kingdomKey, kingdom)
```
and we add them:
```{r add_kingdom_class}
# add class
spec_names <-
spec_names %>%
left_join(kingdom_class %>%
distinct(.data$classKey, .data$class),
by = "classKey"
)
# add kingdom
spec_names <-
spec_names %>%
left_join(kingdom_class %>%
distinct(.data$kingdomKey, .data$kingdom),
by = "kingdomKey"
)
```
Preview:
```{r preview_species}
spec_names %>% head(10)
```
Taxa not assigned to any class:
```{r taxa_no_class}
spec_names %>%
filter(is.na(classKey))
```
## Extract x-y coordinates from cellcodes
Extract coordinates from column `eea_cell_code`. For example, the coordinates of cellcode `1kmE3895N3116` are `x = 3895` and `y = 3116`:
```{r extract_x_y}
df_xy <-
df %>%
distinct(eea_cell_code) %>%
bind_cols(
tibble(
x = unlist(str_extract_all(unique(df$eea_cell_code),
pattern = "(?<=E)\\d+"
)),
y = unlist(str_extract_all(unique(df$eea_cell_code),
pattern = "(?<=N)\\d+"
))
) %>%
mutate_all(as.integer)
)
```
Preview:
```{r x_y_preview}
df_xy %>% head()
```
Do the same for baseline data:
```{r remove corrupted eea_cell_code}
df_bl <- df_bl %>%
mutate(cellcode_length = nchar(eea_cell_code))
table(df_bl$cellcode_length, useNA = "ifany")
corrupt_bl_eea_cell_codes <- df_bl %>%
filter(cellcode_length != 13) %>%
write_csv("./data/interim/corrupt_bl_eea_cell_codes.csv")
df_bl <- df_bl %>%
filter(cellcode_length == 13)
```
```{r extract_x_y_baseline}
df_bl_xy <-
df_bl %>%
distinct(eea_cell_code) %>%
bind_cols(
tibble(
x = unlist(str_extract_all(unique(df_bl$eea_cell_code),
pattern = "(?<=E)\\d+"
)),
y = unlist(str_extract_all(unique(df_bl$eea_cell_code),
pattern = "(?<=N)\\d+"
))
) %>%
mutate_all(as.integer)
)
```
Preview:
```{r x_y_preview_baseline}
df_bl_xy %>% head()
```
## Apply temporal and spatial restraints
Select alien species introduced after 1950 (`year_of_introduction`: 1950) or whose date of introduction is not known:
```{r select_aliens_after_year_of_introduction}
year_of_introduction <- 1950
recent_alien_species <-
taxa_df %>%
# lower limit on date of first introduction if present
filter(first_observed >= year_of_introduction | is.na(first_observed)) %>%
# remove duplicates due to other columns as pathways
distinct(nubKey, first_observed, last_observed) %>%
# get classkey info and filter out taxa without data
inner_join(spec_names, by = c("nubKey" = "taxonKey")) %>%
distinct(
taxonKey = nubKey,
.data$canonicalName,
.data$first_observed,
.data$last_observed,
.data$class,
.data$kingdom,
.data$classKey,
.data$kingdomKey
)
```
Number of taxa recently introduced or whose introduction year is not specified:
```{r n_recent_taxa}
nrow(recent_alien_species)
```
List of alien species excluded as first introduction date is earlier than 1950:
```{r recent_alien_species}
old_introductions_taxa <-
taxa_df %>%
filter(first_observed < year_of_introduction) %>%
anti_join(recent_alien_species %>%
select(taxonKey),
by = c("nubKey" = "taxonKey")
) %>%
distinct(
nubKey,
first_observed,
last_observed
) %>%
inner_join(spec_names, by = c("nubKey" = "taxonKey")) %>%
select(
taxonKey = nubKey,
canonicalName,
first_observed,
last_observed,
class,
kingdom,
everything()
)
old_introductions_taxa
```
We save them in file `taxa_introduced_in_BE_before_1950.tsv` in `data/output`:
```{r save_old_introduced_taxa_as_tsv_file}
write_tsv(old_introductions_taxa,
here::here(
"data",
"output",
paste0(
"taxa_introduced_in_BE_before_",
year_of_introduction,
".tsv"
)
),
na = ""
)
```
We also limit the time series analysis on observations from 1950, start of state of invasion researches:
```{r define_year_range}
first_year <- 1950
```
Some observations can be published with erroneous coordinates (see [#trias-project/occ-processing#13](https://github.com/trias-project/occ-processing/issues/13)) or can be assigned to a grid cell out of Belgium and should therefore removed.
```{r define_x_y_range_values}
be_cellcodes <- df_prot_areas$CELLCODE
```
Apply spatial and temporal restraints defined above to both occurrence data:
```{r apply_restraints_time_space_occ_data}
df <-
df %>%
# recently introduced taxa
filter(taxonKey %in% recent_alien_species$taxonKey) %>%
left_join(df_xy, by = "eea_cell_code") %>%
# recent history
filter(year > first_year) %>%
# in Belgium
filter(eea_cell_code %in% be_cellcodes)
```
and baseline data:
```{r apply_restraints_time_space_baseline_data}
df_bl <-
df_bl %>%
left_join(df_bl_xy, by = "eea_cell_code") %>%
# recent history
filter(year > first_year) %>%
# in Belgium
filter(eea_cell_code %in% be_cellcodes)
```
## Add information about presence in protected areas
We add whether the grid cell intersects any of the Natura2000 Belgian protected areas.
```{r add_natura2000_col}
df <-
df %>%
left_join(df_prot_areas,
by = c("eea_cell_code" = "CELLCODE")
)
```
Preview:
```{r preview_df_with_natura2000_col}
df %>% head()
```
We do the same for baseline data:
```{r add_natura2000_col_baseline}
df_bl <-
df_bl %>%
left_join(df_prot_areas,
by = c("eea_cell_code" = "CELLCODE")
)
```
Preview:
```{r preview_df_bl_with_natura2000_col}
df_bl %>% head()
```
## Create time series
For each species, define cells with at least one observation:
```{r distinct_cells_taxa}
df_cc <-
df %>%
group_by(taxonKey) %>%
distinct(eea_cell_code) %>%
ungroup()
```
For each species, identify the first year with at least one observation:
```{r begin_year_per_species}
df_begin_year <-
df %>%
group_by(taxonKey) %>%
summarize(begin_year = min(year))
```
For each species, combine `begin_year` and unique `eea_cell_code` as found above:
```{r combine_begin_year_cells_taxa}
df_cc <-
df_cc %>%
left_join(df_begin_year, by = "taxonKey") %>%
select(taxonKey, begin_year, eea_cell_code)
```
Preview:
```{r preview_begin_year_cells_combo}
df_cc %>% head()
```
For each cell (`eea_cell_code`) and species (`taxonKey`) we can now create a time series:
```{r make_ts, cache=TRUE}
make_time_series <- function(eea_cell_code, taxonKey, begin_year, last_year ) {
expand_grid(eea_cell_code = eea_cell_code,
taxonKey = taxonKey,
year = seq(from = begin_year, to = last_year))
}
# create timeseries slots
df_ts <- pmap_dfr(df_cc,
.f = make_time_series,
last_year = year(Sys.Date())
)
## Add data
# add occurrence data
df_ts <-
df_ts %>%
left_join(df %>% select(taxonKey, year, eea_cell_code, obs),
by = c("taxonKey", "year", "eea_cell_code"))
# add membership to protected areas
df_ts <-
df_ts %>%
left_join(df_prot_areas %>% select(CELLCODE, natura2000),
by = c("eea_cell_code" = "CELLCODE"))
# add classKey
df_ts <-
df_ts %>%
left_join(spec_names %>%
select(taxonKey, classKey),
by = "taxonKey")
```
## Research effort correction
To correct the effect of research effort of an alien speies, we calculate the number of observations at class level excluding the observations of the alien species itself:
```{r correct_cobs_research_effort_bias}
# add baseline data (at class level) diminished by obs of specific alien taxon
df_ts <-
df_ts %>%
left_join(df_bl %>%
select(year, eea_cell_code, classKey, cobs),
by = c("year", "eea_cell_code", "classKey")) %>%
mutate(cobs = cobs - obs)
# replace NAs with 0
df_ts <-
df_ts %>%
replace_na(list(cobs = 0, obs = 0))
```
Preview:
```{r preview_df_ts}
df_ts %>% head(n = 30)
```
Add column for presence (1) or absence (0)
```{r add_presence_absence}
df_ts <-
df_ts %>%
mutate(pa_cobs = if_else(cobs > 0, 1, 0),
pa_obs = if_else(obs > 0, 1, 0))
```
Arrange order columns:
```{r set_order_columns}
df_ts <-
df_ts %>%
select(taxonKey,
year,
eea_cell_code,
obs,
pa_obs,
cobs,
pa_cobs,
classKey,
natura2000)
```
# Save data
## Save timeseries and taxonomic metadata
Save `df_ts` as it will be used as start point of modelling pipelines:
```{r set creds}
Sys.setenv("AWS_DEFAULT_REGION" = "eu-west-1")
if(Sys.getenv("S3_BUCKET") != ""){
UAT_bucket <- Sys.getenv("S3_BUCKET")
}else{
UAT_bucket <- Sys.getenv("UAT_bucket")
}
```
```{r connect to bucket, eval=FALSE}
source("./src/connect_to_bucket.R")
connect_to_bucket(bucket_name = UAT_bucket)
```
```{r save_data_as_tsv}
# upload data to S3 bucket
aws.s3::s3save(df_ts,
object = "df_timeseries.Rdata",
bucket = UAT_bucket,
opts = list(show_progress = TRUE,
multipart = TRUE))
```
Create time series data
input: "df_timeseries.RData" and "grid.RData" from bucket
output: full_timeseries.RData
```{r}
alienSpecies::createTimeseries(
# read grid.RData from bucket
shapeData = alienSpecies::loadShapeData("grid.RData")$utm1_bel_with_regions,
bucket = UAT_bucket
)
```
We save also the taxonomic information:
```{r save_spec_names, eval = FALSE}
write_tsv(spec_names,
path = here::here("data",
"interim",
"timeseries_taxonomic_info.tsv"),
na = ""
)
```