Skip to content

Commit

Permalink
Add R linting (#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
DriesSchaumont authored Dec 6, 2024
1 parent ae6070f commit d9628ee
Show file tree
Hide file tree
Showing 13 changed files with 331 additions and 126 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/viash-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,26 @@ jobs:
pip install ruff
- name: Run Ruff
run: ruff check --output-format=github .

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
packages: any::lintr, any::styler, any::roxygen2
needs: lint, styler

- name: Lint
run: lintr::lint_dir(path = ".")
shell: Rscript {0}
env:
LINTR_ERROR_ON_LINT: true

- name: Style
run: styler::style_dir(dry = "off")
shell: Rscript {0}


# phase 1
list:
Expand Down
18 changes: 17 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.8.1
hooks:
- id: ruff
- id: ruff-format
- id: ruff-format
- repo: local
hooks:
- id: run_styler
name: run_styler
language: r
description: style files with {styler}
entry: "Rscript -e 'styler::style_file(commandArgs(TRUE))'"
files: '(\.[rR]profile|\.[rR]|\.[rR]md|\.[rR]nw|\.[qQ]md)$'
additional_dependencies:
- styler
- knitr
- repo: https://github.com/lorenzwalthert/precommit
rev: v0.4.3
hooks:
- id: lintr
7 changes: 5 additions & 2 deletions src/convert/from_h5mu_to_seurat/run_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ meta <- list(

cat("> Checking whether output is correct\n")

in_h5mu <- paste0(meta[["resources_dir"]], "/10x_5k_anticmv/5k_human_antiCMV_T_TBNK_connect_mms.h5mu")
in_h5mu <- paste0(
meta[["resources_dir"]],
"/10x_5k_anticmv/5k_human_antiCMV_T_TBNK_connect_mms.h5mu"
)
out_rds <- "output.rds"

cat("> Running ", meta[["name"]], "\n", sep = "")
Expand All @@ -36,4 +39,4 @@ expect_equal(sort(names(slot(obj, "assays"))), sort(c("prot", "rna")))
obj_rna <- slot(obj, "assays")$rna
obj_prot <- slot(obj, "assays")$prot

# todo: check whether obj_rna and obj_prot have correct properties
# todo: check whether obj_rna and obj_prot have correct properties
69 changes: 41 additions & 28 deletions src/convert/from_h5mu_to_seurat/script.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,78 @@ library(hdf5r)

## VIASH START
par <- list(
input = "resources_test/10x_5k_anticmv/5k_human_antiCMV_T_TBNK_connect_mms.h5mu",
input = paste0(
"resources_test/10x_5k_anticmv/",
"5k_human_antiCMV_T_TBNK_connect_mms.h5mu"
),
output = "output.rds"
)
## VIASH END


tempfile <- tempfile(fileext=".h5mu")
file.copy(par$input, tempfile)
temp_h5mu <- tempfile(fileext = ".h5mu")
file.copy(par$input, temp_h5mu)

delete_modality <- function(open_h5, modality_path) {
open_h5$link_delete(modality_path)
mod_name <- sub("/mod/", "", modality_path)
if ("mod-order" %in% names(h5attributes(open_h5[["mod"]]))) {
current_attributes <- h5attributes(open_h5[["mod"]])$`mod-order`
current_attributes <- current_attributes[current_attributes != mod_name]
h5attr(open_h5[["mod"]], "mod-order") <- current_attributes
if ("mod-order" %in% names(hdf5r::h5attributes(open_h5[["mod"]]))) {
current_attributes <- hdf5r::h5attributes(open_h5[["mod"]])$`mod-order`
current_attributes <- current_attributes[current_attributes != mod_name]
hdf5r::h5attr(open_h5[["mod"]], "mod-order") <- current_attributes
}
for (obj_prefix in c("obsm/", "varm/", "varmap/", "obsmap/")) {
obj_path = paste0(obj_prefix, mod_name)
if (existsGroup(open_h5, obj_path)) {
open_h5$link_delete(obj_path)
obj_path <- paste0(obj_prefix, mod_name)
if (hdf5r::existsGroup(open_h5, obj_path)) {
open_h5$link_delete(obj_path)
}
}

}
}

open_file <- H5File$new(temp_h5mu, mode = "r+")
modalities <- list.groups(open_file[["mod"]],
full.names = TRUE, recursive = FALSE
)
to_delete <- c()

open_file <- H5File$new(tempfile, mode="r+")
modalities <- list.groups(open_file[["mod"]], full.names = TRUE, recursive = FALSE)
to_delete = c()
determine_matrix_dims <- function(dataset, indexpointers, indices, rowwise) {
if ("shape" %in% hdf5r::h5attr_names(dataset)) {
x_dims <- hdf5r::h5attr(dataset, "shape")
} else {
x_dims <- c(length(indexpointers) - 1, max(indices) + 1)
if (rowwise) {
x_dims <- rev(x_dims)
}
}
return(x_dims)
}
for (modality_path in modalities) {
dataset <- open_file[[modality_path]][["X"]]
dataset_names <- names(dataset)
if ("data" %in% dataset_names && "indices" %in% dataset_names && "indptr" %in% dataset_names) {
if (
"data" %in% dataset_names &&
"indices" %in% dataset_names &&
"indptr" %in% dataset_names
) {
indexpointers <- dataset[["indptr"]]$read()
indices <- dataset[["indices"]]$read()
rowwise <- FALSE
if ("encoding-type" %in% h5attr_names(dataset)) {
rowwise <- h5attr(dataset, "encoding-type") == "csr_matrix"
}
if ("shape" %in% h5attr_names(dataset)) {
X_dims <- h5attr(dataset, "shape")
} else {
X_dims <- c(length(indexpointers) - 1, max(indices) + 1)
if (rowwise) {
X_dims <- rev(X_dims)
}
}
if (X_dims[2] < 1) {
x_dims <- determine_matrix_dims(dataset, indexpointers, indices, rowwise)
if (x_dims[2] < 1) {
delete_modality(open_file, modality_path)
}
} else if (dataset$dims[1] < 1){
} else if (dataset$dims[1] < 1) {
delete_modality(open_file, modality_path)
}
}

open_file$close_all()

cat("Reading input file\n")
obj <- ReadH5MU(tempfile)
obj <- ReadH5MU(temp_h5mu)

cat("Writing output file\n")
saveRDS(obj, file = par$output, compress = TRUE)
saveRDS(obj, file = par$output, compress = TRUE)
11 changes: 8 additions & 3 deletions src/files/make_params/script.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ paths <- gsub("^/viash_automount", "", paths)

cat("> Checking whether basenames are unique\n")
i <- par$n_basename_id
maxi <- strsplit(paths, "/") %>% map_int(length) %>% max
maxi <- strsplit(paths, "/") %>%
map_int(length) %>%
max()

regex <- paste0(".*/(", paste(rep("[^/]+/", i), collapse = ""), "[^/]*)$")
ids <- gsub("/", "_", gsub(regex, "\\1", paths))
Expand All @@ -43,10 +45,13 @@ cat("\n")

while (i < maxi && any(duplicated(ids))) {
i <- i + 1
cat("Duplicated ids detected, combining with ", i, " dirnames in an attempt to get unique ids.\n")
cat(
"Duplicated ids detected, combining with ", i,
" dirnames in an attempt to get unique ids.\n"
)
regex <- paste0(".*/(", paste(rep("[^/]+/", i), collapse = ""), "[^/]*)$")
ids <- gsub("/", "_", gsub(regex, "\\1", paths))

cat("> Printing first five rows\n")
print(tibble(id = ids, path = paths) %>% head(5))
cat("\n")
Expand Down
12 changes: 8 additions & 4 deletions src/genetic_demux/freemuxlet/script.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ argmap <- c(

for (arg in names(argmap)) {
if (!is.null(par[[arg]])) {
if (arg %in% c("aux_files", "keep_init_missing", "randomize_singlet_score")) {
if (toupper(par[[arg]]) == TRUE)
if (arg %in% c(
"aux_files", "keep_init_missing",
"randomize_singlet_score"
)) {
if (toupper(par[[arg]]) == TRUE) {
cmd <- c(cmd, argmap[[arg]])
}else {
}
} else {
cmd <- c(cmd, argmap[[arg]], par[[arg]])
}
}
Expand Down Expand Up @@ -87,4 +91,4 @@ freemuxlet_assign <- res2 %>% select(cell = BARCODE, donor_id)
readr::write_csv(
freemuxlet_assign,
paste0(par$output, "/cell_annotation.csv")
)
)
2 changes: 1 addition & 1 deletion src/integrate/harmony/script.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ harmony_embedding <- HarmonyMatrix(
rna_data$obsm[[par$obsm_output]] <- harmony_embedding

## Save as h5mu
data$write(par$output, compression=par$output_compression)
data$write(par$output, compression = par$output_compression)
Loading

0 comments on commit d9628ee

Please sign in to comment.