# packages
library(metaDigitise)
library(magrittr)
library(tibble)
library(dplyr)
library(lubridate)
library(dpeatdecomposition)
library(dm)
library(RMariaDB)
Connect to database
# connect to database
con <-
RMariaDB::dbConnect(
drv = RMariaDB::MariaDB(),
dbname = "dpeatdecomposition",
default.file = "~/my.cnf"
)
# get database as dm object
dm_dpeatdecomposition <-
dpeatdecomposition::dp_get_dm(con, learn_keys = TRUE)
Get most current IDs
id_last <-
list(
id_dataset =
dm_dpeatdecomposition %>%
dm::pull_tbl(datasets) %>%
dplyr::pull(id_dataset) %>%
tail(1),
id_sample =
dm_dpeatdecomposition %>%
dm::pull_tbl(samples) %>%
dplyr::pull(id_sample) %>%
tail(1),
id_measurement =
dm_dpeatdecomposition %>%
dm::pull_tbl(data) %>%
dplyr::pull(id_measurement) %>%
tail(1)
) %>%
purrr::map(function(.x) {
if(length(.x) == 0) {
0L
} else {
.x
}
})
Create directories
dir_name <- "d23"
dir_source <- "../raw_data/data/d23"
dir_target <- paste0("../derived_data/", id_last$id_dataset + 1L)
if(!dir.exists(dir_target)) {
dir.create(dir_target)
}
datasets <-
tibble::tibble(
id_dataset = id_last$id_dataset + 1L
)
citations_to_datasets <-
dplyr::bind_rows(
db_template_tables$citations_to_datasets,
tibble::tibble(
id_dataset = datasets$id_dataset,
id_citation = c("Scheffer.2001")
)
)
# mass remaining
samples3 <-
dplyr::bind_rows(
readRDS(paste0(dir_source, "/raw/caldat/Scheffer.2001-Fig1a"))$processed_data,
readRDS(paste0(dir_source, "/raw/caldat/Scheffer.2001-Fig1b"))$processed_data
) %>%
dplyr::select(id, mean, error) %>%
dplyr::rename(
mass_relative_mass = "mean",
mass_relative_mass_error = "error"
) %>%
dplyr::mutate(
mass_absolute = NA_real_,
mass_relative_mass = mass_relative_mass/100,
mass_relative_mass_error = mass_relative_mass_error/100,
mass_relative_mass_error_type = "se",
mass_relative_mass_sample_size = rep(c(5, 10, 10), 2 * 4),
site_label =
dplyr::case_when(
stringr::str_detect(id, "siteSphagnum") ~ "Sphagnum_site",
TRUE ~ "Carex_site"
),
taxon_rank_value =
dplyr::case_when(
stringr::str_detect(id, "papillosum") ~ "Sphagnum papillosum",
stringr::str_detect(id, "squarrosum") ~ "Sphagnum squarrosum",
stringr::str_detect(id, "diandra") ~ "Carex diandra",
stringr::str_detect(id, "lasiocarpa") ~ "Carex lasiocarpa"
),
sample_depth_upper = 0,
sample_depth_lower = 0,
index =
id %>%
stringr::str_extract("sampling\\d+$") %>%
stringr::str_remove("sampling") %>%
as.numeric(),
incubation_duration =
dplyr::case_when(
index == 1 ~ lubridate::dweeks(6),
index == 2 ~ lubridate::dmonths(12),
index == 3 ~ lubridate::dmonths(24)
) %>%
lubridate::time_length(unit = "days"),
sampling_date =
as.Date(as.Date("1993-05-15") + incubation_duration),
sampling_year = lubridate::year(sampling_date),
sampling_month = lubridate::month(sampling_date),
sampling_day = NA_real_,
mesh_size_absolute = 0.3,
is_incubated = TRUE,
incubation_environment = "peat",
id_dataset = datasets$id_dataset[[1]],
experimental_design = as.character(as.numeric(as.factor(site_label))),
sample_type = "litter",
sampling_longitude =
"5°07'E" %>%
sp::char2dms(chd = "°", chm = "'", chs = "''") %>%
as.numeric(),
sampling_latitude =
"52°10'N" %>%
sp::char2dms(chd = "°", chm = "'", chs = "''") %>%
as.numeric(),
taxon_rank_name = "species",
taxon_organ =
dplyr::case_when(
stringr::str_detect(taxon_rank_value, "Sphagnum") ~ "stems",
stringr::str_detect(taxon_rank_value, "Carex") ~ "aboveground parts"
),
sample_treatment = "control",
comments_samples = "Coordinates are only the approximate location of the study site, but not sampling points."
)
# initial mass
samples2 <-
samples3 %>%
dplyr::filter(! duplicated(paste0(site_label, "_", taxon_rank_value))) %>%
dplyr::mutate(
incubation_duration = 0.0,
mass_relative_mass = 1.0,
mass_relative_mass_error = 0.0,
sampling_date = as.Date("1993-05-15"),
sampling_year = lubridate::year(sampling_date),
sampling_month = lubridate::month(sampling_date),
sampling_day = NA_real_
)
# litter collection
samples1 <-
samples2 %>%
dplyr::filter(! duplicated(taxon_rank_value)) %>%
dplyr::mutate(
id_sample = seq_len(nrow(.)) + id_last$id_sample,
id_sample_origin = id_sample,
id_sample_parent = id_sample,
id_sample_incubation_start = NA_integer_,
sample_treatment = "control",
experimental_design = NA_character_,
is_incubated = FALSE,
incubation_environment = NA_character_,
site_label =
dplyr::case_when(
taxon_rank_value %in% c("Carex lasiocarpa", "Sphagnum papillosum") ~ NA_character_,
taxon_rank_value %in% c("Carex diandra") ~ "Carex_site",
taxon_rank_value %in% c("Sphagnum squarrosum") ~ "Sphagnum_site"
),
sampling_longitude =
dplyr::case_when(
is.na(site_label) ~ NA_real_,
TRUE ~ sampling_longitude
),
sampling_latitude =
dplyr::case_when(
is.na(site_label) ~ NA_real_,
TRUE ~ sampling_latitude
),
sampling_date =
dplyr::case_when(
stringr::str_detect(taxon_rank_value, "Sphagnum") ~ "1993-03-15",
taxon_rank_value == "Carex lasiocarpa" ~ "1992-10-15",
taxon_rank_value == "Carex diandra" ~ "1992-12-15"
) %>%
as.Date(),
sampling_year = lubridate::year(sampling_date),
sampling_month = lubridate::month(sampling_date),
sample_depth_upper =
dplyr::case_when(
stringr::str_detect(taxon_rank_value, "Sphagnum") ~ 2,
TRUE ~ NA_real_
),
sample_depth_lower =
dplyr::case_when(
stringr::str_detect(taxon_rank_value, "Sphagnum") ~ 5,
TRUE ~ NA_real_
),
comments_samples =
dplyr::case_when(
is.na(site_label) ~ "The exact sampling location is not mentioned in the text, it is just mentioned that this is a different site than where the litterbags were incubated.",
TRUE ~ "Coordinates are only the approximate location of the study site, but not sampling points."
),
comments_samples =
paste0(
comments_samples,
dplyr::case_when(
stringr::str_detect(taxon_rank_value, "Sphagnum") ~ " Litter is the first 3 cm of the brown part of Sphagnum. I assumed that the capitulum is the green part and is 2 cm long.",
stringr::str_detect(taxon_rank_value, "Carex") ~ " Litter are standing senesced aboveground parts of the plant."
)
)
)
# add missing ids
samples2 <-
dplyr::bind_rows(
samples2 %>%
dplyr::mutate(
type = "samples2"
),
samples3 %>%
dplyr::mutate(
type = "samples3"
)
)
samples2 <-
samples2 %>%
dplyr::mutate(
id_sample = seq_len(nrow(.)) + max(samples1$id_sample),
id_sample_origin =
dplyr::left_join(
samples2 %>% dplyr::select(taxon_rank_value),
samples1 %>% dplyr::select(taxon_rank_value, id_sample),
by = c("taxon_rank_value")
) %>%
dplyr::pull(id_sample),
id_sample_incubation_start =
purrr::map_int(seq_len(nrow(.)), function(i) {
index <- paste0(taxon_rank_value, "_", site_label, "_", taxon_organ) == paste0(taxon_rank_value, "_", site_label, "_", taxon_organ)[[i]] & experimental_design == experimental_design[[i]] & incubation_duration == 0.0
id_sample[index]
}),
id_sample_parent =
purrr::map_int(seq_len(nrow(.)), function(i) {
index <- paste0(taxon_rank_value, "_", site_label, "_", taxon_organ) == paste0(taxon_rank_value, "_", site_label, "_", taxon_organ)[[i]] & experimental_design == experimental_design[[i]] & incubation_duration < incubation_duration[[i]]
if(! any(index)) {
id_sample_origin[[i]]
} else {
target_incubation_duration <- max(incubation_duration[index])
index <- index & incubation_duration == target_incubation_duration
id_sample[index]
}
})
)
# litter chemistry
d23_litter_chemistry_initial <-
readODS::read_ods(paste0(dir_source, "/derived/Scheffer.2001-Tab1.ods")) %>%
dplyr::select(dplyr::starts_with(c("taxon_rank_value", "N", "P", "CN"), ignore.case = FALSE)) %>%
dplyr::rename(
N_relative_mass = "N",
N_relative_mass_error = "N_error",
N_relative_mass_sample_size = "N_sample_size",
N_relative_mass_error_type = "N_error_type",
P_relative_mass = "P",
P_relative_mass_error = "P_error",
P_relative_mass_sample_size = "P_sample_size",
P_relative_mass_error_type = "P_error_type",
C_to_N = "CN",
C_to_N_error = "CN_error",
C_to_N_sample_size = "CN_sample_size",
C_to_N_error_type = "CN_error_type",
) %>%
dplyr::mutate(
dplyr::across(dplyr::all_of(c(paste0(c("N", "P"), "_relative_mass"), paste0(c("N", "P"), "_relative_mass_error"))), magrittr::divide_by, 1000),
C_relative_mass = NA_real_,
C_absolute = NA_real_,
N_absolute = NA_real_,
P_absolute = NA_real_,
incubation_duration = 0.0
)
# N
d23_N <-
dplyr::bind_rows(
readRDS(paste0(dir_source, "/raw/caldat/Scheffer.2001-Fig2a"))$processed_data,
readRDS(paste0(dir_source, "/raw/caldat/Scheffer.2001-Fig2b"))$processed_data
) %>%
dplyr::select(id, mean, error) %>%
dplyr::rename(
N_relative_mass = "mean",
N_relative_mass_error = "error"
) %>%
dplyr::mutate(
dplyr::across(dplyr::starts_with("N_"), function(.x) .x/1000),
N_relative_mass_error_type = "se",
N_relative_mass_sample_size = rep(c(5, 10, 10), 2 * 4),
site_label =
dplyr::case_when(
stringr::str_detect(id, "siteSphagnum") ~ "Sphagnum_site",
TRUE ~ "Carex_site"
),
taxon_rank_value =
dplyr::case_when(
stringr::str_detect(id, "papillosum") ~ "Sphagnum papillosum",
stringr::str_detect(id, "squarrosum") ~ "Sphagnum squarrosum",
stringr::str_detect(id, "diandra") ~ "Carex diandra",
stringr::str_detect(id, "lasiocarpa") ~ "Carex lasiocarpa"
),
index =
id %>%
stringr::str_extract("sampling\\d+$") %>%
stringr::str_remove("sampling") %>%
as.numeric(),
incubation_duration =
dplyr::case_when(
index == 1 ~ lubridate::dweeks(6),
index == 2 ~ lubridate::dmonths(12),
index == 3 ~ lubridate::dmonths(24)
) %>%
lubridate::time_length(unit = "days")
) %>%
dplyr::select(-id, -index)
# P
d23_P <-
dplyr::bind_rows(
readRDS(paste0(dir_source, "/raw/caldat/Scheffer.2001-Fig3a"))$processed_data,
readRDS(paste0(dir_source, "/raw/caldat/Scheffer.2001-Fig3b"))$processed_data
) %>%
dplyr::select(id, mean, error) %>%
dplyr::rename(
P_relative_mass = "mean",
P_relative_mass_error = "error"
) %>%
dplyr::mutate(
dplyr::across(dplyr::starts_with("P_"), function(.x) .x/1000),
P_relative_mass_error_type = "se",
P_relative_mass_sample_size = rep(c(5, 10, 10), 2 * 4),
site_label =
dplyr::case_when(
stringr::str_detect(id, "siteSphagnum") ~ "Sphagnum_site",
TRUE ~ "Carex_site"
),
taxon_rank_value =
dplyr::case_when(
stringr::str_detect(id, "papillosum") ~ "Sphagnum papillosum",
stringr::str_detect(id, "squarrosum") ~ "Sphagnum squarrosum",
stringr::str_detect(id, "diandra") ~ "Carex diandra",
stringr::str_detect(id, "lasiocarpa") ~ "Carex lasiocarpa"
),
index =
id %>%
stringr::str_extract("sampling\\d+$") %>%
stringr::str_remove("sampling") %>%
as.numeric(),
incubation_duration =
dplyr::case_when(
index == 1 ~ lubridate::dweeks(6),
index == 2 ~ lubridate::dmonths(12),
index == 3 ~ lubridate::dmonths(24)
) %>%
lubridate::time_length(unit = "days")
) %>%
dplyr::select(-id, -index)
# CN
d23_CN <-
dplyr::bind_rows(
readRDS(paste0(dir_source, "/raw/caldat/Scheffer.2001-Fig2c"))$processed_data,
readRDS(paste0(dir_source, "/raw/caldat/Scheffer.2001-Fig2d"))$processed_data
) %>%
dplyr::select(id, mean, error) %>%
dplyr::rename(
C_to_N = "mean",
C_to_N_error = "error"
) %>%
dplyr::mutate(
C_to_N_error_type = "se",
C_to_N_sample_size = rep(c(5, 10, 10), 2 * 4),
site_label =
dplyr::case_when(
stringr::str_detect(id, "siteSphagnum") ~ "Sphagnum_site",
TRUE ~ "Carex_site"
),
taxon_rank_value =
dplyr::case_when(
stringr::str_detect(id, "papillosum") ~ "Sphagnum papillosum",
stringr::str_detect(id, "squarrosum") ~ "Sphagnum squarrosum",
stringr::str_detect(id, "diandra") ~ "Carex diandra",
stringr::str_detect(id, "lasiocarpa") ~ "Carex lasiocarpa"
),
index =
id %>%
stringr::str_extract("sampling\\d+$") %>%
stringr::str_remove("sampling") %>%
as.numeric(),
incubation_duration =
dplyr::case_when(
index == 1 ~ lubridate::dweeks(6),
index == 2 ~ lubridate::dmonths(12),
index == 3 ~ lubridate::dmonths(24)
) %>%
lubridate::time_length(unit = "days")
) %>%
dplyr::select(-id, -index)
# combine
d23_litter_chemistry <-
dplyr::bind_rows(
d23_litter_chemistry_initial %>%
dplyr::mutate(
site_label = "Carex_site"
),
d23_litter_chemistry_initial %>%
dplyr::mutate(
site_label = "Sphagnum_site"
),
purrr::reduce(list(d23_N, d23_P, d23_CN), dplyr::left_join, by = c("site_label", "taxon_rank_value", "incubation_duration"))
)
# add to samples2
samples2 <-
dplyr::left_join(
samples2,
d23_litter_chemistry,
by = c("site_label", "taxon_rank_value", "incubation_duration")
)
## combine
samples <-
dplyr::bind_rows(
db_template_tables$samples,
samples1 %>%
dplyr::mutate(
type = "samples1"
),
samples2
) %>%
dplyr::mutate(
site_name = site_label
)
samples_to_samples <-
samples %>%
dplyr::filter(! id_sample %in% id_sample_origin) %>%
dplyr::mutate(
transition_description =
dplyr::case_when(
type %in% c("samples2") ~ "translocate",
type %in% c("samples3") ~ "wait",
TRUE ~ NA_character_
)
) %>%
dplyr::select(id_sample_parent, id_sample, transition_description) %>%
dplyr::rename(
id_sample_child = "id_sample"
)
d2 <-
samples2 %>%
tidyr::pivot_longer(
cols = dplyr::all_of(c("mass_absolute", "mass_relative_mass", "mesh_size_absolute", "C_to_N", "N_relative_mass", "C_relative_mass", "P_relative_mass", "C_absolute", "N_absolute", "P_absolute")),
names_to = "attribute_name",
values_to = "value"
) %>%
dplyr::mutate(
id_measurement = seq_len(nrow(.)) + id_last$id_measurement,
id_measurement_numerator =
purrr::map_int(seq_len(nrow(.)), function(i) {
switch(
attribute_name[[i]],
"C_to_N" = {
id_measurement[id_sample == id_sample[[i]] & attribute_name == "C_relative_mass"]
},
"P_relative_mass" = ,
"N_relative_mass" = ,
"C_relative_mass" = {
id_measurement[id_sample == id_sample[[i]] & attribute_name == stringr::str_replace(attribute_name[[i]], "_relative_mass", "_absolute")]
},
"mass_relative_mass" = {
id_measurement[id_sample == id_sample[[i]] & attribute_name == "mass_absolute"]
},
NA_integer_
)
}),
id_measurement_denominator =
purrr::map_int(seq_len(nrow(.)), function(i) {
switch(
attribute_name[[i]],
"C_to_N" = {
id_measurement[id_sample == id_sample[[i]] & attribute_name == "N_relative_mass"]
},
"P_relative_mass" = ,
"N_relative_mass" = ,
"C_relative_mass" = {
id_measurement[id_sample == id_sample[[i]] & attribute_name == "mass_absolute"]
},
"mass_relative_mass" = {
id_measurement[id_sample == id_sample_incubation_start[[i]] & attribute_name == "mass_absolute"]
},
NA_integer_
)
}),
value_type =
dplyr::case_when(
attribute_name == "mesh_size_absolute" ~ "point",
TRUE ~ "mean"
)
)
d2_sample_size <-
samples2 %>%
tidyr::pivot_longer(
cols = dplyr::ends_with("_sample_size"),
names_to = "attribute_name",
values_to = "sample_size"
) %>%
dplyr::mutate(
attribute_name =
attribute_name %>%
stringr::str_remove(pattern = "_sample_size$"),
attribute_name =
dplyr::case_when(
attribute_name == "mass_remaining" ~ "mass_relative_mass",
attribute_name == "N" ~ "N_relative_mass",
attribute_name == "P" ~ "P_relative_mass",
attribute_name == "CN" ~ "C_to_N",
TRUE ~ attribute_name
)
) %>%
dplyr::select(id_sample, attribute_name, sample_size)
d2_error <-
samples2 %>%
tidyr::pivot_longer(
cols = dplyr::ends_with(c("_error")),
names_to = "attribute_name",
values_to = "error"
) %>%
dplyr::mutate(
attribute_name =
attribute_name %>%
stringr::str_remove(pattern = "_error$")
) %>%
dplyr::select(id_sample, attribute_name, error)
d2_error_type <-
samples2 %>%
tidyr::pivot_longer(
cols = dplyr::ends_with(c("_error_type")),
names_to = "attribute_name",
values_to = "error_type"
) %>%
dplyr::mutate(
attribute_name =
attribute_name %>%
stringr::str_remove(pattern = "_error_type$")
) %>%
dplyr::select(id_sample, attribute_name, error_type)
d2 <-
d2 %>%
dplyr::mutate(
error =
dplyr::left_join(d2, d2_error, by = c("id_sample", "attribute_name")) %>%
dplyr::pull(error),
error_type =
dplyr::left_join(d2, d2_error_type, by = c("id_sample", "attribute_name")) %>%
dplyr::pull(error_type),
sample_size =
dplyr::left_join(d2, d2_sample_size, by = c("id_sample", "attribute_name")) %>%
dplyr::pull(sample_size)
)
# combine
d <-
dplyr::bind_rows(
db_template_tables$data,
d2
) %>%
dplyr::select(dplyr::all_of(colnames(db_template_tables$data)))
experimental_design_format <-
tibble::tibble(
id_dataset = datasets$id_dataset,
file = paste0(id_last$id_dataset + 1L, "/experimental_design_format.csv"),
experimental_design_description = "`site_name`: Name of the site."
)
# csv file to export
experimental_design_format2 <-
samples %>%
dplyr::filter(! is.na(experimental_design)) %>%
dplyr::filter(! duplicated(experimental_design)) %>%
dplyr::select(experimental_design, site_name)
# export
write.csv(experimental_design_format2, paste0(dir_target, "/experimental_design_format.csv"), row.names = FALSE)
# list all tables
dm_insert_in <-
list(
datasets =
datasets %>%
dplyr::select(dplyr::all_of(colnames(dm_dpeatdecomposition$datasets))),
samples =
samples %>%
dplyr::select(dplyr::all_of(colnames(dm_dpeatdecomposition$samples))),
data =
d %>%
dplyr::select(dplyr::all_of(colnames(dm_dpeatdecomposition$data))),
samples_to_samples =
samples_to_samples %>%
dplyr::select(dplyr::all_of(colnames(dm_dpeatdecomposition$samples_to_samples))),
citations_to_datasets =
citations_to_datasets %>%
dplyr::select(dplyr::all_of(colnames(dm_dpeatdecomposition$citations_to_datasets))),
experimental_design_format =
experimental_design_format %>%
dplyr::select(dplyr::all_of(colnames(dm_dpeatdecomposition$experimental_design_format)))
)
# check whether all column names as present in table attributes
all_column_names <-
purrr::map(dm_insert_in, colnames) %>%
unlist() %>%
unique()
if(! all(all_column_names %in% (dm_dpeatdecomposition %>% dm::pull_tbl(attributes) %>% dplyr::pull(attribute_name)))) {
cond <- purrr::map_lgl(all_column_names, function(.x) ! .x %in% (dm_dpeatdecomposition %>% dm::pull_tbl(attributes) %>% dplyr::pull(attribute_name)))
RMariaDB::dbDisconnect(con)
stop(paste0("New `attribute_name`s discovered: ", paste(all_column_names[cond], collapse = ", ")))
}
all_data_attributes <- unique(dm_insert_in$data$attribute_name)
if(! all(all_data_attributes %in% (dm_dpeatdecomposition %>% dm::pull_tbl(attributes) %>% dplyr::pull(attribute_name)))) {
RMariaDB::dbDisconnect(con)
cond <- purrr::map_lgl(all_data_attributes, function(.x) ! .x %in% (dm_dpeatdecomposition %>% dm::pull_tbl(attributes) %>% dplyr::pull(attribute_name)))
stop(paste0("New `attribute_name`s discovered: ", paste(all_data_attributes[cond], collapse = ", ")))
}
# filter empty tables
dm_insert_in_check <-
dm_insert_in[purrr::map_lgl(dm_insert_in, function(x) nrow(x) > 0)] %>%
dm::as_dm() %>%
dp_dm_add_keys(dm_dpeatdecomposition)
# copy into dm_pmird
for(i in seq_along(dm_insert_in)) {
RMariaDB::dbAppendTable(con, name = names(dm_insert_in)[[i]], value = dm_insert_in[[i]])
}
RMariaDB::dbDisconnect(con)