Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #35 from /issues/5
Browse files Browse the repository at this point in the history
Implement regularize
  • Loading branch information
shntnu authored Mar 27, 2020
2 parents 286f41f + 84aa3ce commit 2fc846c
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ LazyData: true
Suggests:
knitr,
rmarkdown,
testthat
testthat (>= 2.1.0)
Imports:
cytominer,
docopt(>= 0.4.5),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export(aggregate)
export(annotate)
export(normalize)
export(preselect)
export(regularize)
export(sample)
importFrom(magrittr,"%<>%")
importFrom(magrittr,"%>%")
Expand Down
57 changes: 57 additions & 0 deletions R/regularize.R
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)

}

}
2 changes: 1 addition & 1 deletion inst/scripts/cytotools_annotate
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extends <- methods::extends
'annotate
Usage:
annotate.R -b <id> -p <id> [-c <str> -d -j <file> -m <str> -o <file> -w <file>]
cytotools_annotate -b <id> -p <id> [-c <str> -d -j <file> -m <str> -o <file> -w <file>]
Options:
-h --help Show this screen.
Expand Down
2 changes: 1 addition & 1 deletion inst/scripts/cytotools_normalize
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extends <- methods::extends
'normalize
Usage:
cytotools_normalize [-i <file> -o <file> -s <query> -g -q <file> -c <str> -m <str> -r <str> -b <id> -p <id> -w <dir> -j <str> -k <str> -l <str>]
cytotools_normalize [-i <file> -o <file> -s <query> -g -q <file> -c <str> -m <str> -r <str> -b <id> -p <id> -w <dir> -j <str> -k <str> -l <str>]
Options:
-h --help Show this screen.
Expand Down
2 changes: 1 addition & 1 deletion inst/scripts/cytotools_preselect
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extends <- methods::extends
'preselect
Usage:
preselect.R -i <file> -r <list> [-b <id>] [-n <n>] [-s <query>] [-c <n>] [-w <dir>]
cytotools_preselect -i <file> -r <list> [-b <id>] [-n <n>] [-s <query>] [-c <n>] [-w <dir>]
options:
-h --help Show this screen.
Expand Down
19 changes: 19 additions & 0 deletions inst/scripts/cytotools_regularize.R
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"]])
2 changes: 1 addition & 1 deletion inst/scripts/cytotools_sample
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extends <- methods::extends
'sample
Usage:
sample -b <id> -f <pattern> [-n <n>] -o <file> [-w dir]
cytotools_sample -b <id> -f <pattern> [-n <n>] -o <file> [-w dir]
Options:
-h --help Show this screen.
Expand Down
16 changes: 16 additions & 0 deletions man/regularize.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions tests/testthat/test-regularize.R
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)

})

0 comments on commit 2fc846c

Please sign in to comment.