Skip to content

Commit 5a53952

Browse files
authored
Error handling (#21)
* catch job submit errors * na to string * Update display.R * error handling and validation * Update DESCRIPTION
1 parent 9482db9 commit 5a53952

8 files changed

Lines changed: 96 additions & 7 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: CyteTypeR
22
Title: CyteType for R
3-
Version: 0.0.1
3+
Version: 0.0.2
44
Description: CyteTypeR is the R version of CyteType python package.'
55
Authors@R:
66
person("Nygen Analytics AB", , ,"contact@nygen.io", role = c("aut", "cre"))

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method(print,cytetype_api_error)
34
export(CyteTypeR)
45
export(GetResults)
56
export(PrepareCyteTypeR)
7+
export(cytetype_api_error)
68
importFrom(Matrix,rowSums)
79
importFrom(Seurat,AddMetaData)
810
importFrom(Seurat,Embeddings)

R/client.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
.submit_job <- function(payload, api_url, auth_token = NULL){
33

44
submit_url <- file.path(api_url, "annotate")
5-
log_info("Submitting job to {submit_url}")
5+
log_info("Submitting job to API {api_url}")
66

77
tryCatch({
88
# Validate inputs
@@ -13,7 +13,7 @@
1313
# Build request
1414
req <- request(submit_url) |>
1515
req_method("POST") |>
16-
req_body_json(payload) |>
16+
req_body_json(payload, na = "string") |>
1717
req_headers("Content-Type" = "application/json") |>
1818
req_timeout(60)
1919

@@ -206,7 +206,7 @@
206206
for (i in seq_len(duration * 2)) {
207207
Sys.sleep(0.5)
208208
if (show_progress){
209-
.display_cluster_status_cli(cluster_status,is_final=FALSE,i)
209+
.display_cluster_status_cli(cluster_status, is_final=FALSE, spinner_frame=i)
210210
}
211211

212212
}

R/cytetype.R

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ PrepareCyteTypeR <- function(obj,
8181
sorted_clusters <- sort(unique(obj[[group_key, drop = TRUE]]))
8282
cluster_map <- setNames(as.character(1:length(sorted_clusters)), sorted_clusters)
8383

84+
.validate_marker_table(marker_table,sorted_clusters)
85+
8486
if (aggregate_metadata){
8587
print("Aggregating metadata...")
8688
group_metadata <- .aggregate_metadata(obj,group_key)
@@ -89,6 +91,8 @@ PrepareCyteTypeR <- function(obj,
8991

9092
} else{group_metadata <- list()
9193
}
94+
print(paste("Preparing marker genes with top",n_top_genes,"genes..."))
95+
9296
marker_genes <- marker_table %>%
9397
group_by(cluster) %>%
9498
dplyr::filter(avg_log2FC > 1) %>%
@@ -240,9 +244,17 @@ CyteTypeR <- function(obj,
240244
if (save_query){
241245
write_json(query_list, path = query_filename, auto_unbox = TRUE, pretty = TRUE)
242246
}
247+
## NA value check on all data before submitting job
248+
243249

244250
# Job submission
245-
job_id <- .submit_job(query_list, api_url, auth_token)
251+
tryCatch({
252+
job_id <- .submit_job(query_list, api_url, auth_token)
253+
},
254+
error = function(e) {
255+
stop("Job submission failed: ", conditionMessage(e))
256+
}
257+
)
246258

247259
# Save job details
248260
report_url <- file.path(api_url, 'report',job_id)

R/display.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
# Visuals for cluster progress status
55

6-
.display_cluster_status_cli <- function(cluster_status, job_id, is_final = FALSE, spinner_frame = 0) {
6+
.display_cluster_status_cli <- function(cluster_status, is_final = FALSE, spinner_frame) {
77
if (length(cluster_status) == 0) return()
88

99
# Status symbols and colors

R/errors.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#' Custom API Error
2+
#'
3+
#' @param message Error message
4+
#' @param call Call that generated the error
5+
#' @return An error object of class cytetype_api_error
6+
#' @export
7+
cytetype_api_error <- function(message, call = NULL) {
8+
structure(
9+
list(message = message, call = call),
10+
class = c("cytetype_api_error", "error", "condition")
11+
)
12+
}
13+
14+
#' @export
15+
print.cytetype_api_error <- function(x, ...) {
16+
cat("CyteType API Error: ", x$message, "\n")
17+
}

R/seurat_helpers.R

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
){
1616
gene_names <- rownames(seurat_obj)
1717
clusters <- seurat_obj[[group_key]][[group_key]]
18-
unique_clusters <- levels(clusters)
18+
unique_clusters <- unique(clusters)
1919

2020
pcent <- list()
2121
n_genes <- dim(seurat_obj)[1]
@@ -165,6 +165,45 @@
165165
}
166166
}
167167

168+
169+
.validate_marker_table <- function(marker_table, sorted_clusters){
170+
log_info("Checking markers table...")
171+
df_like_classes <- c("data.frame", "tbl_df", "tbl", "data.table")
172+
if (inherits(marker_table, df_like_classes)){
173+
log_info(paste("Provided markers data is a dataframe", cli::symbol$tick))
174+
}
175+
176+
if (any(is.na(marker_table))){
177+
log_info("NA value(s) found in the table")
178+
}
179+
else{ log_info(paste("No NA values", cli::symbol$tick))}
180+
181+
table_cols <- names(marker_table)
182+
if ("cluster" %in% table_cols){
183+
log_info(paste("'cluster' column found in marker table", cli::symbol$tick))
184+
}
185+
else{log_info("cluster column not found in marker table, rename your column to 'cluster' if its name is different" )}
186+
187+
# gene symbol column exist?
188+
# has gene symbols?
189+
if ("gene" %in% table_cols){
190+
gene_sym_pattern <- "^[A-Z]([A-Z0-9-]+|[a-z0-9-]+)$"
191+
if (is.null(grepl(gene_sym_pattern,marker_table$gene))){
192+
log_warn("Not gene symbols?")
193+
}
194+
else {log_info(paste("Found gene symbols", cli::symbol$tick))}
195+
}
196+
# cluster labels == seurat obj labels?
197+
if (setequal(sorted_clusters, as.vector(marker_table$cluster))){
198+
log_info(paste("Correct cluster labels", cli::symbol$tick))
199+
}
200+
else{
201+
log_error("Please check if cluster labels are consistent between marker table and seurat obj!")
202+
}
203+
204+
log_info(paste("Markers check: done", cli::symbol$tick))
205+
}
206+
168207
# Transform CyteType Results for Seurat Integration
169208

170209
.transform_results_seurat <- function(raw_results = NULL, job_id = NULL, filename = NULL, cluster_map = NULL){

man/cytetype_api_error.Rd

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)