-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_02_preparing_KSSL_library.R
165 lines (122 loc) · 4.59 KB
/
script_02_preparing_KSSL_library.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
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
## Loading packages
library("tidyverse")
library("readr")
library("readxl")
library("purrr")
library("curl")
library("qs")
# Mounted disk for storing big files
mnt.dir <- "~/projects/mnt-ringtrial/"
dir.preprocessed <- paste0(mnt.dir, "preprocessed/")
## KSSL
kssl.folder <- paste0("~/projects/soilspec4gg-mac/heavydata/KSSL15ksample/")
kssl.subset <- readRDS(paste0(kssl.folder, "ctransfer_kssl_spectra.rds"))
kssl.subset
names(kssl.subset[,1:5])
kssl.subset %>%
group_by(sample_id) %>%
summarise(repeats = n()) %>%
count(repeats)
kssl.subset.test <- kssl.subset %>%
sample_n(100) %>%
select(1:5)
kssl.potassium <- read_csv(paste0(kssl.folder, "clhs/KSSL_cLHS_15k_subset_potassium.csv"))
kssl.potassium
kssl.potassium %>%
mutate(test = gsub("XS|XN", "", id)) %>%
count(test, name = "repeats") %>%
count(repeats)
kssl.potassium <- kssl.potassium %>%
rename(sample_id = id) %>%
mutate(sample_id = gsub("XS|XN", "", sample_id)) %>%
mutate(sample_id = as.integer(sample_id))
kssl.potassium
subset.spectral.range <- as.character(seq(630, 4000, by = 2))
kssl.potassium.join <- kssl.potassium %>%
filter(sample_id %in% kssl.subset$sample_id) %>%
select(sample_id, K)
kssl.potassium.bind <- kssl.potassium %>%
filter(!(sample_id %in% kssl.subset$sample_id)) %>%
select(sample_id, K, all_of(subset.spectral.range))
kssl.subset.full <- kssl.subset %>%
right_join(kssl.potassium.join, ., by = "sample_id") %>%
bind_rows(kssl.potassium.bind)
kssl.subset.full %>%
select(-all_of(subset.spectral.range)) %>%
summarise_all(function(x){sum(!is.na(x))})
# Renaming
kssl.subset <- kssl.subset.full %>%
rename(clay_perc = clay,
pH_H20 = pH,
carbon_tot_perc = TC,
carbon_org_perc = OC,
potassium_cmolkg = K)
kssl.subset
new.spectra.range <- as.character(seq(650, 4000, by = 2))
important.columns <- c("sample_id", "clay_perc", "pH_H20",
"carbon_tot_perc", "carbon_org_perc",
"potassium_cmolkg")
kssl.subset <- kssl.subset %>%
select(all_of(important.columns),
all_of(new.spectra.range))
kssl.subset
write_csv(kssl.subset, paste0(dir.preprocessed, "KSSL_soilMIRspectra_raw.csv"))
## BOC
kssl.subset
kssl.subset.SGS <- kssl.subset %>%
select(-all_of(important.columns)) %>%
as.matrix() %>%
prospectr::savitzkyGolay(X = ., m = 0, p = 2, w = 11, delta.wav = 2) %>%
bind_cols({kssl.subset %>%
select(all_of(important.columns))}, .)
kssl.subset.SGS
kssl.subset.baseline <- kssl.subset.SGS %>%
select(sample_id, any_of(new.spectra.range)) %>%
rowwise(sample_id) %>%
summarise(baseline = min(c_across(everything()))) %>%
left_join(kssl.subset.SGS, by = "sample_id") %>%
ungroup()
kssl.subset.baseline
kssl.subset.BC <- kssl.subset.baseline %>%
pivot_longer(-all_of(c(important.columns, "baseline")),
names_to = "wavenumber", values_to = "absorbance") %>%
mutate(absorbance = absorbance-baseline) %>%
pivot_wider(id_cols = all_of(c(important.columns, "baseline")),
names_from = "wavenumber", values_from = "absorbance") %>%
select(-baseline) %>%
mutate_if(is.numeric, round, digits = 5)
kssl.subset.BC
write_csv(kssl.subset.BC, paste0(dir.preprocessed, "KSSL_soilMIRspectra_BOC.csv"))
## SG First Derivative
kssl.subset
kssl.subset.1stDer <- kssl.subset %>%
select(-all_of(important.columns)) %>%
as.matrix() %>%
prospectr::savitzkyGolay(X = ., m = 1, p = 2, w = 11, delta.wav = 2) %>%
bind_cols({kssl.subset %>%
select(all_of(important.columns))}, .) %>%
mutate_if(is.numeric, round, digits = 8)
kssl.subset.1stDer
write_csv(kssl.subset.1stDer, paste0(dir.preprocessed, "KSSL_soilMIRspectra_SG1stDer.csv"))
## Standard Normal Variate
kssl.subset.SNV <- kssl.subset %>%
select(-all_of(important.columns)) %>%
as.matrix() %>%
prospectr::savitzkyGolay(X = ., m = 0, p = 2, w = 11, delta.wav = 2) %>%
prospectr::standardNormalVariate(X = .) %>%
bind_cols({kssl.subset %>%
select(all_of(important.columns))}, .) %>%
mutate_if(is.numeric, round, digits = 5)
kssl.subset.SNV
write_csv(kssl.subset.SNV, paste0(dir.preprocessed, "KSSL_soilMIRspectra_SNV.csv"))
## SNV + SG1stDer
kssl.subset.SNVplusSG1stDer <- kssl.subset %>%
select(-all_of(important.columns)) %>%
as.matrix() %>%
prospectr::standardNormalVariate(X = .) %>%
prospectr::savitzkyGolay(X = ., m = 1, p = 2, w = 11, delta.wav = 2) %>%
bind_cols({kssl.subset %>%
select(all_of(important.columns))}, .) %>%
mutate_if(is.numeric, round, digits = 8)
kssl.subset.SNVplusSG1stDer
write_csv(kssl.subset.SNVplusSG1stDer, paste0(dir.preprocessed, "KSSL_soilMIRspectra_SNVplusSG1stDer.csv"))