forked from GeospatialCentroid/MAP-FRAC-Database
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_explore_clean.Rmd
More file actions
316 lines (248 loc) · 8.56 KB
/
data_explore_clean.Rmd
File metadata and controls
316 lines (248 loc) · 8.56 KB
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
---
title: "Code to clean and explore data"
author: "Caitlin Mothes"
---
# Workflow to clean and prep all data for the shiny app
```{r}
library(tidyverse)
library(readxl)
library(sf)
library(mapview)
```
## Read in datasets
```{r}
# Data from Kaela
sample <- read_xlsx("data/SAMPLE_geospatial_metadata_3.28.24.xlsx") %>%
janitor::clean_names()
genome <- read_xlsx("data/GENOME_geospatial_metadata_02.17.2026.xlsx")
mag_abundance <- read_xlsx("data/MAGs_average_relativeabund_by_basin.xlsx")
mag_cores <- readxl::read_xlsx("data/core_ALL.xlsx")
# Basins and Plays
# Downloaded from here: https://atlas.eia.gov/search?q=plays&source=u.s.%2520energy%2520information%2520administration
play_basin <- read_sf("data/TightOil_ShaleGas_Plays_Lower48_EIA.shp") %>%
st_transform(crs = 4326) %>%
# remove spaces around hyphens for joining and change Denver basin name
mutate(Shale_play = str_replace(Shale_play, " - ", "-"),
Basin = str_replace(Basin, " Basin", ""))
sediment_basin <- read_sf("data/SedimentaryBasins_US_EIA.shp") %>%
mutate(NAME = str_to_title(NAME)) %>%
# need to transform all of these to WGS84
st_transform(crs = 4326)
```
## Explore International Basins
```{r}
bowland <- read_sf("data/BGS_DECC_Bowland_study/topBHU_limit.shp") %>%
mutate(shale_play = "uk", shale_basin = "Bowland Shale") %>%
st_transform(4326)
mapview(bowland)
west_canada <- read_sf("data/wcsb_shapefiles/WCSBAtlasShp/fg0301_py_ll.shp")
mapview(west_canada)
```
Quick map of basins
```{r}
mapview(play_basin) +
mapview(sediment_basin, color = "red")
```
## Sample Explorer Data
Filter plays to join to sample df to get centroids:
```{r}
play_basin_filtered <- play_basin %>%
st_make_valid() %>%
group_by(Shale_play, Basin) %>%
summarise(st_union(geometry)) %>%
ungroup() %>%
filter(Shale_play %in% sample$shale_play) %>%
rename(shale_play = Shale_play, shale_basin = Basin)
```
Create sample layers for app
```{r}
sample_sf <- sample %>%
left_join(play_basin_filtered, by = c("shale_play", "shale_basin")) %>%
st_as_sf() %>%
# convert to points
st_centroid() %>%
# edit lithology typo
mutate(
# make days_since_frack numeric
days_since_frack = na_if(days_since_frack, "N/A|unk") %>%
as.numeric(.),
# edit time series stage for filtering
timeseries_stage = if_else(timeseries_stage == "N/A" | timeseries == "no", "none", timeseries_stage),
#edit salinity values for filtering
#salinity_classification = if_else(salinity_classification == "N/A", NA_character_, salinity_classification),
salinity_conductivity_m_s_cm = if_else(salinity_conductivity_m_s_cm == "not measured", NA_character_, salinity_conductivity_m_s_cm) %>%
as.numeric(.),
# edit 'N/A' values and make numeric
across(contains("perc"), ~if_else(. == "N/A", NA_character_, .) %>% as.numeric(.)))
# create variables for map symbology
sample_app <- sample_sf %>%
st_drop_geometry() %>%
group_by(well_id) %>%
mutate(
# n_samples = n(),
max_days_since_frack = max(days_since_frack, na.rm = TRUE),
min_days_since_frack = min(days_since_frack, na.rm = TRUE),
# change 'Inf' values to 0
max_days_since_frack = if_else(max_days_since_frack == -Inf, 0, max_days_since_frack),
min_days_since_frack = if_else(min_days_since_frack == Inf, 0, min_days_since_frack)
) %>%
distinct(well_id, .keep_all = TRUE) %>%
select(shale_play,
well_id,
#n_samples,
min_days_since_frack,
max_days_since_frack) %>%
right_join(sample_sf, by = c("shale_play", "well_id")) %>%
ungroup() %>%
st_as_sf()
# separate samples from inputs
sample_inputs <- sample_app %>%
filter(sample_type == "input")
sample_app <- sample_app %>%
filter(sample_type != "input")
```
## Genome Explorer Data
### Relative abudance data
Tie abundance data to genome data
```{r}
genome_joined <- mag_abundance %>%
dplyr::select(MAG_ID = MAG, rel_abundance = aveRelAbund, Play, Basin) %>%
left_join(genome, by = c("MAG_ID")) %>%
select(-basin)
```
Clean genome data and join to basin shapefile
```{r}
genome_app <- genome_joined %>%
mutate(across(domain:species, ~str_remove(.x, ".*__")))
```
Visualize for app:
```{r}
# **will need to do this within the app using filtered data for 'genome_clean'
# basins
genome_app %>%
filter(genus == "Thermotoga") %>%
group_by(Basin, species) %>%
summarise(max_rel_abundance = max(rel_abundance)) %>%
group_by(Basin) %>%
summarise(avg_rel_abundance = mean(max_rel_abundance)) %>%
right_join(sediment_basin, by = c("Basin" = "NAME")) %>%
View()
# plays
genome_app %>%
group_by(Basin, Play, domain, phylum, class, order, family, genus, species) %>%
summarise(max_rel_abundance = max(rel_abundance)) %>%
group_by(Basin, Play) %>%
summarise(avg_rel_abundance = mean(max_rel_abundance)) %>%
inner_join(play_basin, by = c("Play" = "Shale_play")) %>%
st_as_sf() %>%
View()
# test creating column for popup
# Old CODE
# basin_genome <- play_basin %>%
# # trailing spaces causing errors in join
# mutate(
# Basin = str_remove_all(Basin, " ") %>%
# str_remove_all(., "Basin"),
# # fix typo
# Lithology = if_else(Lithology == "MIxed Shale & Chalk", "Mixed Shale & Chalk", Lithology)
# ) %>%
# st_make_valid() %>%
# group_by(Basin) %>%
# summarise(geometry = st_union(geometry)) %>%
# ungroup() %>%
# st_make_valid() %>%
# # join to sample
# inner_join(genome_clean, by = "Basin") %>%
# group_by(Basin) %>%
# summarise(n_MAG_samples = n())
```
### Core Data
```{r}
# prep for app
## basins
core_app <- mag_cores %>%
filter(perc_samples_present_per_basin >= 0.50) %>%
group_by(basin) %>%
count() %>%
# join to shapefile
inner_join(sediment_basin, by = c("basin" = "NAME")) %>%
st_as_sf()
mapview(core_app)
```
## Save all data for app
```{r}
#save updated data
save(sample_app, sample_inputs, play_basin, sediment_basin, genome_app, mag_cores, file = "data/app_data_Update.RData")
```
## Timeseries Plot
```{r}
fig_1 <- sample_app %>%
plotly::plot_ly(colors = "Dark2") %>%
add_trace(x = sample_app$days_since_frack,
y = sample_app$well_id,
split = ~sample_app$well_id,
color = ~sample_app$shale_basin,
name = ~sample_app$shale_basin,
legendgroup = ~sample_app$shale_basin,
type = 'scatter',
mode = 'lines+markers',
connectgaps = TRUE) %>%
layout(showlegend = TRUE, yaxis = list(type = "category",
categoryorder = "array", categoryarray = sort(unique(sample_app$shale_basin)))) %>%
# hacky way to get one trace per group in legend
style(showlegend = FALSE, traces = c(1:4, 7:11, 13:16, 17, 21, 22:24, 26, 28))
fig_2 <- sample_app %>%
plotly::plot_ly(colors = "Dark2") %>%
add_trace(x = sample_app$days_since_frack,
y = sample_app$well_id,
split = ~sample_app$well_id,
color = ~sample_app$shale_basin,
name = ~sample_app$shale_basin,
legendgroup = ~sample_app$shale_basin,
type = 'scatter',
mode = 'lines+markers',
connectgaps = TRUE) %>%
layout(showlegend = TRUE, yaxis = list(type = "category",
categoryorder = "array", categoryarray = sort(unique(sample_app$shale_basin)))) %>%
# hacky way to get one trace per group in legend
style(showlegend = FALSE, traces = c(1:4, 7:11, 13:16, 17, 21, 22:24, 26, 28))
subplot(fig_1, fig_2, nrows = 1, shareY = TRUE, margin = 0.02) %>%
layout(xaxis = list(range = c(0, 2500)),
xaxis2 = list(range = c(4500, 5000)))
```
## Well Area Plots
```{r}
# read in data
load("data/app_data_Update.RData")
# function to create and save all plots
area_plot <- function(x, data) {
data %>%
filter(well_id == x) %>%
ggplot(aes(x = days_since_frack, y = value, fill = group)) +
geom_area() +
scale_fill_manual(
values = c("#de3064", "#077969", "#1052ba"),
name = "Type",
labels = c("Acetate Producers", "Methanogens", "Sulfide Producers")
) +
theme_minimal() +
labs(y = "% Relative Abundance", x = "Day")+
theme(text=element_text(size = 16))
ggsave(
paste0("www/", x, "_area_plot.png"),
width = 6,
height = 4,
dpi = 300,
units = "in"
)
}
# arrange/clean well data
plot_data <- sample_app %>%
st_drop_geometry() %>%
select(well_id, days_since_frack, contains("perc")) %>%
pivot_longer(cols = contains("perc"), names_to = "group")
# create and save plot for each well
walk(unique(plot_data$well_id), area_plot, plot_data)
area_plot("AND-1", plot_data)
ggsave("test_area_plot.png", width = 6, height = 4, dpi = 300, units = "in")
```