-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_raw_data.R
314 lines (285 loc) · 12.1 KB
/
process_raw_data.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
304
305
306
307
308
309
310
311
312
313
314
#' Clean strings by replacing characters and changing case
#'
#' @param str original string
#' @param from old character (to be replaced) [default: "_"]
#' @param to new character (replacement) [default: "-"]
#' @param FUN casing function [default: tolower]
#'
#' @return formated string
#' @export
#'
#' @examples
#' clean("GH_118")
#' clean("GH_118", to = "")
#' clean("GH_118", FUN = toupper)
clean <- function(str, from = "_", to = "-", FUN = tolower) {
str <- gsub(from, to, str)
return(FUN(str))
}
#' Generate labels metadata from raw data
#'
#' @param inputFile filename with or without full path to raw data
#' @param overwrite whether or not to overwrite existing files [default: FALSE]
#' @param cols number of labels per row to display (columns) [default: 6]
#' @param location location on disk to store the index [default: "content/label/"]
#' @param format file format [default: "Rmd"]
#' @param author index's author [default: "Roberto Villegas-Diaz"]
#' @param label_path path to reference aliases [default: ""]
#' @param extra_crumbs_url url for the extra crumbs [default: "/label/]
#'
#' @export
#'
#' @examples
generate_labels <- function(inputFile,
overwrite = FALSE,
cols = 6,
location = "content/label/",
format = "Rmd",
author = "Roberto Villegas-Diaz",
label_path = "",
extra_crumbs_url = "/label/"){
data <- read.csv(inputFile) # Read raw data in CSV format, [House][PlantID]
data <- data[data[, 1] != "", ] # Drop any records with [House] missing
for (i in 1:nrow(data)) {
# Create markdown link
data$Label[i] <- paste0("[", data[i, 2], "](", label_path, clean(data[i, 2]), ")")
# Create new label filename
new_Rmd_name <- paste0(location, clean(data[i, 1]), "-", clean(data[i, 2]), ".", format)
# Verify if file already exists, if so, then rename it by appending timestamp
if (file.exists(new_Rmd_name) && overwrite ) { # Store old files
backup_dir <- paste0(location, ".old/", Sys.Date())
if(!dir.exists(backup_dir))
dir.create(backup_dir, recursive = TRUE)
file.rename(new_Rmd_name, paste0(backup_dir , "/", clean(data[i, 1]), "-", clean(data[i, 2]), ".", format))
}
# Verify if the label file exisis or if overwrite is TRUE
if (!file.exists(new_Rmd_name) || overwrite) {
new_Rmd <- file(new_Rmd_name) # Create reference to file
writeLines(
c("---",
# paste0("title: ", data[i,1], " ", data[i, 2]),
paste0("title: ", data[i, 2]),
paste0("author: ", author),
paste0("slug: '", clean(data[i, 1]), "/", clean(data[i, 2]),"'"),
# "categories:",
# paste0(" - ", clean(data[i, 1], to = "", FUN = toupper)),
# "tags:",
# paste0(" - ", clean(data[i, 2], "_\\d*", "", toupper)),
"aliases:",
paste0(" - ", label_path, tolower(data[i, 1]), "-", tolower(data[i, 2])),
paste0(" - ", label_path, clean(data[i, 1]), "-", clean(data[i, 2])),
paste0(" - ", label_path, clean(data[i, 1], to = ""), "-", clean(data[i, 2], to = "")),
"extra_crumbs:",
paste0(" name: ", clean(data[i, 1], to = "", FUN = toupper)),
paste0(" path: ", extra_crumbs_url, clean(data[i, 1])),
"---",
"",
paste0("# **House**: ", data[i, 1]),
paste0("# **Plant ID**: ", data[i, 2]),
paste0("# **Updates**: "),
paste0(" - ", Sys.Date(),": Nothing new.")
), new_Rmd)
close(new_Rmd)
print(paste0("FILE: ", new_Rmd_name, " created."))
}
else
print(paste0("FILE: ", new_Rmd_name, " already exists."))
}
# Generate Parents indices
for (i in unique(data$House)) {
idx <- data$House == i # Extract indices for current House ID
if (length(idx) < 1) # Verify there are records linked to the House ID
next
labels <- data$Label[idx]
create_index(i, labels, cols,location, format, author, label_path)
print(paste0("Index for ", i, " created."))
}
}
#' Create index for elements with common parent
#'
#' @param parent_id parent identifier, GH_XXX
#' @param labels labels in markdown URL format, [LABEL](label)
#' @param cols number of labels per row to display (columns) [default: 6]
#' @param location location on disk to store the index [default: "content/label/"]
#' @param format file format [default: "Rmd"]
#' @param author index's author [default: "Roberto Villegas-Diaz"]
#' @param label_path path to reference aliases [default: ""]
#' @param title title for the page [default: parent_id]
#'
#' @export
#'
#' @examples
#' create_index("GH_118", c("[F2_002](f2-002)", "[F2_003](f2-003)"), format = "txt")
create_index <- function(parent_id,
labels,
cols = 6,
location = "content/label/",
format = "Rmd",
author = "Roberto Villegas-Diaz",
label_path = "",
title = NA) {
# Check if title is NA
if (is.na(title)) {
title <- clean(parent_id, to = "", FUN = toupper)
}
# Create new house index name
# new_index_Rmd_name <- paste0(location, "/", clean(parent_id), "/index.", format)
new_index_Rmd_name <- paste0(location, "/", clean(parent_id), ".", format)
# Create directory for index
# if(!dir.exists(paste0(location, "/", clean(parent_id)))){
# dir.create(paste0(location, "/", clean(parent_id)), recursive = TRUE)
# }
# Create empty old labels
old_labels <- ""
# Verify if the file exists, if so, then rename it by appending timestamp
if (file.exists(new_index_Rmd_name)) {
# Read contents of old file
old_index_index <- file(new_index_Rmd_name)
old_index_index_contents <- readLines(old_index_index)
close(old_index_index)
# Parse contents, lookup for specific line displaying the knitr::kable command
old_labels_idx <- grep("knitr::kable", old_index_index_contents) + 1
old_labels <- trimws(unlist(strsplit(old_index_index_contents[old_labels_idx], ",")))
old_labels <- gsub("\'", "", old_labels)
old_labels <- old_labels[old_labels != ""]
backup_dir <- paste0(location, ".old/", Sys.Date())
if(!dir.exists(backup_dir))
dir.create(backup_dir, recursive = TRUE)
# Rename old file
file.rename(new_index_Rmd_name, paste0(backup_dir , "/", clean(parent_id), ".", format))
}
# Combine new labels with old ones and keep unique only
labels <- unique(sort(c(labels, old_labels)))
# Verify that the number of labels is multiple of the number of columns
for(i in 1:(cols - length(labels) %% cols))
labels <- c(labels, "")
# Create reference to file
new_index_Rmd <- file(new_index_Rmd_name)
# Create file
writeLines(
c("---",
paste0("author: ", author),
"tags:",
" - index",
paste0("title: ", title),
"aliases:",
paste0(" - ", label_path, tolower(parent_id)),
paste0(" - ", label_path, clean(parent_id)),
paste0(" - ", label_path, clean(parent_id, to = "")),
"---",
"",
"```{css, echo = FALSE}",
".id_links td {",
" padding-right: 20px !important;",
"}",
"",
".split-content {",
" min-width: 600px !important;",
"}",
"```",
"",
"```{r, echo = FALSE}",
"knitr::kable(matrix(c(",
paste0("'", labels, "'", collapse = ", "),
paste0("), ncol = ", cols, ", byrow = TRUE), 'html', booktabs = TRUE, table.attr='class=\"id_links\"')"),
"```"
),
new_index_Rmd
)
# Close file
close(new_index_Rmd)
}
csv2dbf <- function(filename) {
data <- read.csv(filename)
colnames(data) <- c("HOUSE", "PLANTID")
foreign::write.dbf(data, gsub(".csv", ".dbf", filename))
}
#' Generate metadata from raw data for genotypes
#'
#' @param inputFile filename with or without full path to raw data
#' @param overwrite whether or not to overwrite existing files [default: FALSE]
#' @param cols number of labels per row to display (columns) [default: 6]
#' @param location location on disk to store the index [default: "content/genotype/"]
#' @param format file format [default: "Rmd"]
#' @param author index's author [default: "Roberto Villegas-Diaz"]
#' @param label_path path to reference aliases [default: ""]
#' @param extra_crumbs_url url for the extra crumbs [default: "/genotype/]
#'
#' @export
#'
#' @examples
generate_genotypes <- function(inputFile,
overwrite = FALSE,
cols = 6,
location = "content/genotype/",
format = "Rmd",
author = "Roberto Villegas-Diaz",
label_path = "",
extra_crumbs_url = "/genotype/") {
data <- read.csv(inputFile) # Read raw data in CSV format, [Parents][ID]
data <- data[data[, 1] != "", ] # Drop any records with [Parents] missing
data <- apply(data, 2, gsub, pattern = " ", replacement = "") # Remove blanks
data <- data.frame(data)
for (i in 1:nrow(data)) {
# Create markdown link
data$Label[i] <- paste0("[", data[i, 2], "](", label_path, clean(data[i, 2]), ")")
# Create new label filename
# new_Rmd_path <- paste0(location, clean(data[i, 1]))
new_Rmd_path <- paste0(location)
new_Rmd_name <- paste0(new_Rmd_path, clean(data[i, 1]), "-", clean(data[i, 2]), ".", format)
# Create directory for parents
if(!dir.exists(new_Rmd_path)) {
dir.create(new_Rmd_path, recursive = TRUE)
}
# Verify if file already exists, if so, then rename it by appending timestamp
if (file.exists(new_Rmd_name) && overwrite ) { # Store old files
backup_dir <- paste0(location, ".old/", Sys.Date())
if(!dir.exists(backup_dir))
dir.create(backup_dir, recursive = TRUE)
file.rename(new_Rmd_name, paste0(backup_dir , "/", clean(data[i, 1]), "-", clean(data[i, 2]), ".", format))
}
# Verify if the label file exisis or if overwrite is TRUE
if (!file.exists(new_Rmd_name) || overwrite) {
new_Rmd <- file(new_Rmd_name) # Create reference to file
writeLines(
c("---",
# paste0("title: ", gsub("x", "`\\\\times`", data[i,1], TRUE), " ", data[i, 2]),
paste0("title: ", data[i, 2]),
paste0("author: ", author),
paste0("slug: '", clean(data[i, 1]), "/", clean(data[i, 2]), "'"),
#"categories:",
#paste0(" - ", clean(data[i, 1], to = "", FUN = toupper)),
#"tags:",
#paste0(" - ", clean(data[i, 2], "_\\d*", "", toupper)),
"aliases:",
# paste0(" - ", "genotype/", tolower(data[i, 1]), "/", clean(data[i, 2])),
# paste0(" - ", "genotype/", tolower(data[i, 1]), "/", clean(data[i, 2], to = "")),
paste0(" - ", label_path, tolower(data[i, 1]), "-", tolower(data[i, 2])),
paste0(" - ", label_path, clean(data[i, 1]), "-", clean(data[i, 2])),
paste0(" - ", label_path, clean(data[i, 1], to = ""), "-", clean(data[i, 2], to = "")),
"extra_crumbs:",
paste0(" name: ", gsub("x", "`\\\\times`", data[i, 1], TRUE)),
paste0(" path: ", extra_crumbs_url, clean(data[i, 1])),
"---",
"",
paste0("# **Parents**: ", gsub("x", "`$\\\\times$`", data[i, 1], TRUE)),
paste0("# **ID**: ", data[i, 2]),
paste0("# **Updates**: "),
paste0(" - ", Sys.Date(),": Nothing new.")
), new_Rmd)
close(new_Rmd)
print(paste0("FILE: ", new_Rmd_name, " created."))
}
else
print(paste0("FILE: ", new_Rmd_name, " already exists."))
}
# Generate Parents indices
for (i in unique(data[,1])) {
idx <- data[,1] == i # Extract indices for current House ID
if (length(idx) < 1) # Verify there are records linked to the House ID
next
labels <- data$Label[idx]
create_index(i, labels, cols, location, format, author, label_path, gsub("x", "`\\\\times`", i, TRUE))
print(paste0("Index for ", i, " created."))
}
}