This repository has been archived by the owner on Sep 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Implement regularize
- Loading branch information
Showing
10 changed files
with
163 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#' Regularize backend file | ||
#' | ||
#' \code{regularize} regularizes backend file. | ||
#' | ||
#' @param input_file Input file. CSV or SQLite only. | ||
#' @param output_file Output file. Same format as input_file. | ||
#' @importFrom magrittr %>% | ||
#' @importFrom magrittr %<>% | ||
#' @export | ||
regularize <- function(input_file, output_file) { | ||
|
||
input_extension <- tools::file_ext(input_file) | ||
|
||
output_extension <- tools::file_ext(output_file) | ||
|
||
if(input_extension != output_extension) { | ||
stop("Input and output filetypes are different.") | ||
} | ||
|
||
if(!(input_extension %in% c("csv", "sqlite"))) { | ||
stop("Unsupported filetype. Only csv or sqlite is supported.") | ||
} | ||
|
||
if(input_extension == "sqlite") { | ||
file.copy(input_file, output_file) | ||
|
||
db <- DBI::dbConnect(RSQLite::SQLite(), output_file) | ||
|
||
image <- DBI::dbGetQuery(db, "SELECT * from Image;") | ||
|
||
futile.logger::flog.info( | ||
"Stripping Image_ from column names of Image table...") | ||
|
||
image %<>% | ||
setNames(names(image) %>% | ||
stringr::str_remove_all("^Image_")) | ||
|
||
DBI::dbRemoveTable(db, "Image") | ||
|
||
DBI::dbWriteTable(db, "Image", image) | ||
|
||
image <- DBI::dbGetQuery(db, "SELECT * from Image;") | ||
|
||
DBI::dbDisconnect(db) | ||
} else { | ||
|
||
input_data <- suppressMessages(readr::read_csv(input_file)) | ||
|
||
input_data %<>% | ||
setNames(names(input_data) %>% | ||
stringr::str_replace_all("^Image_Metadata", "Metadata")) | ||
|
||
input_data %>% readr::write_csv(output_file) | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/bin/env Rscript | ||
|
||
# https://github.com/tidyverse/dplyr/issues/1760 | ||
extends <- methods::extends | ||
|
||
'regularize | ||
Usage: | ||
cytotools_regularize -i <file> -o <file> | ||
Options: | ||
-h --help Show this screen. | ||
-i <file> --input=<file> Input file. CSV or SQLite only. | ||
-o <file> --output=<file> Output file. Same format as input_file' -> doc | ||
|
||
opts <- docopt::docopt(doc) | ||
|
||
cytotools::regularize(input_file = opts[["input"]], | ||
output_file = opts[["output"]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
context("regularize") | ||
|
||
test_that("`regularize` regularizes SQLite file", { | ||
futile.logger::flog.threshold(futile.logger::WARN) | ||
|
||
output_file <- tempfile("SQ00015116_regularized", fileext = ".sqlite") | ||
|
||
input_file <- | ||
system.file( | ||
"extdata", "backend", "batch0", "SQ00015116", | ||
"SQ00015116.sqlite", | ||
package = "cytotools" | ||
) | ||
|
||
regularize(input_file, output_file) | ||
|
||
db <- DBI::dbConnect(RSQLite::SQLite(), input_file) | ||
|
||
input_image <- DBI::dbGetQuery(db, "SELECT * from Image;") | ||
|
||
DBI::dbDisconnect(db) | ||
|
||
db <- DBI::dbConnect(RSQLite::SQLite(), output_file) | ||
|
||
output_image <- DBI::dbGetQuery(db, "SELECT * from Image;") | ||
|
||
DBI::dbDisconnect(db) | ||
|
||
input_image %<>% | ||
setNames(names(input_image) %>% | ||
stringr::str_remove_all("^Image_")) | ||
|
||
expect_equal(output_image, output_image) | ||
|
||
file.remove(output_file) | ||
|
||
}) | ||
|
||
test_that("`regularize` regularizes CSV file", { | ||
futile.logger::flog.threshold(futile.logger::WARN) | ||
|
||
output_file <- tempfile("SQ00015116_regularized", fileext = ".csv") | ||
|
||
input_file <- | ||
system.file( | ||
"extdata", "backend", "batch0", "SQ00015116", | ||
"SQ00015116.csv", | ||
package = "cytotools" | ||
) | ||
|
||
regularize(input_file, output_file) | ||
|
||
input_data <- readr::read_csv(input_file) | ||
|
||
output_data <- readr::read_csv(output_file) | ||
|
||
input_data %<>% | ||
setNames(names(input_data) %>% | ||
stringr::str_remove_all("^Image_")) | ||
|
||
expect_equal(input_data, output_data) | ||
|
||
file.remove(output_file) | ||
|
||
}) |