-
Notifications
You must be signed in to change notification settings - Fork 0
/
IPTdwc.R
304 lines (212 loc) · 8.47 KB
/
IPTdwc.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
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
# Clean CSVs for DwC
# 1 - run the appropriate specimen "IPT" report from EMu
# 2 - add the output 'ecatalog.csv' to this repo's data01raw/iptSpec/ directory
# 3 - run this IPTdwc.R script
library("readr")
library("tidyr")
library("dplyr")
if(!dir.exists("data01raw/iptSpec")) {
dir.create("data01raw/iptSpec", recursive = T)
print("A 'data01raw/iptSpec' directory was newly created")
print("-- Please add your 'ecatalog.csv' to the 'iptSpec' directory")
}
#### Import CSVs ####
input_file <- "data01raw/iptSpec/ecatalog.csv"
input_encoding <- guess_encoding(input_file, n_max = 1000)
cat <- read_csv(input_file,
guess_max = 1000000,
locale = readr::locale(encoding = input_encoding$encoding[1]))
# # NOTE - make sure file encoding is properly imported
# # IF grepl("Ã", cat[1:NCOL(cat)]) > 0 ), REIMPORT
#### Setup Cleanup Functions: ####
# # Function to check & fix YYYY-MM-DD date fields to ISO date-format
date_fixer <- function (df_to_fix=cat,
column_to_fix,
backup_column) {
if (nchar(backup_column) > 0) {
if (!column_to_fix %in% colnames(df_to_fix)) {
if (backup_column %in% colnames(df_to_fix)) {
df_to_fix[[column_to_fix]] <- df_to_fix[[backup_column]]
}
else {
print(paste("Warning - Input dataframe",
df_to_fix, "missing 'column_to_fix'",
column_to_fix, "or 'backup_column'",
backup_column))
}
}
}
if (column_to_fix %in% colnames(df_to_fix)) {
# if (NROW(cat$DarDateLastModified) > 0) {
print(paste0("Converting dates for '",
column_to_fix,
"' field..."))
# Add leading zero for month
df_to_fix[[column_to_fix]] <- gsub("(^\\d{4}\\-)(\\d{1}\\-)",
"\\10\\2",
df_to_fix[[column_to_fix]])
# Add leading zero for day
df_to_fix[[column_to_fix]] <- gsub("(^\\d+\\-\\d+\\-)(\\dT)",
"\\10\\2",
df_to_fix[[column_to_fix]])
# Change suffix from timezone to UTF-relative
df_to_fix[[column_to_fix]] <- gsub("CMT$",
"-0600",
df_to_fix[[column_to_fix]])
# If need to strip seconds & milliseconds, uncomment next line & rerun:
df_to_fix[[column_to_fix]] <- gsub("(T\\d+\\:\\d+)(\\:\\d{1,2}\\.\\d+)",
"\\1",
df_to_fix[[column_to_fix]])
return(df_to_fix[[column_to_fix]])
} else {
print(paste("Warning - Input dataframe",
df_to_fix, "missing 'column_to_fix'",
column_to_fix, "or 'backup_column'",
backup_column))
}
}
# Function to fix hard-coded EMu DarBasisOfRecord
basis_fixer <- function (df_to_fix=cat,
column_to_fix,
backup_column) {
if (nchar(backup_column) > 0) {
if (!column_to_fix %in% colnames(df_to_fix)) {
if (backup_column %in% colnames(df_to_fix)) {
df_to_fix[[column_to_fix]] <- df_to_fix[[backup_column]]
}
else {
print(paste("Warning - For basis_fixer(), input dataframe",
df_to_fix, "missing 'column_to_fix'",
column_to_fix, "or 'backup_column'",
backup_column))
}
}
}
if (column_to_fix %in% colnames(df_to_fix)) {
# Align EMu DarBasisOfRecord-values to dwc:basisOfRecord
# http://rs.tdwg.org/dwc/dwctype.htm
print(paste0("Checking/Fixing dwc:basisOfRecord values in '",
column_to_fix,
"' field..."))
# Remove spaces, if any
df_to_fix[[column_to_fix]] <- gsub("\\s+",
"",
df_to_fix[[column_to_fix]])
# Fix overly general terms
if (df_to_fix[[column_to_fix]][1] == "Specimen") {
if (!"collectionCode" %in% colnames(df_to_fix)) {
if (!"CatCatalog" %in% colnames(df_to_fix)) {
print(paste("Warning - For basis_fixer(), input dataframe",
df_to_fix,
"missing 'collectionCode' or 'CatCatalog'. ",
"Cannot fix dwc:basisOfRecord"))
return(df_to_fix[[column_to_fix]])
} else {
df_to_fix$collectionCode <- df_to_fix$CatCatalog
}
}
if (grepl("Paleo|Fossil\\.*", df_to_fix$collectionCode[1]) > 0) {
df_to_fix[[column_to_fix]] <- "FossilSpecimen"
} else {
df_to_fix[[column_to_fix]] <- "PreservedSpecimen"
}
}
return(df_to_fix[[column_to_fix]])
} else {
print(paste("Warning - For basis_fixer(), input dataframe",
df_to_fix, "missing 'column_to_fix'",
column_to_fix, "or 'backup_column'",
backup_column))
}
}
# Function to check & replace carriage returns
piper <- function (x) {
x[1:NCOL(x)] <- sapply(x[1:NCOL(x)],
function (y) gsub("\\n|\\r", "|", y))
return(x)
}
#### Prep Data-fields ####
# Prep ColDateVisitedFrom if it's missing
if (!'ColDateVisitedFrom' %in% colnames(cat)) {
if (!NA %in% match("year|month|day", colnames(cat))) {
cat$ColDateVisitedFrom <- paste0(cat$year,
"-", cat$month,
"-", cat$day)
}
else {
if (!'eventDate' %in% colnames(cat)) {
cat$ColDateVisitedFrom <- paste0(cat$DarYearCollected,
"-", cat$DarMonthCollected,
"-", cat$DarDayCollected)
}
else {
cat$ColDateVisitedFrom <- cat$eventDate
}
}
}
cat$ColDateVisitedFrom <- gsub('\\-\\-.*', '-', cat$ColDateVisitedFrom)
cat$ColDateVisitedFrom <- gsub('\\-$', '', cat$ColDateVisitedFrom)
#### Fix ISO dates ####
cat$modified <- date_fixer(cat, "modified", "DarDateLastModified")
cat$eventDate <- date_fixer(cat, "eventDate", "ColDateVisitedFrom")
# #### Fix EMu values for dwc:basisOfRecord ####
# cat$basisOfRecord <- basis_fixer(cat, "basisOfRecord", "DarBasisOfRecord")
if ("DarBasisOfRecord" %in% colnames(cat)) {
if (!"basisOfRecord" %in% colnames(cat)) {
cat$basisOfRecord <- cat$DarBasisOfRecord
}
}
#### Fix carriage returns ####
cat2 <- piper(cat)
#### Flag duplicated GUIDs ####
if (NROW(cat2) > 1000) {
print(paste("Counting GUIDs in", NROW(cat2),
"rows -- May take a minute..."))
}
guid_check <- cat2
# Check/Fix "occurrenceId" capitalization
colnames(guid_check)[colnames(guid_check)=="occurrenceID"] <- "occurrenceId"
if (!"occurrenceId" %in% colnames(guid_check)) {
# Map the 1st GUID column to occurrenceId if not already
guid_check$occurrenceId <-
guid_check[[colnames(guid_check)[grepl("DarGlobalUniqueIdentifier",
colnames(guid_check))>0][1]]]
}
guids <- dplyr::count(guid_check, occurrenceId)
guids_dups <- guids[guids$n > 1,]
cat_dups <- merge(guid_check[,c("irn","occurrenceId")], guids_dups,
by="occurrenceId",
all.y = TRUE)
if (NROW(cat_dups) > 0) {
cat_dups <- unique(cat_dups[,c("irn","occurrenceId","n")])
# Also check duplicated irn's
# (If a record in a reported dataset is edited while that report runs,
# that record's irn may be duplicated in the output...)
re_check <- dplyr::count(cat_dups, occurrenceId)
re_check <- re_check[re_check$n > 1,]
cat_dups <- cat_dups[cat_dups$occurrenceId %in% re_check$occurrenceId,]
}
if (NROW(cat_dups) > 0) {
output_filename <- "dwc_guid_dups.csv"
print(c(paste("Outputting",NROW(re_check), "duplicate GUIDs in",
NROW(cat_dups),"records to: "),
output_filename))
write_csv(cat_dups,
output_filename)
} else {
print(paste("No duplicate GUIDS found in input CSV: ", input_file))
}
#### Output ####
if(!dir.exists("data02output")) {
dir.create("data02output")
print("created 'output' directory")
}
csv_path <- "data02output/"
print("Outputing prepped file here: data02output/Catalog2.csv")
# Write out results
write_csv(cat2, quote = "all",
file=paste0(csv_path, "Catalog2.csv"),
na="")
# write.table(cat2,
# file=paste0(csv_path, "Catalog2.csv"),
# row.names = F, sep=",", na="", col.names = T, quote = TRUE)